Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Update limitations of PSG
[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         int i;
66         xbt_hist_t* data;
67
68         if (fgets(line, 200, fpInput) == NULL)
69                 printf("Error input file is empty!");//Skipping first row
70         while (fgets(line, 200, fpInput) != NULL)
71         {
72                 key = strtok(line, "\t");
73
74                 data = xbt_dict_get_or_null(mydict, key);
75                 if (data)
76                         printf("Error, data with that block_id already exists!");
77
78                 data = (xbt_hist_t *) xbt_new(xbt_hist_t, 1);
79
80                 data->block_id = key;
81                 data->counts = atoi(strtok(NULL, "\t"));
82                 data->mean = atof(strtok(NULL, "\t"));
83                 data->n = atoi(strtok(NULL, "\t"));
84
85                 data->breaks = (double*) malloc(sizeof(double) * data->n);
86                 data->percentage = (double*) malloc(sizeof(double) * (data->n - 1));
87                 for (i = 0; i < data->n; i++)
88                         data->breaks[i] = atof(strtok(NULL, "\t"));
89                 for (i = 0; i < (data->n - 1); i++)
90                         data->percentage[i] = atof(strtok(NULL, "\t"));
91
92                 xbt_dict_set(mydict, key, data, NULL);
93         }
94 }
95
96 /* Initializing xbt dictionary for StarPU version, reading xbt_hist_t entries line by line */
97 static inline void inject_init_starpu(char *inputfile, xbt_dict_t *dict, RngStream *rng)
98 {
99         *dict = xbt_dict_new_homogeneous(free);
100         *rng = RngStream_CreateStream("Randgen1");
101         unsigned long seed[] = {134, 233445, 865, 2634, 424242, 876541};
102         RngStream_SetSeed(*rng, seed);
103
104         xbt_dict_t mydict = *dict;
105         mydict = *dict;
106         FILE* fpInput = fopen(inputfile, "r");
107         if (fpInput == NULL)
108         {
109                 printf("Error while opening the inputfile");
110                 return;
111         }
112
113         fseek(fpInput, 0, 0);
114
115         char line[MAX_LINE_INJ];
116         char *key;
117         int i;
118         xbt_hist_t* data;
119
120         if (fgets(line, MAX_LINE_INJ, fpInput) == NULL)
121         {
122                 printf("Error input file is empty!");//Skipping first row
123                 return;
124         }
125
126
127         while (fgets(line, MAX_LINE_INJ, fpInput) != NULL)
128         {
129                 key = strtok(line, "\t");
130
131                 data = xbt_dict_get_or_null(mydict, key);
132                 if (data)
133                         printf("Error, data with that block_id already exists!");
134
135                 data = (xbt_hist_t *) xbt_new(xbt_hist_t, 1);
136                 data->block_id = key;
137                 data->counts = atoi(strtok(NULL, "\t"));
138                 data->mean = atof(strtok(NULL, "\t"));
139                 data->n = atoi(strtok(NULL, "\t"));
140                 data->breaks = (double*) malloc(sizeof(double) * data->n);
141                 data->percentage = (double*) malloc(sizeof(double) * (data->n - 1));
142
143                 for (i = 0; i < data->n; i++)
144                         data->breaks[i] = atof(strtok(NULL, "\t"));
145                         for (i = 0; i < (data->n - 1); i++)
146                         {
147                                 data->percentage[i] = atof(strtok(NULL, "\t"));
148                         }
149
150                 xbt_dict_set(mydict, key, data, NULL);
151         }
152 }
153
154 /* Injecting time */
155 static inline double xbt_inject_time(char *key)
156 {
157         return xbt_hist_time(key);
158         //return xbt_mean_time(key);
159 }
160
161 /* Injecting mean value */
162 static inline double xbt_mean_time(char *key)
163 {
164         xbt_dict_t mydict = get_dict();
165         xbt_hist_t* data = xbt_dict_get_or_null(mydict, key);
166
167         if (!data)
168         {
169           printf("Warning: element with specified key does not exist (%s)\n",key);
170           return 0;
171         }
172
173         return data->mean;
174 }
175
176 /* Injecting random value from the histogram */
177 static inline double xbt_hist_time(char *key)
178 {
179         int i, k = 0;
180         double left = 0, right = 1;
181         double timer = 0;
182         RngStream rng_stream;
183         double r, r2;
184
185         xbt_dict_t mydict = get_dict();
186         xbt_hist_t* data = xbt_dict_get_or_null(mydict, key);
187
188         if (!data)
189                 {
190                   printf("Warning: element with specified key does not exist (%s)\n",key);
191                   return 0;
192                 }
193
194         /* Choosing random interval of the histogram */
195         rng_stream = get_randgen();
196         r = RngStream_RandU01(rng_stream);
197         for (i = 0; i < (data->n - 1); i++)
198         {
199                 left += (i == 0) ? 0 : data->percentage[i - 1];
200                 right += data->percentage[i];
201                 if (left < r && r <= right)
202                         k = i;
203         }
204
205         /* Choosing random value inside the interval of the histogram */
206         r2 = RngStream_RandU01(rng_stream);
207         timer = data->breaks[k] + r2 * (data->breaks[k + 1] - data->breaks[k]);
208
209         return timer;
210 }
211
212 #endif // __INJECT_H__
213