File: inject.h

package info (click to toggle)
simgrid 4.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 39,192 kB
  • sloc: cpp: 124,913; ansic: 66,744; python: 8,560; java: 6,773; fortran: 6,079; f90: 5,123; xml: 4,587; sh: 2,194; perl: 1,436; makefile: 111; lisp: 49; javascript: 7; sed: 6
file content (199 lines) | stat: -rw-r--r-- 6,080 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
/* Copyright (c) 2013-2025. The SimGrid Team.  All rights reserved.         */

/* This program is free software; you can redistribute it and/or modify it
 * under the terms of the license (GNU LGPL) which comes with this package. */

/* Copy to src/include/xbt/ folder  */

/* Injecting timings for previously benchmarked code blocks */

/* Use functions from bench.h to benchmark execution time of the desired block,
 * then Rhist.R script to read all timings and produce histograms
 * and finally inject.h to inject values instead of executing block*/

#ifndef __INJECT_H__
#define __INJECT_H__

#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include "xbt/RngStream.h"
#include "xbt/dict.h"
#include "xbt/sysdep.h"

#define MAX_LINE_INJ 10000

/*
 * Histogram entry for each measured block
 * Each entry is guarded inside xbt dictionary which is read from the file */
typedef struct xbt_hist {
  int n;
  int counts;
  double mean;
  double* breaks;
  double* percentage;
  char* block_id;
} xbt_hist_t;

extern RngStream get_randgen(void);
typedef RngStream (*get_randgen_func_t)(void);

extern xbt_dict_t get_dict(void);
typedef xbt_dict_t (*get_dict_func_t)(void);

static inline void xbt_inject_init(char *inputfile);
static inline void inject_init_starpu(char *inputfile, xbt_dict_t *dict, RngStream *rng);

static inline double xbt_inject_time(char *key);
static inline double xbt_mean_time(char *key);
static inline double xbt_hist_time(char *key);

/* Initializing xbt dictionary for SMPI version, reading xbt_hist_t entries line by line */
static inline void xbt_inject_init(char *inputfile)
{
  xbt_dict_t mydict = get_dict();
  FILE* fpInput     = fopen(inputfile, "r");
  xbt_assert(fpInput != NULL, "Error while opening the inputfile");
  fseek(fpInput, 0, 0);

  char line[200];
  char* key;

  if (fgets(line, 200, fpInput) == NULL)
    printf("Error input file is empty!"); // Skipping first row
  while (fgets(line, 200, fpInput) != NULL) {
    char *saveptr = NULL; /* for strtok_r() */
    key = strtok_r(line, "\t", &saveptr);

    xbt_hist_t* data = xbt_dict_get_or_null(mydict, key);
    if (data)
      printf("Error, data with that block_id already exists!");

    data = (xbt_hist_t*)xbt_new(xbt_hist_t, 1);

    data->block_id = key;
    data->counts   = atoi(strtok_r(NULL, "\t", &saveptr));
    data->mean     = atof(strtok_r(NULL, "\t", &saveptr));
    data->n        = atoi(strtok_r(NULL, "\t", &saveptr));

    data->breaks     = (double*)malloc(sizeof(double) * data->n);
    data->percentage = (double*)malloc(sizeof(double) * (data->n - 1));
    for (int i        = 0; i < data->n; i++)
      data->breaks[i] = atof(strtok_r(NULL, "\t", &saveptr));
    for (int i            = 0; i < (data->n - 1); i++)
      data->percentage[i] = atof(strtok_r(NULL, "\t", &saveptr));

    xbt_dict_set(mydict, key, data);
  }
  fclose(fpInput);
}

/* Initializing xbt dictionary for StarPU version, reading xbt_hist_t entries line by line */
static inline void inject_init_starpu(char *inputfile, xbt_dict_t *dict, RngStream *rng)
{
  *dict                = xbt_dict_new_homogeneous(free);
  *rng                 = RngStream_CreateStream("Randgen1");
  unsigned long seed[] = {134, 233445, 865, 2634, 424242, 876541};
  RngStream_SetSeed(*rng, seed);

  xbt_dict_t mydict = *dict;
  FILE* fpInput     = fopen(inputfile, "r");
  if (fpInput == NULL) {
    printf("Error while opening the inputfile");
    return;
  }

  fseek(fpInput, 0, 0);

  char line[MAX_LINE_INJ];
  char* key;

  if (fgets(line, MAX_LINE_INJ, fpInput) == NULL) {
    printf("Error input file is empty!"); // Skipping first row
    fclose(fpInput);
    return;
  }

  while (fgets(line, MAX_LINE_INJ, fpInput) != NULL) {
    char *saveptr = NULL; /* for strtok_r() */
    key = strtok_r(line, "\t", &saveptr);

    xbt_hist_t* data = xbt_dict_get_or_null(mydict, key);
    if (data)
      printf("Error, data with that block_id already exists!");

    data             = (xbt_hist_t*)xbt_new(xbt_hist_t, 1);
    data->block_id   = key;
    data->counts     = atoi(strtok_r(NULL, "\t", &saveptr));
    data->mean       = atof(strtok_r(NULL, "\t", &saveptr));
    data->n          = atoi(strtok_r(NULL, "\t", &saveptr));
    data->breaks     = (double*)malloc(sizeof(double) * data->n);
    data->percentage = (double*)malloc(sizeof(double) * (data->n - 1));

    for (int i        = 0; i < data->n; i++)
      data->breaks[i] = atof(strtok_r(NULL, "\t", &saveptr));
    for (int i = 0; i < (data->n - 1); i++) {
      data->percentage[i] = atof(strtok_r(NULL, "\t", &saveptr));
    }

    xbt_dict_set(mydict, key, data);
  }
  fclose(fpInput);
}

/* Injecting time */
static inline double xbt_inject_time(char *key)
{
  return xbt_hist_time(key);
  // return xbt_mean_time(key);
}

/* Injecting mean value */
static inline double xbt_mean_time(char *key)
{
  xbt_dict_t mydict = get_dict();
  xbt_hist_t* data  = xbt_dict_get_or_null(mydict, key);

  if (!data) {
    printf("Warning: element with specified key does not exist (%s)\n", key);
    return 0;
  }

  return data->mean;
}

/* Injecting random value from the histogram */
static inline double xbt_hist_time(char *key)
{
  xbt_dict_t mydict = get_dict();
  xbt_hist_t* data  = xbt_dict_get_or_null(mydict, key);

  if (!data) {
    printf("Warning: element with specified key does not exist (%s)\n", key);
    return 0;
  }

  /* Choosing random interval of the histogram */
  RngStream rng_stream = get_randgen();
  double r             = RngStream_RandU01(rng_stream);
  int k                = 0;
  double left          = 0;
  double right         = 1;
  for (int i = 0; i < (data->n - 1); i++) {
    left += (i == 0) ? 0 : data->percentage[i - 1];
    right += data->percentage[i];
    if (left < r && r <= right)
      k = i;
  }

  /* Choosing random value inside the interval of the histogram */
  double r2    = RngStream_RandU01(rng_stream);
  double timer = data->breaks[k] + r2 * (data->breaks[k + 1] - data->breaks[k]);

  return timer;
}

#endif // __INJECT_H__