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

From Expertiza_Wiki
Jump to navigation Jump to search
No edit summary
No edit summary
Line 1: Line 1:
# Example 1
# Example 1
# Example 2
# Example 2
'''Parallelize the code below using loop distribution'''
'''Parallelize the code below using loop distribution'''
Line 13: Line 14:
----
----


for(i=0; i < n; i++)   
for(i=0; i < n; i++)   
S1: a[i] = b[i-1] * a[i+1];  
  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;
  S2: b[i] = b[i] * 3;
 
----
----


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

Revision as of 03:04, 29 January 2015

  1. Example 1
  1. Example 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