0

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!

Leonardo Trivino
  • 295
  • 1
  • 4
  • 11
  • See this link. I reduced arrays without using a critical section. http://stackoverflow.com/questions/16789242/fill-histograms-array-reduction-in-parallel-with-openmp-without-using-a-critic –  Jun 06 '13 at 15:18
  • @raxman thanks for the suggestion, I should have some performance by avoiding the critical. However, I still would like to use the macro that can simply add two arrays. Is it possible to do this either without the 'for' loop or somehow getting rid of the count/count2 variables? – Leonardo Trivino Jun 06 '13 at 20:42
  • I already answer your question about critical with the link I provided. It has all the information you need. As to removing count. Why don't you try defining count inside the for loop i.e. for(int count=0;count –  Jun 07 '13 at 14:30

0 Answers0