Difference between revisions of "CSC352 Synchronization and Java Threads"

From dftwiki3
Jump to: navigation, search
(Created page with "--~~~~ ---- <bluebox> This page treats of the concepts of synchronization of parallel programs in general, applied to the Java platform in particular. </bluebox> <br /> __TOC...")
 
(It all starts at the Lowest Level: Assembly Language (again))
Line 10: Line 10:
 
=It all starts at the Lowest Level: Assembly Language (again)=
 
=It all starts at the Lowest Level: Assembly Language (again)=
  
;Question
+
Let's look at a different parallel computation of Pi.  Imagine that the two threads in the ParallelPi program updated a global '''sum''' variable by doing something like this?
:What if the two threads in the ParallelPi program updated a global '''sum''' variable by doing something like this?
 
 
   
 
   
 
     sum += thread_sum;
 
     sum += thread_sum;
 
   
 
   
:::where '''thread_sum''' is a local variable used by the thread function to accumulate the terms of the series.  The actual program can be found [[CSC352:_Computing_Pi_and_Synchronization#A_Badly_Written_.28and_Flawed.29_Multithreaded_Computation_of_Pi | here]].
+
where '''thread_sum''' is a local variable used by the thread function to accumulate the terms of the series.  The actual program can be found [[CSC352:_Computing_Pi_and_Synchronization#A_Badly_Written_.28and_Flawed.29_Multithreaded_Computation_of_Pi | here]].
 +
 
 +
;Question 1
 +
: Figure out the assembly language for the java statement above
 +
;Question 2
 +
: Assume that main memory is a stack of index cards.  One index card is '''sum'''.  Two people in the class represent two different processors.  Their notebook represent their registers.  Execute these instructions simultaneously and figure out if there's a way for their simultaneous operation to fail.
 +
;Question 3
 +
: What makes the code fail?  What is the processor missing?
 +
; Question 4
 +
: What is a way around the problem, such that two parallel update of the variable '''sum''' will increment it by 2, every time?
 +
 
  
 
<br />
 
<br />
 
** [[CSC352: Computing Pi and Synchronization| Computing Pi and Synchronization]]
 
** [[CSC352: Computing Pi and Synchronization| Computing Pi and Synchronization]]

Revision as of 14:15, 9 September 2013

--D. Thiebaut (talk) 14:10, 9 September 2013 (EDT)


This page treats of the concepts of synchronization of parallel programs in general, applied to the Java platform in particular.



It all starts at the Lowest Level: Assembly Language (again)

Let's look at a different parallel computation of Pi. Imagine that the two threads in the ParallelPi program updated a global sum variable by doing something like this?

    sum += thread_sum;

where thread_sum is a local variable used by the thread function to accumulate the terms of the series. The actual program can be found here.

Question 1
Figure out the assembly language for the java statement above
Question 2
Assume that main memory is a stack of index cards. One index card is sum. Two people in the class represent two different processors. Their notebook represent their registers. Execute these instructions simultaneously and figure out if there's a way for their simultaneous operation to fail.
Question 3
What makes the code fail? What is the processor missing?
Question 4
What is a way around the problem, such that two parallel update of the variable sum will increment it by 2, every time?