0% found this document useful (0 votes)
50 views2 pages

Copyin: Department CSE, SCAD CET

The document discusses OpenMP directives for parallelizing loops and protecting shared variables. It provides code examples of using copyin, copyprivate, critical, and atomic pragmas to share data between threads and avoid data races when updating shared variables. Improved code is shown for parallelizing an image processing loop that converts a color image to black and white by computing pixel values in parallel threads.

Uploaded by

bsgindia82
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
50 views2 pages

Copyin: Department CSE, SCAD CET

The document discusses OpenMP directives for parallelizing loops and protecting shared variables. It provides code examples of using copyin, copyprivate, critical, and atomic pragmas to share data between threads and avoid data races when updating shared variables. Improved code is shown for parallelizing an image processing loop that converts a color image to black and white by computing pixel values in parallel threads.

Uploaded by

bsgindia82
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 2

copyin provides a way to copy the master thread's threadprivate variable to the threadprivate variable of each other member

of the team executing the parallel region. copyprivate provides a way to use a private variable to broadcast a value from one member of threads to other members of the team executing the parallel region. The copyprivate clause is allowed to associate with the single construct; the broadcast action is completed before any of threads in the team left the barrier at the end of construct.

code converts a color image to black and white.


for ( row = 0; row < height; row++ ) { for ( col = 0; col < width; col++ ) { pGray[col] = (B !") ( p#GB[row]$red % 0$&'' + p#GB[row]$green % 0$()* + p#GB[row]$+l,e % 0$--. ); / pGray += Gray0tride; p#GB += #GB0tride; /

The address computation for each pixel can be done with the following code: pDestLoc = p ray ! col ! row " ray#tride; p#rcLoc = p$ % ! col ! row " $ %#tride; Improved Version:
1prag2a o2p parallel for private (row3 col) 4 firstprivate(do5nit3 pGray3 p#GB) for ( row = 0; row < height; row++ ) { 66 7eed this init test to +e a+le to start at an 66 ar+itrary point within the i2age after threading$ if (do5nit == !#8") { do5nit = 9:;0"; p#GB += ( row % #GB0tride ); pGray += ( row % Gray0tride ); / for ( col =0; col <width; col++ ) { pGray[col] = (B !") ( p#GB[row]$red % 0$&'' + p#GB[row]$green % 0$()* + p#GB[row]$+l,e % 0$--. ); / pGray += Gray0tride; p#GB += #GB0tride; /

Protecting Updates of Shared variables: The critical and atomic pragmas are supported by the &pen'( standard for you to protect the updating of shared variables for avoiding data)race conditions. The code bloc* enclosed by a critical section and an atomic pragma are areas of code that may be entered only when no other thread is executing in them.

Department CSE, SCAD CET

+pragma omp critical , if - max . new/value 0 max = new/value 1 The named critical sections are used when more than one thread is competing for more than one critical resource.
1prag2a o2p critical(2a<val,e) { if ( 2a< < new=val,e ) 2a< = new=val,e /

2t is important to remember that entering nested critical sections runs the ris* of deadloc*. The following code example code shows a deadlock situation:
void de>,e,e(7?@" %node) { 1prag2a o2p critical (<)

Department CSE, SCAD CET

You might also like