Debian Codegraph @

packages/stk/stk_unit_tests/stk_mesh/ngp/UnitTestNgpMeshModification.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/NgpMesh.hpp>
37 #include "ngp/NgpUnitTestUtils.hpp"
38 #include "stk_mesh/base/MeshBuilder.hpp"
39 #include "stk_mesh/base/BulkData.hpp"
40 #include "stk_mesh/base/MetaData.hpp"
41 #include "stk_mesh/base/Part.hpp"
42 #include "stk_mesh/base/Types.hpp"
43 #include "stk_mesh/base/SkinMesh.hpp"
44
45 namespace
46 {
47 using ngp_unit_test_utils::check_bucket_layout;
48
49 class NgpBatchChangeEntityParts : public ::testing::Test
50 {
51 public:
52 NgpBatchChangeEntityParts()
53 {
54 }
55
56 void build_empty_mesh(unsigned initialBucketCapacity, unsigned maximumBucketCapacity)
57 {
58 stk::mesh::MeshBuilder builder(MPI_COMM_WORLD);
59 builder.set_spatial_dimension(3);
60 builder.set_initial_bucket_capacity(initialBucketCapacity);
61 builder.set_maximum_bucket_capacity(maximumBucketCapacity);
62 m_bulk = builder.create();
63 m_meta = &m_bulk->mesh_meta_data();
64 stk::mesh::get_updated_ngp_mesh(*m_bulk);
65 }
66
67 protected:
68 std::unique_ptr<stk::mesh::BulkData> m_bulk;
69 stk::mesh::MetaData * m_meta;
70 };
71
72 stk::mesh::Entity create_node(stk::mesh::BulkData& bulk, stk::mesh::EntityId nodeId,
73 const stk::mesh::PartVector& initialParts = stk::mesh::PartVector())
74 {
75 bulk.modification_begin();
76 stk::mesh::Entity newNode = bulk.declare_node(nodeId, initialParts);
77 bulk.modification_end();
78
79 return newNode;
80 }
81
82 template <typename MeshType>
83 void confirm_host_mesh_is_not_synchronized_from_device(const MeshType& ngpMesh)
84 {
85 if constexpr (std::is_same_v<MeshType, stk::mesh::DeviceMesh>) {
86 EXPECT_EQ(ngpMesh.need_sync_to_host(), true);
87 }
88 else {
89 EXPECT_EQ(ngpMesh.need_sync_to_host(), false); // If host build, HostMesh can't ever be stale
90 }
91 }
92
93 template <typename MeshType>
94 void confirm_host_mesh_is_synchronized_from_device(const MeshType& ngpMesh)
95 {
96 EXPECT_EQ(ngpMesh.need_sync_to_host(), false);
97 }
98
99 using DeviceEntitiesType = Kokkos::View<stk::mesh::Entity*, stk::ngp::MemSpace>;
100 using DevicePartOrdinalsType = Kokkos::View<stk::mesh::PartOrdinal*, stk::ngp::MemSpace>;
101
102 using HostEntitiesType = Kokkos::View<stk::mesh::Entity*, stk::ngp::HostExecSpace>;
103 using HostPartOrdinalsType = Kokkos::View<stk::mesh::PartOrdinal*, stk::ngp::HostExecSpace>;
104
105 void fill_device_views_add_remove_part_from_node(DeviceEntitiesType& entities, DevicePartOrdinalsType& addPartOrdinals,
106 DevicePartOrdinalsType& removePartOrdinals, stk::mesh::NgpMesh& ngpMesh,
107 stk::mesh::Entity node, stk::mesh::Part* addPart,
108 stk::mesh::Part* removePart)
109 {
110 const stk::mesh::BulkData& bulk = ngpMesh.get_bulk_on_host();
111 stk::mesh::EntityId nodeId = bulk.identifier(node);
112 const stk::mesh::PartOrdinal addPartOrdinal = (addPart) ? addPart->mesh_meta_data_ordinal()
113 : stk::mesh::InvalidPartOrdinal;
114 const stk::mesh::PartOrdinal removePartOrdinal = (removePart) ? removePart->mesh_meta_data_ordinal()
115 : stk::mesh::InvalidPartOrdinal;
116
117 Kokkos::parallel_for("Fill Device Views for Part Addition", stk::ngp::DeviceRangePolicy(0, 1),
118 KOKKOS_LAMBDA(size_t /*index*/) {
119 STK_NGP_ThrowRequireMsg(ngpMesh.identifier(node) == nodeId, "Unexpected node found on device");
120 entities(0) = node;
121
122 if (addPartOrdinal != stk::mesh::InvalidPartOrdinal) {
123 addPartOrdinals(0) = addPartOrdinal;
124 }
125
126 if (removePartOrdinal != stk::mesh::InvalidPartOrdinal) {
127 removePartOrdinals(0) = removePartOrdinal;
128 }
129 });
130 }
131
132
133 TEST_F(NgpBatchChangeEntityParts, addPartToNode_host)
134 {
135 if (stk::parallel_machine_size(MPI_COMM_WORLD) != 1) GTEST_SKIP();
136
137 build_empty_mesh(1, 1);
138
139 stk::mesh::Part & part1 = m_meta->declare_part_with_topology("part1", stk::topology::NODE);
140 stk::mesh::Part & part2 = m_meta->declare_part_with_topology("part2", stk::topology::NODE);
141 const unsigned nodeId = 1;
142 const stk::mesh::Entity node1 = create_node(*m_bulk, nodeId, {&part1});
143 check_bucket_layout(*m_bulk, {{{"part1"}, {nodeId}}}, stk::topology::NODE_RANK);
144
145 std::vector<stk::mesh::Entity> entities {node1};
146 std::vector<stk::mesh::Part*> addParts {&part2};
147 std::vector<stk::mesh::Part*> removeParts;
148
149 m_bulk->batch_change_entity_parts(entities, addParts, removeParts);
150
151 check_bucket_layout(*m_bulk, {{{"part1", "part2"}, {nodeId}}}, stk::topology::NODE_RANK);
152 }
153
154 TEST_F(NgpBatchChangeEntityParts, addPartToNode_ngpHost)
155 {
156 if (stk::parallel_machine_size(MPI_COMM_WORLD) != 1) GTEST_SKIP();
157
158 build_empty_mesh(1, 1);
159
160 stk::mesh::Part & part1 = m_meta->declare_part_with_topology("part1", stk::topology::NODE);
161 stk::mesh::Part & part2 = m_meta->declare_part_with_topology("part2", stk::topology::NODE);
162 const unsigned nodeId = 1;
163 const stk::mesh::Entity node1 = create_node(*m_bulk, nodeId, {&part1});
164 check_bucket_layout(*m_bulk, {{{"part1"}, {nodeId}}}, stk::topology::NODE_RANK);
165
166 HostEntitiesType entities("hostEntities", 1);
167 HostPartOrdinalsType addPartOrdinals("hostAddParts", 1);
168 HostPartOrdinalsType removePartOrdinals("hostRemoveParts", 0);
169
170 entities(0) = node1;
171 addPartOrdinals(0) = part2.mesh_meta_data_ordinal();
172
173 stk::mesh::HostMesh hostMesh(*m_bulk);
174 hostMesh.batch_change_entity_parts(entities, addPartOrdinals, removePartOrdinals);
175 confirm_host_mesh_is_synchronized_from_device(hostMesh);
176
177 hostMesh.sync_to_host();
178 confirm_host_mesh_is_synchronized_from_device(hostMesh);
179
180 check_bucket_layout(*m_bulk, {{{"part1", "part2"}, {nodeId}}}, stk::topology::NODE_RANK);
181 }
182
183 TEST_F(NgpBatchChangeEntityParts, addPartToNode_ngpDevice)
184 {
185 if (stk::parallel_machine_size(MPI_COMM_WORLD) != 1) GTEST_SKIP();
186
187 build_empty_mesh(1, 1);
188
189 stk::mesh::Part & part1 = m_meta->declare_part_with_topology("part1", stk::topology::NODE);
190 stk::mesh::Part & part2 = m_meta->declare_part_with_topology("part2", stk::topology::NODE);
191 const unsigned nodeId = 1;
192 const stk::mesh::Entity node1 = create_node(*m_bulk, nodeId, {&part1});
193 check_bucket_layout(*m_bulk, {{{"part1"}, {nodeId}}}, stk::topology::NODE_RANK);
194
195 DeviceEntitiesType entities("deviceEntities", 1);
196 DevicePartOrdinalsType addPartOrdinals("deviceAddParts", 1);
197 DevicePartOrdinalsType removePartOrdinals("deviceRemoveParts", 0);
198
199 stk::mesh::NgpMesh & ngpMesh = stk::mesh::get_updated_ngp_mesh(*m_bulk);
200 fill_device_views_add_remove_part_from_node(entities, addPartOrdinals, removePartOrdinals, ngpMesh,
201 node1, &part2, nullptr);
202
203 ngpMesh.batch_change_entity_parts(entities, addPartOrdinals, removePartOrdinals);
204 confirm_host_mesh_is_not_synchronized_from_device(ngpMesh);
205
206 ngpMesh.sync_to_host();
207 confirm_host_mesh_is_synchronized_from_device(ngpMesh);
208
209 check_bucket_layout(*m_bulk, {{{"part1", "part2"}, {nodeId}}}, stk::topology::NODE_RANK);
210 }
211
212
213 TEST_F(NgpBatchChangeEntityParts, removePartFromNode_host)
214 {
215 if (stk::parallel_machine_size(MPI_COMM_WORLD) != 1) GTEST_SKIP();
216
217 build_empty_mesh(1, 1);
218
219 stk::mesh::Part & part1 = m_meta->declare_part_with_topology("part1", stk::topology::NODE);
220 stk::mesh::Part & part2 = m_meta->declare_part_with_topology("part2", stk::topology::NODE);
221 const unsigned nodeId = 1;
222 const stk::mesh::Entity node1 = create_node(*m_bulk, nodeId, {&part1, &part2});
223 check_bucket_layout(*m_bulk, {{{"part1", "part2"}, {nodeId}}}, stk::topology::NODE_RANK);
224
225 std::vector<stk::mesh::Entity> entities {node1};
226 std::vector<stk::mesh::Part*> addParts;
227 std::vector<stk::mesh::Part*> removeParts {&part1};
228
229 m_bulk->batch_change_entity_parts(entities, addParts, removeParts);
230 stk::mesh::get_updated_ngp_mesh(*m_bulk);
231
232 check_bucket_layout(*m_bulk, {{{"part2"}, {nodeId}}}, stk::topology::NODE_RANK);
233 }
234
235 TEST_F(NgpBatchChangeEntityParts, removePartFromNode_ngpHost)
236 {
237 if (stk::parallel_machine_size(MPI_COMM_WORLD) != 1) GTEST_SKIP();
238
239 build_empty_mesh(1, 1);
240
241 stk::mesh::Part & part1 = m_meta->declare_part_with_topology("part1", stk::topology::NODE);
242 stk::mesh::Part & part2 = m_meta->declare_part_with_topology("part2", stk::topology::NODE);
243 const unsigned nodeId = 1;
244 const stk::mesh::Entity node1 = create_node(*m_bulk, nodeId, {&part1, &part2});
245 check_bucket_layout(*m_bulk, {{{"part1", "part2"}, {nodeId}}}, stk::topology::NODE_RANK);
246
247 HostEntitiesType entities("hostEntities", 1);
248 HostPartOrdinalsType addPartOrdinals("hostAddParts", 0);
249 HostPartOrdinalsType removePartOrdinals("hostRemoveParts", 1);
250
251 entities(0) = node1;
252 removePartOrdinals(0) = part1.mesh_meta_data_ordinal();
253
254 stk::mesh::HostMesh hostMesh(*m_bulk);
255 hostMesh.batch_change_entity_parts(entities, addPartOrdinals, removePartOrdinals);
256 confirm_host_mesh_is_synchronized_from_device(hostMesh);
257
258 hostMesh.sync_to_host();
259 confirm_host_mesh_is_synchronized_from_device(hostMesh);
260
261 check_bucket_layout(*m_bulk, {{{"part2"}, {nodeId}}}, stk::topology::NODE_RANK);
262 }
263
264 TEST_F(NgpBatchChangeEntityParts, removePartFromNode_ngpDevice)
265 {
266 if (stk::parallel_machine_size(MPI_COMM_WORLD) != 1) GTEST_SKIP();
267
268 build_empty_mesh(1, 1);
269
270 stk::mesh::Part & part1 = m_meta->declare_part_with_topology("part1", stk::topology::NODE);
271 stk::mesh::Part & part2 = m_meta->declare_part_with_topology("part2", stk::topology::NODE);
272 const unsigned nodeId = 1;
273 const stk::mesh::Entity node1 = create_node(*m_bulk, nodeId, {&part1, &part2});
274 check_bucket_layout(*m_bulk, {{{"part1", "part2"}, {nodeId}}}, stk::topology::NODE_RANK);
275
276 DeviceEntitiesType entities("deviceEntities", 1);
277 DevicePartOrdinalsType addPartOrdinals("deviceAddParts", 0);
278 DevicePartOrdinalsType removePartOrdinals("deviceRemoveParts", 1);
279
280 stk::mesh::NgpMesh & ngpMesh = stk::mesh::get_updated_ngp_mesh(*m_bulk);
281 fill_device_views_add_remove_part_from_node(entities, addPartOrdinals, removePartOrdinals, ngpMesh,
282 node1, nullptr, &part1);
283
284 ngpMesh.batch_change_entity_parts(entities, addPartOrdinals, removePartOrdinals);
285 confirm_host_mesh_is_not_synchronized_from_device(ngpMesh);
286
287 ngpMesh.sync_to_host();
288 confirm_host_mesh_is_synchronized_from_device(ngpMesh);
289
290 check_bucket_layout(*m_bulk, {{{"part2"}, {nodeId}}}, stk::topology::NODE_RANK);
291 }
292
293
294 TEST_F(NgpBatchChangeEntityParts, addAndRemovePartFromNode_host)
295 {
296 if (stk::parallel_machine_size(MPI_COMM_WORLD) != 1) GTEST_SKIP();
297
298 build_empty_mesh(1, 1);
299
300 stk::mesh::Part & part1 = m_meta->declare_part_with_topology("part1", stk::topology::NODE);
301 stk::mesh::Part & part2 = m_meta->declare_part_with_topology("part2", stk::topology::NODE);
302 const unsigned nodeId = 1;
303 const stk::mesh::Entity node1 = create_node(*m_bulk, nodeId, {&part1});
304 check_bucket_layout(*m_bulk, {{{"part1"}, {nodeId}}}, stk::topology::NODE_RANK);
305
306 std::vector<stk::mesh::Entity> entities {node1};
307 std::vector<stk::mesh::Part*> addParts {&part2};
308 std::vector<stk::mesh::Part*> removeParts {&part1};
309
310 m_bulk->batch_change_entity_parts(entities, addParts, removeParts);
311 stk::mesh::get_updated_ngp_mesh(*m_bulk);
312
313 check_bucket_layout(*m_bulk, {{{"part2"}, {nodeId}}}, stk::topology::NODE_RANK);
314 }
315
316 TEST_F(NgpBatchChangeEntityParts, addAndRemovePartFromNode_ngpHost)
317 {
318 if (stk::parallel_machine_size(MPI_COMM_WORLD) != 1) GTEST_SKIP();
319
320 build_empty_mesh(1, 1);
321
322 stk::mesh::Part & part1 = m_meta->declare_part_with_topology("part1", stk::topology::NODE);
323 stk::mesh::Part & part2 = m_meta->declare_part_with_topology("part2", stk::topology::NODE);
324 const unsigned nodeId = 1;
325 const stk::mesh::Entity node1 = create_node(*m_bulk, nodeId, {&part1});
326 check_bucket_layout(*m_bulk, {{{"part1"}, {nodeId}}}, stk::topology::NODE_RANK);
327
328 HostEntitiesType entities("hostEntities", 1);
329 HostPartOrdinalsType addPartOrdinals("hostAddParts", 1);
330 HostPartOrdinalsType removePartOrdinals("hostRemoveParts", 1);
331
332 entities(0) = node1;
333 addPartOrdinals(0) = part2.mesh_meta_data_ordinal();
334 removePartOrdinals(0) = part1.mesh_meta_data_ordinal();
335
336 stk::mesh::HostMesh hostMesh(*m_bulk);
337 hostMesh.batch_change_entity_parts(entities, addPartOrdinals, removePartOrdinals);
338 confirm_host_mesh_is_synchronized_from_device(hostMesh);
339
340 hostMesh.sync_to_host();
341 confirm_host_mesh_is_synchronized_from_device(hostMesh);
342
343 check_bucket_layout(*m_bulk, {{{"part2"}, {nodeId}}}, stk::topology::NODE_RANK);
344 }
345
346 TEST_F(NgpBatchChangeEntityParts, addAndRemovePartFromNode_ngpDevice)
347 {
348 if (stk::parallel_machine_size(MPI_COMM_WORLD) != 1) GTEST_SKIP();
349
350 build_empty_mesh(1, 1);
351
352 stk::mesh::Part & part1 = m_meta->declare_part_with_topology("part1", stk::topology::NODE);
353 stk::mesh::Part & part2 = m_meta->declare_part_with_topology("part2", stk::topology::NODE);
354 const unsigned nodeId = 1;
355 const stk::mesh::Entity node1 = create_node(*m_bulk, nodeId, {&part1});
356 check_bucket_layout(*m_bulk, {{{"part1"}, {nodeId}}}, stk::topology::NODE_RANK);
357
358 DeviceEntitiesType entities("deviceEntities", 1);
359 DevicePartOrdinalsType addPartOrdinals("deviceAddParts", 1);
360 DevicePartOrdinalsType removePartOrdinals("deviceRemoveParts", 1);
361
362 stk::mesh::NgpMesh & ngpMesh = stk::mesh::get_updated_ngp_mesh(*m_bulk);
363 fill_device_views_add_remove_part_from_node(entities, addPartOrdinals, removePartOrdinals, ngpMesh,
364 node1, &part2, &part1);
365
366 ngpMesh.batch_change_entity_parts(entities, addPartOrdinals, removePartOrdinals);
367 confirm_host_mesh_is_not_synchronized_from_device(ngpMesh);
368
369 ngpMesh.sync_to_host();
370 confirm_host_mesh_is_synchronized_from_device(ngpMesh);
371
372 check_bucket_layout(*m_bulk, {{{"part2"}, {nodeId}}}, stk::topology::NODE_RANK);
373 }
374
375 TEST_F(NgpBatchChangeEntityParts, multipleDeviceMeshMods)
376 {
377 if (stk::parallel_machine_size(MPI_COMM_WORLD) != 1) GTEST_SKIP();
378
379 build_empty_mesh(1, 1);
380
381 stk::mesh::Part & part1 = m_meta->declare_part_with_topology("part1", stk::topology::NODE);
382 stk::mesh::Part & part2 = m_meta->declare_part_with_topology("part2", stk::topology::NODE);
383 stk::mesh::Part & part3 = m_meta->declare_part_with_topology("part3", stk::topology::NODE);
384 const unsigned nodeId = 1;
385 const stk::mesh::Entity node1 = create_node(*m_bulk, nodeId, {&part1});
386 check_bucket_layout(*m_bulk, {{{"part1"}, {nodeId}}}, stk::topology::NODE_RANK);
387
388 DeviceEntitiesType entities("deviceEntities", 1);
389 DevicePartOrdinalsType addPartOrdinals("deviceAddParts", 1);
390 DevicePartOrdinalsType removePartOrdinals("deviceRemoveParts", 0);
391
392 stk::mesh::NgpMesh & ngpMesh = stk::mesh::get_updated_ngp_mesh(*m_bulk);
393 fill_device_views_add_remove_part_from_node(entities, addPartOrdinals, removePartOrdinals, ngpMesh,
394 node1, &part2, nullptr);
395
396 ngpMesh.batch_change_entity_parts(entities, addPartOrdinals, removePartOrdinals);
397 confirm_host_mesh_is_not_synchronized_from_device(ngpMesh);
398
399 fill_device_views_add_remove_part_from_node(entities, addPartOrdinals, removePartOrdinals, ngpMesh,
400 node1, &part3, nullptr);
401
402 ngpMesh.batch_change_entity_parts(entities, addPartOrdinals, removePartOrdinals);
403 confirm_host_mesh_is_not_synchronized_from_device(ngpMesh);
404
405 ngpMesh.sync_to_host();
406 confirm_host_mesh_is_synchronized_from_device(ngpMesh);
407
408 check_bucket_layout(*m_bulk, {{{"part1", "part2"}, {nodeId}}}, stk::topology::NODE_RANK);
409 }
410
411 TEST_F(NgpBatchChangeEntityParts, failedHostAccessAfterDeviceMeshMod)
412 {
413 if (stk::parallel_machine_size(MPI_COMM_WORLD) != 1) GTEST_SKIP();
414
415 build_empty_mesh(1, 1);
416
417 stk::mesh::Part & part1 = m_meta->declare_part_with_topology("part1", stk::topology::NODE);
418 stk::mesh::Part & part2 = m_meta->declare_part_with_topology("part2", stk::topology::NODE);
419 const unsigned nodeId = 1;
420 const stk::mesh::Entity node1 = create_node(*m_bulk, nodeId, {&part1});
421 check_bucket_layout(*m_bulk, {{{"part1"}, {nodeId}}}, stk::topology::NODE_RANK);
422
423 DeviceEntitiesType entities("deviceEntities", 1);
424 DevicePartOrdinalsType addPartOrdinals("deviceAddParts", 1);
425 DevicePartOrdinalsType removePartOrdinals("deviceRemoveParts", 0);
426
427 stk::mesh::NgpMesh & ngpMesh = stk::mesh::get_updated_ngp_mesh(*m_bulk);
428 fill_device_views_add_remove_part_from_node(entities, addPartOrdinals, removePartOrdinals, ngpMesh,
429 node1, &part2, nullptr);
430
431 ngpMesh.batch_change_entity_parts(entities, addPartOrdinals, removePartOrdinals);
432 confirm_host_mesh_is_not_synchronized_from_device(ngpMesh);
433
434 if constexpr (std::is_same_v<stk::mesh::NgpMesh, stk::mesh::DeviceMesh>) {
435 EXPECT_ANY_THROW(m_bulk->buckets(stk::topology::NODE_RANK));
436 EXPECT_ANY_THROW(m_bulk->get_buckets(stk::topology::NODE_RANK, m_meta->universal_part()));
437 EXPECT_ANY_THROW(m_bulk->modification_begin());
438 EXPECT_ANY_THROW(m_bulk->batch_change_entity_parts(stk::mesh::EntityVector{node1}, stk::mesh::PartVector{},
439 stk::mesh::PartVector{}));
440 EXPECT_ANY_THROW(stk::mesh::skin_mesh(*m_bulk, stk::mesh::PartVector{&part1}));
441 }
442 }
443
444 TEST_F(NgpBatchChangeEntityParts, addPartToNode_impl_set_new_part_lists)
445 {
446 if (stk::parallel_machine_size(MPI_COMM_WORLD) != 1) GTEST_SKIP();
447
448 build_empty_mesh(1, 1);
449
450 stk::mesh::Part & part1 = m_meta->declare_part_with_topology("part1", stk::topology::NODE);
451 stk::mesh::Part & part2 = m_meta->declare_part_with_topology("part2", stk::topology::NODE);
452 const unsigned nodeId = 1;
453 const stk::mesh::Entity node1 = create_node(*m_bulk, nodeId, {&part1});
454 check_bucket_layout(*m_bulk, {{{"part1"}, {nodeId}}}, stk::topology::NODE_RANK);
455
456 DeviceEntitiesType entities("deviceEntities", 1);
457 DevicePartOrdinalsType addPartOrdinals("deviceAddParts", 1);
458 DevicePartOrdinalsType removePartOrdinals("deviceRemoveParts", 0);
459
460 stk::mesh::NgpMesh & ngpMesh = stk::mesh::get_updated_ngp_mesh(*m_bulk);
461 fill_device_views_add_remove_part_from_node(entities, addPartOrdinals, removePartOrdinals, ngpMesh,
462 node1, &part2, nullptr);
463
464 const unsigned maxCurrentNumPartsPerEntity = stk::mesh::impl::get_max_num_parts_per_entity(ngpMesh,entities);
465 const unsigned maxNewNumPartsPerEntity = maxCurrentNumPartsPerEntity + addPartOrdinals.size();
466 DevicePartOrdinalsType newPartOrdinalsPerEntity("newPartOrdinals", entities.size()*(1+maxNewNumPartsPerEntity));
467 stk::mesh::impl::set_new_part_list_per_entity(ngpMesh, entities, addPartOrdinals, removePartOrdinals,
468 maxNewNumPartsPerEntity, newPartOrdinalsPerEntity);
469
470 ngpMesh.batch_change_entity_parts(entities, addPartOrdinals, removePartOrdinals);
471 ngpMesh.sync_to_host();
472 const stk::mesh::PartVector& newParts = m_bulk->bucket(node1).supersets();
473
474 auto newPartOrdinalsHost = Kokkos::create_mirror_view_and_copy(stk::ngp::HostExecSpace{}, newPartOrdinalsPerEntity);
475
476 unsigned expectedSize = 1+newParts.size();
477 EXPECT_EQ(expectedSize, newPartOrdinalsHost.size());
478 EXPECT_EQ(newParts.size(), newPartOrdinalsHost(0));
479 for(unsigned i=0; i<newParts.size(); ++i) {
480 EXPECT_EQ(newParts[i]->mesh_meta_data_ordinal(), newPartOrdinalsHost(1+i));
481 }
482 }
483
484 TEST_F(NgpBatchChangeEntityParts, addPartToNode_impl_batch_change_entity_parts)
485 {
486 if (stk::parallel_machine_size(MPI_COMM_WORLD) != 1) GTEST_SKIP();
487
488 build_empty_mesh(1, 1);
489
490 stk::mesh::Part & part1 = m_meta->declare_part_with_topology("part1", stk::topology::NODE);
491 stk::mesh::Part & part2 = m_meta->declare_part_with_topology("part2", stk::topology::NODE);
492 const unsigned nodeId = 1;
493 const stk::mesh::Entity node1 = create_node(*m_bulk, nodeId, {&part1});
494 check_bucket_layout(*m_bulk, {{{"part1"}, {nodeId}}}, stk::topology::NODE_RANK);
495
496 DeviceEntitiesType entities("deviceEntities", 1);
497 DevicePartOrdinalsType addPartOrdinals("deviceAddParts", 1);
498 DevicePartOrdinalsType removePartOrdinals("deviceRemoveParts", 0);
499
500 stk::mesh::NgpMesh & ngpMesh = stk::mesh::get_updated_ngp_mesh(*m_bulk);
501 fill_device_views_add_remove_part_from_node(entities, addPartOrdinals, removePartOrdinals, ngpMesh,
502 node1, &part2, nullptr);
503
504 EXPECT_NO_THROW(ngpMesh.impl_batch_change_entity_parts(entities, addPartOrdinals, removePartOrdinals));
505 }
506
507 TEST_F(NgpBatchChangeEntityParts, removePartFromNode_impl_batch_change_entity_parts)
508 {
509 if (stk::parallel_machine_size(MPI_COMM_WORLD) != 1) GTEST_SKIP();
510
511 build_empty_mesh(1, 1);
512
513 stk::mesh::Part & part1 = m_meta->declare_part_with_topology("part1", stk::topology::NODE);
514 stk::mesh::Part & part2 = m_meta->declare_part_with_topology("part2", stk::topology::NODE);
515 const unsigned nodeId = 1;
516 const stk::mesh::Entity node1 = create_node(*m_bulk, nodeId, {&part1, &part2});
517 check_bucket_layout(*m_bulk, {{{"part1", "part2"}, {nodeId}}}, stk::topology::NODE_RANK);
518
519 DeviceEntitiesType entities("deviceEntities", 1);
520 DevicePartOrdinalsType addPartOrdinals("deviceAddParts", 0);
521 DevicePartOrdinalsType removePartOrdinals("deviceRemoveParts", 1);
522
523 stk::mesh::NgpMesh & ngpMesh = stk::mesh::get_updated_ngp_mesh(*m_bulk);
524 fill_device_views_add_remove_part_from_node(entities, addPartOrdinals, removePartOrdinals, ngpMesh,
525 node1, nullptr, &part1);
526
527 EXPECT_NO_THROW(ngpMesh.impl_batch_change_entity_parts(entities, addPartOrdinals, removePartOrdinals));
528 }
529
530 template<typename ViewType>
531 void init_sorted(ViewType& view)
532 {
533 auto devicePolicy = stk::ngp::DeviceRangePolicy(0,1);
534 Kokkos::parallel_for("init", devicePolicy, KOKKOS_LAMBDA(const int& /*idx*/) {
535 view(0) = 0;
536 view(1) = 2;
537 view(2) = 2;
538 view(3) = 4;
539 view(4) = 9;
540 });
541 }
542
543 template<typename ViewType>
544 void init_unsorted(ViewType& view)
545 {
546 auto devicePolicy = stk::ngp::DeviceRangePolicy(0,1);
547 Kokkos::parallel_for("init", devicePolicy, KOKKOS_LAMBDA(const int& /*idx*/) {
548 view(0) = 0;
549 view(1) = 2;
550 view(2) = 1;
551 view(3) = 4;
552 view(4) = 9;
553 });
554 }
555
556 TEST(NgpMeshImpl, is_sorted)
557 {
558 Kokkos::View<int*,stk::ngp::ExecSpace> emptyView("emptyView", 0);
559 EXPECT_TRUE(Kokkos::Experimental::is_sorted(stk::ngp::ExecSpace{},emptyView));
560
561 Kokkos::View<int*,stk::ngp::ExecSpace> sortedView("sortedView", 5);
562 init_sorted(sortedView);
563
564 EXPECT_TRUE(Kokkos::Experimental::is_sorted(stk::ngp::ExecSpace{},sortedView));
565
566 Kokkos::View<int*,stk::ngp::ExecSpace> unsortedView("unsortedView", 5);
567 init_unsorted(unsortedView);
568
569 EXPECT_FALSE(Kokkos::Experimental::is_sorted(stk::ngp::ExecSpace{},unsortedView));
570 }
571
572 TEST(NgpMeshImpl, get_sorted_view)
573 {
574 Kokkos::View<int*,stk::ngp::ExecSpace> unsortedView("unsortedView", 5);
575 init_unsorted(unsortedView);
576
577 Kokkos::View<int*,stk::ngp::ExecSpace> sortedView = stk::mesh::impl::get_sorted_view(unsortedView);
578 EXPECT_TRUE(Kokkos::Experimental::is_sorted(stk::ngp::ExecSpace{},sortedView));
579 }
580
581 } // namespace