Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You have two options:</p> <ul> <li>have individual random number generators for each thread and seed them differently</li> <li>use mutual exclusion</li> </ul> <p>First, an example of mutual exclusion:</p> <pre><code># pragma omp parallel for for (int bb=0; bb&lt;10000; bb++) { for (int i=0; i&lt;20000; i++) { // enter critical region, disallowing simulatneous access to eng #pragma omp critical { int a = uniform_distribution(0,20000); } // presumably some more code... } // presumably some more code... } </code></pre> <p>Next, an example of thread-local storage with seeding:</p> <pre><code># pragma omp parallel { // declare and seed thread-specific generator boost::random::mt19937 eng(omp_get_thread_num()); #pragma omp for for (int bb=0; bb&lt;10000; bb++) { for (int i=0; i&lt;20000; i++) { int a = uniform_distribution(0,20000, eng); // presumably some more code... } // presumably some more code... } } </code></pre> <p>Both of these snippets are just illustrative, depending on your requirements (say security related vs. a game vs. modelling) you may want to pick one over the other. You will probably also want to change the exact implementation to suit your usage. For instance, how you seed the generator is important if you want it to be either repeatable or closer to truly random (whether that's possible is system specific). This applies to both solutions equally (though to get reproducibility in the mutual exclusion case is harder).</p> <p>The thread-local generator may run faster while the mutual exclusion case should use less memory.</p> <p>EDIT: To be clear, the <strong>mutual exclusion solutions only makes sense if the generation of the random numbers is not the bulk of the thread's work</strong> (that is <code>// presumably some more code...</code> in the example exists and doesn't take a trivial amount of time to complete). The critical section only needs to encompass the access to the shared variable, changing your architecture a little would allow you finer control over that (and in the thread-local storage case, could also allow you to avoid passing an <code>eng</code> reference around)</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload