| 1 |
#ifndef NgpUnitTestUtils_hpp |
| 2 |
#define NgpUnitTestUtils_hpp |
| 3 |
|
| 4 |
#include <gtest/gtest.h> |
| 5 |
#include <Kokkos_Core.hpp> |
| 6 |
#include <Kokkos_DualView.hpp> |
| 7 |
#include <stk_mesh/base/Ngp.hpp> |
| 8 |
#include <stk_mesh/base/NgpMesh.hpp> |
| 9 |
#include <stk_mesh/base/MetaData.hpp> |
| 10 |
#include <stk_mesh/base/BulkData.hpp> |
| 11 |
#include <stk_mesh/base/GetNgpMesh.hpp> |
| 12 |
#include <stk_util/ngp/NgpSpaces.hpp> |
| 13 |
#include <stk_unit_test_utils/TextMesh.hpp> |
| 14 |
#include <stk_unit_test_utils/GetMeshSpec.hpp> |
| 15 |
#include <stk_ngp_test/ngp_test.hpp> |
| 16 |
|
| 17 |
namespace ngp_unit_test_utils { |
| 18 |
|
| 19 |
constexpr unsigned MaxNumParts = 4; |
| 20 |
|
| 21 |
struct BucketContents |
| 22 |
{ |
| 23 |
std::vector<std::string> partNames; |
| 24 |
std::vector<stk::mesh::EntityId> entities; |
| 25 |
}; |
| 26 |
|
| 27 |
template<typename DualViewType> |
| 28 |
DualViewType create_dualview(const std::string& name, unsigned size) |
| 29 |
{ |
| 30 |
DualViewType result(name, size); |
| 31 |
|
| 32 |
Kokkos::deep_copy(result.view_host(), 0); |
| 33 |
result.template modify<typename DualViewType::host_mirror_space>(); |
| 34 |
result.template sync<typename DualViewType::execution_space>(); |
| 35 |
|
| 36 |
return result; |
| 37 |
} |
| 38 |
|
| 39 |
inline void setup_mesh_4hex_4block(stk::mesh::BulkData& bulk, unsigned /*bucketCapacity*/) |
| 40 |
{ |
| 41 |
std::string meshDesc = stk::unit_test_util::get_many_block_mesh_desc(4); |
| 42 |
stk::unit_test_util::setup_text_mesh(bulk, meshDesc); |
| 43 |
} |
| 44 |
|
| 45 |
inline void setup_mesh_3hex_3block(stk::mesh::BulkData& bulk, unsigned /*bucketCapacity*/) |
| 46 |
{ |
| 47 |
std::string meshDesc = "0,1,HEX_8,1,2,3,4,5,6,7,8,block_1\n" |
| 48 |
"0,2,HEX_8,5,6,7,8,9,10,11,12,block_2\n" |
| 49 |
"0,3,HEX_8,9,10,11,12,13,14,15,16,block_3"; |
| 50 |
stk::unit_test_util::setup_text_mesh(bulk, meshDesc); |
| 51 |
} |
| 52 |
|
| 53 |
inline void setup_mesh_3hex_2block(stk::mesh::BulkData& bulk, unsigned /*bucketCapacity*/) |
| 54 |
{ |
| 55 |
std::string meshDesc = "0,1,HEX_8,1,2,3,4,5,6,7,8,block_1\n" |
| 56 |
"0,2,HEX_8,5,6,7,8,9,10,11,12,block_1\n" |
| 57 |
"0,3,HEX_8,9,10,11,12,13,14,15,16,block_3"; |
| 58 |
stk::unit_test_util::setup_text_mesh(bulk, meshDesc); |
| 59 |
} |
| 60 |
|
| 61 |
inline void setup_mesh_2hex_2block(stk::mesh::BulkData& bulk, unsigned /*bucketCapacity*/) |
| 62 |
{ |
| 63 |
std::string meshDesc = "0,1,HEX_8,1,2,3,4,5,6,7,8,block_1\n" |
| 64 |
"0,2,HEX_8,5,6,7,8,9,10,11,12,block_2"; |
| 65 |
stk::unit_test_util::setup_text_mesh(bulk, meshDesc); |
| 66 |
} |
| 67 |
|
| 68 |
struct CheckBucketParts { |
| 69 |
using BucketPartOrdinalType = Kokkos::View<stk::mesh::PartOrdinal*, stk::ngp::MemSpace>; |
| 70 |
CheckBucketParts( |
| 71 |
const stk::mesh::NgpMesh& _ngpMesh, BucketPartOrdinalType _bucketPartOrdinals, size_t _numBuckets, |
| 72 |
const stk::topology::rank_t _bucketRank) |
| 73 |
: ngpMesh(_ngpMesh), |
| 74 |
bucketPartOrdinals(_bucketPartOrdinals), |
| 75 |
numBuckets(_numBuckets), |
| 76 |
bucketRank(_bucketRank) |
| 77 |
{ |
| 78 |
} |
| 79 |
|
| 80 |
KOKKOS_FUNCTION |
| 81 |
void operator()(size_t) const |
| 82 |
{ |
| 83 |
for (unsigned bucketIdx = 0; bucketIdx < numBuckets; ++bucketIdx) { |
| 84 |
for (unsigned partIdx = 0; partIdx < MaxNumParts; ++partIdx) { |
| 85 |
unsigned partOffset = bucketIdx*MaxNumParts + partIdx; |
| 86 |
if (bucketPartOrdinals[partOffset] != stk::mesh::InvalidOrdinal) { |
| 87 |
NGP_EXPECT_TRUE(ngpMesh.get_bucket(bucketRank, bucketIdx).member(bucketPartOrdinals[partOffset])); |
| 88 |
} |
| 89 |
} |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
private: |
| 94 |
stk::mesh::NgpMesh ngpMesh; |
| 95 |
BucketPartOrdinalType bucketPartOrdinals; |
| 96 |
size_t numBuckets; |
| 97 |
stk::topology::rank_t bucketRank; |
| 98 |
}; |
| 99 |
|
| 100 |
inline void check_bucket_layout(const stk::mesh::BulkData& bulk, |
| 101 |
const std::vector<BucketContents> & expectedBucketLayout, |
| 102 |
const stk::topology::rank_t bucketRank = stk::topology::ELEM_RANK) |
| 103 |
{ |
| 104 |
const stk::mesh::MetaData& meta = bulk.mesh_meta_data(); |
| 105 |
const stk::mesh::BucketVector & buckets = bulk.buckets(bucketRank); |
| 106 |
size_t numBuckets = buckets.size(); |
| 107 |
ASSERT_EQ(numBuckets, expectedBucketLayout.size()) << "Found " << numBuckets << " Host Buckets when expecting " |
| 108 |
<< expectedBucketLayout.size(); |
| 109 |
|
| 110 |
size_t numEntitiesAcrossBuckets = 0; |
| 111 |
for (size_t bucketIdx = 0; bucketIdx < numBuckets; ++bucketIdx) { |
| 112 |
const stk::mesh::Bucket & bucket = *buckets[bucketIdx]; |
| 113 |
const BucketContents & bucketContents = expectedBucketLayout[bucketIdx]; |
| 114 |
|
| 115 |
for (const std::string& partName : bucketContents.partNames) { |
| 116 |
const stk::mesh::Part& expectedPart = *meta.get_part(partName); |
| 117 |
EXPECT_TRUE(bucket.member(expectedPart)) << "Host Bucket " << bucket.bucket_id() << " not a member of Part " |
| 118 |
<< expectedPart.name(); |
| 119 |
} |
| 120 |
|
| 121 |
numEntitiesAcrossBuckets += bucket.size(); |
| 122 |
ASSERT_EQ(bucket.size(), bucketContents.entities.size()) << "Found " << bucket.size() |
| 123 |
<< " Entities in Host Bucket when expecting " |
| 124 |
<< bucketContents.entities.size(); |
| 125 |
for (unsigned i = 0; i < bucket.size(); ++i) { |
| 126 |
EXPECT_EQ(bulk.identifier(bucket[i]), bucketContents.entities[i]) << "Found " << bucket[i] |
| 127 |
<< " in Host Bucket when expecting " |
| 128 |
<< bucketContents.entities[i]; |
| 129 |
} |
| 130 |
} |
| 131 |
|
| 132 |
using BucketPartOrdinalType = Kokkos::View<stk::mesh::PartOrdinal*, stk::ngp::MemSpace>; |
| 133 |
BucketPartOrdinalType bucketPartOrdinals("bucketPartOrdinals", numBuckets*MaxNumParts); |
| 134 |
BucketPartOrdinalType::HostMirror hostBucketPartOrdinals = Kokkos::create_mirror_view(bucketPartOrdinals); |
| 135 |
Kokkos::deep_copy(hostBucketPartOrdinals, stk::mesh::InvalidOrdinal); |
| 136 |
for (size_t bucketIdx = 0; bucketIdx < buckets.size(); ++bucketIdx) { |
| 137 |
const unsigned numExpectedParts = expectedBucketLayout[bucketIdx].partNames.size(); |
| 138 |
STK_ThrowRequireMsg(numExpectedParts <= MaxNumParts, "Checking more Parts than test fixture supports"); |
| 139 |
for (size_t partIdx = 0; partIdx < numExpectedParts; ++partIdx) { |
| 140 |
const std::string& partName = expectedBucketLayout[bucketIdx].partNames[partIdx]; |
| 141 |
unsigned partOffset = bucketIdx*MaxNumParts + partIdx; |
| 142 |
hostBucketPartOrdinals[partOffset] = meta.get_part(partName)->mesh_meta_data_ordinal(); |
| 143 |
} |
| 144 |
} |
| 145 |
Kokkos::deep_copy(bucketPartOrdinals, hostBucketPartOrdinals); |
| 146 |
|
| 147 |
stk::mesh::NgpMesh & ngpMesh = stk::mesh::get_updated_ngp_mesh(bulk); |
| 148 |
CheckBucketParts checkBucketParts(ngpMesh, bucketPartOrdinals, numBuckets, bucketRank); |
| 149 |
Kokkos::parallel_for(stk::ngp::DeviceRangePolicy(0, 1), checkBucketParts); |
| 150 |
|
| 151 |
ASSERT_EQ(ngpMesh.num_buckets(bucketRank), numBuckets) << "Found " << ngpMesh.num_buckets(bucketRank) |
| 152 |
<< " Device Buckets when expecting " << numBuckets; |
| 153 |
|
| 154 |
for (unsigned bucketIdx = 0; bucketIdx < numBuckets; ++bucketIdx) { |
| 155 |
const stk::mesh::NgpMesh::BucketType & ngpBucket = ngpMesh.get_bucket(bucketRank, bucketIdx); |
| 156 |
const stk::mesh::Bucket & bucket = *buckets[bucketIdx]; |
| 157 |
ASSERT_EQ(bucket.size(), ngpBucket.size()); |
| 158 |
} |
| 159 |
|
| 160 |
using BucketEntitiesType = Kokkos::View<stk::mesh::Entity*, stk::ngp::MemSpace>; |
| 161 |
BucketEntitiesType bucketEntities("bucketEntities", numEntitiesAcrossBuckets); |
| 162 |
BucketEntitiesType::HostMirror hostBucketEntities = Kokkos::create_mirror_view(bucketEntities); |
| 163 |
|
| 164 |
Kokkos::parallel_for(stk::ngp::DeviceRangePolicy(0, 1), |
| 165 |
KOKKOS_LAMBDA(size_t /*index*/) { |
| 166 |
size_t idx = 0; |
| 167 |
for (unsigned bucketIdx = 0; bucketIdx < numBuckets; ++bucketIdx) { |
| 168 |
const stk::mesh::NgpMesh::BucketType & bucket = ngpMesh.get_bucket(bucketRank, bucketIdx); |
| 169 |
for (size_t i = 0; i < bucket.size(); ++i) { |
| 170 |
bucketEntities[idx++] = bucket[i]; |
| 171 |
} |
| 172 |
} |
| 173 |
}); |
| 174 |
|
| 175 |
Kokkos::deep_copy(hostBucketEntities, bucketEntities); |
| 176 |
|
| 177 |
size_t index = 0; |
| 178 |
for (size_t bucketIdx = 0; bucketIdx < numBuckets; ++bucketIdx) { |
| 179 |
const stk::mesh::Bucket & bucket = *buckets[bucketIdx]; |
| 180 |
for (unsigned i = 0; i < bucket.size(); ++i) { |
| 181 |
const stk::mesh::Entity deviceEntity = hostBucketEntities[index++]; |
| 182 |
EXPECT_EQ(bucket[i], deviceEntity) << "Found " << deviceEntity << " in Device Bucket when expecting " |
| 183 |
<< bucket[i]; |
| 184 |
} |
| 185 |
} |
| 186 |
} |
| 187 |
|
| 188 |
} // ngp_unit_test_utils |
| 189 |
|
| 190 |
#endif |
| 191 |
|