| 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_mesh/base/NgpMesh.hpp> |
| 38 |
#include <stk_mesh/base/NgpField.hpp> |
| 39 |
#include <stk_mesh/base/Field.hpp> |
| 40 |
#include <stk_mesh/base/FieldBase.hpp> |
| 41 |
#include <stk_mesh/base/FieldDataManager.hpp> |
| 42 |
#include <stk_mesh/base/MetaData.hpp> |
| 43 |
#include <stk_mesh/base/Bucket.hpp> |
| 44 |
#include <stk_mesh/base/GetNgpField.hpp> |
| 45 |
#include <stk_mesh/base/GetNgpMesh.hpp> |
| 46 |
#include <stk_mesh/base/MeshBuilder.hpp> |
| 47 |
#include <stk_topology/topology.hpp> |
| 48 |
#include <stk_unit_test_utils/MeshFixture.hpp> |
| 49 |
#include <stk_unit_test_utils/GetMeshSpec.hpp> |
| 50 |
#include <stk_unit_test_utils/timer.hpp> |
| 51 |
#include <stk_io/FillMesh.hpp> |
| 52 |
#include <vector> |
| 53 |
#include <string> |
| 54 |
|
| 55 |
namespace |
| 56 |
{ |
| 57 |
|
| 58 |
double initial_value[3] = {-1, 2, -0.3}; |
| 59 |
|
| 60 |
class NgpFieldAccessPerformance : public stk::unit_test_util::MeshFixture |
| 61 |
{ |
| 62 |
public: |
| 63 |
using DoubleVecField = stk::mesh::Field<double>; |
| 64 |
|
| 65 |
NgpFieldAccessPerformance() |
| 66 |
: batchTimer(get_comm()) |
| 67 |
{ } |
| 68 |
|
| 69 |
virtual ~NgpFieldAccessPerformance() { |
| 70 |
reset_mesh(); |
| 71 |
} |
| 72 |
|
| 73 |
void setup_empty_mesh_with_field_data_manager(stk::mesh::BulkData::AutomaticAuraOption auraOption, |
| 74 |
std::unique_ptr<stk::mesh::FieldDataManager> fieldDataManager, |
| 75 |
unsigned initialBucketCapacity = stk::mesh::get_default_initial_bucket_capacity(), |
| 76 |
unsigned maximumBucketCapacity = stk::mesh::get_default_maximum_bucket_capacity()) |
| 77 |
{ |
| 78 |
stk::mesh::MeshBuilder builder(communicator); |
| 79 |
builder.set_spatial_dimension(m_spatialDim); |
| 80 |
builder.set_entity_rank_names(m_entityRankNames); |
| 81 |
builder.set_aura_option(auraOption); |
| 82 |
builder.set_field_data_manager(std::move(fieldDataManager)); |
| 83 |
builder.set_initial_bucket_capacity(initialBucketCapacity); |
| 84 |
builder.set_maximum_bucket_capacity(maximumBucketCapacity); |
| 85 |
|
| 86 |
if(nullptr == metaData) { |
| 87 |
metaData = builder.create_meta_data(); |
| 88 |
} |
| 89 |
|
| 90 |
if(nullptr == bulkData) { |
| 91 |
bulkData = builder.create(metaData); |
| 92 |
m_auraOption = auraOption; |
| 93 |
m_initialBucketCapacity = initialBucketCapacity; |
| 94 |
m_maximumBucketCapacity = maximumBucketCapacity; |
| 95 |
} |
| 96 |
} |
| 97 |
|
| 98 |
DoubleVecField * createNodalVectorField(const std::string &field_name) |
| 99 |
{ |
| 100 |
const int spatialDimension = static_cast<int>(get_meta().spatial_dimension()); |
| 101 |
DoubleVecField &field = get_meta().declare_field<double>(stk::topology::NODE_RANK, field_name); |
| 102 |
stk::mesh::put_field_on_mesh(field, field.mesh_meta_data().universal_part(), spatialDimension, initial_value); |
| 103 |
return &field; |
| 104 |
} |
| 105 |
|
| 106 |
void createNodalVectorFields() |
| 107 |
{ |
| 108 |
dispField = createNodalVectorField("disp"); |
| 109 |
velField = createNodalVectorField("vel"); |
| 110 |
accField = createNodalVectorField("acc"); |
| 111 |
forceField = createNodalVectorField("force"); |
| 112 |
} |
| 113 |
|
| 114 |
void testVectorFieldSum(unsigned NUM_ITERS) |
| 115 |
{ |
| 116 |
stk::mesh::NgpField<double> & ngpDispField = stk::mesh::get_updated_ngp_field<double>(*dispField); |
| 117 |
stk::mesh::NgpField<double> & ngpVelField = stk::mesh::get_updated_ngp_field<double>(*velField); |
| 118 |
stk::mesh::NgpField<double> & ngpAccField = stk::mesh::get_updated_ngp_field<double>(*accField); |
| 119 |
stk::mesh::NgpField<double> & ngpForceField = stk::mesh::get_updated_ngp_field<double>(*forceField); |
| 120 |
stk::mesh::NgpMesh & ngpMesh = stk::mesh::get_updated_ngp_mesh(get_bulk()); |
| 121 |
const int spatialDimension = static_cast<int>(get_meta().spatial_dimension()); |
| 122 |
|
| 123 |
for (unsigned i = 0; i < NUM_ITERS; ++i) { |
| 124 |
stk::mesh::for_each_entity_run(ngpMesh, stk::topology::NODE_RANK, get_meta().locally_owned_part(), |
| 125 |
KOKKOS_LAMBDA(const stk::mesh::FastMeshIndex & index) |
| 126 |
{ |
| 127 |
for (int component = 0; component < spatialDimension; ++component) { |
| 128 |
ngpForceField(index, component) = alpha * ngpDispField(index, component) + |
| 129 |
beta * ngpVelField(index, component) + |
| 130 |
gamma * ngpAccField(index, component); |
| 131 |
} |
| 132 |
}); |
| 133 |
} |
| 134 |
|
| 135 |
ngpForceField.modify_on_device(); |
| 136 |
} |
| 137 |
|
| 138 |
void testHostVectorFieldSum(unsigned NUM_ITERS) |
| 139 |
{ |
| 140 |
stk::mesh::get_updated_ngp_field<double>(*dispField); |
| 141 |
stk::mesh::get_updated_ngp_field<double>(*velField); |
| 142 |
stk::mesh::get_updated_ngp_field<double>(*accField); |
| 143 |
stk::mesh::get_updated_ngp_field<double>(*forceField); |
| 144 |
const int spatialDimension = static_cast<int>(get_meta().spatial_dimension()); |
| 145 |
|
| 146 |
for (unsigned i = 0; i < NUM_ITERS; ++i) { |
| 147 |
const stk::mesh::BucketVector & buckets = get_bulk().get_buckets(stk::topology::NODE_RANK, get_meta().locally_owned_part()); |
| 148 |
for (const stk::mesh::Bucket * bucket : buckets) { |
| 149 |
for (const stk::mesh::Entity & entity : *bucket) { |
| 150 |
const double * dispFieldData = stk::mesh::field_data(*dispField, entity); |
| 151 |
const double * velFieldData = stk::mesh::field_data(*velField, entity); |
| 152 |
const double * accFieldData = stk::mesh::field_data(*accField, entity); |
| 153 |
double * forceFieldData = stk::mesh::field_data(*forceField, entity); |
| 154 |
for (int component = 0; component < spatialDimension; ++component) { |
| 155 |
forceFieldData[component] = alpha * dispFieldData[component] + |
| 156 |
beta * velFieldData[component] + |
| 157 |
gamma * accFieldData[component]; |
| 158 |
} |
| 159 |
} |
| 160 |
} |
| 161 |
} |
| 162 |
} |
| 163 |
|
| 164 |
void testPureHostVectorFieldSum(unsigned NUM_ITERS) |
| 165 |
{ |
| 166 |
const int spatialDimension = static_cast<int>(get_meta().spatial_dimension()); |
| 167 |
|
| 168 |
for (unsigned i = 0; i < NUM_ITERS; ++i) { |
| 169 |
const stk::mesh::BucketVector & buckets = get_bulk().get_buckets(stk::topology::NODE_RANK, get_meta().locally_owned_part()); |
| 170 |
for (const stk::mesh::Bucket * bucket : buckets) { |
| 171 |
for (const stk::mesh::Entity & entity : *bucket) { |
| 172 |
const double * dispFieldData = stk::mesh::field_data(*dispField, entity); |
| 173 |
const double * velFieldData = stk::mesh::field_data(*velField, entity); |
| 174 |
const double * accFieldData = stk::mesh::field_data(*accField, entity); |
| 175 |
double * forceFieldData = stk::mesh::field_data(*forceField, entity); |
| 176 |
for (int component = 0; component < spatialDimension; ++component) { |
| 177 |
forceFieldData[component] = alpha * dispFieldData[component] + |
| 178 |
beta * velFieldData[component] + |
| 179 |
gamma * accFieldData[component]; |
| 180 |
} |
| 181 |
} |
| 182 |
} |
| 183 |
} |
| 184 |
} |
| 185 |
|
| 186 |
void checkResult() |
| 187 |
{ |
| 188 |
stk::mesh::NgpField<double> & ngpForceField = stk::mesh::get_updated_ngp_field<double>(*forceField); |
| 189 |
ngpForceField.sync_to_host(); |
| 190 |
const int spatialDimension = static_cast<int>(get_meta().spatial_dimension()); |
| 191 |
|
| 192 |
std::vector<double> expectedValues(spatialDimension); |
| 193 |
for (int i = 0; i < spatialDimension; ++i) { |
| 194 |
expectedValues[i] = alpha * initial_value[i] + beta * initial_value[i] + gamma * initial_value[i]; |
| 195 |
} |
| 196 |
|
| 197 |
const stk::mesh::BucketVector & buckets = get_bulk().get_buckets(stk::topology::NODE_RANK, get_meta().locally_owned_part()); |
| 198 |
for (stk::mesh::Bucket * bucket : buckets) { |
| 199 |
for (const stk::mesh::Entity & node : *bucket) { |
| 200 |
const double * forceFieldData = stk::mesh::field_data(*forceField, node); |
| 201 |
for (int component = 0; component < spatialDimension; ++component) { |
| 202 |
EXPECT_DOUBLE_EQ(forceFieldData[component], expectedValues[component]); |
| 203 |
} |
| 204 |
} |
| 205 |
} |
| 206 |
} |
| 207 |
|
| 208 |
void checkHostResult() |
| 209 |
{ |
| 210 |
const int spatialDimension = static_cast<int>(get_meta().spatial_dimension()); |
| 211 |
|
| 212 |
std::vector<double> expectedValues(spatialDimension); |
| 213 |
for (int i = 0; i < spatialDimension; ++i) { |
| 214 |
expectedValues[i] = alpha * initial_value[i] + beta * initial_value[i] + gamma * initial_value[i]; |
| 215 |
} |
| 216 |
|
| 217 |
const stk::mesh::BucketVector & buckets = get_bulk().get_buckets(stk::topology::NODE_RANK, get_meta().locally_owned_part()); |
| 218 |
for (const stk::mesh::Bucket * bucket : buckets) { |
| 219 |
for (const stk::mesh::Entity & entity : *bucket) { |
| 220 |
const double * forceFieldData = stk::mesh::field_data(*forceField, entity); |
| 221 |
for (int component = 0; component < spatialDimension; ++component) { |
| 222 |
EXPECT_DOUBLE_EQ(forceFieldData[component], expectedValues[component]); |
| 223 |
} |
| 224 |
} |
| 225 |
} |
| 226 |
} |
| 227 |
|
| 228 |
static constexpr double alpha = -1.4; |
| 229 |
static constexpr double beta = 0.3333333; |
| 230 |
static constexpr double gamma = 3.14159; |
| 231 |
|
| 232 |
stk::unit_test_util::BatchTimer batchTimer; |
| 233 |
|
| 234 |
DoubleVecField * dispField; |
| 235 |
DoubleVecField * velField; |
| 236 |
DoubleVecField * accField; |
| 237 |
DoubleVecField * forceField; |
| 238 |
}; |
| 239 |
|
| 240 |
TEST_F(NgpFieldAccessPerformance, pureHost_vectorSum_DefaultFieldDataManager) |
| 241 |
{ |
| 242 |
if (get_parallel_size() != 1) return; |
| 243 |
|
| 244 |
unsigned numElemsPerDim = 100; |
| 245 |
const int weKnowThereAreFiveRanks = 5; |
| 246 |
auto fieldDataManager = std::make_unique<stk::mesh::DefaultFieldDataManager>(weKnowThereAreFiveRanks); |
| 247 |
|
| 248 |
batchTimer.initialize_batch_timer(); |
| 249 |
setup_empty_mesh_with_field_data_manager(stk::mesh::BulkData::NO_AUTO_AURA, std::move(fieldDataManager)); |
| 250 |
createNodalVectorFields(); |
| 251 |
stk::io::fill_mesh(stk::unit_test_util::get_mesh_spec(numElemsPerDim), *bulkData); |
| 252 |
|
| 253 |
const unsigned NUM_RUNS = 5; |
| 254 |
const unsigned NUM_ITERS = 1000; |
| 255 |
for (unsigned j = 0; j < NUM_RUNS; j++) { |
| 256 |
batchTimer.start_batch_timer(); |
| 257 |
testPureHostVectorFieldSum(NUM_ITERS); |
| 258 |
batchTimer.stop_batch_timer(); |
| 259 |
} |
| 260 |
batchTimer.print_batch_timing(NUM_ITERS); |
| 261 |
|
| 262 |
checkHostResult(); |
| 263 |
} |
| 264 |
|
| 265 |
TEST_F(NgpFieldAccessPerformance, host_vectorSum_DefaultFieldDataManager) |
| 266 |
{ |
| 267 |
if (get_parallel_size() != 1) return; |
| 268 |
|
| 269 |
unsigned numElemsPerDim = 100; |
| 270 |
const int weKnowThereAreFiveRanks = 5; |
| 271 |
auto fieldDataManager = std::make_unique<stk::mesh::DefaultFieldDataManager>(weKnowThereAreFiveRanks); |
| 272 |
|
| 273 |
batchTimer.initialize_batch_timer(); |
| 274 |
setup_empty_mesh_with_field_data_manager(stk::mesh::BulkData::NO_AUTO_AURA, std::move(fieldDataManager)); |
| 275 |
createNodalVectorFields(); |
| 276 |
stk::io::fill_mesh(stk::unit_test_util::get_mesh_spec(numElemsPerDim), *bulkData); |
| 277 |
|
| 278 |
const unsigned NUM_RUNS = 5; |
| 279 |
const unsigned NUM_ITERS = 1000; |
| 280 |
for (unsigned j = 0; j < NUM_RUNS; j++) { |
| 281 |
batchTimer.start_batch_timer(); |
| 282 |
testHostVectorFieldSum(NUM_ITERS); |
| 283 |
batchTimer.stop_batch_timer(); |
| 284 |
} |
| 285 |
batchTimer.print_batch_timing(NUM_ITERS); |
| 286 |
|
| 287 |
checkHostResult(); |
| 288 |
} |
| 289 |
|
| 290 |
void fill_mesh(stk::mesh::BulkData& bulk, unsigned numElemsPerDim) |
| 291 |
{ |
| 292 |
stk::io::StkMeshIoBroker stkIo(MPI_COMM_WORLD); |
| 293 |
stkIo.set_bulk_data(bulk); |
| 294 |
stkIo.add_mesh_database(stk::unit_test_util::get_mesh_spec(numElemsPerDim), stk::io::READ_MESH); |
| 295 |
stkIo.create_input_mesh(); |
| 296 |
const bool delayFieldDataAllocation = true; |
| 297 |
stkIo.populate_mesh(delayFieldDataAllocation); |
| 298 |
stkIo.populate_field_data(); |
| 299 |
} |
| 300 |
|
| 301 |
TEST_F(NgpFieldAccessPerformance, vectorSum_DefaultFieldDataManager) |
| 302 |
{ |
| 303 |
if (get_parallel_size() != 1) return; |
| 304 |
|
| 305 |
unsigned numElemsPerDim = 100; |
| 306 |
const int weKnowThereAreFiveRanks = 5; |
| 307 |
auto fieldDataManager = std::make_unique<stk::mesh::DefaultFieldDataManager>(weKnowThereAreFiveRanks); |
| 308 |
|
| 309 |
batchTimer.initialize_batch_timer(); |
| 310 |
setup_empty_mesh_with_field_data_manager(stk::mesh::BulkData::NO_AUTO_AURA, std::move(fieldDataManager)); |
| 311 |
createNodalVectorFields(); |
| 312 |
fill_mesh(*bulkData, numElemsPerDim); |
| 313 |
|
| 314 |
const unsigned NUM_RUNS = 5; |
| 315 |
const unsigned NUM_ITERS = 1000; |
| 316 |
for (unsigned j = 0; j < NUM_RUNS; j++) { |
| 317 |
batchTimer.start_batch_timer(); |
| 318 |
testVectorFieldSum(NUM_ITERS); |
| 319 |
batchTimer.stop_batch_timer(); |
| 320 |
} |
| 321 |
batchTimer.print_batch_timing(NUM_ITERS); |
| 322 |
|
| 323 |
checkResult(); |
| 324 |
} |
| 325 |
|
| 326 |
TEST_F(NgpFieldAccessPerformance, vectorSum_ContiguousFieldDataManager) |
| 327 |
{ |
| 328 |
if (get_parallel_size() != 1) return; |
| 329 |
|
| 330 |
unsigned numElemsPerDim = 100; |
| 331 |
auto fieldDataManager = std::make_unique<stk::mesh::ContiguousFieldDataManager>(); |
| 332 |
|
| 333 |
batchTimer.initialize_batch_timer(); |
| 334 |
setup_empty_mesh_with_field_data_manager(stk::mesh::BulkData::NO_AUTO_AURA, std::move(fieldDataManager)); |
| 335 |
createNodalVectorFields(); |
| 336 |
fill_mesh(*bulkData, numElemsPerDim); |
| 337 |
|
| 338 |
const unsigned NUM_RUNS = 5; |
| 339 |
const unsigned NUM_ITERS = 2000; |
| 340 |
for (unsigned j = 0; j < NUM_RUNS; j++) { |
| 341 |
batchTimer.start_batch_timer(); |
| 342 |
testVectorFieldSum(NUM_ITERS); |
| 343 |
batchTimer.stop_batch_timer(); |
| 344 |
} |
| 345 |
batchTimer.print_batch_timing(NUM_ITERS); |
| 346 |
|
| 347 |
checkResult(); |
| 348 |
} |
| 349 |
|
| 350 |
} |
| 351 |
|