1 /* Copy to src/include/xbt/ folder */
3 /* Injecting timings for previously benchmarked code blocks */
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*/
17 #include "xbt/RngStream.h"
19 #include "xbt/sysdep.h"
21 #define MAX_LINE_INJ 1000
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 {
34 extern RngStream get_randgen(void);
35 typedef RngStream (*get_randgen_func_t)(void);
37 extern xbt_dict_t get_dict(void);
38 typedef xbt_dict_t (*get_dict_func_t)(void);
40 static inline void xbt_inject_init(char *inputfile);
41 static inline void inject_init_starpu(char *inputfile, xbt_dict_t *dict, RngStream *rng);
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);
47 /* Initializing xbt dictionary for SMPI version, reading xbt_hist_t entries line by line */
48 static inline void xbt_inject_init(char *inputfile)
50 xbt_dict_t mydict = get_dict();
51 FILE* fpInput = fopen(inputfile, "r");
53 printf("Error while opening the inputfile");
61 if (fgets(line, 200, fpInput) == NULL)
62 printf("Error input file is empty!");//Skipping first row
63 while (fgets(line, 200, fpInput) != NULL)
65 key = strtok(line, " \t");
67 data = xbt_dict_get_or_null(mydict, key);
69 printf("Error, data with that block_id already exists!");
71 data = (xbt_hist_t *) xbt_new(xbt_hist_t, 1);
74 data->mean = atof(strtok(NULL, " \t"));
75 data->n = atoi(strtok(NULL, " \t"));
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"));
84 xbt_dict_set(mydict, key, data, NULL);
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)
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);
96 xbt_dict_t mydict = *dict;
98 FILE* fpInput = fopen(inputfile, "r");
100 printf("Error while opening the inputfile");
102 fseek(fpInput, 0, 0);
104 char line[MAX_LINE_INJ];
109 if (fgets(line, MAX_LINE_INJ, fpInput) == NULL)
110 printf("Error input file is empty!");//Skipping first row
113 while (fgets(line, MAX_LINE_INJ, fpInput) != NULL)
115 key = strtok(line, " \t");
117 data = xbt_dict_get_or_null(mydict, key);
119 printf("Error, data with that block_id already exists!");
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));
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++)
132 data->percentage[i] = atof(strtok(NULL, " \t"));
135 xbt_dict_set(mydict, key, data, NULL);
140 static inline double xbt_inject_time(char *key)
142 return xbt_hist_time(key);
143 //return xbt_mean_time(key);
146 /* Injecting mean value */
147 static inline double xbt_mean_time(char *key)
149 xbt_dict_t mydict = get_dict();
150 xbt_hist_t* data = xbt_dict_get_or_null(mydict, key);
154 printf("Warning: element with specified key does not exist (%s)\n",key);
161 /* Injecting random value from the histogram */
162 static inline double xbt_hist_time(char *key)
165 double left = 0, right = 1;
167 RngStream rng_stream;
170 xbt_dict_t mydict = get_dict();
171 xbt_hist_t* data = xbt_dict_get_or_null(mydict, key);
175 printf("Warning: element with specified key does not exist (%s)\n",key);
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++)
184 left += (i == 0) ? 0 : data->percentage[i - 1];
185 right += data->percentage[i];
186 if (left < r && r <= right)
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]);
197 #endif // __INJECT_H__