Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge tag 'v3_9_90' into hypervisor
[simgrid.git] / contrib / benchmarking_code_block / inject.h
1 /* Copy to src/include/xbt/ folder  */
2
3 /* Injecting timings for previously benchmarked code blocks */
4
5 /* Use functions from bench.h to benchmark execution time of the desired block,
6  * then Rhist.R script to read all timings and produce histograms
7  * and finally inject.h to inject values instead of executing block*/
8
9 #ifndef __INJECT_H__
10 #define __INJECT_H__
11
12 #include <time.h>
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <unistd.h>
16 #include <string.h>
17 #include "xbt/RngStream.h"
18 #include "xbt/dict.h"
19 #include "xbt/sysdep.h"
20
21 #define MAX_LINE_INJ 1000
22
23 /*
24  * Histogram entry for each measured block
25  * Each entry is guarded inside xbt dictionary which is read from the file */
26 typedef struct xbt_hist {
27         int n;
28         double mean;
29         double *breaks;
30         double *percentage;
31         char* block_id;
32 } xbt_hist_t;
33
34 extern RngStream get_randgen(void);
35 typedef RngStream (*get_randgen_func_t)(void);
36
37 extern xbt_dict_t get_dict(void);
38 typedef xbt_dict_t (*get_dict_func_t)(void);
39
40 static inline void xbt_inject_init(char *inputfile);
41 static inline void inject_init_starpu(char *inputfile, xbt_dict_t *dict, RngStream *rng);
42
43 static inline double xbt_inject_time(char *key);
44 static inline double xbt_mean_time(char *key);
45 static inline double xbt_hist_time(char *key);
46
47 /* Initializing xbt dictionary for SMPI version, reading xbt_hist_t entries line by line */
48 static inline void xbt_inject_init(char *inputfile)
49 {
50         xbt_dict_t mydict = get_dict();
51         FILE* fpInput = fopen(inputfile, "r");
52         if (fpInput == NULL)
53                 printf("Error while opening the inputfile");
54         fseek(fpInput, 0, 0);
55
56         char line[200];
57         char *key;
58         int i;
59         xbt_hist_t* data;
60
61         if (fgets(line, 200, fpInput) == NULL)
62                 printf("Error input file is empty!");//Skipping first row
63         while (fgets(line, 200, fpInput) != NULL)
64         {
65                 key = strtok(line, " \t");
66
67                 data = xbt_dict_get_or_null(mydict, key);
68                 if (data)
69                         printf("Error, data with that block_id already exists!");
70
71                 data = (xbt_hist_t *) xbt_new(xbt_hist_t, 1);
72
73                 data->block_id = key;
74                 data->mean = atof(strtok(NULL, " \t"));
75                 data->n = atoi(strtok(NULL, " \t"));
76
77                 data->breaks = (double*) malloc(sizeof(double) * data->n);
78                 data->percentage = (double*) malloc(sizeof(double) * (data->n - 1));
79                 for (i = 0; i < data->n; i++)
80                         data->breaks[i] = atof(strtok(NULL, " \t"));
81                 for (i = 0; i < (data->n - 1); i++)
82                         data->percentage[i] = atof(strtok(NULL, " \t"));
83
84                 xbt_dict_set(mydict, key, data, NULL);
85         }
86 }
87
88 /* Initializing xbt dictionary for StarPU version, reading xbt_hist_t entries line by line */
89 static inline void inject_init_starpu(char *inputfile, xbt_dict_t *dict, RngStream *rng)
90 {
91         *dict = xbt_dict_new_homogeneous(free);
92         *rng = RngStream_CreateStream("Randgen1");
93         unsigned long seed[] = {134, 233445, 865, 2634, 424242, 876541};
94         RngStream_SetSeed(*rng, seed);
95
96         xbt_dict_t mydict = *dict;
97         mydict = *dict;
98         FILE* fpInput = fopen(inputfile, "r");
99         if (fpInput == NULL)
100                 printf("Error while opening the inputfile");
101
102         fseek(fpInput, 0, 0);
103
104         char line[MAX_LINE_INJ];
105         char *key;
106         int i;
107         xbt_hist_t* data;
108
109         if (fgets(line, MAX_LINE_INJ, fpInput) == NULL)
110                 printf("Error input file is empty!");//Skipping first row
111
112
113         while (fgets(line, MAX_LINE_INJ, fpInput) != NULL)
114         {
115                 key = strtok(line, " \t");
116
117                 data = xbt_dict_get_or_null(mydict, key);
118                 if (data)
119                         printf("Error, data with that block_id already exists!");
120
121                 data = (xbt_hist_t *) xbt_new(xbt_hist_t, 1);
122                 data->block_id = key;
123                 data->mean = atof(strtok(NULL, " \t"));
124                 data->n = atoi(strtok(NULL, " \t"));
125                 data->breaks = (double*) malloc(sizeof(double) * data->n);
126                 data->percentage = (double*) malloc(sizeof(double) * (data->n - 1));
127
128                 for (i = 0; i < data->n; i++)
129                         data->breaks[i] = atof(strtok(NULL, " \t"));
130                         for (i = 0; i < (data->n - 1); i++)
131                         {
132                                 data->percentage[i] = atof(strtok(NULL, " \t"));
133                         }
134
135                 xbt_dict_set(mydict, key, data, NULL);
136         }
137 }
138
139 /* Injecting time */
140 static inline double xbt_inject_time(char *key)
141 {
142         return xbt_hist_time(key);
143         //return xbt_mean_time(key);
144 }
145
146 /* Injecting mean value */
147 static inline double xbt_mean_time(char *key)
148 {
149         xbt_dict_t mydict = get_dict();
150         xbt_hist_t* data = xbt_dict_get_or_null(mydict, key);
151
152         if (!data)
153         {
154           printf("Warning: element with specified key does not exist (%s)\n",key);
155           return 0;
156         }
157
158         return data->mean;
159 }
160
161 /* Injecting random value from the histogram */
162 static inline double xbt_hist_time(char *key)
163 {
164         int i, k = 0;
165         double left = 0, right = 1;
166         double timer = 0;
167         RngStream rng_stream;
168         double r, r2;
169
170         xbt_dict_t mydict = get_dict();
171         xbt_hist_t* data = xbt_dict_get_or_null(mydict, key);
172
173         if (!data)
174                 {
175                   printf("Warning: element with specified key does not exist (%s)\n",key);
176                   return 0;
177                 }
178
179         /* Choosing random interval of the histogram */
180         rng_stream = get_randgen();
181         r = RngStream_RandU01(rng_stream);
182         for (i = 0; i < (data->n - 1); i++)
183         {
184                 left += (i == 0) ? 0 : data->percentage[i - 1];
185                 right += data->percentage[i];
186                 if (left < r && r <= right)
187                         k = i;
188         }
189
190         /* Choosing random value inside the interval of the histogram */
191         r2 = RngStream_RandU01(rng_stream);
192         timer = data->breaks[k] + r2 * (data->breaks[k + 1] - data->breaks[k]);
193
194         return timer;
195 }
196
197 #endif // __INJECT_H__
198