SLIDE 13 %*"
Simple example of using tasks! in a recursive algorithm!
int fib(int n) { int i, j; if (n < 2) return n; i = fib(n - 1); j = fib(n - 2); return i + j; } int main() { int n = 10; printf("fib(%d) = %d\n", n, fib(n)); } #pragma omp parallel #pragma omp single #pragma omp task shared(i) #pragma omp task shared(j) #pragma omp taskwait
Computation of Fibonacci numbers! 1,1,2,3,5,8,13,21,34,55,89,144,...!
&+"
Using tasks for tree traversal!
struct node { struct node *left, *right; }; void traverse(struct node *p, int postorder) { if (p->left != NULL) #pragma omp task traverse(p->left, postorder); if (p->right != NULL) #pragma omp task traverse(p->right, postorder); if (postorder) { #pragma omp taskwait } process(p); }
&!"
Task switching!
Certain constructs have suspend/resume points at defined positions within them.! When a thread encounters a suspend point, it is allowed to suspend the current task and resume another. It can then return to the original task and resume it.! A tied task must be resumed by the same task that suspended it.! Tasks are tied by default. A task can be specified to be untied using! ! ! !#pragma omp task untied
&#"
Collapsing of loops!
The collapse clause (in OpenMP 3.0) handles perfectly nested multi-dimensional loops.!
#pragma omp for collapse(2) for (i = 0; i < N; i++) for (j = 0; j < M; j++) for (k = 0; k < K; k++) foo(i, j, k);
Iteration space from i-loop and j-loop is collapsed into a single
- ne, if the two loops are perfectly nested and form a rectangular
iteration space. !