Statistiques
| Révision :

root / ETSN / MySteps_6_openacc.c @ 299

Historique | Voir | Annoter | Télécharger (3,37 ko)

1
/* Simple SillySum 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_6_openacc MySteps_6_openacc.c -lm */
3
/* compilation without sequential compute : gcc -DNOSERIAL -O3 -fopenacc -foffload=nvptx-none -foffload="-O3 -misa=sm_35 -lm" -o MySteps_6_openacc_NoSerial MySteps_6_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 MySillySum(MYFLOAT *res, MYFLOAT *a, MYFLOAT *b,int calls, int size)
21
{
22
  for (uint i=0; i<size;i++) 
23
    {
24
      MYFLOAT ai=a[i];
25
      MYFLOAT bi=b[i];
26
      
27
      for (int c=0;c<calls;c++)
28
        {
29
          ai=MySillyFunction(ai);
30
          bi=MySillyFunction(bi);
31
        }
32

    
33
      res[i] = ai + bi;
34
    }
35
}
36

    
37
void MySillySumOpenACC(MYFLOAT *res, MYFLOAT *a, MYFLOAT *b,int calls, int size)
38
{
39
  #pragma acc data copyin(a[0:size],b[0:size]),copyout(res[0:size])
40
  #pragma acc parallel loop
41
  for (uint i=0; i<size;i++) 
42
    {
43
      MYFLOAT ai=a[i];
44
      MYFLOAT bi=b[i];
45
      
46
      for (int c=0;c<calls;c++)
47
        {
48
          ai=MySillyFunction(ai);
49
          bi=MySillyFunction(bi);
50
        }
51

    
52
      res[i] = ai + bi;
53
    }
54
}
55

    
56
MYFLOAT MyNorm(MYFLOAT *a,MYFLOAT *b,int size)
57
{
58
  MYFLOAT norm=0.;
59

    
60
  for (int i=0;i<size;i++)
61
    {
62
      norm+=pow(a[i]-b[i],2);
63
    }
64

    
65
  return(sqrt(norm));
66
}
67

    
68
void MyPrint(MYFLOAT *a,int size)
69
{
70
  printf("[");
71
  for (int i=0;i<size;i++)
72
    {
73
      printf(" %.8e ",a[i]);
74
    }
75
  printf("]\n");
76
}
77

    
78
int main(int argc,char *argv[])
79
{
80
  float *a,*b,*res,*resacc;
81
  int size=1024;
82
  int calls=1;
83
  struct timeval tv1,tv2;
84
 
85
  if (argc > 1) {
86
    size=(int)atoll(argv[1]);
87
    calls=(int)atoll(argv[2]);
88
  }
89
  else {
90
    printf("\n\tPi : Estimate SillySum\n\n\t\t#1 : size (default 1024)\n\t\t#2 : calls (default 1)\n\n");
91
  }
92

    
93
  printf("%i %i\n",size,calls);
94
  
95
  a=(float*)malloc(size*sizeof(MYFLOAT));
96
  b=(float*)malloc(size*sizeof(MYFLOAT));
97
  res=(float*)malloc(size*sizeof(MYFLOAT));
98
  resacc=(float*)malloc(size*sizeof(MYFLOAT));
99

    
100
  srand(110271);
101
  
102
  for (int i=0;i<size;i++)
103
    {
104
      a[i]=(MYFLOAT)rand()/(MYFLOAT)RAND_MAX;
105
      b[i]=(MYFLOAT)rand()/(MYFLOAT)RAND_MAX;
106
      res[i]=0.;
107
      resacc[i]=0.;
108
    }
109

    
110
#ifndef NOSERIAL 
111
  gettimeofday(&tv1, NULL);
112
  MySillySum(res,a,b,calls,size);
113
  gettimeofday(&tv2, NULL);
114
#endif
115

    
116
  MYFLOAT elapsed=(MYFLOAT)((tv2.tv_sec-tv1.tv_sec) * 1000000L +
117
                            (tv2.tv_usec-tv1.tv_usec))/1000000;
118

    
119
  gettimeofday(&tv1, NULL);
120
  MySillySumOpenACC(resacc,a,b,calls,size);
121
  gettimeofday(&tv2, NULL);
122

    
123
  MYFLOAT elapsedAcc=(MYFLOAT)((tv2.tv_sec-tv1.tv_sec) * 1000000L +
124
                               (tv2.tv_usec-tv1.tv_usec))/1000000;
125

    
126
#ifndef NOSERIAL 
127
  MYFLOAT MyChecker=MyNorm(res,resacc,size);
128
  printf("Norm: %.8e\n",MyChecker);
129
#endif
130

    
131
#ifdef VERBOSE
132
  MyPrint(res,size);
133
  MyPrint(resacc,size);
134
#endif
135
  
136
#ifndef NOSERIAL 
137
  printf("Elapsed Time: %.3f\n",elapsed);
138
  printf("OpenACC Elapsed Time: %.3f\n",elapsedAcc);
139
#endif
140

    
141
#ifndef NOSERIAL 
142
  printf("NativeRate: %.lld\n",(unsigned long)((float)size/elapsed));
143
#endif
144
  printf("OpenACCRate: %.lld\n",(unsigned long)((float)size/elapsedAcc));
145

    
146
#ifndef NOSERIAL 
147
  printf("ACCRatio: %.3f\n",elapsed/elapsedAcc);
148
#endif
149
  
150
  free(a);
151
  free(b);
152
  free(res);
153
  free(resacc);
154
}
155