Debian Codegraph @

packages/stk/stk_unit_tests/stk_mesh/ngp/ngpFieldTest.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_util/stk_config.h>
37 #include <stk_mesh/base/Ngp.hpp>
38 #include <stk_unit_test_utils/getOption.h>
39 #include <stk_unit_test_utils/MeshFixture.hpp>
40 #include <stk_unit_test_utils/TextMesh.hpp>
41 #include <stk_unit_test_utils/GetMeshSpec.hpp>
42 #include <stk_unit_test_utils/PerformanceTester.hpp>
43 #include <stk_mesh/base/MetaData.hpp>
44 #include <stk_mesh/base/BulkData.hpp>
45 #include <stk_mesh/base/DestroyRelations.hpp>
46 #include <stk_mesh/base/Bucket.hpp>
47 #include <stk_mesh/base/Field.hpp>
48 #include <stk_mesh/base/Entity.hpp>
49 #include <stk_mesh/base/GetEntities.hpp>
50 #include <stk_mesh/base/GetNgpField.hpp>
51 #include <stk_mesh/base/GetNgpMesh.hpp>
52 #include <stk_mesh/base/Types.hpp>
53 #include <stk_mesh/base/ForEachEntity.hpp>
54 #include <stk_mesh/base/NgpForEachEntity.hpp>
55 #include <stk_mesh/base/FieldBLAS.hpp>
56 #include <stk_mesh/base/NgpFieldBLAS.hpp>
57 #include <stk_util/util/StkNgpVector.hpp>
58 #include "NgpUnitTestUtils.hpp"
59 #include "NgpFieldTestUtils.hpp"
60 #include <Kokkos_Core.hpp>
61 #include <string>
62 #include <cstdlib>
63
64 namespace ngp_field_test {
65
66 using ngp_unit_test_utils::check_bucket_layout;
67
68 class NgpFieldFixture : public stk::unit_test_util::MeshFixture
69 {
70 public:
71 template <typename T>
72 stk::mesh::Field<T> & create_field(stk::topology::rank_t rank, const std::string & name, unsigned numComponent = 1)
73 {
74 unsigned numStates = 1;
75 const std::vector<T> init(numComponent, 1);
76 stk::mesh::Field<T> & field = get_meta().declare_field<T>(rank, name, numStates);
77 stk::mesh::put_field_on_mesh(field, get_meta().universal_part(), numComponent, init.data());
78 return field;
79 }
80
81 void setup_one_field_one_element_mesh()
82 {
83 const unsigned bucketCapacity = 1;
84 setup_empty_mesh(stk::mesh::BulkData::NO_AUTO_AURA, bucketCapacity, bucketCapacity);
85
86 stk::mesh::Field<int>& stkField = get_meta().declare_field<int>(stk::topology::ELEM_RANK, "field1");
87 stk::mesh::Part& block = get_meta().declare_part_with_topology("block_1", stk::topology::SHELL_QUAD_4);
88
89 const int init1 = 1;
90 stk::mesh::put_field_on_mesh(stkField, block, 1, &init1);
91
92 const std::string meshDesc = "0,1,SHELL_QUAD_4,1,2,5,6,block_1\n";
93 stk::unit_test_util::setup_text_mesh(get_bulk(), meshDesc);
94 }
95
96 void setup_two_field_two_element_mesh()
97 {
98 const unsigned bucketCapacity = 1;
99 setup_empty_mesh(stk::mesh::BulkData::NO_AUTO_AURA, bucketCapacity, bucketCapacity);
100
101 stk::mesh::Field<int>& stkField1 = get_meta().declare_field<int>(stk::topology::ELEM_RANK, "variableLengthField");
102 stk::mesh::Field<int>& stkField2 = get_meta().declare_field<int>(stk::topology::ELEM_RANK, "potentiallyOverwrittenField");
103 stk::mesh::Part& block1 = get_meta().declare_part_with_topology("block_1", stk::topology::SHELL_QUAD_4);
104 stk::mesh::Part& block2 = get_meta().declare_part_with_topology("block_2", stk::topology::SHELL_QUAD_4);
105
106 const int init1[2] = {1, 1};
107 stk::mesh::put_field_on_mesh(stkField1, block1, 1, init1);
108 stk::mesh::put_field_on_mesh(stkField1, block2, 2, init1);
109
110 const int init2 = 1;
111 stk::mesh::put_field_on_mesh(stkField2, block1, 1, &init2);
112 stk::mesh::put_field_on_mesh(stkField2, block2, 1, &init2);
113
114 const std::string meshDesc = "0,1,SHELL_QUAD_4,1,2,5,6,block_1\n"
115 "0,2,SHELL_QUAD_4,2,3,4,5,block_2\n";
116 stk::unit_test_util::setup_text_mesh(get_bulk(), meshDesc);
117 }
118
119 void setup_two_variable_fields_two_element_mesh()
120 {
121 const unsigned bucketCapacity = 1;
122 setup_empty_mesh(stk::mesh::BulkData::NO_AUTO_AURA, bucketCapacity);
123
124 stk::mesh::Field<int>& stkField1 = get_meta().declare_field<int>(stk::topology::ELEM_RANK, "variableLengthField");
125 stk::mesh::Field<int>& stkField2 = get_meta().declare_field<int>(stk::topology::ELEM_RANK, "partiallyAllocatedField");
126 stk::mesh::Part& block1 = get_meta().declare_part_with_topology("block_1", stk::topology::SHELL_QUAD_4);
127 stk::mesh::Part& block2 = get_meta().declare_part_with_topology("block_2", stk::topology::SHELL_QUAD_4);
128
129 const int init1[] = { 1, 2, 3,
130 11, 12, 13,
131 21, 22, 23,
132 31, 32, 33,
133 41, 42, 43};
134 stk::mesh::put_field_on_mesh(stkField1, block1, 3, 4, init1);
135 stk::mesh::put_field_on_mesh(stkField1, block2, 3, 5, init1);
136
137 const int init2[] = {1, 2};
138 stk::mesh::put_field_on_mesh(stkField2, block1, 2, init2);
139
140 const std::string meshDesc = "0,1,SHELL_QUAD_4,1,2,5,6,block_1\n"
141 "0,2,SHELL_QUAD_4,2,3,4,5,block_2\n";
142 stk::unit_test_util::setup_text_mesh(get_bulk(), meshDesc);
143 }
144
145 template<typename T>
146 void setup_two_fields_five_hex_three_block_mesh(const int numComponent1, const int numComponent2, const int numStates = 1)
147 {
148 const unsigned bucketCapacity = 2;
149 setup_empty_mesh(stk::mesh::BulkData::NO_AUTO_AURA, bucketCapacity, bucketCapacity);
150
151 stk::mesh::Field<T>& stkField1 = get_meta().declare_field<T>(stk::topology::ELEM_RANK, "variableLengthField1", numStates);
152 stk::mesh::Field<T>& stkField2 = get_meta().declare_field<T>(stk::topology::ELEM_RANK, "variableLengthField2", numStates);
153
154 stk::mesh::Part& block1 = get_meta().declare_part_with_topology("block_1", stk::topology::HEX_8);
155 stk::mesh::Part& block2 = get_meta().declare_part_with_topology("block_2", stk::topology::HEX_8);
156 get_meta().declare_part_with_topology("block_3", stk::topology::HEX_8);
157
158 const std::vector<T> init1(numComponent1, -1);
159 stk::mesh::put_field_on_mesh(stkField1, block1, numComponent1, init1.data());
160
161 const std::vector<T> init2(numComponent2, -2);
162 stk::mesh::put_field_on_mesh(stkField1, block2, numComponent2, init2.data());
163
164 const std::vector<T> init3(numComponent1, -1);
165 stk::mesh::put_field_on_mesh(stkField2, block1, numComponent1, init3.data());
166
167 const std::vector<T> init4(numComponent2, -2);
168 stk::mesh::put_field_on_mesh(stkField2, block2, numComponent2, init4.data());
169
170 const std::string meshDesc = "0,1,HEX_8,1,2,3,4,5,6,7,8,block_1\n"
171 "0,2,HEX_8,5,6,7,8,9,10,11,12,block_1\n"
172 "0,3,HEX_8,9,13,14,15,16,17,18,19,block_2\n"
173 "0,4,HEX_8,9,20,21,22,23,24,25,26,block_2\n"
174 "0,5,HEX_8,9,27,28,29,30,31,32,33,block_3";
175 stk::unit_test_util::setup_text_mesh(get_bulk(), meshDesc);
176 }
177
178 template<typename T>
179 void setup_three_fields_five_hex_three_block_mesh(const int numComponent1, const int numComponent2, const int numStates = 1)
180 {
181 const unsigned bucketCapacity = 2;
182 setup_empty_mesh(stk::mesh::BulkData::NO_AUTO_AURA, bucketCapacity, bucketCapacity);
183
184 stk::mesh::Field<T>& stkField1 = get_meta().declare_field<T>(stk::topology::NODE_RANK, "variableLengthField1", numStates);
185 stk::mesh::Field<T>& stkField2 = get_meta().declare_field<T>(stk::topology::NODE_RANK, "variableLengthField2", numStates);
186 stk::mesh::Field<T>& stkField3 = get_meta().declare_field<T>(stk::topology::NODE_RANK, "variableLengthField3", numStates);
187
188 stk::mesh::Part& block1 = get_meta().declare_part_with_topology("block_1", stk::topology::HEX_8);
189 stk::mesh::Part& block2 = get_meta().declare_part_with_topology("block_2", stk::topology::HEX_8);
190 get_meta().declare_part_with_topology("block_3", stk::topology::HEX_8);
191
192 const std::vector<T> init1(numComponent1, -1);
193 stk::mesh::put_field_on_mesh(stkField1, block1, numComponent1, init1.data());
194
195 const std::vector<T> init2(numComponent2, -2);
196 stk::mesh::put_field_on_mesh(stkField1, block2, numComponent2, init2.data());
197
198 const std::vector<T> init3(numComponent1, -1);
199 stk::mesh::put_field_on_mesh(stkField2, block1, numComponent1, init3.data());
200
201 const std::vector<T> init4(numComponent2, -2);
202 stk::mesh::put_field_on_mesh(stkField2, block2, numComponent2, init4.data());
203
204 const std::vector<T> init5(numComponent1, 0);
205 stk::mesh::put_field_on_mesh(stkField3, block1, numComponent1, init5.data());
206
207 const std::vector<T> init6(numComponent2, 0);
208 stk::mesh::put_field_on_mesh(stkField3, block2, numComponent2, init6.data());
209
210 const std::string meshDesc = "0,1,HEX_8,1,2,3,4,5,6,7,8,block_1\n"
211 "0,2,HEX_8,5,6,7,8,9,10,11,12,block_1\n"
212 "0,3,HEX_8,9,13,14,15,16,17,18,19,block_2\n"
213 "0,4,HEX_8,9,20,21,22,23,24,25,26,block_2\n"
214 "0,5,HEX_8,9,27,28,29,30,31,32,33,block_3";
215 stk::unit_test_util::setup_text_mesh(get_bulk(), meshDesc);
216 }
217
218 void setup_two_element_mesh_field_on_each_element(stk::mesh::Field<int>& intField, stk::mesh::Field<double>& doubleField)
219 {
220 stk::mesh::Part& block1 = get_meta().declare_part_with_topology("block_1", stk::topology::SHELL_QUAD_4);
221 stk::mesh::Part& block2 = get_meta().declare_part_with_topology("block_2", stk::topology::SHELL_QUAD_4);
222
223 int initIntVal = 1;
224 double initDoubleVal = 2.0;
225 stk::mesh::put_field_on_mesh(intField, block1, 1, &initIntVal);
226 stk::mesh::put_field_on_mesh(doubleField, block2, 1, &initDoubleVal);
227
228 const std::string meshDesc = "0,1,SHELL_QUAD_4,1,2,4,3,block_1\n"
229 "0,2,SHELL_QUAD_4,2,5,6,4,block_2\n";
230 stk::unit_test_util::setup_text_mesh(get_bulk(), meshDesc);
231 }
232
233 void add_3rd_element_to_2hex_3block_mesh()
234 {
235 get_bulk().modification_begin();
236 stk::mesh::PartVector parts {get_meta().get_part("block_3")};
237 stk::mesh::EntityIdVector nodeIds(8);
238 std::iota(nodeIds.begin(), nodeIds.end(), 9);
239 stk::mesh::declare_element(get_bulk(), parts, 3, nodeIds);
240 get_bulk().modification_end();
241 }
242
243 template<typename T>
244 void copy_fields_on_device(stk::mesh::NgpMesh& ngpMesh, stk::mesh::FieldBase* inputField,
245 stk::mesh::FieldBase* outputField, const stk::mesh::Selector& selector)
246 {
247 stk::mesh::NgpField<T> inputNgpField = stk::mesh::get_updated_ngp_field<T>(*inputField);
248 stk::mesh::NgpField<T> outputNgpField = stk::mesh::get_updated_ngp_field<T>(*outputField);
249
250 stk::mesh::for_each_entity_run(ngpMesh, stk::topology::ELEM_RANK, selector,
251 KOKKOS_LAMBDA(const stk::mesh::FastMeshIndex& entityIndex) {
252 const int numScalarsPerEntity = inputNgpField.get_num_components_per_entity(entityIndex);
253
254 for (int component=0; component<numScalarsPerEntity; component++) {
255 outputNgpField(entityIndex, component) = inputNgpField(entityIndex, component);
256 }
257 });
258
259 outputNgpField.modify_on_device();
260 }
261
262 template<typename T>
263 void check_field_data_on_host(stk::mesh::Field<T>& stkField, const stk::mesh::Selector& selector, unsigned multiplier)
264 {
265 stk::mesh::EntityRank rank = stk::topology::ELEM_RANK;
266 stk::mesh::EntityVector elements;
267 stk::mesh::get_entities(get_bulk(), rank, selector, elements);
268
269 for(stk::mesh::Entity element : elements) {
270 T* data = stk::mesh::field_data(stkField, element);
271 unsigned numComponents = stk::mesh::field_scalars_per_entity(stkField, element);
272 for(unsigned j = 0; j < numComponents; j++) {
273 int expectedVal = get_bulk().identifier(element) * multiplier + j;
274 EXPECT_EQ(data[j], expectedVal);
275 }
276 }
277 }
278
279 template<typename T>
280 void check_field_data_on_host(stk::mesh::Field<T>& stkField, unsigned multiplier)
281 {
282 check_field_data_on_host(stkField, stkField, multiplier);
283 }
284
285 template<typename T, typename FieldData, typename FieldDataMirror>
286 void get_field_data_from_device(stk::mesh::EntityVector& elements, stk::mesh::NgpField<T>& ngpField,
287 FieldData& deviceData, FieldDataMirror& hostData)
288 {
289 unsigned numElems = elements.size();
290 stk::NgpVector<stk::mesh::Entity> ngpElements(numElems);
291
292 for(unsigned i = 0; i < numElems; i++) {
293 ngpElements[i] = elements[i];
294 }
295 ngpElements.copy_host_to_device();
296
297 stk::mesh::NgpMesh& ngpMesh = stk::mesh::get_updated_ngp_mesh(get_bulk());
298
299 Kokkos::parallel_for(stk::ngp::DeviceRangePolicy(0, numElems),
300 KOKKOS_LAMBDA(const int& elemIdx) {
301 stk::mesh::Entity elem = ngpElements.device_get(elemIdx);
302 auto meshIndex = ngpMesh.fast_mesh_index(elem);
303 unsigned numScalarsPerEntity = ngpField.get_num_components_per_entity(meshIndex);
304
305 for(unsigned i = 0; i < numScalarsPerEntity; i++) {
306 deviceData(elemIdx,i) = ngpField(meshIndex,i);
307 }
308 }
309 );
310 Kokkos::deep_copy(hostData, deviceData);
311 }
312
313 template<typename T, typename FieldDataMirror, typename Func>
314 void verify_field_data_on_device(const stk::mesh::EntityVector& elements, const stk::mesh::Field<T>& stkField,
315 const FieldDataMirror& hostData, Func&& checkFunc)
316 {
317 for(unsigned i = 0; i < elements.size(); i++) {
318 T* data = stk::mesh::field_data(stkField, elements[i]);
319 unsigned numComponents = stk::mesh::field_scalars_per_entity(stkField, elements[i]);
320 for(unsigned j = 0; j < numComponents; j++) {
321 checkFunc(hostData(i,j), data[j]);
322 }
323 }
324 }
325
326 template<typename T, typename Func>
327 void check_field_data_equality_on_device(stk::mesh::EntityVector& elements,
328 stk::mesh::NgpField<T>& ngpField, stk::mesh::Field<T>& stkField,
329 Func&& checkFunc)
330 {
331 using FieldData = Kokkos::View<T**, Kokkos::LayoutRight, stk::ngp::MemSpace>;
332
333 unsigned numElems = elements.size();
334 unsigned numPerEntity = stkField.max_size();
335 FieldData deviceData = FieldData("deviceData", numElems, numPerEntity);
336 typename FieldData::HostMirror hostData = Kokkos::create_mirror_view(deviceData);
337
338 get_field_data_from_device<T, FieldData, typename FieldData::HostMirror>(elements, ngpField, deviceData, hostData);
339 verify_field_data_on_device<T, typename FieldData::HostMirror>(elements, stkField, hostData, checkFunc);
340 }
341
342 template<typename T>
343 void check_mismatched_field_data_on_device(stk::mesh::NgpField<T>& ngpField, stk::mesh::Field<T>& stkField, stk::mesh::Selector& syncSelector)
344 {
345 stk::mesh::EntityRank rank = stk::topology::ELEM_RANK;
346 stk::mesh::Selector fieldSelector(stkField);
347 fieldSelector &= syncSelector;
348 stk::mesh::EntityVector elements;
349 stk::mesh::get_selected_entities(fieldSelector, get_bulk().buckets(rank), elements);
350 auto checkFunc = [](T hostData, T stkData)
351 {
352 EXPECT_NE(hostData, stkData);
353 };
354 check_field_data_equality_on_device<T>(elements, ngpField, stkField, checkFunc);
355 }
356
357 template<typename T>
358 void check_field_data_on_device(stk::mesh::NgpField<T>& ngpField, stk::mesh::Field<T>& stkField, stk::mesh::Selector& syncSelector)
359 {
360 stk::mesh::EntityRank rank = stk::topology::ELEM_RANK;
361 stk::mesh::Selector fieldSelector(stkField);
362 fieldSelector &= syncSelector;
363 stk::mesh::EntityVector elements;
364 stk::mesh::get_selected_entities(fieldSelector, get_bulk().buckets(rank), elements);
365 auto checkFunc = [](T hostData, T stkData)
366 {
367 EXPECT_EQ(hostData, stkData);
368 };
369 check_field_data_equality_on_device<T>(elements, ngpField, stkField, checkFunc);
370 }
371
372 template<typename T>
373 void check_field_data_on_device(stk::mesh::NgpField<T>& ngpField, stk::mesh::Field<T>& stkField)
374 {
375 stk::mesh::EntityRank rank = stk::topology::ELEM_RANK;
376 stk::mesh::EntityVector elements;
377 stk::mesh::get_entities(get_bulk(), rank, elements);
378 auto checkFunc = [](T hostData, T stkData)
379 {
380 EXPECT_EQ(hostData, stkData);
381 };
382 check_field_data_equality_on_device<T>(elements, ngpField, stkField, checkFunc);
383 }
384
385 void set_element_field_data(stk::mesh::FieldBase* field)
386 {
387 stk::mesh::EntityVector elements;
388 const stk::mesh::BucketVector& buckets = get_bulk().buckets(stk::topology::ELEM_RANK);
389 stk::mesh::get_selected_entities(get_meta().universal_part(), buckets, elements);
390
391 for(stk::mesh::Entity elem : elements) {
392 int* data = static_cast<int*>(stk::mesh::field_data(*field, elem));
393 unsigned numComponents = stk::mesh::field_scalars_per_entity(*field, get_bulk().bucket(elem));
394 for(unsigned i = 0; i < numComponents; i++) {
395 data[i] = get_bulk().identifier(elem) * 10 + i;
396 }
397 }
398 }
399 };
400
401 class OptimizedNgpFieldFixture : public NgpFieldFixture
402 {
403
404 public:
405 void set_element_field_data(stk::mesh::Field<int>& stkIntField, stk::mesh::Selector selector, unsigned multiplier)
406 {
407 stk::mesh::EntityVector elements;
408 stk::mesh::get_selected_entities(selector, get_bulk().buckets(stk::topology::ELEM_RANK), elements);
409
410 for(stk::mesh::Entity elem : elements) {
411 int* data = reinterpret_cast<int*>(stk::mesh::field_data(stkIntField, elem));
412 unsigned numComponents = stk::mesh::field_scalars_per_entity(stkIntField, elem);
413 for(unsigned j = 0; j < numComponents; j++) {
414 data[j] = get_bulk().identifier(elem) * multiplier + j;
415 }
416 }
417 }
418
419 void set_element_field_data_on_device(stk::mesh::NgpMesh& ngpMesh, stk::mesh::Field<int>& stkIntField,
420 const stk::mesh::Selector& selector, unsigned multiplier)
421 {
422 stk::mesh::NgpField<int>& ngpField = stk::mesh::get_updated_ngp_field<int>(stkIntField);
423
424 stk::mesh::for_each_entity_run(ngpMesh, stk::topology::ELEM_RANK, selector,
425 KOKKOS_LAMBDA(const stk::mesh::FastMeshIndex& entityIndex) {
426 const int numScalarsPerEntity = ngpField.get_num_components_per_entity(entityIndex);
427 for (int component=0; component<numScalarsPerEntity; component++) {
428 stk::mesh::Entity entity = ngpMesh.get_entity(stk::topology::ELEM_RANK, entityIndex);
429 ngpField(entityIndex, component) = ngpMesh.identifier(entity) * multiplier + component;
430 }
431 });
432 }
433
434 void setup_3hex_3block_mesh_with_field(unsigned bucketCapacity, stk::mesh::Field<int>& stkIntField)
435 {
436 ngp_unit_test_utils::setup_mesh_3hex_3block(get_bulk(), bucketCapacity);
437 set_element_field_data(stkIntField, get_meta().universal_part(), 10u);
438 }
439
440 void setup_3hex_2block_mesh_with_field(unsigned bucketCapacity, stk::mesh::Field<int>& stkIntField)
441 {
442 ngp_unit_test_utils::setup_mesh_3hex_2block(get_bulk(), bucketCapacity);
443 set_element_field_data(stkIntField, get_meta().universal_part(), 10u);
444 }
445
446 void setup_2hex_3block_mesh_with_field(unsigned bucketCapacity, stk::mesh::Field<int>& stkIntField)
447 {
448 get_meta().declare_part_with_topology("block_3", stk::topology::HEX_8);
449 ngp_unit_test_utils::setup_mesh_2hex_2block(get_bulk(), bucketCapacity);
450 set_element_field_data(stkIntField, get_meta().universal_part(), 10u);
451 }
452
453 void run_add_and_delete_bucket3(unsigned numComponents)
454 {
455 const unsigned bucketCapacity = 2;
456 setup_empty_mesh(stk::mesh::BulkData::NO_AUTO_AURA, bucketCapacity, bucketCapacity);
457 stk::mesh::Field<int>& stkIntField = create_field<int>(stk::topology::ELEM_RANK, "intField", numComponents);
458
459 setup_2hex_3block_mesh_with_field(bucketCapacity, stkIntField);
460 stk::mesh::NgpField<int> ngpIntField = stk::mesh::get_updated_ngp_field<int>(stkIntField);
461 modify_mesh_add_and_delete_bucket3(stkIntField, ngpIntField);
462
463 ngpIntField.modify_on_host();
464 ngpIntField = stk::mesh::get_updated_ngp_field<int>(stkIntField);
465
466 check_field_data_on_device<int>(ngpIntField, stkIntField);
467 }
468
469 void run_add_and_delete_bucket2(unsigned numComponents)
470 {
471 const unsigned bucketCapacity = 2;
472 setup_empty_mesh(stk::mesh::BulkData::NO_AUTO_AURA, bucketCapacity, bucketCapacity);
473 stk::mesh::Field<int>& stkIntField = create_field<int>(stk::topology::ELEM_RANK, "intField", numComponents);
474
475 setup_2hex_3block_mesh_with_field(bucketCapacity, stkIntField);
476 stk::mesh::NgpField<int> ngpIntField = stk::mesh::get_updated_ngp_field<int>(stkIntField);
477 modify_mesh_add_and_delete_bucket2(stkIntField, ngpIntField);
478
479 ngpIntField.modify_on_host();
480 ngpIntField = stk::mesh::get_updated_ngp_field<int>(stkIntField);
481
482 check_field_data_on_device<int>(ngpIntField, stkIntField);
483 }
484
485 void run_add_and_delete_bucket(unsigned numComponents)
486 {
487 const unsigned bucketCapacity = 2;
488 setup_empty_mesh(stk::mesh::BulkData::NO_AUTO_AURA, bucketCapacity, bucketCapacity);
489 stk::mesh::Field<int>& stkIntField = create_field<int>(stk::topology::ELEM_RANK, "intField", numComponents);
490
491 setup_2hex_3block_mesh_with_field(bucketCapacity, stkIntField);
492 stk::mesh::NgpField<int> ngpIntField = stk::mesh::get_updated_ngp_field<int>(stkIntField);
493 modify_mesh_add_and_delete_bucket(stkIntField, ngpIntField);
494
495 ngpIntField.modify_on_host();
496 ngpIntField = stk::mesh::get_updated_ngp_field<int>(stkIntField);
497
498 check_field_data_on_device<int>(ngpIntField, stkIntField);
499 }
500
501
502 void run_delete_bucket_in_middle(unsigned numComponents)
503 {
504 const unsigned bucketCapacity = 2;
505 setup_empty_mesh(stk::mesh::BulkData::NO_AUTO_AURA, bucketCapacity, bucketCapacity);
506 stk::mesh::Field<int>& stkIntField = create_field<int>(stk::topology::ELEM_RANK, "intField", numComponents);
507
508 setup_3hex_3block_mesh_with_field(bucketCapacity, stkIntField);
509 stk::mesh::NgpField<int> ngpIntField = stk::mesh::get_updated_ngp_field<int>(stkIntField);
510 modify_mesh_delete_bucket_in_middle(stkIntField, ngpIntField);
511
512 ngpIntField.modify_on_host();
513 ngpIntField = stk::mesh::get_updated_ngp_field<int>(stkIntField);
514
515 check_field_data_on_device<int>(ngpIntField, stkIntField);
516 }
517
518 void run_add_new_element(unsigned numComponents, unsigned bucketCapacity)
519 {
520 setup_empty_mesh(stk::mesh::BulkData::NO_AUTO_AURA, bucketCapacity, bucketCapacity);
521 stk::mesh::Field<int>& stkIntField = create_field<int>(stk::topology::ELEM_RANK, "intField", numComponents);
522
523 setup_3hex_3block_mesh_with_field(bucketCapacity, stkIntField);
524 stk::mesh::NgpField<int>& ngpIntField = stk::mesh::get_updated_ngp_field<int>(stkIntField);
525
526 modify_and_test_add_element(stkIntField, ngpIntField, bucketCapacity);
527 }
528
529 void run_add_new_element_and_modify_from_device(unsigned numComponents)
530 {
531 const unsigned bucketCapacity = 2;
532 setup_empty_mesh(stk::mesh::BulkData::NO_AUTO_AURA, bucketCapacity, bucketCapacity);
533 stk::mesh::Field<int>& stkIntField = create_field<int>(stk::topology::ELEM_RANK, "intField", numComponents);
534
535 setup_3hex_3block_mesh_with_field(bucketCapacity, stkIntField);
536 {
537 stk::mesh::get_updated_ngp_mesh(get_bulk());
538 stk::mesh::NgpField<int>& ngpIntField = stk::mesh::get_updated_ngp_field<int>(stkIntField);
539
540 modify_and_test_add_element(stkIntField, ngpIntField, bucketCapacity);
541 }
542
543 {
544 stk::mesh::NgpMesh& ngpMesh = stk::mesh::get_updated_ngp_mesh(get_bulk());
545 stk::mesh::NgpField<int>& ngpIntField = stk::mesh::get_updated_ngp_field<int>(stkIntField);
546
547 check_field_data_on_host<int>(stkIntField, 10u);
548
549 ngpIntField.sync_to_device();
550 ngpIntField.clear_sync_state();
551 ngpIntField.modify_on_device();
552
553 stk::mesh::for_each_entity_run(ngpMesh, stk::topology::ELEMENT_RANK, get_meta().universal_part(),
554 KOKKOS_LAMBDA(const stk::mesh::FastMeshIndex& elem) {
555 ngpIntField(elem, 0) *= 2;
556 }
557 );
558
559 ngpIntField.sync_to_host();
560
561 check_field_data_on_host<int>(stkIntField, 20u);
562 }
563 }
564
565 void run_add_bucket_in_middle_copy(unsigned numComponents)
566 {
567 const unsigned bucketCapacity = 1;
568 setup_empty_mesh(stk::mesh::BulkData::NO_AUTO_AURA, bucketCapacity, bucketCapacity);
569 stk::mesh::Field<int>& stkIntField = create_field<int>(stk::topology::ELEM_RANK, "intField", numComponents);
570
571 setup_3hex_3block_mesh_with_field(bucketCapacity, stkIntField);
572 stk::mesh::NgpField<int> ngpIntField = stk::mesh::get_updated_ngp_field<int>(stkIntField);
573
574 modify_and_test_add_bucket_in_middle(stkIntField, ngpIntField);
575 }
576
577 void run_add_bucket_in_middle_external(unsigned numComponents)
578 {
579 const unsigned bucketCapacity = 1;
580 setup_empty_mesh(stk::mesh::BulkData::NO_AUTO_AURA, bucketCapacity, bucketCapacity);
581 stk::mesh::Field<int>& stkIntField = create_field<int>(stk::topology::ELEM_RANK, "intField", numComponents);
582
583 setup_3hex_3block_mesh_with_field(bucketCapacity, stkIntField);
584 stk::mesh::NgpField<int> ngpIntField(get_bulk(), stkIntField);
585
586 modify_and_test_add_bucket_in_middle(stkIntField, ngpIntField);
587 }
588
589 void run_add_bucket_in_middle_internal(unsigned numComponents)
590 {
591 const unsigned bucketCapacity = 1;
592 setup_empty_mesh(stk::mesh::BulkData::NO_AUTO_AURA, bucketCapacity, bucketCapacity);
593 stk::mesh::Field<int>& stkIntField = create_field<int>(stk::topology::ELEM_RANK, "intField", numComponents);
594
595 setup_3hex_3block_mesh_with_field(bucketCapacity, stkIntField);
596 stk::mesh::NgpField<int>& ngpIntField = stk::mesh::get_updated_ngp_field<int>(stkIntField);
597
598 modify_and_test_add_bucket_in_middle(stkIntField, ngpIntField);
599 }
600
601 void modify_and_test_add_bucket_in_middle(stk::mesh::Field<int>& stkIntField, stk::mesh::NgpField<int>& ngpIntField)
602 {
603 modify_mesh_add_bucket_in_middle(stkIntField, ngpIntField);
604
605 ngpIntField.modify_on_host();
606 stk::mesh::get_updated_ngp_field<int>(stkIntField);
607
608 check_field_data_on_device<int>(ngpIntField, stkIntField);
609 }
610
611 void modify_and_test_add_element(stk::mesh::Field<int>& stkIntField, stk::mesh::NgpField<int>& ngpIntField, unsigned bucketCapacity)
612 {
613 modify_mesh_add_element(stkIntField, ngpIntField, bucketCapacity);
614
615 ngpIntField.modify_on_host();
616 stk::mesh::get_updated_ngp_field<int>(stkIntField);
617
618 check_field_data_on_device<int>(ngpIntField, stkIntField);
619 }
620
621 void run_change_bucket_content_by_mesh_modification(unsigned numComponents)
622 {
623 const unsigned bucketCapacity = 2;
624 setup_empty_mesh(stk::mesh::BulkData::NO_AUTO_AURA, bucketCapacity, bucketCapacity);
625 stk::mesh::Field<int>& stkIntField = create_field<int>(stk::topology::ELEM_RANK, "intField", numComponents);
626
627 setup_3hex_2block_mesh_with_field(bucketCapacity, stkIntField);
628 stk::mesh::NgpField<int> ngpIntField = stk::mesh::get_updated_ngp_field<int>(stkIntField);
629 check_field_data_on_device<int>(ngpIntField, stkIntField);
630
631 modify_mesh_change_bucket_content(stkIntField, ngpIntField);
632
633 ngpIntField.clear_sync_state();
634 ngpIntField.modify_on_host();
635 ngpIntField = stk::mesh::get_updated_ngp_field<int>(stkIntField);
636
637 check_field_data_on_device<int>(ngpIntField, stkIntField);
638 }
639
640 void run_change_bucket_content_by_user(unsigned numComponents)
641 {
642 const unsigned bucketCapacity = 2;
643 setup_empty_mesh(stk::mesh::BulkData::NO_AUTO_AURA, bucketCapacity, bucketCapacity);
644 stk::mesh::Field<int>& stkIntField = create_field<int>(stk::topology::ELEM_RANK, "intField", numComponents);
645
646 setup_3hex_2block_mesh_with_field(bucketCapacity, stkIntField);
647 stk::mesh::NgpField<int>& ngpIntField = stk::mesh::get_updated_ngp_field<int>(stkIntField);
648 check_field_data_on_device<int>(ngpIntField, stkIntField);
649
650 set_element_field_data(stkIntField, get_meta().universal_part(), 20u);
651
652 ngpIntField.modify_on_host();
653 ngpIntField.sync_to_device();
654
655 check_field_data_on_device<int>(ngpIntField, stkIntField);
656 }
657
658 void modify_mesh_add_and_delete_bucket(stk::mesh::Field<int>& stkIntField, stk::mesh::NgpField<int>& ngpIntField)
659 {
660 stk::mesh::NgpMesh& ngpMesh = stk::mesh::get_updated_ngp_mesh(get_bulk());
661 check_bucket_layout(get_bulk(), {{{"block_1"}, {1}}, {{"block_2"}, {2}}});
662 check_field_data_on_device<int>(ngpIntField, stkIntField);
663 get_bulk().modification_begin();
664 stk::mesh::PartVector addParts{get_meta().get_part("block_3")};
665 stk::mesh::PartVector removeParts{get_meta().get_part("block_1")};
666 get_bulk().change_entity_parts(get_bulk().get_entity(stk::topology::ELEM_RANK, 1), addParts, removeParts);
667 get_bulk().modification_end();
668 ngpMesh.update_mesh();
669 check_bucket_layout(get_bulk(), {{{"block_3"}, {1}}, {{"block_2"}, {2}}});
670 }
671
672 void fill_nodes(const stk::mesh::Entity element, unsigned numNodes, stk::mesh::EntityVector& nodes)
673 {
674 const stk::mesh::Entity* elementNodes = get_bulk().begin_nodes(element);
675
676 for(unsigned i = 0; i < numNodes; i++) {
677 nodes.push_back(elementNodes[i]);
678 }
679 }
680
681 stk::mesh::Entity add_element_and_place_in_block(const std::string& newBlockName)
682 {
683 stk::mesh::Entity element = get_bulk().get_entity(stk::topology::ELEMENT_RANK, 1);
684 stk::mesh::EntityVector nodes;
685
686 unsigned numNodes = get_bulk().num_nodes(element);
687
688 fill_nodes(element, numNodes, nodes);
689
690 stk::mesh::Part* part = get_meta().get_part(newBlockName);
691 stk::mesh::PartVector parts = {part};
692 parts.push_back(&get_meta().get_topology_root_part(stk::topology::HEX_8));
693 stk::mesh::Entity newElement = get_bulk().declare_element(4, parts);
694
695 for(unsigned i = 0; i < numNodes; i++) {
696 get_bulk().declare_relation(newElement, nodes[i], i);
697 }
698
699 return newElement;
700 }
701
702 void replace_element_and_place_in_block(const std::string& newBlockName)
703 {
704 add_element_and_place_in_block(newBlockName);
705
706 stk::mesh::Entity element = get_bulk().get_entity(stk::topology::ELEMENT_RANK, 1);
707 stk::mesh::destroy_relations(get_bulk(), element, stk::topology::NODE_RANK);
708 get_bulk().destroy_entity(element);
709 }
710
711 void modify_host_bucket_value_with_selector(stk::mesh::Field<int>& stkIntField, stk::mesh::NgpField<int>& ngpIntField,
712 stk::mesh::Selector selector, int newMultiplier)
713 {
714 stk::mesh::NgpMesh& ngpMesh = stk::mesh::get_updated_ngp_mesh(get_bulk());
715 check_field_data_on_device<int>(ngpIntField, stkIntField, selector);
716 set_element_field_data(stkIntField, selector, newMultiplier);
717 ngpMesh.update_mesh();
718 }
719
720 void modify_device_bucket_value_with_selector(stk::mesh::Field<int>& stkIntField, stk::mesh::NgpField<int>& ngpIntField,
721 stk::mesh::Selector selector, int newMultiplier)
722 {
723 stk::mesh::NgpMesh& ngpMesh = stk::mesh::get_updated_ngp_mesh(get_bulk());
724 check_field_data_on_device<int>(ngpIntField, stkIntField, selector);
725 set_element_field_data_on_device(ngpMesh, stkIntField, selector, newMultiplier);
726 // ngpMesh.update_mesh();
727 }
728
729 void modify_mesh_add_and_delete_bucket3(stk::mesh::Field<int>& stkIntField, stk::mesh::NgpField<int>& ngpIntField)
730 {
731 stk::mesh::NgpMesh& ngpMesh = stk::mesh::get_updated_ngp_mesh(get_bulk());
732 check_bucket_layout(get_bulk(), {{{"block_1"}, {1}}, {{"block_2"}, {2}}});
733 check_field_data_on_device<int>(ngpIntField, stkIntField);
734 get_bulk().modification_begin();
735 replace_element_and_place_in_block("block_3");
736 get_bulk().modification_end();
737 ngpMesh.update_mesh();
738 check_bucket_layout(get_bulk(), {{{"block_3"}, {4}}, {{"block_2"}, {2}}});
739 }
740
741 void modify_mesh_add_and_delete_bucket2(stk::mesh::Field<int>& stkIntField, stk::mesh::NgpField<int>& ngpIntField)
742 {
743 stk::mesh::NgpMesh& ngpMesh = stk::mesh::get_updated_ngp_mesh(get_bulk());
744 check_bucket_layout(get_bulk(), {{{"block_1"}, {1}}, {{"block_2"}, {2}}});
745 check_field_data_on_device<int>(ngpIntField, stkIntField);
746 get_bulk().modification_begin();
747 stk::mesh::PartVector addParts{get_meta().get_part("block_2")};
748 stk::mesh::PartVector removeParts{get_meta().get_part("block_1")};
749 get_bulk().change_entity_parts(get_bulk().get_entity(stk::topology::ELEM_RANK, 1), addParts, removeParts);
750 addParts[0] = get_meta().get_part("block_3");
751 removeParts[0] = get_meta().get_part("block_2");
752 get_bulk().change_entity_parts(get_bulk().get_entity(stk::topology::ELEM_RANK, 2), addParts, removeParts);
753 get_bulk().modification_end();
754 ngpMesh.update_mesh();
755 check_bucket_layout(get_bulk(), {{{"block_3"}, {2}}, {{"block_2"}, {1}}});
756 }
757
758 void modify_mesh_delete_bucket_in_middle(stk::mesh::Field<int>& stkIntField, stk::mesh::NgpField<int>& ngpIntField)
759 {
760 stk::mesh::NgpMesh& ngpMesh = stk::mesh::get_updated_ngp_mesh(get_bulk());
761 check_bucket_layout(get_bulk(), {{{"block_1"}, {1}}, {{"block_2"}, {2}}, {{"block_3"}, {3}}});
762 check_field_data_on_device<int>(ngpIntField, stkIntField);
763 get_bulk().modification_begin();
764 stk::mesh::PartVector addParts{get_meta().get_part("block_1")};
765 stk::mesh::PartVector removeParts{get_meta().get_part("block_2")};
766 get_bulk().change_entity_parts(get_bulk().get_entity(stk::topology::ELEM_RANK, 2), addParts, removeParts);
767 get_bulk().modification_end();
768 ngpMesh.update_mesh();
769 check_bucket_layout(get_bulk(), {{{"block_1"}, {1,2}}, {{"block_3"}, {3}}});
770 }
771
772 void modify_mesh_add_bucket_in_middle(stk::mesh::Field<int>& stkIntField, stk::mesh::NgpField<int>& ngpIntField)
773 {
774 stk::mesh::NgpMesh & ngpMesh = stk::mesh::get_updated_ngp_mesh(get_bulk());
775 check_bucket_layout(get_bulk(), {{{"block_1"}, {1}}, {{"block_2"}, {2}}, {{"block_3"}, {3}}});
776 check_field_data_on_device<int>(ngpIntField, stkIntField);
777 get_bulk().modification_begin();
778 stk::mesh::PartVector addParts{get_meta().get_part("block_1")};
779 stk::mesh::PartVector removeParts{get_meta().get_part("block_3")};
780 get_bulk().change_entity_parts(get_bulk().get_entity(stk::topology::ELEM_RANK, 3), addParts, removeParts);
781 get_bulk().modification_end();
782 ngpMesh.update_mesh();
783 check_bucket_layout(get_bulk(), {{{"block_1"}, {1}}, {{"block_1"}, {3}}, {{"block_2"}, {2}}});
784 }
785
786 void modify_mesh_add_element(stk::mesh::Field<int>& stkIntField, stk::mesh::NgpField<int>& ngpIntField, unsigned bucketCapacity)
787 {
788 stk::mesh::NgpMesh & ngpMesh = stk::mesh::get_updated_ngp_mesh(get_bulk());
789 check_bucket_layout(get_bulk(), {{{"block_1"}, {1}}, {{"block_2"}, {2}}, {{"block_3"}, {3}}});
790 check_field_data_on_device<int>(ngpIntField, stkIntField);
791
792 get_bulk().modification_begin();
793 stk::mesh::Entity newElement = add_element_and_place_in_block("block_3");
794 get_bulk().modification_end();
795
796 int* data = stk::mesh::field_data(stkIntField, newElement);
797 *data = get_bulk().identifier(newElement) * 10u;
798 ngpMesh.update_mesh();
799
800 if(bucketCapacity == 1) {
801 check_bucket_layout(get_bulk(), {{{"block_1"}, {1}}, {{"block_2"}, {2}}, {{"block_3"}, {3}}, {{"block_3"}, {4}}});
802 }
803 else if(bucketCapacity == 2) {
804 check_bucket_layout(get_bulk(), {{{"block_1"}, {1}}, {{"block_2"}, {2}}, {{"block_3"}, {3,4}}});
805 }
806 }
807
808 void modify_mesh_change_bucket_content(stk::mesh::Field<int>& stkIntField, stk::mesh::NgpField<int>& ngpIntField)
809 {
810 stk::mesh::NgpMesh& ngpMesh = stk::mesh::get_updated_ngp_mesh(get_bulk());
811 check_bucket_layout(get_bulk(), {{{"block_1"}, {1, 2}}, {{"block_3"}, {3}}});
812 check_field_data_on_device<int>(ngpIntField, stkIntField);
813 get_bulk().modification_begin();
814 stk::mesh::PartVector addParts {get_meta().get_part("block_3")};
815 stk::mesh::PartVector removeParts {get_meta().get_part("block_1")};
816 get_bulk().change_entity_parts(get_bulk().get_entity(stk::topology::ELEM_RANK, 2), addParts, removeParts);
817 get_bulk().modification_end();
818 ngpMesh.update_mesh();
819 check_bucket_layout(get_bulk(), { {{"block_1"}, {1}}, {{"block_3"}, {2, 3}}});
820 }
821 };
822
823 void move_data_between_fields_on_host(const stk::mesh::BulkData & bulk,
824 const stk::mesh::Field<int>& source,
825 stk::mesh::Field<int>& dest)
826 {
827 const stk::mesh::BucketVector& buckets = bulk.buckets(stk::topology::ELEMENT_RANK);
828 stk::mesh::NgpField<int>& ngpSource = stk::mesh::get_updated_ngp_field<int>(source);
829 ngpSource.sync_to_host();
830
831 for(size_t iBucket=0; iBucket<buckets.size(); iBucket++)
832 {
833 const stk::mesh::Bucket &bucket = *buckets[iBucket];
834
835 int* sourceData = static_cast<int*>(stk::mesh::field_data(source, bucket));
836 int* destData = static_cast<int*>(stk::mesh::field_data(dest, bucket));
837 for(size_t iEntity=0; iEntity<bucket.size(); iEntity++)
838 {
839 *destData = *sourceData;
840 }
841 }
842
843 stk::mesh::NgpField<int>& ngpDest = stk::mesh::get_updated_ngp_field<int>(dest);
844 ngpDest.modify_on_host();
845 }
846
847 struct CheckFieldValues {
848 CheckFieldValues(stk::mesh::NgpField<int>& _ngpField, unsigned _numScalarsPerEntity, int _expectedFieldValue)
849 : ngpField(_ngpField), numScalarsPerEntity(_numScalarsPerEntity), expectedFieldValue(_expectedFieldValue)
850 {
851 }
852
853 KOKKOS_FUNCTION
854 void operator()(const stk::mesh::FastMeshIndex& entity) const
855 {
856 for (unsigned component = 0; component < numScalarsPerEntity; component++) {
857 STK_NGP_ThrowRequire(ngpField(entity, component) == expectedFieldValue);
858 }
859 }
860
861 private:
862 stk::mesh::NgpField<int> ngpField;
863 unsigned numScalarsPerEntity;
864 int expectedFieldValue;
865 };
866
867 void test_field_values_on_device_without_initial_sync(stk::mesh::BulkData& bulk,
868 const stk::mesh::Field<int>& stkField,
869 const stk::mesh::Part& part,
870 const int expectedFieldValue)
871 {
872 stk::mesh::NgpField<int> & ngpField = stk::mesh::get_updated_ngp_field<int>(stkField);
873
874 stk::mesh::Selector selection = bulk.mesh_meta_data().locally_owned_part() & part;
875 const stk::mesh::BucketVector& buckets = bulk.get_buckets(stkField.entity_rank(), selection);
876 const unsigned numScalarsPerEntity = stk::mesh::field_scalars_per_entity(stkField, *buckets[0]);
877 stk::mesh::NgpMesh& ngpMesh = stk::mesh::get_updated_ngp_mesh(bulk);
878
879 CheckFieldValues checkFieldValues(ngpField, numScalarsPerEntity, expectedFieldValue);
880 stk::mesh::for_each_entity_run(ngpMesh, stk::topology::ELEM_RANK, selection, checkFieldValues);
881 }
882
883 void test_field_values_on_device(stk::mesh::BulkData& bulk,
884 const stk::mesh::Field<int>& stkField,
885 const stk::mesh::Part& part,
886 const int expectedFieldValue)
887 {
888 stk::mesh::NgpField<int> & ngpField = stk::mesh::get_updated_ngp_field<int>(stkField);
889 ngpField.sync_to_device();
890
891 test_field_values_on_device_without_initial_sync(bulk, stkField, part, expectedFieldValue);
892 }
893
894 void test_field_values_on_device(stk::mesh::BulkData &bulk,
895 const stk::mesh::Field<int> & stkField,
896 const int expectedFieldValue)
897 {
898 test_field_values_on_device(bulk, stkField, bulk.mesh_meta_data().locally_owned_part(), expectedFieldValue);
899 }
900
901 void test_field_values_on_host_without_initial_sync(const stk::mesh::BulkData& bulk,
902 const stk::mesh::Field<int>& stkField,
903 const stk::mesh::Part& part,
904 const int expectedFieldValue)
905 {
906 stk::mesh::Selector selection = bulk.mesh_meta_data().locally_owned_part() & part;
907 const stk::mesh::BucketVector& buckets = bulk.get_buckets(stkField.entity_rank(), selection);
908 for (size_t iBucket=0; iBucket<buckets.size(); iBucket++) {
909 const stk::mesh::Bucket &bucket = *buckets[iBucket];
910 const unsigned numScalarsPerEntity = stk::mesh::field_scalars_per_entity(stkField, bucket);
911
912 int* fieldData = reinterpret_cast<int*>(stk::mesh::field_data(stkField, bucket));
913 for (size_t iEntity=0; iEntity<bucket.size(); iEntity++) {
914 for (unsigned component=0; component<numScalarsPerEntity; component++) {
915 EXPECT_EQ(expectedFieldValue, fieldData[component]);
916 }
917 }
918 }
919 }
920
921 void test_field_values_on_host(const stk::mesh::BulkData& bulk,
922 const stk::mesh::Field<int>& stkField,
923 const stk::mesh::Part& part,
924 const int expectedFieldValue)
925 {
926 stkField.sync_to_host();
927
928 test_field_values_on_host_without_initial_sync(bulk, stkField, part, expectedFieldValue);
929 }
930
931 void test_field_values_on_host(const stk::mesh::BulkData& bulk,
932 const stk::mesh::Field<int>& stkField,
933 const int expectedFieldValue)
934 {
935 test_field_values_on_host(bulk, stkField, bulk.mesh_meta_data().locally_owned_part(), expectedFieldValue);
936 }
937
938 template <typename T>
939 void initialize_ngp_field(stk::mesh::Field<T> & stkField)
940 {
941 stk::mesh::get_updated_ngp_field<T>(stkField);
942 }
943
944 template <typename T>
945 void multiply_field_data_on_host(stk::mesh::BulkData & bulk, stk::mesh::Field<T> & field, int multiplier)
946 {
947 const stk::mesh::BucketVector& buckets = bulk.buckets(field.entity_rank());
948 for (stk::mesh::Bucket * bucket : buckets) {
949 T * fieldData = stk::mesh::field_data(field, *bucket);
950 for(size_t iEntity = 0; iEntity < bucket->size(); ++iEntity) {
951 fieldData[iEntity] *= multiplier;
952 }
953 }
954 }
955
956 template <typename T>
957 void modify_field_on_host(stk::mesh::BulkData & bulk, stk::mesh::Field<T> & field, int multiplier)
958 {
959 multiply_field_data_on_host(bulk, field, multiplier);
960 field.modify_on_host();
961 }
962
963 void modify_mesh_part_membership(stk::mesh::BulkData & bulk, stk::mesh::Part & part)
964 {
965 stk::mesh::PartVector addParts(1, &part);
966 stk::mesh::EntityVector allElements;
967
968 stk::mesh::get_entities(bulk, stk::topology::ELEM_RANK, allElements);
969
970 bulk.modification_begin();
971 bulk.change_entity_parts(allElements[0], addParts);
972 bulk.modification_end();
973 }
974
975 template <typename T>
976 void sync_field_to_device(stk::mesh::Field<T> & field)
977 {
978 field.sync_to_device();
979 }
980
981 template <typename T>
982 void multiply_field_data_on_device(stk::mesh::BulkData & bulk, stk::mesh::Field<T> & stkField, int multiplier)
983 {
984 stk::mesh::NgpMesh & ngpMesh = stk::mesh::get_updated_ngp_mesh(bulk);
985 const stk::mesh::MetaData & meta = bulk.mesh_meta_data();
986 stk::mesh::NgpField<T> & ngpField = stk::mesh::get_updated_ngp_field<T>(stkField);
987
988 stk::mesh::for_each_entity_run(ngpMesh, stk::topology::ELEM_RANK, meta.locally_owned_part(),
989 KOKKOS_LAMBDA(const stk::mesh::FastMeshIndex& entity) {
990 const int numScalarsPerEntity = ngpField.get_num_components_per_entity(entity);
991 for (int component=0; component<numScalarsPerEntity; component++) {
992 ngpField(entity, component) *= multiplier;
993 }
994 });
995 }
996
997 template <typename T>
998 void modify_field_on_device(stk::mesh::BulkData & bulk, stk::mesh::Field<T> & stkField, int multiplier)
999 {
1000 stk::mesh::NgpField<T> & ngpField = stk::mesh::get_updated_ngp_field<T>(stkField);
1001 ngpField.sync_to_device();
1002
1003 multiply_field_data_on_device(bulk, stkField, multiplier);
1004
1005 stkField.modify_on_device();
1006 }
1007
1008 template <typename T>
1009 void modify_field_on_device(stk::mesh::BulkData& bulk, stk::mesh::Field<T>& stkField, stk::mesh::Part& part, int value)
1010 {
1011 stk::mesh::NgpMesh & ngpMesh = stk::mesh::get_updated_ngp_mesh(bulk);
1012 const stk::mesh::MetaData & meta = bulk.mesh_meta_data();
1013 stk::mesh::NgpField<T> & ngpField = stk::mesh::get_updated_ngp_field<T>(stkField);
1014 ngpField.sync_to_device();
1015
1016 stk::mesh::Selector selection = meta.locally_owned_part() & part;
1017 const stk::mesh::BucketVector& buckets = bulk.get_buckets(stkField.entity_rank(), selection);
1018 const unsigned numScalarsPerEntity = stk::mesh::field_scalars_per_entity(stkField, *buckets[0]);
1019 stk::mesh::for_each_entity_run(ngpMesh, stk::topology::ELEM_RANK, selection,
1020 KOKKOS_LAMBDA(const stk::mesh::FastMeshIndex& entity) {
1021 for (unsigned component=0; component<numScalarsPerEntity; component++) {
1022 ngpField(entity, component) = value;
1023 }
1024 });
1025 stkField.modify_on_device();
1026 }
1027
1028 template <typename T>
1029 void sync_field_to_host(stk::mesh::Field<T> & stkField)
1030 {
1031 stkField.sync_to_host();
1032 }
1033
1034 template <typename T>
1035 void check_field_on_host(const stk::mesh::BulkData & bulk,
1036 const stk::mesh::Field<T>& stkField,
1037 int expectedValue)
1038 {
1039 const stk::mesh::BucketVector& buckets = bulk.buckets(stkField.entity_rank());
1040 for (stk::mesh::Bucket * bucket : buckets) {
1041 T * fieldData = stk::mesh::field_data(stkField, *bucket);
1042 for(size_t iEntity = 0; iEntity < bucket->size(); ++iEntity) {
1043 EXPECT_EQ(fieldData[iEntity], expectedValue);
1044 }
1045 }
1046 }
1047
1048 TEST_F(NgpFieldFixture, noFieldDataTest)
1049 {
1050 if (get_parallel_size() != 1) GTEST_SKIP();
1051
1052 unsigned numBlocks = 1;
1053 const unsigned bucketCapacity = 1;
1054 setup_empty_mesh(stk::mesh::BulkData::NO_AUTO_AURA, bucketCapacity, bucketCapacity);
1055
1056 std::string meshDesc = stk::unit_test_util::get_many_block_mesh_desc(numBlocks);
1057 std::vector<double> coordinates = stk::unit_test_util::get_many_block_coordinates(numBlocks);
1058
1059 stk::mesh::Field<int>& field = get_meta().declare_field<int>(stk::topology::ELEM_RANK, "", 1);
1060 stk::unit_test_util::setup_text_mesh(get_bulk(), stk::unit_test_util::get_full_text_mesh_desc(meshDesc, coordinates));
1061 EXPECT_NO_THROW(stk::mesh::get_updated_ngp_field<int>(field));
1062 }
1063
1064 TEST_F(NgpFieldFixture, ModifyOnHostFlagClearedOnInitialNgpFieldConstruction)
1065 {
1066 if (get_parallel_size() != 1) return;
1067
1068 setup_one_field_one_element_mesh();
1069
1070 stk::mesh::Field<int>& field1 = *get_meta().get_field<int>(stk::topology::ELEM_RANK, "field1");
1071 field1.modify_on_host();
1072
1073 auto ngpfield = stk::mesh::get_updated_ngp_field<int>(field1);
1074 EXPECT_FALSE(field1.need_sync_to_device());
1075 }
1076
1077 TEST_F(NgpFieldFixture, InvalidModifyFlagCondition)
1078 {
1079 if (get_parallel_size() != 1) return;
1080
1081 setup_one_field_one_element_mesh();
1082
1083 stk::mesh::Field<int>& field1 = *get_meta().get_field<int>(stk::topology::ELEM_RANK, "field1");
1084
1085 auto ngpfield = stk::mesh::get_updated_ngp_field<int>(field1);
1086 EXPECT_FALSE(field1.need_sync_to_device());
1087
1088 field1.modify_on_host();
1089 EXPECT_THROW(ngpfield.modify_on_device(), std::logic_error);
1090 }
1091
1092 TEST_F(NgpFieldFixture, PersistentModifyOnDeviceFlag)
1093 {
1094 if (get_parallel_size() != 1) GTEST_SKIP();
1095
1096 setup_one_field_one_element_mesh();
1097
1098 stk::mesh::Field<int>& field1 = *get_meta().get_field<int>(stk::topology::ELEM_RANK, "field1");
1099 EXPECT_FALSE(field1.need_sync_to_host());
1100 field1.sync_to_device();
1101 field1.modify_on_device();
1102
1103 auto ngpfield = stk::mesh::get_updated_ngp_field<int>(field1);
1104 EXPECT_TRUE(field1.need_sync_to_host());
1105
1106 multiply_field_data_on_device(get_bulk(), field1, 2);
1107 ngpfield.sync_to_host();
1108
1109 test_field_values_on_host_without_initial_sync(get_bulk(), field1, get_meta().universal_part(), 2);
1110 }
1111
1112 TEST_F(NgpFieldFixture, noOverwriteInVariableLengthFields)
1113 {
1114 if (get_parallel_size() != 1) GTEST_SKIP();
1115
1116 setup_two_field_two_element_mesh();
1117
1118 stk::mesh::Field<int>& variableLengthField = *get_meta().get_field<int>(stk::topology::ELEM_RANK, "variableLengthField");
1119 stk::mesh::Field<int>& potentiallyOverwrittenField = *get_meta().get_field<int>(stk::topology::ELEM_RANK, "potentiallyOverwrittenField");
1120
1121 modify_field_on_device(get_bulk(), variableLengthField, 2);
1122 modify_field_on_host(get_bulk(), potentiallyOverwrittenField, 3);
1123 variableLengthField.sync_to_host();
1124
1125 test_field_values_on_host(get_bulk(), potentiallyOverwrittenField, 3);
1126 }
1127
1128 TEST_F(NgpFieldFixture, FieldCopyVariableLengthField)
1129 {
1130 if (get_parallel_size() != 1) GTEST_SKIP();
1131
1132 const int numComponent1 = 8;
1133 const int numComponent2 = 3;
1134 setup_two_fields_five_hex_three_block_mesh<int>(numComponent1, numComponent2);
1135
1136 auto ngpMesh = stk::mesh::get_updated_ngp_mesh(get_bulk());
1137 auto stkField1 = get_meta().get_field<int>(stk::topology::ELEM_RANK, "variableLengthField1");
1138 auto stkField2 = get_meta().get_field<int>(stk::topology::ELEM_RANK, "variableLengthField2");
1139 auto elem2 = get_bulk().get_entity(stk::topology::ELEMENT_RANK, 2);
1140 auto elem4 = get_bulk().get_entity(stk::topology::ELEMENT_RANK, 4);
1141
1142 int* inData2 = stk::mesh::field_data(*stkField1, elem2);
1143 int* inData4 = stk::mesh::field_data(*stkField1, elem4);
1144
1145 for(int c = 0; c < numComponent1; ++c) {
1146 inData2[c] = 2 + c;
1147 }
1148 for(int c = 0; c < numComponent2; ++c) {
1149 inData4[c] = numComponent1 + 3 + c;
1150 }
1151
1152 copy_fields_on_device<int>(ngpMesh, stkField1, stkField2, get_meta().universal_part());
1153 stkField2->sync_to_host();
1154
1155 int* outData2 = stk::mesh::field_data(*stkField2, elem2);
1156 int* outData4 = stk::mesh::field_data(*stkField2, elem4);
1157
1158 for(int c = 0; c < numComponent1; ++c) {
1159 EXPECT_EQ(2 + c, outData2[c]);
1160 }
1161 for(int c = 0; c < numComponent2; ++c) {
1162 EXPECT_EQ(numComponent1 + 3 + c, outData4[c]);
1163 }
1164 }
1165
1166 #ifdef STK_USE_DEVICE_MESH
1167 TEST_F(NgpFieldFixture, DeviceField_set_all_after_modified_on_host)
1168 {
1169 if (get_parallel_size() != 1) GTEST_SKIP();
1170
1171 const int numComponent1 = 8;
1172 const int numComponent2 = 3;
1173 setup_two_fields_five_hex_three_block_mesh<double>(numComponent1, numComponent2);
1174
1175 auto ngpMesh = stk::mesh::get_updated_ngp_mesh(get_bulk());
1176 auto stkField1 = get_meta().get_field<double>(stk::topology::ELEM_RANK, "variableLengthField1");
1177
1178 EXPECT_FALSE(stkField1->need_sync_to_host());
1179 EXPECT_FALSE(stkField1->need_sync_to_device());
1180
1181 stk::mesh::NgpField<double> ngpField1 = stk::mesh::get_updated_ngp_field<double>(*stkField1);
1182
1183 ngpField1.modify_on_host();
1184 EXPECT_TRUE(stkField1->need_sync_to_device());
1185
1186 ngpField1.set_all(ngpMesh, 97.9);
1187
1188 EXPECT_TRUE(ngpField1.need_sync_to_host());
1189 }
1190
1191 TEST_F(NgpFieldFixture, blas_field_copy_device_to_device)
1192 {
1193 if (get_parallel_size() != 1) GTEST_SKIP();
1194
1195 const int numComponent1 = 8;
1196 const int numComponent2 = 3;
1197 setup_two_fields_five_hex_three_block_mesh<double>(numComponent1, numComponent2);
1198
1199 auto ngpMesh = stk::mesh::get_updated_ngp_mesh(get_bulk());
1200 auto stkField1 = get_meta().get_field<double>(stk::topology::ELEM_RANK, "variableLengthField1");
1201 auto stkField2 = get_meta().get_field<double>(stk::topology::ELEM_RANK, "variableLengthField2");
1202
1203 EXPECT_FALSE(stkField1->need_sync_to_host());
1204 EXPECT_FALSE(stkField2->need_sync_to_host());
1205 EXPECT_FALSE(stkField1->need_sync_to_device());
1206 EXPECT_FALSE(stkField2->need_sync_to_device());
1207
1208
1209 const double myConstantValue = 97.9;
1210 stk::mesh::field_fill(myConstantValue, *stkField1, stk::ngp::ExecSpace());
1211
1212 #ifdef STK_ENABLE_GPU
1213 stk::mesh::NgpField<double>& ngpField1 = stk::mesh::get_updated_ngp_field<double>(*stkField1);
1214 EXPECT_TRUE(ngpField1.need_sync_to_host());
1215 #endif
1216
1217 stk::mesh::field_copy(*stkField1, *stkField2);
1218
1219 #ifdef STK_ENABLE_GPU
1220 EXPECT_TRUE(stkField1->need_sync_to_host());
1221 EXPECT_TRUE(stkField2->need_sync_to_host());
1222 #endif
1223
1224 stk::mesh::Selector selector(*stkField2);
1225 stk::mesh::NgpField<double>& ngpField2 = stk::mesh::get_updated_ngp_field<double>(*stkField2);
1226 ngp_field_test_utils::check_field_data_on_device(ngpMesh, ngpField2, selector, myConstantValue);
1227 }
1228
1229 #endif
1230
1231 void check_expected_num_elements(const stk::mesh::BulkData & bulk, unsigned numElements)
1232 {
1233 std::vector<size_t> counts;
1234 const stk::mesh::Part & locallyOwnedPart = bulk.mesh_meta_data().locally_owned_part();
1235 stk::mesh::count_entities(locallyOwnedPart, bulk, counts);
1236 ASSERT_EQ(counts[stk::topology::ELEM_RANK], numElements);
1237 }
1238
1239 class NumScalarsPerEntity
1240 {
1241 public:
1242 NumScalarsPerEntity() = default;
1243 NumScalarsPerEntity(unsigned scalar0, unsigned scalar1)
1244 {
1245 m_numScalars[0] = scalar0;
1246 m_numScalars[1] = scalar1;
1247 }
1248
1249 KOKKOS_INLINE_FUNCTION
1250 unsigned& operator[](unsigned entityIndex)
1251 {
1252 return m_numScalars[entityIndex];
1253 }
1254
1255 KOKKOS_INLINE_FUNCTION
1256 const unsigned& operator[](const unsigned entityIndex) const
1257 {
1258 return m_numScalars[entityIndex];
1259 }
1260
1261 private:
1262 unsigned m_numScalars[2];
1263 };
1264
1265 void fill_gold_num_scalars_per_entity(const stk::mesh::BulkData & bulk, const stk::mesh::FieldBase & variableLengthField,
1266 unsigned numElements, NumScalarsPerEntity& goldNumScalarsPerEntity)
1267 {
1268 stk::mesh::EntityVector elements;
1269 stk::mesh::get_selected_entities(bulk.mesh_meta_data().locally_owned_part(), bulk.buckets(stk::topology::ELEM_RANK), elements);
1270 for (const stk::mesh::Entity & element : elements) {
1271 const unsigned index = bulk.identifier(element) - 1;
1272 ASSERT_LT(index, numElements);
1273 goldNumScalarsPerEntity[index] = stk::mesh::field_scalars_per_entity(variableLengthField, element);
1274 }
1275 }
1276
1277 struct CheckNumScalarsPerEntity {
1278 CheckNumScalarsPerEntity(stk::mesh::NgpMesh& _ngpMesh,
1279 stk::mesh::NgpField<int>& _ngpVariableLengthField,
1280 NumScalarsPerEntity _goldNumScalarsPerEntity)
1281 : ngpMesh(_ngpMesh),
1282 ngpVariableLengthField(_ngpVariableLengthField),
1283 goldNumScalarsPerEntity(_goldNumScalarsPerEntity)
1284 {
1285 }
1286
1287 KOKKOS_FUNCTION
1288 void operator()(const stk::mesh::FastMeshIndex& index) const
1289 {
1290 const stk::mesh::Entity element = ngpMesh.get_entity(stk::topology::ELEM_RANK, index);
1291 const unsigned goldIndex = ngpMesh.identifier(element) - 1;
1292 NGP_EXPECT_EQ(ngpVariableLengthField.get_num_components_per_entity(index), goldNumScalarsPerEntity[goldIndex]);
1293 }
1294
1295 private:
1296 stk::mesh::NgpMesh ngpMesh;
1297 stk::mesh::NgpField<int> ngpVariableLengthField;
1298 NumScalarsPerEntity goldNumScalarsPerEntity;
1299 };
1300
1301 struct CheckExtentsPerEntity {
1302 CheckExtentsPerEntity(stk::mesh::NgpMesh& _ngpMesh,
1303 stk::mesh::NgpField<int>& _ngpVariableLengthField,
1304 NumScalarsPerEntity _goldVariableLengthExtent0PerEntity,
1305 NumScalarsPerEntity _goldVariableLengthExtent1PerEntity,
1306 stk::mesh::NgpField<int>& _ngpPartiallyAllocatedField,
1307 NumScalarsPerEntity _goldPartiallyAllocatedExtent0PerEntity,
1308 NumScalarsPerEntity _goldPartiallyAllocatedExtent1PerEntity)
1309 : ngpMesh(_ngpMesh),
1310 ngpVariableLengthField(_ngpVariableLengthField),
1311 goldVariableLengthExtent0PerEntity(_goldVariableLengthExtent0PerEntity),
1312 goldVariableLengthExtent1PerEntity(_goldVariableLengthExtent1PerEntity),
1313 ngpPartiallyAllocatedField(_ngpPartiallyAllocatedField),
1314 goldPartiallyAllocatedExtent0PerEntity(_goldPartiallyAllocatedExtent0PerEntity),
1315 goldPartiallyAllocatedExtent1PerEntity(_goldPartiallyAllocatedExtent1PerEntity)
1316 {
1317 }
1318
1319 KOKKOS_FUNCTION
1320 void operator()(const stk::mesh::FastMeshIndex& index) const
1321 {
1322 const stk::mesh::Entity element = ngpMesh.get_entity(stk::topology::ELEM_RANK, index);
1323 const unsigned goldIndex = ngpMesh.identifier(element) - 1;
1324
1325 NGP_EXPECT_EQ(ngpVariableLengthField.get_extent0_per_entity(index), goldVariableLengthExtent0PerEntity[goldIndex]);
1326 NGP_EXPECT_EQ(ngpVariableLengthField.get_extent1_per_entity(index), goldVariableLengthExtent1PerEntity[goldIndex]);
1327 NGP_EXPECT_EQ(ngpVariableLengthField.get_extent_per_entity(index, 0), goldVariableLengthExtent0PerEntity[goldIndex]);
1328 NGP_EXPECT_EQ(ngpVariableLengthField.get_extent_per_entity(index, 1), goldVariableLengthExtent1PerEntity[goldIndex]);
1329
1330 NGP_EXPECT_EQ(ngpPartiallyAllocatedField.get_extent0_per_entity(index), goldPartiallyAllocatedExtent0PerEntity[goldIndex]);
1331 NGP_EXPECT_EQ(ngpPartiallyAllocatedField.get_extent1_per_entity(index), goldPartiallyAllocatedExtent1PerEntity[goldIndex]);
1332 NGP_EXPECT_EQ(ngpPartiallyAllocatedField.get_extent_per_entity(index, 0), goldPartiallyAllocatedExtent0PerEntity[goldIndex]);
1333 NGP_EXPECT_EQ(ngpPartiallyAllocatedField.get_extent_per_entity(index, 1), goldPartiallyAllocatedExtent1PerEntity[goldIndex]);
1334 }
1335
1336 private:
1337 stk::mesh::NgpMesh ngpMesh;
1338 stk::mesh::NgpField<int> ngpVariableLengthField;
1339 NumScalarsPerEntity goldVariableLengthExtent0PerEntity;
1340 NumScalarsPerEntity goldVariableLengthExtent1PerEntity;
1341 stk::mesh::NgpField<int> ngpPartiallyAllocatedField;
1342 NumScalarsPerEntity goldPartiallyAllocatedExtent0PerEntity;
1343 NumScalarsPerEntity goldPartiallyAllocatedExtent1PerEntity;
1344 };
1345
1346 void test_num_scalars_per_entity(stk::mesh::BulkData & bulk, const stk::mesh::FieldBase & variableLengthField)
1347 {
1348 const unsigned numElements = 2;
1349 check_expected_num_elements(bulk, numElements);
1350
1351 NumScalarsPerEntity goldNumScalarsPerEntity;
1352 fill_gold_num_scalars_per_entity(bulk, variableLengthField, numElements, goldNumScalarsPerEntity);
1353
1354 stk::mesh::NgpMesh& ngpMesh = stk::mesh::get_updated_ngp_mesh(bulk);
1355 stk::mesh::NgpField<int> ngpVariableLengthField = stk::mesh::get_updated_ngp_field<int>(variableLengthField);
1356
1357 CheckNumScalarsPerEntity checkNumScalarsPerEntity(ngpMesh, ngpVariableLengthField, goldNumScalarsPerEntity);
1358 stk::mesh::for_each_entity_run(
1359 ngpMesh, stk::topology::ELEM_RANK, bulk.mesh_meta_data().locally_owned_part(), checkNumScalarsPerEntity);
1360 }
1361
1362 void test_extents_per_entity(stk::mesh::BulkData & bulk,
1363 const stk::mesh::FieldBase & variableLengthField,
1364 const stk::mesh::FieldBase & partiallyAllocatedField)
1365 {
1366 const unsigned numElements = 2;
1367 check_expected_num_elements(bulk, numElements);
1368
1369 stk::mesh::NgpMesh& ngpMesh = stk::mesh::get_updated_ngp_mesh(bulk);
1370 stk::mesh::NgpField<int> ngpVariableLengthField = stk::mesh::get_updated_ngp_field<int>(variableLengthField);
1371 stk::mesh::NgpField<int> ngpPartiallyAllocatedField = stk::mesh::get_updated_ngp_field<int>(partiallyAllocatedField);
1372
1373 NumScalarsPerEntity goldVariableLengthExtent0PerEntity(3, 3);
1374 NumScalarsPerEntity goldVariableLengthExtent1PerEntity(4, 5);
1375 NumScalarsPerEntity goldPartiallyAllocatedExtent0PerEntity(2, 0);
1376 NumScalarsPerEntity goldPartiallyAllocatedExtent1PerEntity(1, 0);
1377
1378 CheckExtentsPerEntity checkExtentsPerEntity(ngpMesh,
1379 ngpVariableLengthField, goldVariableLengthExtent0PerEntity, goldVariableLengthExtent1PerEntity,
1380 ngpPartiallyAllocatedField, goldPartiallyAllocatedExtent0PerEntity, goldPartiallyAllocatedExtent1PerEntity);
1381
1382 stk::mesh::for_each_entity_run(
1383 ngpMesh, stk::topology::ELEM_RANK, bulk.mesh_meta_data().locally_owned_part(), checkExtentsPerEntity);
1384 }
1385
1386 TEST_F(NgpFieldFixture, NumScalarsPerEntityOnDevice)
1387 {
1388 if (get_parallel_size() != 1) return;
1389
1390 setup_two_field_two_element_mesh();
1391
1392 stk::mesh::Field<int>& variableLengthField = *get_meta().get_field<int>(stk::topology::ELEM_RANK, "variableLengthField");
1393
1394 test_num_scalars_per_entity(get_bulk(), variableLengthField);
1395 }
1396
1397 TEST_F(NgpFieldFixture, ExtentsPerEntityOnDevice)
1398 {
1399 if (get_parallel_size() != 1) return;
1400
1401 setup_two_variable_fields_two_element_mesh();
1402
1403 stk::mesh::Field<int>& variableLengthField = *get_meta().get_field<int>(stk::topology::ELEM_RANK, "variableLengthField");
1404 stk::mesh::Field<int>& partiallyAllocatedField = *get_meta().get_field<int>(stk::topology::ELEM_RANK, "partiallyAllocatedField");
1405
1406 test_extents_per_entity(get_bulk(), variableLengthField, partiallyAllocatedField);
1407 }
1408
1409 TEST_F(NgpFieldFixture, TestAriaAlgorithm)
1410 {
1411 if (stk::parallel_machine_size(MPI_COMM_WORLD) != 1) return;
1412
1413 setup_empty_mesh(stk::mesh::BulkData::AUTO_AURA);
1414 stk::mesh::Field<int> & f1 = create_field<int>(stk::topology::ELEM_RANK, "f1");
1415 stk::mesh::Field<int> & f2 = create_field<int>(stk::topology::ELEM_RANK, "f2");
1416
1417 stk::io::fill_mesh("generated:1x1x1", get_bulk());
1418
1419 stk::mesh::NgpMesh ngpMesh = stk::mesh::get_updated_ngp_mesh(get_bulk());
1420
1421 int multiplier = 2;
1422 modify_field_on_device(get_bulk(), f1, multiplier);
1423
1424 move_data_between_fields_on_host(get_bulk(), f1, f2);
1425
1426 test_field_values_on_device(get_bulk(), f1, multiplier);
1427 test_field_values_on_device(get_bulk(), f2, multiplier);
1428
1429 test_field_values_on_host(get_bulk(), f1, multiplier);
1430 test_field_values_on_host(get_bulk(), f2, multiplier);
1431 }
1432
1433 TEST_F(NgpFieldFixture, GetNgpField)
1434 {
1435 if (stk::parallel_machine_size(MPI_COMM_WORLD) != 1) return;
1436
1437 setup_empty_mesh(stk::mesh::BulkData::NO_AUTO_AURA);
1438 stk::mesh::Field<int> & stkIntField = create_field<int>(stk::topology::ELEM_RANK, "intField");
1439 stk::io::fill_mesh("generated:1x1x1", get_bulk());
1440
1441 stk::mesh::NgpField<int> & ngpIntField = stk::mesh::get_updated_ngp_field<int>(stkIntField);
1442 EXPECT_EQ(ngpIntField.get_ordinal(), stkIntField.mesh_meta_data_ordinal());
1443
1444 #ifdef STK_USE_DEVICE_MESH
1445 EXPECT_TRUE((std::is_same<decltype(ngpIntField), stk::mesh::DeviceField<int>&>::value));
1446 #else
1447 EXPECT_TRUE((std::is_same<decltype(ngpIntField), stk::mesh::HostField<int>&>::value));
1448 #endif
1449 }
1450
1451 TEST_F(NgpFieldFixture, GetMultipleNgpFields)
1452 {
1453 if (stk::parallel_machine_size(MPI_COMM_WORLD) != 1) return;
1454
1455 setup_empty_mesh(stk::mesh::BulkData::NO_AUTO_AURA);
1456 stk::mesh::Field<int> & stkIntField1 = create_field<int>(stk::topology::ELEM_RANK, "intField1");
1457 stk::mesh::Field<int> & stkIntField2 = create_field<int>(stk::topology::ELEM_RANK, "intField2");
1458 stk::mesh::Field<double> & stkDoubleField1 = create_field<double>(stk::topology::ELEM_RANK, "doubleField1");
1459 stk::mesh::Field<double> & stkDoubleField2 = create_field<double>(stk::topology::ELEM_RANK, "doubleField2");
1460
1461 stk::io::fill_mesh("generated:1x1x1", get_bulk());
1462
1463 stk::mesh::NgpField<int> & ngpIntField1 = stk::mesh::get_updated_ngp_field<int>(stkIntField1);
1464 stk::mesh::NgpField<int> & ngpIntField2 = stk::mesh::get_updated_ngp_field<int>(stkIntField2);
1465 stk::mesh::NgpField<double> & ngpDoubleField1 = stk::mesh::get_updated_ngp_field<double>(stkDoubleField1);
1466 stk::mesh::NgpField<double> & ngpDoubleField2 = stk::mesh::get_updated_ngp_field<double>(stkDoubleField2);
1467
1468 EXPECT_EQ(ngpIntField1.get_ordinal(), stkIntField1.mesh_meta_data_ordinal());
1469 EXPECT_EQ(ngpIntField2.get_ordinal(), stkIntField2.mesh_meta_data_ordinal());
1470 EXPECT_EQ(ngpDoubleField1.get_ordinal(), stkDoubleField1.mesh_meta_data_ordinal());
1471 EXPECT_EQ(ngpDoubleField2.get_ordinal(), stkDoubleField2.mesh_meta_data_ordinal());
1472 }
1473
1474 TEST_F(NgpFieldFixture, ModifyAndSync)
1475 {
1476 if (stk::parallel_machine_size(MPI_COMM_WORLD) != 1) return;
1477
1478 setup_empty_mesh(stk::mesh::BulkData::NO_AUTO_AURA);
1479 stk::mesh::Field<int> & stkIntField = create_field<int>(stk::topology::ELEM_RANK, "intField");
1480 stk::io::fill_mesh("generated:1x1x1", get_bulk());
1481
1482 initialize_ngp_field(stkIntField);
1483
1484 int multiplier = 2;
1485 modify_field_on_host(get_bulk(), stkIntField, multiplier);
1486 check_field_on_host(get_bulk(), stkIntField, multiplier);
1487
1488 sync_field_to_device(stkIntField);
1489 modify_field_on_device(get_bulk(), stkIntField, multiplier);
1490
1491 sync_field_to_host(stkIntField);
1492 check_field_on_host(get_bulk(), stkIntField, multiplier*multiplier);
1493
1494 size_t expectedSyncsToDevice = 2;
1495 size_t expectedSyncsToHost = 1;
1496
1497 EXPECT_EQ(expectedSyncsToDevice, stkIntField.num_syncs_to_device());
1498 EXPECT_EQ(expectedSyncsToHost, stkIntField.num_syncs_to_host());
1499
1500 stk::mesh::NgpField<int>& deviceNgpIntField = stk::mesh::get_updated_ngp_field<int>(stkIntField);
1501 stk::mesh::HostField<int> hostNgpIntField(get_bulk(), stkIntField);
1502
1503 EXPECT_EQ(expectedSyncsToDevice, deviceNgpIntField.num_syncs_to_device());
1504 EXPECT_EQ(expectedSyncsToDevice, hostNgpIntField.num_syncs_to_device());
1505 EXPECT_EQ(expectedSyncsToHost, deviceNgpIntField.num_syncs_to_host());
1506 EXPECT_EQ(expectedSyncsToHost, hostNgpIntField.num_syncs_to_host());
1507 }
1508
1509 TEST_F(NgpFieldFixture, UpdateNgpFieldAfterMeshMod_WithMostCurrentDataOnHost)
1510 {
1511 if (stk::parallel_machine_size(MPI_COMM_WORLD) != 1) return;
1512
1513 setup_empty_mesh(stk::mesh::BulkData::NO_AUTO_AURA);
1514 stk::mesh::Field<int> & stkIntField = create_field<int>(stk::topology::ELEM_RANK, "intField");
1515 stk::mesh::Part & dummyPart = get_meta().declare_part("DummyPart", stk::topology::ELEM_RANK);
1516 stk::io::fill_mesh("generated:1x1x2", get_bulk());
1517
1518 initialize_ngp_field(stkIntField);
1519
1520 int multiplier = 2;
1521 modify_field_on_host(get_bulk(), stkIntField, multiplier);
1522 modify_mesh_part_membership(get_bulk(), dummyPart);
1523 check_field_on_host(get_bulk(), stkIntField, multiplier);
1524
1525 sync_field_to_device(stkIntField);
1526 modify_field_on_device(get_bulk(), stkIntField, multiplier);
1527
1528 sync_field_to_host(stkIntField);
1529 check_field_on_host(get_bulk(), stkIntField, multiplier*multiplier);
1530
1531 size_t expectedSyncsToDevice = 3;
1532 size_t expectedSyncsToHost = 1;
1533
1534 EXPECT_EQ(expectedSyncsToDevice, stkIntField.num_syncs_to_device());
1535 EXPECT_EQ(expectedSyncsToHost, stkIntField.num_syncs_to_host());
1536
1537 stk::mesh::NgpField<int>& deviceNgpIntField = stk::mesh::get_updated_ngp_field<int>(stkIntField);
1538 stk::mesh::HostField<int> hostNgpIntField(get_bulk(), stkIntField);
1539
1540 EXPECT_EQ(expectedSyncsToDevice, deviceNgpIntField.num_syncs_to_device());
1541 EXPECT_EQ(expectedSyncsToDevice, hostNgpIntField.num_syncs_to_device());
1542 EXPECT_EQ(expectedSyncsToHost, deviceNgpIntField.num_syncs_to_host());
1543 EXPECT_EQ(expectedSyncsToHost, hostNgpIntField.num_syncs_to_host());
1544 }
1545
1546 TEST_F(NgpFieldFixture, UpdateNgpFieldAfterMeshMod_WithMostCurrentDataOnDevice)
1547 {
1548 if (stk::parallel_machine_size(MPI_COMM_WORLD) != 1) return;
1549
1550 setup_empty_mesh(stk::mesh::BulkData::NO_AUTO_AURA);
1551 stk::mesh::Field<int> & stkIntField = create_field<int>(stk::topology::ELEM_RANK, "intField");
1552 stk::mesh::Part & dummyPart = get_meta().declare_part("DummyPart", stk::topology::ELEM_RANK);
1553 stk::io::fill_mesh("generated:1x1x2", get_bulk());
1554
1555 initialize_ngp_field(stkIntField);
1556
1557 int multiplier = 2;
1558 modify_field_on_device(get_bulk(), stkIntField, multiplier);
1559 modify_mesh_part_membership(get_bulk(), dummyPart);
1560
1561 sync_field_to_host(stkIntField);
1562 }
1563
1564 TEST_F(NgpFieldFixture, ConsistentNeedToSyncForAllCopies)
1565 {
1566 if (stk::parallel_machine_size(MPI_COMM_WORLD) != 1) return;
1567
1568 setup_empty_mesh(stk::mesh::BulkData::NO_AUTO_AURA);
1569 stk::mesh::Field<int> & stkIntField = create_field<int>(stk::topology::ELEM_RANK, "intField");
1570 get_meta().declare_part("DummyPart", stk::topology::ELEM_RANK);
1571 stk::io::fill_mesh("generated:1x1x2", get_bulk());
1572
1573 initialize_ngp_field(stkIntField);
1574 stk::mesh::NgpField<int> copyNgpField = stk::mesh::get_updated_ngp_field<int>(stkIntField);
1575
1576 int multiplier = 2;
1577 modify_field_on_host(get_bulk(), stkIntField, multiplier);
1578 check_field_on_host(get_bulk(), stkIntField, multiplier);
1579
1580 copyNgpField.sync_to_device();
1581 modify_field_on_device(get_bulk(), stkIntField, multiplier);
1582
1583 copyNgpField.sync_to_host();
1584 check_field_on_host(get_bulk(), stkIntField, multiplier*multiplier);
1585
1586 size_t expectedSyncsToDevice = 2;
1587 size_t expectedSyncsToHost = 1;
1588
1589 EXPECT_EQ(expectedSyncsToDevice, stkIntField.num_syncs_to_device());
1590 EXPECT_EQ(expectedSyncsToHost, stkIntField.num_syncs_to_host());
1591
1592 EXPECT_EQ(stkIntField.num_syncs_to_device(), copyNgpField.num_syncs_to_device());
1593 EXPECT_EQ(stkIntField.num_syncs_to_host(), copyNgpField.num_syncs_to_host());
1594 }
1595
1596 TEST_F(NgpFieldFixture, ConsistentNeedToSyncForAllCopiesNoModifyCall)
1597 {
1598 if (stk::parallel_machine_size(MPI_COMM_WORLD) != 1) return;
1599
1600 setup_empty_mesh(stk::mesh::BulkData::NO_AUTO_AURA);
1601 stk::mesh::Field<int> & stkIntField = create_field<int>(stk::topology::ELEM_RANK, "intField");
1602 get_meta().declare_part("DummyPart", stk::topology::ELEM_RANK);
1603 stk::io::fill_mesh("generated:1x1x2", get_bulk());
1604
1605 initialize_ngp_field(stkIntField);
1606 stk::mesh::NgpField<int> copyNgpField = stk::mesh::get_updated_ngp_field<int>(stkIntField);
1607
1608 int multiplier = 1;
1609 check_field_on_host(get_bulk(), stkIntField, multiplier);
1610 copyNgpField.sync_to_device();
1611 copyNgpField.sync_to_host();
1612 check_field_on_host(get_bulk(), stkIntField, multiplier);
1613
1614 size_t expectedSyncsToDevice = 1;
1615 size_t expectedSyncsToHost = 0;
1616
1617 EXPECT_EQ(expectedSyncsToDevice, stkIntField.num_syncs_to_device());
1618 EXPECT_EQ(expectedSyncsToHost, stkIntField.num_syncs_to_host());
1619
1620 EXPECT_EQ(stkIntField.num_syncs_to_device(), copyNgpField.num_syncs_to_device());
1621 EXPECT_EQ(stkIntField.num_syncs_to_host(), copyNgpField.num_syncs_to_host());
1622 }
1623
1624 TEST_F(NgpFieldFixture, RequireSyncBetweenModifyOnHostAndDevice)
1625 {
1626 if (stk::parallel_machine_size(MPI_COMM_WORLD) != 1) return;
1627
1628 setup_empty_mesh(stk::mesh::BulkData::NO_AUTO_AURA);
1629 stk::mesh::Field<int> & stkIntField = create_field<int>(stk::topology::ELEM_RANK, "intField");
1630 get_meta().declare_part("DummyPart", stk::topology::ELEM_RANK);
1631 stk::io::fill_mesh("generated:1x1x2", get_bulk());
1632
1633 initialize_ngp_field(stkIntField);
1634 stk::mesh::NgpField<int>& ngpField = stk::mesh::get_updated_ngp_field<int>(stkIntField);
1635
1636 int multiplier = 2;
1637 modify_field_on_host(get_bulk(), stkIntField, multiplier);
1638 check_field_on_host(get_bulk(), stkIntField, multiplier);
1639 #ifdef KOKKOS_ENABLE_DEBUG_DUALVIEW_MODIFY_CHECK
1640 EXPECT_THROW(ngpField.modify_on_device(), std::logic_error);
1641 #endif
1642
1643 stkIntField.clear_sync_state();
1644 ngpField.modify_on_device();
1645 #ifdef KOKKOS_ENABLE_DEBUG_DUALVIEW_MODIFY_CHECK
1646 EXPECT_THROW(ngpField.modify_on_host(), std::logic_error);
1647 #endif
1648 }
1649
1650 TEST_F(NgpFieldFixture, ClearSyncStateAfterModifyOnDevice)
1651 {
1652 if(stk::parallel_machine_size(MPI_COMM_WORLD) != 1) { return; }
1653
1654 setup_empty_mesh(stk::mesh::BulkData::NO_AUTO_AURA);
1655 stk::mesh::Field<int>& stkIntField = create_field<int>(stk::topology::ELEM_RANK, "intField");
1656 stk::io::fill_mesh("generated:1x1x1", get_bulk());
1657
1658 stk::mesh::NgpField<int>& ngpField = stk::mesh::get_updated_ngp_field<int>(stkIntField);
1659
1660 ngpField.modify_on_device();
1661
1662 EXPECT_TRUE(ngpField.need_sync_to_host());
1663
1664 stkIntField.clear_sync_state();
1665 EXPECT_FALSE(ngpField.need_sync_to_host());
1666 }
1667
1668 TEST_F(NgpFieldFixture, ClearHostSyncState)
1669 {
1670 if(stk::parallel_machine_size(MPI_COMM_WORLD) != 1) { return; }
1671
1672 setup_empty_mesh(stk::mesh::BulkData::NO_AUTO_AURA);
1673 stk::mesh::Field<int>& stkIntField = create_field<int>(stk::topology::ELEM_RANK, "intField");
1674 stk::io::fill_mesh("generated:1x1x1", get_bulk());
1675
1676 stk::mesh::NgpField<int>& ngpField = stk::mesh::get_updated_ngp_field<int>(stkIntField);
1677
1678 ngpField.modify_on_host();
1679
1680 EXPECT_TRUE(ngpField.need_sync_to_device());
1681
1682 ngpField.clear_host_sync_state();
1683
1684 EXPECT_FALSE(ngpField.need_sync_to_device());
1685 }
1686
1687 TEST_F(NgpFieldFixture, ClearDeviceSyncState)
1688 {
1689 if(stk::parallel_machine_size(MPI_COMM_WORLD) != 1) { return; }
1690
1691 setup_empty_mesh(stk::mesh::BulkData::NO_AUTO_AURA);
1692 stk::mesh::Field<int>& stkIntField = create_field<int>(stk::topology::ELEM_RANK, "intField");
1693 stk::io::fill_mesh("generated:1x1x1", get_bulk());
1694
1695 stk::mesh::NgpField<int>& ngpField = stk::mesh::get_updated_ngp_field<int>(stkIntField);
1696
1697 ngpField.modify_on_device();
1698
1699 EXPECT_TRUE(ngpField.need_sync_to_host());
1700
1701 ngpField.clear_device_sync_state();
1702
1703 EXPECT_FALSE(ngpField.need_sync_to_host());
1704 }
1705
1706 TEST_F(NgpFieldFixture, ClearHostSyncState_doesntClearDeviceMod)
1707 {
1708 if(stk::parallel_machine_size(MPI_COMM_WORLD) != 1) { return; }
1709
1710 setup_empty_mesh(stk::mesh::BulkData::NO_AUTO_AURA);
1711 stk::mesh::Field<int>& stkIntField = create_field<int>(stk::topology::ELEM_RANK, "intField");
1712 stk::io::fill_mesh("generated:1x1x1", get_bulk());
1713
1714 stk::mesh::NgpField<int>& ngpField = stk::mesh::get_updated_ngp_field<int>(stkIntField);
1715
1716 ngpField.modify_on_device();
1717
1718 EXPECT_TRUE(ngpField.need_sync_to_host());
1719
1720 ngpField.clear_host_sync_state();
1721
1722 EXPECT_TRUE(ngpField.need_sync_to_host());
1723 }
1724
1725 TEST_F(NgpFieldFixture, ClearDeviceSyncState_doesntClearHostMod)
1726 {
1727 if(stk::parallel_machine_size(MPI_COMM_WORLD) != 1) { return; }
1728
1729 setup_empty_mesh(stk::mesh::BulkData::NO_AUTO_AURA);
1730 stk::mesh::Field<int>& stkIntField = create_field<int>(stk::topology::ELEM_RANK, "intField");
1731 stk::io::fill_mesh("generated:1x1x1", get_bulk());
1732
1733 stk::mesh::NgpField<int>& ngpField = stk::mesh::get_updated_ngp_field<int>(stkIntField);
1734
1735 ngpField.modify_on_host();
1736
1737 EXPECT_TRUE(ngpField.need_sync_to_device());
1738
1739 ngpField.clear_device_sync_state();
1740
1741 EXPECT_TRUE(ngpField.need_sync_to_device());
1742 }
1743
1744 TEST_F(NgpFieldFixture, updateBucketPtrView)
1745 {
1746 const unsigned bucketCapacity = 1;
1747 setup_empty_mesh(stk::mesh::BulkData::NO_AUTO_AURA, bucketCapacity, bucketCapacity);
1748 stk::mesh::Field<int>& field = create_field<int>(stk::topology::ELEM_RANK, "field");
1749 get_meta().declare_part_with_topology("block_3", stk::topology::HEX_8);
1750 ngp_unit_test_utils::setup_mesh_2hex_2block(get_bulk(), bucketCapacity);
1751 stk::mesh::NgpField<int>& ngpField = stk::mesh::get_updated_ngp_field<int>(field);
1752
1753 add_3rd_element_to_2hex_3block_mesh();
1754
1755 ngpField.update_bucket_pointer_view();
1756 }
1757
1758 TEST_F(NgpFieldFixture, LateFieldUsage)
1759 {
1760 if (stk::parallel_machine_size(MPI_COMM_WORLD) != 1) return;
1761
1762 setup_empty_mesh(stk::mesh::BulkData::NO_AUTO_AURA);
1763 stk::mesh::Field<int> & stkIntField = create_field<int>(stk::topology::ELEM_RANK, "intField");
1764 stk::io::fill_mesh("generated:1x1x1", get_bulk());
1765
1766 initialize_ngp_field(stkIntField);
1767
1768 get_meta().enable_late_fields();
1769 stk::mesh::Field<int> & stkLateIntField = create_field<int>(stk::topology::ELEM_RANK, "lateIntField");
1770
1771 initialize_ngp_field(stkIntField); // Must update early fields after adding late field
1772 initialize_ngp_field(stkLateIntField);
1773
1774 int multiplier = 2;
1775 modify_field_on_host(get_bulk(), stkIntField, multiplier);
1776 modify_field_on_host(get_bulk(), stkLateIntField, multiplier);
1777 check_field_on_host(get_bulk(), stkIntField, multiplier);
1778 check_field_on_host(get_bulk(), stkLateIntField, multiplier);
1779
1780 sync_field_to_device(stkIntField);
1781 sync_field_to_device(stkLateIntField);
1782 modify_field_on_device(get_bulk(), stkIntField, multiplier);
1783 modify_field_on_device(get_bulk(), stkLateIntField, multiplier);
1784
1785 sync_field_to_host(stkIntField);
1786 sync_field_to_host(stkLateIntField);
1787 check_field_on_host(get_bulk(), stkIntField, multiplier*multiplier);
1788 check_field_on_host(get_bulk(), stkLateIntField, multiplier*multiplier);
1789 }
1790
1791 // ------------------------- -------------------------
1792 // | | | | | | | |
1793 // | 1 | 2 | 3 | ===> | 1 | 2 | 3 |
1794 // |block_1|block_1|block_3| |block_1|block_3|block_3|
1795 // ------------------------- -------------------------
1796 //
1797 TEST_F(OptimizedNgpFieldFixture, ChangeBucketContentsUsingLayoutModificationWithSingleComponent)
1798 {
1799 if (get_parallel_size() != 1) return;
1800
1801 unsigned numComponents = 1;
1802
1803 run_change_bucket_content_by_mesh_modification(numComponents);
1804 }
1805
1806 TEST_F(OptimizedNgpFieldFixture, ChangeBucketContentsUsingLayoutModificationWithThreeComponents)
1807 {
1808 if (get_parallel_size() != 1) return;
1809
1810 unsigned numComponents = 3;
1811
1812 run_change_bucket_content_by_mesh_modification(numComponents);
1813 }
1814
1815 void construct_ngp_field(stk::mesh::Field<int>& stkIntField)
1816 {
1817 EXPECT_NO_THROW(stk::mesh::get_updated_ngp_field<int>(stkIntField));
1818 }
1819
1820 TEST_F(OptimizedNgpFieldFixture, CreateConsecutiveNgpFields)
1821 {
1822 if (get_parallel_size() != 1) return;
1823
1824 const unsigned bucketCapacity = 2;
1825 setup_empty_mesh(stk::mesh::BulkData::NO_AUTO_AURA, bucketCapacity, bucketCapacity);
1826
1827 unsigned numComponents = 1;
1828
1829 stk::mesh::Field<int>& stkIntField = create_field<int>(stk::topology::ELEM_RANK, "intField", numComponents);
1830
1831 setup_3hex_2block_mesh_with_field(bucketCapacity, stkIntField);
1832
1833 construct_ngp_field(stkIntField);
1834 construct_ngp_field(stkIntField);
1835 }
1836
1837 // ------------------------- -------------------------
1838 // | | | | | | | |
1839 // | 1 | 2 | 3 | ===> | 1 | 2 | 3 |
1840 // |block_1|block_2|block_3| |block_1|block_2|block_1|
1841 // ------------------------- -------------------------
1842 //
1843
1844 TEST_F(OptimizedNgpFieldFixture, AddBucketInMiddleWithSingleComponent_FieldReference)
1845 {
1846 if (get_parallel_size() != 1) return;
1847
1848 unsigned numComponents = 1;
1849
1850 run_add_bucket_in_middle_internal(numComponents);
1851 }
1852
1853 TEST_F(OptimizedNgpFieldFixture, AddBucketInMiddleWithSingleComponent_FieldCopy)
1854 {
1855 if (get_parallel_size() != 1) return;
1856
1857 unsigned numComponents = 1;
1858
1859 run_add_bucket_in_middle_copy(numComponents);
1860 }
1861
1862 // ------------------------- -------------------------
1863 // | | | | | | | |
1864 // | 1 | 2 | 3 | ===> | 1 | 2 | 3,4 |
1865 // |block_1|block_2|block_3| |block_1|block_1|block_3|
1866 // ------------------------- -------------------------
1867 //
1868
1869 TEST_F(OptimizedNgpFieldFixture, AddNewElementWithBucketCapacity2)
1870 {
1871 if (get_parallel_size() != 1) return;
1872
1873 unsigned numComponents = 1;
1874 unsigned bucketCapacity = 2;
1875
1876 run_add_new_element(numComponents, bucketCapacity);
1877 }
1878
1879 TEST_F(OptimizedNgpFieldFixture, AddNewElementAndModifyFromDevice)
1880 {
1881 if (get_parallel_size() != 1) return;
1882
1883 unsigned numComponents = 1;
1884
1885 run_add_new_element_and_modify_from_device(numComponents);
1886 }
1887
1888 TEST_F(OptimizedNgpFieldFixture, AddNewElementWithBucketCapacity1)
1889 {
1890 if (get_parallel_size() != 1) return;
1891
1892 unsigned numComponents = 1;
1893 unsigned bucketCapacity = 1;
1894
1895 run_add_new_element(numComponents, bucketCapacity);
1896 }
1897
1898 // ------------------------- -------------------------
1899 // | | | | | | | |
1900 // | 1 | 2 | 3 | ===> | 1 | 2 | 3 |
1901 // |block_1|block_2|block_3| |block_1|block_1|block_3|
1902 // ------------------------- -------------------------
1903 //
1904 TEST_F(OptimizedNgpFieldFixture, DeleteBucketInMiddleWithSingleComponent)
1905 {
1906 if (get_parallel_size() != 1) return;
1907
1908 unsigned numComponents = 1;
1909
1910 run_delete_bucket_in_middle(numComponents);
1911 }
1912
1913 // ----------------- -----------------
1914 // | | | | | |
1915 // | 1 | 2 | ===> | 1 | 2 |
1916 // |block_1|block_2| |block_3|block_1|
1917 // ----------------- -----------------
1918 //
1919
1920 TEST_F(OptimizedNgpFieldFixture, AddAndDeleteBucketWithSingleComponent)
1921 {
1922 if (get_parallel_size() != 1) return;
1923
1924 unsigned numComponents = 1;
1925
1926 run_add_and_delete_bucket(numComponents);
1927 }
1928
1929 // ----------------- -----------------
1930 // | | | | | |
1931 // | 1 | 2 | ===> | 1 | 2 |
1932 // |block_1|block_2| |block_2|block_3|
1933 // ----------------- -----------------
1934 //
1935
1936 TEST_F(OptimizedNgpFieldFixture, AddAndDeleteBucketWithSingleComponent2)
1937 {
1938 if (get_parallel_size() != 1) return;
1939
1940 unsigned numComponents = 1;
1941
1942 run_add_and_delete_bucket2(numComponents);
1943 }
1944
1945 // ----------------- -----------------
1946 // | | | | | |
1947 // | 1 | 2 | ===> | 2 | 3 |
1948 // |block_1|block_2| |block_2|block_3|
1949 // ----------------- -----------------
1950 //
1951
1952 TEST_F(OptimizedNgpFieldFixture, AddAndDeleteBucketWithSingleComponent3)
1953 {
1954 if (get_parallel_size() != 1) return;
1955
1956 unsigned numComponents = 1;
1957
1958 run_add_and_delete_bucket3(numComponents);
1959 }
1960
1961 class ModifyBySelectorFixture : public OptimizedNgpFieldFixture
1962 {
1963 public:
1964 ModifyBySelectorFixture()
1965 : entityIdMultiplier(10),
1966 m_numComponents(2),
1967 m_bucketCapacity(1)
1968 { }
1969
1970 stk::mesh::Field<int>& setup_field_on_mesh_two_buckets_two_components()
1971 {
1972 setup_empty_mesh(stk::mesh::BulkData::NO_AUTO_AURA, m_bucketCapacity, m_bucketCapacity);
1973 stk::mesh::Field<int>& stkField = create_field<int>(stk::topology::ELEM_RANK, "intField", m_numComponents);
1974 ngp_unit_test_utils::setup_mesh_2hex_2block(get_bulk(), m_bucketCapacity);
1975 return stkField;
1976 }
1977
1978 stk::mesh::Field<int>& setup_partial_field_on_mesh_three_buckets_two_components(const std::vector<std::string>& partNames)
1979 {
1980 setup_empty_mesh(stk::mesh::BulkData::NO_AUTO_AURA, m_bucketCapacity, m_bucketCapacity);
1981 unsigned numStates = 1;
1982 const std::vector<int> init(m_numComponents, 1);
1983 stk::mesh::Field<int>& stkField = get_meta().declare_field<int>(stk::topology::ELEM_RANK, "intField", numStates);
1984
1985 for (const std::string& name : partNames) {
1986 stk::mesh::Part& part = get_meta().declare_part_with_topology(name, stk::topology::HEX_8);
1987 stk::mesh::put_field_on_mesh(stkField, part, m_numComponents, init.data());
1988 }
1989
1990 ngp_unit_test_utils::setup_mesh_3hex_3block(get_bulk(), m_bucketCapacity);
1991 return stkField;
1992 }
1993
1994 void fill_field_on_device(stk::mesh::Field<int>& stkField)
1995 {
1996 stk::mesh::NgpMesh& ngpMesh = stk::mesh::get_updated_ngp_mesh(get_bulk());
1997 set_element_field_data_on_device(ngpMesh, stkField, get_meta().universal_part(), entityIdMultiplier);
1998 }
1999
2000 protected:
2001 unsigned entityIdMultiplier;
2002
2003 private:
2004 unsigned m_numComponents;
2005 unsigned m_bucketCapacity;
2006 };
2007
2008 TEST_F(ModifyBySelectorFixture, hostToDevice_dontSpecifySelector_byReference)
2009 {
2010 if (get_parallel_size() != 1) GTEST_SKIP();
2011
2012 stk::mesh::Field<int>& stkField = setup_field_on_mesh_two_buckets_two_components();
2013 set_element_field_data(stkField, get_meta().universal_part(), entityIdMultiplier);
2014
2015 stk::mesh::NgpField<int>& ngpFieldByRef = stk::mesh::get_updated_ngp_field<int>(stkField);
2016 ngpFieldByRef.modify_on_host();
2017 ngpFieldByRef.sync_to_device();
2018
2019 check_field_data_on_device<int>(ngpFieldByRef, stkField);
2020 }
2021
2022 TEST_F(ModifyBySelectorFixture, hostToDevice_partialField_byReference)
2023 {
2024 if (get_parallel_size() != 1) GTEST_SKIP();
2025
2026 stk::mesh::Field<int>& stkField = setup_partial_field_on_mesh_three_buckets_two_components({"block_2", "block_3"});
2027 set_element_field_data(stkField, get_meta().universal_part(), entityIdMultiplier);
2028
2029 stk::mesh::NgpField<int>& ngpFieldByRef = stk::mesh::get_updated_ngp_field<int>(stkField);
2030 ngpFieldByRef.modify_on_host(get_meta().universal_part());
2031 ngpFieldByRef.sync_to_device();
2032
2033 check_field_data_on_device<int>(ngpFieldByRef, stkField);
2034 }
2035
2036 TEST(DeviceField, checkSizeof)
2037 {
2038 size_t expectedNumBytes = 384;
2039 std::cout << "sizeof(stk::mesh::DeviceField<double>): " << sizeof(stk::mesh::DeviceField<double>) << std::endl;
2040 EXPECT_TRUE(sizeof(stk::mesh::DeviceField<double>) <= expectedNumBytes);
2041 }
2042
2043
2044 enum PartIds : int {
2045 part_1 = 1,
2046 part_2 = 2,
2047 part_3 = 3,
2048 part_4 = 4,
2049 part_5 = 5,
2050 part_6 = 6,
2051 part_7 = 7,
2052 part_8 = 8,
2053 part_9 = 9,
2054 part_10 = 10,
2055 part_11 = 11,
2056 part_12 = 12,
2057 part_13 = 13,
2058 part_14 = 14,
2059 part_15 = 15
2060 };
2061
2062 struct NodeIdPartId {
2063 int nodeId;
2064 PartIds partId;
2065 };
2066
2067 class NgpFieldUpdate : public ::ngp_testing::Test
2068 {
2069 public:
2070 NgpFieldUpdate()
2071 : m_field(nullptr)
2072 {
2073 stk::mesh::MeshBuilder builder(MPI_COMM_WORLD);
2074 builder.set_spatial_dimension(3);
2075 m_meta = builder.create_meta_data();
2076 }
2077
2078 void create_mesh_with_parts_and_nodes(std::vector<PartIds> partIds, std::vector<NodeIdPartId> nodes)
2079 {
2080 for (PartIds partId : partIds) {
2081 stk::mesh::Part & part = m_meta->declare_part_with_topology("part_" + std::to_string(partId),
2082 stk::topology::NODE);
2083 m_parts[partId] = &part;
2084 }
2085
2086 m_field = &m_meta->declare_field<int>(stk::topology::NODE_RANK, "nodeField");
2087 stk::mesh::put_field_on_mesh(*m_field, m_meta->universal_part(), nullptr);
2088
2089 stk::mesh::MeshBuilder builder(MPI_COMM_WORLD);
2090 builder.set_aura_option(stk::mesh::BulkData::NO_AUTO_AURA);
2091 m_bulk = builder.create(m_meta);
2092
2093 m_bulk->modification_begin();
2094 for (NodeIdPartId node : nodes) {
2095 const stk::mesh::Entity newNode = m_bulk->declare_node(node.nodeId,
2096 stk::mesh::PartVector{m_parts.at(node.partId)});
2097 *stk::mesh::field_data(*m_field, newNode) = node.nodeId;
2098 }
2099 m_bulk->modification_end();
2100
2101 stk::mesh::get_updated_ngp_mesh(*m_bulk);
2102 stk::mesh::get_updated_ngp_field<int>(*m_field);
2103 }
2104
2105 void add_node(NodeIdPartId newNodeInfo)
2106 {
2107 m_bulk->modification_begin();
2108 const stk::mesh::Entity node = m_bulk->declare_node(newNodeInfo.nodeId,
2109 stk::mesh::PartVector{m_parts.at(newNodeInfo.partId)});
2110 *stk::mesh::field_data(*m_field, node) = newNodeInfo.nodeId;
2111 m_bulk->modification_end();
2112 }
2113
2114 void remove_node(int removedNodeId)
2115 {
2116 m_bulk->modification_begin();
2117 const stk::mesh::Entity node = m_bulk->get_entity(stk::topology::NODE_RANK, removedNodeId);
2118 m_bulk->destroy_entity(node);
2119 m_bulk->modification_end();
2120 }
2121
2122 void check_field_values()
2123 {
2124 const stk::mesh::BucketVector& buckets = m_bulk->buckets(stk::topology::NODE_RANK);
2125 unsigned nodeIdx = 0;
2126 for (const stk::mesh::Bucket* bucket : buckets) {
2127 for (stk::mesh::Entity node : *bucket) {
2128 const int* fieldValue = stk::mesh::field_data(*m_field, node);
2129 const int expectedValue = m_bulk->identifier(node);
2130 EXPECT_EQ(*fieldValue, expectedValue);
2131 }
2132 }
2133
2134 const unsigned numNodes = stk::mesh::count_entities(*m_bulk, stk::topology::NODE_RANK, m_meta->universal_part());
2135 Kokkos::View<int*> deviceValues("deviceValues", numNodes);
2136 Kokkos::View<int*>::HostMirror hostValuesFromDevice = Kokkos::create_mirror_view(deviceValues);
2137
2138 stk::mesh::NgpMesh& ngpMesh = stk::mesh::get_updated_ngp_mesh(*m_bulk);
2139 stk::mesh::NgpField<int>& ngpField = stk::mesh::get_updated_ngp_field<int>(*m_field);
2140
2141 Kokkos::parallel_for(stk::ngp::DeviceRangePolicy(0, 1),
2142 KOKKOS_LAMBDA(size_t /*index*/) {
2143 unsigned nodeGlobalIndex = 0;
2144 const unsigned numBuckets = ngpMesh.num_buckets(stk::topology::NODE_RANK);
2145 for (unsigned bucketId = 0; bucketId < numBuckets; ++bucketId) {
2146 const stk::mesh::NgpMesh::BucketType & deviceBucket = ngpMesh.get_bucket(stk::topology::NODE_RANK, bucketId);
2147 const unsigned numNodesInBucket = deviceBucket.size();
2148 for (unsigned nodeOrdinal = 0; nodeOrdinal < numNodesInBucket; ++nodeOrdinal) {
2149 const stk::mesh::FastMeshIndex nodeIndex = ngpMesh.fast_mesh_index(deviceBucket[nodeOrdinal]);
2150 deviceValues[nodeGlobalIndex++] = ngpField.get(nodeIndex, 0);
2151 }
2152 }
2153 });
2154
2155 Kokkos::deep_copy(hostValuesFromDevice, deviceValues);
2156
2157 nodeIdx = 0;
2158 for (const stk::mesh::Bucket * bucket : buckets) {
2159 for (stk::mesh::Entity node : *bucket) {
2160 const int expectedValue = m_bulk->identifier(node);
2161 EXPECT_EQ(hostValuesFromDevice[nodeIdx++], expectedValue);
2162 }
2163 }
2164 }
2165
2166 protected:
2167 std::shared_ptr<stk::mesh::BulkData> m_bulk;
2168 std::shared_ptr<stk::mesh::MetaData> m_meta;
2169 std::map<PartIds, stk::mesh::Part*> m_parts;
2170 stk::mesh::Field<int> * m_field;
2171 };
2172
2173 // o = Node that persists through modification
2174 // * = New node
2175 // x = Node that will be deleted
2176
2177 // | 2 | 3 | | 1 | 2 | 3 |
2178 // | o | o | ==> | * | o | o |
2179 // |part_2 |part_3 | |part_1 |part_2 |part_3 |
2180 //
2181 TEST_F(NgpFieldUpdate, AddBucketAtBeginning_WhileReallocating)
2182 {
2183 if (stk::parallel_machine_size(MPI_COMM_WORLD) != 1) GTEST_SKIP();
2184
2185 create_mesh_with_parts_and_nodes({part_1, part_2, part_3},
2186 {{2, part_2}, {3, part_3}});
2187 add_node({1, part_1});
2188
2189 check_field_values();
2190 }
2191
2192 // | 1 | 3 | | 1 | 2 | 3 |
2193 // | o | o | ==> | o | * | o |
2194 // |part_1 |part_3 | |part_1 |part_2 |part_3 |
2195 //
2196 TEST_F(NgpFieldUpdate, AddBucketInMiddle_WhileReallocating)
2197 {
2198 if (stk::parallel_machine_size(MPI_COMM_WORLD) != 1) GTEST_SKIP();
2199
2200 create_mesh_with_parts_and_nodes({part_1, part_2, part_3},
2201 {{1, part_1}, {3, part_3}});
2202 add_node({2, part_2});
2203
2204 check_field_values();
2205 }
2206
2207 // | 1 | 2 | | 1 | 2 | 3 |
2208 // | o | o | ==> | o | o | * |
2209 // |part_1 |part_2 | |part_1 |part_2 |part_3 |
2210 //
2211 TEST_F(NgpFieldUpdate, AddBucketAtEnd_WhileReallocating)
2212 {
2213 if (stk::parallel_machine_size(MPI_COMM_WORLD) != 1) GTEST_SKIP();
2214
2215 create_mesh_with_parts_and_nodes({part_1, part_2, part_3},
2216 {{1, part_1}, {2, part_2}});
2217 add_node({3, part_3});
2218
2219 check_field_values();
2220 }
2221
2222 // | 1 | 2 | 3 | | 2 | 3 |
2223 // | x | o | o | ==> | o | o |
2224 // |part_1 |part_2 |part_3 | |part_2 |part_3 |
2225 //
2226 TEST_F(NgpFieldUpdate, RemoveBucketAtFront_WhileReallocating)
2227 {
2228 if (stk::parallel_machine_size(MPI_COMM_WORLD) != 1) GTEST_SKIP();
2229
2230 create_mesh_with_parts_and_nodes({part_1, part_2, part_3},
2231 {{1, part_1}, {2, part_2}, {3, part_3}});
2232 remove_node(1);
2233
2234 check_field_values();
2235 }
2236
2237 // | 1 | 2 | 3 | | 1 | 3 |
2238 // | o | x | o | ==> | o | o |
2239 // |part_1 |part_2 |part_3 | |part_1 |part_3 |
2240 //
2241 TEST_F(NgpFieldUpdate, RemoveBucketInMiddle_WhileReallocating)
2242 {
2243 if (stk::parallel_machine_size(MPI_COMM_WORLD) != 1) GTEST_SKIP();
2244
2245 create_mesh_with_parts_and_nodes({part_1, part_2, part_3},
2246 {{1, part_1}, {2, part_2}, {3, part_3}});
2247 remove_node(2);
2248
2249 check_field_values();
2250 }
2251
2252 // | 1 | 2 | 3 | | 1 | 2 |
2253 // | o | o | x | ==> | o | o |
2254 // |part_1 |part_2 |part_3 | |part_1 |part_2 |
2255 //
2256 TEST_F(NgpFieldUpdate, RemoveBucketAtEnd_WhileReallocating)
2257 {
2258 if (stk::parallel_machine_size(MPI_COMM_WORLD) != 1) GTEST_SKIP();
2259
2260 create_mesh_with_parts_and_nodes({part_1, part_2, part_3},
2261 {{1, part_1}, {2, part_2}, {3, part_3}});
2262 remove_node(3);
2263
2264 check_field_values();
2265 }
2266
2267 // | 1 | 3 | 4 | | 1 2 | 3 | 4 | 5 |
2268 // | o | o | o | ==> | o * | o | o | * |
2269 // |part_1 |part_3 |part_4 | |part_1 |part_3 |part_4 |part_5 |
2270 //
2271 TEST_F(NgpFieldUpdate, ModifyBucketAtBeginning_WhileReallocating)
2272 {
2273 if (stk::parallel_machine_size(MPI_COMM_WORLD) != 1) GTEST_SKIP();
2274
2275 create_mesh_with_parts_and_nodes({part_1, part_3, part_4, part_5},
2276 {{1, part_1}, {3, part_3}, {4, part_4}});
2277 add_node({2, part_1});
2278 add_node({5, part_5});
2279
2280 check_field_values();
2281 }
2282
2283 // | 1 | 2 | 4 | | 1 | 2 3 | 4 | 5 |
2284 // | o | o | o | ==> | o | o * | o | * |
2285 // |part_1 |part_2 |part_4 | |part_1 |part_2 |part_4 |part_5 |
2286 //
2287 TEST_F(NgpFieldUpdate, ModifyBucketInMiddle_WhileReallocating)
2288 {
2289 if (stk::parallel_machine_size(MPI_COMM_WORLD) != 1) GTEST_SKIP();
2290
2291 create_mesh_with_parts_and_nodes({part_1, part_2, part_4, part_5},
2292 {{1, part_1}, {2, part_2}, {4, part_4}});
2293 add_node({3, part_2});
2294 add_node({5, part_5});
2295
2296 check_field_values();
2297 }
2298
2299 // | 1 | 3 | 4 | | 1 | 2 | 3 | 4 5 |
2300 // | o | o | o | ==> | o | * | o | o * |
2301 // |part_1 |part_3 |part_4 | |part_1 |part_2 |part_3 |part_4 |
2302 //
2303 TEST_F(NgpFieldUpdate, ModifyBucketAtEnd_WhileReallocating)
2304 {
2305 if (stk::parallel_machine_size(MPI_COMM_WORLD) != 1) GTEST_SKIP();
2306
2307 create_mesh_with_parts_and_nodes({part_1, part_2, part_3, part_4},
2308 {{1, part_1}, {3, part_3}, {4, part_4}});
2309 add_node({2, part_2});
2310 add_node({5, part_4});
2311
2312 check_field_values();
2313 }
2314
2315 // | 1 | 2 | 4 | | 1 | 3 | 4 |
2316 // | o | x | o | ==> | o | * | o |
2317 // |part_1 |part_2 |part_4 | |part_1 |part_3 |part_4 |
2318 //
2319 TEST_F(NgpFieldUpdate, AddAndRemoveBucketSameLocation)
2320 {
2321 if (stk::parallel_machine_size(MPI_COMM_WORLD) != 1) GTEST_SKIP();
2322
2323 create_mesh_with_parts_and_nodes({part_1, part_2, part_3, part_4},
2324 {{1, part_1}, {2, part_2}, {4, part_4}});
2325 remove_node(2);
2326 add_node({3, part_3});
2327
2328 check_field_values();
2329 }
2330
2331 // | 1 | 2 | 3 | 4 | 6 |
2332 // | x | x | o | o | o | ==>
2333 // |part_1 |part_2 |part_3 |part_4 |part_6 |
2334 // ____/ ____/
2335 // ____/ ____/
2336 // ____/ ____/
2337 // / /
2338 // V V
2339 // | 3 | 4 5 | 6 | 7 | 8 |
2340 // | o | o * | o | * | * |
2341 // |part_3 |part_4 |part_6 |part_7 |part_8 |
2342 //
2343 TEST_F(NgpFieldUpdate, OverlapBackwardMovedRangesDueToModifiedBucket)
2344 {
2345 if (stk::parallel_machine_size(MPI_COMM_WORLD) != 1) GTEST_SKIP();
2346
2347 create_mesh_with_parts_and_nodes({part_1, part_2, part_3, part_4, part_6, part_7, part_8},
2348 {{1, part_1}, {2, part_2}, {3, part_3}, {4, part_4}, {6, part_6}});
2349 remove_node(1);
2350 remove_node(2);
2351 add_node({5, part_4});
2352 add_node({7, part_7});
2353 add_node({8, part_8});
2354
2355 check_field_values();
2356 }
2357
2358 // | 1 | 2 | 3 | 4 | 5 |
2359 // | o | x | o | x | o | ==>
2360 // |part_1 |part_2 |part_3 |part_4 |part_5 |
2361 // | __/ ____/
2362 // | _/ ____/
2363 // | _/ ____/
2364 // | / /
2365 // V V V
2366 // | 1 | 3 | 5 | 6 | 7 |
2367 // | o | o | o | * | * |
2368 // |part_1 |part_3 |part_5 |part_6 |part_7 |
2369 //
2370 TEST_F(NgpFieldUpdate, OverlapBackwardMovedRangesDueToDeletedBucket)
2371 {
2372 if (stk::parallel_machine_size(MPI_COMM_WORLD) != 1) GTEST_SKIP();
2373
2374 create_mesh_with_parts_and_nodes({part_1, part_2, part_3, part_4, part_5, part_6, part_7},
2375 {{1, part_1}, {2, part_2}, {3, part_3}, {4, part_4}, {5, part_5}});
2376 remove_node(2);
2377 remove_node(4);
2378 add_node({6, part_6});
2379 add_node({7, part_7});
2380
2381 check_field_values();
2382 }
2383
2384 // | 1 | 2 | 3 | 4 | 5 | 6 | 8 | 9 | 11 |
2385 // | x | x | x | x | o | o | o | o | o | ==>
2386 // |part_1 |part_2 |part_3 |part_4 |part_5 |part_6 |part_8 |part_9 |part_11|
2387 // __________/ __________/ _________/
2388 // _________/ _________/ _________/
2389 // _________/ _________/ _________/
2390 // / / /
2391 // V V V
2392 // | 5 | 6 7 | 8 | 9 10 | 11 | 12 | 13 | 14 | 15 |
2393 // | o | o * | o | o * | o | * | * | * | * |
2394 // |part_5 |part_6 |part_8 |part_9 |part_11|part_12|part_13|part_14|part_15|
2395 //
2396 TEST_F(NgpFieldUpdate, DoubleOverlapBackwardMovedRangesDueToModifiedBucket)
2397 {
2398 if (stk::parallel_machine_size(MPI_COMM_WORLD) != 1) GTEST_SKIP();
2399
2400 create_mesh_with_parts_and_nodes({part_1, part_2, part_3, part_4, part_5, part_6, part_8, part_9, part_11,
2401 part_12, part_13, part_14, part_15},
2402 {{1, part_1}, {2, part_2}, {3, part_3}, {4, part_4}, {5, part_5}, {6, part_6},
2403 {8, part_8}, {9, part_9}, {11, part_11}});
2404 remove_node(1);
2405 remove_node(2);
2406 remove_node(3);
2407 remove_node(4);
2408 add_node({7, part_6});
2409 add_node({10, part_9});
2410 add_node({12, part_12});
2411 add_node({13, part_13});
2412 add_node({14, part_14});
2413 add_node({15, part_15});
2414
2415 check_field_values();
2416 }
2417
2418 // | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 |
2419 // | x | x | o | o | x | o | x | o | ==>
2420 // |part_1 |part_2 |part_3 |part_4 |part_5 |part_6 |part_7 |part_8 |
2421 // ____/ ____/ _______/ __________/
2422 // ____/ ____/ _______/ _________/
2423 // ____/ ____/ ______/ _________/
2424 // / / / /
2425 // V V V V
2426 // | 3 | 4 | 6 | 8 | 9 | 10 | 11 | 12 |
2427 // | o | o | o | o | * | * | * | * |
2428 // |part_3 |part_4 |part_6 |part_8 |part_9 |part_10|part_11|part_12|
2429 //
2430 TEST_F(NgpFieldUpdate, DoubleOverlapBackwardMovedRangesDueToDeletedBucket)
2431 {
2432 if (stk::parallel_machine_size(MPI_COMM_WORLD) != 1) GTEST_SKIP();
2433
2434 create_mesh_with_parts_and_nodes({part_1, part_2, part_3, part_4, part_5, part_6, part_7, part_8, part_9,
2435 part_10, part_11, part_12},
2436 {{1, part_1}, {2, part_2}, {3, part_3}, {4, part_4}, {5, part_5}, {6, part_6},
2437 {7, part_7}, {8, part_8}});
2438 remove_node(1);
2439 remove_node(2);
2440 remove_node(5);
2441 remove_node(7);
2442 add_node({9, part_9});
2443 add_node({10, part_10});
2444 add_node({11, part_11});
2445 add_node({12, part_12});
2446
2447 check_field_values();
2448 }
2449
2450 // | 3 | 4 | 6 | 7 | 8 |
2451 // | o | o | o | x | x | ==>
2452 // |part_3 |part_4 |part_6 |part_7 |part_8 |
2453 // \____ \____
2454 // \____ \____
2455 // \____ \____
2456 // \ \ .
2457 // V V
2458 // | 1 | 2 | 3 | 4 5 | 6 |
2459 // | * | * | o | o * | o |
2460 // |part_1 |part_2 |part_3 |part_4 |part_6 |
2461 //
2462 TEST_F(NgpFieldUpdate, OverlapForwardMovedRangesDueToModifiedBucket)
2463 {
2464 if (stk::parallel_machine_size(MPI_COMM_WORLD) != 1) GTEST_SKIP();
2465
2466 create_mesh_with_parts_and_nodes({part_1, part_2, part_3, part_4, part_6, part_7, part_8},
2467 {{3, part_3}, {4, part_4}, {6, part_6}, {7, part_7}, {8, part_8}});
2468 remove_node(7);
2469 remove_node(8);
2470 add_node({1, part_1});
2471 add_node({2, part_2});
2472 add_node({5, part_4});
2473
2474 check_field_values();
2475 }
2476
2477 // | 3 | 4 | 5 | 6 | 7 |
2478 // | o | x | o | x | o | ==>
2479 // |part_3 |part_4 |part_5 |part_6 |part_7 |
2480 // \____ \__ |
2481 // \____ \_ |
2482 // \____ \_ |
2483 // \ \ |
2484 // V V V
2485 // | 1 | 2 | 3 | 5 | 7 |
2486 // | * | * | o | o | o |
2487 // |part_1 |part_2 |part_3 |part_5 |part_7 |
2488 //
2489 TEST_F(NgpFieldUpdate, OverlapForwardMovedRangesDueToDeletedBucket)
2490 {
2491 if (stk::parallel_machine_size(MPI_COMM_WORLD) != 1) GTEST_SKIP();
2492
2493 create_mesh_with_parts_and_nodes({part_1, part_2, part_3, part_4, part_5, part_6, part_7},
2494 {{3, part_3}, {4, part_4}, {5, part_5}, {6, part_6}, {7, part_7}});
2495 remove_node(4);
2496 remove_node(6);
2497 add_node({1, part_1});
2498 add_node({2, part_2});
2499
2500 check_field_values();
2501 }
2502
2503 // | 5 | 6 | 8 | 9 | 11 | 12 | 13 | 14 | 15 |
2504 // | o | o | o | o | o | x | x | x | x | ==>
2505 // |part_5 |part_6 |part_8 |part_9 |part_11|part_12|part_13|part_14|part_15|
2506 // \__________ \__________ \__________
2507 // \_________ \_________ \_________
2508 // \_________ \_________ \_________
2509 // \ \ \ .
2510 // V V V
2511 // | 1 | 2 | 3 | 4 | 5 | 6 7 | 8 | 9 10 | 11 |
2512 // | * | * | * | * | o | o * | o | o * | o |
2513 // |part_1 |part_2 |part_3 |part_4 |part_5 |part_6 |part_8 |part_9 |part_11|
2514 //
2515 TEST_F(NgpFieldUpdate, DoubleOverlapForwardMovedRangesDueToModifiedBucket)
2516 {
2517 if (stk::parallel_machine_size(MPI_COMM_WORLD) != 1) GTEST_SKIP();
2518
2519 create_mesh_with_parts_and_nodes({part_1, part_2, part_3, part_4, part_5, part_6, part_8, part_9, part_11,
2520 part_12, part_13, part_14, part_15},
2521 {{5, part_5}, {6, part_6}, {8, part_8}, {9, part_9}, {11, part_11}, {12, part_12},
2522 {13, part_13}, {14, part_14}, {15, part_15}});
2523 remove_node(12);
2524 remove_node(13);
2525 remove_node(14);
2526 remove_node(15);
2527 add_node({1, part_1});
2528 add_node({2, part_2});
2529 add_node({3, part_3});
2530 add_node({4, part_4});
2531 add_node({7, part_6});
2532 add_node({10, part_9});
2533
2534 check_field_values();
2535 }
2536
2537 // | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 |
2538 // | o | x | o | x | o | o | x | x | ==>
2539 // |part_5 |part_6 |part_7 |part_8 |part_9 |part_10|part_11|part_12|
2540 // \__________ \_______ \____ \____
2541 // \_________ \_______ \____ \____
2542 // \_________ \______ \____ \____
2543 // \ \ \ \ .
2544 // V V V V
2545 // | 1 | 2 | 3 | 4 | 5 | 7 | 9 | 10 |
2546 // | * | * | * | * | o | o | o | o |
2547 // |part_1 |part_2 |part_3 |part_4 |part_5 |part_7 |part_9 |part_10|
2548 //
2549 TEST_F(NgpFieldUpdate, DoubleOverlapForwardMovedRangesDueToDeletedBucket)
2550 {
2551 if (stk::parallel_machine_size(MPI_COMM_WORLD) != 1) GTEST_SKIP();
2552
2553 create_mesh_with_parts_and_nodes({part_1, part_2, part_3, part_4, part_5, part_6, part_7, part_8, part_9,
2554 part_10, part_11, part_12},
2555 {{5, part_5}, {6, part_6}, {7, part_7}, {8, part_8}, {9, part_9}, {10, part_10},
2556 {11, part_11}, {12, part_12}});
2557 remove_node(6);
2558 remove_node(8);
2559 remove_node(11);
2560 remove_node(12);
2561 add_node({1, part_1});
2562 add_node({2, part_2});
2563 add_node({3, part_3});
2564 add_node({4, part_4});
2565
2566 check_field_values();
2567 }
2568
2569 // | 2 | 3 | 4 | 5 | 8 | 9 |
2570 // | o | x | x | o | o | x | ==>
2571 // |part_2 |part_3 |part_4 |part_5 |part_8 |part_9 |
2572 // \__ __/ \__
2573 // \_ _/ \_
2574 // \_ _/ \_
2575 // \ / \ .
2576 // V V V
2577 // | 1 | 2 | 5 | 6 | 7 | 8 |
2578 // | * | o | o | * | * | o |
2579 // |part_1 |part_2 |part_5 |part_6 |part_7 |part_8 |
2580 //
2581 TEST_F(NgpFieldUpdate, MoveForwardBackwardForward)
2582 {
2583 if (stk::parallel_machine_size(MPI_COMM_WORLD) != 1) GTEST_SKIP();
2584
2585 create_mesh_with_parts_and_nodes({part_1, part_2, part_3, part_4, part_5, part_6, part_7, part_8, part_9},
2586 {{2, part_2}, {3, part_3}, {4, part_4}, {5, part_5}, {8, part_8}, {9, part_9}});
2587 remove_node(3);
2588 remove_node(4);
2589 remove_node(9);
2590 add_node({1, part_1});
2591 add_node({6, part_6});
2592 add_node({7, part_7});
2593
2594 check_field_values();
2595 }
2596
2597 // | 1 | 2 | 5 | 6 | 7 | 8 |
2598 // | x | o | o | x | x | o | ==>
2599 // |part_1 |part_2 |part_5 |part_6 |part_7 |part_8 |
2600 // __/ \__ __/
2601 // _/ \_ _/
2602 // _/ \_ _/
2603 // / \ /
2604 // V V V
2605 // | 2 | 3 | 4 | 5 | 8 | 9 |
2606 // | o | * | * | o | o | * |
2607 // |part_2 |part_3 |part_4 |part_5 |part_8 |part_9 |
2608 //
2609 TEST_F(NgpFieldUpdate, MoveBackwardForwardBackward)
2610 {
2611 if (stk::parallel_machine_size(MPI_COMM_WORLD) != 1) GTEST_SKIP();
2612
2613 create_mesh_with_parts_and_nodes({part_1, part_2, part_3, part_4, part_5, part_6, part_7, part_8, part_9},
2614 {{1, part_1}, {2, part_2}, {5, part_5}, {6, part_6}, {7, part_7}, {8, part_8}});
2615 remove_node(1);
2616 remove_node(6);
2617 remove_node(7);
2618 add_node({3, part_3});
2619 add_node({4, part_4});
2620 add_node({9, part_9});
2621
2622 check_field_values();
2623 }
2624
2625 class NgpFieldExecSpaceTestFixture : public stk::unit_test_util::MeshFixture
2626 {
2627 public:
2628 void setup_empty_mesh_and_field()
2629 {
2630 setup_empty_mesh(stk::mesh::BulkData::NO_AUTO_AURA);
2631
2632 const std::vector<int> init(1, 1);
2633 stk::mesh::Field<int>& field = get_meta().declare_field<int>(stk::topology::ELEM_RANK, "", 1);
2634 stk::mesh::put_field_on_mesh(field, get_meta().universal_part(), 1, init.data());
2635 }
2636
2637 auto get_default_field()
2638 {
2639 return get_meta().get_field(stk::topology::ELEM_RANK, "");
2640 }
2641 };
2642
2643 TEST_F(NgpFieldExecSpaceTestFixture, CheckValidMemSpace)
2644 {
2645 if (get_parallel_size() != 1) GTEST_SKIP();
2646 setup_empty_mesh_and_field();
2647 auto field = get_default_field();
2648
2649 EXPECT_NO_THROW((stk::mesh::get_updated_ngp_field<int>(*field)));
2650
2651 EXPECT_NO_THROW((stk::mesh::get_updated_ngp_field<int, stk::mesh::NgpMeshDefaultMemSpace>(*field)));
2652
2653 #ifdef STK_ENABLE_GPU
2654 EXPECT_ANY_THROW((stk::mesh::get_updated_ngp_field<int, stk::ngp::HostPinnedSpace>(*field)));
2655 #else
2656 EXPECT_NO_THROW((stk::mesh::get_updated_ngp_field<int, stk::ngp::HostPinnedSpace>(*field)));
2657 #endif
2658 }
2659
2660 TEST_F(NgpFieldExecSpaceTestFixture, CheckSameMemSpace)
2661 {
2662 if (get_parallel_size() != 1) GTEST_SKIP();
2663 setup_empty_mesh_and_field();
2664 auto field = get_default_field();
2665
2666 auto& ngpField1 = stk::mesh::get_updated_ngp_field<int>(*field);
2667 auto& ngpField2 = stk::mesh::get_updated_ngp_field<int, stk::mesh::NgpMeshDefaultMemSpace>(*field);
2668
2669 EXPECT_TRUE((std::is_same_v<std::remove_reference_t<decltype(ngpField1)>::MemSpace, stk::mesh::NgpMeshDefaultMemSpace>));
2670 EXPECT_TRUE((std::is_same_v<std::remove_reference_t<decltype(ngpField1)>::MemSpace, std::remove_reference_t<decltype(ngpField2)>::MemSpace>));
2671 }
2672
2673 TEST_F(NgpFieldExecSpaceTestFixture, UseNonDefaultMemSpace)
2674 {
2675 if (get_parallel_size() != 1) GTEST_SKIP();
2676 setup_empty_mesh_and_field();
2677 auto field = get_default_field();
2678
2679 EXPECT_NO_THROW((stk::mesh::get_updated_ngp_field<int, stk::ngp::HostPinnedSpace>(*field)));
2680
2681 #ifdef STK_ENABLE_GPU
2682 EXPECT_ANY_THROW(
2683 #else
2684 EXPECT_NO_THROW(
2685 #endif
2686 (stk::mesh::get_updated_ngp_field<int>(*field)));
2687
2688 #ifdef STK_ENABLE_GPU
2689 EXPECT_ANY_THROW(
2690 #else
2691 EXPECT_NO_THROW(
2692 #endif
2693 (stk::mesh::get_updated_ngp_field<int, stk::mesh::NgpMeshDefaultMemSpace>(*field)));
2694 }
2695
2696 } // namespace ngp_field_test