Logo AND Algorithmique Numérique Distribuée

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