0

If you have a code (which is parallelized by OpenMP) that uses some random function inside of a parallel region, is it possible that changing the number of threads changes the outcome?

user3666197
  • 1
  • 6
  • 50
  • 92
  • 1
    Depends on the actual code-design. In cases, where such a code supports both a requirement of representing a tool for a **repeatable-&-reproducible**-science & a requirement of performance ( without any side-effects ), then the answer must be **NO**. In any other case, having either a PRNG- or a true-source-of-randomness-based such a random function, the answer must be **YES** – user3666197 Feb 27 '22 at 19:13
  • 2
    [How to generate random numbers in parallel?](https://stackoverflow.com/questions/4287531/how-to-generate-random-numbers-in-parallel) isn't a duplicate, but I think it might help you more than the answer to your question. – paleonix Feb 27 '22 at 23:23

2 Answers2

0

If you use some function generating random numbers the output will vary anyways.

In general it is possible and desired to create thread safe functions/loop bodies, which produce equivalent results independent of the chosen number of threads. This is not guaranteed if you're having some race-conditions in your code, p.e., you read data from a location which is manipulated in another loop iteration.

miro
  • 67
  • 6
  • You could also consider using a safe, parallel random-number generator which requires no synchronisation between threads yet also guarantees that each thread sees a separate chunk of the random number sequence. e.g. http://www.thesalmons.org/john/random123/papers/random123sc11.pdf which is now implemented in many maths libraries. – Jim Cownie Mar 01 '22 at 08:40
0

What do you mean by "random function"? If there is a random number genrator involved you need to make sure that it's thread-safe. There are also the issues of non-associativity of floating point arithmetic, so results can be different, but the differences should be small. Finally, if your code has race conditions your results can be very different, even between different runs on the same number of threads.

About random numbers. If every thread needs its own, you could use omp critical. However, that could have big performance problems, depending on how much other work there is to balance it out.

If you use C++, you could have a separate generator in each thread. I don't know of any really good solution for C.

Victor Eijkhout
  • 5,088
  • 2
  • 22
  • 23
  • Yes, I mean a function that generate random number. So, to make it thread safe should I use pragma omp single? –  Feb 27 '22 at 20:42