Statistiques
| Révision :

root / ETSN / MySteps_6_openmp.c @ 301

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

1
/* Simple SillySum function in C and OpenMP/C */
2
/* compilation with sequential compute : gcc -fopenmp -O3 -o MySteps_6_openmp MySteps_6_openmp.c -lm -lgomp */
3
/* compilation without sequential compute : gcc -DNOSERIAL -fopenmp -O3 -o MySteps_6_openmp_NoSerial MySteps_6_openmp.c -lm -lgomp */
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
MYFLOAT MySillyFunction(MYFLOAT x)
15
{
16
    return(pow(sqrt(log(exp(atanh(tanh(asinh(sinh(acosh(cosh(atan(tan(asin(sin(acos(cos(x))))))))))))))),2)); 
17
}
18

    
19
void MySillySum(MYFLOAT *res, MYFLOAT *a, MYFLOAT *b,int calls, int size)
20
{
21
  for (uint i=0; i<size;i++) 
22
    {
23
      MYFLOAT ai=a[i];
24
      MYFLOAT bi=b[i];
25
      
26
      for (int c=0;c<calls;c++)
27
        {
28
          ai=MySillyFunction(ai);
29
          bi=MySillyFunction(bi);
30
        }
31

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

    
36
void MySillySumOMP(MYFLOAT *res, MYFLOAT *a, MYFLOAT *b,int calls, int size)
37
{
38
  #pragma omp parallel for
39
  for (uint i=0; i<size;i++) 
40
    {
41
      MYFLOAT ai=a[i];
42
      MYFLOAT bi=b[i];
43
      
44
      for (int c=0;c<calls;c++)
45
        {
46
          ai=MySillyFunction(ai);
47
          bi=MySillyFunction(bi);
48
        }
49

    
50
      res[i] = ai + bi;
51
    }
52
}
53

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

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

    
63
  return(sqrt(norm));
64
}
65

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

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

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

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

    
108
#ifndef NOSERIAL 
109
  gettimeofday(&tv1, NULL);
110
  MySillySum(res,a,b,calls,size);
111
  gettimeofday(&tv2, NULL);
112
#endif
113
  
114
  MYFLOAT elapsed=(MYFLOAT)((tv2.tv_sec-tv1.tv_sec) * 1000000L +
115
                            (tv2.tv_usec-tv1.tv_usec))/1000000;
116

    
117
  gettimeofday(&tv1, NULL);
118
  MySillySumOMP(resacc,a,b,calls,size);
119
  gettimeofday(&tv2, NULL);
120

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

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

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

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

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