1 /* Copyright (c) 2013-2022. The SimGrid Team.
2 * All rights reserved. */
4 /* This program is free software; you can redistribute it and/or modify it
5 * under the terms of the license (GNU LGPL) which comes with this package. */
7 /* Copy to src/include/xbt/ folder */
9 /* Injecting timings for previously benchmarked code blocks */
11 /* Use functions from bench.h to benchmark execution time of the desired block,
12 * then Rhist.R script to read all timings and produce histograms
13 * and finally inject.h to inject values instead of executing block*/
23 #include "xbt/RngStream.h"
25 #include "xbt/sysdep.h"
27 #define MAX_LINE_INJ 10000
30 * Histogram entry for each measured block
31 * Each entry is guarded inside xbt dictionary which is read from the file */
32 typedef struct xbt_hist {
41 extern RngStream get_randgen(void);
42 typedef RngStream (*get_randgen_func_t)(void);
44 extern xbt_dict_t get_dict(void);
45 typedef xbt_dict_t (*get_dict_func_t)(void);
47 static inline void xbt_inject_init(char *inputfile);
48 static inline void inject_init_starpu(char *inputfile, xbt_dict_t *dict, RngStream *rng);
50 static inline double xbt_inject_time(char *key);
51 static inline double xbt_mean_time(char *key);
52 static inline double xbt_hist_time(char *key);
54 /* Initializing xbt dictionary for SMPI version, reading xbt_hist_t entries line by line */
55 static inline void xbt_inject_init(char *inputfile)
57 xbt_dict_t mydict = get_dict();
58 FILE* fpInput = fopen(inputfile, "r");
59 xbt_assert(fpInput != NULL, "Error while opening the inputfile");
65 if (fgets(line, 200, fpInput) == NULL)
66 printf("Error input file is empty!"); // Skipping first row
67 while (fgets(line, 200, fpInput) != NULL) {
68 char *saveptr = NULL; /* for strtok_r() */
69 key = strtok_r(line, "\t", &saveptr);
71 xbt_hist_t* data = xbt_dict_get_or_null(mydict, key);
73 printf("Error, data with that block_id already exists!");
75 data = (xbt_hist_t*)xbt_new(xbt_hist_t, 1);
78 data->counts = atoi(strtok_r(NULL, "\t", &saveptr));
79 data->mean = atof(strtok_r(NULL, "\t", &saveptr));
80 data->n = atoi(strtok_r(NULL, "\t", &saveptr));
82 data->breaks = (double*)malloc(sizeof(double) * data->n);
83 data->percentage = (double*)malloc(sizeof(double) * (data->n - 1));
84 for (int i = 0; i < data->n; i++)
85 data->breaks[i] = atof(strtok_r(NULL, "\t", &saveptr));
86 for (int i = 0; i < (data->n - 1); i++)
87 data->percentage[i] = atof(strtok_r(NULL, "\t", &saveptr));
89 xbt_dict_set(mydict, key, data);
94 /* Initializing xbt dictionary for StarPU version, reading xbt_hist_t entries line by line */
95 static inline void inject_init_starpu(char *inputfile, xbt_dict_t *dict, RngStream *rng)
97 *dict = xbt_dict_new_homogeneous(free);
98 *rng = RngStream_CreateStream("Randgen1");
99 unsigned long seed[] = {134, 233445, 865, 2634, 424242, 876541};
100 RngStream_SetSeed(*rng, seed);
102 xbt_dict_t mydict = *dict;
103 FILE* fpInput = fopen(inputfile, "r");
104 if (fpInput == NULL) {
105 printf("Error while opening the inputfile");
109 fseek(fpInput, 0, 0);
111 char line[MAX_LINE_INJ];
114 if (fgets(line, MAX_LINE_INJ, fpInput) == NULL) {
115 printf("Error input file is empty!"); // Skipping first row
120 while (fgets(line, MAX_LINE_INJ, fpInput) != NULL) {
121 char *saveptr = NULL; /* for strtok_r() */
122 key = strtok_r(line, "\t", &saveptr);
124 xbt_hist_t* data = xbt_dict_get_or_null(mydict, key);
126 printf("Error, data with that block_id already exists!");
128 data = (xbt_hist_t*)xbt_new(xbt_hist_t, 1);
129 data->block_id = key;
130 data->counts = atoi(strtok_r(NULL, "\t", &saveptr));
131 data->mean = atof(strtok_r(NULL, "\t", &saveptr));
132 data->n = atoi(strtok_r(NULL, "\t", &saveptr));
133 data->breaks = (double*)malloc(sizeof(double) * data->n);
134 data->percentage = (double*)malloc(sizeof(double) * (data->n - 1));
136 for (int i = 0; i < data->n; i++)
137 data->breaks[i] = atof(strtok_r(NULL, "\t", &saveptr));
138 for (int i = 0; i < (data->n - 1); i++) {
139 data->percentage[i] = atof(strtok_r(NULL, "\t", &saveptr));
142 xbt_dict_set(mydict, key, data);
148 static inline double xbt_inject_time(char *key)
150 return xbt_hist_time(key);
151 // return xbt_mean_time(key);
154 /* Injecting mean value */
155 static inline double xbt_mean_time(char *key)
157 xbt_dict_t mydict = get_dict();
158 xbt_hist_t* data = xbt_dict_get_or_null(mydict, key);
161 printf("Warning: element with specified key does not exist (%s)\n", key);
168 /* Injecting random value from the histogram */
169 static inline double xbt_hist_time(char *key)
171 xbt_dict_t mydict = get_dict();
172 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 RngStream rng_stream = get_randgen();
181 double r = RngStream_RandU01(rng_stream);
185 for (int i = 0; i < (data->n - 1); i++) {
186 left += (i == 0) ? 0 : data->percentage[i - 1];
187 right += data->percentage[i];
188 if (left < r && r <= right)
192 /* Choosing random value inside the interval of the histogram */
193 double r2 = RngStream_RandU01(rng_stream);
194 double timer = data->breaks[k] + r2 * (data->breaks[k + 1] - data->breaks[k]);
199 #endif // __INJECT_H__