3.3.2 Filling an array with random numbers using unequal chucks

The following example is very nearly the same as the previous one, except towards the end of the main function, we place each random number generated into an array, so that the array is now filled with the stream of random numbers.

TO DO:

Scroll down to near line 153 in the code to see that the next random value that each thread gets from the stream is placed in an array. This is closer to a realistic example of how random number generators would be used in practice. This also illustrates that we can store the stream of random numbers exactly as they are designed to be produced and that the stream does not change as we change the number of threads filling the array.

Note

The most important aspect of this example is that by using the straightforward for loop with the OpenMP pragma added to it, we are filling this array in a way that is efficient for cache memory use: each thread fills a contiguous region of memory. This is important with applications that use large arrays, which is typical in high performance computing.

Next up: let’s see how this is done with 2-dimensional arrays.

Getting start and stop values for reference

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. It hasn’t changed.

You have attempted of activities on this page