I was searching Google for a page offering some simple OpenMp algorithms.
Probably there is an example to calculate min, max, median, average from a huge data array but I am not capable to find it.
At least I would normally try to divide the array into one chunk for each core and do some boundary calculation afterwards to get the result for the complete array.
I just didn’t want to reinvent the wheel.
Additional Remark:
I know that there are thousands of examples that work with simple reduction.
e.g. Calculating PI.
const int num_steps = 100000;
double x, sum = 0.0;
const double step = 1.0/double(num_steps);
#pragma omp parallel for reduction(+:sum) private(x)
for (int i=1;i<= num_steps; i++){
x = double(i-0.5)*step;
sum += 4.0/(1.0+x*x);
}
const double pi = step * sum;
but when these kind of algorithms aren’t usable there are almost no examples left for reducing algorithms.
OpenMP (at least 2.0) supports reduction for some simple operations, but not for max and min.
In the following example the
reductionclause is used to make a sum and acriticalsection is used to update a shared variable using a thread-local one without conflicts.EDIT: a cleaner implementation: