File: cost_utils.c

package info (click to toggle)
papi 5.7.0+dfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 9,856 kB
  • sloc: ansic: 93,265; fortran: 3,338; xml: 2,460; makefile: 815; sh: 290
file content (133 lines) | stat: -rw-r--r-- 2,759 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>

#define NUM_ITERS	1000000

int num_iters = NUM_ITERS;

/* computes min, max, and mean for an array; returns std deviation */
double
do_stats( long long *array, long long *min, long long *max, double *average )
{
	int i;
	double std, tmp;

	*min = *max = array[0];
	*average = 0;
	for ( i = 0; i < num_iters; i++ ) {
		*average += ( double ) array[i];
		if ( *min > array[i] )
			*min = array[i];
		if ( *max < array[i] )
			*max = array[i];
	}
	*average = *average / ( double ) num_iters;
	std = 0;
	for ( i = 0; i < num_iters; i++ ) {
		tmp = ( double ) array[i] - ( *average );
		std += tmp * tmp;
	}
	std = sqrt( std / ( num_iters - 1 ) );
	return ( std );
}

void
do_std_dev( long long *a, int *s, double std, double ave )
{
	int i, j;
	double dev[10];

	for ( i = 0; i < 10; i++ ) {
		dev[i] = std * ( i + 1 );
		s[i] = 0;
	}

	for ( i = 0; i < num_iters; i++ ) {
		for ( j = 0; j < 10; j++ ) {
			if ( ( ( double ) a[i] - dev[j] ) > ave )
				s[j]++;
		}
	}
}

void
do_dist( long long *a, long long min, long long max, int bins, int *d )
{
	int i, j;
	int dmax = 0;
	int range = ( int ) ( max - min + 1 );	/* avoid edge conditions */

	/* clear the distribution array */
	for ( i = 0; i < bins; i++ ) {
		d[i] = 0;
	}

	/* scan the array to distribute cost per bin */
	for ( i = 0; i < num_iters; i++ ) {
		j = ( ( int ) ( a[i] - min ) * bins ) / range;
		d[j]++;
		if ( j && ( dmax < d[j] ) )
			dmax = d[j];
	}

	/* scale each bin to a max of 100 */
	for ( i = 1; i < bins; i++ ) {
		d[i] = ( d[i] * 100 ) / dmax;
	}
}

/* Long Long compare function for qsort */
static int cmpfunc (const void *a, const void *b) {

	if ( *(long long *)a - *(long long *)b < 0 ) {
		return -1;
	}

	if ( *(long long int*)a - *(long long int*)b > 0 ) {
		return 1;
	}

	return 0;
}

/* Calculate the percentiles for making boxplots */
int do_percentile(long long *a,
		long long *percent25,
		long long *percent50,
		long long *percent75,
		long long *percent99) {

	long long *a_sort;
	int i_25,i_50,i_75,i_99;

	/* Allocate room for a copy of the results */
	a_sort = calloc(num_iters,sizeof(long long));
	if (a_sort==NULL) {
		fprintf(stderr,"Memory allocation error!\n");
		return -1;
	}

	/* Make a copy of the results */
	memcpy(a_sort,a,num_iters*sizeof(long long));

	/* Calculate indices */
	i_25=(int)num_iters/4;
	i_50=(int)num_iters/2;
	// index for  75%, not quite accurate because it doesn't
	// take even or odd into consideration
	i_75=(int)num_iters/4*3;
	i_99=(int)num_iters/10*9.9;

	qsort(a_sort,num_iters-1,sizeof(long long),cmpfunc);

	*percent25=a_sort[i_25];
	*percent50=a_sort[i_50];
	*percent75=a_sort[i_75];
	*percent99=a_sort[i_99];

	free(a_sort);

	return 0;
}