Exercise: Parallel histogram computation in MPI
Completion requirements
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 code with MPI. Do you get significant speedups when running up to 72 processes on the node?
Reminder: In order to compile an MPI program, you have to use one of the wrapper scripts mpiicx, mpiicpx, or mpiifx instead of the normal Intel compiler for C, C++, and Fortran code, respectively. For running the code you use mpirun:$ mpirun -np # ./my_executable
Here, "#" is the number of processes you want to use. - You can also parallelize the code with OpenMP for extra fun.
Last modified: Wednesday, 25 February 2026, 11:31 AM