19 #include <unordered_set>
23 #include <Teuchos_Comm.hpp>
24 #include <Teuchos_DefaultComm.hpp>
30 static const char*
name() {
31 std::cout <<
"You are missing a DECL_TYPE_NAME" << std::endl;
36 #define DECL_TYPE_NAME(x) \
37 template<> struct type_name<x> { static const char* name() {return #x;} }
45 static const std::string
fail =
"FAIL ";
46 static const std::string
pass =
" ";
49 void checkNUnique(std::string &name,
size_t nUniqueGids,
size_t nExpected)
51 if (nUniqueGids != nExpected)
52 std::cout <<
fail << name
53 <<
"nUniqueGids " << nUniqueGids <<
" != " << nExpected
58 template <
typename gno_t>
61 std::vector<gno_t> &gids,
63 const Teuchos::Comm<int> &comm
66 gno_t maxGid = 0, gmaxGid = 0;
67 size_t len = gids.size();
68 for (
size_t i = 0; i < len; i++)
69 if (gids[i] > maxGid) maxGid = gids[i];
71 Teuchos::reduceAll<int, gno_t>(comm, Teuchos::REDUCE_MAX, 1,
73 if (gmaxGid != maxExpected)
74 std::cout <<
fail << name
75 <<
"max Gid " << gmaxGid <<
" != " << maxExpected
80 template <
typename gno_t>
83 std::vector<gno_t> &gids,
85 const Teuchos::Comm<int> &comm
88 gno_t minGid = std::numeric_limits<gno_t>::max(), gminGid;
89 size_t len = gids.size();
90 for (
size_t i = 0; i < len; i++)
91 if (gids[i] < minGid) minGid = gids[i];
93 Teuchos::reduceAll<int, gno_t>(comm, Teuchos::REDUCE_MIN, 1,
95 if (gminGid != minExpected)
96 std::cout <<
fail << name
97 <<
"min Gid " << gminGid <<
" != " << minExpected
102 template <
typename gno_t>
105 std::vector<gno_t> &gids,
108 size_t gidsLen = gids.size();
109 std::unordered_set<gno_t> gidsSet(gidsLen);
112 for (
size_t i = 0; i < gidsLen; i++) {
113 if (gidsSet.find(gids[i]) != gidsSet.end()) {
118 gidsSet.insert(gids[i]);
120 size_t nUnique = gidsLen - nDups;
121 if (nUnique != nExpected)
122 std::cout <<
fail << name
123 <<
"num locally unique Gids " << nUnique <<
" != " << nExpected
129 template <
typename gno_t>
130 void test1(Teuchos::RCP<
const Teuchos::Comm<int> > &comm)
136 int me = comm->getRank();
137 int np = comm->getSize();
139 std::string name = std::string(
" test1: ")
141 if (me == 0) std::cout <<
"--------\n Starting " << name << std::endl;
143 typedef std::array<gno_t, 1> zkey_t;
144 typedef std::vector<zkey_t> keyvec_t;
145 typedef std::vector<gno_t> gidvec_t;
147 const size_t nKeys = me+1;
148 keyvec_t keys(nKeys);
149 gidvec_t gids(nKeys);
151 for (
size_t i = 0; i < nKeys; i++) {
157 size_t nUniqueGids = Zoltan2::findUniqueGids<zkey_t, gno_t>(keys,gids,*comm);
161 std::cout <<
" " << name <<
" nUniqueGids " << nUniqueGids << std::endl;
174 template <
typename gno_t>
175 void test2(Teuchos::RCP<
const Teuchos::Comm<int> > &comm)
183 int me = comm->getRank();
184 int np = comm->getSize();
186 std::string name = std::string(
" test2: ")
188 if (me == 0) std::cout <<
"--------\n Starting " << name << std::endl;
190 typedef std::array<gno_t, 2> zkey_t;
191 typedef std::vector<zkey_t> keyvec_t;
192 typedef std::vector<gno_t> gidvec_t;
194 const size_t nKeys = 6;
195 const size_t nKeysHalf = 3;
196 keyvec_t keys(nKeys);
197 gidvec_t gids(nKeys);
199 for (
size_t i = 0; i < nKeysHalf; i++) {
205 for (
size_t i = 0; i < nKeysHalf; i++) {
207 k[0] =
gno_t((me+i+1)%np);
209 keys[i+nKeysHalf] = k;
212 size_t nUniqueGids = Zoltan2::findUniqueGids<zkey_t,gno_t>(keys,gids,*comm);
216 std::cout <<
" " << name <<
" nUniqueGids " << nUniqueGids << std::endl;
227 template <
typename gno_t>
228 void test3(Teuchos::RCP<
const Teuchos::Comm<int> > &comm)
237 int me = comm->getRank();
238 int np = comm->getSize();
240 std::string name = std::string(
" test3: ")
242 if (me == 0) std::cout <<
"--------\n Starting " << name << std::endl;
244 typedef std::array<gno_t, 3> zkey_t;
245 typedef std::vector<zkey_t> keyvec_t;
246 typedef std::vector<gno_t> gidvec_t;
248 const size_t nKeys = 2*np;
249 const size_t nKeysHalf = np;
250 keyvec_t keys(nKeys);
251 gidvec_t gids(nKeys);
253 for (
size_t i = 0; i < nKeysHalf; i++) {
258 keys[i+nKeysHalf] = k;
260 for (
size_t i = 0; i < nKeysHalf; i++) {
268 size_t nUniqueGids = Zoltan2::findUniqueGids<zkey_t,gno_t>(keys,gids,*comm);
278 std::cout <<
" " << name <<
" nUniqueGids " << nUniqueGids << std::endl;
291 template <
typename gno_t>
292 void test4(Teuchos::RCP<
const Teuchos::Comm<int> > &comm)
298 int me = comm->getRank();
300 std::string name = std::string(
" test4: ")
302 if (me == 0) std::cout <<
"--------\n Starting " << name << std::endl;
304 typedef std::array<gno_t, 4> zkey_t;
305 typedef std::vector<zkey_t> keyvec_t;
306 typedef std::vector<gno_t> gidvec_t;
308 const size_t nKeys = (me+1)%2;
309 keyvec_t keys(nKeys);
310 gidvec_t gids(nKeys);
312 for (
size_t i = 0; i < nKeys; i++) {
321 size_t nUniqueGids = Zoltan2::findUniqueGids<zkey_t,gno_t>(keys,gids,*comm);
325 std::cout <<
" " << name <<
" nUniqueGids " << nUniqueGids << std::endl;
338 template <
typename gno_t>
339 void test5(Teuchos::RCP<
const Teuchos::Comm<int> > &comm)
348 int me = comm->getRank();
349 int np = comm->getSize();
351 std::string name = std::string(
" test5: ")
353 if (me == 0) std::cout <<
"--------\n Starting " << name << std::endl;
357 const size_t nVecs = 3;
358 const size_t nKeys = 2*np;
359 const size_t nKeysHalf = np;
362 Teuchos::OrdinalTraits<Tpetra::global_size_t>::invalid();
364 typedef Tpetra::Map<lno_t, gno_t>
map_t;
365 Teuchos::RCP<const map_t> map = rcp(
new map_t(gNEntries, nKeys, 0, comm),
368 Tpetra::MultiVector<gno_t, lno_t, gno_t> keys(map, nVecs);
369 Tpetra::Vector<gno_t, lno_t, gno_t> gids(map);
371 for (
size_t i = 0; i < nKeysHalf; i++) {
372 keys.replaceLocalValue(i+nKeysHalf, 0,
gno_t(me));
373 keys.replaceLocalValue(i+nKeysHalf, 1,
gno_t(me));
374 keys.replaceLocalValue(i+nKeysHalf, 2,
gno_t(i));
376 for (
size_t i = 0; i < nKeysHalf; i++) {
377 keys.replaceLocalValue(i, 0,
gno_t(i));
378 keys.replaceLocalValue(i, 1,
gno_t(i));
379 keys.replaceLocalValue(i, 2,
gno_t(i));
382 size_t nUniqueGids = Zoltan2::findUniqueGids<lno_t,gno_t>(keys,gids);
386 std::cout <<
" " << name <<
" nUniqueGids " << nUniqueGids << std::endl;
390 Teuchos::ArrayRCP<const gno_t> gidsData = gids.getData();
391 std::vector<gno_t> gidsVec(nKeys);
392 for (
size_t i = 0; i < nKeys; i++) gidsVec[i] = gidsData[i];
403 template <
typename gno_t>
404 void test6(Teuchos::RCP<
const Teuchos::Comm<int> > &comm)
410 int me = comm->getRank();
412 std::string name = std::string(
" test6: ")
414 if (me == 0) std::cout <<
"--------\n Starting " << name << std::endl;
418 const size_t nVecs = 4;
419 const size_t nKeys = (me+1)%2;
422 Teuchos::OrdinalTraits<Tpetra::global_size_t>::invalid();
424 typedef Tpetra::Map<lno_t, gno_t>
map_t;
425 Teuchos::RCP<const map_t> map = rcp(
new map_t(gNEntries, nKeys, 0, comm),
428 Tpetra::MultiVector<gno_t, lno_t, gno_t> keys(map, nVecs);
429 Tpetra::Vector<gno_t, lno_t, gno_t> gids(map);
431 for (
size_t i = 0; i < nKeys; i++) {
432 keys.replaceLocalValue(i, 0,
gno_t(0));
433 keys.replaceLocalValue(i, 1,
gno_t(1));
434 keys.replaceLocalValue(i, 2,
gno_t(2));
435 keys.replaceLocalValue(i, 3,
gno_t(3));
438 size_t nUniqueGids = Zoltan2::findUniqueGids<lno_t,gno_t>(keys,gids);
442 std::cout <<
" " << name <<
" nUniqueGids " << nUniqueGids << std::endl;
446 Teuchos::ArrayRCP<const gno_t> gidsData = gids.getData();
447 std::vector<gno_t> gidsVec(nKeys);
448 for (
size_t i = 0; i < nKeys; i++) gidsVec[i] = gidsData[i];
459 int main(
int narg,
char *arg[])
461 Tpetra::ScopeGuard tscope(&narg, &arg);
462 Teuchos::RCP<const Teuchos::Comm<int> > comm = Tpetra::getDefaultComm();
464 #ifdef HAVE_TPETRA_INT_INT
472 if (comm->getRank() == 0)
473 std::cout <<
"Skipping int tests because Tpetra is not build with "
474 <<
"GO == int" << std::endl;
477 #ifdef HAVE_TPETRA_INT_LONG
485 if (comm->getRank() == 0)
486 std::cout <<
"Skipping long tests because Tpetra is not build with "
487 <<
"GO == long " << std::endl;
490 #ifdef HAVE_TPETRA_INT_LONG_LONG
491 test1<long long>(comm);
492 test2<long long>(comm);
493 test3<long long>(comm);
494 test4<long long>(comm);
495 test5<long long>(comm);
496 test6<long long>(comm);
498 if (comm->getRank() == 0)
499 std::cout <<
"Skipping long long tests because Tpetra is not build with "
500 <<
"GO == long long" << std::endl;
void checkNUnique(std::string &name, size_t nUniqueGids, size_t nExpected)
void test3(Teuchos::RCP< const Teuchos::Comm< int > > &comm)
void test6(Teuchos::RCP< const Teuchos::Comm< int > > &comm)
map_t::global_ordinal_type gno_t
void test2(Teuchos::RCP< const Teuchos::Comm< int > > &comm)
void test5(Teuchos::RCP< const Teuchos::Comm< int > > &comm)
int main(int narg, char **arg)
static const char * name()
map_t::local_ordinal_type lno_t
static const std::string fail
void test1(Teuchos::RCP< const Teuchos::Comm< int > > &comm)
void checkNLocallyUnique(std::string &name, std::vector< gno_t > &gids, size_t nExpected)
static const std::string pass
Tpetra::global_size_t global_size_t
void checkMaxGid(std::string &name, std::vector< gno_t > &gids, gno_t maxExpected, const Teuchos::Comm< int > &comm)
Convert keys stored in std::vector to unique Gids stored in std::vector.
void checkMinGid(std::string &name, std::vector< gno_t > &gids, gno_t minExpected, const Teuchos::Comm< int > &comm)
void test4(Teuchos::RCP< const Teuchos::Comm< int > > &comm)
#define DECL_TYPE_NAME(x)