summaryrefslogtreecommitdiffstats
path: root/fortran/testpar/thyperslab_wr.f90
blob: ff579988e3a56f139b77dfb559cdadd500e75da7 (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

! * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 
!   Copyright by the Board of Trustees of the University of Illinois.         *
!   All rights reserved.                                                      *
!                                                                             *
!   This file is part of HDF5.  The full HDF5 copyright notice, including     *
!   terms governing use, modification, and redistribution, is contained in    *
!   the files COPYING and Copyright.html.  COPYING can be found at the root   *
!   of the source code distribution tree; Copyright.html can be found at the  *
!   root level of an installed copy of the electronic HDF5 document set and   *
!   is linked from the top-level documents page.  It can also be found at     *
!   http://hdf.ncsa.uiuc.edu/HDF5/doc/Copyright.html.  If you do not have     *
!   access to either file, you may request a copy from hdfhelp@ncsa.uiuc.edu. *
! * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 
!
!
! This test writes/reads dataset by hyperslabs collectively. 

     SUBROUTINE dataset_wr_by_hyperslabs(cleanup, total_error) 
     USE THDF5
     IMPLICIT NONE
     LOGICAL, INTENT(IN) :: cleanup
     INTEGER, INTENT(OUT) :: total_error

     CHARACTER(LEN=8), PARAMETER :: filename = "par_sdsf"  ! File name
     CHARACTER(LEN=80) :: fix_filename
     CHARACTER(LEN=8), PARAMETER :: dsetname = "IntArray" ! Dataset name

     INTEGER(HID_T) :: file_id       ! File identifier 
     INTEGER(HID_T) :: dset_id       ! Dataset identifier 
     INTEGER(HID_T) :: filespace     ! Dataspace identifier in file 
     INTEGER(HID_T) :: memspace      ! Dataspace identifier in memory
     INTEGER(HID_T) :: plac_id      ! Property list identifier 
     INTEGER(HID_T) :: plxfer_id      ! Property list identifier 

     INTEGER(HSIZE_T), DIMENSION(2) :: dimsf = (/DIM1,DIM2/) ! Dataset dimensions.

     INTEGER(HSIZE_T), DIMENSION(2) :: count  
     INTEGER(HSIZE_T), DIMENSION(2) :: offset 
     INTEGER, ALLOCATABLE :: data (:,:)  ! Data to write
     INTEGER, ALLOCATABLE :: data_out (:,:)  ! Buffer to store data
     INTEGER :: rank = 2 ! Dataset rank 
     INTEGER :: i, j
     INTEGER :: dims(7)

     INTEGER :: error  ! Error flag
     !
     ! MPI definitions and calls.
     !
     INTEGER :: mpierror       ! MPI error flag
     INTEGER :: comm, info
     INTEGER :: mpi_size, mpi_rank
     comm = MPI_COMM_WORLD
     info = MPI_INFO_NULL
     CALL MPI_COMM_SIZE(comm, mpi_size, mpierror)
     CALL MPI_COMM_RANK(comm, mpi_rank, mpierror) 
     ! 
     ! Setup file access property list with parallel I/O access.
     !
     CALL h5pcreate_f(H5P_FILE_ACCESS_F, plac_id, error)
          CALL check("h5pcreate_f", error, total_error)
     CALL h5pset_fapl_mpio_f(plac_id, comm, info, error)
          CALL check("h5pset_fapl_mpio_f", error, total_error)
     CALL h5_fixname_f(filename, fix_filename, plac_id, error)

     !
     ! Create the file collectively.
     ! 
     CALL h5fcreate_f(fix_filename, H5F_ACC_TRUNC_F, file_id, error, access_prp = plac_id)
          CALL check("h5fcreate_f", error, total_error)
     CALL h5pclose_f(plac_id, error)
          CALL check("h5pclose_f", error, total_error)
     !
     ! Create the data space for the  dataset. 
     !
     CALL h5screate_simple_f(rank, dimsf, filespace, error)
          CALL check("h5screate_simple_f", error, total_error)

     !
     ! Create the dataset with default properties.
     !
     CALL h5dcreate_f(file_id, dsetname, H5T_NATIVE_INTEGER, filespace, &
                      dset_id, error)
          CALL check("h5dcreate_f", error, total_error)
     CALL h5sclose_f(filespace, error)
          CALL check("h5sclose_f", error, total_error)
     !
     ! Each process defines dataset in memory and writes it to the hyperslab
     ! in the file. 
     !
     count(1) = dimsf(1)
     count(2) = dimsf(2)/mpi_size 
     offset(1) = 0
     offset(2) = mpi_rank * count(2) 
     CALL h5screate_simple_f(rank, count, memspace, error) 
          CALL check("h5screate_simple_f", error, total_error)
     ! 
     ! Select hyperslab in the file.
     !
     CALL h5dget_space_f(dset_id, filespace, error)
          CALL check("h5dget_space_f", error, total_error)
     CALL h5sselect_hyperslab_f (filespace, H5S_SELECT_SET_F, offset, count, error)
          CALL check("h5sselect_hyperslab_f", error, total_error)
     ! 
     ! Initialize data buffer with trivial data.
     !
     ALLOCATE ( data(count(1),count(2)))
     dims(1) = count(1)
     dims(2) = count(2)
     data = mpi_rank + 10
     !
     ! Create property list for collective dataset write
     !
     CALL h5pcreate_f(H5P_DATASET_XFER_F, plxfer_id, error) 
          CALL check("h5pcreate_f", error, total_error)
     CALL h5pset_dxpl_mpio_f(plxfer_id, H5FD_MPIO_COLLECTIVE_F, error)
          CALL check("h5pset_dxpl_mpio_f", error, total_error)
     
     !
     ! Write the dataset collectively. 
     !
     CALL h5dwrite_f(dset_id, H5T_NATIVE_INTEGER, data, dims, error, &
                     file_space_id = filespace, mem_space_id = memspace, xfer_prp = plxfer_id)
          CALL check("h5dwrite_f", error, total_error)
     !
     ! Deallocate data buffer.
     !
     DEALLOCATE(data)

     !
     ! Close dataspaces.
     !
     CALL h5sclose_f(filespace, error)
          CALL check("h5sclose_f", error, total_error)
     CALL h5sclose_f(memspace, error)
          CALL check("h5sclose_f", error, total_error)

     !
     ! Close the dataset.
     !
     CALL h5dclose_f(dset_id, error)
          CALL check("h5dclose_f", error, total_error)

     CALL h5pclose_f(plxfer_id, error)
          CALL check("h5pclose_f", error, total_error)

     !
     ! Close the file.
     !
     CALL h5fclose_f(file_id, error)
          CALL check("h5fclose_f", error, total_error)
     !
     ! Reopen the file with || access.
     !
     CALL h5pcreate_f(H5P_FILE_ACCESS_F, plac_id, error)
          CALL check("h5pcreate_f", error, total_error)
     CALL h5pset_fapl_mpio_f(plac_id, comm, info, error)
          CALL check("h5pset_fapl_mpio_f", error, total_error)
     CALL h5fopen_f(fix_filename, H5F_ACC_RDWR_F, file_id, error, plac_id)
          CALL check("h5fopen_f", error, total_error)
     !
     ! Open dataset.
     !
     CALL h5dopen_f(file_id, dsetname, dset_id, error)
          CALL check("h5dopen_f", error, total_error)

     !
     ! Each process defines dataset in memory and reads hyperslab
     ! from the file. 
     !
     count(1) = dimsf(1)
     count(2) = dimsf(2)/mpi_size 
     offset(1) = 0
     offset(2) = mpi_rank * count(2) 
     CALL h5screate_simple_f(rank, count, memspace, error) 
          CALL check("h5screate_simple_f", error, total_error)
     ! 
     ! Select hyperslab in the file.
     !
     CALL h5dget_space_f(dset_id, filespace, error)
          CALL check("h5dget_space_f", error, total_error)
     CALL h5sselect_hyperslab_f (filespace, H5S_SELECT_SET_F, offset, count, error)
          CALL check("h5sselect_hyperslab_f", error, total_error)
     ! 
     ! Allocate data buffer.
     !
     ALLOCATE ( data(count(1),count(2)))
     ALLOCATE ( data_out(count(1),count(2)))
     data = mpi_rank + 10
     !
     ! Create property list for collective dataset write
     !
     CALL h5pcreate_f(H5P_DATASET_XFER_F, plxfer_id, error) 
          CALL check("h5pcreate_f", error, total_error)
     CALL h5pset_dxpl_mpio_f(plxfer_id, H5FD_MPIO_COLLECTIVE_F, error)
          CALL check("h5pset_dxpl_mpio_f", error, total_error)
     
     !
     ! Write the dataset collectively. 
     !
     CALL h5dread_f(dset_id, H5T_NATIVE_INTEGER, data_out, dims, error, &
                     file_space_id = filespace, mem_space_id = memspace, xfer_prp = plxfer_id)
          CALL check("h5dread_f", error, total_error)
     do j = 1, count(2)
      do i = 1, count(1)
        if( data(i,j) .ne. data_out(i,j)) then
              total_error = total_error + 1
              goto 100
        endif
      enddo 
     enddo 
 100 continue
     !
     ! Deallocate data buffer.
     !
     DEALLOCATE(data)
     DEALLOCATE(data_out)
     !
     ! Close dataspaces.
     !
     CALL h5sclose_f(filespace, error)
          CALL check("h5sclose_f", error, total_error)
     CALL h5sclose_f(memspace, error)
          CALL check("h5sclose_f", error, total_error)
     !
     ! Close property list.
     !
     CALL h5pclose_f(plxfer_id, error)
          CALL check("h5pclose_f", error, total_error)
     !
     ! Close dataset.
     !
     CALL h5dclose_f(dset_id, error)
          CALL check("h5dclose_f", error, total_error)
     !
     ! Close the file.
     !
     CALL h5fclose_f(file_id, error)
          CALL check("h5fclose_f", error, total_error)

          if(cleanup) CALL h5_cleanup_f(filename, plac_id, error)
              CALL check("h5_cleanup_f", error, total_error)

     CALL h5pclose_f(plac_id, error)
          CALL check("h5pclose_f", error, total_error)

     RETURN
     END SUBROUTINE dataset_wr_by_hyperslabs