ArticleslgStudy

computer science

Data dependency

Data dependency is a computer science topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand Data dependency rather than just read about it. In short: A data dependency in computer science is a situation in which a program statement (instruction) refers to the data of a preceding statement. In compiler theory, the technique used to discover data dependencies among statements (or instructions) is called dependence analysis.

Key takeaways

  • Data dependency belongs to computer science; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect Data dependency to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of Data dependency from memory before moving on to harder problems.

Reference excerpt

A data dependency in computer science is a situation in which a program statement (instruction) refers to the data of a preceding statement. In compiler theory, the technique used to discover data dependencies among statements (or instructions) is called dependence analysis.

Description Assuming statement S1 and S2, S2 depends on S1 if:

[I(S1)∩O(S2)]∪[O(S1)∩I(S2)]∪[O(S1)∩O(S2)]≠∅ where:

I(Si) is the set of memory locations read by Si, O(Sj) is the set of memory locations written by Sj, and there is a feasible run-time execution path from S1 to S2. These conditions are called Bernstein's Conditions, named after Arthur J. Bernstein. Three cases exist:

Anti-dependence: I(S1)∩O(S2)≠∅, S1→S2 and S1 reads something before S2 overwrites it Flow (data) dependence: O(S1)∩I(S2)≠∅, S1→S2 and S1 writes before something read by S2 Output dependence: O(S1)∩O(S2)≠∅, S1→S2 and both write the same memory location.

Types

Data hazards Data hazards occur when instructions that exhibit data dependence modify data in different stages of a pipeline. Ignoring potential data hazards can result in race conditions (also termed race hazards). There are three situations in which a data hazard can occur:

read after write (RAW), a true dependency write after read (WAR), an anti-dependency write after write (WAW), an output dependency read after read (RAR), a false dependency Read after read (RAR) is not a hazard case. Consider two instructions i1 and i2, with i1 occurring before i2 in program order.

Read after write (RAW) (i2 tries to read a source before i1 writes to it) A read after write (RAW) data hazard refers to a situation where an instruction refers to a result that has not yet been calculated or retrieved. This can occur because even though an instruction is executed after a prior instruction, the prior instruction has been processed only partly through the pipeline.

Example For example:

i1. R2 <- R5 + R8 i2. R4 <- R2 + R8

The first instruction is calculating a value to be saved in register R2, and the second is going to use this value to compute a result for register R4. However, in a pipeline, when operands are fetched for the 2nd operation, the results from the first have not yet been saved, and hence a data dependency occurs. A data dependency occurs with instruction i2, as it is dependent on the completion of instruction i1.

Write after read (WAR) (i2 tries to write a destination before it is read by i1) A write after read (WAR) data hazard represents a problem with concurrent execution.

Example For example:

i1. R4 <- R1 + R5 i2. R5 <- R1 + R2

In any situation with a chance that i2 may finish before i1 (i.e., with concurrent execution), it must be ensured that the result of register R5 is not stored before i1 has had a chance to fetch the operands.

Write after write (WAW) (i2 tries to write an operand before it is written by i1) A write after write (WAW) data hazard may occur in a concurrent execution environment.

Example For example:

i1. R5 <- R4 + R7 i2. R5 <- R1 + R3

The write back (WB) of i2 must be delayed until i1 finishes executing.

True dependency (read-after-write) A true dependency, also known as a flow dependency or data dependency, occurs when an instruction depends on the result of a previous instruction. A violation of a true dependency leads to a read-after-write (RAW) hazard.

1. A = 3 2. B = A 3. C = B

Instruction 3 is truly dependent on instruction 2, as the final value of C depends on the instruction updating B. Instruction 2 is truly dependent on instruction 1, as the final value of B depends on the instruction updating A. Since instruction 3 is truly dependent upon instruction 2 and instruction 2 is truly dependent on instruction 1, instruction 3 is also truly dependent on instruction 1. Instruction level parallelism is therefore not an option in this example.

Anti-dependency (write-after-read) An anti-dependency occurs when an instruction requires a value that is later updated. A violation of an anti-dependency leads to a write-after-read (WAR) hazard. In the following example, instruction 2 anti-depends on instruction 3 — the ordering of these instructions cannot be changed, nor can they be executed in parallel (possibly changing the instruction ordering), as this would affect the final value of A.

1. B = 3 2. A = B + 1 3. B = 7

Example:

MUL R3,R1,R2 ADD R2,R5,R6

It is clear that there is anti-dependence between these 2 instructions. At first we read R2 then in second instruction we are Writing a new value for it. An anti-dependency is an example of a name dependency. That is, renaming of variables could remove the dependency, as in the next example:

1. B = 3 N. B2 = B 2. A = B2 + 1 3. B = 7

A new variable, B2, has been declared as a copy of B in a new instruction, instruction N. The anti-dependency between 2 and 3 has been removed, meaning that these instructions may now be executed in parallel. Note that there is still a read-after-write dependency: instruction 2 is truly dependent on instruction N, which is truly dependent upon instruction 1. This dependency existed in the original version, where instruction 2 was truly dependent on instruction 1. This dependency cannot be safely removed.

Output dependency (write-after-write) An output dependency occurs when the ordering of instructions will affect the final output value of a variable. A violation of an output dependency leads to an write-after-write (WAW) hazard. In the example below, there is an output dependency between instructions 3 and 1 — changing the ordering of instructions in this example will change the final value of A, thus these instructions cannot be executed in parallel.

1. B = 3 2. A = B + 1 3. B = 7

As with anti-dependencies, output dependencies are name dependencies. That is, they may be removed through renaming of variables, as in the below modification of the above example:

1. B2 = 3 2. A = B2 + 1 3. B = 7

… excerpt ends here. Continue reading the full article.

Worked examples

Example 1 — a first encounter with Data dependency

Start with the simplest possible case. Write down what Data dependency claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In computer science, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to Data dependency before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about Data dependency ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of Data dependency

In research
Data dependency appears in computer science research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses Data dependency in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
Data dependency is common in secondary-school and first-year university syllabi. It links to neighbouring topics Analysis of parallel algorithms, Compilers, so understanding it makes those chapters shorter.
In everyday life
Look for Data dependency outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.
Ask Teacher Smith questions about this articleOpens your AI tutor with a question about “Data dependency” →

How to study Data dependency in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what Data dependency means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain Data dependency out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is Data dependency in simple terms?

A data dependency in computer science is a situation in which a program statement (instruction) refers to the data of a preceding statement. In compiler theory, the technique used to discover data dependencies among statements (or instructions) is called dependence analysis.

Why does Data dependency matter?

Because it connects several computer science ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study Data dependency?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on Data dependency.

Tags

  • Analysis of parallel algorithms
  • Compilers

Keep exploring