Simple Parallel Program Issue

General OpenMP discussion

Simple Parallel Program Issue

Postby Guest » Thu Jun 12, 2008 9:14 am

Hello Everyone,
I was wondering if someone could give me some advice on how to solve my simple problem since I'm new to Open MP. I've been testing small program that use the basic Open MP directives and tools to create a small parallel program. But when compiling and testing the program, the code behaves as it was performed in a non-parallel manner because omp_in_parallel() returns a zero, which means a parallel region was not created. I've tried many different examples that are from the tutorial:
https://computing.llnl.gov/tutorials/openMP/
and the spec documentation that provides examples in the appendix but the result appears to be the same. Here is an example of a code that I was testing:
Code: Select all
#include <omp.h>
#include <stdio.h>

int main(void)
{
   int i, u;
   float a[10] ;
   /* Some initializations */
   for (i=0; i < 10; i++)
      a[i] = i * 1.5;

   omp_set_num_threads(4);
   
#pragma omp parallel for
   {
      u=omp_in_parallel();
      for (i=0; i < 10; i++)
         a[i]=2.0*i;
   }
   printf("The previous process was completed in parallel if the following number is a non zero value:%i\n",u);
   /* end of parallel section */
   return u;
}

I've tried various codes, would the problem be in my configuration? I'm programming in C, using an Xcode interface, with intel dual core processor with the MAC OS. I hope that someone can find the time to help me address this issue. Thank you for readying my post.

Dragan
Guest
 

Re: Simple Parallel Program Issue

Postby ejd » Thu Jun 12, 2008 9:39 am

I am not familiar with your environment, so I can't comment on everything. I am puzzled by the fact that you seem to have the omp functions resolved, but that the compiler isn't complaining about your program. Your program is not conforming to the OpenMP spec. A for loop (in canonical form) must follow an "omp for" directive. The code should look more like:

Code: Select all
#pragma omp parallel
{
  #pragma omp master
  u=omp_in_parallel();
  #pragma omp for
   for (i=0; i < 10; i++)
      a[i]=2.0*i;
}

I have put in the master because otherwise you have a race on "u". master doesn't have an implicit barrier at the end and only one thread will execute the code (making it fairly fast).

Check to see that you have specified the "OpenMP flag" on the compile line. Sorry I don't know Xcode flags. You might also check to see if there is a flag that will give you messages about whether the compiler has parallelized a region or loop. This way you can check to make sure that the compiler is actually doing what you think it should be doing.

Another thing that you should be careful with, is that you specified the number of threads to use in a call to omp_set_num_threads. However, depending on the dafault for omp_dynamic, the run time may use a different number of threads. It is generally a good practice to get into, if you really want a certain number of threads, to precede the omp_set_num_threads call with a call to omp_dynamic(0). This tells the run time not to play with the number of threads (unless it really has too - in error cases).
ejd
 
Posts: 1025
Joined: Wed Jan 16, 2008 7:21 am

Re: Simple Parallel Program Issue

Postby guest » Wed Aug 13, 2008 11:30 pm

Thanks for the help,
the problem was in the version of the compiler I was using. The open MP supports gcc 4.2 and above while I was using 4.0. Thanks for taking the time to answer my question.
guest
 


Return to Using OpenMP

Who is online

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