Debian Codegraph @

packages/seacas/libraries/exodus/src/ex_open_par.c

raw
1 /*
2 * Copyright(C) 1999-2024 National Technology & Engineering Solutions
3 * of Sandia, LLC (NTESS). Under the terms of Contract DE-NA0003525 with
4 * NTESS, the U.S. Government retains certain rights in this software.
5 *
6 * See packages/seacas/LICENSE for details
7 */
8 /*****************************************************************************
9 *
10 * exopen - ex_open
11 *
12 * entry conditions -
13 * input parameters:
14 * char* path exodus filename path
15 * int mode access mode w/r
16 *
17 * exit conditions -
18 * int exoid exodus file id
19 * int* comp_ws computer word size
20 * int* io_ws storage word size
21 * float* version EXODUS interface version number
22 *
23 * revision history -
24 *
25 *
26 *****************************************************************************/
27 /* Determine whether compiling against a parallel netcdf... */
28 #include "exodusII.h"
29 #if defined(PARALLEL_AWARE_EXODUS)
30
31 #include "exodusII.h"
32 #include "exodusII_int.h"
33 #include <mpi.h>
34 #include <stdlib.h>
35 /*!
36 \ingroup Utilities
37
38 \note The ex_open_par_int() is an internal function called by
39 ex_open_par(). The user should call ex_open_par() and not ex_open_par_int().
40
41 The function ex_open_par() opens an existing exodus file and returns
42 an ID that can subsequently be used to refer to the file, the word
43 size of the floating point values stored in the file, and the version
44 of the exodus database (returned as a ``float'', regardless of the
45 compute or I/O word size). Multiple files may be ``open'' simultaneously.
46
47 \return In case of an error, ex_open_par() returns a negative
48 number. Possible causes of errors include:
49 - The specified file does not exist.
50 - The mode specified is something other than the predefined constant
51 \fparam{EX_READ} or \fparam{EX_WRITE}.
52 - Database version is earlier than 2.0.
53
54 \param path The file name of the exodus file. This can be given as either an
55 absolute path name (from the root of the file system) or a relative
56 path name (from the current directory).
57
58 \param mode Access mode. Use one of the following predefined constants:
59 - \fparam{EX_READ} To open the file just for reading.
60 - \fparam{EX_WRITE} To open the file for writing and reading.
61
62 \param[in,out] comp_ws The word size in bytes (0, 4 or 8) of the floating point
63 variables
64 used in the application program. If 0 (zero) is passed, the
65 default
66 size of floating point values for the machine will be used and
67 returned in this variable. WARNING: all exodus functions
68 requiring
69 reals must be passed reals declared with this passed in or
70 returned
71 compute word size (4 or 8).
72
73 \param[in,out] io_ws The word size in bytes (0, 4 or 8) of the floating
74 point data as they are stored in the exodus file. If the
75 word
76 size does not match the word size of data stored in the
77 file,
78 a fatal error is returned. If this argument is 0, the word
79 size
80 of the floating point data already stored in the file is
81 returned.
82
83 \param[out] version Returned exodus database version number.
84
85 The following opens an exodus file named \file{test.exo} for parallel
86 read only, using default settings for compute and I/O word sizes:
87
88 ~~~{.c}
89 int CPU_word_size,IO_word_size, exoid;
90 float version;
91
92 CPU_word_size = sizeof(float); \co{float or double}
93 IO_word_size = 0; \co{use what is stored in file}
94
95 \comment{open exodus files}
96 exoid = ex_open_par ("test.exo", \co{filename path}
97 EX_READ, \co{access mode = READ}
98 &CPU_word_size, \co{CPU word size}
99 &IO_word_size, \co{IO word size}
100 &version, \co{ExodusII library version
101 MPI_COMM_WORLD, // CHECK: ALLOW MPI_COMM_WORLD
102 MPI_INFO_NULL);}
103 ~~~
104 */
105
106 struct ncvar /* variable */
107 {
108 char name[MAX_VAR_NAME_LENGTH];
109 nc_type type;
110 int ndims;
111 int dims[NC_MAX_VAR_DIMS];
112 int natts;
113 };
114
115 /* NOTE: Do *not* call `ex_open_par_int()` directly. The public API
116 * function name is `ex_open_par()` which is a wrapper that
117 * calls `ex_open_par_int` with an additional argument to make
118 * sure library and include file are consistent
119 */
120 int ex_open_par_int(const char *path, int mode, int *comp_ws, int *io_ws, float *version,
121 MPI_Comm comm, MPI_Info info, int run_version)
122 {
123 int exoid = -1;
124 int status = 0;
125 int nc_mode = 0;
126 int old_fill = 0;
127 int file_wordsize = 0;
128 int dim_str_name = 0;
129 int int64_status = 0;
130 bool is_hdf5 = false;
131 bool is_pnetcdf = false;
132 bool in_redef = false;
133
134 char errmsg[MAX_ERR_LENGTH];
135
136 EX_FUNC_ENTER();
137
138 /* set error handling mode to no messages, non-fatal errors */
139 ex_opts(exoptval); /* call required to set ncopts first time through */
140
141 exi_check_version(run_version);
142
143 if ((mode & EX_READ) && (mode & EX_WRITE)) {
144 snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: Cannot specify both EX_READ and EX_WRITE");
145 ex_err(__func__, errmsg, EX_BADFILEMODE);
146 EX_FUNC_LEAVE(EX_FATAL);
147 }
148
149 if (!path || strlen(path) == 0) {
150 snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: Filename is not specified.");
151 ex_err(__func__, errmsg, EX_BADFILEMODE);
152 EX_FUNC_LEAVE(EX_FATAL);
153 }
154
155 char *canon_path = exi_canonicalize_filename(path);
156
157 /* Verify that this file is not already open for read or write...
158 In theory, should be ok for the file to be open multiple times
159 for read, but bad things can happen if being read and written
160 at the same time...
161 */
162 if (exi_check_multiple_open(canon_path, mode, __func__)) {
163 free(canon_path);
164 EX_FUNC_LEAVE(EX_FATAL);
165 }
166
167 if (mode & EX_WRITE) {
168 nc_mode = (NC_WRITE | NC_MPIIO);
169 #if NC_HAS_HDF5
170 if (mode & EX_NETCDF4) {
171 nc_mode |= NC_NETCDF4;
172 }
173 #endif
174 #if NC_HAS_CDF5
175 if (mode & EX_64BIT_DATA) {
176 nc_mode |= NC_64BIT_DATA;
177 }
178 #endif
179 }
180 else {
181 nc_mode = (NC_NOWRITE | NC_SHARE | NC_MPIIO);
182 }
183 /* There is an issue on some versions of mpi that limit the length of the path to <250 characters
184 * Check for that here and use `path` if `canon_path` is >=250 characters...
185 */
186 if (strlen(canon_path) >= 250) {
187 status = nc_open_par(path, nc_mode, comm, info, &exoid);
188 }
189 else {
190 status = nc_open_par(canon_path, nc_mode, comm, info, &exoid);
191 }
192 if (status != NC_NOERR) {
193 /* It is possible that the user is trying to open a netcdf4
194 file, but the netcdf4 capabilities aren't available in the
195 netcdf linked to this library. Note that we can't just use a
196 compile-time define since we could be using a shareable
197 netcdf library, so the netcdf4 capabilities aren't known
198 until runtime...
199
200 Later versions of netcdf-4.X have a function that can be
201 queried to determine whether the library being used was
202 compiled with --enable-netcdf4, but not everyone is using that
203 version yet, so we may have to do some guessing...
204
205 At this time, query the beginning of the file and see if it
206 is an HDF-5 file and if it is assume that the open failure
207 is due to the netcdf library not enabling netcdf4 features unless
208 we have the define that shows it is enabled, then assume other error...
209 */
210 int type = 0;
211 exi_check_file_type(path, &type);
212
213 if (type == 0) {
214 /* Error message printed at lower level */
215 }
216 else if (type == 5) {
217 #if NC_HAS_HDF5
218 snprintf(errmsg, MAX_ERR_LENGTH,
219 "EXODUS: ERROR: Attempting to open the netcdf-4 "
220 "file:\n\t'%s'\n\tfailed. The netcdf library supports "
221 "netcdf-4 so there must be a filesystem or some other "
222 "issue \n",
223 canon_path);
224 ex_err(__func__, errmsg, status);
225 free(canon_path);
226 EX_FUNC_LEAVE(EX_FATAL);
227 #else
228 /* This is an hdf5 (netcdf4) file. If NC_HAS_HDF5 is not defined,
229 then we either don't have hdf5 support in this netcdf version,
230 OR this is an older netcdf version that doesn't provide that define.
231
232 In either case, we don't have enough information, so we
233 assume that the netcdf doesn't have netcdf4 capabilities
234 enabled. Tell the user...
235 */
236 snprintf(errmsg, MAX_ERR_LENGTH,
237 "EXODUS: ERROR: Attempting to open the netcdf-4 "
238 "file:\n\t'%s'\n\tEither the netcdf library does not "
239 "support netcdf-4 or there is a filesystem or some "
240 "other issue \n",
241 canon_path);
242 ex_err(__func__, errmsg, status);
243 free(canon_path);
244 EX_FUNC_LEAVE(EX_FATAL);
245 #endif
246 }
247 else if (type == 4) {
248 #if NC_HAS_CDF5
249 snprintf(errmsg, MAX_ERR_LENGTH,
250 "EXODUS: ERROR: Attempting to open the CDF5 "
251 "file:\n\t'%s'\n\tfailed. The netcdf library supports "
252 "CDF5-type files so there must be a filesystem or some other "
253 "issue \n",
254 canon_path);
255 ex_err(__func__, errmsg, status);
256 free(canon_path);
257 EX_FUNC_LEAVE(EX_FATAL);
258 #else
259 /* This is an cdf5 (64BIT_DATA) file. If NC_64BIT_DATA is not defined,
260 then we either don't have cdf5 support in this netcdf version,
261 OR this is an older netcdf version that doesn't provide that define.
262
263 In either case, we don't have enough information, so we
264 assume that the netcdf doesn't have cdf5 capabilities
265 enabled. Tell the user...
266 */
267 snprintf(errmsg, MAX_ERR_LENGTH,
268 "EXODUS: ERROR: Attempting to open the CDF5 "
269 "file:\n\t'%s'\n\tEither the netcdf library does not "
270 "support CDF5 or there is a filesystem or some "
271 "other issue \n",
272 canon_path);
273 ex_err(__func__, errmsg, status);
274 free(canon_path);
275 EX_FUNC_LEAVE(EX_FATAL);
276 #endif
277 }
278 else if (type == 1 || type == 2) {
279 #if NC_HAS_PNETCDF
280 snprintf(errmsg, MAX_ERR_LENGTH,
281 "EXODUS: ERROR: Attempting to open the classic NetCDF "
282 "file:\n\t'%s'\n\tfailed. The netcdf library supports "
283 "PNetCDF files as required for parallel reading of this "
284 "file type, so there must be a filesystem or some other "
285 "issue \n",
286 canon_path);
287 ex_err(__func__, errmsg, status);
288 free(canon_path);
289 EX_FUNC_LEAVE(EX_FATAL);
290 #else
291 /* This is an normal NetCDF format file, for parallel reading, the PNetCDF
292 library is required but that is not compiled into this version.
293 */
294 snprintf(errmsg, MAX_ERR_LENGTH,
295 "EXODUS: ERROR: Attempting to open the NetCDF "
296 "file:\n\t'%s'\n\tThe NetCDF library was not "
297 "built with PNetCDF support as required for parallel access to this file.\n",
298 canon_path);
299 ex_err(__func__, errmsg, status);
300 free(canon_path);
301 EX_FUNC_LEAVE(EX_FATAL);
302 #endif
303 }
304
305 snprintf(errmsg, MAX_ERR_LENGTH,
306 "ERROR: failed to open %s of type %d for reading.\n\t\tThe "
307 "file does not exist, or there is a permission or file "
308 "format issue.",
309 canon_path, type);
310 ex_err(__func__, errmsg, status);
311 free(canon_path);
312 EX_FUNC_LEAVE(EX_FATAL);
313 }
314
315 /* File opened correctly */
316 int type = 0;
317 exi_check_file_type(canon_path, &type);
318 if (type == 5) {
319 is_hdf5 = true;
320 }
321 else if (type == 1 || type == 2 || type == 4) {
322 is_pnetcdf = true;
323 }
324
325 if (mode & EX_WRITE) { /* Appending */
326 /* turn off automatic filling of netCDF variables */
327 if (is_pnetcdf) {
328 if ((status = exi_redef(exoid, __func__)) != NC_NOERR) {
329 snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: failed to put file id %d into define mode", exoid);
330 ex_err_fn(exoid, __func__, errmsg, status);
331 free(canon_path);
332 EX_FUNC_LEAVE(EX_FATAL);
333 }
334 in_redef = true;
335 }
336
337 if ((status = nc_set_fill(exoid, NC_NOFILL, &old_fill)) != NC_NOERR) {
338 snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: failed to set nofill mode in file id %d", exoid);
339 ex_err_fn(exoid, __func__, errmsg, status);
340 free(canon_path);
341 EX_FUNC_LEAVE(EX_FATAL);
342 }
343
344 nc_type att_type = NC_NAT;
345 size_t att_len = 0;
346 int stat_att = nc_inq_att(exoid, NC_GLOBAL, ATT_MAX_NAME_LENGTH, &att_type, &att_len);
347 int stat_dim = nc_inq_dimid(exoid, DIM_STR_NAME, &dim_str_name);
348 if (stat_att != NC_NOERR || stat_dim != NC_NOERR) {
349 if (!in_redef) {
350 if ((status = nc_redef(exoid)) != NC_NOERR) {
351 snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: failed to put file id %d into define mode",
352 exoid);
353 ex_err_fn(exoid, __func__, errmsg, status);
354 free(canon_path);
355 EX_FUNC_LEAVE(EX_FATAL);
356 }
357 in_redef = true;
358 }
359 if (stat_att != NC_NOERR) {
360 int max_so_far = 32;
361 nc_put_att_int(exoid, NC_GLOBAL, ATT_MAX_NAME_LENGTH, NC_INT, 1, &max_so_far);
362 }
363
364 /* If the DIM_STR_NAME variable does not exist on the database, we need to
365 * add it now. */
366 if (stat_dim != NC_NOERR) {
367 /* Not found; set to default value of 32+1. */
368 int max_name = exi_default_max_name_length < 32 ? 32 : exi_default_max_name_length;
369 nc_def_dim(exoid, DIM_STR_NAME, max_name + 1, &dim_str_name);
370 }
371 }
372
373 if (in_redef) {
374 if ((status = nc_enddef(exoid)) != NC_NOERR) {
375 free(canon_path);
376 EX_FUNC_LEAVE(EX_FATAL);
377 }
378 }
379 } /* End of (mode & EX_WRITE) */
380
381 /* If this is a `pnetcdf` file (non HDF5), then we can't set the
382 * collective vs independent setting on a per-variable basis -- it
383 * is set for the entire file. Several apps rely on being able to
384 * access some set or other data in an independent mode, so we can't
385 * set any vars to collective or it sets the file to collective and
386 * we potentially hang...
387 */
388 if (!is_pnetcdf) {
389
390 /* If this is a parallel execution and we are appending, then we
391 * need to set the parallel access method for all transient variables to NC_COLLECTIVE since
392 * they will be being extended.
393 */
394 int ndims; /* number of dimensions */
395 int nvars; /* number of variables */
396 int ngatts; /* number of global attributes */
397 int recdimid; /* id of unlimited dimension */
398
399 /* Determine number of variables on the database... */
400 nc_inq(exoid, &ndims, &nvars, &ngatts, &recdimid);
401
402 for (int varid = 0; varid < nvars; varid++) {
403 struct ncvar var;
404 nc_inq_var(exoid, varid, var.name, &var.type, &var.ndims, var.dims, &var.natts);
405
406 if (((strncmp(var.name, "vals_", 5) == 0) && (strncmp(var.name, "vals_red_", 9) != 0)) ||
407 (strcmp(var.name, VAR_WHOLE_TIME) == 0) || (strncmp(var.name, "coord", 5) == 0) ||
408 (strcmp(var.name, "connect") == 0) || (strcmp(var.name, "edgconn") == 0) ||
409 (strcmp(var.name, "ebconn") == 0) || (strcmp(var.name, "facconn") == 0) ||
410 (strcmp(var.name, "fbconn") == 0) || (strcmp(var.name, "attrib") == 0)) {
411 nc_var_par_access(exoid, varid, NC_COLLECTIVE);
412 }
413 }
414 }
415
416 /* determine version of EXODUS file, and the word size of
417 * floating point and integer values stored in the file
418 */
419
420 if ((status = nc_get_att_float(exoid, NC_GLOBAL, ATT_VERSION, version)) != NC_NOERR) {
421 snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: failed to get database version for file id: %d",
422 exoid);
423 ex_err_fn(exoid, __func__, errmsg, status);
424 free(canon_path);
425 EX_FUNC_LEAVE(EX_FATAL);
426 }
427
428 /* check ExodusII file version - old version 1.x files are not supported */
429 if (*version < 2.0) {
430 snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: Unsupported file version %.2f in file id: %d",
431 *version, exoid);
432 ex_err_fn(exoid, __func__, errmsg, EX_BADPARAM);
433 free(canon_path);
434 EX_FUNC_LEAVE(EX_FATAL);
435 }
436
437 if (nc_get_att_int(exoid, NC_GLOBAL, ATT_FLT_WORDSIZE, &file_wordsize) !=
438 NC_NOERR) { /* try old (prior to db version 2.02) attribute name */
439 if ((status = nc_get_att_int(exoid, NC_GLOBAL, ATT_FLT_WORDSIZE_BLANK, &file_wordsize)) !=
440 NC_NOERR) {
441 snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: failed to get file wordsize from file id: %d",
442 exoid);
443 ex_err_fn(exoid, __func__, errmsg, status);
444 free(canon_path);
445 EX_FUNC_LEAVE(EX_FATAL);
446 }
447 }
448
449 /* See if int64 status attribute exists and if so, what data is stored as
450 * int64
451 * Older files don't have the attribute, so it is not an error if it is
452 * missing
453 */
454 if (nc_get_att_int(exoid, NC_GLOBAL, ATT_INT64_STATUS, &int64_status) != NC_NOERR) {
455 int64_status = 0; /* Just in case it gets munged by a failed get_att_int call */
456 }
457
458 /* Merge in API int64 status flags as specified by caller of function... */
459 int64_status |= (mode & EX_ALL_INT64_API);
460
461 /* Verify that there is not an existing file_item struct for this
462 exoid This could happen (and has) when application calls
463 ex_open(), but then closes file using nc_close() and then reopens
464 file. NetCDF will possibly reuse the exoid which results in
465 internal corruption in exodus data structures since exodus does
466 not know that file was closed and possibly new file opened for
467 this exoid
468 */
469 if (exi_find_file_item(exoid) != NULL) {
470 snprintf(errmsg, MAX_ERR_LENGTH,
471 "ERROR: There is an existing file already using the file "
472 "id %d which was also assigned to file %s.\n\tWas "
473 "nc_close() called instead of ex_close() on an open Exodus "
474 "file?\n",
475 exoid, canon_path);
476 ex_err_fn(exoid, __func__, errmsg, EX_BADFILEID);
477 nc_close(exoid);
478 free(canon_path);
479 EX_FUNC_LEAVE(EX_FATAL);
480 }
481
482 /* initialize floating point and integer size conversion. */
483 if (exi_conv_init(exoid, comp_ws, io_ws, file_wordsize, int64_status, 1, is_hdf5, is_pnetcdf,
484 mode & EX_WRITE) != EX_NOERR) {
485 snprintf(errmsg, MAX_ERR_LENGTH,
486 "ERROR: failed to initialize conversion routines in file id %d", exoid);
487 ex_err_fn(exoid, __func__, errmsg, EX_LASTERR);
488 free(canon_path);
489 EX_FUNC_LEAVE(EX_FATAL);
490 }
491
492 free(canon_path);
493 EX_FUNC_LEAVE(exoid);
494 }
495 #else
496 /*
497 * Prevent warning in some versions of ranlib(1) because the object
498 * file has no symbols.
499 */
500 extern const char exodus_unused_symbol_dummy_ex_open_par;
501 #endif