Statistiques
| Révision :

root / ETSN / MySteps_1.c @ 301

Historique | Voir | Annoter | Télécharger (1,59 ko)

1
/* Simple Sum function in C */
2
/* compilation with : gcc -O3 -o MySteps_1 MySteps_1.c -lm */
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
void MySum(MYFLOAT *res, MYFLOAT *a, MYFLOAT *b, int size)
14
{
15
  for (uint i=0; i<size;i++) 
16
    {
17
      res[i] = a[i] + b[i];
18
    }
19
}
20

    
21
MYFLOAT MyNorm(MYFLOAT *a,MYFLOAT *b,int size)
22
{
23
  MYFLOAT norm=0.;
24

    
25
  for (int i=0;i<size;i++)
26
    {
27
      norm+=pow(a[i]-b[i],2);
28
    }
29

    
30
  return(sqrt(norm));
31
}
32

    
33
void MyPrint(MYFLOAT *a,int size)
34
{
35
  printf("[");
36
  for (int i=0;i<size;i++)
37
    {
38
      printf(" %.8e ",a[i]);
39
    }
40
  printf("]\n");
41
}
42

    
43
int main(int argc,char *argv[])
44
{
45
  float *a,*b,*res,*resacc;
46
  int size=1024;
47
  struct timeval tv1,tv2;
48
 
49
  if (argc > 1) {
50
    size=(int)atoll(argv[1]);
51
  }
52
  else {
53
    printf("\n\tMySteps : Estimate SillySum\n\n\t\t#1 : size (default 1024)\n\n");
54
  }
55

    
56
  printf("%i\n",size);
57
  
58
  a=(float*)malloc(size*sizeof(MYFLOAT));
59
  b=(float*)malloc(size*sizeof(MYFLOAT));
60
  res=(float*)malloc(size*sizeof(MYFLOAT));
61

    
62
  srand(110271);
63
  
64
  for (int i=0;i<size;i++)
65
    {
66
      a[i]=(MYFLOAT)rand()/(MYFLOAT)RAND_MAX;
67
      b[i]=(MYFLOAT)rand()/(MYFLOAT)RAND_MAX;
68
      res[i]=0.;
69
    }
70

    
71
  gettimeofday(&tv1, NULL);
72
  MySum(res,a,b,size);
73
  gettimeofday(&tv2, NULL);
74

    
75
  MYFLOAT elapsed=(MYFLOAT)((tv2.tv_sec-tv1.tv_sec) * 1000000L +
76
                            (tv2.tv_usec-tv1.tv_usec))/1000000;
77

    
78
#ifdef VERBOSE
79
  MyPrint(res,size);
80
#endif
81
  
82
  printf("Elapsed Time: %.3f\n",elapsed);
83

    
84
  printf("NaiveRate: %.lld\n",(unsigned long)((float)size/elapsed));
85

    
86
  free(a);
87
  free(b);
88
  free(res);
89
}
90