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
 ss_file_t *
 ss_file_open(ss_file_t *file,           /* Optional handle to a persistent file object from a /File/ table. */
              const char *name,          /* Optional name of file to be opened. */
              unsigned flags,            /* HDF5-style file access flags. */
              ss_prop_t *props           /* Optional file property list (see File Properties). */
              )
 {
     SS_ENTER(ss_file_open, ss_file_tP);
     size_t              gfileidx;               /* Index into the GFile array */
     ss_gfile_t          *gfile=NULL;            /* An unused slot from the GFile array */
     char                *fixedname=NULL;        /* Normalized absolute name */
     hid_t               fapl_created=-1, fcpl_created=-1, fapl=-1, fcpl=-1;
     MPI_Comm            comm, comm_temp;
     hbool_t             comm_duped=FALSE;
     MPI_Info            info=MPI_INFO_NULL;
     htri_t              creating=FALSE;         /* Did we create a new file? */
     ss_table_t          *scopetab=NULL;
     ss_table_t          *filetab=NULL;
     ss_file_t           *retval=NULL;
     ss_fileobj_t        *fileobj;
     ss_gfile_t          orig_gfile;             /* Original value of the gfile slot to restore during error recovery*/
     size_t              udata[2];               /* Info to pass through to ss_file_boot2_cb() */
     ss_prop_t           *scope_props=NULL;      /* Properties for opening the top scope */
     ss_file_t           *file_here=NULL;        /* File object to destroy on error */

     memset(&orig_gfile, 0, sizeof orig_gfile);

     if (file && name) {
         /* Bind the name to the file. In other words, the file's entry in the file table contains one name but the caller
          * wants to use some other name instead. Perhaps the original file has been moved. */
         if (ss_file_isopen(file, NULL)<0) SS_ERROR(FAILED);
         if (NULL==(fixedname=ss_file_fixname(name, NULL, NULL, 0, NULL))) SS_ERROR(FAILED);
         if (SS_NOSIZE!=(gfileidx=ss_gfile_find_name(fixedname))) {
             gfile = SS_GFILE_IDX(gfileidx);
             if (gfile->cur_open>0) {
                 /* The global file by this name is already open. We can just make the FILE point to this gfile entry as
                  * an implicitly opened file. */
                 SS_FILE(file)->m.gfileidx = gfileidx;
                 if (NULL==(retval = ss_pers_file((ss_pers_t*)(gfile->topscope), NULL))) SS_ERROR(FAILED);
                 goto done;
             }
         }
         SS_STATUS_OK; /*clean up ss_gfile_find_name() failure*/
         if (NULL==(retval=ss_file_open(NULL, name, flags, props))) SS_ERROR(FAILED);

     } else if (file) {
         /* Open file with the name specified in SS_FILE(file) */
         if (NULL==(name=ss_string_ptr(SS_FILE_P(file,name)))) SS_ERROR(FAILED);
         if (NULL==(retval=ss_file_open(file, name, flags, props))) SS_ERROR(FAILED);

     } else {
         /* Obtain the MPI communicator.  If the caller is passing in a communicator then we should dup it so operations
          * performed by SSlib on that communicator don't interfere with the caller. HDF5 will also always dup the communicator
          * when the file is opened/created. */
         if (props && ss_prop_get(props, "comm", H5T_NATIVE_MPI_COMM, &comm_temp)) {
             if (ss_mpi_comm_dup(comm_temp, &comm)<0) SS_ERROR_FMT(FAILED, ("file=\"%s\"", name));
             comm_duped = TRUE;
         } else {
             SS_STATUS_OK; /*for possible failed ss_prop_get()*/
             comm = sslib_g.comm;
         }
         if (!props || NULL==ss_prop_get(props, "info", H5T_NATIVE_MPI_INFO, &info)) {
             SS_STATUS_OK; /*we don't care if ss_prop_get() failed*/
             info = MPI_INFO_NULL;
         }

         /* We want to open a file with the specified name. First scan through the gfile array to see if there's already an
          * entry for this name. If so, the named file is either open already or the name is referenced from the /File/ table
          * of one or more open files. Create an entry if we don't find one existing. */
         if (NULL==(fixedname = ss_file_fixname(name, NULL, NULL, 0, NULL))) SS_ERROR_FMT(FAILED, ("file=\"%s\"", name));
         if (SS_NOSIZE==(gfileidx=ss_gfile_find_name(fixedname))) {
             SS_STATUS_OK;
             if (SS_NOSIZE==(gfileidx = ss_gfile_new(comm))) SS_ERROR_FMT(FAILED, ("file=\"%s\"", name));
             gfile = SS_GFILE_IDX(gfileidx);
             orig_gfile = *gfile;
             if (NULL==(gfile->name=malloc(strlen(fixedname)+1))) SS_ERROR_FMT(RESOURCE, ("file=\"%s\"", name));
             strcpy(gfile->name, fixedname);
             if (NULL==(gfile->dirname=malloc(strlen(fixedname)+1))) SS_ERROR_FMT(RESOURCE, ("file=\"%s\"", name));
             strcpy(gfile->dirname, gfile->name);

 #ifdef WIN32
             {
                 char *fwd_slash = strrchr(gfile->dirname, '/');
                 char *back_slash = strrchr(gfile->dirname, '\\');
                 SS_ASSERT(fwd_slash || back_slash);
                 if(fwd_slash && !back_slash) *fwd_slash='\0';
                 else if(!fwd_slash && back_slash) *back_slash='\0';
                 else if(fwd_slash > back_slash) *fwd_slash='\0';
                 else *back_slash='\0';
             }
 #else
             SS_ASSERT(strrchr(gfile->dirname, '/'));
             *(strrchr(gfile->dirname, '/')) = '\0';
 #endif

         } else {
             gfile = SS_GFILE_IDX(gfileidx);
             orig_gfile = *gfile;
         }

         /* If the gfile entry is an open file then we have an error. It should not be possible to open the same file more than
          * once concurrently. Increment the file-open counter early because some functions below (e.g., ss_pers_deref()) will
          * need to see that the file is at least in the process of being opened. */
         if (gfile->cur_open>0) SS_ERROR_FMT(PERM, ("file already open: %s", fixedname));
         gfile->cur_open++;
         gfile->open_serial++;

         /* Create file access and creation properties or use the ones supplied in PROPS. If we create them here we'll have to
          * close them below. */
         if (!props || NULL==ss_prop_get(props, "fapl", H5T_NATIVE_HID, &fapl) || fapl<=0) {
             SS_STATUS_OK; /*possible failed ss_prop_get()*/
             if ((fapl=fapl_created=H5Pcreate(H5P_FILE_ACCESS))<0) SS_ERROR_FMT(HDF5, ("file=\"%s\"", name));
         }
         if (!props || NULL==ss_prop_get(props, "fcpl", H5T_NATIVE_HID, &fcpl) || fcpl<=0) {
             SS_STATUS_OK; /*possible failed ss_prop_get()*/
             if ((fcpl=fcpl_created=H5Pcreate(H5P_FILE_CREATE))<0) SS_ERROR_FMT(HDF5, ("file=\"%s\"", name));
         }

         /* Create data transfer properties -- we modify them below if necessary */
         if (gfile->dxpl_independent<=0) gfile->dxpl_independent = H5Pcreate(H5P_DATASET_XFER);
         if (gfile->dxpl_collective<=0) gfile->dxpl_collective = H5Pcreate(H5P_DATASET_XFER);
         gfile->flags = flags;

         /* If neither fcpl nor fapl were supplied then set various things now in the ones we created. */
         if (fapl_created>=0 && fcpl_created>=0) {
 #if H5_VERS_NUMBER>=1005000
             /* The following property causes HDF5 to close all objects related to the file when the file is closed. This is
              * important for the MPI file driver to prevent HDF5 from making MPI calls after MPI_Finalize(), but it doesn't
              * hurt to do the same for the other drivers. In fact, doing this for the serial driver will help us detect errors
              * in SSlib if it tries to use an object after the object's file has been closed because HDF5 will report an invalid
              * object ID. */
             H5Pset_fclose_degree(fapl, H5F_CLOSE_STRONG);
 #endif
 #ifdef HAVE_PARALLEL
             if (1==ss_mpi_comm_size(comm)) {
                 /* Set the VFD to sec2 */
                 if (H5Pset_fapl_sec2(fapl)<0) SS_ERROR_FMT(HDF5, ("file=\"%s\"", name));
             } else {
                 /* Set the VFD to mpio. If we choose to use the mpiposix driver instead we'll reset it below. */
                 if (H5Pset_fapl_mpio(fapl, comm, MPI_INFO_NULL)<0) SS_ERROR_FMT(HDF5, ("file=\"%s\"", name));
                 if (H5Pset_dxpl_mpio(gfile->dxpl_independent, H5FD_MPIO_INDEPENDENT)<0) SS_ERROR_FMT(HDF5, ("file=\"%s\"", name));
                 if (H5Pset_dxpl_mpio(gfile->dxpl_collective, H5FD_MPIO_COLLECTIVE)<0) SS_ERROR_FMT(HDF5, ("file=\"%s\"", name));

                 /* GPFS on LLNL's SP systems grants all file tokens to the first process that writes at file offset zero,
                  * resulting in extra overhead when other processes need to write to parts of the file. Therefore, we tell HDF5
                  * to avoid ever writing into the first GPFS block of the file. Also, by using HDF5's MPI/POSIX driver we can
                  * bypass MPI-IO, but be warned that this is only expected to work if all tasks are on a single node or the
                  * files are on a parallel file system. */
 #if 0
                 if (!props || ss_prop_get_i(props, "use_mpiio")<=0) {
                     SS_STATUS_OK; /*possible failed ss_prop_get_i() has same meaning as use_mpiio==false*/
                     if (H5Pset_fapl_mpiposix(fapl, comm, TRUE)<0) SS_ERROR_FMT(HDF5, ("file=\"%s\"", name));
                     if (H5Pset_userblock(fcpl, (hsize_t)512*1024)<0) SS_ERROR_FMT(HDF5, ("file=\"%s\"", name));
                     if (H5Pset_alignment(fapl, (hsize_t)64*1024, (hsize_t)512*1024)<0) SS_ERROR_FMT(HDF5, ("file=\"%s\"", name));
                 }
 #endif
             }
 #endif /*HAVE_PARALLEL*/
         }

         /* Create or open the file. Use the supplied name just in case ss_file_fixname() has some problem. This would be much
          * easier if H5Fopen() worked more like unix open(), being able to create new files, truncate existing files, etc. */
         if (flags & H5F_ACC_TRANSIENT) {
             gfile->fid = 1; /* non-negative indicates no error, but not a valid hdf5 file handle*/
         } else if (flags & (H5F_ACC_TRUNC | H5F_ACC_EXCL)) {
             unsigned tempflags = flags & ~(H5F_ACC_RDWR|H5F_ACC_CREAT); /* H5Fcreate() is picky about these: they must be off */
             if ((gfile->fid=H5Fcreate(name, tempflags, fcpl, fapl))<0)
                 SS_ERROR_FMT(HDF5, ("unable to create file: %s", fixedname));
         } else {
             if ((gfile->fid=H5Fopen(name, flags, fapl))<0) SS_ERROR_FMT(HDF5, ("unable to open file: %s", fixedname));
         }

         /* Create or read the basic file structure. */
         if ((creating=ss_scope_boot_top(gfileidx, comm, comm_duped))<0) SS_ERROR_FMT(FAILED, ("file=\"%s\"", name));
         gfile = SS_GFILE_IDX(gfileidx); /*might have been clobbered by previous call*/

         /* Create or read the basic blob storage information */
         if (NULL==(gfile->gblob = ss_blob_boot(gfile->topscope, (hbool_t)creating))) SS_ERROR_FMT(FAILED, ("file=\"%s\"", name));

         /* If we're creating the file then the first entry of the top-scope's File table must be the file itself. This is an
          * appropriate place to call this function because the communicator for the top scope is the same as for the file,
          * which is the communicator over which ss_file_open() was called. */
         if (NULL==(filetab=ss_scope_table(gfile->topscope, SS_MAGIC(ss_file_t), NULL))) SS_ERROR_FMT(FAILED, ("file=\"%s\"", name));
         if (creating) {
             if (NULL==(file = file_here = SS_PERS_NEW(gfile->topscope, ss_file_t, SS_ALLSAME)))
                 SS_ERROR_FMT(FAILED, ("file=\"%s\"", name));
             if (ss_string_set(SS_FILE_P(file,name), name)<0) SS_ERROR_FMT(FAILED, ("file=\"%s\"", name));
         } else {
             if (ss_table_read(filetab, gfile->topscope)<0) SS_ERROR_FMT(FAILED, ("file=\"%s\"", name));
             if (NULL==(fileobj=(ss_fileobj_t*)ss_table_lookup(filetab, 0, SS_STRICT)))
                 SS_ERROR_FMT(CORRUPT, ("top scope's file table is empty"));
             if (NULL==(file=file_here=(ss_file_t*)ss_pers_refer(gfile->topscope, (ss_persobj_t*)fileobj, NULL)))
                 SS_ERROR_FMT(FAILED, ("file=\"%s\"", name));
         }

         /* Process all entries of all File tables by adding records to GFile array. Table slot zero is already in
          * the GFile array at location gfileidx. */
         udata[0] = gfileidx;
         udata[1] = comm;
         if (NULL==(scopetab=ss_scope_table(gfile->topscope, SS_MAGIC(ss_scope_t), NULL)))
             SS_ERROR_FMT(FAILED, ("file=\"%s\"", name));
         if (ss_table_scan(scopetab, gfile->topscope, 0, ss_file_boot1_cb, udata)<0) SS_ERROR_FMT(FAILED, ("file=\"%s\"", name));
         gfile = SS_GFILE_IDX(gfileidx); /*may have been clobbered by previous call*/

         /* Open this scope */
         if (NULL==(scope_props=ss_prop_new("scope open props"))) SS_ERROR_FMT(FAILED, ("file=\"%s\"", name));
         if (ss_prop_add(scope_props, "comm", H5T_NATIVE_MPI_COMM, &comm)<0) SS_ERROR_FMT(FAILED, ("file=\"%s\"", name));
         if (ss_prop_add(scope_props, "duped", H5T_NATIVE_HBOOL, &comm_duped)<0) SS_ERROR_FMT(FAILED, ("file=\"%s\"", name));
         if (ss_scope_open(gfile->topscope, gfile->flags, scope_props)<0) SS_ERROR_FMT(FAILED, ("file=\"%s\"", name));
         if (ss_prop_dest(scope_props)<0) SS_ERROR_FMT(FAILED, ("file=\"%s\"", name));
         scope_props = NULL;

         /* Mark file as explicitly opened (gfile.cur_open was incremented above) */
         SS_FILE(file)->m.explicit_open = TRUE;
         retval = file;
         file = NULL;
     }

 done:
     /* successful cleanup */
     SS_FREE(fixedname);
     if (fapl_created>0) H5Pclose(fapl_created);
     if (fcpl_created>0) H5Pclose(fcpl_created);

 SS_CLEANUP:
     SS_FREE(fixedname);

     /* Destroy the `file' return value */
     if (file_here) {
         ss_pers_dest((ss_pers_t*)file_here);
         file_here = SS_FREE(file_here);
     }

     /* Restore the `gfile' entry to the original value */
     if (gfile) {
         if (gfile->name!=orig_gfile.name) SS_FREE(gfile->name);
         if (gfile->fid>0 && gfile->fid!=orig_gfile.fid) H5Fclose(gfile->fid);
         if (gfile->topscope && gfile->topscope!=orig_gfile.topscope) ss_scope_dest(gfile->topscope);
         SS_ASSERT(NULL==gfile->gblob || NULL==orig_gfile.gblob || gfile->gblob==orig_gfile.gblob);
         if (gfile->dxpl_independent>0 && gfile->dxpl_independent!=orig_gfile.dxpl_independent) H5Pclose(gfile->dxpl_independent);
         if (gfile->dxpl_collective>0 && gfile->dxpl_collective!=orig_gfile.dxpl_collective) H5Pclose(gfile->dxpl_collective);
         SS_ASSERT(NULL==gfile->reg || gfile->reg==orig_gfile.reg);
         *gfile = orig_gfile;
     }

     if (scope_props) ss_prop_dest(scope_props);
     if (fapl_created>0) H5Pclose(fapl_created);
     if (fcpl_created>0) H5Pclose(fcpl_created);
     if (comm_duped) ss_mpi_comm_free(&comm);

     SS_LEAVE(retval);
 }