Debian Codegraph @

packages/seacas/libraries/ioss/src/utest/Utst_blob_write_read.C

raw
1 // Copyright(C) 1999-2023 National Technology & Engineering Solutions
2 // of Sandia, LLC (NTESS). Under the terms of Contract DE-NA0003525 with
3 // NTESS, the U.S. Government retains certain rights in this software.
4 //
5 // See packages/seacas/LICENSE for details
6
7 #include "Ionit_Initializer.h"
8 #include "Ioss_Blob.h"
9 #include "Ioss_DatabaseIO.h"
10 #include "Ioss_IOFactory.h"
11 #include "Ioss_Property.h"
12 #include "Ioss_Region.h"
13 #include "Ioss_Sort.h"
14 #include "Ioss_Utils.h"
15 #include <cstdlib>
16 #include <iostream>
17 #include <stdint.h>
18 #include <string>
19 #include <vector>
20
21 #include "Ioss_CodeTypes.h"
22 #include "Ioss_DBUsage.h"
23 #include "Ioss_Field.h"
24 #include "Ioss_ParallelUtils.h"
25 #include "Ioss_PropertyManager.h"
26 #include "Ioss_ScopeGuard.h"
27 #include "Ioss_State.h"
28 #include "Ioss_VariableType.h"
29
30 //--------------------------------------------------------------------
31 /*----------------------------------------------------------------------
32 * IOSS Blob Example
33 *
34 */
35 void write_blob();
36 bool read_blob();
37
38 std::vector<double> generate_data(double time, size_t /* global_size */, double offset,
39 size_t local_size, size_t proc_offset)
40 {
41 // Determine this ranks portion of the data
42 std::vector<double> data;
43 data.reserve(local_size);
44 for (size_t i = 0; i < local_size; i++) {
45 size_t ii = proc_offset + i;
46 data.push_back(10.0 * ii + 100 * time + offset);
47 }
48 return data;
49 }
50
51 namespace {
52 std::pair<size_t, size_t> get_blob_size(size_t global_size, size_t parallel_size, size_t my_rank)
53 {
54 size_t per_proc = global_size / parallel_size;
55 size_t extra = global_size % parallel_size;
56 size_t count = per_proc + (my_rank < extra ? 1 : 0);
57
58 size_t offset = 0;
59 if (my_rank < extra) {
60 offset = (per_proc + 1) * my_rank;
61 }
62 else {
63 offset = (per_proc + 1) * extra + per_proc * (my_rank - extra);
64 }
65 return std::make_pair(count, offset);
66 }
67 } // namespace
68
69 int main(IOSS_MAYBE_UNUSED int argc, IOSS_MAYBE_UNUSED char *argv[])
70 {
71 #ifdef SEACAS_HAVE_MPI
72 MPI_Init(&argc, &argv);
73 ON_BLOCK_EXIT(MPI_Finalize);
74 #endif
75
76 Ioss::Init::Initializer io; // Initialize IOSS library.
77
78 write_blob();
79 bool diff = read_blob();
80 if (diff) {
81 std::cout << "Differences detected\n";
82 return 1;
83 }
84 else {
85 std::cout << "No Differences detected\n";
86 return 0;
87 }
88 }
89
90 void write_blob()
91 {
92 std::cout << "***** Writing Blob Example File...\n";
93 Ioss::PropertyManager properties;
94 properties.add(Ioss::Property("COMPOSE_RESTART", "YES"));
95 Ioss::DatabaseIO *dbo =
96 Ioss::IOFactory::create("exodus", "ioss_blob_example.e", Ioss::WRITE_RESTART,
97 Ioss::ParallelUtils::comm_world(), properties);
98 if (dbo == nullptr || !dbo->ok(true)) {
99 std::exit(EXIT_FAILURE);
100 }
101
102 // NOTE: 'region' owns 'dbo' pointer at this time
103 Ioss::Region region(dbo, "example_region");
104 region.begin_mode(Ioss::STATE_DEFINE_MODEL);
105
106 const size_t b1_size = 100;
107 const size_t b2_size = 200;
108 const size_t b3_size = 57;
109
110 // For example, we will just spread blob evenly across all ranks..
111 size_t par_size = dbo->util().parallel_size();
112 size_t my_rank = dbo->util().parallel_rank();
113
114 // Define a blob -- give name and size
115 auto count_offset = get_blob_size(b1_size, par_size, my_rank);
116 auto *blob1 = new Ioss::Blob(dbo, "Tempus", count_offset.first);
117 region.add(blob1);
118
119 // NOTE: These properties are not needed for serial case, but don't cause problems
120 blob1->property_add(Ioss::Property("global_size", (int64_t)b1_size));
121
122 count_offset = get_blob_size(b2_size, par_size, my_rank);
123 auto *blob2 = new Ioss::Blob(dbo, "Solver", count_offset.first);
124 region.add(blob2);
125
126 blob2->property_add(Ioss::Property("global_size", (int64_t)b2_size));
127
128 count_offset = get_blob_size(b3_size, par_size, my_rank);
129 auto *blob3 = new Ioss::Blob(dbo, "ABlob", count_offset.first);
130 region.add(blob3);
131
132 blob3->property_add(Ioss::Property("global_size", (int64_t)b3_size));
133
134 // These are "entity attributes" for blob1. Non-transient (constant) property
135 // applied to the blob, not each entry in the blob.
136 std::vector<double> offsets{1.0, 2.0, 0.0};
137 std::vector<double> scales{10.5, 11.5, 17.5};
138 blob1->property_add(Ioss::Property("Offset", offsets, Ioss::Property::ATTRIBUTE));
139 blob1->property_add(Ioss::Property("Scale", scales, Ioss::Property::ATTRIBUTE));
140
141 region.end_mode(Ioss::STATE_DEFINE_MODEL);
142
143 region.begin_mode(Ioss::STATE_DEFINE_TRANSIENT);
144
145 // Aaadd some transient fields to the blobs. There will be a value per entry in the blob.
146 Ioss::Field x("X", Ioss::Field::BasicType::REAL, "scalar", Ioss::Field::RoleType::TRANSIENT);
147 Ioss::Field dx("XDOT", Ioss::Field::BasicType::REAL, "scalar", Ioss::Field::RoleType::TRANSIENT);
148 Ioss::Field ddx("XDDOT", Ioss::Field::BasicType::REAL, "scalar",
149 Ioss::Field::RoleType::TRANSIENT);
150
151 blob1->field_add(x);
152 blob1->field_add(dx);
153 blob1->field_add(std::move(ddx));
154
155 // Blobs can have different fields
156 blob2->field_add(x);
157 blob2->field_add(dx);
158
159 blob3->field_add(x);
160
161 // Reduction Fields -- Single value per blob per timestep
162 Ioss::Field momentum("Momentum", Ioss::Field::BasicType::REAL, "vector_3d",
163 Ioss::Field::RoleType::REDUCTION);
164 Ioss::Field ke("kinetic_energy", Ioss::Field::BasicType::REAL, "scalar",
165 Ioss::Field::RoleType::REDUCTION);
166
167 blob1->field_add(ke);
168 blob1->field_add(momentum);
169
170 blob2->field_add(ke);
171 blob2->field_add(momentum);
172
173 blob3->field_add(ke);
174 blob3->field_add(momentum);
175
176 region.end_mode(Ioss::STATE_DEFINE_TRANSIENT);
177
178 region.begin_mode(Ioss::STATE_TRANSIENT);
179 const size_t num_ts = 10;
180 for (size_t ts = 0; ts < num_ts; ts++) {
181 double time = ts;
182 auto step = region.add_state(time);
183 region.begin_state(step);
184
185 const auto &blobs = region.get_blobs();
186 int idx = 0;
187 for (const auto *blob : blobs) {
188 // Dummy data for the fields. All the same here...
189 // Would be different in reality.
190 const size_t size = blob->entity_count();
191
192 // Get the global size and offset of this blob on this rank...
193 size_t gl_size = blob->get_optional_property("global_size", size);
194 size_t p_offset = blob->get_optional_property("_processor_offset", 0);
195
196 // Get the fields that are defined on this blob...
197 Ioss::NameList fields = blob->field_describe(Ioss::Field::RoleType::TRANSIENT);
198 Ioss::sort(fields.begin(), fields.end()); // Just done for testing; not needed
199 for (const auto &field : fields) {
200 std::vector<double> data = generate_data(time, gl_size, idx++, size, p_offset);
201 blob->put_field_data(field, data);
202 }
203
204 // Reduction fields...
205 Ioss::NameList red_fields = blob->field_describe(Ioss::Field::RoleType::REDUCTION);
206 for (const auto &field : red_fields) {
207 std::vector<double> data = generate_data(time, 3, idx++, 3, 0);
208 blob->put_field_data(field, data);
209 }
210 }
211 region.end_state(step);
212 }
213 region.end_mode(Ioss::STATE_TRANSIENT);
214 // File closed when `region` goes out of scope.
215 }
216
217 bool read_blob()
218 {
219 std::cout << "\n***** Reading Blob Example File...\n";
220 Ioss::PropertyManager properties;
221 properties.add(Ioss::Property("DECOMPOSITION_METHOD", "linear"));
222 Ioss::DatabaseIO *dbi =
223 Ioss::IOFactory::create("exodus", "ioss_blob_example.e", Ioss::READ_RESTART,
224 Ioss::ParallelUtils::comm_world(), properties);
225 if (dbi == nullptr || !dbi->ok(true)) {
226 std::exit(EXIT_FAILURE);
227 }
228
229 // NOTE: 'region' owns 'dbi' pointer at this time
230 Ioss::Region region(dbi, "example_region");
231
232 // Print a summary of the properties and fields on each blob...
233 const auto &blobs = region.get_blobs();
234 for (const auto *blob : blobs) {
235 std::cout << "\nBlob " << blob->name() << " contains: " << blob->entity_count()
236 << " item(s).\n";
237
238 Ioss::Utils::info_property(blob, Ioss::Property::ATTRIBUTE, "\tAttributes (Reduction): ");
239 Ioss::Utils::info_fields(blob, Ioss::Field::TRANSIENT, "\n\tTransient: ");
240 Ioss::Utils::info_fields(blob, Ioss::Field::REDUCTION, "\n\tTransient (Reduction): ", "\t");
241 }
242
243 size_t ts_count = 0;
244 if (region.property_exists("state_count")) {
245 ts_count = region.get_property("state_count").get_int();
246 }
247 std::cout << "\nFile contains " << ts_count << " timesteps.\n";
248
249 std::vector<Ioss::NameList> all_fields;
250 std::vector<Ioss::NameList> all_red_fields;
251
252 for (const auto *blob : blobs) {
253 // Get the names of the fields that are defined on this blob...
254 Ioss::NameList fields = blob->field_describe(Ioss::Field::RoleType::TRANSIENT);
255 Ioss::sort(fields.begin(), fields.end()); // Just done for testing; not needed
256 all_fields.push_back(fields);
257
258 // Reduction fields...
259 Ioss::NameList red_fields = blob->field_describe(Ioss::Field::RoleType::REDUCTION);
260 all_red_fields.push_back(red_fields);
261 }
262
263 bool diff = false;
264 for (size_t step = 1; step <= ts_count; step++) {
265 region.begin_state(step);
266 double time = region.get_state_time(step); // Region steps are 1-based
267 std::cout << "\n*** Step " << step << " is at time " << time << "\n";
268
269 int idx = 0;
270 std::vector<double> data;
271 int offset = 0;
272 for (const auto *blob : blobs) {
273 // Get the global size and offset of this blob on this rank...
274 // These are only needed for the comparison, not needed to just read data.
275 size_t size = blob->entity_count();
276 size_t gl_size = blob->get_optional_property("global_size", size);
277 size_t p_offset = blob->get_optional_property("_processor_offset", 0);
278
279 const auto &fields = all_fields[idx];
280 for (const auto &field : fields) {
281 blob->get_field_data(field, data);
282
283 // Compare with what was written to make sure read/write is ok.
284 std::vector<double> gold = generate_data(time, gl_size, offset++, size, p_offset);
285 if (data != gold) {
286 std::cout << "Difference for field " << field << " on blob " << blob->name()
287 << " at step " << step << "\n";
288 for (size_t i = 0; i < data.size(); i++) {
289 if (data[i] != gold[i]) {
290 std::cout << "Difference at index " << i << ", Data[i] = " << data[i]
291 << ", Gold[i] = " << gold[i] << "\n";
292 }
293 }
294 diff = true;
295 }
296 }
297
298 const auto &red_fields = all_red_fields[idx++];
299 for (const auto &field : red_fields) {
300 blob->get_field_data(field, data);
301 offset++; // Just for the testing part.
302 std::cout << "\t\tReduction Field " << field << ", Value = ";
303
304 size_t comp_count = blob->get_field(field).raw_storage()->component_count();
305 for (size_t i = 0; i < comp_count; i++) {
306 std::cout << data[i] << " ";
307 }
308 std::cout << "\n";
309 }
310 }
311 }
312 // File closed when `region` goes out of scope.
313 return diff;
314 }