1 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
2  * Copyright by The HDF Group.                                               *
3  * Copyright by the Board of Trustees of the University of Illinois.         *
4  * All rights reserved.                                                      *
5  *                                                                           *
6  * This file is part of HDF5.  The full HDF5 copyright notice, including     *
7  * terms governing use, modification, and redistribution, is contained in    *
8  * the COPYING file, which can be found at the root of the source code       *
9  * distribution tree, or in https://support.hdfgroup.org/ftp/HDF5/releases.  *
10  * If you do not have access to either file, you may request a copy from     *
11  * help@hdfgroup.org.                                                        *
12  * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
13 
14 
15 #include "h5test.h"
16 #include "vds_swmr.h"
17 
18 int
main(int argc,char * argv[])19 main(int argc, char *argv[])
20 {
21     int file_number     = -1;   /* Source file number               */
22 
23     hid_t fid           = -1;   /* HDF5 file ID                     */
24     hid_t faplid        = -1;   /* file access property list ID                */
25     hid_t did           = -1;   /* dataset ID                       */
26     hid_t msid          = -1;   /* memory dataspace ID              */
27     hid_t fsid          = -1;   /* file dataspace ID                */
28 
29     hsize_t extent[RANK];       /* dataset extents                  */
30     hsize_t start[RANK];        /* hyperslab start point            */
31 
32     int *buffer         = NULL; /* data buffer                      */
33     int value           = -1;   /* value written to datasets        */
34 
35     hsize_t n_elements  = 0;    /* number of elements in a plane    */
36 
37     hsize_t i;                  /* iterator                         */
38     hsize_t j;                  /* iterator                         */
39 
40 
41     /******************************
42      * Fill a source dataset file *
43      ******************************/
44 
45     /* The file number is passed on the command line.
46      * This is an integer index into the FILE_NAMES array.
47      */
48     if(argc != 2) {
49         HDfprintf(stderr, "ERROR: Must pass the source file number on the command line.\n");
50         return EXIT_FAILURE;
51     }
52 
53     file_number = HDatoi(argv[1]);
54     if(file_number < 0 || file_number >= N_SOURCES)
55         TEST_ERROR
56 
57     /* Open the source file and dataset */
58     /* All SWMR files need to use the latest file format */
59     if((faplid = h5_fileaccess()) < 0)
60         TEST_ERROR
61     if(H5Pset_libver_bounds(faplid, H5F_LIBVER_LATEST, H5F_LIBVER_LATEST) < 0)
62         TEST_ERROR
63     if((fid = H5Fopen(FILE_NAMES[file_number], H5F_ACC_RDWR | H5F_ACC_SWMR_WRITE, faplid)) < 0)
64         TEST_ERROR
65     if((did = H5Dopen2(fid, SOURCE_DSET_PATH, H5P_DEFAULT)) < 0)
66         TEST_ERROR
67 
68 
69     /* Create a data buffer that represents a plane */
70     n_elements = PLANES[file_number][1] * PLANES[file_number][2];
71     if(NULL == (buffer = (int *)HDmalloc(n_elements * sizeof(int))))
72         TEST_ERROR
73 
74     /* Create the memory dataspace */
75     if((msid = H5Screate_simple(RANK, PLANES[file_number], NULL)) < 0)
76         TEST_ERROR
77 
78     /* Write planes to the dataset */
79     for(i = 0; i < N_PLANES_TO_WRITE; i++) {
80 
81         time_t delay;           /* Time interval between plane writes */
82 
83         /* Cork the dataset's metadata in the cache */
84         if(H5Odisable_mdc_flushes(did) < 0)
85             TEST_ERROR
86 
87         /* Set the dataset's extent. This is inefficient but that's ok here. */
88         extent[0] = i + 1;
89         extent[1] = PLANES[file_number][1];
90         extent[2] = PLANES[file_number][2];
91         if(H5Dset_extent(did, extent) < 0)
92             TEST_ERROR
93 
94         /* Get the file dataspace */
95         if((fsid = H5Dget_space(did)) < 0)
96             TEST_ERROR
97 
98         /* Each plane is filled with the plane number as a data value. */
99         value = (((int)i + 1) * 10) + (int)i;
100         for(j = 0; j < n_elements; j++)
101            buffer[j] = value;
102 
103         /* Set up the hyperslab for writing. */
104         start[0] = i;
105         start[1] = 0;
106         start[2] = 0;
107         if(H5Sselect_hyperslab(fsid, H5S_SELECT_SET, start, NULL, PLANES[file_number], NULL) < 0)
108             TEST_ERROR
109 
110         /* Write the plane to the dataset. */
111         if(H5Dwrite(did, H5T_NATIVE_INT, msid, fsid, H5P_DEFAULT, buffer) < 0)
112             TEST_ERROR
113 
114         /* Uncork the dataset's metadata from the cache */
115         if(H5Oenable_mdc_flushes(did) < 0)
116             TEST_ERROR
117 
118         /* Wait one second between writing planes */
119         delay = HDtime(0) + (time_t)1;
120         while(HDtime(0) < delay)
121             ;
122 
123         /* Flush */
124         if(H5Fflush(fid, H5F_SCOPE_GLOBAL) < 0)
125             TEST_ERROR
126 
127     } /* end for */
128 
129     if(H5Pclose(faplid) < 0)
130         TEST_ERROR
131     if(H5Sclose(msid) < 0)
132         TEST_ERROR
133     if(H5Sclose(fsid) < 0)
134         TEST_ERROR
135     if(H5Dclose(did) < 0)
136         TEST_ERROR
137     if(H5Fclose(fid) < 0)
138         TEST_ERROR
139     HDfree(buffer);
140 
141     HDfprintf(stderr, "SWMR writer exited successfully\n");
142     return EXIT_SUCCESS;
143 
144 error:
145 
146     H5E_BEGIN_TRY {
147         if(fid >= 0)
148             (void)H5Fclose(fid);
149         if(faplid >= 0)
150             (void)H5Pclose(faplid);
151         if(did >= 0)
152             (void)H5Dclose(did);
153         if(msid >= 0)
154             (void)H5Sclose(msid);
155         if(fsid >= 0)
156             (void)H5Sclose(fsid);
157         if(buffer != NULL)
158             HDfree(buffer);
159     } H5E_END_TRY
160 
161     HDfprintf(stderr, "ERROR: SWMR writer exited with errors\n");
162     return EXIT_FAILURE;
163 
164 } /* end main */
165 
166