Debian Codegraph @

packages/stk/stk_performance_tests/stk_mesh/Selector.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/environment/CPUTime.hpp>
37 #include <stk_util/environment/WallTime.hpp>
38 #include <stk_util/environment/perf_util.hpp>
39 #include <stk_util/parallel/Parallel.hpp>
40
41 #include "stk_unit_test_utils/stk_mesh_fixtures/SelectorFixture.hpp"
42 #include <stdexcept>
43 #include <stk_mesh/base/Types.hpp>
44 #include <stk_mesh/base/Bucket.hpp>
45 #include <stk_mesh/base/ExodusTranslator.hpp>
46 #include <stk_mesh/base/MeshBuilder.hpp>
47 #include <stk_mesh/base/Part.hpp>
48 #include <stk_mesh/base/Selector.hpp>
49 #include <stk_io/FillMesh.hpp>
50 #include <stk_unit_test_utils/timer.hpp>
51
52 namespace {
53
54 using stk::mesh::fixtures::VariableSelectorFixture;
55
56 }
57
58 TEST(selector_timings, selector_timings)
59 {
60 // A scaling test for selector and bucket operations
61
62 // If we are running with STL in debug mode we shrink the problem
63 // down in order to keep things running in a reasonable amount of
64 // time.
65 #ifdef _GLIBCXX_DEBUG
66 size_t N = 500;
67 #else
68 size_t N = 50000;
69 #endif
70
71 const unsigned NUM_RUNS = 5;
72 stk::unit_test_util::BatchTimer batchTimer(MPI_COMM_WORLD);
73 batchTimer.initialize_batch_timer();
74 for (unsigned j = 0; j < NUM_RUNS; j++) {
75 VariableSelectorFixture fix(N);
76
77 size_t total_buckets_grabbed = 0;
78
79 batchTimer.start_batch_timer();
80
81 for (size_t n = 1 ; n<N; n*=2) {
82 // Selector creation
83 stk::mesh::Selector selectUnion;
84 for (size_t part_i = 0 ; part_i<n ; ++part_i) {
85 selectUnion |= *fix.m_declared_part_vector[part_i];
86 }
87
88 // Selector usage:
89 stk::mesh::EntityRank entity_rank = stk::topology::NODE_RANK;
90 stk::mesh::BucketVector const& buckets_out = fix.m_BulkData.get_buckets(entity_rank, selectUnion);
91 total_buckets_grabbed += buckets_out.size();
92 }
93
94 std::cout << "total_buckets_grabbed: " << total_buckets_grabbed << std::endl;
95
96 batchTimer.stop_batch_timer();
97 }
98 const int numIterations = 1;
99 batchTimer.print_batch_timing(numIterations);
100 }
101
102 void create_elem_block_parts(int numParts, stk::mesh::MetaData& meta)
103 {
104 const int firstBlockAfterBlock1 = 2;
105 for(int i=0; i<numParts; ++i) {
106 stk::mesh::Part& part = meta.declare_part(std::string("elemBlock")+std::to_string(i+firstBlockAfterBlock1), stk::topology::ELEM_RANK);
107 meta.set_part_id(part, i+firstBlockAfterBlock1);
108 }
109 }
110
111 void move_elems_to_each_part(stk::mesh::BulkData& bulk, int elemsPerPart)
112 {
113 stk::mesh::PartVector blocks = stk::mesh::ExodusTranslator(bulk).get_element_block_parts();
114 stk::mesh::MetaData& meta = bulk.mesh_meta_data();
115 stk::mesh::Part* block1 = meta.get_part("block_1");
116 STK_ThrowRequire(block1 != nullptr);
117 stk::mesh::EntityVector elems;
118 stk::mesh::get_entities(bulk, stk::topology::ELEM_RANK, meta.locally_owned_part(), elems);
119
120 bulk.modification_begin();
121
122 unsigned elemCounter = 0;
123 unsigned blockCounter = 0;
124 stk::mesh::PartVector addParts = {blocks[blockCounter++]};
125 stk::mesh::PartVector rmParts = {block1};
126
127 while(elemCounter < elems.size()) {
128 if (addParts[0] != block1) {
129 for(int e=0; e<elemsPerPart; ++e) {
130 if (elemCounter >= elems.size()) {
131 break;
132 }
133 bulk.change_entity_parts(elems[elemCounter++], addParts, rmParts);
134 }
135 }
136 if (blockCounter >= blocks.size()) {
137 break;
138 }
139 addParts[0] = blocks[blockCounter++];
140 }
141
142 bulk.modification_end();
143 }
144
145 TEST(selector_timings, get_buckets)
146 {
147 MPI_Comm comm = stk::parallel_machine_world();
148 if (stk::parallel_machine_size(comm) > 1) { GTEST_SKIP(); }
149
150 std::shared_ptr<stk::mesh::BulkData> bulkPtr = stk::mesh::MeshBuilder(comm).set_spatial_dimension(3).create();
151
152 stk::mesh::MetaData& meta = bulkPtr->mesh_meta_data();
153 const int numParts = 5000;
154 create_elem_block_parts(numParts, meta);
155
156 stk::io::fill_mesh("generated:100x100x100", *bulkPtr);
157 const int numElems = 100*100*100;
158 const int elemsPerPart = numElems/numParts;
159 move_elems_to_each_part(*bulkPtr, elemsPerPart);
160
161 stk::unit_test_util::BatchTimer batchTimer(MPI_COMM_WORLD);
162 batchTimer.initialize_batch_timer();
163
164 batchTimer.start_batch_timer();
165
166 stk::mesh::PartVector blocks = stk::mesh::ExodusTranslator(*bulkPtr).get_element_block_parts();
167 for(stk::mesh::Part* partPtr : blocks) {
168 stk::mesh::Selector selector(*partPtr);
169 selector.get_buckets(stk::topology::NODE_RANK);
170 }
171
172 batchTimer.stop_batch_timer();
173
174 const int numIterations = 1;
175 batchTimer.print_batch_timing(numIterations);
176 }
177
178 TEST(Verify, selectorAlgorithmicComplexity)
179 {
180 //
181 // Verify expected algorithmic complexity is obtained by selector classes.
182 // Only test serial, in parallel this gets a bit ill posed as some procs have empty bucket selections
183 // and the complexity is not meaningful.
184 //
185
186 stk::mesh::fixtures::SelectorFixture fix;
187 fix.m_meta_data.commit();
188 fix.m_bulk_data.modification_begin();
189 fix.generate_mesh();
190
191
192 if (fix.get_BulkData().parallel_size() > 1) return;
193
194 stk::mesh::Part & partA = fix.m_partA;
195 stk::mesh::Part & partB = fix.m_partB;
196 stk::mesh::Part & partC = fix.m_partC;
197 stk::mesh::Part & partD = fix.m_partD;
198
199 stk::mesh::Selector selectABC = partA & partB & partC;
200 stk::mesh::Selector selectA = partA;
201 stk::mesh::Selector selectB = partB;
202 stk::mesh::Selector selectC = partC;
203 stk::mesh::Selector selectD = partD;
204
205 unsigned numCopiesBase = 1000000;
206 std::vector<unsigned> trialLen;
207
208 trialLen.push_back(128);
209 trialLen.push_back(256);
210 trialLen.push_back(512);
211 trialLen.push_back(1024);
212 trialLen.push_back(2048);
213 trialLen.push_back(4096);
214
215 std::vector<double> elapsedTime(trialLen.size());
216
217 //
218 // Test complexity of construction of large binary selector as would be made of selectUnion of a large number of parts
219 //
220 for(unsigned j=0; j<trialLen.size(); ++j) {
221
222 unsigned numCopies = (numCopiesBase/2)/trialLen[j];
223
224 double startTime = stk::cpu_time();
225 for(unsigned i=0; i<numCopies; ++i) {
226 stk::mesh::Selector selectBig ;
227 for(unsigned k=0; k< trialLen[j] ; ++k) {
228 selectBig |= selectC & selectD;
229 }
230 }
231 elapsedTime[j] = (stk::cpu_time()-startTime)/(double)numCopies;
232 std::cout<<"Selector Constructor Complexity Check, Length: "<<trialLen[j]
233 <<" time: "<<elapsedTime[j]<<" Expecting linear complexity"<<std::endl;
234 }
235 // Expected factor = N, which implies the |= algorithm is O(N) where N is the number of times or is called
236 // Due to cache effects and random runtime variability though keep a pretty loose tolerane around this expectation
237 double expectedFactor = ((double)trialLen.back())/trialLen[0];
238 double factor = elapsedTime.back()/elapsedTime[0];
239 double relative_factor = std::abs(expectedFactor-factor)/expectedFactor;
240
241 std::cout << "relative_factor= " << relative_factor << std::endl;
242
243 double gold_relative_factor = 0.40;
244 EXPECT_TRUE( relative_factor < gold_relative_factor );
245
246 std::cout<<" Speedup factors: "<<expectedFactor<<" vs. "<<factor<<std::endl;
247
248 //
249 // Test complexity of traversal of the large selector. Some buckets will be found immediately and some will require traversal of
250 // the entire selector to exclude thus this operation should be O(N) in selector size.
251 const stk::mesh::BucketVector& buckets = fix.get_BulkData().get_buckets(stk::topology::NODE_RANK, fix.get_MetaData().locally_owned_part());
252
253 for(unsigned j=0; j<trialLen.size(); ++j) {
254 unsigned count=0;
255
256 unsigned numCopies = numCopiesBase/trialLen[j];
257
258 stk::mesh::Selector selectCD = selectC | selectD;
259 stk::mesh::Selector selectBig = selectCD;
260 for(int i=0; i< ((int)trialLen[j]-1) ; ++i) {
261 selectBig &= selectCD;
262 }
263 double startTime = stk::cpu_time();
264 for(unsigned i=0; i<numCopies; ++i) {
265 for(unsigned ibucket=0, n=buckets.size(); ibucket<n; ++ibucket) {
266 if(selectBig(*(buckets[ibucket]))) {
267 count++;
268 }
269 }
270 }
271
272 EXPECT_TRUE(count == 2 * numCopies);
273
274 elapsedTime[j] = (stk::cpu_time()-startTime)/(double)numCopies;
275 std::cout<<"Selector Traversal Complexity Check, Length: "<<trialLen[j]
276 <<" time: "<<elapsedTime[j]<<" Expecting linear complexity"<<std::endl;
277 }
278
279 expectedFactor = ((double)trialLen.back())/trialLen[0];
280 factor = elapsedTime.back()/elapsedTime[0];
281
282 std::cout<<" Speedup factors: "<<expectedFactor<<" vs. "<<factor<<std::endl;
283
284 relative_factor = std::abs(expectedFactor-factor)/expectedFactor;
285 std::cout <<" relative_factor= " << relative_factor << "\n";
286 EXPECT_TRUE( relative_factor < 0.60);
287 }