Our website uses cookies to enhance your browsing experience.
Accept
to the top
>
>
Use of rand() in OpenMP parallel sectio…

Use of rand() in OpenMP parallel sections

May 27 2009
Author:

I came across an interesting thread at RSDN forum where a specific error of rand() function use in OpenMP parallel sections is considered (http://rsdn.org/forum/cpp.applied/3400925.flat). I collect various errors which deal with OpenMP technology use so that to implement their troubleshooting in VivaMP static code analyzer in future. The error considered at the forum is perhaps a very specific one to implement a rule for its verification, so I decided just to write about it in the blog.

The error consists in the fact that every parallel thread has its own seed and if no special initialization is carried out, rand() function will return the same value in all the threads. Most likely, this will not be the required result.

Note: seed is initial value given to the random sequence generator in order to obtain the first random number. If you assign seed a particular value, the numbers sequence will always repeat starting with this very number.

The following code example is given at the forum:

void initMatrix(int** m, int H, int W)
{
  #pragma omp parallel
  {
    #pragma omp for
    for (int i = 0; i < H; ++i)
      for (int j = 0; j < W; ++j)
         m[i][j] = rand()%15;
  }
}

The result of such code work is filling of matrix with repeating blocks of numbers. For example, a 10×10 matrix filled in in two threads can look as follows:

0012_Use_of_rand()_in_OpenMP_parallel_sections/image1.png

As we can see, the upper and the lower parts of the matrix filled in in two different threads are the same.

In order to avoid the same rand() function behavior, in your code, you should initialize random numbers generator in each parallel thread with various values. To do this, the combination of current time from the current thread number can be used.

The corrected code will look as follows:

void initMatrix(int** m, int H, int W)
{
  #pragma omp parallel
  {
    srand(int(time(NULL)) ^ omp_get_thread_num());
    #pragma omp for
    for (int i = 0; i < H; ++i)
      for (int j = 0; j < W; ++j)
        m[i][j] = rand()%15;
  }
}
Popular related articles


Comments (0)

Next comments next comments
close comment form
close form

Fill out the form in 2 simple steps below:

Your contact information:

Step 1
Congratulations! This is your promo code!

Desired license type:

Step 2
Team license
Enterprise license
** By clicking this button you agree to our Privacy Policy statement
close form
Request our prices
New License
License Renewal
--Select currency--
USD
EUR
* By clicking this button you agree to our Privacy Policy statement

close form
Free PVS‑Studio license for Microsoft MVP specialists
* By clicking this button you agree to our Privacy Policy statement

close form
To get the licence for your open-source project, please fill out this form
* By clicking this button you agree to our Privacy Policy statement

close form
I am interested to try it on the platforms:
* By clicking this button you agree to our Privacy Policy statement

close form
check circle
Message submitted.

Your message has been sent. We will email you at


If you do not see the email in your inbox, please check if it is filtered to one of the following folders:

  • Promotion
  • Updates
  • Spam