Debian Codegraph @

packages/stk/stk_performance_tests/stk_mesh/CommunicateFieldData.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_mesh/base/Field.hpp>
37 #include <stk_mesh/base/FieldBase.hpp>
38 #include <stk_mesh/base/FieldParallel.hpp>
39 #include <stk_mesh/base/MetaData.hpp>
40 #include <stk_mesh/base/BulkData.hpp>
41 #include <stk_mesh/base/MeshBuilder.hpp>
42 #include <stk_mesh/base/ExodusTranslator.hpp>
43 #include <stk_topology/topology.hpp>
44 #include <stk_util/environment/CPUTime.hpp>
45 #include <stk_util/environment/WallTime.hpp>
46 #include <stk_util/environment/perf_util.hpp>
47 #include <stk_util/environment/memory_util.hpp>
48 #include <stk_util/parallel/Parallel.hpp>
49 #include <stk_unit_test_utils/timer.hpp>
50 #include <stk_performance_tests/stk_mesh/multi_block.hpp>
51
52 #include <stk_io/StkMeshIoBroker.hpp> // for StkMeshIoBroker
53 #include <stk_mesh/base/GetEntities.hpp> // for count_entities
54 #include <stk_mesh/base/GetNgpField.hpp>
55 #include <stk_mesh/base/NgpFieldParallel.hpp>
56 #include <vector>
57 #include <string>
58
59 #include <stk_unit_test_utils/getOption.h>
60
61 namespace
62 {
63
64 double initial_value1[3] = {-1, 2, -0.3};
65
66 void createNodalVectorField(stk::mesh::MetaData& meshMetaData,
67 const std::string &fieldName,
68 const std::string &partName)
69 {
70 stk::mesh::Field<double> &field1 = meshMetaData.declare_field<double>(stk::topology::NODE_RANK, fieldName);
71 stk::mesh::Part* part = meshMetaData.get_part(partName);
72 ASSERT_TRUE(part != nullptr)<<partName<<" not found";
73 stk::mesh::put_field_on_mesh(field1, *part, 3, initial_value1);
74 }
75
76 void createNodalVectorFields(stk::mesh::MetaData& meshMetaData,
77 const std::string& fieldBaseName,
78 unsigned numFields,
79 const std::string& partName)
80 {
81 for(unsigned i=0; i<numFields; ++i) {
82 std::string fieldName = fieldBaseName+std::to_string(i);
83 createNodalVectorField(meshMetaData, fieldName, partName);
84 }
85 }
86
87 size_t setFieldData(stk::mesh::BulkData& mesh, stk::mesh::Selector select)
88 {
89 const stk::mesh::MetaData& meta = mesh.mesh_meta_data();
90 const stk::mesh::FieldVector& fields = meta.get_fields(stk::topology::NODE_RANK);
91 const stk::mesh::BucketVector& node_buckets = mesh.get_buckets(stk::topology::NODE_RANK, select);
92 size_t num_nodes = 0;
93 for(size_t i=0; i<node_buckets.size(); ++i) {
94 stk::mesh::Bucket& bucket = *node_buckets[i];
95 num_nodes += bucket.size();
96 for(size_t f=0; f<fields.size(); ++f) {
97 double* data = static_cast<double*>(stk::mesh::field_data(*fields[f], bucket));
98 unsigned flen = stk::mesh::field_scalars_per_entity(*fields[f], bucket);
99 for(size_t n=0; n<flen*bucket.size(); ++n) {
100 data[n] = static_cast<double>(f);
101 }
102 }
103 }
104
105 return num_nodes;
106 }
107
108 void createMetaAndBulkData(stk::io::StkMeshIoBroker &exodusFileReader,
109 const std::string& genMeshSpec,
110 unsigned numBlocks,
111 unsigned numFields)
112 {
113 std::string exodusFileName = stk::unit_test_util::get_option("-i", "NO_FILE_SPECIFIED");
114 if (exodusFileName == "NO_FILE_SPECIFIED") {
115 exodusFileName = genMeshSpec;
116 }
117
118 exodusFileReader.add_mesh_database(exodusFileName, stk::io::READ_MESH);
119
120 stk::ParallelMachine communicator = MPI_COMM_WORLD;
121 int myProc = stk::parallel_machine_rank(communicator);
122
123 if (myProc==0) {
124 std::cerr << "Starting To Read Mesh: " << exodusFileName << std::endl;
125 }
126
127 exodusFileReader.create_input_mesh();
128 std::shared_ptr<stk::mesh::MetaData> stkMeshMetaData = exodusFileReader.meta_data_ptr();
129 if (numBlocks==1) {
130 createNodalVectorFields(*stkMeshMetaData, "nodalField", numFields, "{universal}");
131 }
132 else {
133 stk::performance_tests::setup_multiple_blocks(*stkMeshMetaData, numBlocks);
134 stk::mesh::PartVector blockParts;
135 stk::mesh::fill_element_block_parts(*stkMeshMetaData, stk::topology::HEX_8, blockParts);
136 for(const stk::mesh::Part* part : blockParts) {
137 std::string fieldBaseName = part->name()+"nodalField";
138 createNodalVectorFields(*stkMeshMetaData, fieldBaseName, numFields, part->name());
139 }
140 }
141 stkMeshMetaData->commit();
142
143 std::shared_ptr<stk::mesh::BulkData> arg_bulk_data = stk::mesh::MeshBuilder(MPI_COMM_WORLD).set_aura_option(stk::mesh::BulkData::AUTO_AURA).create(stkMeshMetaData);
144 exodusFileReader.set_bulk_data(arg_bulk_data);
145 stk::mesh::BulkData& stkMeshBulkData = *arg_bulk_data;
146
147 bool delay_field_data_allocation = true;
148 exodusFileReader.populate_mesh(delay_field_data_allocation);
149 exodusFileReader.populate_field_data();
150
151 size_t num_ghostings = stkMeshBulkData.ghostings().size();
152 std::vector<size_t> num_ghost_nodes(num_ghostings,0);
153 for (size_t ghost_i = 0 ; ghost_i < num_ghostings ; ++ghost_i) {
154 num_ghost_nodes[ghost_i] = setFieldData(stkMeshBulkData, stkMeshBulkData.ghosting_part(*stkMeshBulkData.ghostings()[ghost_i]));
155 }
156
157 for(int i=0; i<stkMeshBulkData.parallel_size(); ++i) {
158 stk::parallel_machine_barrier(MPI_COMM_WORLD);
159 if (stkMeshBulkData.parallel_rank() == i) {
160 for (size_t ghost_i = 0 ; ghost_i<num_ghostings ; ++ghost_i) {
161 std::cerr <<"proc "<<stkMeshBulkData.parallel_rank()<<", Number of " << stkMeshBulkData.ghostings()[ghost_i]->name() << " nodes: " << num_ghost_nodes[ghost_i] << std::endl;
162 }
163 }
164
165 stk::parallel_machine_barrier(MPI_COMM_WORLD);
166 std::cerr << std::flush;
167 stk::parallel_machine_barrier(MPI_COMM_WORLD);
168 }
169 if (myProc==0) {
170 std::cerr << "Finished Reading Mesh" << std::endl;
171 }
172
173 stk::mesh::Selector allEntities = stkMeshMetaData->universal_part();
174 std::vector<size_t> entityCounts;
175 stk::mesh::count_entities(allEntities, stkMeshBulkData, entityCounts);
176 size_t numElements = entityCounts[stk::topology::ELEMENT_RANK];
177 size_t numNodes = entityCounts[stk::topology::NODE_RANK];
178
179 if (myProc==0) {
180 std::cerr << "Number of elements: " << numElements << std::endl;
181 std::cerr << "Number of nodes: " << numNodes << std::endl;
182 }
183 }
184
185 void createNgpFields(stk::mesh::BulkData& bulk)
186 {
187 auto stkFields = bulk.mesh_meta_data().get_fields();
188 for(auto stkField : stkFields) {
189 stk::mesh::get_updated_ngp_field<double>(*stkField);
190 }
191 }
192
193 template<typename T>
194 void set_modify_on_device(std::vector<stk::mesh::NgpField<T>*> ngpFields)
195 {
196 for(auto ngpField : ngpFields) {
197 ngpField->modify_on_device();
198 }
199 }
200
201 void addPartToGhosting(stk::mesh::BulkData & bulk, const std::string & partName, stk::mesh::Ghosting& ghost)
202 {
203 const int numProcs = bulk.parallel_size();
204 const int myProc = bulk.parallel_rank();
205 stk::mesh::MetaData &meta = bulk.mesh_meta_data();
206 std::vector<stk::mesh::EntityProc> entities_to_ghost;
207 stk::mesh::Selector surface_selector = *meta.get_part(partName) & meta.locally_owned_part();
208 stk::mesh::EntityVector surface_nodes;
209 stk::mesh::get_selected_entities(surface_selector,bulk.buckets(stk::topology::NODE_RANK), surface_nodes);
210 const int next_proc = (myProc+1)%numProcs;
211 for (size_t i=0 ; i<surface_nodes.size() ; ++i) {
212 entities_to_ghost.push_back(stk::mesh::EntityProc(surface_nodes[i],next_proc));
213 }
214 bulk.change_ghosting( ghost, entities_to_ghost );
215 }
216
217 void test_communicate_field_data_all_ghosting(stk::ParallelMachine communicator, int num_iters)
218 {
219 stk::unit_test_util::BatchTimer batchTimer(communicator);
220 batchTimer.initialize_batch_timer();
221
222 stk::io::StkMeshIoBroker exodusFileReader(communicator);
223
224 std::string genMeshSpec = "generated:60x60x48|sideset:xXyY";
225 const unsigned numBlocks = 1;
226 const unsigned numFields = 8;
227 createMetaAndBulkData(exodusFileReader,genMeshSpec, numBlocks, numFields);
228 stk::mesh::BulkData &mesh = exodusFileReader.bulk_data();
229
230 mesh.modification_begin();
231 stk::mesh::Ghosting & ghosting_1 = mesh.create_ghosting( "CUSTOM_1" );
232 stk::mesh::Ghosting & ghosting_2 = mesh.create_ghosting( "CUSTOM_2" );
233 stk::mesh::Ghosting & ghosting_3 = mesh.create_ghosting( "CUSTOM_3" );
234 stk::mesh::Ghosting & ghosting_4 = mesh.create_ghosting( "CUSTOM_4" );
235
236 addPartToGhosting(mesh, "surface_1", ghosting_1);
237 addPartToGhosting(mesh, "surface_2", ghosting_2);
238 addPartToGhosting(mesh, "surface_3", ghosting_3);
239 addPartToGhosting(mesh, "surface_4", ghosting_4);
240 mesh.modification_end();
241
242 size_t num_ghostings = mesh.ghostings().size();
243 std::ostringstream oss;
244 for (size_t ghost_i = 0 ; ghost_i < num_ghostings ; ++ghost_i) {
245 size_t num_nodes = stk::mesh::count_selected_entities(mesh.ghosting_part(*mesh.ghostings()[ghost_i]),mesh.buckets(stk::topology::NODE_RANK));
246 oss <<"proc "<<mesh.parallel_rank()<<", Number of " << mesh.ghostings()[ghost_i]->name() << " nodes: " << num_nodes << std::endl;
247 }
248
249 std::cerr << oss.str() << std::endl;
250
251 const int my_proc = mesh.parallel_rank();
252 if (my_proc == 0) {
253 std::cerr << "Calling communicate_field_data " << num_iters << " times"<<std::endl;
254 }
255
256 const stk::mesh::MetaData& meta = mesh.mesh_meta_data();
257 const stk::mesh::FieldVector& fields = meta.get_fields();
258 std::vector<const stk::mesh::FieldBase*> const_fields(fields.size());
259 for(size_t i=0; i<fields.size(); ++i) {
260 const_fields[i] = fields[i];
261 }
262
263 const unsigned NUM_RUNS = 5;
264
265 for (unsigned j = 0; j < NUM_RUNS; j++) {
266 stk::parallel_machine_barrier(mesh.parallel());
267 batchTimer.start_batch_timer();
268
269 for(int iter=0; iter<num_iters; ++iter) {
270 for (size_t ghost_i = 0 ; ghost_i<mesh.ghostings().size() ; ++ghost_i) {
271 stk::mesh::communicate_field_data(*mesh.ghostings()[ghost_i], const_fields);
272 }
273 }
274
275 for(int iter=0; iter<num_iters; ++iter) {
276 stk::mesh::communicate_field_data(mesh, const_fields);
277 }
278
279 batchTimer.stop_batch_timer();
280 }
281 batchTimer.print_batch_timing(num_iters);
282 }
283
284 void test_communicate_field_data_ghosting(MPI_Comm communicator,
285 unsigned numBlocks, unsigned numFields, int num_iters)
286 {
287 stk::unit_test_util::BatchTimer batchTimer(communicator);
288 batchTimer.initialize_batch_timer();
289
290 stk::io::StkMeshIoBroker exodusFileReader(communicator);
291 std::string genMeshSpec = "generated:100x100x48|sideset:xXyY";
292 createMetaAndBulkData(exodusFileReader,genMeshSpec, numBlocks, numFields);
293
294 stk::mesh::BulkData& mesh = exodusFileReader.bulk_data();
295 const int my_proc = mesh.parallel_rank();
296 if (my_proc == 0) {
297 std::cerr << "Calling communicate_field_data " << num_iters << " times"<<std::endl;
298 }
299
300 const stk::mesh::MetaData& meta = mesh.mesh_meta_data();
301 const stk::mesh::FieldVector& fields = meta.get_fields();
302 std::vector<const stk::mesh::FieldBase*> const_fields(fields.size());
303 for(size_t i=0; i<fields.size(); ++i) {
304 const_fields[i] = fields[i];
305 }
306
307 const unsigned NUM_RUNS = 5;
308
309 for (unsigned j = 0; j < NUM_RUNS; j++) {
310 stk::parallel_machine_barrier(mesh.parallel());
311 batchTimer.start_batch_timer();
312
313 for(int iter=0; iter<num_iters; ++iter) {
314 stk::mesh::communicate_field_data(mesh, const_fields);
315 }
316 batchTimer.stop_batch_timer();
317 }
318 batchTimer.print_batch_timing(num_iters);
319 }
320
321 void test_communicate_field_data_ngp_ghosting(int num_iters, bool syncToHostEveryIter)
322 {
323 const int meshDim = stk::unit_test_util::get_command_line_option("-s", 100);
324 std::string meshDimStr = std::to_string(meshDim);
325 std::string meshSpec = "generated:" + meshDimStr + "x" + meshDimStr + "x" + meshDimStr;
326
327 stk::ParallelMachine comm = MPI_COMM_WORLD;
328 stk::unit_test_util::BatchTimer batchTimer(comm);
329 batchTimer.initialize_batch_timer();
330
331 stk::io::StkMeshIoBroker exodusFileReader(comm);
332
333 const unsigned numBlocks = 1;
334 const unsigned numFields = 8;
335 createMetaAndBulkData(exodusFileReader, meshSpec, numBlocks, numFields);
336
337 stk::mesh::BulkData &mesh = exodusFileReader.bulk_data();
338 const int my_proc = mesh.parallel_rank();
339
340 if (my_proc == 0) {
341 std::cerr << "Calling communicate_field_data " << num_iters << " times"<<std::endl;
342 }
343
344 createNgpFields(mesh);
345 const stk::mesh::MetaData& meta = mesh.mesh_meta_data();
346 const stk::mesh::FieldVector& fields = meta.get_fields();
347 std::vector<stk::mesh::NgpField<double>*> ngpFields;
348 for(auto field : fields) {
349 ngpFields.push_back(&stk::mesh::get_updated_ngp_field<double>(*field));
350 }
351
352 const unsigned NUM_RUNS = 5;
353
354 for (unsigned j = 0; j < NUM_RUNS; j++) {
355 stk::parallel_machine_barrier(mesh.parallel());
356 batchTimer.start_batch_timer();
357
358 for(int iter=0; iter<num_iters; ++iter) {
359 if(syncToHostEveryIter) {
360 set_modify_on_device<double>(ngpFields);
361 }
362
363 stk::mesh::communicate_field_data(mesh, ngpFields, true);
364 }
365 batchTimer.stop_batch_timer();
366 }
367 batchTimer.print_batch_timing(num_iters);
368 }
369
370 TEST(CommunicateFieldData, copy_to_all)
371 {
372 stk::ParallelMachine comm = MPI_COMM_WORLD;
373 if (stk::parallel_machine_size(comm) < 2) { GTEST_SKIP(); }
374
375 test_communicate_field_data_all_ghosting(comm, 300);
376 }
377
378
379 TEST(CommunicateFieldData, Ghosting)
380 {
381 stk::ParallelMachine communicator = MPI_COMM_WORLD;
382 int numProcs = stk::parallel_machine_size(communicator);
383 if (numProcs < 2) {
384 return;
385 }
386
387 const unsigned numBlocks = 1;
388 const unsigned numFields = 8;
389 test_communicate_field_data_ghosting(communicator, numBlocks, numFields, 1000);
390 }
391
392 TEST(CommunicateFieldData, Ghosting_MultiBlock)
393 {
394 stk::ParallelMachine communicator = MPI_COMM_WORLD;
395 int numProcs = stk::parallel_machine_size(communicator);
396 if (numProcs < 2) {
397 return;
398 }
399
400 const unsigned numBlocks = 1;
401 const unsigned numFields = 8;
402 test_communicate_field_data_ghosting(communicator, numBlocks, numFields, 300);
403 }
404
405 TEST(CommunicateFieldData, NgpGhosting)
406 {
407 if (stk::parallel_machine_size(MPI_COMM_WORLD) < 2) { GTEST_SKIP(); }
408
409 int iter = stk::unit_test_util::get_command_line_option("-t", 1000);
410 bool syncToHostEveryIter = stk::unit_test_util::get_command_line_option("-h", false);
411 test_communicate_field_data_ngp_ghosting(iter, syncToHostEveryIter);
412 }
413
414 }