3

My book presents a simple example which I'm a bit confused about:

It says, "consider the following program, and assume that the fine-grained atomic actions are reading and writing the variables:"

int y = 0, z = 0;
co x = y+z; // y=1; z=2; oc;

"If x = y + z is implemented by loading a register with y and then adding z to it, the final value of x can be 0,1,2, or 3. "

2? How does 2 work?

Note: co starts a concurrent process and // denote parallel-running statements

Gilles 'SO- stop being evil'
  • 104,111
  • 38
  • 209
  • 254
Caffeinated
  • 11,982
  • 40
  • 122
  • 216

2 Answers2

6

In your program there are two parallel sequences:

Sequence 1: x = y+z;

Sequence 2: y=1; z=2;

The operations of sequence 1 are:

  1. y Copy the value of y into a register.
  2. + z Add the value of z to the value in the register.
  3. x = Copy the value of the register into x.

The operations of sequence 2 are:

  1. y=1; Set the value of y to 1.
  2. z=2; Set the value of z to 2.

These two sequences are running at the same time, though the steps within a sequence must occur in order. Therefore, you can get an x value of '2' in the following sequence:

  1. y=0
  2. z=0
  3. y Copy the value of y into a register. (register value is now '0')
  4. y=1; Set the value of y to 1. (has no effect on the result, we've already copied y to the register)
  5. z=2; Set the value of z to 2.
  6. + z Add the value of z to the value in the register. (register value is now '2')
  7. x = Copy the value of the register into x. (the value of x is now '2')
goto10
  • 4,370
  • 1
  • 22
  • 33
0

Since they are assumed to run in parallel, I think an even simpler case could be y=0, z=2 when the assignment x = y + z occurs.

vishakvkt
  • 864
  • 6
  • 7
  • I'm not familiar with the pseudocode syntax being used, but I took the // to mean that everything on the right runs in sequence but in parallel with everything on the left, so z=2 cannot execute until after y=1. If that wasn't the case I'd expect another // between y=1 and z=2. – goto10 Oct 16 '11 at 06:48
  • the 'co' notation indicates that everything between 'co' and 'oc' are executed in parallel. I remember as it was the notation in the book I had used to learn parallel programming by Greg Andrews, called 'Foundations of Multithreaded, Parallel, and Distributed Programming' – vishakvkt Oct 18 '11 at 11:27
  • Well now you've made me look it up. co/oc indicates a list of sequential programs. // is a delimiter that separates one sequential program from another. In the example given, "x = y+z;" is one sequential program and "y=1; z=2;" is a second sequential program. It is impossible for z=2; in the second sequential program to execute prior to y=1;. See slide #9 at http://www.infj.ulst.ac.uk/~cbdq23/teaching/com577/lectures/com577-lecture2-notation-concurrency-specification.ppt and slide 15 at http://www.docstoc.com/docs/80526532/T-1065600-Concurrent-Programming_4_ which discusses this exact case. – goto10 Oct 18 '11 at 14:03
  • A program with the behavior you describe would be co x = y+z; // y=1; // z=2; oc; – goto10 Oct 18 '11 at 14:06
  • @goto10 Agreed. That seems logical. But in your answer, you mentioned 'y=1' is set but it has no effect. why? Are you talking about cache-memory coherence ? Even so, how will you set y to 1 without actually reading from memory->register, or cache->register and cpu doing its increment? The cpu always looks at the cache first before looking at the memory. – vishakvkt Oct 19 '11 at 02:16
  • We are at a very low granularity already, and actually we could go further. The pdf also I am afraid is not very accurate. It assumes there are two processes running with different address spaces. That is actually a different scenario compared to SMP which is based on threading commonly discussed during program parallelism. The paper is right surely, but its assumptions are somewhat very generic, and no where near the granularity of your answer. – vishakvkt Oct 19 '11 at 02:21
  • The granularity of my answer is what is described by the problem instructions. An addition with assignment is three operations, an assignment is one. There is one addition/assignment and two assignments, therefore five operations. I showed the only way a '2' is possible without making assumptions about cpu architecture that aren't given. – goto10 Oct 19 '11 at 04:29