CSC/ECE 506 Spring 2015/37 mr: Difference between revisions

From Expertiza_Wiki
Jump to navigation Jump to search
No edit summary
 
(18 intermediate revisions by 2 users not shown)
Line 1: Line 1:
# Example 1
== A (simple) Introduction to OpenMP ==
# Example 2
 
How to run the samples on EOS
 
gcc -fopenmp hello.c -o hello
 
== Question 1 ==
'''Parallelize the following code using OpenMP'''
----
 
The code to parallelize:
int main()
{
      omp_set_num_threads(6);
      long sum = 1;
      int size = 5;
      int i, j, rowsum;
      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} };
      for ( i = 0 ; i < size ; i++ ){
              rowsum = 0;
              for ( j = 0 ; j < size ; j++ ){
                      rowsum = rowsum + first[i][j];
              }
              sum = rowsum*sum;
      }
      printf("sum: %d\n", sum);
    return 0;
}
 
== Answer 1 ==
int main()
{
        omp_set_num_threads(6);
        long sum = 1;
        int size = 5;
        int i, j, rowsum;
  #pragma omp parallel shared(rowsum, i, sum) private(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} };
        #pragma omp for
        for ( i = 0 ; i < size ; i++ ){
                rowsum = 0;
                for ( j = 0 ; j < size ; j++ ){
                        rowsum = rowsum + first[i][j];
                }
                #pragma omp critical
                sum = rowsum*sum;
        }
  }
        printf("sum: %d\n", sum);
    return 0;
}
 
== Question 2 ==
'''Parallelize the code below using loop distribution'''
'''Parallelize the code below using loop distribution'''
----


  for (i=0; i<n; i++) {  
  for (i=0; i<n; i++) {  
Line 10: Line 63:
   
   
'''After loop distribution'''
'''After loop distribution'''
<nowiki> for(i=0; i < n; i++)  
----
S1: a[i] = b[i-1] * a[i+1];  
 
for(i=0; i < n; i++)
  S1: a[i] = b[i-1] * a[i+1];  
  for(i=0; i < n; i++)  
  for(i=0; i < n; i++)  
S3: c[i] = .2* c[i-1] * c[i];
  S3: c[i] = .2* c[i-1] * c[i];
  for(i=0; i < n; i++)  
  for(i=0; i < n; i++)  
S2: b[i] = b[i] * 3</nowiki>;
  S2: b[i] = b[i] * 3;
 
----


'''Now write the parallelized code in openMP using C++'''
'''Now write the parallelized code in openMP using C++'''
<nowiki>#pragma omp parallel shared(a,b,c)private(i) {
----
#pragma omp sections nowait
#pragma omp parallel shared(a,b,c)private(i) {
{
  #pragma omp sections nowait
#pragma omp section
  {
for(i=0; i < n; i++)
        #pragma omp section
a[i] = b[i-1] * a[i+1];
        for(i=0; i < n; i++)
#pragma omp section
          a[i] = b[i-1] * a[i+1];
for(i=0; i<n; i++)
        #pragma omp section
c[i] = .2* c[i-1]*c[i];
        for(i=0; i<n; i++)
#pragma omp section
        c[i] = .2* c[i-1]*c[i];
for(i=0; i < n; i++)
        #pragma omp section
b[i] = b[i] * 3;
        for(i=0; i < n; i++)
}//end omp sections
        b[i] = b[i] * 3;
} //end omp parallel
  }//end omp sections
</nowiki>
} //end omp parallel

Latest revision as of 04:49, 3 February 2015

A (simple) Introduction to OpenMP

How to run the samples on EOS

gcc -fopenmp hello.c -o hello

Question 1

Parallelize the following code using OpenMP


The code to parallelize:

int main()
{
      omp_set_num_threads(6);
      long sum = 1;
      int size = 5;
      int i, j, rowsum;
      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} };
      for ( i = 0 ; i < size ; i++ ){
              rowsum = 0;
              for ( j = 0 ; j < size ; j++ ){
                      rowsum = rowsum + first[i][j];
              }
              sum = rowsum*sum;
      }
      printf("sum: %d\n", sum);
   return 0;
}

Answer 1

int main()
{
       omp_set_num_threads(6);
       long sum = 1;
       int size = 5;
       int i, j, rowsum;
 #pragma omp parallel shared(rowsum, i, sum) private(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} };
       #pragma omp for
       for ( i = 0 ; i < size ; i++ ){
               rowsum = 0;
               for ( j = 0 ; j < size ; j++ ){
                       rowsum = rowsum + first[i][j];
               }
               #pragma omp critical
               sum = rowsum*sum;
       }
 }
       printf("sum: %d\n", sum);
    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