2

I have written code in which I use parallel computing. Because adding random numbers to a parallel loop is tricky, I have tried to generate independent random numbers by generating the random numbers just before the parallel loop. However, I am still not sure if this is completely correct or could cause other problems. I would appreciate it if you could help me with this.

The part of the code which is relevant to the above discussion is:

void Nran::UpdateF(bool first)
{
    const int numbe=100;
    double figran[numbe];

    for(unsigned fr=0; fr<Domain; ++fr)
    {
        figran[fr]=random_real()-0.5;
    }

#pragma omp parallel for num_threads(nthreads) if(nthreads)
    for(unsigned g=0; g<DomainSize; ++g)
        UpdateNode(g, first,figran);
}

IMPORTANT NOTE: in the function UpdateNode I do some calculations with each random number in the array (figran[g]).

user258046
  • 53
  • 4
  • 1
    That should be fine. I did this once to generate GUIDs in a browser. (You can't.) I just generated a bunch on the server and passed them to the browser when the page loaded. – 3Dave Nov 25 '20 at 21:52
  • Even if in the parallel loop, I do calculations on elements of the figran array separately? @3Dave – user258046 Nov 25 '20 at 21:58
  • You could use one [`std::random_device`](https://en.cppreference.com/w/cpp/numeric/random/random_device) and suitable [generator](https://en.cppreference.com/w/cpp/numeric/random#Predefined_random_number_generators) per thread. – Some programmer dude Nov 25 '20 at 21:59
  • [`double figran[numbe]`](https://stackoverflow.com/questions/1887097/why-arent-variable-length-arrays-part-of-the-c-standard) looks questionable. – chux - Reinstate Monica Nov 25 '20 at 22:02
  • @Someprogrammerdude Should I use 'std::random_device' inside the 'for(unsigned fr=0; fr – user258046 Nov 25 '20 at 22:05
  • I have improved the code. I think "double figran[numbe]" is not questionable anymore, right? Because it does not change size anymore. @chux-ReinstateMonica – user258046 Nov 25 '20 at 22:07
  • 1
    [C++ does not have VLAs.](https://stackoverflow.com/a/39334548/2410359).. Maybe someday. Yet VLA is not the most important aspect of this question, mostly a distraction. – chux - Reinstate Monica Nov 25 '20 at 22:10
  • Do you have suggestions to fix this? @chux-ReinstateMonica – user258046 Nov 25 '20 at 22:12
  • 1
    Could demo the problem with `double figran[100];` – chux - Reinstate Monica Nov 25 '20 at 22:15
  • @user258046 If the values are generated ahead of time, and you're not over-**writing** the same element in the array from separate threads, it'll be fine. It's also arguably more efficient do this outside of the parallel code. – 3Dave Nov 25 '20 at 22:20
  • 1
    @chux-ReinstateMonica `double figran[numbe];` is fine: it's a normal non-VLA array. `numbe` is "usable in constant expressions" in this context, as a `const` integral type initialized with a constant expression. – N. Shead Nov 26 '20 at 03:39
  • 1
    The code seems totally fine to me. As long as random number generation isn't the main source of work, this solution is good enough. It decouples random number generation and parallelism, such that there shouldn't be any new/additional problems. – paleonix Nov 26 '20 at 13:16

0 Answers0