maximizing use of threads

General OpenMP discussion

maximizing use of threads

Postby johnyjj2 » Wed Jun 05, 2013 11:02 am

Hello,

I am new to the world of OpenMP. I would like to maximize number of threads in the following code:

Code: Select all
#include "stdafx.h"
#include <omp.h>

#define N 5
int a[N][N];
void main ()
{
   int maxThreads = omp_get_max_threads();
   omp_set_num_threads(maxThreads);

   int i, j;
   #pragma omp parallel for reduction(+:a) schedule(static,1)
   for (i = 0; i < N; i++)
      for (j = 0; j < N; j++)
         a[i][j] = i + j;
}


I have added maxThreads and omp_set_num_threads(), as well as pragma.

However, for pragma I get following error:
Error 1 error C3031: 'a' : variable in 'reduction' clause must have scalar arithmetic type


What should I do to succesfully maximize number of threads being used and to parallelize code?

Thanks in advance for guidelines!
johnyjj2
 
Posts: 2
Joined: Sat Jun 01, 2013 7:56 am

Re: maximizing use of threads

Postby ftinetti » Wed Jun 05, 2013 2:44 pm

Hi,

However, for pragma I get following error:
Error 1 error C3031: 'a' : variable in 'reduction' clause must have scalar arithmetic type

The compiler is right, a is not scalar so it cannot be a reduction variable. Also, it does not make sense being a reduction variable, since there is not reduction on a. There are other issues in the code such as:
1) variable j should be declared private, since it is shared by default, which is a problem
2) Processing 5x5 matrices in parallel would not provide any speedup, since there is almost no work to do (neither sequentially, nor in parallel)
3) Maybe schedule(static,1) would only add overhead on this kind of processing, I would use the default schedule (i.e. no schedule clause)

HTH,

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

Re: maximizing use of threads

Postby johnyjj2 » Thu Jun 06, 2013 11:59 am

Thanks for reply!

ftinetti wrote:The compiler is right, a is not scalar so it cannot be a reduction variable. Also, it does not make sense being a reduction variable, since there is not reduction on a.

So I have changed the whole reduction to shared.

ftinetti wrote:1) variable j should be declared private, since it is shared by default, which is a problem

OK, I have changed code to private(j) so I understand that only i (and those parts of a that have corresponding i) is used for parallel processing and each thread has only j with different value for each thread.

ftinetti wrote:2) Processing 5x5 matrices in parallel would not provide any speedup, since there is almost no work to do (neither sequentially, nor in parallel)

I think it can be done in parallel in such a way that one row is calculated in one thread, the other row in the other thread and so on.

ftinetti wrote:3) Maybe schedule(static,1) would only add overhead on this kind of processing, I would use the default schedule (i.e. no schedule clause)

I have changed the whole reduction to shared.

Now the whole code looks like this:

Code: Select all
#include "stdafx.h"
#include <omp.h>

#define N 5
int a[N][N];
void main ()
{
   int maxThreads = omp_get_max_threads();
   omp_set_num_threads(maxThreads);

   int i, j;
   //#pragma omp parallel for reduction(+:a)
   #pragma omp parallel for shared(a) private(j)
   for (i = 0; i < N; i++)
      for (j = 0; j < N; j++)
         a[i][j] = i + j;
}


Is it good approach?

In fact this is theoretical exercise from book about parallel processing. I assume that solving this task may help me to understand basics of OpenMP better.

Regards!
johnyjj2
 
Posts: 2
Joined: Sat Jun 01, 2013 7:56 am

Re: maximizing use of threads

Postby ftinetti » Thu Jun 06, 2013 1:00 pm

Hi,

Is it good approach?

I think it's fine (I didn't use #include "stdafx.h"). However,

ftinetti wrote:
2) Processing 5x5 matrices in parallel would not provide any speedup, since there is almost no work to do (neither sequentially, nor in parallel)

I think it can be done in parallel in such a way that one row is calculated in one thread, the other row in the other thread and so on.


maybe it's better by example: with #define N 5

$ export OMP_NUM_THREADS=1
$ time a.out

real 0m0.002s

$ export OMP_NUM_THREADS=2
$ time a.out

real 0m0.002s

$ export OMP_NUM_THREADS=4
$ time a.out

real 0m0.002s

$ export OMP_NUM_THREADS=8
$ time a.out

real 0m0.002s

(i.e no performance improvement using more threads) with #define N 22000

$ export OMP_NUM_THREADS=1
$ time a.out

real 0m3.603s


$ export OMP_NUM_THREADS=2
$ time a.out

real 0m2.212s

$ export OMP_NUM_THREADS=4
$ time a.out

real 0m1.201s


$ export OMP_NUM_THREADS=8
$ time a.out

real 0m0.726s

HTH,

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

Re: maximizing use of threads

Postby MarkB » Fri Jun 07, 2013 2:19 am

Hi there,

As Fernando says, your code is now correct!

I just wanted to note that the lines

Code: Select all
   int maxThreads = omp_get_max_threads();
   omp_set_num_threads(maxThreads);


are not required, since the first line returns the value of the nthreads-var internal control variable (which controls the number of threads used for subsequent parallel regions) and the second line resets it to the same value.

Mark.
MarkB
 
Posts: 480
Joined: Thu Jan 08, 2009 10:12 am
Location: EPCC, University of Edinburgh


Return to Using OpenMP

Who is online

Users browsing this forum: Yahoo [Bot] and 15 guests