1 /* Copy to src/include/xbt/ folder */
3 /* Benchmarking a code block */
5 /* Use functions from bench.h to benchmark execution time of the desired block,
6 * then Rhist.R script to read all timings and produce histograms
7 * and finally inject.h to inject values instead of executing block*/
16 /* Structure that has all benchmarking information for the block*/
17 typedef struct bench {
18 struct timespec start_time;
19 struct timespec end_time;
26 extern bench_t get_mybench(void);
27 typedef bench_t (*get_bench_func_t)(void);
29 /* In order to divide nanoseconds and get result in seconds */
30 #define BILLION 1000000000L
32 /* Macros for benchmarking */
33 #define BENCH_BLOCK(block_id) for(bench_begin_block();bench_end_block(block_id);)
34 #define BENCH_EXTEND(block_id) xbt_bench_extend(block_id)
36 static inline void xbt_bench_init(char *tracefile);
37 static inline void bench_init_starpu(char *tracefile, bench_t *bench);
39 static inline void bench_begin_block();
40 static inline int bench_end_block(char* block_id);
42 static inline void xbt_bench_begin(char* block_id);
43 static inline int xbt_bench_end(char* block_id);
45 static inline void xbt_bench_extend(char* block_id);
47 /* Additional functions in order to manipulate with struct timespec */
48 static inline void xbt_diff_time(struct timespec* start, struct timespec* end, struct timespec* result_time);
49 static inline double xbt_get_time(struct timespec* timer);
51 /* Initializing SMPI benchmarking */
52 static inline void xbt_bench_init(char *tracefile)
54 bench_t mybench = get_mybench();
55 mybench->output = fopen(tracefile, "a+");
56 if (mybench->output == NULL)
57 printf("Error while opening the tracefile");
61 /* Initializing StarPU benchmarking */
62 static inline void bench_init_starpu(char *tracefile, bench_t *bench)
64 *bench = (bench_t) malloc(sizeof(**bench));
65 bench_t mybench = *bench;
66 mybench->output = fopen(tracefile, "a+");
67 if (mybench->output == NULL)
68 printf("Error while opening the tracefile");
72 /* Start benchmarking using macros */
73 static inline void bench_begin_block()
75 bench_t mybench = get_mybench();
76 clock_gettime(CLOCK_REALTIME, &mybench->start_time);
77 mybench->benchmarking = 1; // Only benchmarking once
80 /* End benchmarking using macros */
81 static inline int bench_end_block(char* block_id)
83 bench_t mybench = get_mybench();
84 if (mybench->benchmarking > 0)
86 mybench->benchmarking--;
91 clock_gettime(CLOCK_REALTIME, &mybench->end_time);
92 struct timespec interval;
93 xbt_diff_time(&mybench->start_time, &mybench->end_time, &interval);
94 fprintf(mybench->output, "%s %lf %lf %lf\n", block_id, xbt_get_time(&mybench->start_time), xbt_get_time(&mybench->end_time), xbt_get_time(&interval));
99 /* Start SMPI benchmarking */
100 static inline void xbt_bench_begin(char* block_id)
102 bench_t mybench = get_mybench();
104 strcpy (mybench->block_id, block_id);
106 strcpy (mybench->block_id, "");
107 clock_gettime(CLOCK_REALTIME, &mybench->start_time);
108 mybench->benchmarking = 1; // Only benchmarking once
111 /* End SMPI benchmarking */
112 static inline int xbt_bench_end(char* block_id)
114 bench_t mybench = get_mybench();
116 clock_gettime(CLOCK_REALTIME, &mybench->end_time);
117 struct timespec interval;
118 xbt_diff_time(&mybench->start_time, &mybench->end_time, &interval);
120 if(mybench->suffix != NULL)
122 strcat (mybench->block_id, mybench->suffix);
123 strcpy (mybench->suffix, "");
126 strcat (mybench->block_id, block_id);
127 if(mybench->block_id == NULL)
128 strcat (mybench->block_id, "NONAME");
130 fprintf(mybench->output, "%s %lf %lf %lf\n", mybench->block_id, xbt_get_time(&mybench->start_time), xbt_get_time(&mybench->end_time), xbt_get_time(&interval));
134 /* Extending the block_id name*/
135 static inline void xbt_bench_extend(char* block_id)
137 bench_t mybench = get_mybench();
138 strcpy (mybench->suffix, block_id);
141 /* Calculating time difference */
142 static inline void xbt_diff_time(struct timespec* start, struct timespec* end, struct timespec* result_time)
144 if ((end->tv_nsec - start->tv_nsec) < 0)
146 result_time->tv_sec = end->tv_sec - start->tv_sec - 1;
147 result_time->tv_nsec = (double) BILLION + end->tv_nsec - start->tv_nsec;
151 result_time->tv_sec = end->tv_sec - start->tv_sec;
152 result_time->tv_nsec = end->tv_nsec - start->tv_nsec;
156 /* Printing time in "double" format */
157 static inline double xbt_get_time(struct timespec* timer)
159 return timer->tv_sec + (double) (timer->tv_nsec / (double) BILLION);