cs 470/570 lecture 7 dot product examples odd-even transposition sort more openmp directives

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

Upload: raymond-hensley

Post on 22-Dec-2015

217 views

Category:

Documents


0 download

TRANSCRIPT

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

CS 470/570 Lecture 7

• Dot Product Examples• Odd-even transposition sort• More OpenMP Directives

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

Dot Product Example 1

#pragma omp parallel forfor ( i = 0; i < 5; i++){

int j;j = A[i]*B[i];#pragma omp atomic

sum = sum + j;}

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

Dot Product Example 2#pragma omp parallel for num_threads(5) reduction(+:sum)for ( i = 0; i < 5; i++){

sum = A[i]*B[i];}

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

Dot Product Example 3#pragma omp parallel for reduction(+:sum)for ( i = 0; i < 5; i++){

sum = sum+A[i]*B[i];}

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

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 */

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

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 */

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

Odd-even Transposition Sortvoid 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

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

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 */

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

Odd-even Transposition Sortvoid 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; } }

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

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 */

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

Synchronization Directives

• #pragma omp master• #pragma omp critical• #pragma omp barrier

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

#pragma omp master

• Only the master thread executes the structured block

• Syntax#pragma omp master

structured_block

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

#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 atomicsingle statement

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

#pragma omp criticalint 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);

}}

}

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

#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

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

#pragma omp barrierint 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 barrierprintf("%d %d\n", omp_get_thread_num(), x);

}}