Statistiques
| Révision :

root / ETSN / MySteps_openmp.c @ 294

Historique | Voir | Annoter | Télécharger (2,87 ko)

1
/* Simple SillySum function in C and OpenMP/C */
2
/* compilation with : gcc -fopenmp -O3 -o MySteps_openmp MySteps_openmp.c -lm -lgomp */
3

    
4
#include <math.h>
5
#include <stdio.h>
6
#include <stdlib.h>
7
#include <sys/time.h>
8

    
9
#define PI 3.141592653589793
10

    
11
#define MYFLOAT float
12

    
13
MYFLOAT MySillyFunction(MYFLOAT x)
14
{
15
    return(pow(sqrt(log(exp(atanh(tanh(asinh(sinh(acosh(cosh(atan(tan(asin(sin(acos(cos(x))))))))))))))),2)); 
16
}
17

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

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

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

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

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

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

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

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

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

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

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

    
107
  gettimeofday(&tv1, NULL);
108
  MySillySum(res,a,b,calls,size);
109
  gettimeofday(&tv2, NULL);
110

    
111
  MYFLOAT elapsed=(MYFLOAT)((tv2.tv_sec-tv1.tv_sec) * 1000000L +
112
                            (tv2.tv_usec-tv1.tv_usec))/1000000;
113

    
114
  gettimeofday(&tv1, NULL);
115
  MySillySumOMP(resacc,a,b,calls,size);
116
  gettimeofday(&tv2, NULL);
117

    
118
  MYFLOAT elapsedAcc=(MYFLOAT)((tv2.tv_sec-tv1.tv_sec) * 1000000L +
119
                               (tv2.tv_usec-tv1.tv_usec))/1000000;
120

    
121
  MYFLOAT MyChecker=MyNorm(res,resacc,size);
122
  printf("Norm: %.8e\n",MyChecker);
123

    
124
#ifdef VERBOSE
125
  MyPrint(res,size);
126
  MyPrint(resacc,size);
127
#endif
128
  
129
  printf("Elapsed Time: %.3f\n",elapsed);
130
  printf("OMP Elapsed Time: %.3f\n",elapsedAcc);
131

    
132
  printf("NaiveRate: %.i\n",(int)((float)size/elapsed));
133
  printf("OMPRate: %.i\n",(int)((float)size/elapsedAcc));
134

    
135
  printf("AccRatio: %.3f\n",elapsed/elapsedAcc);
136
  
137
  free(a);
138
  free(b);
139
  free(res);
140
  free(resacc);
141
}
142