Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'mc' into mc++
[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 1000
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         double mean;
35         double *breaks;
36         double *percentage;
37         char* block_id;
38 } xbt_hist_t;
39
40 extern RngStream get_randgen(void);
41 typedef RngStream (*get_randgen_func_t)(void);
42
43 extern xbt_dict_t get_dict(void);
44 typedef xbt_dict_t (*get_dict_func_t)(void);
45
46 static inline void xbt_inject_init(char *inputfile);
47 static inline void inject_init_starpu(char *inputfile, xbt_dict_t *dict, RngStream *rng);
48
49 static inline double xbt_inject_time(char *key);
50 static inline double xbt_mean_time(char *key);
51 static inline double xbt_hist_time(char *key);
52
53 /* Initializing xbt dictionary for SMPI version, reading xbt_hist_t entries line by line */
54 static inline void xbt_inject_init(char *inputfile)
55 {
56         xbt_dict_t mydict = get_dict();
57         FILE* fpInput = fopen(inputfile, "r");
58         if (fpInput == NULL)
59                 printf("Error while opening the inputfile");
60         fseek(fpInput, 0, 0);
61
62         char line[200];
63         char *key;
64         int i;
65         xbt_hist_t* data;
66
67         if (fgets(line, 200, fpInput) == NULL)
68                 printf("Error input file is empty!");//Skipping first row
69         while (fgets(line, 200, fpInput) != NULL)
70         {
71                 key = strtok(line, " \t");
72
73                 data = xbt_dict_get_or_null(mydict, key);
74                 if (data)
75                         printf("Error, data with that block_id already exists!");
76
77                 data = (xbt_hist_t *) xbt_new(xbt_hist_t, 1);
78
79                 data->block_id = key;
80                 data->mean = atof(strtok(NULL, " \t"));
81                 data->n = atoi(strtok(NULL, " \t"));
82
83                 data->breaks = (double*) malloc(sizeof(double) * data->n);
84                 data->percentage = (double*) malloc(sizeof(double) * (data->n - 1));
85                 for (i = 0; i < data->n; i++)
86                         data->breaks[i] = atof(strtok(NULL, " \t"));
87                 for (i = 0; i < (data->n - 1); i++)
88                         data->percentage[i] = atof(strtok(NULL, " \t"));
89
90                 xbt_dict_set(mydict, key, data, NULL);
91         }
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         mydict = *dict;
104         FILE* fpInput = fopen(inputfile, "r");
105         if (fpInput == NULL)
106                 printf("Error while opening the inputfile");
107
108         fseek(fpInput, 0, 0);
109
110         char line[MAX_LINE_INJ];
111         char *key;
112         int i;
113         xbt_hist_t* data;
114
115         if (fgets(line, MAX_LINE_INJ, fpInput) == NULL)
116                 printf("Error input file is empty!");//Skipping first row
117
118
119         while (fgets(line, MAX_LINE_INJ, fpInput) != NULL)
120         {
121                 key = strtok(line, " \t");
122
123                 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->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 (i = 0; i < data->n; i++)
135                         data->breaks[i] = atof(strtok(NULL, " \t"));
136                         for (i = 0; i < (data->n - 1); i++)
137                         {
138                                 data->percentage[i] = atof(strtok(NULL, " \t"));
139                         }
140
141                 xbt_dict_set(mydict, key, data, NULL);
142         }
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         {
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         int i, k = 0;
171         double left = 0, right = 1;
172         double timer = 0;
173         RngStream rng_stream;
174         double r, r2;
175
176         xbt_dict_t mydict = get_dict();
177         xbt_hist_t* data = xbt_dict_get_or_null(mydict, key);
178
179         if (!data)
180                 {
181                   printf("Warning: element with specified key does not exist (%s)\n",key);
182                   return 0;
183                 }
184
185         /* Choosing random interval of the histogram */
186         rng_stream = get_randgen();
187         r = RngStream_RandU01(rng_stream);
188         for (i = 0; i < (data->n - 1); i++)
189         {
190                 left += (i == 0) ? 0 : data->percentage[i - 1];
191                 right += data->percentage[i];
192                 if (left < r && r <= right)
193                         k = i;
194         }
195
196         /* Choosing random value inside the interval of the histogram */
197         r2 = RngStream_RandU01(rng_stream);
198         timer = data->breaks[k] + r2 * (data->breaks[k + 1] - data->breaks[k]);
199
200         return timer;
201 }
202
203 #endif // __INJECT_H__
204