summaryrefslogtreecommitdiffstats
path: root/doc/html/TechNotes/openmp-test.txt
blob: 686e647c1e20ac72932d0b054e2d20623517cd9e (plain)
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
Attached are three 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 
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 
  1.  have your hdf5 program compiled, 
  2.  go to hdf5/examples directory,
  3.  add -mp option to the end of the CFLAGS list in the Makefile.  If you 
      have the compiled program in another directory, you should go to the 
      examples in that directory.
  4.  modify the hdf5/examples/h5_write.c according to the program attached 
      here.  
  5.  use hdf5/tools/h5dump to examine the output file.



		I.  First Program
-------------------------------------------------------------------------------
/*  
 *  This example compute the element values of an array in parallel 
 *  by two threads.  Then write this dataset into HDF file.  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>

#define FILE        "SDS.h5"
#define DATASETNAME "IntArray" 
#define NX     5                      /* dataset dimensions */
#define NY     6
#define RANK   2

int
main (void)
{
    hid_t       file, dataset;         /* file and dataset handles */
    hid_t       datatype, dataspace;   /* handles */
    hsize_t     dimsf[2];              /* dataset dimensions */
    herr_t      status;                             
    int         data[NX][NY];          /* data to write */
    int         i, j;

    /*
     * Create a new file using H5F_ACC_TRUNC access,
     * default file creation properties, and default file
     * access properties.
     */
    file = 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_INT);
    status = H5Tset_order(datatype, H5T_ORDER_LE);

    /* Disable dynamic allocation of threads. */
    omp_set_dynamic(0);
    
    /* Allocate 2 threads */
    omp_set_num_threads(2);


    /* Parallel computation.  Let 2 threads handle this nested for loops
     * in parallel.  Only one data array is computed.  */  
    #pragma omp parallel default(shared) 
    {
        #pragma omp for 
            for (j = 0; j < NX; j++) {
                #pragma omp parallel shared(j, NY) 
                {
                    #pragma omp for 
                        for (i = 0; i < NY; i++)
                            data[j][i] = i + j; 
                }
            }
    }

    /* Write this dataset into HDF file */
    dataset = H5Dcreate(file, DATASETNAME, datatype, dataspace,
                        H5P_DEFAULT); 
    H5Dwrite(dataset, H5T_NATIVE_INT, H5S_ALL, H5S_ALL,
                        H5P_DEFAULT, data);
    H5Dclose(dataset);

    /*
     * Close/release resources.
     */
    H5Sclose(dataspace);
    H5Tclose(datatype);
    H5Fclose(file);
 
    return 0;
}     



		II.  Second Program
-------------------------------------------------------------------------------
/*  
 *  This example create two threads.  Each thread writes  a dataset to 
 *  the HDF5 file in parallel.  This program only works occasionally.
 */
 
#include <hdf5.h>
#include <omp.h>

#define FILE   "SDS.h5"
#define NX     5                      /* dataset dimensions */
#define NY     6
#define RANK   2

void writeData(int, hid_t, hid_t, hid_t, char*);

int main (void)
{
    hid_t       file;                  /* file and dataset handles */
    hid_t       datatype, dataspace;   /* handles */
    hsize_t     dimsf[2];              /* dataset dimensions */
    herr_t      status;                             
    int         id;
    char        dname[2][10] = {"Array1", "Array2"};

    /*
     * Create a new file using H5F_ACC_TRUNC access,
     * default file creation properties, and default file
     * access properties.
     */
    file = 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_INT);
    status = H5Tset_order(datatype, H5T_ORDER_LE);

    /*Disable dynamic allocation of threads*/
    omp_set_dynamic(0);
 
    /*Allocate 2 threads*/
    omp_set_num_threads(2);

    /*Parallel Part:  each thread call function writeData; id is private to 
     *                thread while others are shared                     */
    #pragma omp parallel shared(file, dataspace, datatype, dname) private(id)
    {
         id = omp_get_thread_num();
         writeData(id, file, dataspace, datatype, dname[id]);           
    }


    /*
     * Close/release resources.
     */
    H5Sclose(dataspace);
    H5Tclose(datatype);
    H5Fclose(file);
 
    return 0;
}   


/*Each thread call this function to write a dataset into HDF5 file*/
 
void writeData(int id, hid_t file, hid_t dataspace, hid_t datatype, char *dname)
{
    int    data[NX][NY];
    hid_t  dataset;
    int    i, j;

    for (j = 0; j < NX; j++) {
        for (i = 0; i < NY; i++)
            data[j][i] = i + j + id;
    }

    dataset = H5Dcreate(file, dname, datatype, dataspace,
                       H5P_DEFAULT);
    H5Dwrite(dataset, H5T_NATIVE_INT, H5S_ALL, H5S_ALL,
                       H5P_DEFAULT, data);
    H5Dclose(dataset);
}


		III.  Third Program
-------------------------------------------------------------------------------
/*  
 * This example compute and write two datasets into HDF file in
 * parallel.  It also only works occasionally.      
 */
 
#include <hdf5.h>
#include <omp.h>

#define FILE        "SDS.h5"
#define DATASETNAME "IntArray" 
#define NX     5                      /* dataset dimensions */
#define NY     6
#define RANK   2

int
main (void)
{
    hid_t       file, dataset;         /* file and dataset handles */
    hid_t       datatype, dataspace;   /* handles */
    hsize_t     dimsf[2];              /* dataset dimensions */
    herr_t      status;                             
    int         data[NX][NY];          /* data to write */
    int         i, j, id;
    char        dname[2][10] = {"intArray1", "intArray2"};

    /*
     * Create a new file using H5F_ACC_TRUNC access,
     * default file creation properties, and default file
     * access properties.
     */
    file = 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_INT);
    status = H5Tset_order(datatype, H5T_ORDER_LE);

    omp_set_dynamic(0);
    omp_set_num_threads(2);
    
    
    /* This part of program compute and write two datasets in parallel. */
    #pragma omp parallel shared(file, datatype, dataspace, dname) private(id, j, i, data, dataset)
    {
        id = omp_get_thread_num();
        for (j = 0; j < NX; j++) {
            for (i = 0; i < NY; i++)
                data[j][i] = i + j + id;
        }

        dataset = H5Dcreate(file, dname[id], datatype, dataspace,
                            H5P_DEFAULT); 
        H5Dwrite(dataset, H5T_NATIVE_INT, H5S_ALL, H5S_ALL,
                              H5P_DEFAULT, data);
        H5Dclose(dataset);
    }


    /*
     * Close/release resources.
     */
    H5Sclose(dataspace);
    H5Tclose(datatype);
    H5Fclose(file);
 
    return 0;
}