Skip to content

Commit

Permalink
hdf5_parallel, fix #16
Browse files Browse the repository at this point in the history
  • Loading branch information
jmorice91 committed Nov 21, 2024
1 parent 7334107 commit def84c6
Show file tree
Hide file tree
Showing 5 changed files with 313 additions and 213 deletions.
27 changes: 16 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -344,33 +344,38 @@ the selection where to write in the file dataset.

Running the code from the previous exercises in parallel should already work and
yield one file per process containing the local data block.
In this exercise you will write one single file with parallel HDF5 whose content
In this exercise you will write one single file `ex9.h5`(see `ex9.yml`) with parallel HDF5 whose content
should be independent from the number of processes used.
Once again, you only need to modify the YAML file in this exercise, no need to
touch the C file.

\attention You need to do this exercise with a parallel version of HDF5 and the \ref Decl_HDF5_plugin "Decl'HDF5 plugin" compiled in parallel.

Once again, you only need to modify the YAML file in this exercise, no need to touch the C file.

* Examine the YAML file and compile the code.

The `mpi` plugin was loaded to make sharing MPI communicators possible.
* Load the `mpi` plugin to make sharing MPI communicators possible.

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

* Set the size of the dataset to take the global (parallel) array size into
account.
\note we have added the directive `collision_policy: write_into` of the \ref Decl_HDF5_plugin "Decl'HDF5 plugin" (see section COLLISION_POLICY). This parameter is used to define what to do when writing to a file or dataset that already exists.

* Set the size of the dataset to take the global (parallel) array size into account.
You will need to multiply the local size by the number of processes in each
dimension (use `psize`).

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

Match the output from `ex9.h5dump`, that should be independent from the number
of processes used. You can easily check if the files are the same by running:
You should be able to match the expected output described in `ex9.h5dump`. You can easily check if the files are the same by running:
```bash
diff ex9.h5dump <(h5dump ex9*.h5)
```
To see your `h5` file in readable file format, you can check the section [Comparison with the `h5dump` command](#h5comparison).

\warning
If you relaunch the executable, remember to delete your old `ex9.h5` file before, otherwise the data will not be changed correctly.

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

Expand Down
42 changes: 26 additions & 16 deletions ex9.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
#include <time.h>

#include <paraconf.h>
// load the PDI header
#include <pdi.h>

/// size of the local data as [HEIGHT, WIDTH] including ghosts & boundary constants
Expand All @@ -44,13 +43,32 @@ int pcoord[2];
/// the alpha coefficient used in the computation
double alpha;

double L=1.0;
double source1[4]={0.4, 0.4, 0.2, 100};
double source2[4]={0.7, 0.8, 0.1, 200};

/** Initialize the data all to 0 except for the left border (XX==0) initialized to 1 million
* \param[out] dat the local data to initialize
*/
void init(double dat[dsize[0]][dsize[1]])
{
for (int yy=0; yy<dsize[0]; ++yy) for (int xx=0; xx<dsize[1]; ++xx) dat[yy][xx] = 0;
if ( pcoord[1] == 0 ) for (int yy=0; yy<dsize[0]; ++yy) dat[yy][0] = 1000000;
double dy = L / ((dsize[0]-2) *psize[0]) ;
double dx = L / ((dsize[1]-2) *psize[1]) ;

double cpos_x,cpos_y;
for(int yy=0; yy<dsize[0];++yy) {
cpos_y=(yy+pcoord[0]*(dsize[0]-2))*dy-0.5*dy;
for(int xx=0; xx<dsize[1];++xx) {
cpos_x=(xx+pcoord[1]*(dsize[1]-2))*dx-0.5*dx;
if((cpos_y-source1[0])*(cpos_y-source1[0]) + (cpos_x-source1[1])*(cpos_x-source1[1]) <= source1[2]*source1[2]) {
dat[yy][xx] = source1[3];
}
if((cpos_y-source2[0])*(cpos_y-source2[0]) + (cpos_x-source2[1])*(cpos_x-source2[1]) <= source2[2]*source2[2]) {
dat[yy][xx] = source2[3];
}
}
}
}

/** Compute the values at the next time-step based on the values at the current time-step
Expand All @@ -60,21 +78,15 @@ void init(double dat[dsize[0]][dsize[1]])
void iter(double cur[dsize[0]][dsize[1]], double next[dsize[0]][dsize[1]])
{
int xx, yy;
for (xx=0; xx<dsize[1]; ++xx) next[0][xx] = cur[0][xx];
for (yy=1; yy<dsize[0]-1; ++yy) {
next[yy][0] = cur[yy][0];
for (xx=1; xx<dsize[1]-1; ++xx) {
next[yy][xx] =
(1.-4.*alpha) * cur[yy][xx]
+ alpha * ( cur[yy][xx-1]
+ cur[yy][xx+1]
+ cur[yy-1][xx]
+ cur[yy+1][xx]
);
next[yy][xx] = (1.-4.*alpha) * cur[yy][xx]
+alpha * ( cur[yy][xx-1]
+ cur[yy][xx+1]
+ cur[yy-1][xx]
+ cur[yy+1][xx]);
}
next[yy][dsize[1]-1] = cur[yy][dsize[1]-1];
}
for (xx=0; xx<dsize[1]; ++xx) next[dsize[0]-1][xx] = cur[dsize[0]-1][xx];
}

/** Exchanges ghost values with neighbours
Expand Down Expand Up @@ -162,7 +174,7 @@ int main( int argc, char* argv[] )
dsize[1] = global_size[1]/psize[1] + 2;

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

Expand All @@ -178,11 +190,9 @@ int main( int argc, char* argv[] )
int ii=0;

// share useful configuration bits with PDI
PDI_expose("ii", &ii, PDI_OUT);
PDI_expose("pcoord", pcoord, PDI_OUT);
PDI_expose("dsize", dsize, PDI_OUT);
PDI_expose("psize", psize, PDI_OUT);
PDI_expose("main_field", cur, PDI_OUT);

// the main loop
for (; ii<10; ++ii) {
Expand Down
Loading

0 comments on commit def84c6

Please sign in to comment.