-
-
Save jingkaihe/7106062 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <math.h> | |
#define N 729 | |
#define reps 100 | |
#include <omp.h> | |
double a[N][N], b[N][N], c[N]; | |
int jmax[N]; | |
void init1(void); | |
void init2(void); | |
void loop1(int chuncaksize); | |
void loop2(int chuncksize); | |
void valid1(void); | |
void valid2(void); | |
int main(int argc, char *argv[]) { | |
double start1,start2,end1,end2; | |
int r; | |
int chunksize = atoi(argv[1]); | |
init1(); | |
start1 = omp_get_wtime(); | |
for (r=0; r<reps; r++){ | |
loop1(chunksize); | |
} | |
end1 = omp_get_wtime(); | |
valid1(); | |
printf("Total time for %d reps of loop 1 = %f\n",reps, (float)(end1-start1)); | |
init2(); | |
start2 = omp_get_wtime(); | |
for (r=0; r<reps; r++){ | |
loop2(chunksize); | |
} | |
end2 = omp_get_wtime(); | |
valid2(); | |
printf("Total time for %d reps of loop 2 = %f\n",reps, (float)(end2-start2)); | |
} | |
void init1(void){ | |
int i,j; | |
for (i=0; i<N; i++){ | |
for (j=0; j<N; j++){ | |
a[i][j] = 0.0; | |
b[i][j] = 3.142*(i+j); | |
} | |
} | |
} | |
void init2(void){ | |
int i,j, expr; | |
for (i=0; i<N; i++){ | |
expr = i%( 3*(i/30) + 1); | |
if ( expr == 0) { | |
jmax[i] = N; | |
} | |
else { | |
jmax[i] = 1; | |
} | |
c[i] = 0.0; | |
} | |
for (i=0; i<N; i++){ | |
for (j=0; j<N; j++){ | |
b[i][j] = (double) (i*j+1) / (double) (N*N); | |
} | |
} | |
} | |
void loop1(int chunksize) { | |
int i,j; | |
#pragma omp parallel for private(i,j) shared(a, b) schedule(guided, chunksize) | |
for (i=0; i<N; i++){ | |
for (j=N-1; j>i; j--){ | |
a[i][j] += cos(b[i][j]); | |
} | |
} | |
} | |
void loop2(int chunksize) { | |
int i,j,k; | |
double rN2; | |
rN2 = 1.0 / (double) (N*N); | |
#pragma omp parallel for private(i,j,k) shared(c, b, rN2) schedule(guided, chunksize) | |
for (i=0; i<N; i++){ | |
for (j=0; j < jmax[i]; j++){ | |
for (k=0; k<j; k++){ | |
c[i] += (k+1) * log (b[i][j]) * rN2; | |
} | |
} | |
} | |
} | |
void valid1(void) { | |
int i,j; | |
double suma; | |
suma= 0.0; | |
for (i=0; i<N; i++){ | |
for (j=0; j<N; j++){ | |
suma += a[i][j]; | |
} | |
} | |
printf("Loop 1 check: Sum of a is %lf\n", suma); | |
} | |
void valid2(void) { | |
int i; | |
double sumc; | |
sumc= 0.0; | |
for (i=0; i<N; i++){ | |
sumc += c[i]; | |
} | |
printf("Loop 2 check: Sum of c is %f\n", sumc); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment