Debian Codegraph @

packages/stk/stk_performance_tests/stk_mesh/ParallelSum.cpp

raw
1 /*--------------------------------------------------------------------*/
2 /* Copyright 2009, 2011 Sandia Corporation. */
3 /* Under the terms of Contract DE-AC04-94AL85000, there is a */
4 /* non-exclusive license for use of this work by or on behalf */
5 /* of the U.S. Government. Export of this program may require */
6 /* a license from the United States Government. */
7 /*--------------------------------------------------------------------*/
8
9 // ####################### Start Clang Header Tool Managed Headers ########################
10 // clang-format off
11 #include <gtest/gtest.h>
12 #include <stk_util/parallel/Parallel.hpp> // for parallel_machine_rank
13 #include <stk_util/parallel/ParallelComm.hpp> // for parallel_data_exchange...
14 #include "stk_mesh/base/Types.hpp" // for BucketVector, EntityPr...
15 #include <stk_mesh/base/Bucket.hpp> // for Bucket
16 #include <stk_mesh/base/BulkData.hpp> // for BulkData, BulkData::NO...
17 #include <stk_mesh/base/MeshBuilder.hpp>
18 #include <stk_mesh/base/FieldBase.hpp> // for field_data, FieldBase
19 #include <stk_mesh/base/FieldParallel.hpp> // for parallel_sum, parallel...
20 #include <stk_mesh/base/NgpFieldParallel.hpp>
21 #include <stk_mesh/base/GetNgpMesh.hpp>
22 #include <stk_mesh/base/GetNgpField.hpp>
23 #include <stk_mesh/base/GetEntities.hpp> // for count_selected_entities
24 #include <stk_mesh/base/Part.hpp> // for Part
25 #include <stk_mesh/base/Selector.hpp> // for Selector, operator|
26 #include <stk_topology/topology.hpp> // for topology, topology::NO...
27 #include <stk_util/environment/CPUTime.hpp> // for cpu_time
28 #include "stk_io/FillMesh.hpp" // for fill_mesh
29 #include "stk_mesh/base/Entity.hpp" // for Entity
30 #include "stk_mesh/base/Field.hpp" // for Field
31 #include "stk_mesh/base/MetaData.hpp" // for MetaData, put_field_on...
32 #include "stk_util/environment/Env.hpp" // for parallel_rank, paralle...
33 #include "stk_util/environment/perf_util.hpp" // for get_max_hwm_across_procs
34 #include <cstddef> // for size_t
35 #include <algorithm> // for fill
36 #include <iostream> // for operator<<, basic_ostr...
37 #include <string> // for operator<<, char_traits
38 #include <vector> // for vector
39 namespace stk { namespace mesh { class Ghosting; } }
40 // clang-format on
41 // ####################### End Clang Header Tool Managed Headers ########################
42
43 namespace STKperf {
44
45 static const int X_DIM = 800; //num_elems_x
46 static const int Y_DIM = 800; //num_elems_y
47
48 static const int NUM_FIELDS = 120;
49
50 static const int NUM_ITERS = 40;
51
52 stk::mesh::EntityId node_id( unsigned x , unsigned y , unsigned z, unsigned nx, unsigned ny )
53 {
54 return 1 + x + ( nx + 1 ) * ( y + ( ny + 1 ) * z );
55 }
56
57 void do_stk_test(bool with_ghosts=false, bool device_mpi=false)
58 {
59 using namespace stk::mesh;
60
61 typedef Field<double> ScalarField;
62
63 stk::ParallelMachine pm = MPI_COMM_WORLD;
64 const int parallel_size = stk::parallel_machine_size(pm);
65 const int parallel_rank = stk::parallel_machine_rank(pm);
66 if (parallel_size < 3)
67 {
68 if (parallel_rank == 0)
69 {
70 std::cerr << "returning, this test only runs on 3 or more processors." << std::endl;
71 }
72 return;
73 }
74
75 const int xdim = X_DIM;
76 const int ydim = Y_DIM;
77 const int zdim = parallel_size*2;
78 const int numFields = NUM_FIELDS;
79 const int numIters = NUM_ITERS;
80
81 std::ostringstream oss;
82 oss << "generated:" << xdim << "x" << ydim << "x" << zdim;
83
84 stk::mesh::MeshBuilder builder(pm);
85 unsigned spatialDim = 3;
86 builder.set_spatial_dimension(spatialDim);
87 builder.set_aura_option(stk::mesh::BulkData::NO_AUTO_AURA);
88 std::shared_ptr<stk::mesh::BulkData> bulkPtr = builder.create();
89 stk::mesh::MetaData& meta = bulkPtr->mesh_meta_data();
90 stk::mesh::BulkData& bulk = *bulkPtr;
91 if (parallel_rank == 0)
92 {
93 std::cerr << "Mesh: " << oss.str() << std::endl;
94 }
95
96 std::vector<const FieldBase*> fields(numFields);
97 for (int i = 0; i < numFields; ++i) {
98 std::ostringstream oss2;
99 oss2 << "field_" << i;
100 FieldBase* field = &meta.declare_field<double>(stk::topology::NODE_RANK, oss2.str());
101 fields[i] = field;
102 stk::mesh::put_field_on_mesh(*field, meta.universal_part(), nullptr);
103 }
104
105 PartVector hex_topo(1, &meta.declare_part_with_topology("hex_part", stk::topology::HEX_8));
106
107 stk::io::fill_mesh(oss.str(),bulk);
108
109 if (parallel_rank == 0) {
110 std::cout << "STK mesh constructed" << std::endl;
111 }
112
113 int last_proc = parallel_size - 1;
114 int next_to_last_proc = last_proc - 1;
115
116 stk::mesh::Selector shared_sel = meta.globally_shared_part();
117 stk::mesh::Selector communicated_nodes = shared_sel;
118 unsigned num_ghost_nodes = 0;
119
120 if (with_ghosts)
121 {
122 bulk.modification_begin();
123 stk::mesh::Ghosting& node_ghosting = bulk.create_ghosting("node ghosting");
124 bulk.modification_end();
125
126 stk::mesh::EntityProcVec nodes_to_ghost;
127
128
129 stk::mesh::Selector send_ghost_selector;
130 if (parallel_rank == 0)
131 {
132 send_ghost_selector = meta.locally_owned_part() & !shared_sel;
133 const stk::mesh::BucketVector& node_buckets = bulk.get_buckets(stk::topology::NODE_RANK, send_ghost_selector);
134 for(const stk::mesh::Bucket* bucket : node_buckets)
135 {
136 for(stk::mesh::Entity node : *bucket)
137 {
138 nodes_to_ghost.push_back(stk::mesh::EntityProc(node, last_proc));
139 nodes_to_ghost.push_back(stk::mesh::EntityProc(node, next_to_last_proc));
140 }
141 }
142 }
143
144 bulk.batch_add_to_ghosting(node_ghosting, nodes_to_ghost);
145
146 num_ghost_nodes = nodes_to_ghost.size()/2; //because each node is going to 2 destination procs
147 if (parallel_rank == last_proc)
148 {
149 num_ghost_nodes = stk::mesh::count_selected_entities(bulk.ghosting_part(node_ghosting), bulk.buckets(stk::topology::NODE_RANK));
150 EXPECT_GT(num_ghost_nodes, 0u);
151 }
152
153 communicated_nodes = send_ghost_selector | bulk.ghosting_part(node_ghosting) | shared_sel;
154
155 if (parallel_rank != 0)
156 {
157 num_ghost_nodes = stk::mesh::count_selected_entities(bulk.ghosting_part(node_ghosting), bulk.buckets(stk::topology::NODE_RANK));
158 }
159
160 if (parallel_rank == 0) {
161 std::cout << "Ghosts added to mesh" << std::endl;
162 }
163 }
164
165 // populate field data
166 stk::mesh::BucketVector const& node_buckets = bulk.get_buckets(stk::topology::NODE_RANK, communicated_nodes);
167 for (int b = 0, be = node_buckets.size(); b < be; ++b) {
168 stk::mesh::Bucket const& bucket = *node_buckets[b];
169 for (int i = 0; i < numFields; ++i) {
170 const ScalarField& field = dynamic_cast<const ScalarField&>(*fields[i]);
171 double* data = stk::mesh::field_data(field, bucket);
172 for (int n = 0, ne = bucket.size(); n < ne; ++n) {
173 data[n] = static_cast<double>(i+1);
174 }
175 }
176 }
177
178 MPI_Barrier(pm);
179
180 double start_time = stk::cpu_time();
181 NgpMesh* ngpMesh = nullptr;
182 if (device_mpi) {
183 ngpMesh = & stk::mesh::get_updated_ngp_mesh(bulk);
184 }
185
186 std::vector<NgpField<double>*> ngpFields(numFields);
187 if (device_mpi) {
188 for (int i = 0; i < numFields; ++i) {
189 ngpFields[i] = &stk::mesh::get_updated_ngp_field<double>(*fields[i]);
190 }
191 }
192
193 for (int t = 0; t < numIters; ++t) {
194 if (with_ghosts) {
195 STK_ThrowRequireMsg(!device_mpi, "NGP parallel_sum_including_ghosts not implemented yet.");
196 stk::mesh::parallel_sum_including_ghosts(bulk, fields);
197 }
198 else {
199 if (device_mpi) {
200 stk::mesh::parallel_sum(*ngpMesh, ngpFields);
201 }
202 else {
203 stk::mesh::parallel_sum(bulk, fields);
204
205 for (int i = 0; i < numFields; ++i) {
206 ngpFields[i] = &stk::mesh::get_updated_ngp_field<double>(*fields[i]);
207 }
208 }
209 }
210 }
211
212 double stk_sum_time = stk::cpu_time() - start_time;
213
214 double max_time;
215 MPI_Reduce(static_cast<void*>(&stk_sum_time), static_cast<void*>(&max_time), 1, MPI_DOUBLE, MPI_MAX, 0 /*root*/, MPI_COMM_WORLD);
216
217 double power2 = std::pow(2,numIters);
218 double power3 = std::pow(3,numIters);
219 const double tolerance = 1.e-8;
220
221 if (device_mpi) {
222 for (int i = 0; i < numFields; ++i) {
223 ngpFields[i]->sync_to_host();
224 }
225 }
226
227 // Sanity check
228 size_t num_comm_nodes = 0;
229 for (int b = 0, be = node_buckets.size(); b < be; ++b) {
230 stk::mesh::Bucket const& bucket = *node_buckets[b];
231 const bool isShared = bucket.shared();
232 num_comm_nodes += bucket.size();
233 for (int f = 0; f < numFields; ++f) {
234 const ScalarField& field = dynamic_cast<const ScalarField&>(*fields[f]);
235 const double* stk_data = stk::mesh::field_data(field, bucket);
236 const double expected_shared_value = static_cast<double>(f+1) * power2;
237 const double expected_ghosted_value = static_cast<double>(f+1) * power3;
238 const double expected = isShared ? expected_shared_value : expected_ghosted_value;
239 for (int n = 0, ne = bucket.size(); n < ne; ++n) {
240 const double relativeError = std::abs(stk_data[n] - expected) / expected;
241 EXPECT_NEAR(0.0, relativeError, tolerance)<<"node "<<bulk.identifier(bucket[n])<<", expected="<<expected<<", stk_data="<<stk_data[n];
242 }
243 }
244 }
245
246 size_t expected_num_shared_nodes = (xdim+1) * (ydim+1);
247 if (parallel_rank != 0 && parallel_rank != last_proc)
248 {
249 expected_num_shared_nodes += (xdim+1) * (ydim+1);
250 }
251 if (parallel_rank == 0 || parallel_rank == last_proc || parallel_rank == next_to_last_proc)
252 {
253 expected_num_shared_nodes += num_ghost_nodes;
254 }
255 EXPECT_EQ(expected_num_shared_nodes, num_comm_nodes);
256
257 size_t hwm_max = stk::get_max_hwm_across_procs(pm);
258
259 stk::print_stats_for_performance_compare(std::cout, max_time, hwm_max, numIters, pm);
260 }
261
262 TEST(ParallelDataExchange, test_nonsym_known_sizes_from_proc0_to_all_other_procs)
263 {
264 stk::ParallelMachine comm = MPI_COMM_WORLD;
265 int num_procs = stk::parallel_machine_size(comm);
266 if (num_procs >= 2) {
267 int this_proc = stk::parallel_machine_rank(comm);
268
269 std::vector<int> sendOffsets(num_procs+1);
270 std::vector<int> recvOffsets(num_procs+1);
271 std::vector<double> send_data(num_procs-1);
272 std::vector<double> recv_data(1);
273 if (this_proc == 0) {
274 sendOffsets[0] = 0;
275 sendOffsets[1] = 0; // Don't send anything to self
276 for (int iproc = 1; iproc < num_procs; ++iproc) {
277 sendOffsets[iproc+1] = sendOffsets[iproc]+1;
278 }
279
280 std::fill(send_data.begin(), send_data.end(), this_proc+1);
281 }
282 else {
283 std::fill(recvOffsets.begin(), recvOffsets.end(), 1);
284 recvOffsets[0] = 0; // Only receive from p0
285 }
286
287 stk::parallel_data_exchange_nonsym_known_sizes_t(sendOffsets.data(), send_data.data(),
288 recvOffsets.data(), recv_data.data(), comm );
289
290 if (this_proc == 0) {
291 for (int iproc = 0; iproc < num_procs; ++iproc) {
292 const int recvSizeFromProc = recvOffsets[iproc+1]-recvOffsets[iproc];
293 EXPECT_EQ(0, recvSizeFromProc);
294 }
295 }
296 else {
297 const int recvSizeFromProc0 = recvOffsets[1]-recvOffsets[0];
298 EXPECT_EQ(1, recvSizeFromProc0);
299 EXPECT_DOUBLE_EQ(1.0, recv_data[recvOffsets[0]]);
300 for (int iproc = 1; iproc < num_procs; ++iproc) {
301 const int recvSizeFromProc = recvOffsets[iproc+1]-recvOffsets[iproc];
302 EXPECT_EQ(0, recvSizeFromProc);
303 }
304 }
305 }
306 }
307
308 TEST(ParallelDataExchange, test_nonsym_known_sizes_from_all_other_procs_to_proc0)
309 {
310 stk::ParallelMachine comm = MPI_COMM_WORLD;
311 int num_procs = stk::parallel_machine_size(comm);
312 if (num_procs >= 2) {
313 int this_proc = stk::parallel_machine_rank(comm);
314
315 std::vector<int> sendOffsets(num_procs+1);
316 std::vector<int> recvOffsets(num_procs+1);
317 std::vector<double> send_data(1);
318 std::vector<double> recv_data(num_procs-1);
319 if (this_proc == 0) {
320 recvOffsets[0] = 0;
321 recvOffsets[1] = 0; // Don't receive from self
322 for (int iproc = 1; iproc < num_procs; ++iproc) {
323 recvOffsets[iproc+1] = recvOffsets[iproc]+1;
324 }
325 }
326 else {
327 std::fill(sendOffsets.begin(), sendOffsets.end(), 1);
328 sendOffsets[0] = 0; // Only send to p0
329
330 send_data[0] = static_cast<double>(this_proc+1);
331 }
332
333 stk::parallel_data_exchange_nonsym_known_sizes_t(sendOffsets.data(), send_data.data(),
334 recvOffsets.data(), recv_data.data(), comm );
335
336 if (this_proc == 0) {
337 const int recvSizeFromProc0 = recvOffsets[1]-recvOffsets[0];
338 EXPECT_EQ(0, recvSizeFromProc0);
339 for (int iproc = 1; iproc < num_procs; ++iproc) {
340 const int recvSizeFromProc = recvOffsets[iproc+1]-recvOffsets[iproc];
341 EXPECT_EQ(1, recvSizeFromProc);
342 double expected_recv_value = iproc+1;
343 EXPECT_EQ(expected_recv_value, recv_data[recvOffsets[iproc]]);
344 }
345 }
346 else {
347 for (int iproc = 0; iproc < num_procs; ++iproc) {
348 const int recvSizeFromProc = recvOffsets[iproc+1]-recvOffsets[iproc];
349 EXPECT_EQ(0, recvSizeFromProc);
350 }
351 }
352 }
353 }
354
355 TEST(STKMesh_perf, parallel_sum)
356 {
357 const bool with_ghosts = false;
358 do_stk_test(with_ghosts);
359 }
360
361 TEST(STKMesh_perf, parallel_sum_device_mpi)
362 {
363 const bool with_ghosts = false;
364 const bool device_mpi = true;
365 do_stk_test(with_ghosts, device_mpi);
366 }
367
368 TEST(STKMesh_perf, parallel_sum_including_ghosts)
369 {
370 const bool with_ghosts = true;
371 do_stk_test(with_ghosts);
372 }
373
374 } //namespace STKperf