Statistiques
| Révision :

root / ETSN / MySteps_6.c @ 303

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

1
/* Simple SillySum function in C */
2
/* compilation with : gcc -O3 -o MySteps_6 MySteps_6.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
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
MYFLOAT MyNorm(MYFLOAT *a,MYFLOAT *b,int size)
36
{
37
  MYFLOAT norm=0.;
38

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

    
44
  return(sqrt(norm));
45
}
46

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

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

    
72
  printf("%i %i\n",size,calls);
73
  
74
  a=(float*)malloc(size*sizeof(MYFLOAT));
75
  b=(float*)malloc(size*sizeof(MYFLOAT));
76
  res=(float*)malloc(size*sizeof(MYFLOAT));
77
  resacc=(float*)malloc(size*sizeof(MYFLOAT));
78

    
79
  srand(110271);
80
  
81
  for (int i=0;i<size;i++)
82
    {
83
      a[i]=(MYFLOAT)rand()/(MYFLOAT)RAND_MAX;
84
      b[i]=(MYFLOAT)rand()/(MYFLOAT)RAND_MAX;
85
      res[i]=0.;
86
      resacc[i]=0.;
87
    }
88

    
89
  gettimeofday(&tv1, NULL);
90
  MySillySum(res,a,b,calls,size);
91
  gettimeofday(&tv2, NULL);
92

    
93
  MYFLOAT elapsed=(MYFLOAT)((tv2.tv_sec-tv1.tv_sec) * 1000000L +
94
                            (tv2.tv_usec-tv1.tv_usec))/1000000;
95

    
96
#ifdef VERBOSE
97
  MyPrint(res,size);
98
  MyPrint(resacc,size);
99
#endif
100
  
101
  printf("Elapsed Time: %.3f\n",elapsed);
102

    
103
  printf("NativeRate: %.lld\n",(unsigned long)((float)size/elapsed));
104

    
105
  free(a);
106
  free(b);
107
  free(res);
108
  free(resacc);
109
}
110