| 1 |
#include <gtest/gtest.h> |
| 2 |
#include <stk_util/stk_config.h> |
| 3 |
#include <stk_mesh/base/Ngp.hpp> |
| 4 |
#include <stk_mesh/base/NgpMesh.hpp> |
| 5 |
#include <stk_mesh/base/NgpField.hpp> |
| 6 |
#include <stk_mesh/base/MetaData.hpp> |
| 7 |
#include <stk_mesh/base/BulkData.hpp> |
| 8 |
#include <stk_mesh/base/MeshBuilder.hpp> |
| 9 |
#include <stk_mesh/base/Bucket.hpp> |
| 10 |
#include <stk_mesh/base/Field.hpp> |
| 11 |
#include <stk_mesh/base/Entity.hpp> |
| 12 |
#include <stk_mesh/base/GetNgpField.hpp> |
| 13 |
#include <stk_util/parallel/Parallel.hpp> |
| 14 |
#include <stk_io/FillMesh.hpp> |
| 15 |
#include "NgpUnitTestUtils.hpp" |
| 16 |
|
| 17 |
namespace { |
| 18 |
|
| 19 |
using UnsignedDualViewType = Kokkos::DualView<unsigned*, stk::ngp::ExecSpace>; |
| 20 |
|
| 21 |
void test_view_of_fields(const stk::mesh::BulkData& /*bulk*/, |
| 22 |
stk::mesh::Field<double>& field1, |
| 23 |
stk::mesh::Field<double>& field2) |
| 24 |
{ |
| 25 |
using FieldViewType = Kokkos::View<stk::mesh::NgpField<double>*,stk::ngp::MemSpace>; |
| 26 |
|
| 27 |
FieldViewType fields(Kokkos::ViewAllocateWithoutInitializing("fields"),2); |
| 28 |
FieldViewType::HostMirror hostFields = Kokkos::create_mirror_view(fields); |
| 29 |
|
| 30 |
Kokkos::parallel_for(stk::ngp::DeviceRangePolicy(0, 2), |
| 31 |
KOKKOS_LAMBDA(const unsigned& i) |
| 32 |
{ |
| 33 |
new (&fields(i)) stk::mesh::NgpField<double>(); |
| 34 |
}); |
| 35 |
|
| 36 |
hostFields(0) = stk::mesh::get_updated_ngp_field<double>(field1); |
| 37 |
hostFields(1) = stk::mesh::get_updated_ngp_field<double>(field2); |
| 38 |
|
| 39 |
Kokkos::deep_copy(fields, hostFields); |
| 40 |
|
| 41 |
unsigned numResults = 2; |
| 42 |
UnsignedDualViewType result = ngp_unit_test_utils::create_dualview<UnsignedDualViewType>("result",numResults); |
| 43 |
|
| 44 |
Kokkos::parallel_for(stk::ngp::DeviceRangePolicy(0, 2), |
| 45 |
KOKKOS_LAMBDA(const unsigned& i) |
| 46 |
{ |
| 47 |
result.view_device()(i) = fields(i).get_ordinal(); |
| 48 |
}); |
| 49 |
|
| 50 |
result.modify<UnsignedDualViewType::execution_space>(); |
| 51 |
result.sync<UnsignedDualViewType::host_mirror_space>(); |
| 52 |
|
| 53 |
EXPECT_EQ(hostFields(0).get_ordinal(), result.view_host()(0)); |
| 54 |
EXPECT_EQ(hostFields(1).get_ordinal(), result.view_host()(1)); |
| 55 |
|
| 56 |
#if !defined(KOKKOS_ENABLE_CUDA) && !defined(KOKKOS_ENABLE_HIP) |
| 57 |
for (unsigned i = 0; i < 2; ++i) { |
| 58 |
#ifdef STK_USE_DEVICE_MESH // Compiler can't resolve destructor type through NgpField using statement |
| 59 |
fields(i).~DeviceField(); |
| 60 |
#else |
| 61 |
fields(i).~HostField(); |
| 62 |
#endif |
| 63 |
} |
| 64 |
#endif |
| 65 |
} |
| 66 |
|
| 67 |
TEST(UnitTestNgp, viewOfFields) |
| 68 |
{ |
| 69 |
std::shared_ptr<stk::mesh::BulkData> bulk = stk::mesh::MeshBuilder(MPI_COMM_WORLD).create(); |
| 70 |
stk::mesh::MetaData& meta = bulk->mesh_meta_data(); |
| 71 |
auto &field1 = meta.declare_field<double>(stk::topology::NODE_RANK, "myField1"); |
| 72 |
auto &field2 = meta.declare_field<double>(stk::topology::NODE_RANK, "myField2"); |
| 73 |
stk::mesh::put_field_on_mesh(field1, meta.universal_part(), nullptr); |
| 74 |
stk::mesh::put_field_on_mesh(field2, meta.universal_part(), nullptr); |
| 75 |
stk::io::fill_mesh("generated:1x1x4|sideset:zZ", *bulk); |
| 76 |
|
| 77 |
test_view_of_fields(*bulk, field1, field2); |
| 78 |
} |
| 79 |
|
| 80 |
} |