Exercise: Parallel Histogram

The following code fragment calculates a histogram with 16 bins from the results of the standard rand_r() random number generator:

unsigned int seed = 123;
long hist[16];
for(int i=0; i<16; ++i)


timing(&wcstart, &ct);
for(long i=0; i<1000000000; ++i) {

hist[rand_r(&seed) & 0xf]++;

timing(&wcend, &ct);
for(int i=0; i<16; ++i) {

cout << "hist[" << i << "]=" << hist[i] << endl;

cout << "Time: " << wcend-wcstart << " sec" << endl;

Parallelize the histogram calculation using OpenMP. You can find serial (Fortran and C) example codes in the EX/HISTO folder. Look at the runtimes for the original (serial) code, the 1-thread and the 20-thread parallel versions on one Emmy node. Does your solution scale? If it does not, make it scale!

Last modified: Tuesday, 24 March 2015, 8:06 AM