Another option code parallelization

General OpenMP discussion

Another option code parallelization

Postby miltt » Mon Jan 30, 2012 5:42 am

Hello, please tell me whether it is possible to parallelize this code differently? He works slowly from the directive atomic. If this directive is removed, then we get a wrong result.
Code: Select all
int a, b, c, d, e, f, sum = 0, s[55];
   memset(s, 0, sizeof(s));
            
   #pragma omp parallel for private(a, b, c, d, e, f)
      for(a = 0; a < 10; a++)
      for(b = 0; b < 10; b++)
      for(c = 0; c < 10; c++)
      for(d = 0; d < 10; d++)
      for(e = 0; e < 10; e++)
      for(f = 0; f < 10; f++)
      {   
         #pragma omp atomic
         s[a+b+c+d+e+f]++;
      }
      for (a = 0; a < 55; a++)
         sum += (s[a]*s[a]);
miltt
 
Posts: 3
Joined: Mon Jan 30, 2012 5:34 am

Re: Another option code parallelization

Postby ftinetti » Mon Jan 30, 2012 6:05 am

I think some more information is needed:
1) How are you measuring time? I suggest you use omp_get_wtime ()
2) How much runtime is involved? (overhead becomes relatively high for runtimes in the milliseconds range)
3) Compiler? Compiler options? Computer?

I would suggest trying with critical instead of atomic and setting OMP_WAIT_POLICY to PASSIVE, but maybe you will not get much better results...

HTH.
ftinetti
 
Posts: 582
Joined: Wed Feb 10, 2010 2:44 pm

Re: Another option code parallelization

Postby miltt » Mon Jan 30, 2012 11:20 am

1)How are you measuring time? I suggest you use omp_get_wtime ()

I measure time as follows:
Code: Select all
#include <time.h>
clock_t start, stop;

start = clock();
...
stop = clock();

cout << ((double)(stop - start)/1000.0)  << "sec\n";

but it is not so important.
2) How much runtime is involved?

Sequential code is executed for ~0 milliseconds. Parallel code with the directive Atomic runs for 0.34 milliseconds. Parallel code without directive Atomic runs for ~0 milliseconds, but it gives a wrong result.
3) Compiler? Compiler options? Computer?


Intel Core 2 CPU 6300 @ 1.86GHz, MS Windows 7 Ultimate x64, Visual Studio 2010 Pro, Intel Parallel Studio XE 2011

In this case, I've simplified algorithm. In fact, the program runs a few minutes because of this I want this code to parallel. Possible to replace the directive atomic to something else? Or, for example, do not use the directives. And to use a functions? Or need to change the algorithm, although this algorithm is simple :)
miltt
 
Posts: 3
Joined: Mon Jan 30, 2012 5:34 am

Re: Another option code parallelization

Postby ftinetti » Mon Jan 30, 2012 11:34 am

I measure time as follows:

Code: Select all
#include <time.h>
clock_t start, stop;

start = clock();
...
stop = clock();

cout << ((double)(stop - start)/1000.0)  << "sec\n";


but it is not so important.

Well, it is... if clock() measures wall clock elapsed time then there is no problem, but if clock() measures CPU time then it is possible you have a wrong way of computing performance values.

In this case, I've simplified algorithm. In fact, the program runs a few minutes because of this I want this code to parallel. Possible to replace the directive atomic to something else?

Well, see the previous post: a) critical, b) wait_policy

Or, for example, do not use the directives. And to use a functions? Or need to change the algorithm, although this algorithm is simple

Well, there are race conditions on the assignment s[a+b+c+d+e+f]++ in the current code so you have to synchronize threads to have correct results. If there is enough RAM, you could "replicate" s, so that every thread has its own s and, after the parallel section you aggregate every result in the "general" s... hmmm... maybe my explanation is not very... clear... so ask for a better one if you do not understand the idea.

HTH.
ftinetti
 
Posts: 582
Joined: Wed Feb 10, 2010 2:44 pm

Re: Another option code parallelization

Postby miltt » Mon Jan 30, 2012 11:55 am

I understand your idea, do you suggest I do reduction(), but manually, right? I'll try, thanks for the help.
miltt
 
Posts: 3
Joined: Mon Jan 30, 2012 5:34 am

Re: Another option code parallelization

Postby ftinetti » Mon Jan 30, 2012 11:58 am

Yes, a reduction on an array, which does not exist... yet.
ftinetti
 
Posts: 582
Joined: Wed Feb 10, 2010 2:44 pm


Return to Using OpenMP

Who is online

Users browsing this forum: No registered users and 9 guests