diff options
-rw-r--r-- | doc/html/TechNotes/openmp-hdf5.html | 141 |
1 files changed, 136 insertions, 5 deletions
diff --git a/doc/html/TechNotes/openmp-hdf5.html b/doc/html/TechNotes/openmp-hdf5.html index 3902927..decf1de 100644 --- a/doc/html/TechNotes/openmp-hdf5.html +++ b/doc/html/TechNotes/openmp-hdf5.html @@ -21,9 +21,9 @@ II. Programming(SGI MPISpro compiler and C language) III. Sample Programs -Attached are three OpenMP-HDF5 test programs. The purpose of these tests is to +Attached are four OpenMP-HDF5 test programs. The purpose of these tests is to experience OpenMP parallelism with HDF5. All the tests were run on modi4 with -SGI MPISpro compiler(cc) and make. The first one is the working +SGI MPISpro compiler(cc) and make. The first two are the working program. The two following only work occasionally. I modify them directly from hdf5/examples/h5_write.c. If you want to try them out, the quickest way is @@ -38,7 +38,138 @@ is - I. First Program + I. First Program +------------------------------------------------------------------------------- +/* + * This example writes 64 datasets to a HDF5 file, using multiple threads + * (OpenMP). Each thread grab the lock while it tries to call HDF5 functions + * to write out dataset. In this way, the HDF5 calls are serialized, while + * the calculation part is in parallel. This is one of the ways to do + * OpenMP computation with HDF. As long as not to do HDF I/O in parallel, + * it is safe to use HDF. + */ + +#include <hdf5.h> +#include <omp.h> +#include <math.h> + +#define NUM_THREADS 4 +#define NUM_MDSET 16 +#define FILE "SDS.h5" +#define NX 5 /* dataset dimensions */ +#define NY 18 +#define RANK 2 + +void CalcWriteData(hid_t, hid_t, hid_t); + +/*Global variable, OpenMP lock*/ +omp_lock_t lock; + + +int +main (void) +{ + hid_t fid; /* file and dataset handles */ + hid_t datatype, dataspace; /* handles */ + hsize_t dimsf[2]; /* dataset dimensions */ + herr_t status; + int i; + + /* + * Create a new file using H5F_ACC_TRUNC access, + * default file creation properties, and default file + * access properties. + */ + fid = H5Fcreate(FILE, H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT); + + /* + * Describe the size of the array and create the data space for fixed + * size dataset. + */ + dimsf[0] = NX; + dimsf[1] = NY; + dataspace = H5Screate_simple(RANK, dimsf, NULL); + + /* + * Define datatype for the data in the file. + * We will store little endian INT numbers. + */ + datatype = H5Tcopy(H5T_NATIVE_DOUBLE); + status = H5Tset_order(datatype, H5T_ORDER_LE); + + /*Disable dynamic allocation of threads*/ + omp_set_dynamic(0); + + /*Allocate threads*/ + omp_set_num_threads(NUM_THREADS); + + /*Initialize lock*/ + omp_init_lock(&lock); + + /*Each thread grab one iteration in the for loop and call function + * CaclWriteData*/ + #pragma omp parallel default(shared) + { + #pragma omp for + for(i=0; i<NUM_THREADS; i++) { + CalcWriteData(fid, dataspace, datatype); + } + } + + /*Finished lock mechanism, destroy it*/ + omp_destroy_lock(&lock); + + /* + * Close/release resources. + */ + H5Sclose(dataspace); + H5Tclose(datatype); + H5Fclose(fid); + + return 0; +} + +/*Each thread will call this function independantly. They calculate dataset + *and then write it out to hdf, for NUM_MDSET times */ +void CalcWriteData(hid_t fid, hid_t dataspace, hid_t datatype) +{ + double data[NX][NY]; + hid_t dataset; + char dname[16]; + int tid; + int i, j, k; + + tid = omp_get_thread_num(); + + for(i=0; i<NUM_MDSET; i++) { + /*Weird calculation to extend computing time*/ + for(j=0; j<NX; j++) { + for(k=0; k<NY; k++) + data[j][k] = ( pow(sin(tid), 2.0) + pow(cos(tid), 2.0) ) * + ( tid + i ) + + ( pow(123456789.0, 8.0) - pow(123456789.0, 8.0) ); + + } + sprintf(dname, "tid%d-dataset%d", tid, i); + + /*Serialize HDF dataset writing using OpenMP lock*/ + omp_set_lock(&lock); + + dataset = H5Dcreate(fid, dname, datatype, dataspace, H5P_DEFAULT); + H5Dwrite(dataset, H5T_NATIVE_DOUBLE, H5S_ALL, H5S_ALL, H5P_DEFAULT, + data); + H5Dclose(dataset); + + /*Release lock*/ + omp_unset_lock(&lock); + } + +} + + + + + II. Second Program ------------------------------------------------------------------------------- /* * This example compute the element values of an array in parallel @@ -130,7 +261,7 @@ main (void) - II. Second Program + III. Third Program ------------------------------------------------------------------------------- /* * This example create two threads. Each thread writes a dataset to @@ -225,7 +356,7 @@ void writeData(int id, hid_t file, hid_t dataspace, hid_t datatype, char *dname) } - III. Third Program + IV. Fourth Program ------------------------------------------------------------------------------- /* * This example compute and write two datasets into HDF file in |