Debian Codegraph @

packages/stk/stk_doc_tests/stk_mesh/howToUseNgpFieldAsyncCopy.cpp

raw
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/MeshBuilder.hpp>
37 #include <stk_mesh/base/Ngp.hpp>
38 #include <stk_mesh/base/GetNgpMesh.hpp>
39 #include <stk_mesh/base/GetNgpField.hpp>
40 #include <stk_mesh/base/GetNgpExecutionSpace.hpp>
41 #include <stk_mesh/base/NgpForEachEntity.hpp>
42 #include <stk_util/ngp/NgpSpaces.hpp>
43 #include <stk_mesh/base/NgpUtils.hpp>
44 #include <stk_ngp_test/ngp_test.hpp>
45 #include <stk_unit_test_utils/ioUtils.hpp>
46 #include <Kokkos_Core.hpp>
47
48 namespace {
49
50 void check_field_data_on_device(stk::mesh::BulkData& bulk, stk::mesh::NgpField<double>& ngpDoubleField,
51 stk::mesh::NgpField<int>& ngpIntField, double expectedDoubleValue, int expectedIntValue)
52 {
53 stk::mesh::NgpMesh& ngpMesh = stk::mesh::get_updated_ngp_mesh(bulk);
54 stk::mesh::Selector selector = bulk.mesh_meta_data().universal_part();
55
56 stk::mesh::for_each_entity_run(ngpMesh, stk::topology::ELEM_RANK, selector,
57 KOKKOS_LAMBDA(const stk::mesh::FastMeshIndex& elem)
58 {
59 double doubleFieldValue = ngpDoubleField(elem, 0);
60 NGP_EXPECT_EQ(expectedDoubleValue, doubleFieldValue);
61
62 int intFieldValue = ngpIntField(elem, 0);
63 NGP_EXPECT_EQ(expectedIntValue, intFieldValue);
64 });
65 }
66
67 TEST(stkMeshHowTo, ngpFieldAsyncCopy)
68 {
69 MPI_Comm communicator = MPI_COMM_WORLD;
70 if(stk::parallel_machine_size(communicator) > 1) { GTEST_SKIP();}
71
72 using DoubleField = stk::mesh::Field<double>;
73 using IntField = stk::mesh::Field<int>;
74
75 const unsigned spatialDimension = 3;
76 stk::mesh::MeshBuilder builder(MPI_COMM_WORLD);
77 builder.set_spatial_dimension(spatialDimension);
78 std::shared_ptr<stk::mesh::BulkData> bulkPtr = stk::mesh::MeshBuilder(MPI_COMM_WORLD).create();
79 stk::mesh::MetaData& meta = bulkPtr->mesh_meta_data();
80 stk::mesh::BulkData& bulk = *bulkPtr;
81
82 unsigned numStates = 1;
83 DoubleField& doubleField = meta.declare_field<double>(stk::topology::ELEM_RANK, "doubleField", numStates);
84 IntField& intField = meta.declare_field<int>(stk::topology::ELEM_RANK, "intField", numStates);
85
86 double initialDoubleFieldValue = 1.0;
87 double modifiedDoubleFieldValue = initialDoubleFieldValue*2;
88 int initialIntFieldValue = 2;
89 int modifiedIntFieldValue = initialIntFieldValue*2;
90
91 stk::mesh::put_field_on_entire_mesh_with_initial_value(doubleField, &initialDoubleFieldValue);
92 stk::mesh::put_field_on_entire_mesh_with_initial_value(intField, &initialIntFieldValue);
93 stk::io::fill_mesh("generated:1x1x1", bulk);
94
95 stk::mesh::NgpField<double>& ngpDoubleField = stk::mesh::get_updated_ngp_field<double>(doubleField);
96 stk::mesh::NgpField<int>& ngpIntField = stk::mesh::get_updated_ngp_field<int>(intField);
97
98 stk::mesh::ExecSpaceWrapper<> execSpaceWithStream1 = stk::mesh::get_execution_space_with_stream();
99 stk::mesh::ExecSpaceWrapper<> execSpaceWithStream2 = stk::mesh::get_execution_space_with_stream();
100
101 stk::mesh::Entity elem = bulk.get_entity(stk::topology::ELEM_RANK, 1u);
102 double* doubleData = reinterpret_cast<double*>(stk::mesh::field_data(doubleField, elem));
103 *doubleData = initialDoubleFieldValue*2;
104 int* intData = reinterpret_cast<int*>(stk::mesh::field_data(intField, elem));
105 *intData = initialIntFieldValue*2;
106
107 ngpDoubleField.modify_on_host();
108 ngpDoubleField.sync_to_device(execSpaceWithStream1);
109 ngpIntField.modify_on_host();
110 ngpIntField.sync_to_device(execSpaceWithStream2.get_execution_space());
111
112 stk::mesh::ngp_field_fence(meta);
113
114 check_field_data_on_device(bulk, ngpDoubleField, ngpIntField, modifiedDoubleFieldValue, modifiedIntFieldValue);
115 }
116
117 }