Debian Codegraph @

packages/stk/stk_unit_tests/stk_util/parallel/UnitTestGenerateUniqueConsistentIDs.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/parallel/GenerateParallelUniqueIDs.hpp" // for generate_parallel_unique_ids
37 #include "stk_util/parallel/Parallel.hpp" // for MPI_COMM_WORLD, MPI_Allreduce
38 #include "stk_util/parallel/ParallelVectorConcat.hpp" // for parallel_vector_concat
39 #include <algorithm> // for binary_search, sort
40 #include <cstdint> // for uint64_t
41 #include <vector> // for vector
42
43 //-------------------------------------------------------------------------------------------------------------------
44 // Verify that a result generated from the unique ids function meets all properties nessecary of unique ids
45 void VerifyUniqueIds(uint64_t maxAllowableId,
46 std::vector<uint64_t>& existingIdsLocal,
47 std::vector<uint64_t>& newIdsLocal,
48 stk::ParallelMachine comm) {
49 //
50 // Generate a global list of all ids created matched and all ids that existed perviously
51 //
52 std::vector<uint64_t> existingIdsGlobal;
53 stk::parallel_vector_concat(comm, existingIdsLocal, existingIdsGlobal);
54 std::sort(existingIdsGlobal.begin(), existingIdsGlobal.end());
55
56 std::vector<uint64_t> newIdsGlobal;
57 stk::parallel_vector_concat(comm, newIdsLocal, newIdsGlobal);
58
59
60 //
61 // Verify properties of the returned ids
62 //
63 for(uint64_t i=0; i<newIdsGlobal.size(); ++i) {
64 uint64_t curNewID = newIdsGlobal[i];
65 // 1) Verify all ids must be less than or equal to maxAllowableId
66 EXPECT_LE(curNewID, maxAllowableId);
67 // 2) Verify no overlap between existing and new id set
68 EXPECT_FALSE(std::binary_search(existingIdsGlobal.begin(), existingIdsGlobal.end(), curNewID));
69 }
70
71 }
72
73 //------------------------------------------------------------------------------
74 //
75 // Test the generate_parallel_unique_ids function in stk_util/parallel
76 //
77 TEST(UnitTestParallel, testGenerateParallelUniqueIDs) {
78 int mpi_rank = stk::parallel_machine_rank(MPI_COMM_WORLD);
79 int mpi_size = stk::parallel_machine_size(MPI_COMM_WORLD);
80 //
81 // Test 1, nominal element death case
82 // Each processor starts with 1000 unique densly packed ids ordered 1->N. Add 10 new ids per processor
83 // produced by kill every 100th element creating 10 new elements per processor.
84 //
85 uint64_t maxAllowableId = ~0U;
86
87 {
88 std::vector<uint64_t> newIds;
89 std::vector<uint64_t> existingIds(1000);
90 uint64_t firstId = mpi_rank*1000;
91
92 for(uint64_t i=0; i<1000; ++i) {
93 uint64_t currentElemId = firstId+i;
94 existingIds[i] = currentElemId;
95 }
96 newIds = stk::generate_parallel_unique_ids(maxAllowableId, existingIds, 10, MPI_COMM_WORLD);
97
98 uint64_t localSize = newIds.size();
99 uint64_t globalSize;
100 MPI_Allreduce(&localSize, &globalSize, 1, MPI_UNSIGNED_LONG, MPI_SUM, MPI_COMM_WORLD);
101 EXPECT_EQ(globalSize, (uint64_t)mpi_size*10);
102
103 VerifyUniqueIds(maxAllowableId, existingIds, newIds, MPI_COMM_WORLD);
104 }
105 //
106 // Test 2, same total source data as case 1, but source data scattered among processors
107 //
108 {
109 int numRequest=0;
110 std::vector<uint64_t> newIds;
111 std::vector<uint64_t> existingIds;
112 uint64_t destProc = mpi_size/2;
113 for(int i=0; i<1000*mpi_size; ++i) {
114 destProc += i;
115 if(destProc >= (uint64_t)mpi_size) {
116 destProc = destProc%mpi_size;
117 }
118 if(destProc == (uint64_t)mpi_rank) {
119 existingIds.push_back(i);
120 if(i%100 == 0) {
121 numRequest++;
122 }
123 }
124 }
125 newIds = stk::generate_parallel_unique_ids(maxAllowableId, existingIds, numRequest, MPI_COMM_WORLD);
126
127 uint64_t localSize = newIds.size();
128 uint64_t globalSize;
129 MPI_Allreduce(&localSize, &globalSize, 1, MPI_UNSIGNED_LONG, MPI_SUM, MPI_COMM_WORLD);
130 EXPECT_EQ(globalSize, (uint64_t)mpi_size*10);
131
132 VerifyUniqueIds(maxAllowableId, existingIds, newIds, MPI_COMM_WORLD);
133 }
134 //
135 // Test 2.5, same as case one, except all new ids to be created on one processor
136 //
137 {
138 int numRequest=0;
139 std::vector<uint64_t> newIds;
140 std::vector<uint64_t> existingIds;
141 uint64_t destProc = mpi_size/2;
142 for(int i=0; i<1000*mpi_size; ++i) {
143 destProc += i;
144 if(destProc >= (uint64_t)mpi_size) {
145 destProc = destProc%mpi_size;
146 }
147 if(destProc == (uint64_t)mpi_rank) {
148 existingIds.push_back(i);
149 }
150 }
151
152 if(mpi_rank == mpi_size/2) {
153 numRequest = mpi_size*10;
154 }
155
156
157 newIds = stk::generate_parallel_unique_ids(maxAllowableId, existingIds, numRequest, MPI_COMM_WORLD);
158
159 uint64_t localSize = newIds.size();
160 uint64_t globalSize;
161 MPI_Allreduce(&localSize, &globalSize, 1, MPI_UNSIGNED_LONG, MPI_SUM, MPI_COMM_WORLD);
162 EXPECT_EQ(globalSize, (uint64_t)mpi_size*10);
163
164 VerifyUniqueIds(maxAllowableId, existingIds, newIds, MPI_COMM_WORLD);
165 }
166
167 //
168 // Test 3, sparse fill
169 //
170
171 {
172 std::vector<uint64_t> newIds;
173 std::vector<uint64_t> existingIds(1000);
174 uint64_t firstId = mpi_rank*1000;
175
176 for(uint64_t i=0; i<1000; ++i) {
177 uint64_t currentElemId = firstId+i;
178 existingIds[i] = currentElemId;
179 }
180
181 if(mpi_rank == 0) {
182 existingIds.push_back(~0U);
183 }
184
185 newIds = stk::generate_parallel_unique_ids(maxAllowableId, existingIds, 10, MPI_COMM_WORLD);
186
187 uint64_t localSize = newIds.size();
188 uint64_t globalSize;
189 MPI_Allreduce(&localSize, &globalSize, 1, MPI_UNSIGNED_LONG, MPI_SUM, MPI_COMM_WORLD);
190 EXPECT_EQ(globalSize, (uint64_t)mpi_size*10);
191
192 VerifyUniqueIds(maxAllowableId, existingIds, newIds, MPI_COMM_WORLD);
193 }
194
195
196 }