I would like to find the best way to reduce private arrays (1-D or 2-D) in OpenMP with a critical statement towards the end of a parallel zone. This is to solve the problem of having an array whose elements are being incremented at various points by all threads, which I solved by declaring temporary arrays inside the parallel zone (i.e. the temporary arrays are initialized with 0, then incremented inside the parallel zone, and added to the original shared array, and finally deleted from memory).
In order to solve this I created the following macros to increment one array by the other:
// This is in a frame.h file
static INT count,count2;
/* Increment 1-D array x by array y */
#define Arr1Inc(x,y,n) \
{ for(count=0;count<n;count++) \
x[count]+=y[count]; }
/* Increment 2-D array x by array y */
#define Arr2Inc(x,y,m,n) \
{ for(count2=0;count2<m;count2++) \
for(count=0;count<n;count++) \
x[count2][count]+=y[count2][count]; }
An example of using these macros is like this:
// This is at the end of my parallel zone
// d1nfcx and d2nft are shared arrays
// d1nfcx_tmp and d2nft_tmp are private arrays
// mnodim and mnopo are the array sizes
#pragma omp critical (update_force)
{
Arr1Inc(d1nfcx,d1nfcx_tmp,mnopo)
Arr2Inc(d2nft,d2nft_tmp,mnodim,mnopo)
}
free(d1nfcx_tmp);
free(d2nft_tmp);
This seems to work fine, however, for it to work I need to declare count and count2 variables as private at the beginning of my parallel zone. Is there a better way to do the reduction?
Note that I need this to be as efficient as possible, as my arrays can be pretty big (>100,000s elements).
Thanks!