Data Copy in Copy Out

2
Data Copy-in & Copy-out: When you parallelize a program, you would normally have to deal with how to copy in the initial value of a private variable to initialize its private copy for each thread in the team.We would also copy out the value of the private variable computed in the last iteration/section to its original variable for the master thread at the end of parallel region. OpenMP standard provides four clauses firstprivate lastprivate copyin and copyprivate For you to accomplish the data copy-in and copy-out operations whenever necessary based on your program and parallelization scheme. The semantics of the Scheme is described below, Firstprivate:provides a way to initialize the value of a private variable for each thread with the value of variable from the master thread. Normally, temporary private variables have an undefined initial value saving the performance overhead of the copy. lastprivate provides a way to copy out the value of the private variable computed in the last iteration/section to the copy of the variable in the master thread. Variables can be declared both firstprivate and lastprivate at the same time. 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

Transcript of Data Copy in Copy Out

Page 1: Data Copy in Copy Out

Data Copy-in & Copy-out: When you parallelize a program, you would normally have to deal with how to copy in

the initial value of a private variable to initialize its private copy for each thread in the team.We would also copy out the value of the private variable computed in the last iteration/section to its original variable for the master thread at the end of parallel region.

OpenMP standard provides four clauses firstprivate lastprivate copyin and copyprivate

For you to accomplish the data copy-in and copy-out operations whenever necessary based on your program and parallelization scheme.

The semantics of the Scheme is described below,

Firstprivate:provides a way to initialize the value of a private variable for each thread with the value of variable from the master thread. Normally, temporary private variables have an undefinedinitial value saving the performance overhead of the copy.

lastprivate provides a way to copy out the value of the private variable computed in the last iteration/section to the copy of the variable in the master thread. Variables can be declared bothfirstprivate and lastprivate at the same time.

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] = (BYTE)( pRGB[row].red * 0.299 +pRGB[row].green * 0.587 +pRGB[row].blue * 0.114 );}pGray += GrayStride;pRGB += RGBStride;}

Page 2: Data Copy in Copy Out

The address computation for each pixel can be done with the following code:pDestLoc = pGray + col + row * GrayStride;pSrcLoc = pRGB + col + row * RGBStride;

Improved Version:#pragma omp parallel for private (row, col) \firstprivate(doInit, pGray, pRGB)for ( row = 0; row < height; row++ ) {// Need this init test to be able to start at an// arbitrary point within the image after threading.if (doInit == TRUE) {doInit = FALSE;pRGB += ( row * RGBStride );pGray += ( row * GrayStride );} for ( col =0; col <width; col++ ){pGray[col] = (BYTE) ( pRGB[row].red * 0.299 +pRGB[row].green * 0.587 +pRGB[row].blue * 0.114 );} pGray += GrayStride;pRGB += RGBStride;}