Logo AND Algorithmique Numérique Distribuée

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