parallel for

General OpenMP discussion

parallel for

Postby cybernike » Wed Oct 01, 2008 1:43 am

Hi there,
Could you someone please tell me why only one of my 8 cores is working for the following code?

Thanks.

Code: Select all
               #pragma omp parallel
               {
               #pragma omp for private(k)
                  for(k=0;k<timeslots*2;k++)
                  {

                     if(m-result[k].starttime==pow(2.,k+timestartpwr))
                     {

                        calculate(states,para_msd,per_msd1,per_msd2,
                           result[k].starttime, tailptr, lines,G_ssum,Fhat_1,Fhat_1counter, k);
                        result[k].para_msdsum+=para_msd;
                        result[k].per_msd1sum+=per_msd1;
                        result[k].per_msd2sum+=per_msd2;               
                        result[k].starttime=m;
                        result[k].counter++;
                        for(int j=0;j<uniqueklength;j++)
                        {
                           for(int m=0;m<kveclist[j].diff;m++)
                              G_ssum[j][k][m][1]++;
                        }


                     }         



                  }
               }
cybernike
 
Posts: 6
Joined: Wed Oct 01, 2008 1:39 am

Re: parallel for

Postby ejd » Wed Oct 01, 2008 5:40 am

You didn't tell me the compiler or operating system so I can only make general comments. The usual problems are:
  • that you didn't specify the compiler flag to compile the program to use OpenMP, or
  • that you didn't specify the number of threads to be used.

For the first problem, check your compiler to see what sort of flag is needed. It will be something like -fopenmp, -xopenmp, etc.

For the second problem, you need to do one of the following:
  • make calls to omp_set_dynamic() and omp_set_num_threads(),
  • set the environment variables OMP_DYNAMIC and OMP_NUM_THREADS, or
  • use a num_threads() clause on the parallel directive.
ejd
 
Posts: 1025
Joined: Wed Jan 16, 2008 7:21 am

Re: parallel for

Postby cybernike » Wed Oct 01, 2008 5:26 pm

Thanks.
My OS is redhat linux with gcc 4.2.

It turns out that more than 1 core was working, but the part of the code that is multi-threaded is so insignificant that the cores finished the job without showing a signal in the CPU monitor.
Therefore, I am trying to multi-thread a bigger portion of the program.

Here is my new question

Suppose I have a for loop

Code: Select all
for(int i=0; i< N; i++)
{

(complicated code here with quite a few nested for loops, but the whole section is independent for each i)

}


How can I make sure OPENMP will only assign one thread for each i? I don't definitely don't want to have more than 1 thread to handle each i since I am not sure how the complication of the code will affect OPENMP, but one thing is certain; the section of the code is absolutely independent for each i and I just need to run the section N times.
cybernike
 
Posts: 6
Joined: Wed Oct 01, 2008 1:39 am

Re: parallel for

Postby ejd » Wed Oct 01, 2008 6:35 pm

When you use an OpenMP worksharing for construct such as the following:
Code: Select all
#include <omp.h>
#include <stdio.h>
#define N 10

void main(void) {
  omp_set_dynamic(0);
  omp_set_num_threads(4);
  #pragma omp parallel for
  for(int i=0; i< N; i++)
  {
     printf("i: %i  t#: %i\n", i, omp_get_thread_num());
  }
}

each iteration of "i" is assigned to a thread - not multiple threads. The output will look something like the following:
Code: Select all
% a.out
i: 0  t#: 0
i: 8  t#: 3
i: 9  t#: 3
i: 1  t#: 0
i: 2  t#: 0
i: 6  t#: 2
i: 7  t#: 2
i: 3  t#: 1
i: 4  t#: 1
i: 5  t#: 1

As you can see, in this particular case (using the Sun Studio C compiler which defaults the schedule to static), thread 0 ran iterations i=0,1,2, thread 1 ran iterations i=3,4,5, thread 2 ran iterations i=6,7, and thread 3 ran iterations i=8,9. You can change this by using a schedule clause on the directive. For example, adding "schedule(static,1)" will get you output that looks something like:
Code: Select all
% a.out
i: 3  t#: 3
i: 7  t#: 3
i: 1  t#: 1
i: 5  t#: 1
i: 9  t#: 1
i: 0  t#: 0
i: 4  t#: 0
i: 8  t#: 0
i: 2  t#: 2
i: 6  t#: 2

This time, you will see thread 0 ran iterations i=0,4,8, thread 1 ran i=1,5,9, etc. Try playing with it and you can see what happens. Hope that helps.
ejd
 
Posts: 1025
Joined: Wed Jan 16, 2008 7:21 am


Return to Using OpenMP

Who is online

Users browsing this forum: Google [Bot] and 11 guests