CSC/ECE 506 Spring 2015/37 mr: Difference between revisions
Jump to navigation
Jump to search
No edit summary |
No edit summary |
||
Line 1: | Line 1: | ||
== A (simple) Introduction to OpenMP == | |||
# | How to run the samples on EOS | ||
gcc -fopenmp hello.c -o hello | |||
== Question 1 == | |||
'''Write a C program that will transpose a 5 x 5 matrix and parallelize the resulting code''' | |||
---- | |||
The code to parallelize: | |||
#include <stdio.h> | |||
int main() | |||
{ | |||
int size = 5; | |||
int i, j; | |||
int first[5][5] = { { 1, 2, 3, 4, 5 }, { 6, 7, 8, 9, 10 }, { 11, 12, 13, 14, 15 }, {16, 17, 18, 19, 20}, {21, 22, 23, 24, 25} }; | |||
int second[5][5]; | |||
for ( i = 0 ; i < size ; i++ ){ | |||
for ( j = 0 ; j < size ; j++ ){ | |||
second[i][j] = first[j][i]; | |||
} | |||
} | |||
printf("Transposed matric:-\n"); | |||
for ( i = 0 ; i < size ; i++ ) | |||
{ | |||
for ( j = 0 ; j < size ; j++ ) | |||
printf("%d\t", second[i][j]); | |||
printf("\n"); | |||
} | |||
return 0; | |||
} | |||
Note that this can be done using DOALL with openMP because we created a copy of the matrix! | |||
== Answer 1 == | |||
int main() | |||
{ | |||
omp_set_num_threads(4); | |||
#pragma omp parallel | |||
{ | |||
int size = 5; | |||
int i, j; | |||
int first[5][5] = { { 1, 2, 3, 4, 5 }, { 6, 7, 8, 9, 10 }, { 11, 12, 13, 14, 15 }, {16, 17, 18, 19, 20}, {21, 22, 23, 24, 25} }; | |||
int second[5][5]; | |||
#pragma omp parallel for default (shared) | |||
for ( i = 0 ; i < size ; i++ ){ | |||
for ( j = 0 ; j < size ; j++ ){ | |||
second[i][j] = first[j][i]; | |||
} | |||
} | |||
printf("Transposed matrix:\n"); | |||
for ( i = 0 ; i < size ; i++ ) | |||
{ | |||
for ( j = 0 ; j < size ; j++ ) | |||
printf("%d\t", second[i][j]); | |||
printf("\n"); | |||
} | |||
} | |||
return 0; | |||
} | |||
== Question 2 == | |||
'''Parallelize the code below using loop distribution''' | '''Parallelize the code below using loop distribution''' | ||
---- | ---- |
Revision as of 03:20, 29 January 2015
A (simple) Introduction to OpenMP
How to run the samples on EOS
gcc -fopenmp hello.c -o hello
Question 1
Write a C program that will transpose a 5 x 5 matrix and parallelize the resulting code
The code to parallelize:
#include <stdio.h>
int main() { int size = 5; int i, j; int first[5][5] = { { 1, 2, 3, 4, 5 }, { 6, 7, 8, 9, 10 }, { 11, 12, 13, 14, 15 }, {16, 17, 18, 19, 20}, {21, 22, 23, 24, 25} }; int second[5][5]; for ( i = 0 ; i < size ; i++ ){ for ( j = 0 ; j < size ; j++ ){ second[i][j] = first[j][i]; } } printf("Transposed matric:-\n"); for ( i = 0 ; i < size ; i++ ) { for ( j = 0 ; j < size ; j++ ) printf("%d\t", second[i][j]); printf("\n"); } return 0;
}
Note that this can be done using DOALL with openMP because we created a copy of the matrix!
Answer 1
int main() { omp_set_num_threads(4); #pragma omp parallel { int size = 5; int i, j; int first[5][5] = { { 1, 2, 3, 4, 5 }, { 6, 7, 8, 9, 10 }, { 11, 12, 13, 14, 15 }, {16, 17, 18, 19, 20}, {21, 22, 23, 24, 25} }; int second[5][5]; #pragma omp parallel for default (shared) for ( i = 0 ; i < size ; i++ ){ for ( j = 0 ; j < size ; j++ ){ second[i][j] = first[j][i]; } } printf("Transposed matrix:\n"); for ( i = 0 ; i < size ; i++ ) { for ( j = 0 ; j < size ; j++ ) printf("%d\t", second[i][j]); printf("\n"); } } return 0; }
Question 2
Parallelize the code below using loop distribution
for (i=0; i<n; i++) { S1: a[i] = b[i-1] * a[i+1]; S2: b[i] = b[i] * 3; S3: c[i] = .2 * (c[i-1] * c[i]); }
After loop distribution
for(i=0; i < n; i++) S1: a[i] = b[i-1] * a[i+1]; for(i=0; i < n; i++) S3: c[i] = .2* c[i-1] * c[i]; for(i=0; i < n; i++) S2: b[i] = b[i] * 3;
Now write the parallelized code in openMP using C++
#pragma omp parallel shared(a,b,c)private(i) { #pragma omp sections nowait { #pragma omp section for(i=0; i < n; i++) a[i] = b[i-1] * a[i+1]; #pragma omp section for(i=0; i<n; i++) c[i] = .2* c[i-1]*c[i]; #pragma omp section for(i=0; i < n; i++) b[i] = b[i] * 3; }//end omp sections } //end omp parallel