






Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!







file:///D|/...ry,%20Dr.%20Sanjeev%20K%20Aggrwal%20&%20Dr.%20Rajat%20Moona/Multi-core_Architecture/lecture%2020/20_1a.htm[6/14/2012 12:03:38 PM]
Private(list) Lastprivate(list) Firstprivate(list) Reduction(operator:list) Nowait
What happens if the number of threads and the number of sections are different? More threads than sections?
#pragma omp parallel shared(n,a,b,c,d) private(i) { #pragma omp sections nowait { #pragma omp section for (i = 0; i < n-1; i++) b[i] = (a[i] + a[i+1])/2; #pragma omp section for (i = 0; i < n; i++) d[i] = 1.0/c[i]; } /– End of sections –/ } /– End of parallel region –/
By default, there is a barrier at the end of the “omp sections”. Use of “nowait” clause turns off the barrier
OpenMP provides combined Parallel Worksharing directive that are merely shortcuts: PARALLEL DO/parallel for Parallel SECTIONS These directives behave same as an individual PARALLEL directive being immediately followed by a separate work-sharing directive. Most of the rules and clauses that apply to both directives are in effect
#pragma omp parallel #pragma omp for for (...)
#pragma omp parallel for for (....)
#pragma omp parallel #pragma omp sections {...}
#pragma omp parallel sections {.... }
Consider a simple example where two threads on two different processors are both trying to increment a variable x at the same time (assume x is initially 0):
THREAD 1 increment(x){ x = x+1; } THREAD 1 10 LOAD A, (x address) 20 ADD A, 1 30 STORE A, (x address)
increment(x){ x = x+1; } THREAD 2 10 LOAD A, (x address) 20 ADD A, 1 30 STORE A, (x address)
What are the possible outputs?
Consider a simple example where two threads on two different processors are both trying to increment a variable x at the same time (assume x is initially 0):
THREAD 1 increment(x){ x = x+1; } THREAD 1 10 LOAD A, (x address) 20 ADD A, 1 30 STORE A, (x address)
increment(x){ x = x+1; } THREAD 2 10 LOAD A, (x address) 20 ADD A, 1 30 STORE A, (x address)
What are the possible outputs? After exuction of both threads resultant value of x may be 1 for some execution sequence To avoid situations like this the incrementation of x must be synchronized between the two threads
Synchronization is used to impose order constraints and to protect access to shared data
Critical Atomic Barrier Ordered
Flush Locks
#pragma omp atomic statement expression
The atomic directive specifies that a specific memory location must be updated atomically, rather than letting multiple threads attempt to write to it
Suppose we run each of these two loops in parallel
for( i = 0; i < N; i++) a[i] = b[i] + c[i]; for(i = 0; i < N; i++) d[i] = a[i] + b[i];
This may give us wrong result WHY?
We should have updated all a[]’s before using them
for( i = 0; i < N; i++) a[i] = b[i] + c[i];
All threads should wait here for other threads to complete so we need a barrier here
for(i = 0; i < N; i++) d[i] = a[i] + b[i];
All threads wait at the barrier point and only continue when all threads have reached the barrier point If there is the guarantee that the mapping of iterations onto threads is identical for both loops, there will not be a data race in this case
#pragma omp barrier
A thread will wait at barrier until all other threads have reached that barrier. All threads then resume executing in parallel the code that follows the barrier
Used within a DO/for loop with and ordered clause The ordered directive specifies that iterations of the enclosed loop will be executed in the same
#pragma omp ordered structured block
#pragma omp parallel for ordered for(I = 0 ; I < N ; I++){ tmp = NEAT STUFF(I); #pragma omp ordered res += consume(tmp); }
Defines a sequence point at which a thread is guaranteed to see a consistent view of memory with respect to the “flush set” The flush set is : List of variables In absence of list all thread visible variable are in list Flush forces data to be updated in memory so other threads see the most recent value
The Data Scope Attribute Clauses are used to explicitly define how variables should be scoped.They include :
Private Firstprivate Lastprivate Shared Default Reduction Copyin
Purpose: The private clause declares variables in its list to be private to each thread Format: private (list)
A new object of the same type is declared for each thread in the team Private(var) creates a new local copy of var for each thread
void wrong() { int tmp = 0; #pragma omp parallel for private(tmp) for (int j = 0; j < 1000; ++j) tmp += j; printf(“%d”, tmp); }
What is wrong with the code? Value of tmp was not initialized at line 5 What value of tmp it will be printed in second last line? In OpenMP 2.5 the value of the shared variable is undefined after the region So tmp is unspecified for OpenMP 2.5 while 0 in OpenMP 3.
file:///D|/...ry,%20Dr.%20Sanjeev%20K%20Aggrwal%20&%20Dr.%20Rajat%20Moona/Multi-core_Architecture/lecture%2020/20_10.htm[6/14/2012 12:03:40 PM]
Purpose: The firstprivate clause combines the behavior of the private clause with automatic initialization of the variables in the list Format: firstprivate (list)
Special case of private clause Initializes each private copy with the corresponding value from the master thread prior to entry into the parallel or work-sharing construct
void useless() { int tmp = 0; #pragma omp parallel for firstprivate(tmp) for (int j = 0; j ¡ 1000; ++j) tmp += j; printf(“%d”, tmp); }
Each thread gets its own code with initial value 0.Is there something still wrong with the code? What value of tmp it will be printed in second last line? Tmp is unspecified for OpenMP 2.5 while 0 in OpenMP 3.
Purpose: The lastprivate clause combines the behavior of the private clause with a copy from the last loop iteration or section to the original variable object. Format: lastprivate (list)
The value copied back into the original variable object is obtained from the last (sequentially) iteration or section of the enclosing construct.