Debian Codegraph @

packages/stk/stk_performance_tests/stk_mesh/NgpFieldUpdate.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 <string>
36 #include <ostream>
37 #include <gtest/gtest.h>
38 #include <stk_mesh/base/NgpMesh.hpp>
39 #include <stk_mesh/base/BulkData.hpp>
40 #include <stk_mesh/base/GetNgpField.hpp>
41 #include <stk_mesh/base/GetNgpMesh.hpp>
42 #include <stk_mesh/base/GetEntities.hpp>
43 #include <stk_mesh/base/ExodusTranslator.hpp>
44 #include <stk_util/environment/WallTime.hpp>
45 #include <stk_util/environment/perf_util.hpp>
46 #include <stk_util/parallel/ParallelReduce.hpp>
47 #include <stk_unit_test_utils/MeshFixture.hpp>
48 #include <stk_unit_test_utils/TextMesh.hpp>
49 #include <stk_unit_test_utils/getOption.h>
50 #include <stk_unit_test_utils/GetMeshSpec.hpp>
51 #include <stk_unit_test_utils/timer.hpp>
52 #include <stk_performance_tests/stk_mesh/multi_block.hpp>
53
54 class NgpFieldSyncTest : public stk::unit_test_util::MeshFixture
55 {
56 public:
57 NgpFieldSyncTest() : stk::unit_test_util::MeshFixture()
58 {}
59
60 void setup_mesh_with_many_blocks_many_elements(unsigned numBlocks, unsigned numElemPerDim)
61 {
62 std::string meshDesc = "generated:" + std::to_string(numElemPerDim) + "x"
63 + std::to_string(numElemPerDim) + "x"
64 + std::to_string(numElemPerDim);
65 stk::performance_tests::setup_multiple_blocks(get_meta(), numBlocks);
66 stk::io::fill_mesh(meshDesc, get_bulk());
67 stk::performance_tests::move_elements_to_other_blocks(get_bulk(), numElemPerDim);
68 }
69
70 void setup_fields(unsigned numComponent, unsigned tensorFieldSizePerElem, unsigned vectorFieldSizePerElem)
71 {
72 const std::vector<int> init(numComponent, 1);
73 auto tensorField = &get_meta().declare_field<double>(stk::topology::ELEMENT_RANK, "TensorField");
74 auto vectorField = &get_meta().declare_field<double>(stk::topology::ELEMENT_RANK, "VectorField");
75 auto stkIntField = &get_meta().declare_field<int>(stk::topology::ELEMENT_RANK, "intField", 1);
76
77 stk::mesh::put_field_on_mesh(*tensorField, get_meta().universal_part(), tensorFieldSizePerElem, nullptr);
78 stk::mesh::put_field_on_mesh(*vectorField, get_meta().universal_part(), vectorFieldSizePerElem, nullptr);
79 stk::mesh::put_field_on_mesh(*stkIntField, get_meta().universal_part(), numComponent, init.data());
80 }
81
82 stk::mesh::Selector get_non_contiguous_partial_selector(unsigned numBlocksToSync)
83 {
84 stk::mesh::PartVector parts;
85 unsigned partCount = 0;
86
87 stk::mesh::PartVector elemBlockParts;
88 stk::mesh::fill_element_block_parts(get_meta(), stk::topology::HEX_8, elemBlockParts);
89 const unsigned numBlocks = elemBlockParts.size();
90
91 for(unsigned i = 0; i < numBlocks; i++) {
92 if(i % 2 == 0) {
93 if(partCount == numBlocksToSync) { break; }
94 parts.push_back(elemBlockParts[i]);
95 partCount++;
96 }
97 }
98
99 for(unsigned i = 0; i < numBlocks; i++) {
100 if(i % 2 == 1) {
101 if(partCount == numBlocksToSync) { break; }
102 parts.push_back(elemBlockParts[i]);
103 partCount++;
104 }
105 }
106
107 return stk::mesh::selectUnion(parts);
108 }
109
110 stk::mesh::Selector get_contiguous_partial_selector(unsigned numBlocksToSync)
111 {
112 stk::mesh::PartVector parts;
113
114 stk::mesh::PartVector elemBlockParts;
115 stk::mesh::fill_element_block_parts(get_meta(), stk::topology::HEX_8, elemBlockParts);
116 const unsigned numBlocks = elemBlockParts.size();
117 STK_ThrowRequire(numBlocksToSync <= numBlocks);
118
119 for(unsigned i = 0; i < numBlocksToSync; i++) {
120 parts.push_back(elemBlockParts[i]);
121 }
122
123 return stk::mesh::selectUnion(parts);
124 }
125 };
126
127 class NgpFieldUpdateFixture : public stk::unit_test_util::MeshFixture
128 {
129 public:
130 NgpFieldUpdateFixture()
131 : stk::unit_test_util::MeshFixture(),
132 tensorField(nullptr),
133 vectorField(nullptr),
134 tensorFieldSizePerElem(72),
135 vectorFieldSizePerElem(8),
136 numElemBlocks(100),
137 numElemsPerDim(100),
138 numElements(std::pow(numElemsPerDim, 3))
139 {}
140
141 virtual void setup_host_mesh()
142 {
143 setup_empty_mesh(stk::mesh::BulkData::NO_AUTO_AURA);
144 setup_mesh_with_fields("generated:100x100x100");
145 }
146
147 std::string generate_stacked_block_mesh_desc(unsigned numBlocks)
148 {
149 std::string meshDesc;
150
151 for(unsigned i = 0; i < numBlocks; i++) {
152 meshDesc += get_nodal_string_for_block(i+1);
153 if(i != numBlocks-1) {
154 meshDesc += "\n";
155 }
156 }
157 return meshDesc;
158 }
159
160 std::string get_nodal_string_for_block(unsigned blockId)
161 {
162 std::ostringstream blockStr;
163 for(unsigned i = 0; i < 2; i++) {
164 blockStr << "0," << blockId*2+i-1 << ",HEX_8,";
165 for(unsigned j = 0; j < 8; j++) {
166 blockStr << j+1 + ((blockId-1)*2 + i) * 8 << ",";
167 }
168 blockStr << "block_" << blockId;
169 if(i != 1) {
170 blockStr << "\n";
171 }
172 }
173 return blockStr.str();
174 }
175
176 void setup_mesh_with_stacked_blocks(unsigned numBlocks)
177 {
178 double init = 0.0;
179 setup_empty_mesh(stk::mesh::BulkData::NO_AUTO_AURA);
180 std::string meshDesc = generate_stacked_block_mesh_desc(numBlocks);
181
182 stk::mesh::FieldBase* field = &get_meta().declare_field<double>(stk::topology::ELEMENT_RANK, "FieldA");
183 stk::mesh::put_field_on_mesh(*field, get_meta().universal_part(), &init);
184 stk::unit_test_util::setup_text_mesh(get_bulk(), meshDesc);
185 }
186
187 void setup_mesh_with_fields(const std::string &meshSpecification)
188 {
189 tensorField = &get_meta().declare_field<double>(stk::topology::ELEMENT_RANK, "TensorField");
190 vectorField = &get_meta().declare_field<double>(stk::topology::ELEMENT_RANK, "VectorField");
191 stk::mesh::put_field_on_mesh(*tensorField, get_meta().universal_part(), tensorFieldSizePerElem, nullptr);
192 stk::mesh::put_field_on_mesh(*vectorField, get_meta().universal_part(), vectorFieldSizePerElem, nullptr);
193 stk::performance_tests::setup_multiple_blocks(get_meta(), numElemBlocks);
194 stk::io::fill_mesh(meshSpecification, get_bulk());
195 stk::performance_tests::move_elements_to_other_blocks(get_bulk(), numElemsPerDim);
196 }
197
198 void update_fields()
199 {
200 stk::mesh::NgpField<double>& ngpTensorField = stk::mesh::get_updated_ngp_field<double>(*tensorField);
201 ngpTensorField.sync_to_device();
202
203 stk::mesh::NgpField<double>& ngpVectorField = stk::mesh::get_updated_ngp_field<double>(*vectorField);
204 ngpVectorField.sync_to_device();
205 }
206
207 protected:
208 stk::mesh::Field<double>* tensorField;
209 stk::mesh::Field<double>* vectorField;
210 unsigned tensorFieldSizePerElem;
211 unsigned vectorFieldSizePerElem;
212 unsigned numElemBlocks;
213 unsigned numElemsPerDim;
214 unsigned numElements;
215 };
216
217 class NgpMeshChangeElementPartMembershipWithFields : public NgpFieldUpdateFixture
218 {
219 public:
220 NgpMeshChangeElementPartMembershipWithFields()
221 : NgpFieldUpdateFixture()
222 { }
223
224 void setup_host_mesh() override
225 {
226 setup_empty_mesh(stk::mesh::BulkData::NO_AUTO_AURA);
227 get_meta().declare_part(newPartName);
228 setup_mesh_with_fields("generated:100x100x100");
229 }
230
231 void change_element_part_membership(int cycle)
232 {
233 get_bulk().modification_begin();
234 const stk::mesh::Part* part = get_part();
235 STK_ThrowRequireMsg(part!=nullptr,"get_part returned nullptr, newPartName="<<newPartName);
236 get_bulk().change_entity_parts<stk::mesh::ConstPartVector>(get_element(cycle), {get_part()});
237 get_bulk().modification_end();
238 stk::mesh::get_updated_ngp_mesh(get_bulk());
239 }
240
241 private:
242 stk::mesh::Entity get_element(int cycle)
243 {
244 stk::mesh::EntityId elemId = cycle+1;
245 return get_bulk().get_entity(stk::topology::ELEM_RANK, elemId);
246 }
247
248 const stk::mesh::Part* get_part()
249 {
250 return get_meta().get_part(newPartName);
251 }
252
253 std::string newPartName;
254 };
255
256 class NgpMeshCreateEntityWithFields : public NgpFieldUpdateFixture
257 {
258 public:
259 NgpMeshCreateEntityWithFields()
260 : NgpFieldUpdateFixture()
261 { }
262
263 void create_entity(int cycle, int numElemsToCreatePerModCycle)
264 {
265 get_bulk().modification_begin();
266 for(int i=0; i<numElemsToCreatePerModCycle; ++i) {
267 const int index = cycle*numElemsToCreatePerModCycle + i;
268 get_bulk().declare_element(get_new_entity_id(index));
269 }
270 get_bulk().modification_end();
271 stk::mesh::get_updated_ngp_mesh(get_bulk());
272 }
273
274 private:
275 stk::mesh::EntityId get_new_entity_id(int cycle)
276 {
277 return numElements + cycle + 1;
278 }
279 };
280
281 class NgpMeshGhostingEntityWithFields : public NgpFieldUpdateFixture
282 {
283 public:
284 NgpMeshGhostingEntityWithFields()
285 : NgpFieldUpdateFixture(),
286 ghostingName("myGhosting"),
287 ghosting(nullptr)
288 { }
289
290 void setup_host_mesh() override
291 {
292 setup_empty_mesh(stk::mesh::BulkData::NO_AUTO_AURA);
293 setup_mesh_with_fields("generated:100x100x100");
294 get_bulk().modification_begin();
295 ghosting = &get_bulk().create_ghosting(ghostingName);
296 get_bulk().modification_end();
297 }
298
299 protected:
300 void ghost_element(int cycle)
301 {
302 get_bulk().modification_begin();
303 get_bulk().change_ghosting(*ghosting, element_to_ghost(cycle));
304 get_bulk().modification_end();
305 stk::mesh::get_updated_ngp_mesh(get_bulk());
306 }
307
308 private:
309 stk::mesh::EntityProcVec element_to_ghost(int cycle)
310 {
311 stk::mesh::EntityId firstLocalElemId = get_parallel_rank()*numElements/2 + 1;
312 stk::mesh::EntityId elemId = firstLocalElemId + cycle;
313 stk::mesh::Entity elem = get_bulk().get_entity(stk::topology::ELEM_RANK, elemId);
314 int ghostRank = 1 - get_parallel_rank();
315 return {{elem, ghostRank}};
316 }
317
318 std::string ghostingName;
319 stk::mesh::Ghosting* ghosting;
320 };
321
322 TEST_F( NgpMeshChangeElementPartMembershipWithFields, Timing )
323 {
324 if (get_parallel_size() != 1) return;
325
326 const unsigned NUM_RUNS = 5;
327 #ifdef STK_ENABLE_GPU
328 const int NUM_ITERS = 50;
329 #else
330 const int NUM_ITERS = 2500;
331 #endif
332 const int NUM_FAKE_ITERS = 2500;
333
334 stk::unit_test_util::BatchTimer batchTimer(get_comm());
335
336 batchTimer.initialize_batch_timer();
337 for (unsigned j = 0; j < NUM_RUNS; j++) {
338 setup_host_mesh();
339 batchTimer.start_batch_timer();
340
341 for (int i = 0; i < NUM_ITERS; i++) {
342 change_element_part_membership(i);
343 update_fields();
344 }
345 batchTimer.stop_batch_timer();
346 reset_mesh();
347 }
348 batchTimer.print_batch_timing(NUM_FAKE_ITERS);
349 }
350
351 TEST_F( NgpMeshCreateEntityWithFields, Timing )
352 {
353 if (get_parallel_size() != 1) return;
354
355 const unsigned NUM_RUNS = 5;
356 #ifdef STK_ENABLE_GPU
357 const int numModCycles = 50;
358 #else
359 const int numModCycles = 4000;
360 #endif
361 const int numFakeModCycles = 4000;
362 const int numElemsToCreatePerModCycle = 40;
363
364 stk::unit_test_util::BatchTimer batchTimer(get_comm());
365 batchTimer.initialize_batch_timer();
366 for (unsigned j = 0; j < NUM_RUNS; j++) {
367 setup_host_mesh();
368 batchTimer.start_batch_timer();
369
370
371 for (int i=0; i<numModCycles; i++) {
372 create_entity(i, numElemsToCreatePerModCycle);
373 update_fields();
374 }
375 batchTimer.stop_batch_timer();
376 reset_mesh();
377 }
378 batchTimer.print_batch_timing(numFakeModCycles);
379 };
380
381 TEST_F( NgpMeshGhostingEntityWithFields, Timing )
382 {
383 if (get_parallel_size() != 2) return;
384
385 const unsigned NUM_RUNS = 5;
386 const int NUM_ITERS = 500;
387
388 stk::unit_test_util::BatchTimer batchTimer(get_comm());
389
390 batchTimer.initialize_batch_timer();
391 for (unsigned j = 0; j < NUM_RUNS; j++) {
392 setup_host_mesh();
393 batchTimer.start_batch_timer();
394 for (int i = 0; i < NUM_ITERS; i++) {
395 ghost_element(i);
396 update_fields();
397 }
398 batchTimer.stop_batch_timer();
399 reset_mesh();
400 }
401 batchTimer.print_batch_timing(NUM_ITERS);
402 }
403
404 TEST_F(NgpFieldSyncTest, PartialSyncTiming)
405 {
406 if(get_parallel_size() != 1) return;
407
408 const unsigned NUM_RUNS = 5;
409 const unsigned NUM_ITERS = stk::unit_test_util::get_command_line_option("-r", 20000000);
410 unsigned numComponents = stk::unit_test_util::get_command_line_option("-c", 1);
411 unsigned numBlocks = stk::unit_test_util::get_command_line_option("-b", 20);
412 unsigned numBlocksToSync = stk::unit_test_util::get_command_line_option("-s", 5);
413 unsigned numElemPerDim = stk::unit_test_util::get_command_line_option("-e", 100);
414 unsigned tensorFieldSizePerElem = stk::unit_test_util::get_command_line_option("--tensorField", 72);
415 unsigned vectorFieldSizePerElem = stk::unit_test_util::get_command_line_option("-vectorField", 8);
416 bool justSyncAll = stk::unit_test_util::get_command_line_option("-a", false);
417 bool contiguousBlocks = stk::unit_test_util::get_command_line_option("-t", true);
418 numBlocksToSync = std::min(numBlocks, numBlocksToSync);
419 stk::unit_test_util::BatchTimer batchTimer(MPI_COMM_WORLD);
420
421 setup_empty_mesh(stk::mesh::BulkData::NO_AUTO_AURA);
422 setup_fields(numComponents, tensorFieldSizePerElem, vectorFieldSizePerElem);
423 setup_mesh_with_many_blocks_many_elements(numBlocks, numElemPerDim);
424
425 stk::mesh::FieldBase* fieldBase = get_meta().get_field(stk::topology::ELEMENT_RANK, "intField");
426 stk::mesh::FieldBase* tensorFieldBase = get_meta().get_field(stk::topology::ELEMENT_RANK, "TensorField");
427 stk::mesh::FieldBase* vectorFieldBase = get_meta().get_field(stk::topology::ELEMENT_RANK, "VectorField");
428 stk::mesh::NgpField<int>& ngpIntField = stk::mesh::get_updated_ngp_field<int>(*fieldBase);
429 stk::mesh::NgpField<double>& ngpTensorField = stk::mesh::get_updated_ngp_field<double>(*tensorFieldBase);
430 stk::mesh::NgpField<double>& ngpVectorField = stk::mesh::get_updated_ngp_field<double>(*vectorFieldBase);
431 stk::mesh::Selector selector;
432
433 if(contiguousBlocks) {
434 selector = get_contiguous_partial_selector(numBlocksToSync);
435 } else {
436 selector = get_non_contiguous_partial_selector(numBlocksToSync);
437 }
438
439 batchTimer.initialize_batch_timer();
440 for (unsigned j = 0; j < NUM_RUNS; j++) {
441 batchTimer.start_batch_timer();
442 for(unsigned i = 0; i < NUM_ITERS; i++) {
443 if(justSyncAll) {
444 ngpIntField.modify_on_host();
445 ngpTensorField.modify_on_host();
446 ngpVectorField.modify_on_host();
447 }
448 else {
449 ngpIntField.modify_on_host(selector);
450 ngpTensorField.modify_on_host(selector);
451 ngpVectorField.modify_on_host(selector);
452 }
453 ngpIntField.sync_to_device();
454 ngpTensorField.sync_to_device();
455 ngpVectorField.sync_to_device();
456 }
457 batchTimer.stop_batch_timer();
458 }
459
460 std::cout << "Blocks: " << numBlocks << " Blocks to sync: "
461 << numBlocksToSync << " (" << (numBlocksToSync * 100.0 / numBlocks) << " %)" << std::endl;
462 batchTimer.print_batch_timing(NUM_ITERS);
463 }