Skip to content

Commit 9707882

Browse files
jmorice91JAuriac
andauthored
tutorial update: hdf5_parallel, fix #16 (#20)
Update ex9 --------- Co-authored-by: JAuriac <56091659+JAuriac@users.noreply.github.com>
1 parent 0a69d12 commit 9707882

5 files changed

Lines changed: 323 additions & 213 deletions

File tree

README.md

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -401,33 +401,47 @@ the selection where to write in the file dataset.
401401

402402
Running the code from the previous exercises in parallel should already work and
403403
yield one file per process containing the local data block.
404-
In this exercise you will write one single file with parallel HDF5 whose content
405-
should be independent from the number of processes used.
406-
Once again, you only need to modify the YAML file in this exercise, no need to
407-
touch the C file.
404+
In this exercise you will write one single file `ex9.h5`(see `ex9.yml`) with
405+
parallel HDF5 whose content should be independent from the number of
406+
processes used.
407+
408+
\attention You need to do this exercise with a parallel version of HDF5 and the
409+
\ref Decl_HDF5_plugin "Decl'HDF5 plugin" compiled in parallel.
410+
411+
Once again, you only need to modify the YAML file in this exercise,
412+
no need to touch the C file.
408413

409414
* Examine the YAML file and compile the code.
410415

411-
The `mpi` plugin was loaded to make sharing MPI communicators possible.
416+
* Load the `mpi` plugin to make sharing MPI communicators possible.
412417

413-
* Uncomment the `communicator` directive of the
414-
\ref Decl_HDF5_plugin "Decl'HDF5 plugin" to switch to parallel I/O and change
415-
the file name so that all processes access the same file.
418+
* Define the `communicator` directive of the \ref Decl_HDF5_plugin "Decl'HDF5 plugin"
419+
to switch to parallel I/O for HDF5. Set the value of the communicator to MPI_COMM_WORLD.
416420

417-
* Set the size of the dataset to take the global (parallel) array size into
418-
account.
421+
\note We have added the directive `collision_policy: write_into` of the
422+
\ref Decl_HDF5_plugin "Decl'HDF5 plugin" (see section COLLISION_POLICY).
423+
This parameter is used to define what to do when writing to a file or dataset
424+
that already exists.
425+
426+
* Set the size of the dataset to take the global (parallel) array size into account.
419427
You will need to multiply the local size by the number of processes in each
420428
dimension (use `psize`).
421429

422430
* Ensure the dataset selection of each process does not overlap with the others.
423431
You will need to make a selection in the dataset that depends on the global
424432
coordinate of the local data block (use `pcoord`).
425433

426-
Match the output from `ex9.h5dump`, that should be independent from the number
427-
of processes used. You can easily check if the files are the same by running:
434+
You should be able to match the expected output described in `ex9.h5dump`.
435+
You can easily check if the files are the same by running:
428436
```bash
429437
diff ex9.h5dump <(h5dump ex9*.h5)
430438
```
439+
To see your `h5` file in readable file format,
440+
you can check the section [Comparison with the `h5dump` command](#h5comparison).
441+
442+
\warning
443+
If you relaunch the executable, remember to delete your old `ex9.h5` file before,
444+
otherwise the data will not be changed correctly.
431445

432446
![graphical representation of the parallel I/O](PDI_hdf5_parallel.jpg)
433447

ex9.c

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,32 @@ int pcoord[2];
4444
/// the alpha coefficient used in the computation
4545
double alpha;
4646

47+
double L=1.0;
48+
double source1[4]={0.4, 0.4, 0.2, 100};
49+
double source2[4]={0.7, 0.8, 0.1, 200};
50+
4751
/** Initialize the data all to 0 except for the left border (XX==0) initialized to 1 million
4852
* \param[out] dat the local data to initialize
4953
*/
5054
void init(double dat[dsize[0]][dsize[1]])
5155
{
5256
for (int yy=0; yy<dsize[0]; ++yy) for (int xx=0; xx<dsize[1]; ++xx) dat[yy][xx] = 0;
53-
if ( pcoord[1] == 0 ) for (int yy=0; yy<dsize[0]; ++yy) dat[yy][0] = 1000000;
57+
double dy = L / ((dsize[0]-2) *psize[0]) ;
58+
double dx = L / ((dsize[1]-2) *psize[1]) ;
59+
60+
double cpos_x,cpos_y;
61+
for(int yy=0; yy<dsize[0];++yy) {
62+
cpos_y=(yy+pcoord[0]*(dsize[0]-2))*dy-0.5*dy;
63+
for(int xx=0; xx<dsize[1];++xx) {
64+
cpos_x=(xx+pcoord[1]*(dsize[1]-2))*dx-0.5*dx;
65+
if((cpos_y-source1[0])*(cpos_y-source1[0]) + (cpos_x-source1[1])*(cpos_x-source1[1]) <= source1[2]*source1[2]) {
66+
dat[yy][xx] = source1[3];
67+
}
68+
if((cpos_y-source2[0])*(cpos_y-source2[0]) + (cpos_x-source2[1])*(cpos_x-source2[1]) <= source2[2]*source2[2]) {
69+
dat[yy][xx] = source2[3];
70+
}
71+
}
72+
}
5473
}
5574

5675
/** Compute the values at the next time-step based on the values at the current time-step
@@ -60,21 +79,15 @@ void init(double dat[dsize[0]][dsize[1]])
6079
void iter(double cur[dsize[0]][dsize[1]], double next[dsize[0]][dsize[1]])
6180
{
6281
int xx, yy;
63-
for (xx=0; xx<dsize[1]; ++xx) next[0][xx] = cur[0][xx];
6482
for (yy=1; yy<dsize[0]-1; ++yy) {
65-
next[yy][0] = cur[yy][0];
6683
for (xx=1; xx<dsize[1]-1; ++xx) {
67-
next[yy][xx] =
68-
(1.-4.*alpha) * cur[yy][xx]
69-
+ alpha * ( cur[yy][xx-1]
70-
+ cur[yy][xx+1]
71-
+ cur[yy-1][xx]
72-
+ cur[yy+1][xx]
73-
);
84+
next[yy][xx] = (1.-4.*alpha) * cur[yy][xx]
85+
+alpha * ( cur[yy][xx-1]
86+
+ cur[yy][xx+1]
87+
+ cur[yy-1][xx]
88+
+ cur[yy+1][xx]);
7489
}
75-
next[yy][dsize[1]-1] = cur[yy][dsize[1]-1];
7690
}
77-
for (xx=0; xx<dsize[1]; ++xx) next[dsize[0]-1][xx] = cur[dsize[0]-1][xx];
7891
}
7992

8093
/** Exchanges ghost values with neighbours
@@ -162,7 +175,7 @@ int main( int argc, char* argv[] )
162175
dsize[1] = global_size[1]/psize[1] + 2;
163176

164177
// create a 2D Cartesian MPI communicator & get our coordinate (rank) in it
165-
int cart_period[2] = { 0, 0 };
178+
int cart_period[2] = { 1, 1 };
166179
MPI_Comm cart_comm; MPI_Cart_create(main_comm, 2, psize, cart_period, 1, &cart_comm);
167180
MPI_Cart_coords(cart_comm, pcoord_1d, 2, pcoord);
168181

@@ -178,11 +191,9 @@ int main( int argc, char* argv[] )
178191
int ii=0;
179192

180193
// share useful configuration bits with PDI
181-
PDI_expose("ii", &ii, PDI_OUT);
182194
PDI_expose("pcoord", pcoord, PDI_OUT);
183195
PDI_expose("dsize", dsize, PDI_OUT);
184196
PDI_expose("psize", psize, PDI_OUT);
185-
PDI_expose("main_field", cur, PDI_OUT);
186197

187198
// the main loop
188199
for (; ii<10; ++ii) {

0 commit comments

Comments
 (0)