Debian Codegraph @

packages/stk/stk_performance_tests/stk_mesh/ManyBlocksSidesetsPerformance.cpp

raw
1 // Copyright 2002 - 2008, 2010, 2011 National Technology Engineering
2 // Solutions of Sandia, LLC (NTESS). Under the terms of Contract
3 // DE-NA0003525 with NTESS, the U.S. Government retains certain rights
4 // in this software.
5 //
6 // Redistribution and use in source and binary forms, with or without
7 // modification, are permitted provided that the following conditions are
8 // met:
9 //
10 // * Redistributions of source code must retain the above copyright
11 // notice, this list of conditions and the following disclaimer.
12 //
13 // * Redistributions in binary form must reproduce the above
14 // copyright notice, this list of conditions and the following
15 // disclaimer in the documentation and/or other materials provided
16 // with the distribution.
17 //
18 // * Neither the name of NTESS nor the names of its contributors
19 // may be used to endorse or promote products derived from this
20 // software without specific prior written permission.
21 //
22 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
25 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
26 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
28 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
32 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 //
34
35 #include <gtest/gtest.h>
36 #include <stk_util/stk_config.h>
37 #include <stk_unit_test_utils/getOption.h>
38 #include <stk_unit_test_utils/MeshFixture.hpp>
39 #include <stk_unit_test_utils/GetMeshSpec.hpp>
40 #include <stk_unit_test_utils/PerformanceTester.hpp>
41 #include <stk_util/environment/memory_util.hpp>
42 #include <stk_util/parallel/ParallelReduce.hpp>
43 #include <stk_mesh/base/MetaData.hpp>
44 #include <stk_mesh/base/BulkData.hpp>
45 #include <stk_mesh/base/Bucket.hpp>
46 #include <stk_mesh/base/Field.hpp>
47 #include <stk_mesh/base/Entity.hpp>
48 #include <stk_mesh/base/GetEntities.hpp>
49 #include "stk_mesh/base/ExodusTranslator.hpp"
50 #include <stk_unit_test_utils/timer.hpp>
51 #include <stk_performance_tests/stk_mesh/multi_block.hpp>
52
53 #include <stk_mesh/base/SkinBoundary.hpp>
54 #include <stk_tools/mesh_tools/DetectHingesImpl.hpp>
55 #include <stk_tools/mesh_tools/DisconnectBlocks.hpp>
56 #include <stk_tools/mesh_tools/DisconnectBlocksImpl.hpp>
57 #include <stk_tools/mesh_tools/DisconnectUtils.hpp>
58 namespace stk_perf_many_blocks
59 {
60
61 class ManyBlocksSidesets : public stk::unit_test_util::MeshFixture
62 {
63 public:
64 ManyBlocksSidesets()
65 : batchTimer(get_comm())
66 { }
67
68 protected:
69 std::string get_mesh_spec(unsigned numElemsPerDim)
70 {
71 std::ostringstream os;
72 os<<"generated:"<<numElemsPerDim<<"x"<<numElemsPerDim<<"x"<<numElemsPerDim
73 <<"|shell:xXyYzZ|sideset:xXyYzZ";
74 return os.str();
75 }
76
77 std::string get_mesh_spec(unsigned nX, unsigned nY, unsigned nZ)
78 {
79 std::ostringstream os;
80 os<<"generated:"<<nX<<"x"<<nY<<"x"<<nZ
81 <<"|shell:xXyYzZ|sideset:xXyYzZ";
82 return os.str();
83 }
84
85 std::string get_mesh_spec_without_sidesets(unsigned numElemsPerDim, unsigned numBlocks)
86 {
87 std::ostringstream os;
88 os<<"generated:"<<numBlocks<<"x"<<numElemsPerDim<<"x"<<numElemsPerDim;
89 return os.str();
90 }
91
92 void setup_multi_block_mesh(unsigned numElemsPerDim, unsigned numBlocks, unsigned numFields=0, bool allFieldsSameSize=true)
93 {
94 stk::mesh::BulkData::AutomaticAuraOption auraOption = stk::mesh::BulkData::NO_AUTO_AURA;
95 setup_empty_mesh(auraOption);
96 stk::performance_tests::setup_multiple_blocks(get_meta(), numBlocks);
97 stk::performance_tests::setup_elem_fields_on_blocks(get_meta(), numFields, allFieldsSameSize);
98 stk::performance_tests::setup_sidesets_between_blocks(get_meta());
99 setup_mesh(get_mesh_spec(numElemsPerDim), auraOption);
100 stk::performance_tests::move_elements_to_other_blocks(get_bulk(), numElemsPerDim);
101 stk::performance_tests::fill_sidesets_between_blocks(get_bulk());
102 }
103
104 void setup_multi_block_mesh(unsigned nX, unsigned nY, unsigned nZ, unsigned numBlocks, unsigned numFields=0,
105 bool allFieldsSameSize=true,
106 unsigned initialBucketCapacity = stk::mesh::get_default_initial_bucket_capacity(),
107 unsigned maximumBucketCapacity = stk::mesh::get_default_maximum_bucket_capacity())
108 {
109 stk::mesh::BulkData::AutomaticAuraOption auraOption = stk::mesh::BulkData::NO_AUTO_AURA;
110 setup_empty_mesh(auraOption, initialBucketCapacity, maximumBucketCapacity);
111 stk::performance_tests::setup_multiple_blocks(get_meta(), numBlocks);
112 stk::performance_tests::setup_elem_fields_on_blocks(get_meta(), numFields, allFieldsSameSize);
113 stk::performance_tests::setup_sidesets_between_blocks(get_meta());
114 setup_mesh(get_mesh_spec(nX, nY, nZ), auraOption, initialBucketCapacity, maximumBucketCapacity);
115 stk::performance_tests::move_elements_to_other_blocks(get_bulk(), nX);
116 stk::performance_tests::fill_sidesets_between_blocks(get_bulk());
117 }
118
119 void setup_multi_block_mesh_without_sidesets(unsigned numElemsPerDim, unsigned numBlocks, unsigned numFields=0)
120 {
121 stk::mesh::BulkData::AutomaticAuraOption auraOption = stk::mesh::BulkData::NO_AUTO_AURA;
122 setup_empty_mesh(auraOption);
123 stk::performance_tests::setup_multiple_blocks(get_meta(), numBlocks);
124 const bool allFieldsSameSize = true;
125 stk::performance_tests::setup_elem_fields_on_blocks(get_meta(), numFields, allFieldsSameSize);
126 setup_mesh(get_mesh_spec_without_sidesets(numElemsPerDim, numBlocks), auraOption);
127 stk::performance_tests::move_elements_to_other_contiguous_blocks(get_bulk(), numBlocks);
128 }
129
130 void empty_mod_cycle()
131 {
132 get_bulk().modification_begin();
133 get_bulk().modification_end();
134 }
135
136 void output_mesh(stk::mesh::BulkData & bulk, const std::string & fileName)
137 {
138 std::string writeOutput = stk::unit_test_util::get_option("--output", "off");
139 if (writeOutput == "on") {
140 stk::io::write_mesh(fileName, bulk);
141 }
142 }
143
144 void output_mesh(stk::mesh::BulkData & bulk)
145 {
146 const std::string fileName = std::string(::testing::UnitTest::GetInstance()->current_test_info()->name()) + ".g";
147 output_mesh(bulk, fileName);
148 }
149
150 void print_memory_stats(const std::string& preamble, std::ostream &stream = std::cout)
151 {
152 size_t maxHwm = 0, minHwm = 0, avgHwm = 0;
153 stk::get_memory_high_water_mark_across_processors(get_comm(), maxHwm, minHwm, avgHwm);
154
155 if (get_parallel_rank() == 0) {
156 std::ostringstream os;
157 const double bytesInMegabyte = 1024*1024;
158 os << preamble << " "
159 << std::setw(6) << std::fixed << std::setprecision(1)
160 << "Max HWM: " <<double(maxHwm)/double(bytesInMegabyte) << "MB"
161 <<", Min HWM: "<<double(minHwm)/double(bytesInMegabyte) << "MB"
162 <<", Avg HWM: "<<double(avgHwm)/double(bytesInMegabyte) << "MB" <<std::endl;
163
164 stream << os.str();
165 }
166 }
167
168 void print_stats(const std::string& preamble, std::ostream &stream = std::cout)
169 {
170 print_memory_stats(preamble, stream);
171
172 unsigned localNumFaces = stk::mesh::count_entities(get_bulk(), stk::topology::FACE_RANK, get_meta().locally_owned_part());
173 unsigned globalNumFaces;
174 stk::all_reduce_sum(get_comm(), &localNumFaces, &globalNumFaces, 1);
175
176 if (get_parallel_rank() == 0) {
177 stream << "Global number of face created: " << globalNumFaces << "\n" << std::endl;
178 }
179 }
180
181 stk::unit_test_util::BatchTimer batchTimer;
182 };
183
184 TEST_F(ManyBlocksSidesets, timing)
185 {
186 if (get_parallel_size() > 10) return;
187
188 const unsigned NUM_RUNS = 5;
189 const int NUM_ITERS = 4000;
190 const int ELEMS_PER_DIM = 100;
191 const int NUM_BLOCKS = 100;
192
193
194 batchTimer.initialize_batch_timer();
195 for (unsigned j = 0; j < NUM_RUNS; j++) {
196 setup_multi_block_mesh(ELEMS_PER_DIM, NUM_BLOCKS);
197 batchTimer.start_batch_timer();
198 for (int i = 0; i < NUM_ITERS; i++) {
199 empty_mod_cycle();
200 }
201 batchTimer.stop_batch_timer();
202 reset_mesh();
203 }
204 batchTimer.print_batch_timing(NUM_ITERS);
205 }
206
207 TEST_F(ManyBlocksSidesets, disconnect_blocks_face_creation)
208 {
209 if (get_parallel_size() > 16) return;
210
211 const unsigned NUM_RUNS = 5;
212 const unsigned NUM_ITERS = 1;
213 int ELEMS_PER_DIM = stk::unit_test_util::get_command_line_option("--ne", 400);
214 int NUM_BLOCKS = stk::unit_test_util::get_command_line_option("--nb", 10);
215 bool verbose = stk::unit_test_util::has_option("--v");
216
217 batchTimer.initialize_batch_timer();
218 for (unsigned j = 0; j < NUM_RUNS; j++) {
219 setup_multi_block_mesh_without_sidesets(ELEMS_PER_DIM, NUM_BLOCKS);
220
221 stk::tools::disconnect_all_blocks(get_bulk());
222
223 stk::performance_tests::setup_sidesets_for_blocks(get_meta());
224
225 stk::mesh::PartVector elemBlocks;
226 stk::mesh::fill_element_block_parts(get_meta(), stk::topology::HEX_8, elemBlocks);
227
228 print_memory_stats("Before face creation:");
229
230 batchTimer.start_batch_timer();
231
232 for(stk::mesh::Part* block : elemBlocks) {
233 stk::mesh::Part& blockPart = *block;
234 unsigned partId = blockPart.id();
235
236 std::string sidesetName = "surface_" + std::to_string(partId);
237
238 stk::mesh::Part* surface = get_meta().get_part(sidesetName);
239 EXPECT_TRUE(nullptr != surface) << "Could not find " << sidesetName;
240 stk::mesh::create_exposed_block_boundary_sides(get_bulk(), *block, stk::mesh::PartVector{surface});
241
242 if(verbose) {
243 print_stats("After face creation for " + sidesetName + ":");
244 }
245 }
246
247 print_stats("After face creation :");
248
249 batchTimer.stop_batch_timer();
250 reset_mesh();
251 }
252 batchTimer.print_batch_timing(NUM_ITERS);
253 }
254
255 void write_restart_null_database(stk::io::StkMeshIoBroker &outStkIo, int step, double time)
256 {
257 const std::string dummyFileName("foo.restart");
258 size_t outputFileIndex = outStkIo.create_output_mesh(dummyFileName, stk::io::WRITE_RESTART, "null");
259
260 if (step>0)
261 {
262 const stk::mesh::FieldVector fields = outStkIo.bulk_data().mesh_meta_data().get_fields();
263 for(stk::mesh::FieldBase* field : fields)
264 {
265 const Ioss::Field::RoleType* fieldRole = stk::io::get_field_role(*field);
266 if(fieldRole == nullptr || *fieldRole == Ioss::Field::TRANSIENT)
267 outStkIo.add_field(outputFileIndex, *field);
268 }
269 }
270
271 outStkIo.write_output_mesh(outputFileIndex);
272
273 if (step>0)
274 {
275 outStkIo.begin_output_step(outputFileIndex, time);
276 outStkIo.write_defined_output_fields(outputFileIndex);
277 outStkIo.end_output_step(outputFileIndex);
278 }
279 }
280
281 TEST_F(ManyBlocksSidesets, sidesets_writeRestart)
282 {
283 if (get_parallel_size() > 16) return;
284
285 const unsigned NUM_RUNS = 1;
286 const unsigned NUM_ITERS = 5;
287 int ELEMS_PER_DIM = stk::unit_test_util::get_command_line_option("--ne", 200);
288 int NUM_BLOCKS = stk::unit_test_util::get_command_line_option("--nb", 200);
289 int NUM_FIELDS = stk::unit_test_util::get_command_line_option("--nf", 100);
290
291 batchTimer.initialize_batch_timer();
292 for (unsigned j = 0; j < NUM_RUNS; j++) {
293 setup_multi_block_mesh(ELEMS_PER_DIM, NUM_BLOCKS, NUM_FIELDS);
294
295 stk::parallel_machine_barrier(get_bulk().parallel());
296
297 batchTimer.start_batch_timer();
298
299 for(unsigned i=0; i<NUM_ITERS; ++i) {
300 stk::io::StkMeshIoBroker ioBroker;
301 ioBroker.set_bulk_data(get_bulk());
302 int outputTimeStep = static_cast<int>(i);
303 double outputTime = 1.0*outputTimeStep;
304 write_restart_null_database(ioBroker, outputTimeStep, outputTime);
305 }
306
307 batchTimer.stop_batch_timer();
308 reset_mesh();
309 }
310 batchTimer.print_batch_timing(NUM_ITERS);
311 }
312
313 TEST_F(ManyBlocksSidesets, noSidesets_writeRestart)
314 {
315 if (get_parallel_size() > 16) return;
316
317 const unsigned NUM_RUNS = 1;
318 const unsigned NUM_ITERS = 5;
319 int ELEMS_PER_DIM = stk::unit_test_util::get_command_line_option("--ne", 200);
320 int NUM_BLOCKS = stk::unit_test_util::get_command_line_option("--nb", 200);
321 int NUM_FIELDS = stk::unit_test_util::get_command_line_option("--nf", 100);
322
323 batchTimer.initialize_batch_timer();
324 for (unsigned j = 0; j < NUM_RUNS; j++) {
325 setup_multi_block_mesh_without_sidesets(ELEMS_PER_DIM, NUM_BLOCKS, NUM_FIELDS);
326
327 stk::parallel_machine_barrier(get_bulk().parallel());
328
329 batchTimer.start_batch_timer();
330
331 for(unsigned i=0; i<NUM_ITERS; ++i) {
332 stk::io::StkMeshIoBroker ioBroker;
333 ioBroker.set_bulk_data(get_bulk());
334 int outputTimeStep = static_cast<int>(i);
335 double outputTime = 1.0*outputTimeStep;
336 write_restart_null_database(ioBroker, outputTimeStep, outputTime);
337 }
338
339 batchTimer.stop_batch_timer();
340 reset_mesh();
341 }
342 batchTimer.print_batch_timing(NUM_ITERS);
343 }
344
345 TEST_F(ManyBlocksSidesets, find_restriction)
346 {
347 if (get_parallel_size() != 1) GTEST_SKIP();
348
349 const unsigned NUM_RUNS = 1;
350 const unsigned NUM_ITERS = 200;
351 unsigned numElemsX = 400;
352 unsigned numElemsY = 1;
353 unsigned numElemsZ = 2;
354 int NUM_BLOCKS = stk::unit_test_util::get_command_line_option("--nb", 400);
355 int NUM_FIELDS = stk::unit_test_util::get_command_line_option("--nf", 50);
356
357 unsigned initialBucketCapacity = 1;
358 unsigned maxBucketCapacity = 8;
359
360 stk::mesh::PartVector elemBlockParts;
361
362 batchTimer.initialize_batch_timer();
363
364 for (unsigned j = 0; j < NUM_RUNS; j++) {
365 const bool allFieldsSameSize = false;
366 setup_multi_block_mesh(numElemsX, numElemsY, numElemsZ, NUM_BLOCKS, NUM_FIELDS, allFieldsSameSize, initialBucketCapacity, maxBucketCapacity);
367
368 stk::mesh::fill_element_block_parts(get_meta(), stk::topology::INVALID_TOPOLOGY, elemBlockParts);
369
370 stk::parallel_machine_barrier(get_bulk().parallel());
371
372 batchTimer.start_batch_timer();
373
374 for(unsigned i=0; i<NUM_ITERS; ++i) {
375 for(const stk::mesh::Part* part : elemBlockParts) {
376 for(const stk::mesh::FieldBase* fieldPtr : get_meta().get_fields()) {
377 unsigned fieldLen = fieldPtr->length(*part);
378 STK_ThrowRequireMsg(fieldLen <= static_cast<unsigned>(NUM_BLOCKS), "Silly throw-require just to reference the fieldLen variable...");
379 }
380 }
381 }
382
383 batchTimer.stop_batch_timer();
384 reset_mesh();
385 }
386 batchTimer.print_batch_timing(NUM_ITERS);
387 }
388
389 TEST_F(ManyBlocksSidesets, find_restriction_part_union)
390 {
391 if (get_parallel_size() != 1) GTEST_SKIP();
392
393 const unsigned NUM_RUNS = 1;
394 const unsigned NUM_ITERS = 700;
395 unsigned numElemsX = 400;
396 unsigned numElemsY = 1;
397 unsigned numElemsZ = 2;
398 int NUM_BLOCKS = stk::unit_test_util::get_command_line_option("--nb", 400);
399 int NUM_FIELDS = stk::unit_test_util::get_command_line_option("--nf", 50);
400
401 unsigned initialBucketCapacity = 1;
402 unsigned maxBucketCapacity = 8;
403
404 stk::mesh::PartVector elemBlockParts;
405
406 batchTimer.initialize_batch_timer();
407
408 for (unsigned j = 0; j < NUM_RUNS; j++) {
409 const bool allFieldsSameSize = true;
410 setup_multi_block_mesh(numElemsX, numElemsY, numElemsZ, NUM_BLOCKS, NUM_FIELDS, allFieldsSameSize, initialBucketCapacity, maxBucketCapacity);
411
412 stk::mesh::fill_element_block_parts(get_meta(), stk::topology::INVALID_TOPOLOGY, elemBlockParts);
413
414 stk::parallel_machine_barrier(get_bulk().parallel());
415
416 batchTimer.start_batch_timer();
417
418 for(unsigned i=0; i<NUM_ITERS; ++i) {
419 for(const stk::mesh::Part* part : elemBlockParts) {
420 for(const stk::mesh::FieldBase* fieldPtr : get_meta().get_fields()) {
421 unsigned fieldLen = fieldPtr->length(*part);
422 STK_ThrowRequireMsg(fieldLen <= static_cast<unsigned>(NUM_BLOCKS), "Silly throw-require just to reference the fieldLen variable...");
423 }
424 }
425 }
426
427 batchTimer.stop_batch_timer();
428 reset_mesh();
429 }
430 batchTimer.print_batch_timing(NUM_ITERS);
431 }
432
433 TEST_F(ManyBlocksSidesets, selectUnion)
434 {
435 if (get_parallel_size() != 1) GTEST_SKIP();
436
437 const unsigned NUM_RUNS = 1;
438 const unsigned NUM_ITERS = 700;
439 unsigned numElemsX = 400;
440 unsigned numElemsY = 1;
441 unsigned numElemsZ = 2;
442 int NUM_BLOCKS = stk::unit_test_util::get_command_line_option("--nb", 400);
443 int NUM_FIELDS = stk::unit_test_util::get_command_line_option("--nf", 50);
444
445 unsigned initialBucketCapacity = 1;
446 unsigned maxBucketCapacity = 8;
447
448 stk::mesh::PartVector elemBlockParts;
449
450 batchTimer.initialize_batch_timer();
451
452 for (unsigned j = 0; j < NUM_RUNS; j++) {
453 const bool allFieldsSameSize = true;
454 setup_multi_block_mesh(numElemsX, numElemsY, numElemsZ, NUM_BLOCKS, NUM_FIELDS, allFieldsSameSize, initialBucketCapacity, maxBucketCapacity);
455
456 stk::mesh::fill_element_block_parts(get_meta(), stk::topology::INVALID_TOPOLOGY, elemBlockParts);
457
458 stk::parallel_machine_barrier(get_bulk().parallel());
459
460 batchTimer.start_batch_timer();
461
462 for(unsigned i=0; i<NUM_ITERS; ++i) {
463 for(int f=0; f<NUM_FIELDS; ++f) {
464 stk::mesh::selectUnion(elemBlockParts);
465 }
466 }
467
468 batchTimer.stop_batch_timer();
469 reset_mesh();
470 }
471 batchTimer.print_batch_timing(NUM_ITERS);
472 }
473
474 }