Debian Codegraph @

packages/stk/stk_performance_tests/stk_mesh/HexSTKGather.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 <sstream>
36 #include <gtest/gtest.h>
37
38 #include <stk_util/environment/CPUTime.hpp>
39 #include <stk_util/environment/memory_util.hpp>
40 #include <stk_util/environment/perf_util.hpp>
41
42 #include <stk_io/util/Gmesh_STKmesh_Fixture.hpp>
43 #include <stk_performance_tests/stk_mesh/calculate_centroid.hpp>
44
45 #include <stk_mesh/base/Relation.hpp>
46 #include <stk_mesh/base/Entity.hpp>
47 #include <stk_mesh/base/BulkData.hpp>
48 #include <stk_mesh/base/MetaData.hpp>
49 #include <stk_mesh/base/Field.hpp>
50
51 namespace stk {
52 namespace performance_tests {
53
54 namespace {
55
56 //This very simple test will visit all local elements, gather coordinates,
57 //compute element-centroid (simple average of nodal coords) for each, and
58 //store the sum of the centroids in sum_centroid.
59 void do_stk_gather_test(stk::mesh::BulkData& bulk, std::vector<double>& sum_centroid)
60 {
61 using namespace stk::mesh;
62 typedef Field<double> VectorField;
63
64 const MetaData& meta = bulk.mesh_meta_data();
65 const unsigned spatial_dim = meta.spatial_dimension();
66 for(unsigned d=0; d<spatial_dim; ++d) sum_centroid[d] = 0;
67
68 std::vector<double> elem_centroid(spatial_dim, 0);
69
70 const VectorField * coord_field = meta.get_field<double>(stk::topology::NODE_RANK, "coordinates");
71 STK_ThrowAssert(coord_field != nullptr);
72
73 Selector local = meta.locally_owned_part();
74
75 BucketVector const& buckets = bulk.get_buckets(stk::topology::ELEMENT_RANK, local);
76
77 std::vector<double> elem_node_coords;
78
79 for(size_t ib=0; ib<buckets.size(); ++ib) {
80 const Bucket& b = *buckets[ib];
81 const size_t num_nodes = b.topology().num_nodes();
82 const size_t len = num_nodes*spatial_dim;
83 if (elem_node_coords.size() != len) elem_node_coords.resize(len);
84
85 for(size_t i=0; i<b.size(); ++i) {
86
87 //here's the gather:
88
89 Entity const *node_rel_itr = b.begin_nodes(i);
90 Entity const *node_rels_end = b.end_nodes(i);
91
92 unsigned offset = 0;
93 for(; node_rel_itr != node_rels_end; ++node_rel_itr)
94 {
95 Entity node = *node_rel_itr;
96 double* node_coords = stk::mesh::field_data(*coord_field, node);
97 elem_node_coords[offset++] = node_coords[0];
98 elem_node_coords[offset++] = node_coords[1];
99 elem_node_coords[offset++] = node_coords[2];
100 }
101
102 stk::performance_tests::calculate_centroid_3d(num_nodes, &elem_node_coords[0], &elem_centroid[0]);
103
104 //add this element-centroid to the sum_centroid vector, and
105 //re-zero the element-centroid vector:
106 sum_centroid[0] += elem_centroid[0]; elem_centroid[0] = 0;
107 sum_centroid[1] += elem_centroid[1]; elem_centroid[1] = 0;
108 sum_centroid[2] += elem_centroid[2]; elem_centroid[2] = 0;
109 }
110 }
111 }
112
113 } // empty namespace
114
115 TEST(hex_gather, hex_gather)
116 {
117 //vector of mesh-dimensions holds the number of elements in each dimension.
118 //Hard-wired to 3. This test can run with spatial-dimension less than 3,
119 //(if generated-mesh can do that) but not greater than 3.
120 std::vector<int> mesh_dims(3);
121 #ifndef NDEBUG
122 mesh_dims[0]=30; //num_elems_x
123 mesh_dims[1]=30; //num_elems_y
124 mesh_dims[2]=30; //num_elems_z
125 #else
126 mesh_dims[0]=150; //num_elems_x
127 mesh_dims[1]=150; //num_elems_y
128 mesh_dims[2]=150; //num_elems_z
129 #endif
130
131 std::ostringstream oss;
132 oss << mesh_dims[0] << "x" << mesh_dims[1] << "x" << mesh_dims[2];
133
134 double start_time = stk::cpu_time();
135
136 stk::io::util::Gmesh_STKmesh_Fixture fixture(MPI_COMM_WORLD, oss.str());
137 fixture.commit();
138
139 double mesh_create_time = stk::cpu_time() - start_time;
140
141 const size_t spatial_dim = fixture.getMetaData().spatial_dimension();
142
143 //compute total number of elements in the mesh:
144 size_t num_elems = mesh_dims[0];
145 for(size_t d=1; d<spatial_dim; ++d) {
146 num_elems *= mesh_dims[d];
147 }
148
149 std::cout << "num_elems: " << num_elems << std::endl;
150
151 std::vector<double> avg_centroid(spatial_dim, 0);
152
153 start_time = stk::cpu_time();
154
155 const int num_iters = 100;
156 for(int t=0; t<num_iters; ++t) {
157
158 do_stk_gather_test(fixture.getBulkData(), avg_centroid);
159
160 //Next, divide by num_elems to turn the sum into an average,
161 //then insist that the average matches our expected value.
162 //NOTE: This error-check won't work in parallel. It is expecting
163 //the local-average centroid to be a global-average.
164
165 double tolerance = 1.e-6;
166
167 for(size_t d=0; d<spatial_dim; ++d) {
168 avg_centroid[d] /= num_elems;
169
170 double expected = mesh_dims[d]/2.0;
171 EXPECT_LT(std::abs(avg_centroid[d] - expected), tolerance);
172 }
173 }
174
175 double gather_time = stk::cpu_time() - start_time;
176 double total_time = mesh_create_time + gather_time;
177
178 static const int NUM_TIMERS = 3;
179 const double timers[NUM_TIMERS] = {mesh_create_time, gather_time, total_time};
180 const char* timer_names[NUM_TIMERS] = {"Create mesh", "Gather", "Total time"};
181
182 stk::print_timers_and_memory(timer_names, timers, NUM_TIMERS);
183
184 stk::parallel_print_time_without_output_and_hwm(MPI_COMM_WORLD, total_time);
185 }
186
187 } //namespace performance_tests
188 } //namespace stk