Statistiques
| Révision :

root / ETSN / MySteps_1_openmp.c @ 302

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

1
/* Simple Sum function in C and OpenMP/C */
2
/* compilation with sequential compute : gcc -fopenmp -O3 -o MySteps_1_openmp MySteps_1_openmp.c -lm -lgomp */
3
/* compilation without sequential compute : gcc -DNOSERIAL -fopenmp -O3 -o MySteps_1_openmp_1_NoSerial MySteps_1_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
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 MySillySumOMP(MYFLOAT *res, MYFLOAT *a, MYFLOAT *b, int size)
23
{
24
  #pragma omp parallel for
25
  for (uint i=0; i<size;i++) 
26
    {
27
      res[i] = a[i] + b[i];
28
    }
29
}
30

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

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

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

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

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

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

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

    
83
#ifndef NOSERIAL 
84
  gettimeofday(&tv1, NULL);
85
  MySum(res,a,b,size);
86
  gettimeofday(&tv2, NULL);
87
#endif
88
  
89
  MYFLOAT elapsed=(MYFLOAT)((tv2.tv_sec-tv1.tv_sec) * 1000000L +
90
                            (tv2.tv_usec-tv1.tv_usec))/1000000;
91

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

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

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

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

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

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