Debian Codegraph @

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

raw
1 /*
2 * Copyright(C) 1999-2021, 2023, 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 \ingroup Utilities
11
12 \note The ex_create_par_int() is an internal function called by
13 ex_create_par(). The user should call ex_create_par() and not ex_create_par_int().
14
15 The function ex_create() creates a new exodus file and returns an ID
16 that can subsequently be used to refer to the file.
17
18 All floating point values in an exodus file are stored as either
19 4-byte (float) or 8-byte (double) numbers; no mixing of 4- and
20 8-byte numbers in a single file is allowed. An application code can
21 compute either 4- or 8-byte values and can designate that the values
22 be stored in the exodus file as either 4- or 8-byte numbers;
23 conversion between the 4- and 8-byte values is performed automatically
24 by the API routines. Thus, there are four possible combinations of
25 compute word size and storage (or I/O) word size.
26
27 \return In case of an error, ex_create() returns a negative number. Possible
28 causes of errors include:
29 - Passing a file name that includes a directory that does not
30 exist.
31 - Specifying a file name of a file that exists and also
32 specifying a no clobber option.
33 - Attempting to create a file in a directory without permission
34 to create files there.
35 - Passing an invalid file clobber mode.
36
37 \param path The file name of the new exodus file. This can be given as either an
38 absolute path name (from the root of the file system) or a relative
39 path name (from the current directory).
40
41 \param cmode Mode. Use one of the following predefined constants:
42 \arg #EX_NOCLOBBER To create the new file only if the given file name does
43 not refer to a
44 file that already exists.
45
46 \arg #EX_CLOBBER To create the new file, regardless of whether a file with
47 the same
48 name already exists. If a file with the same name does
49 exist, its
50 contents will be erased.
51
52 \arg #EX_64BIT_OFFSET To create a model that can store individual datasets
53 larger than
54 2 gigabytes. This modifies the internal storage used by
55 exodusII and
56 also puts the underlying NetCDF file into the \e 64-bit
57 offset'
58 mode. See largemodel for more details on this
59 mode. A large model file will also be created if the
60 environment variable EXODUS_LARGE_MODEL is defined
61 in the users environment. A message will be printed to
62 standard output
63 if this environment variable is found. #EX_LARGE_MODEL is
64 alias.
65
66 \arg #EX_NORMAL_MODEL Create a standard model.
67
68 \arg #EX_64BIT_DATA To create a model using the CDF5 format which uses the
69 classic model but has 64-bit dimensions and sizes.
70 This type will also be created if the
71 environment variable EXODUS_NETCDF5 is defined in the
72 users environment. A message will be printed to standard
73 output if
74 this environment variable is found.
75
76 \arg #EX_NETCDF4 To create a model using the HDF5-based NetCDF-4
77 output. An HDF5-based NetCDF-4 file will also be created
78 if the
79 environment variable EXODUS_NETCDF4 is defined in the
80 users environment. A message will be printed to standard
81 output if
82 this environment variable is found.
83
84 \arg #EX_NOSHARE Do not open the underlying NetCDF file in \e share
85 mode. See the
86 NetCDF documentation for more details.
87
88 \arg #EX_SHARE Do open the underlying NetCDF file in \e share mode. See
89 the NetCDF
90 documentation for more details.
91
92 \param[in,out] comp_ws The word size in bytes (0, 4 or 8) of the floating point
93 variables
94 used in the application program. If 0 (zero) is passed,
95 the default
96 sizeof(float) will be used and returned in this
97 variable. WARNING: all
98 exodus functions requiring floats must be passed floats
99 declared with
100 this passed in or returned compute word size (4 or 8).}
101
102 \param io_ws The word size in bytes (4 or 8) of the floating point
103 data as they are to be stored in the exodus file.
104
105 \param run_version (internally generated) used to verify compatibility of library
106 and include files.
107
108 The following code segment creates an exodus file called \file{test.exo}:
109
110 ~~~{.c}
111 #include "exodusII.h"
112 int CPU_word_size, IO_word_size, exoid;
113 CPU_word_size = sizeof(float); \comment{use float or double}
114 IO_word_size = 8; \comment{store variables as doubles}
115
116 \comment{create exodus file}
117 exoid = ex_create ("test.exo" \comment{filename path}
118 EX_CLOBBER, \comment{create mode}
119 &CPU_word_size, \comment{CPU float word size in bytes}
120 &IO_word_size); \comment{I/O float word size in bytes}
121 ~~~
122
123 */
124
125 /* Determine whether compiling against a parallel netcdf... */
126 #include "exodusII.h"
127 #if defined(PARALLEL_AWARE_EXODUS)
128
129 #include "exodusII_int.h"
130 #include <mpi.h>
131 #include <stdlib.h>
132
133 /* NOTE: Do *not* call `ex_create_par_int()` directly. The public API
134 * function name is `ex_create_par()` which is a wrapper that calls
135 * `ex_create_par_int` with an additional argument to make sure
136 * library and include file are consistent
137 */
138 int ex_create_par_int(const char *path, int cmode, int *comp_ws, int *io_ws, MPI_Comm comm,
139 MPI_Info info, int run_version)
140 {
141 int exoid = -1;
142 int status = 0;
143 char errmsg[MAX_ERR_LENGTH];
144 int nc_mode = 0;
145
146 unsigned int my_mode = cmode;
147 int is_parallel = 1;
148
149 EX_FUNC_ENTER();
150
151 #if !NC_HAS_PARALLEL
152 /* Library does NOT support parallel output via netcdf-4 or pnetcdf */
153 snprintf(errmsg, MAX_ERR_LENGTH,
154 "EXODUS: ERROR: Parallel output requires the netcdf-4 and/or "
155 "pnetcdf library format, but this netcdf library does not "
156 "support either.\n");
157 ex_err(__func__, errmsg, EX_BADPARAM);
158 EX_FUNC_LEAVE(EX_FATAL);
159 #endif
160
161 if (!path || strlen(path) == 0) {
162 snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: Filename is not specified.");
163 ex_err(__func__, errmsg, EX_BADFILEMODE);
164 EX_FUNC_LEAVE(EX_FATAL);
165 }
166
167 char *canon_path = exi_canonicalize_filename(path);
168
169 /* Verify that this file is not already open for read or write...
170 In theory, should be ok for the file to be open multiple times
171 for read, but bad things can happen if being read and written
172 at the same time...
173 */
174 if (exi_check_multiple_open(canon_path, EX_WRITE, __func__)) {
175 free(canon_path);
176 EX_FUNC_LEAVE(EX_FATAL);
177 }
178
179 nc_mode = exi_handle_mode(my_mode, is_parallel, run_version);
180
181 #if defined NC_NOATTCREORD
182 /* Disable attribute creation order tracking if available... */
183 if (my_mode & EX_NETCDF4) {
184 nc_mode |= NC_NOATTCREORD;
185 }
186 #endif
187
188 #if defined NC_NODIMSCALE_ATTACH
189 /* Disable attaching dimscales to variables (netcdf-c issue #2128) if available */
190 if (my_mode & EX_NETCDF4) {
191 nc_mode |= NC_NODIMSCALE_ATTACH;
192 }
193 #endif
194
195 #if NC_HAS_PNETCDF
196 bool i_created_info = false;
197 if (info == MPI_INFO_NULL) {
198 MPI_Info_create(&info);
199 i_created_info = true;
200 }
201 MPI_Info_set(info, "nc_header_align_size", "1048576");
202 #endif
203 /* There is an issue on some versions of mpi that limit the length of the path to <250 characters
204 * Check for that here and use `path` if `canon_path` is >=250 characters...
205 */
206
207 if (strlen(canon_path) >= 250) {
208 status = nc_create_par(path, nc_mode, comm, info, &exoid);
209 }
210 else {
211 status = nc_create_par(canon_path, nc_mode, comm, info, &exoid);
212 }
213
214 #if NC_HAS_PNETCDF
215 if (i_created_info) {
216 MPI_Info_free(&info);
217 }
218 #endif
219
220 if (status != NC_NOERR) {
221 if (my_mode & EX_NETCDF4) {
222 #if NC_HAS_PARALLEL4
223 snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: file create failed for %s.", canon_path);
224 #else
225 snprintf(errmsg, MAX_ERR_LENGTH,
226 "ERROR: file create failed for %s in NetCDF-4 "
227 "mode.\n\tThis library does not support parallel NetCDF-4 files (HDF5-based).",
228 canon_path);
229 #endif
230 }
231 else {
232 #if NC_HAS_PNETCDF
233 snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: file create failed for %s", canon_path);
234 #else
235 snprintf(errmsg, MAX_ERR_LENGTH,
236 "ERROR: file create failed for %s in PnetCDF "
237 "mode.\n\tThis library does not provide PnetCDF support.",
238 canon_path);
239 #endif
240 }
241 ex_err(__func__, errmsg, status);
242 free(canon_path);
243 EX_FUNC_LEAVE(EX_FATAL);
244 }
245
246 status = exi_populate_header(exoid, canon_path, my_mode, is_parallel, comp_ws, io_ws);
247 if (status != EX_NOERR) {
248 free(canon_path);
249 EX_FUNC_LEAVE(status);
250 }
251 free(canon_path);
252 EX_FUNC_LEAVE(exoid);
253 }
254 #else
255 /*
256 * Prevent warning in some versions of ranlib(1) because the object
257 * file has no symbols.
258 */
259 extern const char exodus_unused_symbol_dummy_ex_create_par;
260 #endif