Splitting an arbitrary range of numbers into rougly equal size partitions in C using OpenMP -
i split range of numbers equal size in c using openmp. example, if have range 7 through 24 , number of threads 8. first thread begin 7 , end @ 9. second thread begin 10 , end @ 12. third thread begin 13 , end @ 14. fourth thread begin @ 15 , end @ 16 , forth... until last thread begins @ 23 , ends @ 24. code wrote follows, not getting explained results. wondering if there missed can or there more efficient way of doing this? appreciated.
note of predefined declaration of variables based on example given above:
first = 7 last = 24 size = 2 (which signifies amount of numbers per thread) r = 2 (r signifies remainder) nthreads = 8 myid = thread id in range of 0 7
if (r > 0) { if (myid == 0) { start = first + myid*size; end = start + size; } else if (myid == nthreads - 1) { start = first + myid*size + myid; end = last; } else { start = first + myid*size + myid; end = start + size; } } else { start = first + myid*size; if (myid == nthreads - 1) end = last; else end = start + size - 1; }
as far remember, #pragma omp parallel for
automatically divides work between threads in equal chunks, , ok in situations.
however, if want manually, here piece of code want:
int len = last - first + 1; int chunk = len / nthreads; int r = len % nthreads; if (myid < r) { start = first + (chunk + 1) * myid; end = start + chunk; } else { start = first + (chunk + 1) * r + chunk * (myid - r); end = start + chunk - 1; }
if there no additional restrictions, such distribution indeed optimal.
Comments
Post a Comment