| 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 |
#ifndef NGPDEBUGFIELDSYNCFIXTURES_HPP |
| 36 |
#define NGPDEBUGFIELDSYNCFIXTURES_HPP |
| 37 |
|
| 38 |
#include <gtest/gtest.h> |
| 39 |
#include <stk_mesh/base/NgpFieldSyncDebugger.hpp> |
| 40 |
#include <stk_unit_test_utils/MeshFixture.hpp> |
| 41 |
#include <stk_mesh/base/GetNgpField.hpp> |
| 42 |
#include <stk_mesh/base/FEMHelpers.hpp> |
| 43 |
#include <stk_mesh/base/Comm.hpp> |
| 44 |
#include <string> |
| 45 |
|
| 46 |
template <typename T, typename NgpMemSpace> |
| 47 |
using NgpDebugger = stk::mesh::NgpFieldSyncDebugger<T, NgpMemSpace>; |
| 48 |
|
| 49 |
template <typename T, typename NgpMemSpace = stk::mesh::NgpMeshDefaultMemSpace> |
| 50 |
using StkDebugger = typename NgpDebugger<T, NgpMemSpace>::StkFieldSyncDebuggerType; |
| 51 |
|
| 52 |
void extract_warning(std::string & stdoutString, int numExpectedOccurrences, const std::string & warningString); |
| 53 |
|
| 54 |
void check_no_warnings(const std::string & stdoutString); |
| 55 |
|
| 56 |
void check_contains_file_name(const std::string & stdoutString, const std::string & fileName); |
| 57 |
|
| 58 |
void check_contains_a_line_number(const std::string & stdoutString); |
| 59 |
|
| 60 |
template <typename T> |
| 61 |
STK_INLINE_FUNCTION |
| 62 |
void access_for_memory_checking_tool(T valuePtr, unsigned numValues = 1) |
| 63 |
{ |
| 64 |
bool temp = true; |
| 65 |
for (unsigned i = 0; i < numValues; ++i) { |
| 66 |
temp &= (valuePtr[i] == 0); |
| 67 |
} |
| 68 |
} |
| 69 |
|
| 70 |
struct EntityIdAddRemovePart { |
| 71 |
stk::mesh::EntityId id; |
| 72 |
std::string addPart; |
| 73 |
std::string removePart; |
| 74 |
}; |
| 75 |
|
| 76 |
class NgpDebugFieldSyncFixture : public stk::unit_test_util::MeshFixture |
| 77 |
{ |
| 78 |
public: |
| 79 |
template <typename T> |
| 80 |
void declare_scalar_field(const std::string & fieldName, |
| 81 |
const std::vector<std::string> & partsForField, |
| 82 |
unsigned numStates = 1) |
| 83 |
{ |
| 84 |
stk::mesh::Selector fieldParts = create_parts(partsForField); |
| 85 |
create_scalar_field<T>(fieldName, stk::topology::ELEM_RANK, fieldParts, numStates); |
| 86 |
} |
| 87 |
|
| 88 |
template <typename T> |
| 89 |
void declare_vector_field(const std::string & fieldName, |
| 90 |
unsigned numComponents, |
| 91 |
const std::vector<std::string> & partsForField) |
| 92 |
{ |
| 93 |
stk::mesh::Selector fieldParts = create_parts(partsForField); |
| 94 |
create_vector_field<T>(fieldName, stk::topology::ELEM_RANK, numComponents, fieldParts); |
| 95 |
} |
| 96 |
|
| 97 |
stk::mesh::Selector |
| 98 |
create_parts(const std::vector<std::string> & partNames) |
| 99 |
{ |
| 100 |
stk::mesh::Selector allParts; |
| 101 |
for (const std::string & partName : partNames) { |
| 102 |
allParts |= get_meta().declare_part_with_topology(partName, stk::topology::HEX_8); |
| 103 |
} |
| 104 |
return allParts; |
| 105 |
} |
| 106 |
|
| 107 |
template <typename T> |
| 108 |
stk::mesh::Field<T> & create_scalar_field(const std::string & name, |
| 109 |
stk::topology::rank_t rank, |
| 110 |
stk::mesh::Selector & fieldParts, |
| 111 |
unsigned numStates = 1) |
| 112 |
{ |
| 113 |
get_meta().enable_field_sync_debugger(); |
| 114 |
const T init = 1; |
| 115 |
stk::mesh::Field<T> & field = get_meta().declare_field<T>(rank, name, numStates); |
| 116 |
stk::mesh::put_field_on_mesh(field, fieldParts, &init); |
| 117 |
return field; |
| 118 |
} |
| 119 |
|
| 120 |
template <typename T> |
| 121 |
stk::mesh::Field<T> & create_vector_field(const std::string & name, |
| 122 |
stk::topology::rank_t rank, |
| 123 |
unsigned numComponents, |
| 124 |
stk::mesh::Selector & fieldParts) |
| 125 |
{ |
| 126 |
get_meta().enable_field_sync_debugger(); |
| 127 |
unsigned numStates = 1; |
| 128 |
const std::vector<T> init(numComponents, 1); |
| 129 |
stk::mesh::Field<T> & field = get_meta().declare_field<T>(rank, name, numStates); |
| 130 |
stk::mesh::put_field_on_mesh(field, fieldParts, numComponents, init.data()); |
| 131 |
return field; |
| 132 |
} |
| 133 |
|
| 134 |
struct PartConfiguration { |
| 135 |
std::string partName; |
| 136 |
unsigned numElements; |
| 137 |
}; |
| 138 |
|
| 139 |
void build_mesh(const std::vector<PartConfiguration> & partConfiguration) |
| 140 |
{ |
| 141 |
std::vector<std::string> allMeshPartNames; |
| 142 |
unsigned numElems = 0; |
| 143 |
for (const auto & config : partConfiguration) { |
| 144 |
allMeshPartNames.push_back(config.partName); |
| 145 |
numElems += config.numElements; |
| 146 |
} |
| 147 |
|
| 148 |
create_parts(allMeshPartNames); |
| 149 |
stk::io::fill_mesh("generated:1x1x" + std::to_string(numElems), get_bulk()); |
| 150 |
|
| 151 |
set_initial_part_membership(partConfiguration); |
| 152 |
} |
| 153 |
|
| 154 |
void set_initial_part_membership(const std::vector<PartConfiguration> & partConfiguration) |
| 155 |
{ |
| 156 |
get_bulk().modification_begin(); |
| 157 |
stk::mesh::EntityId elemId = 0; |
| 158 |
for (const auto & config : partConfiguration) { |
| 159 |
stk::mesh::PartVector addParts {get_meta().get_part(config.partName)}; |
| 160 |
stk::mesh::EntityVector elemsToChange; |
| 161 |
for (unsigned elemNum = 0; elemNum < config.numElements; ++elemNum) { |
| 162 |
stk::mesh::Entity element = get_bulk().get_entity(stk::topology::ELEM_RANK, ++elemId); |
| 163 |
STK_ThrowRequireMsg(get_bulk().is_valid(element), "Invalid element in fixture!"); |
| 164 |
elemsToChange.push_back(element); |
| 165 |
} |
| 166 |
get_bulk().change_entity_parts(elemsToChange, addParts); |
| 167 |
} |
| 168 |
get_bulk().modification_end(); |
| 169 |
} |
| 170 |
|
| 171 |
template <typename T> |
| 172 |
stk::mesh::Field<T> & initialized_field(const std::string & fieldName) |
| 173 |
{ |
| 174 |
stk::mesh::Field<T> & stkField = *(get_meta().get_field<T>(stk::topology::ELEM_RANK, fieldName)); |
| 175 |
fill_initial_field<T>(stkField); |
| 176 |
initialize_ngp_field<T>(stkField); |
| 177 |
return stkField; |
| 178 |
} |
| 179 |
|
| 180 |
template <typename T> |
| 181 |
stk::mesh::Field<T> & initialized_vector_field(const std::string & fieldName) |
| 182 |
{ |
| 183 |
stk::mesh::Field<T> & stkField = *(get_meta().get_field<T>(stk::topology::ELEM_RANK, fieldName)); |
| 184 |
fill_initial_field<T>(stkField); |
| 185 |
initialize_ngp_field<T>(stkField); |
| 186 |
return stkField; |
| 187 |
} |
| 188 |
|
| 189 |
template <typename T> |
| 190 |
void fill_initial_field(stk::mesh::FieldBase & stkField) |
| 191 |
{ |
| 192 |
const stk::mesh::BucketVector& buckets = get_bulk().get_buckets(stkField.entity_rank(), stkField); |
| 193 |
for (stk::mesh::Bucket * bucket : buckets) { |
| 194 |
for (const stk::mesh::Entity & entity : *bucket) { |
| 195 |
const stk::mesh::EntityId id = get_bulk().identifier(entity); |
| 196 |
T * fieldData = static_cast<T*>(stk::mesh::field_data<stk::mesh::FieldBase, stk::mesh::EmptyStkFieldSyncDebugger>(stkField, entity)); |
| 197 |
const unsigned numComponents = stk::mesh::field_scalars_per_entity(stkField, *bucket); |
| 198 |
for (unsigned component = 0; component < numComponents; ++component) { |
| 199 |
fieldData[component] = 10*id + component; |
| 200 |
} |
| 201 |
} |
| 202 |
} |
| 203 |
} |
| 204 |
|
| 205 |
template <typename T> |
| 206 |
void initialize_ngp_field(stk::mesh::Field<T> & stkField) |
| 207 |
{ |
| 208 |
stk::mesh::get_updated_ngp_field<T, stk::mesh::NgpMeshDefaultMemSpace, NgpDebugger>(stkField); |
| 209 |
} |
| 210 |
|
| 211 |
template <typename T> |
| 212 |
void initialize_ngp_field(stk::mesh::FieldBase & stkField) |
| 213 |
{ |
| 214 |
stk::mesh::get_updated_ngp_field<T, stk::mesh::NgpMeshDefaultMemSpace, NgpDebugger>(stkField); |
| 215 |
} |
| 216 |
|
| 217 |
template <typename T, typename V = void> |
| 218 |
void create_element(const std::vector<std::pair<stk::mesh::EntityId, std::string>> & elemParts, |
| 219 |
stk::mesh::FieldBase& stkField) |
| 220 |
{ |
| 221 |
std::vector<size_t> counts; |
| 222 |
stk::mesh::comm_mesh_counts(get_bulk(), counts); |
| 223 |
|
| 224 |
get_bulk().modification_begin(); |
| 225 |
for (const auto & elemPart : elemParts) { |
| 226 |
stk::mesh::PartVector parts {get_meta().get_part(elemPart.second), get_meta().get_part("block_1")}; |
| 227 |
stk::mesh::EntityIdVector nodeIds; |
| 228 |
stk::mesh::EntityId nodeId = counts[stk::topology::ELEM_RANK] * 4 + 1; |
| 229 |
for (unsigned i = 0; i < 8; ++i) { |
| 230 |
nodeIds.push_back(nodeId++); |
| 231 |
} |
| 232 |
|
| 233 |
stk::mesh::declare_element(get_bulk(), parts, elemPart.first, nodeIds); |
| 234 |
} |
| 235 |
get_bulk().modification_end(); |
| 236 |
|
| 237 |
fill_initial_field<T>(stkField); |
| 238 |
} |
| 239 |
|
| 240 |
template <typename T> |
| 241 |
void create_element(const std::vector<std::pair<stk::mesh::EntityId, std::string>> & elemParts, |
| 242 |
stk::mesh::Field<T> & stkField) |
| 243 |
{ |
| 244 |
create_element<T, void>(elemParts, stkField); |
| 245 |
} |
| 246 |
|
| 247 |
void delete_element(const std::vector<stk::mesh::EntityId> & elemIds) |
| 248 |
{ |
| 249 |
get_bulk().modification_begin(); |
| 250 |
for (const stk::mesh::EntityId & elemId : elemIds) { |
| 251 |
get_bulk().destroy_entity(get_bulk().get_entity(stk::topology::ELEM_RANK, elemId)); |
| 252 |
} |
| 253 |
get_bulk().modification_end(); |
| 254 |
} |
| 255 |
|
| 256 |
void change_element_parts(const std::vector<EntityIdAddRemovePart>& elemAddRemoveParts) |
| 257 |
{ |
| 258 |
for (const auto & elemAddRemovePart : elemAddRemoveParts) { |
| 259 |
stk::mesh::EntityVector elemsToChange {get_bulk().get_entity(stk::topology::ELEM_RANK, elemAddRemovePart.id)}; |
| 260 |
stk::mesh::PartVector addParts {get_meta().get_part(elemAddRemovePart.addPart)}; |
| 261 |
stk::mesh::PartVector removeParts {get_meta().get_part(elemAddRemovePart.removePart)}; |
| 262 |
get_bulk().change_entity_parts(elemsToChange, addParts, removeParts); |
| 263 |
} |
| 264 |
} |
| 265 |
|
| 266 |
void modify_element_part_membership(const std::vector<EntityIdAddRemovePart>& elemAddRemoveParts) |
| 267 |
{ |
| 268 |
get_bulk().modification_begin(); |
| 269 |
change_element_parts(elemAddRemoveParts); |
| 270 |
get_bulk().modification_end(); |
| 271 |
} |
| 272 |
|
| 273 |
void batch_modify_element_part_membership(const std::vector<EntityIdAddRemovePart>& elemAddRemoveParts) |
| 274 |
{ |
| 275 |
stk::mesh::EntityVector elemsToChange; |
| 276 |
std::vector<stk::mesh::PartVector> elemsAddParts; |
| 277 |
std::vector<stk::mesh::PartVector> elemsRmParts; |
| 278 |
|
| 279 |
for (const auto & elemAddRemovePart : elemAddRemoveParts) { |
| 280 |
elemsToChange.push_back(get_bulk().get_entity(stk::topology::ELEM_RANK, elemAddRemovePart.id)); |
| 281 |
stk::mesh::PartVector addParts {get_meta().get_part(elemAddRemovePart.addPart)}; |
| 282 |
elemsAddParts.push_back(addParts); |
| 283 |
stk::mesh::PartVector removeParts {get_meta().get_part(elemAddRemovePart.removePart)}; |
| 284 |
elemsRmParts.push_back(removeParts); |
| 285 |
} |
| 286 |
|
| 287 |
get_bulk().batch_change_entity_parts(elemsToChange, elemsAddParts, elemsRmParts); |
| 288 |
} |
| 289 |
|
| 290 |
template <typename T> |
| 291 |
void modify_element_part_membership_with_scalar_field_write_using_entity( |
| 292 |
const std::vector<EntityIdAddRemovePart> & elemAddRemoveParts, |
| 293 |
stk::mesh::Field<T> & stkField, T value) |
| 294 |
{ |
| 295 |
get_bulk().modification_begin(); |
| 296 |
|
| 297 |
write_scalar_field_on_host_using_entity(stkField, value); |
| 298 |
|
| 299 |
change_element_parts(elemAddRemoveParts); |
| 300 |
|
| 301 |
get_bulk().modification_end(); |
| 302 |
} |
| 303 |
|
| 304 |
template <typename T> |
| 305 |
void write_scalar_field_on_host_using_entity(stk::mesh::Field<T> & stkField, T value) |
| 306 |
{ |
| 307 |
write_scalar_field_on_host_using_entity(stkField, stkField, value); |
| 308 |
} |
| 309 |
|
| 310 |
template <typename T> |
| 311 |
void write_vector_field_on_host_using_entity(stk::mesh::FieldBase & stkField, const stk::mesh::Selector& selector, T value) |
| 312 |
{ |
| 313 |
const stk::mesh::BucketVector& buckets = get_bulk().get_buckets(stkField.entity_rank(), selector); |
| 314 |
for (stk::mesh::Bucket * bucket : buckets) { |
| 315 |
unsigned numComponents = stk::mesh::field_scalars_per_entity(stkField, *bucket); |
| 316 |
for (const stk::mesh::Entity & entity : *bucket) { |
| 317 |
T * fieldData = static_cast<T*>(stk::mesh::field_data<stk::mesh::FieldBase, StkDebugger<T>>(stkField, entity)); |
| 318 |
for (unsigned d=0; d<numComponents; ++d) { |
| 319 |
fieldData[d] = value; |
| 320 |
} |
| 321 |
} |
| 322 |
} |
| 323 |
} |
| 324 |
|
| 325 |
template <typename T> |
| 326 |
void write_vector_field_on_host_using_entity(stk::mesh::FieldBase & stkField, T value) |
| 327 |
{ |
| 328 |
write_vector_field_on_host_using_entity(stkField, stkField, value); |
| 329 |
} |
| 330 |
|
| 331 |
template <typename T> |
| 332 |
void write_scalar_field_on_host_using_entity(stk::mesh::Field<T> & stkField, const stk::mesh::Selector& selector, T value) |
| 333 |
{ |
| 334 |
write_vector_field_on_host_using_entity(stkField, selector, value); |
| 335 |
} |
| 336 |
|
| 337 |
template <typename T> |
| 338 |
void write_scalar_field_on_host_using_bucket(stk::mesh::Field<T> & stkField, T value) |
| 339 |
{ |
| 340 |
const stk::mesh::BucketVector& buckets = get_bulk().get_buckets(stkField.entity_rank(), stkField); |
| 341 |
for (stk::mesh::Bucket * bucket : buckets) { |
| 342 |
T * fieldData = stk::mesh::field_data<stk::mesh::Field<T>, StkDebugger<T>>(stkField, *bucket); |
| 343 |
for(size_t iEntity = 0; iEntity < bucket->size(); ++iEntity) { |
| 344 |
fieldData[iEntity] = value; |
| 345 |
} |
| 346 |
} |
| 347 |
} |
| 348 |
|
| 349 |
template <typename T> |
| 350 |
void write_scalar_field_on_host_using_bucket_id(stk::mesh::Field<T> & stkField, T value) |
| 351 |
{ |
| 352 |
const stk::mesh::BucketVector& buckets = get_bulk().get_buckets(stkField.entity_rank(), stkField); |
| 353 |
for (stk::mesh::Bucket * bucket : buckets) { |
| 354 |
const unsigned bucketId = bucket->bucket_id(); |
| 355 |
T * fieldData = stk::mesh::field_data<stk::mesh::Field<T>, StkDebugger<T>>(stkField, bucketId); |
| 356 |
for(size_t iEntity = 0; iEntity < bucket->size(); ++iEntity) { |
| 357 |
fieldData[iEntity] = value; |
| 358 |
} |
| 359 |
} |
| 360 |
} |
| 361 |
|
| 362 |
template <typename T> |
| 363 |
stk::mesh::EntityVector write_scalar_field_on_host_using_bucket_id_and_ordinal(stk::mesh::Field<T> & stkField, T value) |
| 364 |
{ |
| 365 |
stk::mesh::EntityVector entities; |
| 366 |
const stk::mesh::BucketVector& buckets = get_bulk().get_buckets(stkField.entity_rank(), stkField); |
| 367 |
for (stk::mesh::Bucket * bucket : buckets) { |
| 368 |
for (const stk::mesh::Entity & entity : *bucket) { |
| 369 |
entities.push_back(entity); |
| 370 |
const stk::mesh::MeshIndex & meshIndex = get_bulk().mesh_index(entity); |
| 371 |
const unsigned bucketId = meshIndex.bucket->bucket_id(); |
| 372 |
const stk::mesh::Bucket::size_type bucketOrd = meshIndex.bucket_ordinal; |
| 373 |
T * fieldData = stk::mesh::field_data<stk::mesh::Field<T>, StkDebugger<T>>(stkField, bucketId, bucketOrd); |
| 374 |
fieldData[0] = value; |
| 375 |
} |
| 376 |
} |
| 377 |
return entities; |
| 378 |
} |
| 379 |
|
| 380 |
template <typename T> |
| 381 |
void write_scalar_field_on_host_using_bucket_id_and_ordinal_and_size(stk::mesh::Field<T> & stkField, T value) |
| 382 |
{ |
| 383 |
const stk::mesh::BucketVector& buckets = get_bulk().get_buckets(stkField.entity_rank(), stkField); |
| 384 |
for (stk::mesh::Bucket * bucket : buckets) { |
| 385 |
for (const stk::mesh::Entity & entity : *bucket) { |
| 386 |
const stk::mesh::MeshIndex & meshIndex = get_bulk().mesh_index(entity); |
| 387 |
const unsigned bucketId = meshIndex.bucket->bucket_id(); |
| 388 |
const stk::mesh::Bucket::size_type bucketOrd = meshIndex.bucket_ordinal; |
| 389 |
const unsigned numBytesPerEntity = stk::mesh::field_bytes_per_entity(stkField, *bucket); |
| 390 |
T * fieldData = stk::mesh::field_data<stk::mesh::Field<T>, StkDebugger<T>>(stkField, bucketId, bucketOrd, numBytesPerEntity); |
| 391 |
fieldData[0] = value; |
| 392 |
} |
| 393 |
} |
| 394 |
} |
| 395 |
|
| 396 |
template <typename T> |
| 397 |
void write_scalar_field_on_device(stk::mesh::Field<T> & stkField, const stk::mesh::Selector& selector, T value) |
| 398 |
{ |
| 399 |
const int component = 0; |
| 400 |
stk::mesh::NgpMesh & ngpMesh = stk::mesh::get_updated_ngp_mesh(get_bulk()); |
| 401 |
stk::mesh::NgpField<T, stk::mesh::NgpMeshDefaultMemSpace, NgpDebugger> & ngpField = stk::mesh::get_updated_ngp_field<T, stk::mesh::NgpMeshDefaultMemSpace, NgpDebugger>(stkField); |
| 402 |
ngpField.sync_to_device(); |
| 403 |
|
| 404 |
stk::mesh::for_each_entity_run(ngpMesh, stk::topology::ELEM_RANK, selector, |
| 405 |
KOKKOS_LAMBDA(const stk::mesh::FastMeshIndex& entityIndex) { |
| 406 |
ngpField(entityIndex, component) = value; |
| 407 |
}); |
| 408 |
} |
| 409 |
|
| 410 |
template <typename T> |
| 411 |
void write_scalar_field_on_device(stk::mesh::Field<T> & stkField, T value) |
| 412 |
{ |
| 413 |
write_scalar_field_on_device(stkField, stkField, value); |
| 414 |
} |
| 415 |
|
| 416 |
template <typename T> |
| 417 |
void write_vector_field_on_device(stk::mesh::FieldBase & stkField, const stk::mesh::Selector& selector, T value) |
| 418 |
{ |
| 419 |
stk::mesh::NgpMesh & ngpMesh = stk::mesh::get_updated_ngp_mesh(get_bulk()); |
| 420 |
stk::mesh::NgpField<T, stk::mesh::NgpMeshDefaultMemSpace, NgpDebugger> & ngpField = stk::mesh::get_updated_ngp_field<T, stk::mesh::NgpMeshDefaultMemSpace, NgpDebugger>(stkField); |
| 421 |
ngpField.sync_to_device(); |
| 422 |
|
| 423 |
stk::mesh::for_each_entity_run(ngpMesh, stk::topology::ELEM_RANK, selector, |
| 424 |
KOKKOS_LAMBDA(const stk::mesh::FastMeshIndex& entityIndex) { |
| 425 |
unsigned numComponents = ngpField.get_num_components_per_entity(entityIndex); |
| 426 |
for (unsigned component=0; component<numComponents; component++) { |
| 427 |
ngpField(entityIndex, component) = value; |
| 428 |
} |
| 429 |
}); |
| 430 |
} |
| 431 |
|
| 432 |
template <typename T> |
| 433 |
void write_vector_field_on_device(stk::mesh::FieldBase & stkField, T value) |
| 434 |
{ |
| 435 |
write_vector_field_on_device(stkField, stkField, value); |
| 436 |
} |
| 437 |
|
| 438 |
template <typename T> |
| 439 |
void device_field_set_all(stk::mesh::Field<T> & stkField, T value) |
| 440 |
{ |
| 441 |
stk::mesh::NgpMesh & ngpMesh = stk::mesh::get_updated_ngp_mesh(get_bulk()); |
| 442 |
stk::mesh::NgpField<T, stk::mesh::NgpMeshDefaultMemSpace, NgpDebugger> & ngpField = stk::mesh::get_updated_ngp_field<T, stk::mesh::NgpMeshDefaultMemSpace, NgpDebugger>(stkField); |
| 443 |
|
| 444 |
ngpField.set_all(ngpMesh, value); |
| 445 |
} |
| 446 |
|
| 447 |
template <typename T> |
| 448 |
void read_field_on_host_using_entity(stk::mesh::FieldBase & stkField, const stk::mesh::Selector& selector) |
| 449 |
{ |
| 450 |
const stk::mesh::BucketVector& buckets = get_bulk().get_buckets(stkField.entity_rank(), selector); |
| 451 |
for (stk::mesh::Bucket * bucket : buckets) { |
| 452 |
const unsigned numComponents = stk::mesh::field_scalars_per_entity(stkField, *bucket); |
| 453 |
for (const stk::mesh::Entity & entity : *bucket) { |
| 454 |
const T * fieldData = reinterpret_cast<const T*>(stk::mesh::field_data<stk::mesh::FieldBase, StkDebugger<T>>(stkField, entity)); |
| 455 |
access_for_memory_checking_tool(fieldData, numComponents); |
| 456 |
} |
| 457 |
} |
| 458 |
} |
| 459 |
|
| 460 |
template <typename T> |
| 461 |
void read_scalar_field_on_host_using_entity(stk::mesh::Field<T> & stkField, const stk::mesh::Selector& selector) |
| 462 |
{ |
| 463 |
read_field_on_host_using_entity<T>(stkField, selector); |
| 464 |
} |
| 465 |
|
| 466 |
template <typename T> |
| 467 |
void read_scalar_field_on_host_using_entity(stk::mesh::Field<T> & stkField) |
| 468 |
{ |
| 469 |
read_scalar_field_on_host_using_entity(stkField, stkField); |
| 470 |
} |
| 471 |
|
| 472 |
template <typename T> |
| 473 |
void read_vector_field_on_host_using_entity(stk::mesh::FieldBase & stkField, const stk::mesh::Selector& selector) |
| 474 |
{ |
| 475 |
read_field_on_host_using_entity<T>(stkField, selector); |
| 476 |
} |
| 477 |
|
| 478 |
template <typename T> |
| 479 |
void read_vector_field_on_host_using_entity(stk::mesh::FieldBase & stkField) |
| 480 |
{ |
| 481 |
read_vector_field_on_host_using_entity<T>(stkField, stkField); |
| 482 |
} |
| 483 |
|
| 484 |
template <typename T> |
| 485 |
void read_scalar_field_on_host_using_bucket(stk::mesh::Field<T> & stkField) |
| 486 |
{ |
| 487 |
const stk::mesh::BucketVector& buckets = get_bulk().get_buckets(stkField.entity_rank(), stkField); |
| 488 |
for (stk::mesh::Bucket * bucket : buckets) { |
| 489 |
const T * fieldData = stk::mesh::field_data<stk::mesh::Field<T>, StkDebugger<T>>(stkField, *bucket); |
| 490 |
access_for_memory_checking_tool(fieldData, bucket->size()); |
| 491 |
} |
| 492 |
} |
| 493 |
|
| 494 |
template <typename T> |
| 495 |
void read_field_on_device(stk::mesh::FieldBase & stkField, const stk::mesh::Selector& selector) |
| 496 |
{ |
| 497 |
stk::mesh::NgpMesh & ngpMesh = stk::mesh::get_updated_ngp_mesh(get_bulk()); |
| 498 |
stk::mesh::NgpField<T, stk::mesh::NgpMeshDefaultMemSpace, NgpDebugger> & ngpField = stk::mesh::get_updated_ngp_field<T, stk::mesh::NgpMeshDefaultMemSpace, NgpDebugger>(stkField); |
| 499 |
stk::NgpVector<unsigned> bucketIds = ngpMesh.get_bucket_ids(stkField.entity_rank(), selector); |
| 500 |
stk::mesh::EntityRank rank = ngpField.get_rank(); |
| 501 |
|
| 502 |
Kokkos::parallel_for(stk::ngp::DeviceRangePolicy(0, 1), KOKKOS_LAMBDA(unsigned ) { |
| 503 |
for (unsigned i = 0; i < bucketIds.size(); ++i) { |
| 504 |
const stk::mesh::NgpMesh::BucketType & bucket = ngpMesh.get_bucket(rank, bucketIds.device_get(i)); |
| 505 |
for (unsigned j = 0; j < bucket.size(); ++j) { |
| 506 |
stk::mesh::FastMeshIndex index = ngpMesh.fast_mesh_index(bucket[j]); |
| 507 |
unsigned numComponents = ngpField.get_num_components_per_entity(index); |
| 508 |
for (unsigned component=0; component<numComponents; ++component) { |
| 509 |
#if defined(DEVICE_USE_LOCATION_BUILTINS) |
| 510 |
access_for_memory_checking_tool(&ngpField(index, component)); |
| 511 |
#else |
| 512 |
access_for_memory_checking_tool(&ngpField(index, component, __FILE__, __LINE__)); |
| 513 |
#endif |
| 514 |
} |
| 515 |
} |
| 516 |
} |
| 517 |
}); |
| 518 |
} |
| 519 |
|
| 520 |
template <typename T> |
| 521 |
void read_scalar_field_on_device(stk::mesh::Field<T> & stkField, const stk::mesh::Selector& selector) |
| 522 |
{ |
| 523 |
read_field_on_device<T>(stkField, selector); |
| 524 |
} |
| 525 |
|
| 526 |
template <typename T> |
| 527 |
void read_scalar_field_on_device(stk::mesh::Field<T> & stkField) |
| 528 |
{ |
| 529 |
read_scalar_field_on_device(stkField, stkField); |
| 530 |
} |
| 531 |
|
| 532 |
template <typename T> |
| 533 |
void read_vector_field_on_device(stk::mesh::FieldBase & stkField, const stk::mesh::Selector& selector) |
| 534 |
{ |
| 535 |
read_field_on_device<T>(stkField, selector); |
| 536 |
} |
| 537 |
|
| 538 |
template <typename T> |
| 539 |
void read_vector_field_on_device(stk::mesh::FieldBase & stkField) |
| 540 |
{ |
| 541 |
read_vector_field_on_device<T>(stkField, stkField); |
| 542 |
} |
| 543 |
|
| 544 |
template <typename T, typename NGPFIELD> |
| 545 |
void read_old_scalar_field_on_device(stk::mesh::Field<T> & stkField, NGPFIELD & ngpField, |
| 546 |
stk::mesh::EntityId maxIdToRead = std::numeric_limits<stk::mesh::EntityId>::max()) |
| 547 |
{ |
| 548 |
const int component = 0; |
| 549 |
const stk::mesh::NgpMesh & ngpMesh = stk::mesh::get_updated_ngp_mesh(get_bulk()); |
| 550 |
stk::NgpVector<unsigned> bucketIds = ngpMesh.get_bucket_ids(stkField.entity_rank(), stkField); |
| 551 |
stk::mesh::EntityRank rank = ngpField.get_rank(); |
| 552 |
|
| 553 |
Kokkos::parallel_for(stk::ngp::DeviceRangePolicy(0, 1), KOKKOS_LAMBDA(unsigned ) { |
| 554 |
for (unsigned i = 0; i < bucketIds.size(); ++i) { |
| 555 |
const stk::mesh::NgpMesh::BucketType & bucket = ngpMesh.get_bucket(rank, bucketIds.device_get(i)); |
| 556 |
for (unsigned j = 0; j < bucket.size(); ++j) { |
| 557 |
stk::mesh::FastMeshIndex index = ngpMesh.fast_mesh_index(bucket[j]); |
| 558 |
const stk::mesh::Entity elem = ngpMesh.get_entity(stk::topology::ELEM_RANK, index); |
| 559 |
const stk::mesh::EntityId elemId = ngpMesh.identifier(elem); |
| 560 |
if (elemId <= maxIdToRead) { |
| 561 |
#if defined(DEVICE_USE_LOCATION_BUILTINS) |
| 562 |
access_for_memory_checking_tool(&ngpField(index, component)); |
| 563 |
#else |
| 564 |
access_for_memory_checking_tool(&ngpField(index, component, __FILE__, __LINE__)); |
| 565 |
#endif |
| 566 |
} |
| 567 |
} |
| 568 |
} |
| 569 |
}); |
| 570 |
} |
| 571 |
|
| 572 |
template <typename T> |
| 573 |
void read_field_on_device_using_entity_field_data(stk::mesh::Field<T> & stkField) |
| 574 |
{ |
| 575 |
stk::mesh::NgpMesh & ngpMesh = stk::mesh::get_updated_ngp_mesh(get_bulk()); |
| 576 |
stk::mesh::NgpField<T, stk::mesh::NgpMeshDefaultMemSpace, NgpDebugger> & ngpField = stk::mesh::get_updated_ngp_field<T, stk::mesh::NgpMeshDefaultMemSpace, NgpDebugger>(stkField); |
| 577 |
stk::NgpVector<unsigned> bucketIds = ngpMesh.get_bucket_ids(stkField.entity_rank(), stkField); |
| 578 |
stk::mesh::EntityRank rank = ngpField.get_rank(); |
| 579 |
|
| 580 |
Kokkos::parallel_for(stk::ngp::DeviceRangePolicy(0, 1), KOKKOS_LAMBDA(unsigned ) { |
| 581 |
for (unsigned bucketId = 0; bucketId < bucketIds.size(); ++bucketId) { |
| 582 |
const stk::mesh::NgpMesh::BucketType & bucket = ngpMesh.get_bucket(rank, bucketIds.device_get(bucketId)); |
| 583 |
for (unsigned offset = 0; offset < bucket.size(); ++offset) { |
| 584 |
stk::mesh::FastMeshIndex index = ngpMesh.fast_mesh_index(bucket[offset]); |
| 585 |
#if defined(DEVICE_USE_LOCATION_BUILTINS) |
| 586 |
stk::mesh::EntityFieldData<double> vals = ngpField(index); |
| 587 |
#else |
| 588 |
stk::mesh::EntityFieldData<double> vals = ngpField(index, __FILE__, __LINE__); |
| 589 |
#endif |
| 590 |
access_for_memory_checking_tool(vals, vals.size()); |
| 591 |
} |
| 592 |
} |
| 593 |
}); |
| 594 |
} |
| 595 |
|
| 596 |
template <typename T> |
| 597 |
void read_field_on_device_using_mesh_index(stk::mesh::Field<T> & stkField) |
| 598 |
{ |
| 599 |
stk::mesh::NgpMesh & ngpMesh = stk::mesh::get_updated_ngp_mesh(get_bulk()); |
| 600 |
stk::mesh::NgpField<T, stk::mesh::NgpMeshDefaultMemSpace, NgpDebugger> & ngpField = stk::mesh::get_updated_ngp_field<T, stk::mesh::NgpMeshDefaultMemSpace, NgpDebugger>(stkField); |
| 601 |
stk::NgpVector<unsigned> bucketIds = ngpMesh.get_bucket_ids(stkField.entity_rank(), stkField); |
| 602 |
stk::mesh::EntityRank rank = ngpField.get_rank(); |
| 603 |
|
| 604 |
Kokkos::parallel_for(stk::ngp::DeviceRangePolicy(0, 1), KOKKOS_LAMBDA(unsigned ) { |
| 605 |
for (unsigned i = 0; i < bucketIds.size(); ++i) { |
| 606 |
const stk::mesh::NgpMesh::BucketType & bucket = ngpMesh.get_bucket(rank, bucketIds.device_get(i)); |
| 607 |
for (unsigned j = 0; j < bucket.size(); ++j) { |
| 608 |
stk::mesh::FastMeshIndex fastMeshIndex{bucket.bucket_id(), static_cast<unsigned>(j)}; |
| 609 |
const unsigned numComponents = ngpField.get_num_components_per_entity(fastMeshIndex); |
| 610 |
for (unsigned component = 0; component < numComponents; ++component) { |
| 611 |
#if defined(DEVICE_USE_LOCATION_BUILTINS) |
| 612 |
access_for_memory_checking_tool(&ngpField(fastMeshIndex, component)); |
| 613 |
#else |
| 614 |
access_for_memory_checking_tool(&ngpField(fastMeshIndex, component, __FILE__, __LINE__)); |
| 615 |
#endif |
| 616 |
} |
| 617 |
} |
| 618 |
} |
| 619 |
}); |
| 620 |
} |
| 621 |
|
| 622 |
}; |
| 623 |
|
| 624 |
#endif // NGPDEBUGFIELDSYNCFIXTURES_HPP |