PAPI  5.7.0.0
cost_utils.c
Go to the documentation of this file.
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <math.h>
5 
6 #define NUM_ITERS 1000000
7 
9 
10 /* computes min, max, and mean for an array; returns std deviation */
11 double
12 do_stats( long long *array, long long *min, long long *max, double *average )
13 {
14  int i;
15  double std, tmp;
16 
17  *min = *max = array[0];
18  *average = 0;
19  for ( i = 0; i < num_iters; i++ ) {
20  *average += ( double ) array[i];
21  if ( *min > array[i] )
22  *min = array[i];
23  if ( *max < array[i] )
24  *max = array[i];
25  }
26  *average = *average / ( double ) num_iters;
27  std = 0;
28  for ( i = 0; i < num_iters; i++ ) {
29  tmp = ( double ) array[i] - ( *average );
30  std += tmp * tmp;
31  }
32  std = sqrt( std / ( num_iters - 1 ) );
33  return ( std );
34 }
35 
36 void
37 do_std_dev( long long *a, int *s, double std, double ave )
38 {
39  int i, j;
40  double dev[10];
41 
42  for ( i = 0; i < 10; i++ ) {
43  dev[i] = std * ( i + 1 );
44  s[i] = 0;
45  }
46 
47  for ( i = 0; i < num_iters; i++ ) {
48  for ( j = 0; j < 10; j++ ) {
49  if ( ( ( double ) a[i] - dev[j] ) > ave )
50  s[j]++;
51  }
52  }
53 }
54 
55 void
56 do_dist( long long *a, long long min, long long max, int bins, int *d )
57 {
58  int i, j;
59  int dmax = 0;
60  int range = ( int ) ( max - min + 1 ); /* avoid edge conditions */
61 
62  /* clear the distribution array */
63  for ( i = 0; i < bins; i++ ) {
64  d[i] = 0;
65  }
66 
67  /* scan the array to distribute cost per bin */
68  for ( i = 0; i < num_iters; i++ ) {
69  j = ( ( int ) ( a[i] - min ) * bins ) / range;
70  d[j]++;
71  if ( j && ( dmax < d[j] ) )
72  dmax = d[j];
73  }
74 
75  /* scale each bin to a max of 100 */
76  for ( i = 1; i < bins; i++ ) {
77  d[i] = ( d[i] * 100 ) / dmax;
78  }
79 }
80 
81 /* Long Long compare function for qsort */
82 static int cmpfunc (const void *a, const void *b) {
83 
84  if ( *(long long *)a - *(long long *)b < 0 ) {
85  return -1;
86  }
87 
88  if ( *(long long int*)a - *(long long int*)b > 0 ) {
89  return 1;
90  }
91 
92  return 0;
93 }
94 
95 /* Calculate the percentiles for making boxplots */
96 int do_percentile(long long *a,
97  long long *percent25,
98  long long *percent50,
99  long long *percent75,
100  long long *percent99) {
101 
102  long long *a_sort;
103  int i_25,i_50,i_75,i_99;
104 
105  /* Allocate room for a copy of the results */
106  a_sort = calloc(num_iters,sizeof(long long));
107  if (a_sort==NULL) {
108  fprintf(stderr,"Memory allocation error!\n");
109  return -1;
110  }
111 
112  /* Make a copy of the results */
113  memcpy(a_sort,a,num_iters*sizeof(long long));
114 
115  /* Calculate indices */
116  i_25=(int)num_iters/4;
117  i_50=(int)num_iters/2;
118  // index for 75%, not quite accurate because it doesn't
119  // take even or odd into consideration
120  i_75=(int)num_iters/4*3;
121  i_99=(int)num_iters/10*9.9;
122 
123  qsort(a_sort,num_iters-1,sizeof(long long),cmpfunc);
124 
125  *percent25=a_sort[i_25];
126  *percent50=a_sort[i_50];
127  *percent75=a_sort[i_75];
128  *percent99=a_sort[i_99];
129 
130  free(a_sort);
131 
132  return 0;
133 }
static double array[ARRAYSIZE]
Definition: papi_l1_dca.c:23
double tmp
double do_stats(long long *array, long long *min, long long *max, double *average)
Definition: cost_utils.c:12
static int cmpfunc(const void *a, const void *b)
Definition: cost_utils.c:82
#define NUM_ITERS
Definition: cost_utils.c:6
double s
Definition: byte_profile.c:36
#define min(x, y)
Definition: darwin-common.h:4
void do_dist(long long *a, long long min, long long max, int bins, int *d)
Definition: cost_utils.c:56
int range
Definition: fileop.c:139
void do_std_dev(long long *a, int *s, double std, double ave)
Definition: cost_utils.c:37
static double b[MATRIX_SIZE][MATRIX_SIZE]
Definition: libmsr_basic.c:39
int do_percentile(long long *a, long long *percent25, long long *percent50, long long *percent75, long long *percent99)
Definition: cost_utils.c:96
static double a[MATRIX_SIZE][MATRIX_SIZE]
Definition: libmsr_basic.c:38
int i
Definition: fileop.c:140
int num_iters
Definition: cost_utils.c:8