| 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/Ngp.hpp> |
| 37 |
#include <stk_unit_test_utils/getOption.h> |
| 38 |
#include <stk_unit_test_utils/MeshFixture.hpp> |
| 39 |
#include <stk_unit_test_utils/TextMesh.hpp> |
| 40 |
#include <stk_unit_test_utils/GetMeshSpec.hpp> |
| 41 |
#include <stk_unit_test_utils/PerformanceTester.hpp> |
| 42 |
#include <stk_mesh/base/MetaData.hpp> |
| 43 |
#include <stk_mesh/base/BulkData.hpp> |
| 44 |
#include <stk_mesh/base/Bucket.hpp> |
| 45 |
#include <stk_mesh/base/Field.hpp> |
| 46 |
#include <stk_mesh/base/Entity.hpp> |
| 47 |
#include <stk_mesh/base/GetEntities.hpp> |
| 48 |
#include <stk_mesh/base/GetNgpField.hpp> |
| 49 |
#include <stk_mesh/base/GetNgpMesh.hpp> |
| 50 |
#include <stk_mesh/base/GetNgpExecutionSpace.hpp> |
| 51 |
#include <stk_mesh/base/Types.hpp> |
| 52 |
#include <stk_mesh/base/NgpForEachEntity.hpp> |
| 53 |
#include <stk_util/ngp/NgpSpaces.hpp> |
| 54 |
#include <stk_util/stk_config.h> |
| 55 |
#include <stk_util/util/StkNgpVector.hpp> |
| 56 |
#include "NgpUnitTestUtils.hpp" |
| 57 |
#include <Kokkos_Core.hpp> |
| 58 |
#include <string> |
| 59 |
#include <cstdlib> |
| 60 |
|
| 61 |
#ifndef KOKKOS_ENABLE_CUDA |
| 62 |
#define TEST_ONLY_ON_CUDA(testname) DISABLED_##testname |
| 63 |
#else |
| 64 |
#define TEST_ONLY_ON_CUDA(testname) testname |
| 65 |
#endif |
| 66 |
|
| 67 |
class NgpAsyncDeepCopyFixture : public stk::unit_test_util::MeshFixture |
| 68 |
{ |
| 69 |
public: |
| 70 |
NgpAsyncDeepCopyFixture() |
| 71 |
: m_numComponents(3), |
| 72 |
m_bucketCapacity(5), |
| 73 |
m_numBlocks(3), |
| 74 |
m_numFields(m_numBlocks), |
| 75 |
m_numStreams(3), |
| 76 |
m_multiplier(5), |
| 77 |
m_defaultLaunchBlockingEnvVarSet(false), |
| 78 |
m_defaultLaunchBlockingEnvVarValue(0) |
| 79 |
{ |
| 80 |
set_launch_blocking_env_var(); |
| 81 |
setup_empty_mesh(stk::mesh::BulkData::NO_AUTO_AURA, m_bucketCapacity, m_bucketCapacity); |
| 82 |
} |
| 83 |
|
| 84 |
~NgpAsyncDeepCopyFixture() |
| 85 |
{ |
| 86 |
revert_launch_blocking_env_var(); |
| 87 |
} |
| 88 |
|
| 89 |
std::vector<stk::mesh::ExecSpaceWrapper<>> get_execution_spaces_with_streams(unsigned numStreams) |
| 90 |
{ |
| 91 |
std::vector<stk::mesh::ExecSpaceWrapper<>> execSpaces; |
| 92 |
|
| 93 |
for(unsigned i = 0; i < numStreams; i++) { |
| 94 |
execSpaces.push_back(stk::mesh::get_execution_space_with_stream()); |
| 95 |
} |
| 96 |
|
| 97 |
return execSpaces; |
| 98 |
} |
| 99 |
|
| 100 |
void setup_multi_block_mesh_with_field_per_block() |
| 101 |
{ |
| 102 |
std::string meshDesc = stk::unit_test_util::get_many_block_mesh_desc(m_numBlocks); |
| 103 |
std::vector<double> coordinates = stk::unit_test_util::get_many_block_coordinates(m_numBlocks); |
| 104 |
|
| 105 |
setup_field_per_block(); |
| 106 |
stk::unit_test_util::setup_text_mesh( |
| 107 |
get_bulk(), stk::unit_test_util::get_full_text_mesh_desc(meshDesc, coordinates)); |
| 108 |
construct_ngp_fields(); |
| 109 |
} |
| 110 |
|
| 111 |
void setup_multi_block_mesh_with_fields_on_all_blocks() |
| 112 |
{ |
| 113 |
std::string meshDesc = stk::unit_test_util::get_many_block_mesh_desc(m_numBlocks); |
| 114 |
std::vector<double> coordinates = stk::unit_test_util::get_many_block_coordinates(m_numBlocks); |
| 115 |
|
| 116 |
setup_fields_on_all_blocks(); |
| 117 |
stk::unit_test_util::setup_text_mesh( |
| 118 |
get_bulk(), stk::unit_test_util::get_full_text_mesh_desc(meshDesc, coordinates)); |
| 119 |
construct_ngp_fields(); |
| 120 |
} |
| 121 |
|
| 122 |
void construct_ngp_fields() |
| 123 |
{ |
| 124 |
for(auto field : m_fields) { |
| 125 |
stk::mesh::get_updated_ngp_field<int>(*field); |
| 126 |
} |
| 127 |
} |
| 128 |
|
| 129 |
void setup_field_per_block() |
| 130 |
{ |
| 131 |
unsigned numStates = 1; |
| 132 |
std::vector<int> init; |
| 133 |
setup_field_component_data(init); |
| 134 |
|
| 135 |
for(unsigned i = 1; i <= m_numBlocks; i++) { |
| 136 |
std::string blockName = "block_" + std::to_string(i); |
| 137 |
std::string fieldName = "field_" + std::to_string(i); |
| 138 |
|
| 139 |
stk::mesh::Part& part = get_meta().declare_part_with_topology(blockName, stk::topology::HEX_8); |
| 140 |
get_meta().set_part_id(part, i); |
| 141 |
EXPECT_NE(&part, nullptr); |
| 142 |
|
| 143 |
stk::mesh::Field<int>& field = get_meta().declare_field<int>(stk::topology::ELEM_RANK, fieldName, numStates); |
| 144 |
m_fields.push_back(&field); |
| 145 |
stk::mesh::put_field_on_mesh(field, part, m_numComponents, init.data()); |
| 146 |
} |
| 147 |
} |
| 148 |
|
| 149 |
void setup_fields_on_all_blocks() |
| 150 |
{ |
| 151 |
unsigned numStates = 1; |
| 152 |
std::vector<int> init; |
| 153 |
setup_field_component_data(init); |
| 154 |
|
| 155 |
for(unsigned i = 1; i <= m_numBlocks; i++) { |
| 156 |
std::string blockName = "block_" + std::to_string(i); |
| 157 |
|
| 158 |
stk::mesh::Part& part = get_meta().declare_part_with_topology(blockName, stk::topology::HEX_8); |
| 159 |
get_meta().set_part_id(part, i); |
| 160 |
EXPECT_NE(&part, nullptr); |
| 161 |
|
| 162 |
for(unsigned j = 1; j <= m_numFields; j++) { |
| 163 |
std::string fieldName = "field_on_all_blocks_" + std::to_string(j); |
| 164 |
stk::mesh::Field<int>& field = get_meta().declare_field<int>(stk::topology::ELEM_RANK, fieldName, numStates); |
| 165 |
stk::mesh::put_field_on_mesh(field, part, m_numComponents, init.data()); |
| 166 |
m_fields.push_back(&field); |
| 167 |
} |
| 168 |
} |
| 169 |
} |
| 170 |
|
| 171 |
std::vector<stk::mesh::Field<int>*> get_fields() |
| 172 |
{ |
| 173 |
return m_fields; |
| 174 |
} |
| 175 |
|
| 176 |
stk::mesh::PartVector get_parts() |
| 177 |
{ |
| 178 |
auto allParts = get_meta().get_parts(); |
| 179 |
stk::mesh::PartVector elemParts; |
| 180 |
|
| 181 |
for(auto part : allParts) { |
| 182 |
if(part->primary_entity_rank() == stk::topology::ELEM_RANK && part->id() != stk::mesh::Part::INVALID_ID) { |
| 183 |
elemParts.push_back(part); |
| 184 |
} |
| 185 |
} |
| 186 |
|
| 187 |
return elemParts; |
| 188 |
} |
| 189 |
|
| 190 |
void setup_test(unsigned numBlocks, unsigned numStreams, unsigned multiplier) |
| 191 |
{ |
| 192 |
m_numBlocks = numBlocks; |
| 193 |
m_numStreams = numStreams; |
| 194 |
m_multiplier = multiplier; |
| 195 |
m_numFields = m_numBlocks; |
| 196 |
} |
| 197 |
|
| 198 |
void setup_field_data_on_host() |
| 199 |
{ |
| 200 |
for(auto field : m_fields) { |
| 201 |
set_element_field_data(*field, stk::mesh::Selector(*field), m_multiplier); |
| 202 |
} |
| 203 |
} |
| 204 |
|
| 205 |
void setup_selected_field_data_on_host(stk::mesh::Selector& selector) |
| 206 |
{ |
| 207 |
for(auto field : m_fields) { |
| 208 |
set_element_field_data(*field, selector, m_multiplier); |
| 209 |
} |
| 210 |
} |
| 211 |
|
| 212 |
void setup_field_data_on_device() |
| 213 |
{ |
| 214 |
for(auto field : m_fields) { |
| 215 |
auto& ngpMesh = stk::mesh::get_updated_ngp_mesh(get_bulk()); |
| 216 |
set_element_field_data_on_device(ngpMesh, *field, stk::mesh::Selector(*field), m_multiplier); |
| 217 |
} |
| 218 |
} |
| 219 |
|
| 220 |
void setup_selected_field_data_on_device(stk::mesh::Selector& selector) |
| 221 |
{ |
| 222 |
for(auto field : m_fields) { |
| 223 |
auto& ngpMesh = stk::mesh::get_updated_ngp_mesh(get_bulk()); |
| 224 |
set_element_field_data_on_device(ngpMesh, *field, selector, m_multiplier); |
| 225 |
} |
| 226 |
} |
| 227 |
|
| 228 |
void add_parts_to_all_blocks(stk::mesh::PartVector& addParts) |
| 229 |
{ |
| 230 |
stk::mesh::EntityVector entities; |
| 231 |
stk::mesh::get_entities(get_bulk(), stk::topology::ELEM_RANK, entities); |
| 232 |
|
| 233 |
get_bulk().batch_change_entity_parts(entities, addParts, {}); |
| 234 |
} |
| 235 |
|
| 236 |
void change_parts_on_selected_blocks(stk::mesh::PartVector& addParts, stk::mesh::PartVector& removeParts, |
| 237 |
stk::mesh::Selector& blockSelector) |
| 238 |
{ |
| 239 |
stk::mesh::EntityVector entities; |
| 240 |
stk::mesh::get_selected_entities(blockSelector, get_bulk().buckets(stk::topology::ELEM_RANK), entities); |
| 241 |
|
| 242 |
get_bulk().batch_change_entity_parts(entities, addParts, removeParts); |
| 243 |
} |
| 244 |
|
| 245 |
void sync_fields_to_host() |
| 246 |
{ |
| 247 |
for(unsigned i = 0; i < m_fields.size(); i++) { |
| 248 |
auto field = m_fields[i]; |
| 249 |
auto& ngpField = stk::mesh::get_updated_ngp_field<int>(*field); |
| 250 |
ngpField.modify_on_device(); |
| 251 |
ngpField.sync_to_host(); |
| 252 |
} |
| 253 |
} |
| 254 |
|
| 255 |
void sync_fields_to_host_async(std::vector<stk::mesh::ExecSpaceWrapper<>>& execSpaces) |
| 256 |
{ |
| 257 |
for(unsigned i = 0; i < m_fields.size(); i++) { |
| 258 |
auto field = m_fields[i]; |
| 259 |
auto& ngpField = stk::mesh::get_updated_ngp_field<int>(*field); |
| 260 |
ngpField.modify_on_device(); |
| 261 |
ngpField.sync_to_host(execSpaces[i % execSpaces.size()]); |
| 262 |
} |
| 263 |
} |
| 264 |
|
| 265 |
void sync_fields_to_host_async(std::vector<stk::mesh::ExecSpaceWrapper<>>& execSpaces, |
| 266 |
stk::mesh::Selector& selector) |
| 267 |
{ |
| 268 |
for(unsigned i = 0; i < m_fields.size(); i++) { |
| 269 |
auto field = m_fields[i]; |
| 270 |
auto& ngpField = stk::mesh::get_updated_ngp_field<int>(*field); |
| 271 |
ngpField.modify_on_device(selector); |
| 272 |
ngpField.sync_to_host(execSpaces[i % execSpaces.size()]); |
| 273 |
} |
| 274 |
} |
| 275 |
|
| 276 |
void sync_fields_to_device_async(std::vector<stk::mesh::ExecSpaceWrapper<>>& execSpaces) |
| 277 |
{ |
| 278 |
for(unsigned i = 0; i < m_fields.size(); i++) { |
| 279 |
auto field = m_fields[i]; |
| 280 |
auto& ngpField = stk::mesh::get_updated_ngp_field<int>(*field); |
| 281 |
ngpField.modify_on_host(); |
| 282 |
ngpField.sync_to_device(execSpaces[i % execSpaces.size()]); |
| 283 |
} |
| 284 |
} |
| 285 |
|
| 286 |
template<typename FieldType> |
| 287 |
void compare_device_data_to_init_data(FieldType& field) |
| 288 |
{ |
| 289 |
unsigned numComponents = m_numComponents; |
| 290 |
unsigned multiplier = m_multiplier; |
| 291 |
auto& ngpMesh = stk::mesh::get_updated_ngp_mesh(get_bulk()); |
| 292 |
auto& ngpField = stk::mesh::get_updated_ngp_field<int>(*field); |
| 293 |
|
| 294 |
stk::mesh::for_each_entity_run(ngpMesh, stk::topology::ELEM_RANK, stk::mesh::Selector(*field), |
| 295 |
KOKKOS_LAMBDA(const stk::mesh::FastMeshIndex& elem) |
| 296 |
{ |
| 297 |
auto entity = ngpMesh.get_entity(stk::topology::ELEM_RANK, elem); |
| 298 |
|
| 299 |
STK_NGP_ThrowRequire(numComponents == ngpField.get_num_components_per_entity(elem)); |
| 300 |
for(unsigned i = 0; i < numComponents; i++) { |
| 301 |
int expected = ngpMesh.identifier(entity) * multiplier + i; |
| 302 |
int fieldValue = ngpField(elem, i); |
| 303 |
NGP_EXPECT_EQ(expected, fieldValue); |
| 304 |
} |
| 305 |
}); |
| 306 |
Kokkos::fence(); |
| 307 |
} |
| 308 |
|
| 309 |
template<typename FieldType> |
| 310 |
void test_partial_copy_to_device_result(FieldType field, stk::mesh::Selector& selector) |
| 311 |
{ |
| 312 |
unsigned numComponents = m_numComponents; |
| 313 |
unsigned multiplier = m_multiplier; |
| 314 |
auto& ngpMesh = stk::mesh::get_updated_ngp_mesh(get_bulk()); |
| 315 |
auto& ngpField = stk::mesh::get_updated_ngp_field<int>(*field); |
| 316 |
|
| 317 |
stk::mesh::for_each_entity_run(ngpMesh, stk::topology::ELEM_RANK, selector, |
| 318 |
KOKKOS_LAMBDA(const stk::mesh::FastMeshIndex& elem) |
| 319 |
{ |
| 320 |
auto entity = ngpMesh.get_entity(stk::topology::ELEM_RANK, elem); |
| 321 |
|
| 322 |
for(unsigned i = 0; i < numComponents; i++) { |
| 323 |
int expected = ngpMesh.identifier(entity) * multiplier + i; |
| 324 |
int fieldValue = ngpField(elem, i); |
| 325 |
NGP_EXPECT_EQ(expected, fieldValue); |
| 326 |
} |
| 327 |
}); |
| 328 |
Kokkos::fence(); |
| 329 |
|
| 330 |
stk::NgpVector<int> ngpVector; |
| 331 |
std::vector<int> init; |
| 332 |
setup_field_component_data(init); |
| 333 |
|
| 334 |
for(auto val : init) { |
| 335 |
ngpVector.push_back(val); |
| 336 |
} |
| 337 |
ngpVector.copy_host_to_device(); |
| 338 |
|
| 339 |
stk::mesh::for_each_entity_run(ngpMesh, stk::topology::ELEM_RANK, !selector, |
| 340 |
KOKKOS_LAMBDA(const stk::mesh::FastMeshIndex& elem) |
| 341 |
{ |
| 342 |
for(unsigned i = 0; i < numComponents; i++) { |
| 343 |
int expected = ngpVector.device_get(i); |
| 344 |
int fieldValue = ngpField(elem, i); |
| 345 |
NGP_EXPECT_EQ(expected, fieldValue); |
| 346 |
} |
| 347 |
}); |
| 348 |
Kokkos::fence(); |
| 349 |
} |
| 350 |
|
| 351 |
template<typename FieldType> |
| 352 |
void compare_host_data_to_modified_data(FieldType field, unsigned scale = 1) |
| 353 |
{ |
| 354 |
stk::mesh::EntityVector elems; |
| 355 |
stk::mesh::get_selected_entities(stk::mesh::Selector(*field), get_bulk().buckets(stk::topology::ELEM_RANK), elems); |
| 356 |
unsigned multiplier = m_multiplier * scale; |
| 357 |
|
| 358 |
for(auto elem : elems) { |
| 359 |
int* data = reinterpret_cast<int*>(stk::mesh::field_data(*field, elem)); |
| 360 |
unsigned numComponents = stk::mesh::field_scalars_per_entity(*field, elem); |
| 361 |
for(unsigned j = 0; j < numComponents; j++) { |
| 362 |
unsigned expectedValue = get_bulk().identifier(elem) * multiplier + j; |
| 363 |
EXPECT_EQ((int)expectedValue, data[j]); |
| 364 |
} |
| 365 |
} |
| 366 |
} |
| 367 |
|
| 368 |
template<typename FieldType> |
| 369 |
void test_partial_copy_to_host_result(FieldType field, const stk::mesh::Selector& selector) |
| 370 |
{ |
| 371 |
stk::mesh::EntityVector elems; |
| 372 |
stk::mesh::get_entities(get_bulk(), stk::topology::ELEM_RANK, selector, elems); |
| 373 |
|
| 374 |
check_result_on_host_expect_multiplied_data(field, elems, m_multiplier); |
| 375 |
|
| 376 |
stk::mesh::Selector otherSelector = stk::mesh::Selector(*field) - selector; |
| 377 |
|
| 378 |
stk::mesh::EntityVector notSelectedElems; |
| 379 |
stk::mesh::get_entities(get_bulk(), stk::topology::ELEM_RANK, otherSelector, notSelectedElems); |
| 380 |
|
| 381 |
check_result_on_host_expect_init_data(field, notSelectedElems); |
| 382 |
} |
| 383 |
|
| 384 |
void set_element_field_data(stk::mesh::Field<int>& stkIntField, const stk::mesh::Selector& selector, unsigned multiplier) |
| 385 |
{ |
| 386 |
stk::mesh::EntityVector elements; |
| 387 |
stk::mesh::get_entities(get_bulk(), stk::topology::ELEM_RANK, selector, elements); |
| 388 |
|
| 389 |
for(stk::mesh::Entity elem : elements) { |
| 390 |
int* data = reinterpret_cast<int*>(stk::mesh::field_data(stkIntField, elem)); |
| 391 |
const unsigned numComponents = stk::mesh::field_scalars_per_entity(stkIntField, elem); |
| 392 |
for(unsigned j = 0; j < numComponents; j++) { |
| 393 |
data[j] = get_bulk().identifier(elem) * multiplier + j; |
| 394 |
} |
| 395 |
} |
| 396 |
} |
| 397 |
|
| 398 |
void set_element_field_data_on_device(stk::mesh::NgpMesh& ngpMesh, stk::mesh::Field<int>& stkIntField, |
| 399 |
const stk::mesh::Selector& selector, unsigned multiplier) |
| 400 |
{ |
| 401 |
stk::mesh::NgpField<int>& ngpField = stk::mesh::get_updated_ngp_field<int>(stkIntField); |
| 402 |
|
| 403 |
stk::mesh::for_each_entity_run(ngpMesh, stk::topology::ELEM_RANK, selector, |
| 404 |
KOKKOS_LAMBDA(const stk::mesh::FastMeshIndex& entityIndex) { |
| 405 |
const int numScalarsPerEntity = ngpField.get_num_components_per_entity(entityIndex); |
| 406 |
for (int component=0; component<numScalarsPerEntity; component++) { |
| 407 |
stk::mesh::Entity entity = ngpMesh.get_entity(stk::topology::ELEM_RANK, entityIndex); |
| 408 |
ngpField(entityIndex, component) = ngpMesh.identifier(entity) * multiplier + component; |
| 409 |
} |
| 410 |
}); |
| 411 |
} |
| 412 |
|
| 413 |
private: |
| 414 |
template<typename FieldType, typename Func> |
| 415 |
void check_result_on_host(FieldType field, stk::mesh::EntityVector elems, Func&& testValues) |
| 416 |
{ |
| 417 |
for(auto elem : elems) { |
| 418 |
int* data = reinterpret_cast<int*>(stk::mesh::field_data(*field, elem)); |
| 419 |
unsigned numComponents = stk::mesh::field_scalars_per_entity(*field, elem); |
| 420 |
for(unsigned j = 0; j < numComponents; j++) { |
| 421 |
testValues(data, elem, j); |
| 422 |
} |
| 423 |
} |
| 424 |
} |
| 425 |
|
| 426 |
template<typename FieldType> |
| 427 |
void check_result_on_host_expect_init_data(FieldType field, stk::mesh::EntityVector& elems) |
| 428 |
{ |
| 429 |
auto expectInitData = [](int* data, stk::mesh::Entity /*entity*/, unsigned component) |
| 430 |
{ |
| 431 |
int expectedValue = component; |
| 432 |
EXPECT_EQ(data[component], expectedValue); |
| 433 |
}; |
| 434 |
|
| 435 |
check_result_on_host(field, elems, expectInitData); |
| 436 |
} |
| 437 |
|
| 438 |
template<typename FieldType> |
| 439 |
void check_result_on_host_expect_multiplied_data(FieldType field, stk::mesh::EntityVector& elems, unsigned multiplier) |
| 440 |
{ |
| 441 |
auto expectInitData = [this, multiplier](int* data, stk::mesh::Entity elem, unsigned component) |
| 442 |
{ |
| 443 |
int expectedValue = get_bulk().identifier(elem) * multiplier + component; |
| 444 |
EXPECT_EQ(data[component], expectedValue); |
| 445 |
}; |
| 446 |
|
| 447 |
check_result_on_host(field, elems, expectInitData); |
| 448 |
} |
| 449 |
|
| 450 |
void setup_field_component_data(std::vector<int>& init) |
| 451 |
{ |
| 452 |
for(unsigned i = 0; i < m_numComponents; i++) { |
| 453 |
init.push_back(i); |
| 454 |
} |
| 455 |
} |
| 456 |
|
| 457 |
void revert_launch_blocking_env_var() |
| 458 |
{ |
| 459 |
if(m_defaultLaunchBlockingEnvVarSet) { |
| 460 |
setenv(m_launchBlockingEnvVar.c_str(), std::to_string(m_defaultLaunchBlockingEnvVarValue).c_str(), 1); |
| 461 |
} else { |
| 462 |
unsetenv(m_launchBlockingEnvVar.c_str()); |
| 463 |
} |
| 464 |
} |
| 465 |
|
| 466 |
void set_launch_blocking_env_var() |
| 467 |
{ |
| 468 |
char* varValue = std::getenv(m_launchBlockingEnvVar.c_str()); |
| 469 |
m_defaultLaunchBlockingEnvVarSet = (varValue != nullptr); |
| 470 |
|
| 471 |
if(m_defaultLaunchBlockingEnvVarSet) { |
| 472 |
m_defaultLaunchBlockingEnvVarValue = stk::get_env_var_as_int(m_launchBlockingEnvVar, 0); |
| 473 |
setenv(m_launchBlockingEnvVar.c_str(), "0", 1); |
| 474 |
} |
| 475 |
} |
| 476 |
|
| 477 |
unsigned m_numComponents; |
| 478 |
unsigned m_bucketCapacity; |
| 479 |
unsigned m_numBlocks; |
| 480 |
unsigned m_numFields; |
| 481 |
unsigned m_numStreams; |
| 482 |
unsigned m_multiplier; |
| 483 |
|
| 484 |
bool m_defaultLaunchBlockingEnvVarSet; |
| 485 |
int m_defaultLaunchBlockingEnvVarValue; |
| 486 |
|
| 487 |
std::vector<stk::mesh::Field<int>*> m_fields; |
| 488 |
const std::string m_launchBlockingEnvVar = "CUDA_LAUNCH_BLOCKING"; |
| 489 |
}; |
| 490 |
|
| 491 |
NGP_TEST_F(NgpAsyncDeepCopyFixture, TwoStreamsAsyncSyncToDevice) |
| 492 |
{ |
| 493 |
if (get_parallel_size() != 1) { GTEST_SKIP(); } |
| 494 |
|
| 495 |
unsigned numBlocks = 2; |
| 496 |
unsigned numStreams = 2; |
| 497 |
unsigned multiplier = 5; |
| 498 |
|
| 499 |
setup_test(numBlocks, numStreams, multiplier); |
| 500 |
setup_multi_block_mesh_with_field_per_block(); |
| 501 |
std::vector<stk::mesh::ExecSpaceWrapper<>> execSpaces = get_execution_spaces_with_streams(numStreams); |
| 502 |
|
| 503 |
setup_field_data_on_host(); |
| 504 |
|
| 505 |
sync_fields_to_device_async(execSpaces); |
| 506 |
|
| 507 |
stk::mesh::ngp_field_fence(get_meta()); |
| 508 |
|
| 509 |
for(auto field : get_fields()) { |
| 510 |
compare_device_data_to_init_data(field); |
| 511 |
} |
| 512 |
} |
| 513 |
|
| 514 |
NGP_TEST_F(NgpAsyncDeepCopyFixture, TwoStreamsAsyncSyncToHostFenceAll) |
| 515 |
{ |
| 516 |
if (get_parallel_size() != 1) GTEST_SKIP(); |
| 517 |
|
| 518 |
unsigned numBlocks = 2; |
| 519 |
unsigned numStreams = 2; |
| 520 |
unsigned multiplier = 5; |
| 521 |
|
| 522 |
setup_test(numBlocks, numStreams, multiplier); |
| 523 |
|
| 524 |
setup_multi_block_mesh_with_field_per_block(); |
| 525 |
std::vector<stk::mesh::ExecSpaceWrapper<>> execSpaces = get_execution_spaces_with_streams(numStreams); |
| 526 |
|
| 527 |
setup_field_data_on_device(); |
| 528 |
|
| 529 |
sync_fields_to_host_async(execSpaces); |
| 530 |
|
| 531 |
stk::mesh::ngp_field_fence(get_meta()); |
| 532 |
|
| 533 |
for(auto field : get_fields()) { |
| 534 |
compare_host_data_to_modified_data(field); |
| 535 |
} |
| 536 |
} |
| 537 |
|
| 538 |
NGP_TEST_F(NgpAsyncDeepCopyFixture, TwoStreamsAsyncSyncToHostFenceEach) |
| 539 |
{ |
| 540 |
if (get_parallel_size() != 1) GTEST_SKIP(); |
| 541 |
|
| 542 |
unsigned numBlocks = 2; |
| 543 |
unsigned numStreams = 2; |
| 544 |
unsigned multiplier = 5; |
| 545 |
|
| 546 |
setup_test(numBlocks, numStreams, multiplier); |
| 547 |
|
| 548 |
setup_multi_block_mesh_with_field_per_block(); |
| 549 |
std::vector<stk::mesh::ExecSpaceWrapper<>> execSpaces = get_execution_spaces_with_streams(numStreams); |
| 550 |
|
| 551 |
setup_field_data_on_device(); |
| 552 |
|
| 553 |
sync_fields_to_host_async(execSpaces); |
| 554 |
|
| 555 |
for(auto field : get_fields()) { |
| 556 |
auto& ngpField = stk::mesh::get_updated_ngp_field<int>(*field); |
| 557 |
ngpField.fence(); |
| 558 |
compare_host_data_to_modified_data(field); |
| 559 |
} |
| 560 |
} |
| 561 |
|
| 562 |
NGP_TEST_F(NgpAsyncDeepCopyFixture, TwoStreamsAsyncSync) |
| 563 |
{ |
| 564 |
if (get_parallel_size() != 1) GTEST_SKIP(); |
| 565 |
|
| 566 |
unsigned numBlocks = 3; |
| 567 |
unsigned numStreams = 3; |
| 568 |
unsigned multiplier = 5; |
| 569 |
|
| 570 |
setup_test(numBlocks, numStreams, multiplier); |
| 571 |
|
| 572 |
setup_multi_block_mesh_with_field_per_block(); |
| 573 |
std::vector<stk::mesh::ExecSpaceWrapper<>> execSpaces = get_execution_spaces_with_streams(numStreams); |
| 574 |
|
| 575 |
setup_field_data_on_host(); |
| 576 |
|
| 577 |
sync_fields_to_device_async(execSpaces); |
| 578 |
sync_fields_to_host_async(execSpaces); |
| 579 |
|
| 580 |
stk::mesh::ngp_field_fence(get_meta()); |
| 581 |
|
| 582 |
for(auto field : get_fields()) { |
| 583 |
compare_host_data_to_modified_data(field); |
| 584 |
} |
| 585 |
} |
| 586 |
|
| 587 |
NGP_TEST_F(NgpAsyncDeepCopyFixture, AsyncSyncUsingSameStreams) |
| 588 |
{ |
| 589 |
if (get_parallel_size() != 1) GTEST_SKIP(); |
| 590 |
|
| 591 |
unsigned numBlocks = 3; |
| 592 |
unsigned numStreams = 1; |
| 593 |
unsigned multiplier = 5; |
| 594 |
|
| 595 |
setup_test(numBlocks, numStreams, multiplier); |
| 596 |
|
| 597 |
setup_multi_block_mesh_with_field_per_block(); |
| 598 |
std::vector<stk::mesh::ExecSpaceWrapper<>> execSpaces = get_execution_spaces_with_streams(numStreams); |
| 599 |
|
| 600 |
setup_field_data_on_host(); |
| 601 |
|
| 602 |
sync_fields_to_device_async(execSpaces); |
| 603 |
sync_fields_to_host_async(execSpaces); |
| 604 |
|
| 605 |
stk::mesh::ngp_field_fence(get_meta()); |
| 606 |
|
| 607 |
for(auto field : get_fields()) { |
| 608 |
compare_device_data_to_init_data(field); |
| 609 |
} |
| 610 |
} |
| 611 |
|
| 612 |
NGP_TEST_F(NgpAsyncDeepCopyFixture, AsyncSyncToDeviceThenMeshMod) |
| 613 |
{ |
| 614 |
if (get_parallel_size() != 1) GTEST_SKIP(); |
| 615 |
|
| 616 |
unsigned numBlocks = 3; |
| 617 |
unsigned numStreams = 2; |
| 618 |
unsigned multiplier = 5; |
| 619 |
|
| 620 |
setup_test(numBlocks, numStreams, multiplier); |
| 621 |
|
| 622 |
setup_multi_block_mesh_with_field_per_block(); |
| 623 |
std::vector<stk::mesh::ExecSpaceWrapper<>> execSpaces = get_execution_spaces_with_streams(numStreams); |
| 624 |
|
| 625 |
setup_field_data_on_host(); |
| 626 |
|
| 627 |
sync_fields_to_device_async(execSpaces); |
| 628 |
|
| 629 |
stk::mesh::ngp_field_fence(get_meta()); |
| 630 |
|
| 631 |
stk::mesh::Part& newPart = get_meta().declare_part("testPart", stk::topology::ELEMENT_RANK); |
| 632 |
stk::mesh::PartVector addParts(1, &newPart); |
| 633 |
|
| 634 |
add_parts_to_all_blocks(addParts); |
| 635 |
|
| 636 |
for(auto field : get_fields()) { |
| 637 |
compare_device_data_to_init_data(field); |
| 638 |
} |
| 639 |
} |
| 640 |
|
| 641 |
NGP_TEST_F(NgpAsyncDeepCopyFixture, AsyncCopyFollowedBySyncCopy) |
| 642 |
{ |
| 643 |
if (get_parallel_size() != 1) GTEST_SKIP(); |
| 644 |
|
| 645 |
unsigned numBlocks = 3; |
| 646 |
unsigned numStreams = 3; |
| 647 |
unsigned multiplier = 5; |
| 648 |
|
| 649 |
setup_test(numBlocks, numStreams, multiplier); |
| 650 |
|
| 651 |
setup_multi_block_mesh_with_field_per_block(); |
| 652 |
std::vector<stk::mesh::ExecSpaceWrapper<>> execSpaces = get_execution_spaces_with_streams(numStreams); |
| 653 |
|
| 654 |
setup_field_data_on_host(); |
| 655 |
|
| 656 |
sync_fields_to_device_async(execSpaces); |
| 657 |
|
| 658 |
stk::mesh::ngp_field_fence(get_meta()); |
| 659 |
|
| 660 |
sync_fields_to_host(); |
| 661 |
|
| 662 |
for(auto field : get_fields()) { |
| 663 |
compare_host_data_to_modified_data(field); |
| 664 |
} |
| 665 |
} |
| 666 |
|
| 667 |
NGP_TEST_F(NgpAsyncDeepCopyFixture, AsyncCopyFollowedByGetUpdatedNgpField) |
| 668 |
{ |
| 669 |
if (get_parallel_size() != 1) GTEST_SKIP(); |
| 670 |
|
| 671 |
unsigned numBlocks = 3; |
| 672 |
unsigned numStreams = 3; |
| 673 |
unsigned multiplier = 5; |
| 674 |
|
| 675 |
setup_test(numBlocks, numStreams, multiplier); |
| 676 |
|
| 677 |
setup_multi_block_mesh_with_field_per_block(); |
| 678 |
std::vector<stk::mesh::ExecSpaceWrapper<>> execSpaces = get_execution_spaces_with_streams(numStreams); |
| 679 |
|
| 680 |
setup_field_data_on_host(); |
| 681 |
|
| 682 |
sync_fields_to_device_async(execSpaces); |
| 683 |
|
| 684 |
for(auto field : get_fields()) { |
| 685 |
stk::mesh::get_updated_ngp_field<int>(*field); |
| 686 |
} |
| 687 |
sync_fields_to_host(); |
| 688 |
|
| 689 |
for(auto field : get_fields()) { |
| 690 |
compare_host_data_to_modified_data(field); |
| 691 |
} |
| 692 |
} |
| 693 |
|
| 694 |
NGP_TEST_F(NgpAsyncDeepCopyFixture, AsyncSyncToHostFollowedByMeshMod) |
| 695 |
{ |
| 696 |
if (get_parallel_size() != 1) GTEST_SKIP(); |
| 697 |
|
| 698 |
unsigned numBlocks = 3; |
| 699 |
unsigned numStreams = 3; |
| 700 |
unsigned multiplier = 5; |
| 701 |
|
| 702 |
setup_test(numBlocks, numStreams, multiplier); |
| 703 |
|
| 704 |
setup_multi_block_mesh_with_field_per_block(); |
| 705 |
std::vector<stk::mesh::ExecSpaceWrapper<>> execSpaces = get_execution_spaces_with_streams(numStreams); |
| 706 |
|
| 707 |
setup_field_data_on_device(); |
| 708 |
|
| 709 |
sync_fields_to_host_async(execSpaces); |
| 710 |
|
| 711 |
stk::mesh::ngp_field_fence(get_meta()); |
| 712 |
|
| 713 |
stk::mesh::Part& newPart = get_meta().declare_part("testPart", stk::topology::ELEMENT_RANK); |
| 714 |
stk::mesh::PartVector addParts(1, &newPart); |
| 715 |
|
| 716 |
add_parts_to_all_blocks(addParts); |
| 717 |
|
| 718 |
for(auto field : get_fields()) { |
| 719 |
compare_host_data_to_modified_data(field); |
| 720 |
} |
| 721 |
} |
| 722 |
|
| 723 |
NGP_TEST_F(NgpAsyncDeepCopyFixture, AsyncSyncToHostFollowedByDataModOnHostThenGetUpdatedNgpField) |
| 724 |
{ |
| 725 |
if (get_parallel_size() != 1) GTEST_SKIP(); |
| 726 |
|
| 727 |
unsigned numBlocks = 1; |
| 728 |
unsigned numStreams = 1; |
| 729 |
unsigned multiplier = 5; |
| 730 |
|
| 731 |
setup_test(numBlocks, numStreams, multiplier); |
| 732 |
|
| 733 |
setup_multi_block_mesh_with_field_per_block(); |
| 734 |
std::vector<stk::mesh::ExecSpaceWrapper<>> execSpaces = get_execution_spaces_with_streams(numStreams); |
| 735 |
|
| 736 |
setup_field_data_on_device(); |
| 737 |
|
| 738 |
sync_fields_to_host_async(execSpaces); |
| 739 |
|
| 740 |
stk::mesh::ngp_field_fence(get_meta()); |
| 741 |
|
| 742 |
unsigned scale = 3; |
| 743 |
|
| 744 |
for(auto field : get_fields()) { |
| 745 |
set_element_field_data(*field, stk::mesh::Selector(*field), multiplier*scale); |
| 746 |
} |
| 747 |
|
| 748 |
for(auto field : get_fields()) { |
| 749 |
compare_host_data_to_modified_data(field, scale); |
| 750 |
} |
| 751 |
|
| 752 |
stk::mesh::ngp_field_fence(get_meta()); |
| 753 |
|
| 754 |
for(auto field : get_fields()) { |
| 755 |
compare_host_data_to_modified_data(field, scale); |
| 756 |
} |
| 757 |
} |
| 758 |
|
| 759 |
NGP_TEST_F(NgpAsyncDeepCopyFixture, AsyncGetUpdatedNgpField) |
| 760 |
{ |
| 761 |
if (get_parallel_size() != 1) GTEST_SKIP(); |
| 762 |
|
| 763 |
unsigned numBlocks = 2; |
| 764 |
unsigned numStreams = 2; |
| 765 |
unsigned multiplier = 5; |
| 766 |
|
| 767 |
setup_test(numBlocks, numStreams, multiplier); |
| 768 |
setup_multi_block_mesh_with_field_per_block(); |
| 769 |
std::vector<stk::mesh::ExecSpaceWrapper<>> execSpaces = get_execution_spaces_with_streams(numStreams); |
| 770 |
|
| 771 |
setup_field_data_on_host(); |
| 772 |
|
| 773 |
sync_fields_to_device_async(execSpaces); |
| 774 |
|
| 775 |
for(unsigned i = 0; i < get_fields().size(); i++) { |
| 776 |
auto field = get_fields()[i]; |
| 777 |
auto execSpace = execSpaces[i % execSpaces.size()]; |
| 778 |
auto& ngpField = stk::mesh::get_updated_ngp_field_async<int>(*field, execSpace); |
| 779 |
ngpField.modify_on_host(); |
| 780 |
ngpField.sync_to_device(execSpace); |
| 781 |
} |
| 782 |
|
| 783 |
stk::mesh::ngp_field_fence(get_meta()); |
| 784 |
|
| 785 |
for(auto field : get_fields()) { |
| 786 |
compare_device_data_to_init_data(field); |
| 787 |
} |
| 788 |
} |