/*--------------------------------------------------------------------*/ /* Copyright 2009, 2011 Sandia Corporation. */ /* Under the terms of Contract DE-AC04-94AL85000, there is a */ /* non-exclusive license for use of this work by or on behalf */ /* of the U.S. Government. Export of this program may require */ /* a license from the United States Government. */ /*--------------------------------------------------------------------*/ // ####################### Start Clang Header Tool Managed Headers ######################## // clang-format off #include #include // for parallel_machine_rank #include // for parallel_data_exchange... #include "stk_mesh/base/Types.hpp" // for BucketVector, EntityPr... #include // for Bucket #include // for BulkData, BulkData::NO... #include #include // for field_data, FieldBase #include // for parallel_sum, parallel... #include #include #include #include // for count_selected_entities #include // for Part #include // for Selector, operator| #include // for topology, topology::NO... #include // for cpu_time #include "stk_io/FillMesh.hpp" // for fill_mesh #include "stk_mesh/base/Entity.hpp" // for Entity #include "stk_mesh/base/Field.hpp" // for Field #include "stk_mesh/base/MetaData.hpp" // for MetaData, put_field_on... #include "stk_util/environment/Env.hpp" // for parallel_rank, paralle... #include "stk_util/environment/perf_util.hpp" // for get_max_hwm_across_procs #include // for size_t #include // for fill #include // for operator<<, basic_ostr... #include // for operator<<, char_traits #include // for vector namespace stk { namespace mesh { class Ghosting; } } // clang-format on // ####################### End Clang Header Tool Managed Headers ######################## namespace STKperf { static const int X_DIM = 800; //num_elems_x static const int Y_DIM = 800; //num_elems_y static const int NUM_FIELDS = 120; static const int NUM_ITERS = 40; stk::mesh::EntityId node_id( unsigned x , unsigned y , unsigned z, unsigned nx, unsigned ny ) { return 1 + x + ( nx + 1 ) * ( y + ( ny + 1 ) * z ); } void do_stk_test(bool with_ghosts=false, bool device_mpi=false) { using namespace stk::mesh; typedef Field ScalarField; stk::ParallelMachine pm = MPI_COMM_WORLD; const int parallel_size = stk::parallel_machine_size(pm); const int parallel_rank = stk::parallel_machine_rank(pm); if (parallel_size < 3) { if (parallel_rank == 0) { std::cerr << "returning, this test only runs on 3 or more processors." << std::endl; } return; } const int xdim = X_DIM; const int ydim = Y_DIM; const int zdim = parallel_size*2; const int numFields = NUM_FIELDS; const int numIters = NUM_ITERS; std::ostringstream oss; oss << "generated:" << xdim << "x" << ydim << "x" << zdim; stk::mesh::MeshBuilder builder(pm); unsigned spatialDim = 3; builder.set_spatial_dimension(spatialDim); builder.set_aura_option(stk::mesh::BulkData::NO_AUTO_AURA); std::shared_ptr bulkPtr = builder.create(); stk::mesh::MetaData& meta = bulkPtr->mesh_meta_data(); stk::mesh::BulkData& bulk = *bulkPtr; if (parallel_rank == 0) { std::cerr << "Mesh: " << oss.str() << std::endl; } std::vector fields(numFields); for (int i = 0; i < numFields; ++i) { std::ostringstream oss2; oss2 << "field_" << i; FieldBase* field = &meta.declare_field(stk::topology::NODE_RANK, oss2.str()); fields[i] = field; stk::mesh::put_field_on_mesh(*field, meta.universal_part(), nullptr); } PartVector hex_topo(1, &meta.declare_part_with_topology("hex_part", stk::topology::HEX_8)); stk::io::fill_mesh(oss.str(),bulk); if (parallel_rank == 0) { std::cout << "STK mesh constructed" << std::endl; } int last_proc = parallel_size - 1; int next_to_last_proc = last_proc - 1; stk::mesh::Selector shared_sel = meta.globally_shared_part(); stk::mesh::Selector communicated_nodes = shared_sel; unsigned num_ghost_nodes = 0; if (with_ghosts) { bulk.modification_begin(); stk::mesh::Ghosting& node_ghosting = bulk.create_ghosting("node ghosting"); bulk.modification_end(); stk::mesh::EntityProcVec nodes_to_ghost; stk::mesh::Selector send_ghost_selector; if (parallel_rank == 0) { send_ghost_selector = meta.locally_owned_part() & !shared_sel; const stk::mesh::BucketVector& node_buckets = bulk.get_buckets(stk::topology::NODE_RANK, send_ghost_selector); for(const stk::mesh::Bucket* bucket : node_buckets) { for(stk::mesh::Entity node : *bucket) { nodes_to_ghost.push_back(stk::mesh::EntityProc(node, last_proc)); nodes_to_ghost.push_back(stk::mesh::EntityProc(node, next_to_last_proc)); } } } bulk.batch_add_to_ghosting(node_ghosting, nodes_to_ghost); num_ghost_nodes = nodes_to_ghost.size()/2; //because each node is going to 2 destination procs if (parallel_rank == last_proc) { num_ghost_nodes = stk::mesh::count_selected_entities(bulk.ghosting_part(node_ghosting), bulk.buckets(stk::topology::NODE_RANK)); EXPECT_GT(num_ghost_nodes, 0u); } communicated_nodes = send_ghost_selector | bulk.ghosting_part(node_ghosting) | shared_sel; if (parallel_rank != 0) { num_ghost_nodes = stk::mesh::count_selected_entities(bulk.ghosting_part(node_ghosting), bulk.buckets(stk::topology::NODE_RANK)); } if (parallel_rank == 0) { std::cout << "Ghosts added to mesh" << std::endl; } } // populate field data stk::mesh::BucketVector const& node_buckets = bulk.get_buckets(stk::topology::NODE_RANK, communicated_nodes); for (int b = 0, be = node_buckets.size(); b < be; ++b) { stk::mesh::Bucket const& bucket = *node_buckets[b]; for (int i = 0; i < numFields; ++i) { const ScalarField& field = dynamic_cast(*fields[i]); double* data = stk::mesh::field_data(field, bucket); for (int n = 0, ne = bucket.size(); n < ne; ++n) { data[n] = static_cast(i+1); } } } MPI_Barrier(pm); double start_time = stk::cpu_time(); NgpMesh* ngpMesh = nullptr; if (device_mpi) { ngpMesh = & stk::mesh::get_updated_ngp_mesh(bulk); } std::vector*> ngpFields(numFields); if (device_mpi) { for (int i = 0; i < numFields; ++i) { ngpFields[i] = &stk::mesh::get_updated_ngp_field(*fields[i]); } } for (int t = 0; t < numIters; ++t) { if (with_ghosts) { STK_ThrowRequireMsg(!device_mpi, "NGP parallel_sum_including_ghosts not implemented yet."); stk::mesh::parallel_sum_including_ghosts(bulk, fields); } else { if (device_mpi) { stk::mesh::parallel_sum(*ngpMesh, ngpFields); } else { stk::mesh::parallel_sum(bulk, fields); for (int i = 0; i < numFields; ++i) { ngpFields[i] = &stk::mesh::get_updated_ngp_field(*fields[i]); } } } } double stk_sum_time = stk::cpu_time() - start_time; double max_time; MPI_Reduce(static_cast(&stk_sum_time), static_cast(&max_time), 1, MPI_DOUBLE, MPI_MAX, 0 /*root*/, MPI_COMM_WORLD); double power2 = std::pow(2,numIters); double power3 = std::pow(3,numIters); const double tolerance = 1.e-8; if (device_mpi) { for (int i = 0; i < numFields; ++i) { ngpFields[i]->sync_to_host(); } } // Sanity check size_t num_comm_nodes = 0; for (int b = 0, be = node_buckets.size(); b < be; ++b) { stk::mesh::Bucket const& bucket = *node_buckets[b]; const bool isShared = bucket.shared(); num_comm_nodes += bucket.size(); for (int f = 0; f < numFields; ++f) { const ScalarField& field = dynamic_cast(*fields[f]); const double* stk_data = stk::mesh::field_data(field, bucket); const double expected_shared_value = static_cast(f+1) * power2; const double expected_ghosted_value = static_cast(f+1) * power3; const double expected = isShared ? expected_shared_value : expected_ghosted_value; for (int n = 0, ne = bucket.size(); n < ne; ++n) { const double relativeError = std::abs(stk_data[n] - expected) / expected; EXPECT_NEAR(0.0, relativeError, tolerance)<<"node "<