Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS 470/570 Lecture 7 Dot Product Examples Odd-even transposition sort More OpenMP Directives.

Similar presentations


Presentation on theme: "CS 470/570 Lecture 7 Dot Product Examples Odd-even transposition sort More OpenMP Directives."— Presentation transcript:

1 CS 470/570 Lecture 7 Dot Product Examples Odd-even transposition sort More OpenMP Directives

2 Dot Product Example 1 #pragma omp parallel for for ( i = 0; i < 5; i++) { int j; j = A[i]*B[i]; #pragma omp atomic sum = sum + j; }

3 Dot Product Example 2 #pragma omp parallel for num_threads(5) reduction(+:sum) for ( i = 0; i < 5; i++) { sum = A[i]*B[i]; }

4 Dot Product Example 3 #pragma omp parallel for reduction(+:sum) for ( i = 0; i < 5; i++) { sum = sum+A[i]*B[i]; }

5 Odd-even Transposition Sort void Odd_even_sort(int a[], int n ) { int phase, i, temp; for (phase = 0; phase < n; phase++) if (phase % 2 == 0) { /* Even phase */ for (i = 1; i < n; i += 2) if (a[i-1] > a[i]) { temp = a[i]; a[i] = a[i-1]; a[i-1] = temp; } } else { /* Odd phase */

6 Odd-even Transposition Sort } else { /* Odd phase */ for (i = 1; i < n-1; i += 2) if (a[i] > a[i+1]) { temp = a[i]; a[i] = a[i+1]; a[i+1] = temp; } } /* Odd_even_sort */

7 Odd-even Transposition Sort void Odd_even(int a[], int n) { int phase, i, tmp; for (phase = 0; phase < n; phase++) { if (phase % 2 == 0) # pragma omp parallel for num_threads(thread_count) \ default(none) shared(a, n) private(i, tmp) for (i = 1; i < n; i += 2) { if (a[i-1] > a[i]) { tmp = a[i-1]; a[i-1] = a[i]; a[i] = tmp; } else

8 Odd-even Transposition Sort # pragma omp parallel for num_threads(thread_count) \ default(none) shared(a, n) private(i, tmp) for (i = 1; i < n-1; i += 2) { if (a[i] > a[i+1]) { tmp = a[i+1]; a[i+1] = a[i]; a[i] = tmp; } } /* Odd_even */

9 Odd-even Transposition Sort void Odd_even(int a[], int n) { int phase, i, tmp; # pragma omp parallel num_threads(thread_count) \ default(none) shared(a, n) private(i, tmp, phase) for (phase = 0; phase < n; phase++) { if (phase % 2 == 0) # pragma omp for for (i = 1; i < n; i += 2) { if (a[i-1] > a[i]) { tmp = a[i-1]; a[i-1] = a[i]; a[i] = tmp; }

10 Odd-even Transponder Sort else # pragma omp for for (i = 1; i < n-1; i += 2) { if (a[i] > a[i+1]) { tmp = a[i+1]; a[i+1] = a[i]; a[i] = tmp; } } /* Odd_even */

11 Synchronization Directives #pragma omp master #pragma omp critical #pragma omp barrier

12 #pragma omp master Only the master thread executes the structured block Syntax #pragma omp master structured_block

13 #pragma omp critical Identify code that should be executed by one thread at a time Syntax #pragma omp critical [ name ] structured_block Related expression #pragma omp atomic single statement

14 #pragma omp critical int main(int argc, char *argv[]) { int x; x = 0; #pragma omp parallel num_threads(4) { #pragma omp critical { x = x + 1; printf("%d %d\n", omp_get_thread_num(), x); }

15 #pragma omp barrier Establish a barrier synchronization point in a parallel region Threads block at the barrier until all threads have reached the barrier Useful when you need a synchronization point in a parallel region Syntax – #pragma omp barrier

16 #pragma omp barrier int main(int argc, char *argv[]) { int x = 0; x = 0; #pragma omp parallel num_threads(4) { #pragma omp critical { x = x + 1; printf("%d %d\n", omp_get_thread_num(), x); } #pragma omp barrier printf("%d %d\n", omp_get_thread_num(), x); }


Download ppt "CS 470/570 Lecture 7 Dot Product Examples Odd-even transposition sort More OpenMP Directives."

Similar presentations


Ads by Google