Statistiques
| Révision :

root / ETSN / MySteps_1_openacc.c @ 298

Historique | Voir | Annoter | Télécharger (2,79 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
void MySum(MYFLOAT *res, MYFLOAT *a, MYFLOAT *b, int size)
15
{
16
  for (uint i=0; i<size;i++) 
17
    {
18
      res[i] = a[i] + b[i];
19
    }
20
}
21

    
22
void MySumOpenACC(MYFLOAT *res, MYFLOAT *a, MYFLOAT *b, int size)
23
{
24
  #pragma acc data copyin(a[0:size],b[0:size]),copyout(res[0:size])
25
  #pragma acc parallel loop
26
  for (uint i=0; i<size;i++) 
27
    {
28
      res[i] = a[i] + b[i];
29
    }
30
}
31

    
32
MYFLOAT MyNorm(MYFLOAT *a,MYFLOAT *b,int size)
33
{
34
  MYFLOAT norm=0.;
35

    
36
  for (int i=0;i<size;i++)
37
    {
38
      norm+=pow(a[i]-b[i],2);
39
    }
40

    
41
  return(sqrt(norm));
42
}
43

    
44
void MyPrint(MYFLOAT *a,int size)
45
{
46
  printf("[");
47
  for (int i=0;i<size;i++)
48
    {
49
      printf(" %.8e ",a[i]);
50
    }
51
  printf("]\n");
52
}
53

    
54
int main(int argc,char *argv[])
55
{
56
  float *a,*b,*res,*resacc;
57
  int size=1024;
58
  struct timeval tv1,tv2;
59
 
60
  if (argc > 1) {
61
    size=(int)atoll(argv[1]);
62
  }
63
  else {
64
    printf("\n\tPi : Estimate SillySum\n\n\t\t#1 : size (default 1024)\n\n");
65
  }
66

    
67
  printf("%i\n",size);
68
  
69
  a=(float*)malloc(size*sizeof(MYFLOAT));
70
  b=(float*)malloc(size*sizeof(MYFLOAT));
71
  res=(float*)malloc(size*sizeof(MYFLOAT));
72
  resacc=(float*)malloc(size*sizeof(MYFLOAT));
73

    
74
  srand(110271);
75
  
76
  for (int i=0;i<size;i++)
77
    {
78
      a[i]=(MYFLOAT)rand()/(MYFLOAT)RAND_MAX;
79
      b[i]=(MYFLOAT)rand()/(MYFLOAT)RAND_MAX;
80
      res[i]=0.;
81
      resacc[i]=0.;
82
    }
83

    
84
#ifndef NOSERIAL 
85
  gettimeofday(&tv1, NULL);
86
  MySum(res,a,b,size);
87
  gettimeofday(&tv2, NULL);
88
#endif
89

    
90
  MYFLOAT elapsed=(MYFLOAT)((tv2.tv_sec-tv1.tv_sec) * 1000000L +
91
                            (tv2.tv_usec-tv1.tv_usec))/1000000;
92

    
93
  gettimeofday(&tv1, NULL);
94
  MySumOpenACC(resacc,a,b,size);
95
  gettimeofday(&tv2, NULL);
96

    
97
  MYFLOAT elapsedAcc=(MYFLOAT)((tv2.tv_sec-tv1.tv_sec) * 1000000L +
98
                               (tv2.tv_usec-tv1.tv_usec))/1000000;
99

    
100
#ifndef NOSERIAL 
101
  MYFLOAT MyChecker=MyNorm(res,resacc,size);
102
  printf("Norm: %.8e\n",MyChecker);
103
#endif
104

    
105
#ifdef VERBOSE
106
  MyPrint(res,size);
107
  MyPrint(resacc,size);
108
#endif
109
  
110
#ifndef NOSERIAL 
111
  printf("Elapsed Time: %.3f\n",elapsed);
112
  printf("OpenACC Elapsed Time: %.3f\n",elapsedAcc);
113
#endif
114

    
115
#ifndef NOSERIAL 
116
  printf("NaiveRate: %.lld\n",(unsigned long)((float)size/elapsed));
117
#endif
118
  printf("OpenACCRate: %.lld\n",(unsigned long)((float)size/elapsedAcc));
119

    
120
#ifndef NOSERIAL 
121
  printf("OpenACCRatio: %.3f\n",elapsed/elapsedAcc);
122
#endif
123
  
124
  free(a);
125
  free(b);
126
  free(res);
127
  free(resacc);
128
}
129