Debian Codegraph @

packages/stk/stk_unit_tests/stk_mesh/ngp/ngpMultiStateFieldTests.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 <stk_util/stk_config.h>
36 #include <stk_ngp_test/ngp_test.hpp>
37 #include <stk_mesh/base/Ngp.hpp>
38 #include <stk_unit_test_utils/stk_mesh_fixtures/TestHexFixture.hpp>
39 #include <stk_mesh/base/Types.hpp>
40 #include <stk_mesh/base/MetaData.hpp>
41 #include <stk_mesh/base/BulkData.hpp>
42 #include <stk_mesh/base/Field.hpp>
43 #include <stk_mesh/base/NgpFieldBLAS.hpp>
44 #include <stk_mesh/base/Bucket.hpp>
45 #include <stk_mesh/base/Entity.hpp>
46 #include <stk_mesh/base/GetEntities.hpp>
47 #include <stk_mesh/base/GetNgpField.hpp>
48 #include <stk_mesh/base/GetNgpMesh.hpp>
49 #include <stk_mesh/base/NgpForEachEntity.hpp>
50
51 class ClassWithNgpField
52 {
53 public:
54 KOKKOS_FUNCTION
55 ClassWithNgpField(const stk::mesh::NgpField<double>& ngpField)
56 : m_ngpField(ngpField)
57 {}
58
59 KOKKOS_FUNCTION
60 unsigned num_components_per_entity(const stk::mesh::FastMeshIndex& entity) const
61 { return m_ngpField.get_num_components_per_entity(entity); }
62
63 KOKKOS_FUNCTION
64 double access_field_data(const stk::mesh::FastMeshIndex& entity, unsigned component) const
65 { return m_ngpField(entity, component); }
66
67 private:
68 stk::mesh::NgpField<double> m_ngpField;
69 };
70
71 #ifdef STK_ENABLE_GPU
72 #define MY_LAMBDA KOKKOS_LAMBDA
73 #else
74 #define MY_LAMBDA [&]
75 #endif
76
77 ClassWithNgpField* create_class_on_device(const stk::mesh::NgpField<double>& ngpField)
78 {
79 ClassWithNgpField* devicePtr = static_cast<ClassWithNgpField*>(
80 Kokkos::kokkos_malloc<stk::ngp::MemSpace>("device class memory", sizeof(ClassWithNgpField)));
81 Kokkos::parallel_for("construct class on device", stk::ngp::DeviceRangePolicy(0, 1),
82 MY_LAMBDA(const int /*i*/) { new (devicePtr) ClassWithNgpField(ngpField); }
83 );
84 Kokkos::fence();
85 return devicePtr;
86 }
87
88 void delete_class_on_device(ClassWithNgpField* devicePtr)
89 {
90 Kokkos::parallel_for("device_destruct", stk::ngp::DeviceRangePolicy(0, 1),
91 KOKKOS_LAMBDA(const int /*i*/) { devicePtr->~ClassWithNgpField(); }
92 );
93 Kokkos::fence();
94 Kokkos::kokkos_free<stk::ngp::MemSpace>(static_cast<void*>(devicePtr));
95 }
96
97 class NgpMultiStateFieldTest : public stk::mesh::fixtures::TestHexFixture
98 {
99 public:
100
101 template <typename T>
102 stk::mesh::Field<T> & create_multistate_field(stk::topology::rank_t rank, const std::string & name, unsigned numStates)
103 {
104 stk::mesh::Field<T> & field = get_meta().declare_field<T>(rank, name, numStates);
105 stk::mesh::put_field_on_mesh(field, get_meta().universal_part(), nullptr);
106 return field;
107 }
108
109 void setup_multistate_field()
110 {
111 const unsigned numStates = 2;
112 m_fieldNew = &create_multistate_field<double>(stk::topology::NODE_RANK, "myField", numStates);
113 EXPECT_EQ(stk::mesh::StateNew, m_fieldNew->state());
114
115 m_fieldOld = &m_fieldNew->field_of_state(stk::mesh::StateOld);
116 EXPECT_EQ(stk::mesh::StateOld, m_fieldOld->state());
117 }
118
119 stk::mesh::Field<double>& get_field_new() { return *m_fieldNew; }
120 stk::mesh::Field<double>& get_field_old() { return *m_fieldOld; }
121
122 template <typename ValueType>
123 struct CheckValueUsingNgpField {
124 CheckValueUsingNgpField(const stk::mesh::NgpField<ValueType>& _ngpField, ValueType _expectedValue)
125 : ngpField(_ngpField), expectedValue(_expectedValue)
126 {
127 }
128
129 KOKKOS_FUNCTION
130 void operator()(const stk::mesh::FastMeshIndex& entity) const
131 {
132 unsigned numComponents = ngpField.get_num_components_per_entity(entity);
133 for (unsigned component = 0; component < numComponents; ++component) {
134 NGP_EXPECT_EQ(expectedValue, ngpField(entity, component));
135 }
136 }
137
138 private:
139 stk::mesh::NgpField<ValueType> ngpField;
140 ValueType expectedValue;
141 };
142
143 template<typename T>
144 void check_field_data_value_on_device(const stk::mesh::NgpMesh& ngpMesh,
145 const stk::mesh::NgpField<T>& ngpField,
146 T expectedValue)
147 {
148 stk::mesh::Selector owned = ngpMesh.get_bulk_on_host().mesh_meta_data().locally_owned_part();
149 CheckValueUsingNgpField<T> checkValueUsingNgpField(ngpField, expectedValue);
150 stk::mesh::for_each_entity_run(ngpMesh, ngpField.get_rank(), owned, checkValueUsingNgpField);
151 }
152
153 template <typename ValueType>
154 struct CheckValueUsingClass {
155 CheckValueUsingClass(const ClassWithNgpField* _deviceClassPointer, ValueType _expectedValue)
156 : deviceClassPointer(_deviceClassPointer), expectedValue(_expectedValue)
157 {
158 }
159
160 KOKKOS_FUNCTION
161 void operator()(const stk::mesh::FastMeshIndex& entity) const
162 {
163 unsigned numComponents = deviceClassPointer->num_components_per_entity(entity);
164 for (unsigned component = 0; component < numComponents; ++component) {
165 NGP_EXPECT_EQ(expectedValue, deviceClassPointer->access_field_data(entity, component));
166 }
167 }
168
169 private:
170 const ClassWithNgpField* deviceClassPointer;
171 ValueType expectedValue;
172 };
173
174 template<typename T>
175 void check_field_data_value_on_device(const stk::mesh::NgpMesh& ngpMesh,
176 stk::mesh::EntityRank rank,
177 const ClassWithNgpField* deviceClassPointer,
178 T expectedValue)
179 {
180 stk::mesh::Selector owned = ngpMesh.get_bulk_on_host().mesh_meta_data().locally_owned_part();
181 CheckValueUsingClass<T> checkValueUsingClass(deviceClassPointer, expectedValue);
182 stk::mesh::for_each_entity_run(ngpMesh, rank, owned, checkValueUsingClass);
183 }
184
185 void perform_field_state_rotation()
186 {
187 stk::mesh::sync_to_host_and_mark_modified(get_meta());
188 get_bulk().update_field_data_states();
189 }
190
191 stk::mesh::Field<double>* m_fieldNew;
192 stk::mesh::Field<double>* m_fieldOld;
193 };
194
195 NGP_TEST_F(NgpMultiStateFieldTest, multistateField_rotateDeviceStates_syncStatesUnchanged)
196 {
197 if (get_parallel_size() != 1) GTEST_SKIP();
198 setup_multistate_field();
199 setup_mesh(2, 2, 2);
200
201 const double valueNew = 44.4;
202 const double valueOld = 22.2;
203 stk::mesh::field_fill(valueNew, get_field_new());
204 stk::mesh::field_fill(valueOld, get_field_old());
205
206 stk::mesh::NgpMesh& ngpMesh = stk::mesh::get_updated_ngp_mesh(get_bulk());
207 stk::mesh::NgpField<double>& ngpFieldNew = stk::mesh::get_updated_ngp_field<double>(get_field_new());
208 stk::mesh::NgpField<double>& ngpFieldOld = stk::mesh::get_updated_ngp_field<double>(get_field_old());
209
210 check_field_data_value_on_device(ngpMesh, ngpFieldNew, valueNew);
211 check_field_data_value_on_device(ngpMesh, ngpFieldOld, valueOld);
212
213 const bool rotateDeviceNgpFieldStates = true;
214 get_bulk().update_field_data_states(rotateDeviceNgpFieldStates);
215
216 EXPECT_FALSE(ngpFieldNew.need_sync_to_host());
217 EXPECT_FALSE(ngpFieldOld.need_sync_to_host());
218 EXPECT_FALSE(ngpFieldNew.need_sync_to_device());
219 EXPECT_FALSE(ngpFieldOld.need_sync_to_device());
220
221 check_field_data_value_on_device(ngpMesh, ngpFieldNew, valueOld);
222 check_field_data_value_on_device(ngpMesh, ngpFieldOld, valueNew);
223 }
224
225 NGP_TEST_F(NgpMultiStateFieldTest, multistateField_copyHasCorrectDataAfterStateRotation)
226 {
227 if (get_parallel_size() != 1) GTEST_SKIP();
228 setup_multistate_field();
229 setup_mesh(2, 2, 2);
230
231 const double valueNew = 44.4;
232 const double valueOld = 22.2;
233 stk::mesh::field_fill(valueNew, get_field_new());
234 stk::mesh::field_fill(valueOld, get_field_old());
235
236 stk::mesh::NgpMesh& ngpMesh = stk::mesh::get_updated_ngp_mesh(get_bulk());
237 stk::mesh::NgpField<double>& ngpFieldNew = stk::mesh::get_updated_ngp_field<double>(get_field_new());
238 stk::mesh::NgpField<double>& ngpFieldOld = stk::mesh::get_updated_ngp_field<double>(get_field_old());
239 stk::mesh::NgpField<double> copyOfNgpFieldNew(ngpFieldNew);
240
241 check_field_data_value_on_device(ngpMesh, ngpFieldNew, valueNew);
242 check_field_data_value_on_device(ngpMesh, ngpFieldOld, valueOld);
243
244 perform_field_state_rotation();
245
246 ngpFieldNew.sync_to_device();
247 ngpFieldOld.sync_to_device();
248
249 check_field_data_value_on_device(ngpMesh, ngpFieldNew, valueOld);
250 check_field_data_value_on_device(ngpMesh, ngpFieldOld, valueNew);
251
252 check_field_data_value_on_device(ngpMesh, copyOfNgpFieldNew, valueOld);
253 }
254
255 NGP_TEST_F(NgpMultiStateFieldTest, multistateField_copyHasWrongDataAfterDeviceStateRotation)
256 {
257 if (get_parallel_size() != 1) GTEST_SKIP();
258 setup_multistate_field();
259 setup_mesh(2, 2, 2);
260
261 const double valueNew = 44.4;
262 const double valueOld = 22.2;
263 stk::mesh::field_fill(valueNew, get_field_new());
264 stk::mesh::field_fill(valueOld, get_field_old());
265
266 stk::mesh::NgpMesh& ngpMesh = stk::mesh::get_updated_ngp_mesh(get_bulk());
267 stk::mesh::NgpField<double>& ngpFieldNew = stk::mesh::get_updated_ngp_field<double>(get_field_new());
268 stk::mesh::NgpField<double>& ngpFieldOld = stk::mesh::get_updated_ngp_field<double>(get_field_old());
269 stk::mesh::NgpField<double> copyOfNgpFieldNew(ngpFieldNew);
270
271 check_field_data_value_on_device(ngpMesh, ngpFieldNew, valueNew);
272 check_field_data_value_on_device(ngpMesh, ngpFieldOld, valueOld);
273
274 const bool rotateDeviceNgpFieldStates = true;
275 get_bulk().update_field_data_states(rotateDeviceNgpFieldStates);
276
277 check_field_data_value_on_device(ngpMesh, ngpFieldNew, valueOld);
278 check_field_data_value_on_device(ngpMesh, ngpFieldOld, valueNew);
279
280 #ifdef STK_USE_DEVICE_MESH
281 const double valueNewBecauseCopyOfDeviceFieldGotDisconnectedByDeviceRotation = valueNew;
282 check_field_data_value_on_device(ngpMesh, copyOfNgpFieldNew, valueNewBecauseCopyOfDeviceFieldGotDisconnectedByDeviceRotation);
283 #else
284 check_field_data_value_on_device(ngpMesh, copyOfNgpFieldNew, valueOld);
285 #endif
286 }
287
288 NGP_TEST_F(NgpMultiStateFieldTest, persistentDeviceField_hasCorrectDataAfterStateRotation)
289 {
290 if (get_parallel_size() != 1) GTEST_SKIP();
291 setup_multistate_field();
292 setup_mesh(2, 2, 2);
293
294 const double valueNew = 44.4;
295 const double valueOld = 22.2;
296 stk::mesh::field_fill(valueNew, get_field_new());
297 stk::mesh::field_fill(valueOld, get_field_old());
298
299 stk::mesh::NgpMesh& ngpMesh = stk::mesh::get_updated_ngp_mesh(get_bulk());
300 stk::mesh::NgpField<double>& ngpFieldNew = stk::mesh::get_updated_ngp_field<double>(get_field_new());
301 stk::mesh::NgpField<double>& ngpFieldOld = stk::mesh::get_updated_ngp_field<double>(get_field_old());
302
303 ClassWithNgpField* persistentDeviceClass = create_class_on_device(ngpFieldNew);
304
305 check_field_data_value_on_device(ngpMesh, stk::topology::NODE_RANK, persistentDeviceClass, valueNew);
306
307 perform_field_state_rotation();
308
309 ngpFieldNew.sync_to_device();
310 ngpFieldOld.sync_to_device();
311
312 check_field_data_value_on_device(ngpMesh, stk::topology::NODE_RANK, persistentDeviceClass, valueOld);
313
314 delete_class_on_device(persistentDeviceClass);
315 }
316
317 NGP_TEST_F(NgpMultiStateFieldTest, persistentDeviceField_hasWrongDataAfterDeviceStateRotation)
318 {
319 if (get_parallel_size() != 1) GTEST_SKIP();
320 setup_multistate_field();
321 setup_mesh(2, 2, 2);
322
323 const double valueNew = 44.4;
324 const double valueOld = 22.2;
325 stk::mesh::field_fill(valueNew, get_field_new());
326 stk::mesh::field_fill(valueOld, get_field_old());
327
328 stk::mesh::NgpMesh& ngpMesh = stk::mesh::get_updated_ngp_mesh(get_bulk());
329 stk::mesh::NgpField<double>& ngpFieldNew = stk::mesh::get_updated_ngp_field<double>(get_field_new());
330 stk::mesh::NgpField<double>& ngpFieldOld = stk::mesh::get_updated_ngp_field<double>(get_field_old());
331
332 ClassWithNgpField* persistentDeviceClass = create_class_on_device(ngpFieldNew);
333
334 check_field_data_value_on_device(ngpMesh, stk::topology::NODE_RANK, persistentDeviceClass, valueNew);
335
336 const bool rotateDeviceNgpFieldStates = true;
337 get_bulk().update_field_data_states(rotateDeviceNgpFieldStates);
338
339 ngpFieldNew.sync_to_device();
340 ngpFieldOld.sync_to_device();
341
342 check_field_data_value_on_device(ngpMesh, ngpFieldNew, valueOld);
343 #ifdef STK_USE_DEVICE_MESH
344 const double valueNewBecausePersistentDeviceFieldGotDisconnectedByDeviceRotation = valueNew;
345 check_field_data_value_on_device(ngpMesh, stk::topology::NODE_RANK, persistentDeviceClass, valueNewBecausePersistentDeviceFieldGotDisconnectedByDeviceRotation);
346 #else
347 check_field_data_value_on_device(ngpMesh, stk::topology::NODE_RANK, persistentDeviceClass, valueOld);
348 #endif
349
350 delete_class_on_device(persistentDeviceClass);
351 }
352
353 NGP_TEST_F(NgpMultiStateFieldTest, persistentSyncToDeviceCountAfterStateRotation)
354 {
355 if (get_parallel_size() != 1) GTEST_SKIP();
356 setup_multistate_field();
357 setup_mesh(2, 2, 2);
358
359 stk::mesh::NgpField<double>& ngpFieldNew = stk::mesh::get_updated_ngp_field<double>(get_field_new());
360 stk::mesh::NgpField<double>& ngpFieldOld = stk::mesh::get_updated_ngp_field<double>(get_field_old());
361
362 EXPECT_EQ(ngpFieldNew.num_syncs_to_device(), ngpFieldOld.num_syncs_to_device());
363
364 ngpFieldNew.modify_on_host();
365 ngpFieldNew.sync_to_device();
366
367 EXPECT_EQ(ngpFieldNew.num_syncs_to_device(), ngpFieldOld.num_syncs_to_device()+1);
368
369 perform_field_state_rotation();
370
371 EXPECT_EQ(ngpFieldNew.num_syncs_to_device()+1, ngpFieldOld.num_syncs_to_device());
372 }
373
374 NGP_TEST_F(NgpMultiStateFieldTest, persistentSyncToHostCountAfterStateRotation)
375 {
376 if (get_parallel_size() != 1) GTEST_SKIP();
377 setup_multistate_field();
378 setup_mesh(2, 2, 2);
379
380 stk::mesh::NgpField<double>& ngpFieldNew = stk::mesh::get_updated_ngp_field<double>(get_field_new());
381 stk::mesh::NgpField<double>& ngpFieldOld = stk::mesh::get_updated_ngp_field<double>(get_field_old());
382
383 EXPECT_EQ(ngpFieldNew.num_syncs_to_device(), ngpFieldOld.num_syncs_to_device());
384
385 ngpFieldOld.modify_on_device();
386 ngpFieldOld.sync_to_host();
387
388 EXPECT_EQ(ngpFieldNew.num_syncs_to_host()+1, ngpFieldOld.num_syncs_to_host());
389
390 perform_field_state_rotation();
391
392 EXPECT_EQ(ngpFieldNew.num_syncs_to_host(), ngpFieldOld.num_syncs_to_host()+1);
393 }
394
395 NGP_TEST_F(NgpMultiStateFieldTest, persistentModifyOnHostAfterStateRotation)
396 {
397 if (get_parallel_size() != 1) GTEST_SKIP();
398 setup_multistate_field();
399 setup_mesh(2, 2, 2);
400
401 stk::mesh::NgpField<double>& ngpFieldNew = stk::mesh::get_updated_ngp_field<double>(get_field_new());
402 stk::mesh::NgpField<double>& ngpFieldOld = stk::mesh::get_updated_ngp_field<double>(get_field_old());
403 ngpFieldNew.clear_sync_state();
404 ngpFieldOld.clear_sync_state();
405
406 ngpFieldNew.modify_on_host();
407
408 EXPECT_TRUE(ngpFieldNew.need_sync_to_device());
409 EXPECT_FALSE(ngpFieldOld.need_sync_to_device());
410
411 get_bulk().update_field_data_states();
412
413 EXPECT_FALSE(ngpFieldNew.need_sync_to_device());
414 EXPECT_TRUE(ngpFieldOld.need_sync_to_device());
415 }