| 1 |
// Copyright 2002 - 2008, 2010, 2011 National Technology Engineering |
| 2 |
// Solutions of Sandia, LLC (NTESS). Under the terms of Contract |
| 3 |
// DE-NA0003525 with NTESS, the U.S. Government retains certain rights |
| 4 |
// in this software. |
| 5 |
// |
| 6 |
// Redistribution and use in source and binary forms, with or without |
| 7 |
// modification, are permitted provided that the following conditions are |
| 8 |
// met: |
| 9 |
// |
| 10 |
// * Redistributions of source code must retain the above copyright |
| 11 |
// notice, this list of conditions and the following disclaimer. |
| 12 |
// |
| 13 |
// * Redistributions in binary form must reproduce the above |
| 14 |
// copyright notice, this list of conditions and the following |
| 15 |
// disclaimer in the documentation and/or other materials provided |
| 16 |
// with the distribution. |
| 17 |
// |
| 18 |
// * Neither the name of NTESS nor the names of its contributors |
| 19 |
// may be used to endorse or promote products derived from this |
| 20 |
// software without specific prior written permission. |
| 21 |
// |
| 22 |
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 23 |
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 24 |
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 25 |
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 26 |
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 27 |
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 28 |
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 29 |
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 30 |
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 31 |
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 32 |
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 33 |
// |
| 34 |
|
| 35 |
#include <gtest/gtest.h> |
| 36 |
#include <stk_mesh/base/NgpTypes.hpp> |
| 37 |
#include <stk_mesh/base/NgpField.hpp> |
| 38 |
#include <stk_mesh/base/FieldDataManager.hpp> |
| 39 |
#include <stk_mesh/baseImpl/NgpFieldAux.hpp> |
| 40 |
#include <stk_util/parallel/Parallel.hpp> |
| 41 |
#include <stk_util/util/FieldDataAllocator.hpp> |
| 42 |
#include <stk_ngp_test/ngp_test.hpp> |
| 43 |
|
| 44 |
namespace { |
| 45 |
|
| 46 |
constexpr unsigned bucketCapacity = 4; |
| 47 |
constexpr unsigned numPerEntity = 3; |
| 48 |
constexpr unsigned bytesPerBucket = sizeof(double)*bucketCapacity*numPerEntity; |
| 49 |
|
| 50 |
class TestTranspose : public ::testing::Test |
| 51 |
{ |
| 52 |
public: |
| 53 |
TestTranspose() {} |
| 54 |
|
| 55 |
~TestTranspose() |
| 56 |
{ |
| 57 |
for(unsigned char* ptr : rawBucketAllocations) { |
| 58 |
fieldDataAllocator.deallocate(ptr, bytesPerBucket); |
| 59 |
} |
| 60 |
rawBucketAllocations.clear(); |
| 61 |
} |
| 62 |
|
| 63 |
double get_value(unsigned bktIndex, unsigned entityIndex, unsigned component) |
| 64 |
{ |
| 65 |
return (bktIndex+1)*1000 + (entityIndex+1)*100 + (component+1); |
| 66 |
} |
| 67 |
|
| 68 |
void fill_gold_host_field_data(unsigned numBuckets) |
| 69 |
{ |
| 70 |
for(unsigned bkt=0; bkt<numBuckets; ++bkt) { |
| 71 |
for(unsigned entityIndex=0; entityIndex<bucketCapacity; ++entityIndex) { |
| 72 |
for(unsigned comp=0; comp<numPerEntity; ++comp) { |
| 73 |
goldHostFieldData(bkt,ORDER_INDICES(entityIndex,comp)) = get_value(bkt,entityIndex,comp); |
| 74 |
} |
| 75 |
} |
| 76 |
} |
| 77 |
} |
| 78 |
|
| 79 |
void fill_bucket_pointers(unsigned numBuckets) |
| 80 |
{ |
| 81 |
rawBucketAllocations.resize(numBuckets); |
| 82 |
for(unsigned i=0; i<numBuckets; ++i) { |
| 83 |
rawBucketAllocations[i] = fieldDataAllocator.allocate(bytesPerBucket); |
| 84 |
ASAN_UNPOISON_MEMORY_REGION(rawBucketAllocations[i], bytesPerBucket); |
| 85 |
double* hostBucketPtr = reinterpret_cast<double*>(rawBucketAllocations[i]); |
| 86 |
|
| 87 |
for(unsigned entityIndex=0; entityIndex<bucketCapacity; ++entityIndex) { |
| 88 |
for(unsigned comp=0; comp<numPerEntity; ++comp) { |
| 89 |
hostBucketPtr[entityIndex*numPerEntity + comp] = get_value(i,entityIndex,comp); |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
double* deviceBucketPtr = hostBucketPtr; |
| 94 |
|
| 95 |
#ifdef KOKKOS_ENABLE_CUDA |
| 96 |
cudaError_t status = cudaHostGetDevicePointer((void**)&deviceBucketPtr, (void*)hostBucketPtr, 0); |
| 97 |
STK_ThrowRequireMsg(status == cudaSuccess, "Something went wrong during cudaHostGetDevicePointer: " + std::string(cudaGetErrorString(status))); |
| 98 |
#elif defined(KOKKOS_ENABLE_HIP) |
| 99 |
hipError_t status = hipHostGetDevicePointer((void**)&deviceBucketPtr, (void*)hostBucketPtr, 0); |
| 100 |
STK_ThrowRequireMsg(status == hipSuccess, "Something went wrong during hipHostGetDevicePointer: " + std::string(hipGetErrorString(status))); |
| 101 |
#endif |
| 102 |
|
| 103 |
hostBucketPtrData(i) = reinterpret_cast<uintptr_t>(deviceBucketPtr); |
| 104 |
} |
| 105 |
|
| 106 |
Kokkos::deep_copy(deviceBucketPtrData, hostBucketPtrData); |
| 107 |
} |
| 108 |
|
| 109 |
void setup_views(unsigned numBuckets, double overallocationFactor) |
| 110 |
{ |
| 111 |
deviceFieldData = stk::mesh::FieldDataDeviceViewType<double, stk::mesh::NgpMeshDefaultMemSpace>(Kokkos::view_alloc(Kokkos::WithoutInitializing, "deviceFieldData"), numBuckets, ORDER_INDICES(bucketCapacity, numPerEntity)); |
| 112 |
goldHostFieldData = stk::mesh::FieldDataHostViewType<double>(Kokkos::view_alloc(Kokkos::WithoutInitializing, "goldHostFieldData"), numBuckets, ORDER_INDICES(bucketCapacity,numPerEntity)); |
| 113 |
|
| 114 |
fill_gold_host_field_data(numBuckets); |
| 115 |
|
| 116 |
const unsigned numAlloc = std::lround(numBuckets*overallocationFactor); |
| 117 |
|
| 118 |
deviceBucketPtrData = stk::mesh::FieldDataPointerDeviceViewType(Kokkos::view_alloc(Kokkos::WithoutInitializing, "DeviceBktPtrData"), numAlloc); |
| 119 |
hostBucketPtrData = stk::mesh::FieldDataPointerHostViewType(Kokkos::view_alloc(Kokkos::WithoutInitializing, "HostBktPtrData"), numAlloc); |
| 120 |
|
| 121 |
fill_bucket_pointers(numBuckets); |
| 122 |
|
| 123 |
deviceBucketSizes = stk::mesh::UnsignedViewType(Kokkos::view_alloc(Kokkos::WithoutInitializing, "DeviceBucketSizes"), numAlloc); |
| 124 |
hostBucketSizes = Kokkos::create_mirror_view(deviceBucketSizes); |
| 125 |
Kokkos::deep_copy(hostBucketSizes, bucketCapacity); |
| 126 |
Kokkos::deep_copy(deviceBucketSizes, hostBucketSizes); |
| 127 |
|
| 128 |
deviceBucketComponentsPerEntity = stk::mesh::UnsignedViewType(Kokkos::view_alloc(Kokkos::WithoutInitializing, "DeviceBucketComponentsPerEntity"), numAlloc); |
| 129 |
hostBucketComponentsPerEntity = Kokkos::create_mirror_view(deviceBucketComponentsPerEntity); |
| 130 |
Kokkos::deep_copy(hostBucketComponentsPerEntity, numPerEntity); |
| 131 |
Kokkos::deep_copy(deviceBucketComponentsPerEntity, hostBucketComponentsPerEntity); |
| 132 |
|
| 133 |
deviceBucketsMarkedModified = stk::mesh::UnsignedViewType(Kokkos::view_alloc(Kokkos::WithoutInitializing, "DeviceBucketsMarkedModified"), numAlloc); |
| 134 |
hostBucketsMarkedModified = Kokkos::create_mirror_view(deviceBucketsMarkedModified); |
| 135 |
Kokkos::deep_copy(hostBucketsMarkedModified, true); |
| 136 |
Kokkos::deep_copy(deviceBucketsMarkedModified, hostBucketsMarkedModified); |
| 137 |
} |
| 138 |
|
| 139 |
void check_field_data_values(unsigned numBuckets) |
| 140 |
{ |
| 141 |
Kokkos::fence(); |
| 142 |
auto checkFieldData = Kokkos::create_mirror_view(deviceFieldData); |
| 143 |
Kokkos::deep_copy(checkFieldData, deviceFieldData); |
| 144 |
|
| 145 |
for(unsigned bkt=0; bkt<numBuckets; ++bkt) { |
| 146 |
for(unsigned entityIndex=0; entityIndex<bucketCapacity; ++entityIndex) { |
| 147 |
for(unsigned comp=0; comp<numPerEntity; ++comp) { |
| 148 |
EXPECT_NEAR(goldHostFieldData(bkt,ORDER_INDICES(entityIndex,comp)), checkFieldData(bkt,ORDER_INDICES(entityIndex,comp)), 1.e-8)<<bkt<<":"<<entityIndex<<":"<<comp<<";"; |
| 149 |
} |
| 150 |
} |
| 151 |
} |
| 152 |
} |
| 153 |
|
| 154 |
void check_host_field_data_values(unsigned numBuckets) |
| 155 |
{ |
| 156 |
Kokkos::fence(); |
| 157 |
for(unsigned bkt=0; bkt<numBuckets; ++bkt) { |
| 158 |
const double* hostBucketPtr = reinterpret_cast<const double*>(hostBucketPtrData(bkt)); |
| 159 |
for(unsigned entityIndex=0; entityIndex<bucketCapacity; ++entityIndex) { |
| 160 |
for(unsigned comp=0; comp<numPerEntity; ++comp) { |
| 161 |
EXPECT_NEAR(goldHostFieldData(bkt,ORDER_INDICES(entityIndex,comp)), hostBucketPtr[entityIndex*numPerEntity+comp], 1.e-8); |
| 162 |
} |
| 163 |
} |
| 164 |
} |
| 165 |
} |
| 166 |
|
| 167 |
protected: |
| 168 |
stk::mesh::AllocatorAdaptor<stk::impl::FieldDataAllocator<unsigned char>> fieldDataAllocator; |
| 169 |
std::vector<unsigned char*> rawBucketAllocations; |
| 170 |
|
| 171 |
stk::mesh::FieldDataPointerHostViewType hostBucketPtrData; |
| 172 |
stk::mesh::FieldDataPointerDeviceViewType deviceBucketPtrData; |
| 173 |
|
| 174 |
stk::mesh::FieldDataDeviceViewType<double, stk::mesh::NgpMeshDefaultMemSpace> deviceFieldData; |
| 175 |
stk::mesh::FieldDataHostViewType<double> goldHostFieldData; |
| 176 |
|
| 177 |
stk::mesh::UnsignedViewType deviceBucketSizes; |
| 178 |
stk::mesh::UnsignedViewType::HostMirror hostBucketSizes; |
| 179 |
stk::mesh::UnsignedViewType deviceBucketComponentsPerEntity; |
| 180 |
stk::mesh::UnsignedViewType::HostMirror hostBucketComponentsPerEntity; |
| 181 |
stk::mesh::UnsignedViewType deviceBucketsMarkedModified; |
| 182 |
stk::mesh::UnsignedViewType::HostMirror hostBucketsMarkedModified; |
| 183 |
}; |
| 184 |
|
| 185 |
TEST_F(TestTranspose, no_overallocation_transpose_from) |
| 186 |
{ |
| 187 |
if (stk::parallel_machine_size(MPI_COMM_WORLD) > 1) { GTEST_SKIP(); } |
| 188 |
|
| 189 |
const unsigned numBuckets = 2; |
| 190 |
const double overAllocFactor = 1.0; |
| 191 |
setup_views(numBuckets, overAllocFactor); |
| 192 |
stk::ngp::ExecSpace execSpace = Kokkos::DefaultExecutionSpace(); |
| 193 |
stk::mesh::impl::transpose_from_pinned_and_mapped_memory(execSpace, deviceBucketPtrData, deviceFieldData, deviceBucketSizes, deviceBucketComponentsPerEntity); |
| 194 |
check_field_data_values(numBuckets); |
| 195 |
} |
| 196 |
|
| 197 |
TEST_F(TestTranspose, overallocate_2_transpose_from) |
| 198 |
{ |
| 199 |
if (stk::parallel_machine_size(MPI_COMM_WORLD) > 1) { GTEST_SKIP(); } |
| 200 |
|
| 201 |
const unsigned numBuckets = 3; |
| 202 |
const double overAllocFactor = 2.0; |
| 203 |
setup_views(numBuckets, overAllocFactor); |
| 204 |
stk::ngp::ExecSpace execSpace = Kokkos::DefaultExecutionSpace(); |
| 205 |
stk::mesh::impl::transpose_from_pinned_and_mapped_memory(execSpace, deviceBucketPtrData, deviceFieldData, deviceBucketSizes, deviceBucketComponentsPerEntity); |
| 206 |
check_field_data_values(numBuckets); |
| 207 |
} |
| 208 |
|
| 209 |
TEST_F(TestTranspose, no_overallocation_transpose_new_and_modified) |
| 210 |
{ |
| 211 |
if (stk::parallel_machine_size(MPI_COMM_WORLD) > 1) { GTEST_SKIP(); } |
| 212 |
|
| 213 |
const unsigned numBuckets = 4; |
| 214 |
const double overAllocFactor = 1.0; |
| 215 |
setup_views(numBuckets, overAllocFactor); |
| 216 |
stk::ngp::ExecSpace execSpace = Kokkos::DefaultExecutionSpace(); |
| 217 |
stk::mesh::impl::transpose_new_and_modified_buckets_to_device(execSpace, deviceBucketPtrData, deviceFieldData, deviceBucketSizes, deviceBucketComponentsPerEntity, deviceBucketsMarkedModified); |
| 218 |
check_field_data_values(numBuckets); |
| 219 |
} |
| 220 |
|
| 221 |
TEST_F(TestTranspose, overallocate_2_transpose_new_and_modified) |
| 222 |
{ |
| 223 |
if (stk::parallel_machine_size(MPI_COMM_WORLD) > 1) { GTEST_SKIP(); } |
| 224 |
|
| 225 |
const unsigned numBuckets = 200; |
| 226 |
const double overAllocFactor = 2.0; |
| 227 |
setup_views(numBuckets, overAllocFactor); |
| 228 |
stk::ngp::ExecSpace execSpace = Kokkos::DefaultExecutionSpace(); |
| 229 |
stk::mesh::impl::transpose_new_and_modified_buckets_to_device(execSpace, deviceBucketPtrData, deviceFieldData, deviceBucketSizes, deviceBucketComponentsPerEntity, deviceBucketsMarkedModified); |
| 230 |
check_field_data_values(numBuckets); |
| 231 |
} |
| 232 |
|
| 233 |
TEST_F(TestTranspose, no_overallocation_transpose_to) |
| 234 |
{ |
| 235 |
if (stk::parallel_machine_size(MPI_COMM_WORLD) > 1) { GTEST_SKIP(); } |
| 236 |
|
| 237 |
const unsigned numBuckets = 100; |
| 238 |
const double overAllocFactor = 1.0; |
| 239 |
setup_views(numBuckets, overAllocFactor); |
| 240 |
stk::ngp::ExecSpace execSpace = Kokkos::DefaultExecutionSpace(); |
| 241 |
stk::mesh::impl::transpose_from_pinned_and_mapped_memory(execSpace, deviceBucketPtrData, deviceFieldData, deviceBucketSizes, deviceBucketComponentsPerEntity); |
| 242 |
stk::mesh::impl::transpose_to_pinned_and_mapped_memory(execSpace, deviceBucketPtrData, deviceFieldData, deviceBucketSizes, deviceBucketComponentsPerEntity); |
| 243 |
check_host_field_data_values(numBuckets); |
| 244 |
} |
| 245 |
|
| 246 |
TEST_F(TestTranspose, overallocate_2_transpose_to) |
| 247 |
{ |
| 248 |
if (stk::parallel_machine_size(MPI_COMM_WORLD) > 1) { GTEST_SKIP(); } |
| 249 |
|
| 250 |
const unsigned numBuckets = 5; |
| 251 |
const double overAllocFactor = 2.0; |
| 252 |
setup_views(numBuckets, overAllocFactor); |
| 253 |
stk::ngp::ExecSpace execSpace = Kokkos::DefaultExecutionSpace(); |
| 254 |
stk::mesh::impl::transpose_from_pinned_and_mapped_memory(execSpace, deviceBucketPtrData, deviceFieldData, deviceBucketSizes, deviceBucketComponentsPerEntity); |
| 255 |
stk::mesh::impl::transpose_to_pinned_and_mapped_memory(execSpace, deviceBucketPtrData, deviceFieldData, deviceBucketSizes, deviceBucketComponentsPerEntity); |
| 256 |
check_host_field_data_values(numBuckets); |
| 257 |
} |
| 258 |
|
| 259 |
} |