Debian Codegraph @

packages/stk/stk_performance_tests/stk_mesh/SkinningLargeCube.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 <stdexcept>
36
37 #include <gtest/gtest.h>
38 #include <stk_util/environment/WallTime.hpp>
39 #include <stk_util/parallel/ParallelComm.hpp>
40 #include <stk_util/parallel/CommSparse.hpp>
41 #include <stk_util/parallel/ParallelReduce.hpp>
42 #include <stk_util/environment/perf_util.hpp>
43 #include <stk_unit_test_utils/timer.hpp>
44
45 #include <stk_mesh/base/BulkModification.hpp>
46 #include <stk_mesh/base/MetaData.hpp>
47 #include <stk_mesh/base/BulkData.hpp>
48 #include <stk_mesh/base/Entity.hpp>
49 #include <stk_mesh/base/EntityLess.hpp>
50 #include <stk_mesh/base/EntityKey.hpp>
51 #include <stk_mesh/base/GetEntities.hpp>
52 #include <stk_mesh/base/Selector.hpp>
53 #include <stk_mesh/base/CreateEdges.hpp>
54
55 #include "stk_unit_test_utils/stk_mesh_fixtures/HexFixture.hpp"
56 #include <stk_mesh/base/BoundaryAnalysis.hpp>
57 #include <stk_mesh/base/SkinMesh.hpp>
58
59 #include <stk_io/WriteMesh.hpp>
60
61 namespace {
62
63 using stk::mesh::EntityRank;
64
65 static const stk::mesh::EntityRank NODE_RANK = stk::topology::NODE_RANK;
66
67 size_t count_skin_entities( stk::mesh::BulkData & mesh, stk::mesh::Part & skin_part, EntityRank skin_rank ) {
68
69 const stk::mesh::MetaData & fem_meta = mesh.mesh_meta_data();
70
71 stk::mesh::Selector select_skin = skin_part & fem_meta.locally_owned_part() ;
72
73 const stk::mesh::BucketVector& buckets = mesh.buckets( skin_rank );
74
75 return count_selected_entities( select_skin, buckets);
76 }
77
78 void delete_skin( stk::mesh::BulkData & mesh, stk::mesh::Part & skin_part, EntityRank skin_rank ) {
79
80 const stk::mesh::MetaData & fem_meta = mesh.mesh_meta_data();
81
82 stk::mesh::Selector select_skin = skin_part & fem_meta.locally_owned_part() ;
83
84 const stk::mesh::BucketVector& buckets = mesh.buckets( skin_rank );
85
86 stk::mesh::EntityVector skin_entities;
87
88 stk::mesh::get_selected_entities( select_skin, buckets, skin_entities);
89
90 mesh.modification_begin();
91
92 for ( stk::mesh::EntityVector::iterator i = skin_entities.begin(); i != skin_entities.end(); ++i) {
93 mesh.destroy_entity(*i);
94 }
95
96 mesh.modification_end();
97 }
98
99 void update_skin( stk::mesh::BulkData & mesh, stk::mesh::Part *skin_part, EntityRank element_rank ) {
100
101 stk::mesh::EntityVector owned_elements, modified_elements;
102
103 // select owned
104 const stk::mesh::MetaData & fem_meta = mesh.mesh_meta_data();
105 stk::mesh::Selector owned = fem_meta.locally_owned_part();
106 stk::mesh::get_selected_entities( owned,
107 mesh.buckets(element_rank),
108 owned_elements);
109
110 for( stk::mesh::EntityVector::iterator i = owned_elements.begin();
111 i != owned_elements.end(); ++i )
112 {
113 stk::mesh::Entity entity= *i;
114 if ( mesh.state(entity) == stk::mesh::Created ||
115 mesh.state(entity) == stk::mesh::Modified ) {
116 modified_elements.push_back(entity);
117 }
118 }
119
120 stk::mesh::PartVector add_parts(1, skin_part);
121 stk::mesh::skin_mesh(mesh, add_parts);
122 }
123
124 void find_owned_nodes_with_relations_outside_closure(
125 const stk::mesh::BulkData& mesh,
126 stk::mesh::EntityVector & closure,
127 stk::mesh::Selector select_owned,
128 stk::mesh::EntityVector & nodes)
129 {
130 nodes.clear();
131
132 //the closure is a sorted unique vector
133 const EntityRank upward_rank = stk::topology::EDGE_RANK;
134 stk::mesh::EntityVector::iterator node_end = std::lower_bound(closure.begin(),
135 closure.end(),
136 stk::mesh::EntityKey(upward_rank, 0),
137 stk::mesh::EntityLess(mesh));
138
139 for (stk::mesh::EntityVector::iterator itr = closure.begin(); itr != node_end; ++itr) {
140 stk::mesh::Entity node = *itr;
141
142 if (select_owned(mesh.bucket(node))) {
143
144 const stk::mesh::Bucket &node_bkt = mesh.bucket(node);
145 const stk::mesh::Ordinal node_bkt_idx = mesh.bucket_ordinal(node);
146
147 //loop over the relations and check to see if they are in the closure
148 for (EntityRank irank = stk::topology::BEGIN_RANK;
149 irank != stk::topology::END_RANK;
150 ++irank)
151 {
152 stk::mesh::Entity const * rel_entity_i = node_bkt.begin(node_bkt_idx, irank);
153 stk::mesh::Entity const * rel_entity_end = node_bkt.end(node_bkt_idx, irank);
154 for ( ; rel_entity_i != rel_entity_end ; ++rel_entity_i ) {
155 stk::mesh::Entity current_entity = *rel_entity_i;
156 //has relation outside of closure
157 if ( !std::binary_search(node_end,
158 closure.end(),
159 current_entity,
160 stk::mesh::EntityLess(mesh)) )
161 {
162 nodes.push_back(node);
163 break;
164 }
165 }
166 }
167 }
168 }
169 }
170
171 void copy_nodes_and_break_relations( stk::mesh::BulkData & mesh,
172 stk::mesh::EntityVector & closure,
173 stk::mesh::EntityVector & nodes,
174 stk::mesh::EntityVector & new_nodes)
175 {
176 for (size_t i = 0; i < nodes.size(); ++i) {
177 stk::mesh::Entity entity = nodes[i];
178 stk::mesh::Entity new_entity = new_nodes[i];
179
180 std::vector<stk::mesh::EntitySideComponent> sides;
181
182 //loop over the relations and check to see if they are in the closure
183 for (stk::mesh::EntityRank irank = stk::topology::END_RANK;
184 irank != stk::topology::BEGIN_RANK; )
185 {
186 --irank;
187
188 int num_rels = mesh.num_connectivity(entity, irank);
189 stk::mesh::Entity const *rel_ents = mesh.begin(entity, irank);
190 stk::mesh::ConnectivityOrdinal const *rel_ords = mesh.begin_ordinals(entity, irank);
191
192 for (int k = num_rels - 1; k >= 0; --k)
193 {
194 stk::mesh::Entity current_entity = rel_ents[k];
195 size_t side_ordinal = rel_ords[k];
196
197 if (mesh.in_receive_ghost(mesh.entity_key(current_entity))) {
198 // TODO deleteing the ghost triggers a logic error at the
199 // end of the NEXT modification cycle. We need to fix this!
200 //mesh.destroy_entity(current_entity);
201 continue;
202 }
203 else if ( std::binary_search(closure.begin(),
204 //has relation in closure
205 closure.end(),
206 current_entity,
207 stk::mesh::EntityLess(mesh)) )
208 {
209 sides.push_back(stk::mesh::EntitySideComponent(current_entity,side_ordinal));
210 }
211 }
212 }
213
214 //loop over the sides and break the relations between the old nodes
215 //and set up the relations with the new
216 for ( std::vector<stk::mesh::EntitySideComponent>::iterator itr = sides.begin();
217 itr != sides.end(); ++itr)
218 {
219 mesh.destroy_relation(itr->entity, entity, itr->side_ordinal);
220 mesh.declare_relation(itr->entity, new_entity, itr->side_ordinal);
221 }
222
223 //copy non-induced part membership from nodes[i] to new_nodes[i]
224 //there are NO non-induced parts for this example
225
226 //copy field data from nodes[i] to new_nodes[i]
227 mesh.copy_entity_fields( entity, new_entity);
228
229 if (mesh.has_no_relations(entity)) {
230 mesh.destroy_entity(entity);
231 }
232
233 if (mesh.has_no_relations(new_entity)) {
234 mesh.destroy_entity(new_entity);
235 }
236 }
237 }
238
239 void communicate_and_create_shared_nodes( stk::mesh::BulkData & mesh,
240 stk::mesh::EntityVector & nodes,
241 stk::mesh::EntityVector & new_nodes)
242 {
243
244
245 stk::CommSparse comm(mesh.parallel());
246
247 for (size_t i = 0; i < nodes.size(); ++i) {
248 stk::mesh::Entity node = nodes[i];
249 stk::mesh::Entity new_node = new_nodes[i];
250
251 std::vector<int> shared_procs;
252 mesh.comm_shared_procs(mesh.entity_key(node),shared_procs);
253
254 for (size_t shared_procs_i=0 ; shared_procs_i<shared_procs.size() ; ++shared_procs_i) {
255
256 size_t proc = shared_procs[shared_procs_i];
257 comm.send_buffer(proc).pack<stk::mesh::EntityKey>(mesh.entity_key(node))
258 .pack<stk::mesh::EntityKey>(mesh.entity_key(new_node));
259
260 }
261 }
262
263 comm.allocate_buffers();
264
265 for (size_t i = 0; i < nodes.size(); ++i) {
266 stk::mesh::Entity node = nodes[i];
267 stk::mesh::Entity new_node = new_nodes[i];
268
269 std::vector<int> shared_procs;
270 mesh.comm_shared_procs(mesh.entity_key(node),shared_procs);
271
272 for (size_t shared_procs_i=0 ; shared_procs_i<shared_procs.size() ; ++shared_procs_i) {
273
274 size_t proc = shared_procs[shared_procs_i];
275 comm.send_buffer(proc).pack<stk::mesh::EntityKey>(mesh.entity_key(node))
276 .pack<stk::mesh::EntityKey>(mesh.entity_key(new_node));
277
278 }
279 }
280
281 comm.communicate();
282
283 const stk::mesh::PartVector no_parts;
284
285 for (int process = 0; process < mesh.parallel_size(); ++process) {
286 stk::mesh::EntityKey old_key;
287 stk::mesh::EntityKey new_key;
288
289 while ( comm.recv_buffer(process).remaining()) {
290
291 comm.recv_buffer(process).unpack<stk::mesh::EntityKey>(old_key)
292 .unpack<stk::mesh::EntityKey>(new_key);
293
294 stk::mesh::Entity old_entity = mesh.get_entity(old_key);
295
296 EXPECT_EQ(stk::topology::NODE_RANK, new_key.rank());
297 stk::mesh::Entity new_entity = mesh.declare_node(new_key.id(), no_parts);
298
299 nodes.push_back(old_entity);
300 new_nodes.push_back(new_entity);
301
302 }
303 }
304 }
305
306 void separate_and_skin_mesh(
307 stk::mesh::MetaData & fem_meta,
308 stk::mesh::BulkData & mesh,
309 stk::mesh::Part & /*skin_part*/,
310 stk::mesh::EntityVector & entities_to_separate
311 )
312 {
313
314 stk::mesh::EntityVector entities_closure;
315 stk::mesh::find_closure(mesh,
316 entities_to_separate,
317 entities_closure);
318
319 stk::mesh::Selector select_owned = fem_meta.locally_owned_part();
320
321 stk::mesh::EntityVector nodes;
322 find_owned_nodes_with_relations_outside_closure( mesh, entities_closure, select_owned, nodes);
323
324 //ask for new nodes to represent the copies
325 std::vector<size_t> requests(fem_meta.entity_rank_count(), 0);
326 requests[NODE_RANK] = nodes.size();
327
328 mesh.modification_begin();
329
330 // generate_new_entities creates new blank entities of the requested ranks
331 stk::mesh::EntityVector new_nodes;
332 mesh.generate_new_entities(requests, new_nodes);
333
334 //communicate and create new nodes everywhere the old node is shared
335 communicate_and_create_shared_nodes(mesh, nodes, new_nodes);
336
337 copy_nodes_and_break_relations(mesh, entities_closure, nodes, new_nodes);
338
339 mesh.modification_end();
340
341
342 return;
343 }
344
345 }//end unnamped namespace
346
347 // \TODO Idea: ADD scaling test over mesh size and compute the slope.
348 // \TODO Idea: ADD different partitioning such that the reskinning spans more than one process.
349
350 TEST( skinning_large_cube, skinning_large_cube)
351 {
352 stk::ParallelMachine pm = MPI_COMM_WORLD;
353
354 const size_t p_size = stk::parallel_machine_size(pm);
355 const size_t p_rank = stk::parallel_machine_rank(pm);
356
357 // Every processor will be involved in detachment and skin-update up to 500 processors.
358 // This puts 5000 elements on each process unless we are running with STL
359 // in debug mode in which case we shrink the problem down in order
360 // to keep things running in a reasonable amount of time.
361 #ifdef _GLIBCXX_DEBUG
362 const size_t NX = p_size*10, NY = 4, NZ = 5;
363 #else
364 const size_t NX = p_size*100, NY = 100, NZ = 100;
365 #endif
366
367 static const int TIMER_COUNT = 6;
368 /* timings[0] = create mesh
369 * timings[1] = intial skin mesh
370 * timings[2] = detach mesh
371 * timings[3] = delete skin
372 * timings[4] = reskin mesh
373 * timings[5] = sum(timings[0:4])
374 */
375 double timings[TIMER_COUNT] = {0};
376 double timing_sums[TIMER_COUNT] = {0};
377 const char* timer_names[TIMER_COUNT] = {"Create mesh", "Initial skin", "Detach mesh", "Delete skin", "Reskin", "Total time"};
378 double start_time = 0;
379 size_t memory_max[2] = {0};
380 const char* memory_names[2] = {"Current memory", "Memory high water"};
381
382 //recreate skin
383 for ( int test_run = 0; test_run < 4; ++test_run) {
384 //create the mesh
385
386 start_time = stk::wall_time();
387 stk::mesh::fixtures::HexFixture fixture(pm,NX,NY,NZ);
388 const EntityRank element_rank = stk::topology::ELEMENT_RANK;
389 const EntityRank side_rank = fixture.m_meta.side_rank();
390
391 stk::mesh::MetaData & fem_meta = fixture.m_meta;
392 stk::mesh::BulkData & mesh = fixture.m_bulk_data;
393
394 stk::mesh::Part & skin_part = fem_meta.declare_part("skin_part");
395 fem_meta.commit();
396
397 fixture.generate_mesh();
398 timings[0] = stk::wall_dtime(start_time);
399
400 //intial skin of the mesh
401 start_time = stk::wall_time();
402 {
403 stk::mesh::PartVector add_parts(1,&skin_part);
404 stk::mesh::skin_mesh(mesh, add_parts);
405 }
406 timings[1] = stk::wall_dtime(start_time);
407
408 stk::mesh::EntityVector entities_to_separate;
409
410 if ( test_run < 2) {
411 //detach 1/3 of the mesh
412 size_t num_detached_this_proc = 0;
413 for (size_t ix=NX/3; ix < 2*NX/3; ++ix) {
414 for (size_t iy=0; iy < NY; ++iy) {
415 for (size_t iz=0; iz < NZ; ++iz) {
416 stk::mesh::Entity element = fixture.elem(ix,iy,iz);
417 if (mesh.is_valid(element) && mesh.parallel_owner_rank(element) == mesh.parallel_rank()) {
418 entities_to_separate.push_back(element);
419 num_detached_this_proc++;
420 }
421 }
422 }
423 }
424 EXPECT_TRUE( num_detached_this_proc > 0u );
425 } else {
426 //detach middle of the mesh
427 for (size_t ix=NX/2; ix < NX/2+1; ++ix) {
428 for (size_t iy=NY/2; iy < NY/2+1; ++iy) {
429 for (size_t iz=NZ/2; iz < NZ/2+1; ++iz) {
430 stk::mesh::Entity element = fixture.elem(ix,iy,iz);
431 if (mesh.is_valid(element) && mesh.parallel_owner_rank(element) == mesh.parallel_rank()) {
432 entities_to_separate.push_back(element);
433 }
434 }
435 }
436 }
437 }
438
439 start_time = stk::wall_time();
440 separate_and_skin_mesh(
441 fem_meta,
442 mesh,
443 skin_part,
444 entities_to_separate
445 );
446 timings[2] = stk::wall_dtime(start_time);
447
448 if (test_run%2 == 0) { // delete the skin
449 start_time = stk::wall_time();
450 delete_skin( mesh, skin_part, side_rank );
451 timings[3] = stk::wall_dtime(start_time);
452
453 //reskin the entire mesh
454 start_time = stk::wall_time();
455 {
456 stk::mesh::PartVector add_parts(1,&skin_part);
457 stk::mesh::skin_mesh( mesh, add_parts);
458 }
459 timings[4] = stk::wall_dtime(start_time);
460 }
461 else { //update the skin
462 timings[3] = 0;
463
464 //update the skin of the mesh
465 start_time = stk::wall_time();
466 update_skin( mesh, &skin_part, element_rank);
467 timings[4] = stk::wall_dtime(start_time);
468 }
469
470 //total the timings
471 timings[5] = 0;
472 for (int i=0; i <5; ++i) {
473 timings[5] += timings[i];
474 }
475
476 size_t mem_now = 0, mem_hwm = 0;
477 stk::get_memory_usage(mem_now, mem_hwm);
478
479 stk::all_reduce(pm, stk::ReduceMax<5>(timings));
480 stk::all_reduce(pm, stk::ReduceMax<1>(&mem_now));
481 stk::all_reduce(pm, stk::ReduceMax<1>(&mem_hwm));
482
483 if (mem_now > memory_max[0]) {
484 memory_max[0] = mem_now;
485 }
486 if (mem_hwm > memory_max[1]) {
487 memory_max[1] = mem_hwm;
488 }
489
490 //compute sums
491 for (int i=0; i<TIMER_COUNT; ++i) {
492 timing_sums[i] += timings[i];
493 }
494
495 if (p_rank == 0) {
496 std::cout << "\n\n";
497 switch (test_run) {
498 case 0:
499 std::cout << "Recreate entire skin after detaching 1/3 of the mesh:\n";
500 break;
501 case 1:
502 std::cout << "Update skin after detaching 1/3 of the mesh:\n";
503 break;
504 case 2:
505 std::cout << "Recreate entire skin after detaching middle of the mesh:\n";
506 break;
507 case 3:
508 std::cout << "Update skin after detaching middle of the mesh:\n";
509 break;
510 }
511
512 std::cout << "Num procs: " << p_size << "\n";
513 std::cout << "Mesh size: " << NX << 'x' << NY << 'x' << NZ << " = " << NX*NY*NZ << " elements\n";
514 std::cout << "Total time: " << timings[5] << "\n";
515 std::cout << "\tCreate mesh: " << timings[0] << "\n";
516 std::cout << "\tInitial skin: " << timings[1] << "\n";
517 std::cout << "\tDetach mesh: " << timings[2] << "\n";
518 std::cout << "\tDelete skin: " << timings[3] << "\n";
519 std::cout << "\tReskin: " << timings[4] << "\n";
520 std::cout << "\n\n";
521 }
522
523 size_t num_skin_entities = count_skin_entities(mesh, skin_part, side_rank );
524
525 stk::all_reduce(pm, stk::ReduceSum<1>(&num_skin_entities));
526
527 size_t expected_num_skin = 0;
528
529 if ( test_run < 2) {
530 expected_num_skin = 2*(NX*NY + NX*NZ + 3*NY*NZ);
531 } else {
532 expected_num_skin = 2*(NX*NY + NX*NZ + NY*NZ) + 12;
533 }
534
535 EXPECT_EQ( num_skin_entities, expected_num_skin );
536 }
537
538 if (p_rank == 0) {
539 stk::print_timers_and_memory(&timer_names[0], &timing_sums[0], TIMER_COUNT, &memory_names[0], &memory_max[0], 2);
540 }
541
542 stk::parallel_print_time_without_output_and_hwm(pm, timings[5]);
543 }
544
545 double run_skinning_large_cube_test(bool createEdges, unsigned numRuns, std::vector<size_t>& dims)
546 {
547 stk::ParallelMachine pm = MPI_COMM_WORLD;
548
549 EXPECT_TRUE(dims.size() >= 3);
550 const size_t NX = dims[0], NY = dims[1], NZ = dims[2];
551
552 double skinningTime = 0.0;
553
554 for (unsigned testRun = 0; testRun < numRuns; ++testRun) {
555 stk::mesh::fixtures::HexFixture fixture(pm,NX,NY,NZ);
556
557 stk::mesh::MetaData & fem_meta = fixture.m_meta;
558
559 stk::mesh::Part & skin_part = fem_meta.declare_part("skin_part");
560 fem_meta.commit();
561
562 fixture.generate_mesh();
563
564 if(createEdges) {
565 stk::mesh::create_edges(fixture.m_bulk_data);
566 }
567
568 stk::mesh::PartVector add_parts(1,&skin_part);
569 double startTime = stk::wall_time();
570 stk::mesh::skin_mesh(fixture.m_bulk_data, add_parts);
571 skinningTime += stk::wall_dtime(startTime);
572 }
573
574 return skinningTime;
575 }
576
577 TEST(skinning_large_cube_perf_test, skinning_large_cube)
578 {
579 std::vector<size_t> dims = {50, 50, 50};
580 const unsigned NUM_RUNS = 5;
581 const unsigned NUM_ITERS = 5;
582 stk::unit_test_util::BatchTimer batchTimer(MPI_COMM_WORLD);
583 batchTimer.initialize_batch_timer();
584 for (unsigned j = 0; j < NUM_RUNS; j++) {
585 batchTimer.start_batch_timer();
586 run_skinning_large_cube_test(true, NUM_ITERS, dims);
587 batchTimer.stop_batch_timer();
588 }
589 batchTimer.print_batch_timing(NUM_ITERS);
590
591 }