Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
a79ac2863d397614c2098357892b6681d5b9f0c1
[simgrid.git] / contrib / benchmarking_code_block / inject.h
1 /* Copyright (c) 2013-2014. The SimGrid Team.
2  * All rights reserved.                                                     */
3
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. */
6
7 /* Copy to src/include/xbt/ folder  */
8
9 /* Injecting timings for previously benchmarked code blocks */
10
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*/
14
15 #ifndef __INJECT_H__
16 #define __INJECT_H__
17
18 #include <time.h>
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <unistd.h>
22 #include <string.h>
23 #include "xbt/RngStream.h"
24 #include "xbt/dict.h"
25 #include "xbt/sysdep.h"
26
27 #define MAX_LINE_INJ 10000
28
29 /*
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 {
33   int n;
34   int counts;
35   double mean;
36   double* breaks;
37   double* percentage;
38   char* block_id;
39 } xbt_hist_t;
40
41 extern RngStream get_randgen(void);
42 typedef RngStream (*get_randgen_func_t)(void);
43
44 extern xbt_dict_t get_dict(void);
45 typedef xbt_dict_t (*get_dict_func_t)(void);
46
47 static inline void xbt_inject_init(char *inputfile);
48 static inline void inject_init_starpu(char *inputfile, xbt_dict_t *dict, RngStream *rng);
49
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);
53
54 /* Initializing xbt dictionary for SMPI version, reading xbt_hist_t entries line by line */
55 static inline void xbt_inject_init(char *inputfile)
56 {
57   xbt_dict_t mydict = get_dict();
58   FILE* fpInput     = fopen(inputfile, "r");
59   if (fpInput == NULL)
60     printf("Error while opening the inputfile");
61   fseek(fpInput, 0, 0);
62
63   char line[200];
64   char* key;
65
66   if (fgets(line, 200, fpInput) == NULL)
67     printf("Error input file is empty!"); // Skipping first row
68   while (fgets(line, 200, fpInput) != NULL) {
69     key = strtok(line, "\t");
70
71     xbt_hist_t* data = xbt_dict_get_or_null(mydict, key);
72     if (data)
73       printf("Error, data with that block_id already exists!");
74
75     data = (xbt_hist_t*)xbt_new(xbt_hist_t, 1);
76
77     data->block_id = key;
78     data->counts   = atoi(strtok(NULL, "\t"));
79     data->mean     = atof(strtok(NULL, "\t"));
80     data->n        = atoi(strtok(NULL, "\t"));
81
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(NULL, "\t"));
86     for (int i            = 0; i < (data->n - 1); i++)
87       data->percentage[i] = atof(strtok(NULL, "\t"));
88
89     xbt_dict_set(mydict, key, data, NULL);
90   }
91   fclose(fpInput);
92 }
93
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)
96 {
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);
101
102   xbt_dict_t mydict = *dict;
103   FILE* fpInput     = fopen(inputfile, "r");
104   if (fpInput == NULL) {
105     printf("Error while opening the inputfile");
106     return;
107   }
108
109   fseek(fpInput, 0, 0);
110
111   char line[MAX_LINE_INJ];
112   char* key;
113
114   if (fgets(line, MAX_LINE_INJ, fpInput) == NULL) {
115     printf("Error input file is empty!"); // Skipping first row
116     fclose(fpInput);
117     return;
118   }
119
120   while (fgets(line, MAX_LINE_INJ, fpInput) != NULL) {
121     key = strtok(line, "\t");
122
123     xbt_hist_t* data = xbt_dict_get_or_null(mydict, key);
124     if (data)
125       printf("Error, data with that block_id already exists!");
126
127     data             = (xbt_hist_t*)xbt_new(xbt_hist_t, 1);
128     data->block_id   = key;
129     data->counts     = atoi(strtok(NULL, "\t"));
130     data->mean       = atof(strtok(NULL, "\t"));
131     data->n          = atoi(strtok(NULL, "\t"));
132     data->breaks     = (double*)malloc(sizeof(double) * data->n);
133     data->percentage = (double*)malloc(sizeof(double) * (data->n - 1));
134
135     for (int i        = 0; i < data->n; i++)
136       data->breaks[i] = atof(strtok(NULL, "\t"));
137     for (int i = 0; i < (data->n - 1); i++) {
138       data->percentage[i] = atof(strtok(NULL, "\t"));
139     }
140
141     xbt_dict_set(mydict, key, data, NULL);
142   }
143   fclose(fpInput);
144 }
145
146 /* Injecting time */
147 static inline double xbt_inject_time(char *key)
148 {
149   return xbt_hist_time(key);
150   // return xbt_mean_time(key);
151 }
152
153 /* Injecting mean value */
154 static inline double xbt_mean_time(char *key)
155 {
156   xbt_dict_t mydict = get_dict();
157   xbt_hist_t* data  = xbt_dict_get_or_null(mydict, key);
158
159   if (!data) {
160     printf("Warning: element with specified key does not exist (%s)\n", key);
161     return 0;
162   }
163
164   return data->mean;
165 }
166
167 /* Injecting random value from the histogram */
168 static inline double xbt_hist_time(char *key)
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     printf("Warning: element with specified key does not exist (%s)\n", key);
175     return 0;
176   }
177
178   /* Choosing random interval of the histogram */
179   RngStream rng_stream = get_randgen();
180   double r             = RngStream_RandU01(rng_stream);
181   int k                = 0;
182   double left          = 0;
183   double right         = 1;
184   for (int i = 0; i < (data->n - 1); i++) {
185     left += (i == 0) ? 0 : data->percentage[i - 1];
186     right += data->percentage[i];
187     if (left < r && r <= right)
188       k = i;
189   }
190
191   /* Choosing random value inside the interval of the histogram */
192   double r2    = RngStream_RandU01(rng_stream);
193   double timer = data->breaks[k] + r2 * (data->breaks[k + 1] - data->breaks[k]);
194
195   return timer;
196 }
197
198 #endif // __INJECT_H__
199