c - openmp recursive tasks example slower than sequential -
i've started looking openmp , have been reading on tasks. seems sun's illustration here slower sequential version. believe has overhead of task creation , management. correct? if so, there way create code faster using tasks without changing algorithm?
int fib(int n) { int i, j; if (n<2) homecoming n; else { #pragma omp task shared(i) firstprivate(n) i=fib(n-1); #pragma omp task shared(j) firstprivate(n) j=fib(n-2); #pragma omp taskwait homecoming i+j; } }
the sensible way cutting parallelism @ level, below makes no sense overhead becomes larger work beingness done. best way have 2 separate fib
implementations - serial one, let's fib_ser
, , parallel one, fib
- , switch between them under given threshold.
int fib_ser(int n) { if (n < 2) homecoming n; else homecoming fib_ser(n-1) + fib_ser(n-2); } int fib(int n) { int i, j; if (n <= 20) homecoming fib_ser(n); else { #pragma omp task shared(i) = fib(n-1); #pragma omp task shared(j) j = fib(n-2); #pragma omp taskwait homecoming i+j; } }
here threshold n == 20
. chosen arbitrarily , optimal value different on different machines , different openmp runtimes.
another alternative dynamically command tasking if
clause:
int fib(int n) { int i, j; if (n<2) homecoming n; else { #pragma omp task shared(i) if(n > 20) i=fib(n-1); #pragma omp task shared(j) if(n > 20) j=fib(n-2); #pragma omp taskwait homecoming i+j; } }
this switch off 2 explicit tasks if n <= 20
, , code execute in serial, there still overhead openmp code transformation, run slower previous version separate serial implementation.
c parallel-processing openmp
No comments:
Post a Comment