3.2.2 Parallel loop with chunks of one with 2 methods of random number streams¶
You should study the code for this version, focusing on the for loop and how it differs from the previous version.
Note
Rule of thumb: splitting a for loop into equal chunks requires the #pragma omp for syntax. You do not use this syntax if you move forward the proper amount per thread yourself in the code. Note how this is how we accomplish “chunks of one” in the loop on line 117 by incrementing the loop counter by the number of threads (i+=numThreads).
As a reference, here is the set of numbers when using one thread, which is the same as the sequential version:
t 0( 0):83
t 0( 1):63
t 0( 2):97
t 0( 3):21
t 0( 4):62
t 0( 5):54
t 0( 6):14
t 0( 7):46
Now try different setups for this code, as you did for the equal chunks version:
['-c', '-n 8', '-d block', '-t 2']
['-c', '-n 8', '-d block', '-t 4']
['-c', '-n 8', '-d leapfrog', '-t 2']
['-c', '-n 8', '-d leapfrog', '-t 4']
['-c', '-n 8', '-d leapfrog', '-t 3']
Note that the output is explaining how it is running. You should be able to observe the following:
With block splitting and 2 threads, thread 0 still gets the first block of 4 values from the sequential version, and thread 1 gets the next block, even though the loop indexes it completes are different from the previous equal chunk loop version.
With leapfrog and 2 threads, each thread gets the same numbers as the equal chunk loop version:
Thread 0 should get every other number: 83, 97, 62, 14
Thread 1 should get every other number: 63, 21, 54, 46
Leapfrog works with 3 threads
Command line code for reference¶
This code block below has code for handling the command line arguments for the parallel versions in this subsection and the previous one.
