Debian Codegraph @

packages/stk/stk_performance_tests/stk_mesh/ChangeEntityPartPerfTest.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" // for AssertHelper, EXPECT_EQ, etc
36 #include <stk_mesh/base/BulkData.hpp> // for BulkData
37 #include <stk_mesh/base/GetEntities.hpp>
38 #include <stk_util/diag/StringUtil.hpp>
39 #include <stk_unit_test_utils/MeshFixture.hpp>
40 #include <stk_unit_test_utils/PerformanceTester.hpp>
41 #include <stk_unit_test_utils/getOption.h>
42 #include <stk_performance_tests/stk_mesh/multi_block.hpp>
43 #include <stk_unit_test_utils/timer.hpp>
44
45 namespace
46 {
47
48 class ChangePartsTest : public stk::unit_test_util::MeshFixture
49 {
50 public:
51 ChangePartsTest() : stk::unit_test_util::MeshFixture(),
52 batchTimer(get_comm()),
53 elementsOnBlock1(true)
54 {
55 }
56
57 void setup_mesh_with_many_blocks_many_elements_in_one_block()
58 {
59 std::string meshDesc = "generated:" + std::to_string(numElemPerDim) + "x"
60 + std::to_string(numElemPerDim) + "x"
61 + std::to_string(numElemPerDim);
62 setup_empty_mesh(stk::mesh::BulkData::NO_AUTO_AURA);
63 setup_multiple_blocks();
64
65 stk::io::fill_mesh(meshDesc, get_bulk());
66
67 setup_block_part_vectors();
68 }
69
70 void setup_block_part_vectors()
71 {
72 stk::mesh::Part* block1Part = get_meta().get_part("block_1");
73 stk::mesh::Part* block2Part = &get_meta().declare_part("block_2", stk::topology::ELEM_RANK);
74
75 block1PartVector.push_back(block1Part);
76 block2PartVector.push_back(block2Part);
77 }
78
79 void setup_multiple_blocks()
80 {
81 std::string block1Name("block_1");
82 stk::mesh::Part& block1Part = get_meta().declare_part_with_topology(block1Name, stk::topology::HEX_8);
83 get_meta().set_part_id(block1Part, 1);
84 parts.push_back(&block1Part);
85
86 for (unsigned i = 1; i < numBlocks; i++) {
87 unsigned partId = i+1;
88 std::string blockName = "block_" + std::to_string(partId);
89 stk::mesh::Part& part = get_meta().declare_part_with_topology(blockName, stk::topology::HEX_8);
90 get_meta().set_part_id(part, partId);
91 parts.push_back(&part);
92 }
93 }
94
95 void elements_from_block1_to_other_blocks()
96 {
97 if(numBlocks < std::pow(numElemPerDim, 3)) { return; }
98
99 stk::mesh::EntityVector entities;
100 stk::mesh::get_entities(get_bulk(), stk::topology::ELEM_RANK, entities);
101 stk::mesh::Part* removePart = parts[0];
102
103 for(unsigned i = 1; i < entities.size(); i++) {
104 stk::mesh::Part* addPart = parts[i];
105
106 get_bulk().modification_begin();
107 get_bulk().change_entity_parts(entities[i], stk::mesh::PartVector{addPart}, stk::mesh::PartVector{removePart});
108 get_bulk().modification_end();
109 }
110 }
111
112 void elements_back_to_block1()
113 {
114 if(numBlocks < std::pow(numElemPerDim, 3)) { return; }
115
116 stk::mesh::EntityVector entities;
117 stk::mesh::get_entities(get_bulk(), stk::topology::ELEM_RANK, entities);
118
119 for(unsigned i = 1; i < entities.size(); i++) {
120 stk::mesh::Part* addPart = parts[0];
121 stk::mesh::Part* removePart = parts[i];
122
123 get_bulk().modification_begin();
124 get_bulk().change_entity_parts(entities[i], stk::mesh::PartVector{addPart}, stk::mesh::PartVector{removePart});
125 get_bulk().modification_end();
126 }
127 }
128
129 void elements_back_to_block1_using_selector()
130 {
131 if(numBlocks < std::pow(numElemPerDim, 3)) { return; }
132
133 stk::mesh::Selector selector = stk::mesh::selectUnion(parts);
134 stk::mesh::Part* addPart = parts[0];
135
136 get_bulk().batch_change_entity_parts(selector, stk::topology::ELEM_RANK, stk::mesh::PartVector{addPart}, stk::mesh::PartVector{});
137 }
138
139 void prepare_move_elements_between_block1_and_block2(stk::mesh::PartVector& addParts, stk::mesh::PartVector& removeParts)
140 {
141 if(elementsOnBlock1) {
142 addParts = block2PartVector;
143 removeParts = block1PartVector;
144 } else {
145 addParts = block1PartVector;
146 removeParts = block2PartVector;
147 }
148 elementsOnBlock1 = !elementsOnBlock1;
149 }
150
151 protected:
152 unsigned numBlocks;
153 unsigned numElemPerDim;
154 stk::mesh::PartVector parts;
155 stk::mesh::PartVector block1PartVector;
156 stk::mesh::PartVector block2PartVector;
157 stk::unit_test_util::BatchTimer batchTimer;
158
159 private:
160 bool elementsOnBlock1;
161 };
162
163 TEST_F(ChangePartsTest, changeEntityPartsUsingEntityVectorSimplePerfTest)
164 {
165 if(get_parallel_size() > 1) { GTEST_SKIP(); }
166
167 const unsigned NUM_RUNS = 5;
168 const unsigned NUM_ITERS = stk::unit_test_util::get_command_line_option("-r", 50);
169 numElemPerDim = stk::unit_test_util::get_command_line_option("-e", 50);
170 numBlocks = 1;
171
172 batchTimer.initialize_batch_timer();
173
174 setup_mesh_with_many_blocks_many_elements_in_one_block();
175
176 stk::mesh::PartVector addParts;
177 stk::mesh::PartVector removeParts;
178 stk::mesh::EntityVector elements;
179 get_bulk().get_entities(stk::topology::ELEM_RANK, stk::mesh::Selector(*block1PartVector[0]), elements);
180
181 for (unsigned j = 0; j < NUM_RUNS; j++) {
182 batchTimer.start_batch_timer();
183 for(unsigned i = 0; i < NUM_ITERS; i++) {
184 prepare_move_elements_between_block1_and_block2(addParts, removeParts);
185 get_bulk().batch_change_entity_parts(elements, addParts, removeParts);
186 }
187 batchTimer.stop_batch_timer();
188 }
189 batchTimer.print_batch_timing(NUM_ITERS);
190 }
191
192 TEST_F(ChangePartsTest, changeEntityPartsUsingSelectorSimplePerfTest)
193 {
194 if(get_parallel_size() > 1) { GTEST_SKIP(); }
195
196 const unsigned NUM_RUNS = 5;
197 const unsigned NUM_ITERS = stk::unit_test_util::get_command_line_option("-r", 200);
198 numElemPerDim = stk::unit_test_util::get_command_line_option("-e", 50);
199 numBlocks = 1;
200
201 batchTimer.initialize_batch_timer();
202
203 setup_mesh_with_many_blocks_many_elements_in_one_block();
204
205 stk::mesh::PartVector addParts;
206 stk::mesh::PartVector removeParts;
207
208 for (unsigned j = 0; j < NUM_RUNS; j++) {
209 batchTimer.start_batch_timer();
210 for(unsigned i = 0; i < NUM_ITERS; i++) {
211 prepare_move_elements_between_block1_and_block2(addParts, removeParts);
212 stk::mesh::Selector elemSelector(*removeParts[0]);
213 get_bulk().batch_change_entity_parts(elemSelector, stk::topology::ELEM_RANK, addParts, removeParts);
214 }
215 batchTimer.stop_batch_timer();
216 }
217 batchTimer.print_batch_timing(NUM_ITERS);
218 }
219
220 TEST_F(ChangePartsTest, cacheRemovalImpactChangeEntityPartsWithEntityVector)
221 {
222 if(get_parallel_size() > 1) { GTEST_SKIP(); }
223
224 const unsigned NUM_RUNS = 5;
225 const unsigned NUM_ITERS = stk::unit_test_util::get_command_line_option("-r", 500000);
226 numElemPerDim = stk::unit_test_util::get_command_line_option("-e", 80);
227 numBlocks = stk::unit_test_util::get_command_line_option("-b", 125);
228
229 batchTimer.initialize_batch_timer();
230
231 setup_mesh_with_many_blocks_many_elements_in_one_block();
232
233 for (unsigned j = 0; j < NUM_RUNS; j++) {
234 batchTimer.start_batch_timer();
235 for(unsigned i = 0; i < NUM_ITERS; i++) {
236 elements_from_block1_to_other_blocks();
237 elements_back_to_block1();
238
239 for(unsigned block = 0; block < numBlocks; block++) {
240 get_bulk().get_buckets(stk::topology::ELEM_RANK, stk::mesh::Selector(*parts[block]));
241 }
242 }
243 batchTimer.stop_batch_timer();
244 }
245 batchTimer.print_batch_timing(NUM_ITERS);
246 }
247
248 TEST_F(ChangePartsTest, cacheRemovalImpactChangeEntityPartsWithSelector)
249 {
250 if(get_parallel_size() > 1) { GTEST_SKIP(); }
251
252 const unsigned NUM_RUNS = 5;
253 const unsigned NUM_ITERS = stk::unit_test_util::get_command_line_option("-r", 500000);
254 numElemPerDim = stk::unit_test_util::get_command_line_option("-e", 80);
255 numBlocks = stk::unit_test_util::get_command_line_option("-b", 125);
256
257 batchTimer.initialize_batch_timer();
258
259 setup_mesh_with_many_blocks_many_elements_in_one_block();
260
261 for (unsigned j = 0; j < NUM_RUNS; j++) {
262 batchTimer.start_batch_timer();
263 for(unsigned i = 0; i < NUM_ITERS; i++) {
264 elements_from_block1_to_other_blocks();
265 elements_back_to_block1_using_selector();
266
267 for(unsigned block = 0; block < numBlocks; block++) {
268 get_bulk().get_buckets(stk::topology::ELEM_RANK, stk::mesh::Selector(*parts[block]));
269 }
270 }
271 batchTimer.stop_batch_timer();
272 }
273 batchTimer.print_batch_timing(NUM_ITERS);
274 }
275
276 }