Click here to Skip to main content
Click here to Skip to main content

Use of rand() in OpenMP parallel sections

, 28 Mar 2012 CPOL
Rate this:
Please Sign up or sign in to vote.
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.

I came across an interesting thread at RSDN forum where a specific error of rand() function use in OpenMP parallel sections is considered (using rand() function with OpenMP [RU]). I collect various errors which deal with OpenMP technology use so that to implement their troubleshooting in VivaMP 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:

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;
  }
}

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Karpov Andrey
Architect Program Verification Systems, Co Ltd
Russian Federation Russian Federation

Andrey Karpov is technical manager of the OOO "Program Verification Systems" (Co Ltd) company developing the PVS-Studio tool which is a package of static code analyzers integrating into the Visual Studio development environment.

PVS-Studio is a static analyzer that detects errors in source code of C/C++ applications. There are 3 sets of rules included into PVS-Studio:

  1. Diagnosis of 64-bit errors (Viva64)
  2. Diagnosis of parallel errors (VivaMP)
  3. General-purpose diagnosis

Awards: MVP, Intel Black Belt

Andrey Karpov is also the author of many articles on the topic of 64-bit and parallel software development. To learn more about the PVS-Studio tool and sources concerning 64-bit and parallel software development, please visit the www.viva64.com site.

Best Articles:

My page on LinkedIn site: http://www.linkedin.com/pub/4/585/6a3

E-mail: karpov@viva64(dot)com

Follow on   Twitter

Comments and Discussions

 
Questionmy vote of 5 PinmemberAuslandskrankenversicherung29-Mar-12 0:02 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.1411023.1 | Last Updated 29 Mar 2012
Article Copyright 2012 by Karpov Andrey
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid