Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' of scm.gforge.inria.fr:/gitroot/simgrid/simgrid
[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(fInput);
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     return;
117   }
118
119   while (fgets(line, MAX_LINE_INJ, fpInput) != NULL) {
120     key = strtok(line, "\t");
121
122     xbt_hist_t* data = xbt_dict_get_or_null(mydict, key);
123     if (data)
124       printf("Error, data with that block_id already exists!");
125
126     data             = (xbt_hist_t*)xbt_new(xbt_hist_t, 1);
127     data->block_id   = key;
128     data->counts     = atoi(strtok(NULL, "\t"));
129     data->mean       = atof(strtok(NULL, "\t"));
130     data->n          = atoi(strtok(NULL, "\t"));
131     data->breaks     = (double*)malloc(sizeof(double) * data->n);
132     data->percentage = (double*)malloc(sizeof(double) * (data->n - 1));
133
134     for (int i        = 0; i < data->n; i++)
135       data->breaks[i] = atof(strtok(NULL, "\t"));
136     for (int i = 0; i < (data->n - 1); i++) {
137       data->percentage[i] = atof(strtok(NULL, "\t"));
138     }
139
140     xbt_dict_set(mydict, key, data, NULL);
141   }
142   fclose(fInput);
143 }
144
145 /* Injecting time */
146 static inline double xbt_inject_time(char *key)
147 {
148   return xbt_hist_time(key);
149   // return xbt_mean_time(key);
150 }
151
152 /* Injecting mean value */
153 static inline double xbt_mean_time(char *key)
154 {
155   xbt_dict_t mydict = get_dict();
156   xbt_hist_t* data  = xbt_dict_get_or_null(mydict, key);
157
158   if (!data) {
159     printf("Warning: element with specified key does not exist (%s)\n", key);
160     return 0;
161   }
162
163   return data->mean;
164 }
165
166 /* Injecting random value from the histogram */
167 static inline double xbt_hist_time(char *key)
168 {
169   xbt_dict_t mydict = get_dict();
170   xbt_hist_t* data  = xbt_dict_get_or_null(mydict, key);
171
172   if (!data) {
173     printf("Warning: element with specified key does not exist (%s)\n", key);
174     return 0;
175   }
176
177   /* Choosing random interval of the histogram */
178   RngStream rng_stream = get_randgen();
179   double r             = RngStream_RandU01(rng_stream);
180   int k                = 0;
181   double left          = 0;
182   double right         = 1;
183   for (int 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)
187       k = i;
188   }
189
190   /* Choosing random value inside the interval of the histogram */
191   double r2    = RngStream_RandU01(rng_stream);
192   double timer = data->breaks[k] + r2 * (data->breaks[k + 1] - data->breaks[k]);
193
194   return timer;
195 }
196
197 #endif // __INJECT_H__
198