Debian Codegraph @

packages/stk/stk_performance_tests/stk_mesh/NgpFieldAccess.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/Ngp.hpp>
37 #include <stk_mesh/base/NgpMesh.hpp>
38 #include <stk_mesh/base/NgpField.hpp>
39 #include <stk_unit_test_utils/getOption.h>
40 #include <stk_unit_test_utils/MeshFixture.hpp>
41 #include <stk_unit_test_utils/GetMeshSpec.hpp>
42 #include <stk_unit_test_utils/PerformanceTester.hpp>
43 #include <stk_mesh/base/MetaData.hpp>
44 #include <stk_mesh/base/BulkData.hpp>
45 #include <stk_mesh/base/Bucket.hpp>
46 #include <stk_mesh/base/Field.hpp>
47 #include <stk_mesh/base/Entity.hpp>
48 #include <stk_mesh/base/GetEntities.hpp>
49 #include <stk_mesh/base/ExodusTranslator.hpp>
50 #include <stk_mesh/base/GetNgpMesh.hpp>
51 #include <stk_util/stk_config.h>
52 #include <stk_unit_test_utils/timer.hpp>
53 #include <stk_performance_tests/stk_mesh/calculate_centroid.hpp>
54 #include <stk_performance_tests/stk_mesh/multi_block.hpp>
55
56 namespace ngp_field_perf_test
57 {
58
59 class NgpFieldAccess : public stk::unit_test_util::MeshFixture
60 {
61 public:
62 NgpFieldAccess()
63 : batchTimer(get_comm())
64 { }
65
66 protected:
67 void declare_centroid_field()
68 {
69 centroid = &get_meta().declare_field<double>(stk::topology::ELEM_RANK, "centroid");
70 stk::mesh::put_field_on_mesh(*centroid, get_meta().universal_part(), 3, nullptr);
71
72 hostCentroid = &get_meta().declare_field<double>(stk::topology::ELEM_RANK, "hostCentroid");
73 stk::mesh::put_field_on_mesh(*hostCentroid, get_meta().universal_part(), 3, nullptr);
74 }
75
76 void declare_centroid_partial_mesh(unsigned numBlocks)
77 {
78 centroid = &get_meta().declare_field<double>(stk::topology::ELEM_RANK, "centroid");
79 for(unsigned i = 1; i <= numBlocks; i++) {
80 const std::string partName = "block_" + std::to_string(i);
81 stk::mesh::Part& part = get_meta().declare_part(partName, stk::topology::ELEM_RANK);
82 stk::mesh::put_field_on_mesh(*centroid, part, 3, nullptr);
83 }
84 }
85
86 void setup_multi_block_mesh(unsigned numElemsPerDim, unsigned numBlocks)
87 {
88 stk::performance_tests::setup_multiple_blocks(get_meta(), numBlocks);
89 stk::io::fill_mesh(stk::unit_test_util::get_mesh_spec(numElemsPerDim), get_bulk());
90 stk::performance_tests::move_elements_to_other_blocks(get_bulk(), numElemsPerDim);
91 }
92
93 void verify_averaged_centroids_are_center_of_mesh(int elemsPerDim, stk::mesh::Field<double>& centroidField)
94 {
95 std::vector<double> average = stk::performance_tests::get_centroid_average_from_host(get_bulk(), centroidField, stk::mesh::Selector(get_meta().universal_part()));
96 double meshCenter = elemsPerDim/2.0;
97 for(size_t dim = 0; dim < 3; dim++) {
98 EXPECT_DOUBLE_EQ(meshCenter, average[dim]);
99 }
100 }
101
102 void verify_averaged_centroids_are_center_of_mesh(int /*elemsPerDim*/, const stk::mesh::Selector& selector)
103 {
104 std::vector<double> hostAverage = stk::performance_tests::get_centroid_average_from_host(get_bulk(), *centroid, selector);
105 std::vector<double> deviceAverage = stk::performance_tests::get_centroid_average_from_device(get_bulk(), *centroid, selector);
106 for(size_t dim = 0; dim < 3; dim++) {
107 EXPECT_DOUBLE_EQ(hostAverage[dim], deviceAverage[dim]);
108 }
109 }
110
111 void compare_and_verify_average_centroids(int /*elemsPerDim*/, const stk::mesh::Selector& selector)
112 {
113 std::vector<double> centroidAverage = stk::performance_tests::get_centroid_average_from_host(get_bulk(), *centroid, selector);
114 std::vector<double> hostCentroidAverage = stk::performance_tests::get_centroid_average_from_host(get_bulk(), *hostCentroid, selector);
115 for(size_t dim = 0; dim < 3; dim++) {
116 EXPECT_DOUBLE_EQ(centroidAverage[dim], hostCentroidAverage[dim]);
117 }
118 }
119
120 stk::unit_test_util::BatchTimer batchTimer;
121 stk::mesh::Field<double> *centroid;
122 stk::mesh::Field<double> *hostCentroid;
123 };
124
125 TEST_F(NgpFieldAccess, Centroid)
126 {
127 if (get_parallel_size() != 1) { GTEST_SKIP(); }
128
129 const unsigned NUM_RUNS = 5;
130 const int NUM_ITERS = 100;
131 const int ELEMS_PER_DIM = 120;
132
133 batchTimer.initialize_batch_timer();
134
135 setup_empty_mesh(stk::mesh::BulkData::NO_AUTO_AURA);
136 declare_centroid_field();
137 stk::io::fill_mesh(stk::unit_test_util::get_mesh_spec(ELEMS_PER_DIM), get_bulk());
138
139 for (unsigned j = 0; j < NUM_RUNS; j++) {
140 batchTimer.start_batch_timer();
141 for (int i = 0; i <NUM_ITERS; i++) {
142 stk::performance_tests::calculate_centroid_using_coord_field<stk::mesh::NgpField<double>>(get_bulk(), *centroid);
143 }
144 verify_averaged_centroids_are_center_of_mesh(ELEMS_PER_DIM, *centroid);
145 batchTimer.stop_batch_timer();
146 }
147 batchTimer.print_batch_timing(NUM_ITERS);
148 }
149
150 TEST_F(NgpFieldAccess, HostCentroid)
151 {
152 if (get_parallel_size() != 1) { GTEST_SKIP(); }
153
154 const unsigned NUM_RUNS = 5;
155 const int NUM_ITERS = 100;
156 const int ELEMS_PER_DIM = 120;
157
158 batchTimer.initialize_batch_timer();
159
160 setup_empty_mesh(stk::mesh::BulkData::NO_AUTO_AURA);
161 declare_centroid_field();
162 stk::io::fill_mesh(stk::unit_test_util::get_mesh_spec(ELEMS_PER_DIM), get_bulk());
163
164 for (unsigned j = 0; j < NUM_RUNS; j++) {
165 batchTimer.start_batch_timer();
166
167 for (int i = 0; i < NUM_ITERS; i++) {
168 stk::performance_tests::calculate_centroid_using_host_coord_fields(get_bulk(), *hostCentroid);
169 }
170 verify_averaged_centroids_are_center_of_mesh(ELEMS_PER_DIM, *hostCentroid);
171
172 batchTimer.stop_batch_timer();
173 }
174 batchTimer.print_batch_timing(NUM_ITERS);
175 }
176
177 TEST_F(NgpFieldAccess, CentroidMultiBlock)
178 {
179 if (get_parallel_size() != 1) return;
180
181 const unsigned NUM_RUNS = 5;
182 const int NUM_ITERS = 100;
183 const int ELEMS_PER_DIM = 100;
184 const int NUM_BLOCKS = 100;
185
186 batchTimer.initialize_batch_timer();
187
188 setup_empty_mesh(stk::mesh::BulkData::NO_AUTO_AURA);
189 declare_centroid_field();
190 setup_multi_block_mesh(ELEMS_PER_DIM, NUM_BLOCKS);
191
192 stk::mesh::PartVector elemBlockParts;
193 stk::mesh::fill_element_block_parts(get_meta(), stk::topology::HEX_8, elemBlockParts);
194
195 for (unsigned j = 0; j < NUM_RUNS; j++) {
196 batchTimer.start_batch_timer();
197 for (int i = 0; i < NUM_ITERS; i++) {
198 for (const stk::mesh::Part* blockPart : elemBlockParts) {
199 stk::performance_tests::calculate_centroid_using_coord_field<stk::mesh::NgpField<double>>(
200 get_bulk(), *blockPart, *centroid);
201 }
202 }
203
204 verify_averaged_centroids_are_center_of_mesh(ELEMS_PER_DIM, get_meta().universal_part());
205 batchTimer.stop_batch_timer();
206 }
207 batchTimer.print_batch_timing(NUM_ITERS);
208 }
209
210 TEST_F(NgpFieldAccess, CentroidPartialBlock)
211 {
212 if (get_parallel_size() != 1) return;
213
214 const unsigned NUM_RUNS = 5;
215 const int NUM_ITERS = 250;
216 const int ELEMS_PER_DIM = 100;
217 const int NUM_BLOCKS = 100;
218 int BLOCKS = stk::unit_test_util::get_command_line_option<int>("-n", 50);
219 BLOCKS = std::max(BLOCKS, 1);
220 BLOCKS = std::min(BLOCKS, NUM_BLOCKS);
221
222 batchTimer.initialize_batch_timer();
223
224 setup_empty_mesh(stk::mesh::BulkData::NO_AUTO_AURA);
225 declare_centroid_partial_mesh(BLOCKS);
226 setup_multi_block_mesh(ELEMS_PER_DIM, NUM_BLOCKS);
227
228 stk::mesh::PartVector elemBlockParts;
229 stk::mesh::fill_element_block_parts(get_meta(), stk::topology::HEX_8, elemBlockParts);
230
231 for (unsigned j = 0; j < NUM_RUNS; j++) {
232 batchTimer.start_batch_timer();
233
234 stk::mesh::get_updated_ngp_mesh(get_bulk());
235 for (int i = 0; i < NUM_ITERS; i++) {
236 for (const stk::mesh::Part* blockPart : elemBlockParts) {
237 stk::performance_tests::calculate_centroid_using_coord_field<stk::mesh::NgpField<double>>(
238 get_bulk(), *blockPart, *centroid);
239 verify_averaged_centroids_are_center_of_mesh(ELEMS_PER_DIM, *blockPart);
240 }
241 }
242 batchTimer.stop_batch_timer();
243 }
244 batchTimer.print_batch_timing(NUM_ITERS);
245 }
246
247 }