root / ETSN / MySteps_1_openacc.c @ 296
Historique | Voir | Annoter | Télécharger (2,99 ko)
1 |
/* Simple Sum function in C and OpenACC/C */
|
---|---|
2 |
/* compilation with sequential compute : gcc -O3 -fopenacc -foffload=nvptx-none -foffload="-O3 -misa=sm_35 -lm" -o MySteps_1_openacc MySteps_1_openacc.c -lm */
|
3 |
/* compilation without sequential compute : gcc -DNOSERIAL -O3 -fopenacc -foffload=nvptx-none -foffload="-O3 -misa=sm_35 -lm" -o MySteps_1_openacc_NoSerial MySteps_1_openacc.c -lm */
|
4 |
|
5 |
#include <math.h> |
6 |
#include <stdio.h> |
7 |
#include <stdlib.h> |
8 |
#include <sys/time.h> |
9 |
|
10 |
#define PI 3.141592653589793 |
11 |
|
12 |
#define MYFLOAT float |
13 |
|
14 |
/* #pragma acc routine */
|
15 |
/* MYFLOAT MySillyFunction(MYFLOAT x) */
|
16 |
/* { */
|
17 |
/* return(pow(sqrt(log(exp(atanh(tanh(asinh(sinh(acosh(cosh(atan(tan(asin(sin(acos(cos(x))))))))))))))),2)); */
|
18 |
/* } */
|
19 |
|
20 |
void MySum(MYFLOAT *res, MYFLOAT *a, MYFLOAT *b, int size) |
21 |
{ |
22 |
for (uint i=0; i<size;i++) |
23 |
{ |
24 |
res[i] = a[i] + b[i]; |
25 |
} |
26 |
} |
27 |
|
28 |
void MySumOpenACC(MYFLOAT *res, MYFLOAT *a, MYFLOAT *b, int size) |
29 |
{ |
30 |
#pragma acc data copyin(a[0:size],b[0:size]),copyout(res[0:size]) |
31 |
#pragma acc parallel loop
|
32 |
for (uint i=0; i<size;i++) |
33 |
{ |
34 |
res[i] = a[i] + b[i]; |
35 |
} |
36 |
} |
37 |
|
38 |
MYFLOAT MyNorm(MYFLOAT *a,MYFLOAT *b,int size)
|
39 |
{ |
40 |
MYFLOAT norm=0.;
|
41 |
|
42 |
for (int i=0;i<size;i++) |
43 |
{ |
44 |
norm+=pow(a[i]-b[i],2);
|
45 |
} |
46 |
|
47 |
return(sqrt(norm));
|
48 |
} |
49 |
|
50 |
void MyPrint(MYFLOAT *a,int size) |
51 |
{ |
52 |
printf("[");
|
53 |
for (int i=0;i<size;i++) |
54 |
{ |
55 |
printf(" %.8e ",a[i]);
|
56 |
} |
57 |
printf("]\n");
|
58 |
} |
59 |
|
60 |
int main(int argc,char *argv[]) |
61 |
{ |
62 |
float *a,*b,*res,*resacc;
|
63 |
int size=1024; |
64 |
struct timeval tv1,tv2;
|
65 |
|
66 |
if (argc > 1) { |
67 |
size=(int)atoll(argv[1]); |
68 |
} |
69 |
else {
|
70 |
printf("\n\tPi : Estimate SillySum\n\n\t\t#1 : size (default 1024)\n\n");
|
71 |
} |
72 |
|
73 |
printf("%i\n",size);
|
74 |
|
75 |
a=(float*)malloc(size*sizeof(MYFLOAT)); |
76 |
b=(float*)malloc(size*sizeof(MYFLOAT)); |
77 |
res=(float*)malloc(size*sizeof(MYFLOAT)); |
78 |
resacc=(float*)malloc(size*sizeof(MYFLOAT)); |
79 |
|
80 |
srand(110271);
|
81 |
|
82 |
for (int i=0;i<size;i++) |
83 |
{ |
84 |
a[i]=(MYFLOAT)rand()/(MYFLOAT)RAND_MAX; |
85 |
b[i]=(MYFLOAT)rand()/(MYFLOAT)RAND_MAX; |
86 |
res[i]=0.;
|
87 |
resacc[i]=0.;
|
88 |
} |
89 |
|
90 |
#ifndef NOSERIAL
|
91 |
gettimeofday(&tv1, NULL);
|
92 |
MySum(res,a,b,size); |
93 |
gettimeofday(&tv2, NULL);
|
94 |
#endif
|
95 |
|
96 |
MYFLOAT elapsed=(MYFLOAT)((tv2.tv_sec-tv1.tv_sec) * 1000000L +
|
97 |
(tv2.tv_usec-tv1.tv_usec))/1000000;
|
98 |
|
99 |
gettimeofday(&tv1, NULL);
|
100 |
MySumOpenACC(resacc,a,b,size); |
101 |
gettimeofday(&tv2, NULL);
|
102 |
|
103 |
MYFLOAT elapsedAcc=(MYFLOAT)((tv2.tv_sec-tv1.tv_sec) * 1000000L +
|
104 |
(tv2.tv_usec-tv1.tv_usec))/1000000;
|
105 |
|
106 |
#ifndef NOSERIAL
|
107 |
MYFLOAT MyChecker=MyNorm(res,resacc,size); |
108 |
printf("Norm: %.8e\n",MyChecker);
|
109 |
#endif
|
110 |
|
111 |
#ifdef VERBOSE
|
112 |
MyPrint(res,size); |
113 |
MyPrint(resacc,size); |
114 |
#endif
|
115 |
|
116 |
#ifndef NOSERIAL
|
117 |
printf("Elapsed Time: %.3f\n",elapsed);
|
118 |
printf("OpenACC Elapsed Time: %.3f\n",elapsedAcc);
|
119 |
#endif
|
120 |
|
121 |
#ifndef NOSERIAL
|
122 |
printf("NaiveRate: %.lld\n",(unsigned long)((float)size/elapsed)); |
123 |
#endif
|
124 |
printf("OpenACCRate: %.lld\n",(unsigned long)((float)size/elapsedAcc)); |
125 |
|
126 |
#ifndef NOSERIAL
|
127 |
printf("OpenACCRatio: %.3f\n",elapsed/elapsedAcc);
|
128 |
#endif
|
129 |
|
130 |
free(a); |
131 |
free(b); |
132 |
free(res); |
133 |
free(resacc); |
134 |
} |
135 |
|