Debian Codegraph @

packages/stk/stk_unit_tests/stk_util/parallel/UnitTestParallelVectorConcat.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_util/diag/String.hpp" // for String
37 #include "stk_util/parallel/Parallel.hpp" // for MPI_COMM_WORLD, MPI_SUCCESS, paral...
38 #include "stk_util/parallel/ParallelVectorConcat.hpp" // for parallel_vector_concat
39 #include <iostream> // for operator<<, ostringstream, basic_o...
40 #include <memory> // for allocator_traits<>::value_type
41 #include <string> // for string, basic_string, char_traits
42 #include <vector> // for vector
43
44
45 class TestStruct {
46 public:
47 unsigned int data1;
48 double data2;
49 double data3;
50 };
51
52
53 TEST(UnitTestParallel, testParallelVectorConcat)
54 {
55 int mpi_rank = stk::parallel_machine_rank(MPI_COMM_WORLD);
56 int mpi_size = stk::parallel_machine_size(MPI_COMM_WORLD);
57 //
58 // Test 1, concat integer lists, each process P creates n-1 copies of P thus on 6 processors the
59 // expected concat list is:
60 //
61 // 2 3 3 4 4 4 5 5 5 5
62 //
63 {
64 std::vector<int> localIntList;
65 for(int i=0; i<mpi_rank-1; ++i) {
66 localIntList.push_back(mpi_rank);
67 }
68 std::vector<int> globalIntList;
69 int status = stk::parallel_vector_concat(MPI_COMM_WORLD, localIntList, globalIntList);
70 EXPECT_EQ(status, MPI_SUCCESS);
71 std::vector<int> expectedIntList;
72 for(int iproc=0; iproc<mpi_size; ++iproc) {
73 for(int i=0; i<iproc-1; ++i) {
74 expectedIntList.push_back(iproc);
75 }
76 }
77 for(unsigned int i=0; i<expectedIntList.size(); ++i) {
78 EXPECT_EQ(globalIntList[i], expectedIntList[i]);
79 }
80 }
81 //
82 // Test 2, concat a bit more complex data struct with two reals and an int, same general format
83 // as before
84 //
85 {
86 std::vector<TestStruct> localStructList;
87 std::vector<TestStruct> expectedStructList;
88 for(int iproc=0; iproc<mpi_size; ++iproc) {
89 for(int i=0; i<mpi_size; ++i) {
90 TestStruct ts;
91 ts.data1 = iproc;
92 ts.data2 = 1.23;
93 ts.data3 = 1.0/i;
94 if(iproc == mpi_rank) {
95 localStructList.push_back(ts);
96 }
97 expectedStructList.push_back(ts);
98 }
99 }
100 std::vector<TestStruct> globalStructList;
101 int status = stk::parallel_vector_concat(MPI_COMM_WORLD, localStructList, globalStructList);
102 EXPECT_EQ(status, MPI_SUCCESS);
103 for(unsigned int i=0; i<expectedStructList.size(); ++i) {
104 EXPECT_EQ(globalStructList[i].data1, expectedStructList[i].data1);
105 EXPECT_EQ(globalStructList[i].data2, expectedStructList[i].data2);
106 EXPECT_EQ(globalStructList[i].data3, expectedStructList[i].data3);
107 }
108 }
109 //
110 // Test 3, boundary case, check for handling of empty list.
111 //
112 {
113 std::vector<char> localCharList;
114 std::vector<char> globalCharList(10, 'a');
115 int status = stk::parallel_vector_concat(MPI_COMM_WORLD, localCharList, globalCharList);
116 EXPECT_EQ(status, MPI_SUCCESS);
117 EXPECT_EQ(globalCharList.size(), (unsigned)0);
118 }
119 //
120 // Test 4, test std::string specialization. Net string should have form:
121 // 0: "PROC |1|"
122 // 1: "PROC |2|"
123 // 2: "PROC |2|"
124 // 3: "PROC |3|"
125 // 4: "PROC |3|"
126 // 5: "PROC |3|"
127 //
128 {
129 std::vector<std::string> localStringList;
130 std::vector<std::string> globalStringList;
131 std::vector<std::string> expectedStringList;
132 for(int iproc=0; iproc<mpi_size; ++iproc) {
133 for(int i=0; i<iproc; ++i) {
134 std::ostringstream os;
135 os <<"PROC |"<<iproc<<"|";
136 if(iproc == mpi_rank) {
137 localStringList.push_back(os.str());
138 }
139 expectedStringList.push_back(os.str());
140 }
141 }
142
143 int status = stk::parallel_vector_concat(MPI_COMM_WORLD, localStringList, globalStringList);
144
145 EXPECT_EQ(status, MPI_SUCCESS);
146
147 EXPECT_EQ(globalStringList.size(), expectedStringList.size());
148
149
150 for(unsigned int i=0; i<expectedStringList.size(); ++i) {
151 EXPECT_EQ(globalStringList[i], expectedStringList[i]);
152 }
153 }
154 //
155 // Test 5, test sierra::String specialization. Net string should have form:
156 // 0: "PROC |1|"
157 // 1: "PROC |2|"
158 // 2: "PROC |2|"
159 // 3: "PROC |3|"
160 // 4: "PROC |3|"
161 // 5: "PROC |3|"
162 //
163 {
164 std::vector<sierra::String> localStringList;
165 std::vector<sierra::String> globalStringList;
166 std::vector<sierra::String> expectedStringList;
167 for(int iproc=0; iproc<mpi_size; ++iproc) {
168 for(int i=0; i<iproc; ++i) {
169 std::ostringstream os;
170 os <<"PROC |"<<iproc<<"|";
171 if(iproc == mpi_rank) {
172 localStringList.push_back(os.str().c_str());
173 }
174 expectedStringList.push_back(os.str().c_str());
175 }
176 }
177
178 int status = stk::parallel_vector_concat(MPI_COMM_WORLD, localStringList, globalStringList);
179
180 EXPECT_EQ(status, MPI_SUCCESS);
181
182 EXPECT_EQ(globalStringList.size(), expectedStringList.size());
183
184
185 for(unsigned int i=0; i<expectedStringList.size(); ++i) {
186 EXPECT_EQ(globalStringList[i], expectedStringList[i]);
187 }
188 }
189 }
190
191 TEST(UnitTestParallel, test_bool_ParallelVectorConcat)
192 {
193 int mpi_rank = stk::parallel_machine_rank(MPI_COMM_WORLD);
194 int mpi_size = stk::parallel_machine_size(MPI_COMM_WORLD);
195 //
196 // concat bool lists, each process P creates P-1 bools
197 //
198 {
199 std::vector<bool> localVec;
200 for(int i=0; i<mpi_rank-1; ++i) {
201 const bool val = i%2==0 ? true : false;
202 localVec.push_back(val);
203 }
204 std::vector<bool> globalVec;
205 int status = stk::parallel_vector_concat(MPI_COMM_WORLD, localVec, globalVec);
206 EXPECT_EQ(status, MPI_SUCCESS);
207 std::vector<bool> expectedVec;
208 for(int iproc=0; iproc<mpi_size; ++iproc) {
209 for(int i=0; i<iproc-1; ++i) {
210 const bool val = i%2==0 ? true : false;
211 expectedVec.push_back(val);
212 }
213 }
214 for(unsigned int i=0; i<expectedVec.size(); ++i) {
215 EXPECT_EQ(globalVec[i], expectedVec[i]);
216 }
217 }
218 }
219