1 /* gras/emul.h - public interface to emulation support */
2 /* (specific parts for SG or RL) */
4 /* Copyright (c) 2005, 2006, 2007, 2009, 2010. The SimGrid Team.
5 * All rights reserved. */
7 /* This program is free software; you can redistribute it and/or modify it
8 * under the terms of the license (GNU LGPL) which comes with this package. */
13 #include "xbt/misc.h" /* SG_BEGIN_DECL */
16 /** @addtogroup GRAS_emul
17 * @brief Code execution "emulation" and "virtualization".
19 * Emulation and virtualization words have a lot of different meanings in
20 * computer science. Here is what we mean, and what this module allows you
21 * to do (if it does not match your personal belives, I'm sorry):
23 * - Virtualization: Having some specific code for the simulation or for the reality
24 * - Emulation: Report within the simulator the execution time of your code
26 * \section GRAS_emul_virtualization Virtualization
28 * The whole idea of GRAS is to share the same code between the simulator
29 * and the real implementation. But it is sometimes impossible, such as
30 * when you want to deal with the OS. As an example, you may want to add
31 * some extra delay before initiating a communication in RL to ensure that
32 * the receiver is listening. This is usually useless in SG since you have
33 * a much better control on process launch time.
35 * This would be done with the following snipet:
36 * \verbatim if (gras_if_RL())
37 gras_os_sleep(1);\endverbatim
39 * Please note that those are real functions and not pre-processor
40 * defines. This is to ensure that the same object code can be linked
41 * against the SG library or the RL one without recompilation.
45 /** \brief Returns true only if the program runs on real life */
46 XBT_PUBLIC(int) gras_if_RL(void);
48 /** \brief Returns true only if the program runs within the simulator */
49 XBT_PUBLIC(int) gras_if_SG(void);
53 XBT_PUBLIC(int) gras_bench_always_begin(const char *location, int line);
54 XBT_PUBLIC(int) gras_bench_always_end(void);
55 XBT_PUBLIC(int) gras_bench_once_begin(const char *location, int line);
56 XBT_PUBLIC(int) gras_bench_once_end(void);
58 /** @addtogroup GRAS_emul
59 * \section GRAS_emul_timing Emulation
61 * For simulation accuracy, it is mandatory to report the execution time
62 * of your code into the simulator. For example, if your application is a
63 * parallel matrix multiplication, you naturally have to slow down the
64 * simulated hosts actually doing the computation.
66 * If you know beforehands how long each task will last, simply add a call
67 * to the gras_bench_fixed function described below. If not, you can have
68 * GRAS benchmarking your code automatically. Simply enclose the code to
69 * time between a macro GRAS_BENCH_*_BEGIN and GRAS_BENCH_*_END, and
70 * you're done. There is three pair of such macros, whose characteristics
71 * are summarized in the following table.
75 * <td><b>Name</b></td>
76 * <td><b>Run on host machine?</b></td>
77 * <td><b>Benchmarked?</b></td>
78 * <td><b>Corresponding time reported to simulation?</b></td>
81 * <td>GRAS_BENCH_ALWAYS_BEGIN()<br>
82 * GRAS_BENCH_ALWAYS_END()</td>
88 * <td>GRAS_BENCH_ONCE_RUN_ONCE_BEGIN()<br>
89 * GRAS_BENCH_ONCE_RUN_ONCE_END()</td>
90 * <td>Only first time</td>
91 * <td>Only first time</td>
92 * <td>Each time (with stored value)</td>
95 * <td>GRAS_BENCH_ONCE_RUN_ALWAYS_BEGIN()<br>
96 * GRAS_BENCH_ONCE_RUN_ALWAYS_END()</td>
98 * <td>Only first time</td>
99 * <td>Each time (with stored value)</td>
103 * As you can see, whatever macro pair you use, the corresponding value is
104 * repported to the simulator. After all, that's what those macro are
107 * The GRAS_BENCH_ALWAYS_* macros are the simplest ones. Each time the
108 * corresponding block is encountered, the corresponding code is executed
109 * and timed. Then, the simulated host is given the corresponding amount
112 * The GRAS_BENCH_ONCE_RUN_ONCE_* macros are good for cases where you know
113 * that your execution time is constant and where you don't care about the
114 * result in simulation mode. In our example, each sub-block
115 * multiplication takes exactly the same amount of work (time depends only
116 * on size, not on content), and the operation result can safely be
117 * ignored for algorithm result. Doing so allows you to considerably
118 * reduce the amount of computation needed when running on simulator.
120 * The GRAS_BENCH_ONCE_RUN_ALWAYS_* macros are good for cases where you
121 * know that each block will induce the same amount of work (you thus
122 * don't want to bench it each time), but you actually need the result (so
123 * you have to run it each time). You may ask why you don't use
124 * GRAS_BENCH_ONCE_RUN_ONCE_* macros in this case (why you save the
125 * benchmarking time). The timing operation is not very intrusive by
126 * itself, but it has to be done in an exclusive way between the several
127 * GRAS threads (protected by mutex). So, the day where there will be
128 * threads in GRAS, this will do a big difference. Ok, I agree. For now,
129 * it makes no difference.
133 * - Blocks are automatically differenciated using the filename and line
134 * position at which the *_BEGIN part was called. Don't put two of them
137 * - You cannot nest blocks. It would make no sense, either.
139 * - By the way, GRAS is not exactly designed for parallel algorithm such
140 * as parallel matrix multiplication but for distributed ones, you weirdo.
141 * But it's just an example ;)
145 /** \brief Start benchmarking this code block
147 #define GRAS_BENCH_ALWAYS_BEGIN() gras_bench_always_begin(__FILE__, __LINE__)
148 /** \brief Stop benchmarking this code block
150 #define GRAS_BENCH_ALWAYS_END() gras_bench_always_end()
152 /** \brief Start benchmarking this code block if it has never been benchmarked, run it in any case
153 * \hideinitializer */
154 #define GRAS_BENCH_ONCE_RUN_ALWAYS_BEGIN() gras_bench_once_begin(__FILE__, __LINE__)
155 /** \brief Stop benchmarking this part of the code
157 #define GRAS_BENCH_ONCE_RUN_ALWAYS_END() gras_bench_once_end()
159 /** \brief Start benchmarking this code block if it has never been benchmarked, ignore it if it was
161 #define GRAS_BENCH_ONCE_RUN_ONCE_BEGIN() if (gras_bench_once_begin(__FILE__, __LINE__)) {
162 /** \brief Stop benchmarking this part of the code
164 #define GRAS_BENCH_ONCE_RUN_ONCE_END() } gras_bench_once_end()
166 XBT_PUBLIC(void) gras_cpu_burn(double flops);
170 #endif /* GRAS_COND_H */