Debian Codegraph @

packages/stk/stk_unit_tests/stk_emend/UnitTestIndependentSet.cpp

raw
1 #include <gtest/gtest.h>
2 #include <stk_emend/independent_set/IndependentSetFinder.hpp>
3 #include <stk_util/parallel/Parallel.hpp>
4
5 #include <vector>
6
7 namespace
8 {
9
10 class TestInfo
11 {
12 public:
13 using ExclusionIdentifierType = size_t;
14 using GlobalId = size_t;
15
16 TestInfo(
17 const int owner,
18 const GlobalId priorityAndId,
19 const std::vector<ExclusionIdentifierType> &conflictingIds,
20 const std::vector<int> &procsThatNeedToKnow)
21 : mOwner{owner},
22 mId{priorityAndId},
23 mPriority{priorityAndId},
24 mTiePriority{priorityAndId},
25 mConflictingGlobalIds{conflictingIds},
26 mProcsThatNeedToKnow{procsThatNeedToKnow}
27 {}
28
29 TestInfo(
30 const int owner,
31 const GlobalId id,
32 const size_t priority,
33 const size_t tiePriority,
34 const std::vector<ExclusionIdentifierType> &conflictingIds,
35 const std::vector<int> &procsThatNeedToKnow)
36 : mOwner{owner},
37 mId{id},
38 mPriority{priority},
39 mTiePriority{tiePriority},
40 mConflictingGlobalIds{conflictingIds},
41 mProcsThatNeedToKnow{procsThatNeedToKnow}
42 {}
43
44 const GlobalId &get_unique_id() const { return mId; }
45 int get_owner() const { return mOwner; }
46 const std::vector<int> &get_procs_that_need_to_know_about_this_info() const { return mProcsThatNeedToKnow; }
47 const std::vector<ExclusionIdentifierType> &get_conflicting_ids() const { return mConflictingGlobalIds; }
48
49 class Comparator
50 {
51 public:
52 bool is_first_higher_priority_than_second(const TestInfo& a,const TestInfo& b) const
53 {
54 return a.mPriority > b.mPriority;
55 }
56
57 bool does_first_win_priority_tie_with_second(const TestInfo& a,const TestInfo& b) const
58 {
59 return a.mTiePriority > b.mTiePriority;
60 }
61 };
62
63 friend std::ostream & operator<<(std::ostream & os, const TestInfo &info);
64
65 private:
66 const int mOwner;
67 const GlobalId mId;
68 const size_t mPriority;
69 const size_t mTiePriority;
70 const std::vector<ExclusionIdentifierType> mConflictingGlobalIds;
71 const std::vector<int> mProcsThatNeedToKnow;
72 };
73
74 std::ostream & operator<<(std::ostream & os, const TestInfo &info)
75 {
76 os << "Info: mOwner: " << info.mOwner
77 << ", mId: " << info.mId
78 << ", mPriority: " << info.mPriority
79 << ", mTiePriority: " << info.mTiePriority
80 << ", mConflictingGlobalIds: {";
81 for( const TestInfo::ExclusionIdentifierType &other : info.mConflictingGlobalIds)
82 os << other << ", ";
83 os << "} mProcsThatNeedToKnow: {";
84 for( const int proc : info.mProcsThatNeedToKnow)
85 os << proc << ", ";
86 os << "}";
87 return os;
88 }
89
90 bool operator==(const TestInfo &a, const TestInfo &b)
91 {
92 return a.get_unique_id() == b.get_unique_id() &&
93 a.get_owner() == b.get_owner() &&
94 a.get_procs_that_need_to_know_about_this_info() == b.get_procs_that_need_to_know_about_this_info() &&
95 a.get_conflicting_ids() == b.get_conflicting_ids();
96 }
97
98 TEST(TestInfoOutputOperator, callIt_noUncoveredLines)
99 {
100 const TestInfo info(0, 17, {1}, {0});
101 std::ostringstream ss;
102 ss << info;
103 EXPECT_FALSE(ss.str().empty());
104 }
105
106 class TestIndependentSetFinder : public ::testing::Test
107 {
108 protected:
109 TestInfo::Comparator mComparator;
110 const stk::ParallelMachine mComm {MPI_COMM_WORLD};
111 std::vector<int> mAllProcs;
112
113 TestIndependentSetFinder()
114 {
115 for (int i{0}; i < stk::parallel_machine_size(mComm); ++i)
116 mAllProcs.push_back(i);
117 }
118
119 void test_independent_set(const std::vector<TestInfo> &infos, const std::vector<TestInfo> &goldIndependentInfos)
120 {
121 std::vector<TestInfo> independentInfos = independent_set::IndependentSetFinder<TestInfo>::find_independent_set(
122 infos,
123 mComparator,
124 mComm);
125 EXPECT_EQ(goldIndependentInfos, independentInfos);
126 }
127
128 TestInfo make_info_with_id_and_priortity_owned_on_p0_that_all_procs_need_to_know(
129 const TestInfo::GlobalId id,
130 const size_t priority,
131 const size_t tiePriority,
132 const std::vector<TestInfo::ExclusionIdentifierType> &exclusionIds)
133 {
134 return TestInfo(0, id, priority, tiePriority, exclusionIds, mAllProcs);
135 }
136
137 TestInfo make_info_owned_on_p0_that_all_procs_need_to_know(
138 const TestInfo::GlobalId id,
139 const std::vector<TestInfo::ExclusionIdentifierType> &exclusionIds)
140 {
141 return TestInfo(0, id, exclusionIds, mAllProcs);
142 }
143 };
144
145 TEST_F(TestIndependentSetFinder,noInfos_findIndependentSet_emptyIndependentSet)
146 {
147 test_independent_set({}, {});
148 }
149
150 TEST_F(TestIndependentSetFinder,oneInfo_findIndependentSet_1InfoIndependentSet)
151 {
152 std::vector<TestInfo> infos{ make_info_owned_on_p0_that_all_procs_need_to_know(17, {}) };
153 test_independent_set(infos, infos);
154 }
155
156 TEST_F(TestIndependentSetFinder,twoConflictingInfos_findIndependentSet_1InfoIndependentSet)
157 {
158 const std::vector<TestInfo> infos
159 {
160 make_info_owned_on_p0_that_all_procs_need_to_know(17, {13}),
161 make_info_owned_on_p0_that_all_procs_need_to_know(18, {13}),
162 };
163 test_independent_set(infos, {infos[1]});
164 }
165
166 TEST_F(TestIndependentSetFinder,twoNonConflictingInfos_findIndependentSet_2InfoIndependentSet)
167 {
168 const std::vector<TestInfo> infos
169 {
170 make_info_owned_on_p0_that_all_procs_need_to_know(19, {}),
171 make_info_owned_on_p0_that_all_procs_need_to_know(20, {}),
172 };
173 test_independent_set(infos, infos);
174 }
175
176 TEST_F(TestIndependentSetFinder,threeConflictingInfos_findIndependentSet_1InfoIndependentSet)
177 {
178 const std::vector<TestInfo> infos
179 {
180 make_info_owned_on_p0_that_all_procs_need_to_know(21, {1}),
181 make_info_owned_on_p0_that_all_procs_need_to_know(22, {1,2}),
182 make_info_owned_on_p0_that_all_procs_need_to_know(23, {2}),
183 };
184 test_independent_set(infos, {infos[2]});
185 }
186
187 TEST_F(TestIndependentSetFinder,threeConflictingInfosBut2LowestPrioritiesAreTied_findIndependentSet_2InfoIndependentSetIncludingTheHighestPriorityAndTheLowestThatDoesntConflictWithHighest)
188 {
189 const std::vector<TestInfo> infos
190 {
191 make_info_with_id_and_priortity_owned_on_p0_that_all_procs_need_to_know(21, 1, 8, {1}),
192 make_info_with_id_and_priortity_owned_on_p0_that_all_procs_need_to_know(22, 1, 9, {1,2}),
193 make_info_with_id_and_priortity_owned_on_p0_that_all_procs_need_to_know(23, 2, 0, {2}),
194 };
195 test_independent_set(infos, {infos[0], infos[2]});
196 }
197
198 TEST_F(TestIndependentSetFinder,twoTiedInfosThatDontConflictWithHigherPriorityInfosAnd2ndWinsTie_findIndependentSet_choose2ndWithTieBreaker)
199 {
200 const std::vector<TestInfo> infos
201 {
202 make_info_with_id_and_priortity_owned_on_p0_that_all_procs_need_to_know(20, 1, 7, {1}),
203 make_info_with_id_and_priortity_owned_on_p0_that_all_procs_need_to_know(21, 1, 8, {1}),
204 make_info_with_id_and_priortity_owned_on_p0_that_all_procs_need_to_know(22, 1, 9, {1,2}),
205 make_info_with_id_and_priortity_owned_on_p0_that_all_procs_need_to_know(23, 2, 0, {2}),
206 };
207 test_independent_set(infos, {infos[1], infos[3]});
208 }
209
210 TEST_F(TestIndependentSetFinder,twoTiedInfosThatDontConflictWithHigherPriorityInfosAnd1stWinsTie_findIndependentSet_choose1stWithTieBreaker)
211 {
212 const std::vector<TestInfo> infos
213 {
214 make_info_with_id_and_priortity_owned_on_p0_that_all_procs_need_to_know(20, 1, 8, {1}),
215 make_info_with_id_and_priortity_owned_on_p0_that_all_procs_need_to_know(21, 1, 7, {1}),
216 make_info_with_id_and_priortity_owned_on_p0_that_all_procs_need_to_know(22, 1, 9, {1,2}),
217 make_info_with_id_and_priortity_owned_on_p0_that_all_procs_need_to_know(23, 2, 0, {2}),
218 };
219 test_independent_set(infos, {infos[0], infos[3]});
220 }
221
222 TEST_F(TestIndependentSetFinder,FourChainedConflictingInfosWithBottom3TiedAndAlternatingTieBreaker_find_independentSet_ChooseTieBreaker)
223 {
224 const std::vector<TestInfo> infos
225 {
226 make_info_with_id_and_priortity_owned_on_p0_that_all_procs_need_to_know(20, 99, 15, {1}),
227 make_info_with_id_and_priortity_owned_on_p0_that_all_procs_need_to_know(21, 99, 10, {1,2}),
228 make_info_with_id_and_priortity_owned_on_p0_that_all_procs_need_to_know(22, 99, 20, {2,3}),
229 make_info_with_id_and_priortity_owned_on_p0_that_all_procs_need_to_know(23, 100, 0, {3}),
230 };
231 test_independent_set(infos, {infos[0], infos[3]});
232 }
233
234 TEST_F(TestIndependentSetFinder,SplitInfoChainWhereOneBranchIsHigherAndOtherLowerPriorityThanJoint_findIndependentSet_bothBranchesChosen)
235 {
236 const std::vector<TestInfo> infos
237 {
238 make_info_with_id_and_priortity_owned_on_p0_that_all_procs_need_to_know(19, 99, 5, {1}),
239 make_info_with_id_and_priortity_owned_on_p0_that_all_procs_need_to_know(20, 99, 15, {2}),
240 make_info_with_id_and_priortity_owned_on_p0_that_all_procs_need_to_know(21, 99, 10, {1,2,3}),
241 make_info_with_id_and_priortity_owned_on_p0_that_all_procs_need_to_know(22, 99, 20, {3,4}),
242 make_info_with_id_and_priortity_owned_on_p0_that_all_procs_need_to_know(23, 100, 0, {4})
243 };
244 test_independent_set(infos, {infos[0], infos[1], infos[4]});
245 }
246
247 TEST_F(TestIndependentSetFinder,SplitInfoChainWhereOneBranchIsHigherPriorityAndOtherChainOnOtherProcIsLowerPriorityThanJoint_findIndependentSet_bothBranchesChosen)
248 {
249 if(stk::parallel_machine_size(mComm) >= 2)
250 {
251 const std::vector<TestInfo> infos
252 {
253 TestInfo(1, 19, 99, 5, {1}, mAllProcs),
254 make_info_with_id_and_priortity_owned_on_p0_that_all_procs_need_to_know(20, 99, 15, {2}),
255 make_info_with_id_and_priortity_owned_on_p0_that_all_procs_need_to_know(21, 99, 10, {1,2,3}),
256 make_info_with_id_and_priortity_owned_on_p0_that_all_procs_need_to_know(22, 99, 20, {3,4}),
257 make_info_with_id_and_priortity_owned_on_p0_that_all_procs_need_to_know(23, 100, 0, {4})
258 };
259 test_independent_set(infos, {infos[0], infos[1], infos[4]});
260 }
261 }
262
263 TEST_F(TestIndependentSetFinder,TwoChainsThatMeetAtLowestPriorityTiedInfo_findIndependentSet_lowestInfoInAfterResolvingBothChains)
264 {
265 const std::vector<TestInfo> infos
266 {
267 make_info_with_id_and_priortity_owned_on_p0_that_all_procs_need_to_know( 19, 100, 5, {1}),
268 make_info_with_id_and_priortity_owned_on_p0_that_all_procs_need_to_know(119, 100, 5, {101}),
269 make_info_with_id_and_priortity_owned_on_p0_that_all_procs_need_to_know( 20, 99, 5, {1,2}),
270 make_info_with_id_and_priortity_owned_on_p0_that_all_procs_need_to_know(120, 99, 5, {101,102}),
271 make_info_with_id_and_priortity_owned_on_p0_that_all_procs_need_to_know( 1, 99, 1, {2,102}),
272 };
273 test_independent_set(infos, {infos[0], infos[1], infos[4]});
274 }
275
276 TEST_F(TestIndependentSetFinder,threePartiallyConflictingInfos_findIndependentSet_2InfoIndependentSet)
277 {
278 const std::vector<TestInfo> infos
279 {
280 make_info_owned_on_p0_that_all_procs_need_to_know(21, {1,2}),
281 make_info_owned_on_p0_that_all_procs_need_to_know(22, {1}),
282 make_info_owned_on_p0_that_all_procs_need_to_know(23, {2}),
283 };
284 test_independent_set(infos, {infos[1], infos[2]});
285 }
286
287 TEST_F(TestIndependentSetFinder,twoConflictingInfosOnDifferentProcs_findIndependentSet_1InfoIndependentSet)
288 {
289 if(stk::parallel_machine_size(mComm) >= 2)
290 {
291 const std::vector<TestInfo> infos
292 {
293 {0, 17, {13}, mAllProcs},
294 {1, 18, {13}, mAllProcs},
295 };
296 test_independent_set(infos, {infos[1]});
297 }
298 }
299
300 TEST_F(TestIndependentSetFinder,firstInfoIsBetterThanOneNeighborAndLosesTieToAnOutNeighbor_findIndependentSet_firstInfoShouldBeIn)
301 {
302 const std::vector<TestInfo> infos
303 {
304 make_info_with_id_and_priortity_owned_on_p0_that_all_procs_need_to_know( 1, 10, 1, {1,2}),
305 make_info_with_id_and_priortity_owned_on_p0_that_all_procs_need_to_know( 2, 9, 2, {1}),
306 make_info_with_id_and_priortity_owned_on_p0_that_all_procs_need_to_know( 3, 10, 2, {2,3}),
307 make_info_with_id_and_priortity_owned_on_p0_that_all_procs_need_to_know( 4, 11, 0, {3}),
308 };
309 test_independent_set(infos, {infos[0], infos[3]});
310 }
311
312 class TestIsBetterOrWinsTieInIndependentSet : public TestIndependentSetFinder
313 {
314 protected:
315 void test_is_better_or_wins_tie_with_unknown_neighbor(
316 bool isInfoTiedWithNeighbor,
317 bool doesInfoWinTieOverNeighbor,
318 bool isNeighborUnknown,
319 bool goldIsBetter)
320 {
321 const size_t infoPriority = 10;
322 const size_t neighborInfoPriority = isInfoTiedWithNeighbor ? infoPriority : infoPriority-1;
323 const size_t infoTiedPriority = 10;
324 const size_t neighborInfoTiedPriority = doesInfoWinTieOverNeighbor ? infoTiedPriority-1 : infoTiedPriority+1;
325 const independent_set::IndependentSetFinder<TestInfo>::InOutUnknownStatus neighborStatus =
326 isNeighborUnknown ?
327 independent_set::IndependentSetFinder<TestInfo>::UNKNOWN_STATUS :
328 independent_set::IndependentSetFinder<TestInfo>::OUT;
329 const std::vector<TestInfo> infos
330 {
331 make_info_with_id_and_priortity_owned_on_p0_that_all_procs_need_to_know( 1, infoPriority, infoTiedPriority, {1}),
332 make_info_with_id_and_priortity_owned_on_p0_that_all_procs_need_to_know( 2, neighborInfoPriority, neighborInfoTiedPriority, {1}),
333 };
334
335 const std::vector<independent_set::IndependentSetFinder<TestInfo>::InOutUnknownStatus> infoInOutStatus
336 {
337 independent_set::IndependentSetFinder<TestInfo>::UNKNOWN_STATUS,
338 neighborStatus
339 };
340
341 const independent_set::IndependentSetInfoToInfoGraph<TestInfo> conflictingInfoGraph{infos};
342 bool isBetter = independent_set::IndependentSetFinder<TestInfo>::is_better_than_neighbors_or_wins_tie_breaker_with_unknown_neighbors(
343 0,
344 infos,
345 mComparator,
346 conflictingInfoGraph,
347 infoInOutStatus);
348 EXPECT_EQ(goldIsBetter, isBetter);
349 }
350
351 const bool infoIsTiedWithNeighbor{true};
352 const bool infoIsBetterThanNeighbor{false};
353 const bool infoWinsTieOverNeighbor{true};
354 const bool infoLosesTieOverNeighbor{false};
355 const bool neighborIsUnknown{true};
356 const bool neighborIsOut{false};
357 };
358
359 TEST_F(TestIsBetterOrWinsTieInIndependentSet,infoThatIsBetterThanNeighborButWouldLoseTieBreakerIfChecked_isBetterOrWinsTie_true)
360 {
361 test_is_better_or_wins_tie_with_unknown_neighbor(infoIsBetterThanNeighbor, infoLosesTieOverNeighbor, neighborIsUnknown, true);
362 }
363
364 TEST_F(TestIsBetterOrWinsTieInIndependentSet,infoThatIsTiedAndWouldLoseTieBreakerWithUnknownNeighbor_isBetterOrWinsTie_false)
365 {
366 test_is_better_or_wins_tie_with_unknown_neighbor(infoIsTiedWithNeighbor, infoLosesTieOverNeighbor, neighborIsUnknown, false);
367 }
368
369 TEST_F(TestIsBetterOrWinsTieInIndependentSet,infoThatIsTiedAndWouldWinTieBreakerWithUnknownNeighbor_isBetterOrWinsTie_true)
370 {
371 test_is_better_or_wins_tie_with_unknown_neighbor(infoIsTiedWithNeighbor, infoWinsTieOverNeighbor, neighborIsUnknown, true);
372 }
373
374 TEST_F(TestIsBetterOrWinsTieInIndependentSet,infoThatIsTiedAndWouldLoseTieBreakerWithOutNeighbor_isBetterOrWinsTie_true)
375 {
376 test_is_better_or_wins_tie_with_unknown_neighbor(infoIsTiedWithNeighbor, infoLosesTieOverNeighbor, neighborIsOut, true);
377 }
378 }