Exercise: Parallel histogram computation
The following code calculates a histogram with 16 bins (the least significant four bits) from the results of the standard rand_r() random number generator. You can find serial (Fortran and C) example codes in the HISTO
folder.
unsigned int seed = 123;
long hist[16];
for(int i=0; i<16; ++i)
hist[i]=0;
timing(&wcstart, &ct);
for(long i=0; i<2000000000; ++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. Does your parallel code scale to 36 cores on a Fritz socket? If it does not, make it scale! (It really should!)
- Parallelize the code with MPI. Same deal.
Note that "the code scales" means that it runs (almost) N times faster with N threads/processes than with a single thread/process.
Last modified: Wednesday, 21 February 2024, 11:30 AM