Presentation is loading. Please wait.

Presentation is loading. Please wait.

2 3 Parent Thread Fork Join Start End Child Threads Compute time Overhead.

Similar presentations


Presentation on theme: "2 3 Parent Thread Fork Join Start End Child Threads Compute time Overhead."— Presentation transcript:

1

2 2

3 3 Parent Thread Fork Join Start End Child Threads Compute time Overhead

4 4

5 5 Fork Join Start End Fork Join Start End Idle

6 6

7

8 8 http://www.openmp.org Current spec is OpenMP 3.0 318 Pages (combined C/C++ and Fortran)

9 9 Parallel Regions Master Thread

10 10

11 11

12 12

13 13

14 14 Automatically divides work among threads

15 15 #pragma omp parallel #pragma omp for Implicit barrier i = 1 i = 2 i = 3 i = 4 i = 5 i = 6 i = 7 i = 8 i = 9 i = 10 i = 11 i = 12 // assume N=12 #pragma omp parallel #pragma omp for for(i = 1, i < N+1, i++) c[i] = a[i] + b[i];

16 16 #pragma omp parallel { #pragma omp for for (i=0;i< MAX; i++) { res[i] = huge(); } #pragma omp parallel for for (i=0;i< MAX; i++) { res[i] = huge(); }

17 17 void* work(float* c, int N) { float x, y; int i; #pragma omp parallel for private(x,y) for(i=0; i<N; i++) { x = a[i]; y = b[i]; c[i] = x + y; }

18 18

19 19 #pragma omp parallel for schedule (static, 8) for( int i = start; i <= end; i += 2 ) { if ( TestForPrime(i) ) gPrimesFound++; } Iterations are divided into chunks of 8 If start = 3, then first chunk is i ={3,5,7,9,11,13,15,17}

20 20

21 21

22 22 temp A, index, count temp A, index, count Which variables are shared and which variables are private? float A[10]; main () { integer index[10]; #pragma omp parallel { Work (index); } printf (“%d\n”, index[1]); } extern float A[10]; void Work (int *index) { float temp[10]; static integer count; } A, index, and count are shared by all threads, but temp is local to each thread

23 23 float dot_prod(float* a, float* b, int N) { float sum = 0.0; #pragma omp parallel for for(int i=0; i<N; i++) { sum += a[i] * b[i]; } return sum; } What is Wrong?

24 24

25 25 Value of area Thread AThread B 11.667 +3.765 15.432 + 3.563 18.995 Value of area Thread A Thread B 11.667 +3.765 11.667 15.432 + 3.563 15.230 Order of thread execution causes non determinant behavior in a data race

26 26 float dot_prod(float* a, float* b, int N) { float sum = 0.0; #pragma omp parallel for for(int i=0; i<N; i++) { #pragma omp critical sum += a[i] * b[i]; } return sum; }

27 27 float RES; #pragma omp parallel { float B; #pragma omp for for(int i=0; i<niters; i++){ B = big_job(i); #pragma omp critical (RES_lock) consum (B, RES); } } Threads wait their turn –at a time, only one calls consum() thereby protecting RES from race conditions Naming the critical construct RES_lock is optional Good Practice – Name all critical sections

28 28

29 29 #pragma omp parallel for reduction(+:sum) for(i=0; i<N; i++) { sum += a[i] * b[i]; }

30 30 OperandInitial Value +0 *1 -0 ^0 OperandInitial Value &~0 |0 &&1 ||0

31 31 4.0 2.0 1.0 0.0 4.0 (1+x 2 ) f(x) = X  4.0 (1+x 2 ) dx =  0 1 static long num_steps=100000; double step, pi; void main() { int i; double x, sum = 0.0; step = 1.0/(double) num_steps; for (i=0; i< num_steps; i++){ x = (i+0.5)*step; sum = sum + 4.0/(1.0 + x*x); } pi = step * sum; printf(“Pi = %f\n”,pi);}

32 32

33 33 #pragma omp parallel { DoManyThings(); #pragma omp single { ExchangeBoundaries(); } // threads wait here for single DoManyMoreThings(); }

34 34 #pragma omp parallel { DoManyThings(); #pragma omp master { // if not master skip to next stmt ExchangeBoundaries(); } DoManyMoreThings(); }

35 35

36 36 #pragma single nowait { [...] } #pragma omp for nowait for(...) {...}; #pragma omp for schedule(dynamic,1) nowait for(int i=0; i<n; i++) a[i] = bigFunc1(i); #pragma omp for schedule(dynamic,1) for(int j=0; j<m; j++) b[j] = bigFunc2(j);

37 37 #pragma omp parallel shared (A, B, C) { DoSomeWork(A,B); printf(“Processed A into B\n”); #pragma omp barrier DoSomeWork(B,C); printf(“Processed B into C\n”); }

38 38 #pragma omp parallel for shared(x, y, index, n) for (i = 0; i < n; i++) { #pragma omp atomic x[index[i]] += work1(i); y[i] += work2(i); }

39 39 a = alice(); b = bob(); s = boss(a, b); c = cy(); printf ("%6.2f\n", bigboss(s,c)); alice,bob, and cy can be computed in parallel alice bob boss bigboss cy

40 40

41 41 #pragma omp parallel sections { #pragma omp section /* Optional */ a = alice(); #pragma omp section b = bob(); #pragma omp section c = cy(); } s = boss(a, b); printf ("%6.2f\n", bigboss(s,c));

42 42 SerialParallel #pragma omp parallel sections { #pragma omp section phase1(); #pragma omp section phase2(); #pragma omp section phase3(); }

43 43

44 44 SerialParallel

45 45 A pool of 8 threads is created here #pragma omp parallel // assume 8 threads { #pragma omp single private(p) { … while (p) { #pragma omp task { processwork(p); } p = p->next; } One thread gets to execute the while loop The single “while loop” thread creates a task for each instance of processwork()

46 46 #pragma omp parallel { #pragma omp single { // block 1 node * p = head; while (p) { //block 2 #pragma omp task process(p); p = p->next; //block 3 }

47 Have potential to parallelize irregular patterns and recursive function calls #pragma omp parallel { #pragma omp single { // block 1 node * p = head; while (p) { //block 2 #pragma omp task process(p); p = p->next; //block 3 } Block1 Block 2 Task 1 Block 2 Task 2 Block 2 Task 3 Block3 Time Single Threaded Block1 Thr1 Thr2 Thr3 Thr4 Block 2 Task 2 Block 2 Task 1 Block 2 Task 3 Time Saved Idle Block3

48 48 Tasks are gauranteed to be complete: At thread or task barriers At the directive: #pragma omp barrier At the directive: #pragma omp taskwait

49 49 #pragma omp parallel { #pragma omp task foo(); #pragma omp barrier #pragma omp single { #pragma omp task bar(); } Multiple foo tasks created here – one for each thread All foo tasks guaranteed to be completed here One bar task created here bar task guaranteed to be completed here

50 50 CS 491 – Parallel and Distributed Computing

51 51 CS 491 – Parallel and Distributed Computing

52 52 CS 491 – Parallel and Distributed Computing while(p != NULL){ do_work(p->data); p = p->next; }

53 53

54 54 CS 491 – Parallel and Distributed Computing

55 55

56 56

57 57

58 58


Download ppt "2 3 Parent Thread Fork Join Start End Child Threads Compute time Overhead."

Similar presentations


Ads by Google