forked from melissa-sa/melissa-sa.github.io
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tutorial.html
324 lines (302 loc) · 15.6 KB
/
tutorial.html
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<link href="prettify.css" type="text/css" rel="stylesheet" />
<script type="text/javascript" src="prettify.js"></script>
<link rel="stylesheet" href="melissa.css" />
<title>Melissa</title>
</head>
<body onload="prettyPrint()">
<div id="titlearea">
<table cellspacing="0" cellpadding="0">
<tbody>
<tr style="height: 56px;">
<td style="padding-left: 0.5em;">
<div id="melissa">
Melissa
</div>
</td>
</tr>
</tbody>
</table>
</div>
<nav id="navrow1" class="tabs">
<a href="index.html"><div id="melissa">Melissa</div></a>
<ul class="tablist">
<li><a href="getting_started.html"><span>Getting Started</span></a></li>
<li class="current"><a href="tutorial.html"><span>Tutorial</span></a></li>
<ul class="tablist2">
<li><a href="#sbs"><span>Overview</span></a></li>
<li><a href="#api"><span>Instrumentation</span></a></li>
<li><a href="#launcher"><span>Melissa Launcher</span></a></li>
</ul>
<li><a href="doxygen.html""><span>Documentation</span></a></li>
<li><a href="contacts.html"><span>Contacts</span></a></li>
<li><a href="https://github.com/melissa-sa/melissa" target="_blank"><span>Source</span></a></li>
</li>
</ul>
</nav>
<div id="contener1">
<section id="tuto_text" class="text">
<h1>Tutorial</h1><hr/>
<h2 id="sbs">Step-by-step tutorial with the heat equation solver</h2>
<p>
In this section, we will use Melissa to perform a global sensitivity analysis on a very simple case: a heat equation solver.<br />
This example is available on the folder <code class="code">examples/heat_example</code>.
It solves the heat equation on a regular 2D grid, with backward Euler method.<br />
The original source codes are <code class="code">heat_base.c</code> in C and <code class="code">heat_base.f90</code> in Fortran, available in <code class="code">examples/heat_example_base</code>.<br />
We will go through Melissa integration step by step in these examples.
The final results are available in <code class="code">heat.c</code>, <code class="code">heat.f90</code> and <code class="code">options.py</code>. They are compiled with Melissa if the option <code class="code">-DBUILD_EXAMPLES=ON</code> is given to CMake.<br />
Select your simulation type and language:
</p>
<!-- <form method="post" action="traitement.php"> -->
<div class="fieldsets">
<fieldset class="lang" id="par">
<legend>Simulation type</legend> <!-- Titre du fieldset -->
<p>
<input type="radio" name="MPI" value="nompi" id="MPI" checked/> <label for="MPI">MPI </label><br />
<input type="radio" name="MPI" value="mpi" id="noMPI" /> <label for="noMPI">No MPI</label><br />
</p>
</fieldset>
<fieldset class="lang" id="lang">
<legend>Simulation language</legend> <!-- Titre du fieldset -->
<p>
<input type="radio" name="lang" value="f" id="C" checked/> <label for="C">C</label><br />
<input type="radio" name="lang" value="c" id="Fortran" /> <label for="Fortran">Fortran</label><br />
</p>
</fieldset>
</div>
<p>
The grid is a 10 by 10 square with 100 subdivisions on each axis. The solver takes options from the command line.
The first option is the initial temperature. It is mandatory. The four next ones are the four borders temperatures (Dirichlet boundary conditions). They are optional, and are set to zero if not defined.<br />
For example, with an initial temperature of 10 and boundary conditions of 5:
<pre class="prettyprint c mpi"><code class="lang-bash">
mpirun ./heatc_base 10 5 5 5 5
</code></pre>
<pre class="prettyprint c nompi"><code class="lang-bash">
./heatc_base 10 5 5 5 5
</code></pre>
<pre class="prettyprint f mpi"><code class="lang-bash">
mpirun ./heatf_base 10 5 5 5 5
</code></pre>
<pre class="prettyprint f nompi"><code class="lang-bash">
./heatf_base 10 5 5 5 5
</code></pre>
</p>
<!-- </form> -->
<h2 id="api">Simulation Code Instrumentation</h2>
<p>
To use the heat equation solver with Melissa, we first have to define a way to pass the parameter sample ID and the Sobol rank to the simulation code.
Melissa will need it to identify which simulation sent the data.
In our case, we will do that by adding arguments to the command line.<br />
We first store the sample ID and the Sobol's rank of the simulation in two integers.
The solver can then take from one to five input parameters, stored in five doubles.
<pre class="prettyprint c"><code class="lang-c">
int sample_id = 0;
int sobol_rank = 0;
double param[5];
if (argc < 4)
{
fprintf (stderr, "Missing parameter");
return -1;
}
sobol_rank = (int)strtol(argv[1], NULL, 10); // sobol rank
sample_id = (int)strtol(argv[2], NULL, 10); // sobol group
param[0] = strtod(argv[3], NULL); // initial temperature
for (n=0; n<4; n++)
{
param[n+1] = param[0];
if (argc > n+4)
{
param[n+1] = strtod(argv[n+4], NULL);
}
}
</code></pre>
<pre class="prettyprint f"><code class="lang-fortran">
integer :: sample_id = 0
integer :: sobol_rank = 0
real*8,dimension(5) :: param
if (iargc() .lt. 4) then
print*,"Missing parameter"
return
endif
call getarg(1, arg)
read( arg, * ) sobol_rank ! sobol rank
call getarg(2, arg)
read( arg, * ) sample_id ! sobol group
call getarg(3, arg)
read( arg, * ) param(1) ! initial temperature
param(:) = param(1)
do n=5, 8
if(iargc() .ge. n) then
call getarg(n-1, arg)
read( arg, * ) param(n-3)
endif
enddo
</code></pre>
<span class="mpi">
In the case of Sobol' indices computation, we must define the way simulations are connected inside a group.
If all the simulations in a Sobol's group can easily be launched in a single MPMD MPI call, we can define a "coupled" group.
Links between simulations will be MPI communications in that case.
Otherwise, if the simulation relies on MPI_COMM_WORLD for MPI routines or is not MPI at all, simulations have to be connected via ZeroMQ.
This is called the "not coupled" case.<br />
In the heat solver, we can easily split MPI communicator, so we will use a coupled case.
Define a new integer variable, with a value of <code class="code">1</code>:
<pre class="prettyprint c"><code class="lang-c">
int coupling = 1;
</code></pre>
<pre class="prettyprint f"><code class="lang-fortran">
integer :: coupling = 1
</code></pre>
</span>
To use the Melissa functions, we have to include <code class="code">melissa_api<span class="f">.f90</span><span class="c nompi">_no_mpi.h</span><span class="c mpi">.h</span></code> at the begining of the code.
<pre class="prettyprint c mpi"><code class="lang-c">
#include <melissa_api.h>
</code></pre>
<pre class="prettyprint c nompi"><code class="lang-c">
#include <melissa_api_no_mpi.h>
</code></pre>
<pre class="prettyprint f"><code class="lang-fortran">
include "melissa_api.f90"
</code></pre>
The first Melissa function can be called before the main <code class="code">for</code> loop.
It has to be called exactly once <span class="mpi"> by every process</span>.
<span class="mpi">It takes seven arguments:</span>
<span class="nompi">It takes tree arguments:</span>
<ul>
<li><code class="code">vect_size</code>: the size of the local result vector</li>
<span class="mpi"><li><code class="code">np</code>: the size of the local MPI communicator</li></span>
<span class="mpi"><li><code class="code">me</code>: the process rank in the local MPI communicator</li></span>
<li><code class="code">sobol_rank</code>: the rank of the simulation in the Sobol' group</li>
<span class="mpi"><li><code class="code">sample_id</code>: the ID of the group in the study</li></span>
<span class="mpi"><li><code class="code">comm</code>: the local MPI communicator</li></span>
<li><code class="code">coupling</code>: the coupling variable</li>
</ul>
<pre class="prettyprint c mpi"><code class="lang-c">
melissa_init(&vect_size, &np, &me, &sobol_rank, &sample_id, &comm, &coupling);
</code></pre>
<pre class="prettyprint f mpi"><code class="lang-fortran">
call melissa_init(vect_size, np, me, sobol_rank, sample_id, comm, coupling)
</code></pre>
<pre class="prettyprint c nompi"><code class="lang-c">
melissa_init_no_mpi(&vect_size, &sobol_rank, &sample_id);
</code></pre>
<pre class="prettyprint f nompi"><code class="lang-fortran">
call melissa_init_no_mpi(vect_size, sobol_rank, sample_id)
</code></pre>
Inside the main loop, the result vector is updated by the <code class="code">conjgrad</code> function.
Send this updated vector to Melissa Server by calling <span class="mpi"><code class="code">melissa_send</code></span><span class="nompi"><code class="code">melissa_send_no_mpi</code></span> right after the <code class="code">conjgrad</code> function.
<span class="mpi">This function takes six arguments:</span>
<span class="nompi">This function takes five arguments:</span>
<ul>
<li><code class="code">n</code>: the current iteration number</li>
<li><code class="code">field_name</code>: the name of the field sent</li>
<li><code class="code">u</code>: the vector to send to Melissa Server</li>
<span class="mpi"><li><code class="code">me</code>: the process rank in the local MPI communicator</li></span>
<li><code class="code">sobol_rank</code>: the rank of the simulation in the Sobol' group</li>
<li><code class="code">sample_id</code>: the ID of the group in the study</li>
</ul>
<pre class="prettyprint c mpi"><code class="lang-c">
conjgrad (&a[0], &f[0], &u[0], &nx, &ny, &epsilon, &i1, &in, &np, &me, &next, &previous, &fcomm);
melissa_send (&n, field_name, u, &me, &sobol_rank, &sample_id);
</code></pre>
<pre class="prettyprint f mpi"><code class="lang-fortran">
call conjgrad(A, F, U, nx, ny, epsilon, i1, in, np, me, next, previous, comm)
call melissa_send(n, name, u, me, sobol_rank, sample_id)
</code></pre>
<pre class="prettyprint c nompi"><code class="lang-c">
conjgrad (&a[0], &f[0], &u[0], &nx, &ny, &epsilon);
melissa_send_no_mpi(&n, field_name, u, &sobol_rank, &sample_id);
</code></pre>
<pre class="prettyprint f nompi"><code class="lang-fortran">
call conjgrad(A, F, U, nx, ny, epsilon)
call melissa_send_no_mpi(n, name, u, sobol_rank, sample_id)
</code></pre>
After the main loop, call <code class="code">melissa_finalize</code> to free Melissa structures and disconnect the simulations from the server.
This function does not take any argument.
<pre class="prettyprint c"><code class="lang-c">
melissa_finalize();
</code></pre>
<pre class="prettyprint f"><code class="lang-fortran">
call melissa_finalize()
</code></pre>
Link the code to the <code class="code">melissa_api</code> library, and compile it.
</p>
<h2 id="launcher">Melissa Launcher</h2>
<p>
Once the simulation is instrumented, Melissa Launcher needs to know how to handle the simulation jobs.<br />
This is done by giving the launcher some functions and variables through a python file.
The file must be called <code class="code">options.py</code>. Take <code class="code">launcher/options.py</code> as a base for this file.
There are six sets of variables to define, as dictionaries.
<ul>
<li><code class="code">GLOBAL_OPTIONS</code>: contains informations about your environement.<br />
<li><code class="code">STUDY_OPTIONS</code>: sets the parameters of your sensitivity study. They will be used by the launcher to generate its internal structures for the study management.</li>
<li><code class="code">MELISSA_STATS</code>: are used to activate (or not) the computations of the iterative statistics.</li>
<li><code class="code">USER_FUNCTIONS</code>: are pointers to user defined functions, used by the launcher. Some of them are optional, others are mandatory. We will describe these functions in this section.</li>
</ul>
Note that you can add fields to the option dictionaries, and they can be used in the user defined functions.
see the <a href="getting_started.html#launcher">getting started</a> section for more details.
</p>
An example <code class="code">options.py</code> file is available in <code class="code">examples/heat_example</code>. Use it as a base to define your own <code class="code">options.py</code> file.
<h3 id="run">Launch the study</h3>
<p>
To launch a study with the heat equation solver, call the launcher with the path to your <code class="code">option.py</code> file:
<pre class="prettyprint"><code class="lang-bash">
cd examples/heat_example
python ../../launcher/melissa_launcher ./
</code></pre>
The results will be on the simulation directorie, in files of the form: <code class="code"><field_name>_<stat>.<time_stamp></code><br />
For example, the variance of the field "heat" at the first timestep whould be: <code class="code">heat_variance.001</code>
</p>
</section>
</div>
<script>
<!--
var par = document.querySelector('input[name="MPI"]:checked').value;
var lang = document.querySelector('input[name="lang"]:checked').value;
var element1 = document.getElementById('par');
element1.addEventListener('change', plop );
var element2 = document.getElementById('lang');
element2.addEventListener('change', plop );
function plop()
{
par = document.querySelector('input[name="MPI"]:checked').value;
lang = document.querySelector('input[name="lang"]:checked').value;
var i, blocks;
blocks = document.getElementsByClassName('c');
for (i = 0; i < blocks.length ; i++)
{
blocks[i].style.display = '';
}
blocks = document.getElementsByClassName('f');
for (i = 0; i < blocks.length ; i++)
{
blocks[i].style.display = '';
}
blocks = document.getElementsByClassName('mpi');
for (i = 0; i < blocks.length ; i++)
{
blocks[i].style.display = '';
}
blocks = document.getElementsByClassName('nompi');
for (i = 0; i < blocks.length ; i++)
{
blocks[i].style.display = '';
}
blocks = document.getElementsByClassName(par);
for (i = 0; i < blocks.length ; i++)
{
blocks[i].style.display = 'none';
}
blocks = document.getElementsByClassName(lang);
for (i = 0; i < blocks.length ; i++)
{
blocks[i].style.display = 'none';
}
}
plop();
//-->
</script>
</body>
</html>