Zoltan2  Version of the Day
IdentifierMap.cpp
Go to the documentation of this file.
1 // @HEADER
2 //
3 // ***********************************************************************
4 //
5 // Zoltan2: A package of combinatorial algorithms for scientific computing
6 // Copyright 2012 Sandia Corporation
7 //
8 // Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
9 // the U.S. Government retains certain rights in this software.
10 //
11 // Redistribution and use in source and binary forms, with or without
12 // modification, are permitted provided that the following conditions are
13 // met:
14 //
15 // 1. Redistributions of source code must retain the above copyright
16 // notice, this list of conditions and the following disclaimer.
17 //
18 // 2. Redistributions in binary form must reproduce the above copyright
19 // notice, this list of conditions and the following disclaimer in the
20 // documentation and/or other materials provided with the distribution.
21 //
22 // 3. Neither the name of the Corporation nor the names of the
23 // contributors may be used to endorse or promote products derived from
24 // this software without specific prior written permission.
25 //
26 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
27 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
30 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
31 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
32 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
33 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
34 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
35 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
36 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 //
38 // Questions? Contact Karen Devine (kddevin@sandia.gov)
39 // Erik Boman (egboman@sandia.gov)
40 // Siva Rajamanickam (srajama@sandia.gov)
41 //
42 // ***********************************************************************
43 //
44 // @HEADER
45 //
46 // Test the IdentifierMap class.
47 //
48 // Test local IDs are implied, not supplied by app.
49 
51 #include <Zoltan2_TestHelpers.hpp>
52 
53 #if 0
54 #include <string>
55 #include <ostream>
56 #include <iostream>
57 #include <exception>
58 #include <utility>
59 #endif
60 
61 #include <Teuchos_Comm.hpp>
62 #include <Teuchos_DefaultComm.hpp>
63 #include <Teuchos_RCP.hpp>
64 #include <Teuchos_ArrayRCP.hpp>
65 #include <Teuchos_Array.hpp>
66 #include <Teuchos_ArrayView.hpp>
67 
68 
69 using namespace std;
70 using Teuchos::RCP;
71 using Teuchos::rcp;
72 using Teuchos::ArrayRCP;
73 using Teuchos::Array;
74 using Teuchos::ArrayView;
75 using Teuchos::Comm;
76 
77 template <typename T>
78 struct UserIdTraits{
79  static std::string stringify(T val) {return std::string("INVALID");}
80 };
81 
82 template<>
83 struct UserIdTraits<std::pair<int, int> >{
84  static std::string stringify(std::pair<int, int> p) {
85  ostringstream oss;
86  oss << "pair(" << p.first << ", " << p.second << ")";
87  return oss.str();
88  }
89 };
90 
91 template<>
92 struct UserIdTraits<long>{
93  static std::string stringify(long val) {
94  ostringstream oss;
95  oss << val;
96  return oss.str();
97  }
98 };
99 
100 template<>
101 struct UserIdTraits<int>{
102  static std::string stringify(int val) {
103  ostringstream oss;
104  oss << val;
105  return oss.str();
106  }
107 };
108 
109 template <typename IDMAP>
110  void testIdMap( RCP<const Comm<int> > &comm,
111  IDMAP *map, bool gnosAreGids, bool gnosAreConsecutive,
112  ArrayRCP<typename IDMAP::gid_t> &gids,
113  ArrayRCP<typename IDMAP::gid_t> &remoteGids,
114  bool verbose)
115 {
116  typedef typename IDMAP::lno_t LNO;
117  typedef typename IDMAP::gno_t GNO;
118  typedef typename IDMAP::gid_t GID;
119 
120  int rank = comm->getRank();
121  int nprocs = comm->getSize();
122 
123  int fail = 0;
124 
125  if (map->gnosAreGids() != gnosAreGids)
126  fail = 1;
127 
128  TEST_FAIL_AND_THROW(*comm, fail==0, "gnosAreGids")
129 
130  if (map->gnosAreConsecutive() != gnosAreConsecutive)
131  fail = 1;
132 
133  TEST_FAIL_AND_THROW(*comm, fail==0, "consecutiveGids")
134 
135  // Get Zoltan2's global numbers given user global Ids
136 
137  size_t nLocalIds = gids.size();
138  Array<GNO> z2Ids(nLocalIds);
139 
140  try {
141  map->gidTranslate(gids(), z2Ids(), Zoltan2::TRANSLATE_APP_TO_LIB);
142  }
143  catch (std::exception &e){
144  fail = 2;
145  }
146 
147  TEST_FAIL_AND_THROW(*comm, fail==0, "gidTranslate")
148 
149  if (verbose){
150  comm->barrier();
151  if (rank == 0)
152  std::cout << "Zoltan2 GNOs = User GIDs: " << gnosAreGids << std::endl;
153  for (int p=0; p < nprocs; p++){
154  if (p == rank){
155  std::cout << "Rank " << p << " gnos: ";
156  for (size_t i=0; i < nLocalIds; i++){
157  std::cout << z2Ids[i] << " ";
158  }
159  std::cout << std::endl;
160  std::cout.flush();
161  }
162  comm->barrier();
163  }
164  comm->barrier();
165  if (rank == 0){
166  std::cout << "MIN GNO " << map->getMinimumGlobalId();
167  std::cout << ", MAX GNO " << map->getMaximumGlobalId() << std::endl;
168  std::cout.flush();
169  }
170  comm->barrier();
171  }
172 
173  // Get Zoltan2's global numbers given user local indices
174 
175  Array<GNO> z2Ids2(nLocalIds);
176  Array<LNO> indices(nLocalIds);
177 
178  for (LNO i=nLocalIds-1,j=0; i >= 0; i--,j++){
179  indices[j] = i;
180  }
181 
182 
183  try {
184  map->lnoTranslate(indices(), z2Ids2(), Zoltan2::TRANSLATE_APP_TO_LIB);
185  }
186  catch (std::exception &e){
187  fail = 3;
188  }
189 
190  TEST_FAIL_AND_THROW(*comm, fail==0, "lidTranslate")
191 
192  for (LNO i=nLocalIds-1, j=0; i >= 0; i--, j++){
193  if (z2Ids2[j] != z2Ids[i]){
194  fail = 4;
195  break;
196  }
197  }
198 
199  TEST_FAIL_AND_THROW(*comm, fail==0, "lnoTranslate results")
200 
201  // Get User's global Ids give Zoltan2's global numbers
202 
203  Array<GID> userGids(nLocalIds);
204 
205  try {
206  map->gidTranslate(userGids(), z2Ids(), Zoltan2::TRANSLATE_LIB_TO_APP);
207  }
208  catch (std::exception &e){
209  fail = 5;
210  }
211 
212  TEST_FAIL_AND_THROW(*comm, fail==0, "gidTranslate 2")
213 
214  for (size_t i=0; i < nLocalIds; i++){
215  if (userGids[i] != gids[i]){
216  fail = 6;
217  break;
218  }
219  }
220 
221  TEST_FAIL_AND_THROW(*comm, fail==0, "gidTranslate 2 results")
222 
223  if (nprocs > 1){
224  // Get Zoltan2 global number and owner of some remote User global Ids
225  size_t nRemoteIds = remoteGids.size();
226  Array<GNO> remoteGno(nRemoteIds);
227  Array<int> remoteProc(nRemoteIds);
228 
229  try {
230  map->gidGlobalTranslate(remoteGids(), remoteGno(), remoteProc());
231  }
232  catch (std::exception &e){
233  fail = 7;
234  }
235 
236  TEST_FAIL_AND_THROW(*comm, fail==0, "gidGLobalTranslate")
237 
238  if (verbose){
239  comm->barrier();
240  for (int p=0; p < nprocs; p++){
241  if (rank == 0)
242  std::cout << "Global info obtained from map:" << std::endl;
243  if (p == rank){
244  std::cout << "Rank " << p << std::endl;
245  for (size_t i=0; i < nRemoteIds; i++){
246  std::cout << " GID: ";
247  std::cout << UserIdTraits<GID>::stringify(remoteGids[i]);
248  std::cout << ", GNO " << remoteGno[i];
249  std::cout << ", Owner " << remoteProc[i] << std::endl;
250  }
251  std::cout << std::endl;
252  std::cout.flush();
253  }
254  comm->barrier();
255  }
256  comm->barrier();
257  }
258  }
259 }
260 
261 int main(int argc, char *argv[])
262 {
263  Teuchos::GlobalMPISession session(&argc, &argv);
264  RCP<const Comm<int> > comm = Teuchos::DefaultComm<int>::getComm();
265  int nprocs = comm->getSize();
266  int rank = comm->getRank();
267  RCP<const Zoltan2::Environment> env = rcp(new Zoltan2::Environment);
268 
269  lno_t numLocalObjects = 10;
270  long numRemoteObjects = 3; // numRemoteObjects < numLocalObjects
271  bool verbose = true;
272  bool consecutiveGids=true;
273  bool gnosAreGids=true;
274 
275  // Test these cases:
276  // 1. GIDs are non-consecutive ordinals
277  // 2. GIDs are non-consecutive ordinals, but we ask IdentifierMap to
278  // map them to consecutive IDs
279  // 3. GIDs are consecutive ordinals
280  // 4. GIDs are not Teuchos Ordinals
281 
282  ArrayRCP<gno_t> gids(new gno_t [numLocalObjects], 0, numLocalObjects, true);
283  ArrayRCP<gno_t> remoteGids(new gno_t [numRemoteObjects], 0,
284  numRemoteObjects, true);
285 
287 
289 
291  // Ids are non-consecutive ordinals.
292 
293  gno_t base1 = 10000 * rank;
294  gno_t base2 = base1 + 5000;
295  int fail = 0;
296  gno_t base = base1;
297 
298  for (lno_t i=0; i < numLocalObjects; i++){
299  gids[i] = base + i;
300  if (i == numLocalObjects/2) base = base2;
301  }
302 
303  typedef IdentifierMap<UserTypes> idmap_t;
304 
305  idmap_t *idMap = NULL;
306 
307  try{
308  idMap = new idmap_t(env, comm, gids, false);
309  }
310  catch (std::exception &e){
311  std::cerr << rank << ") " << e.what() << std::endl;
312  fail = 1;
313  }
314  TEST_FAIL_AND_EXIT(*comm, fail==0, "constructor first case", 1);
315 
316  if (nprocs > 1){
317  int remoteProc = (rank ? rank-1 : nprocs-1);
318  base = remoteProc * 10000;
319  for (int i=0; i < numRemoteObjects; i++)
320  remoteGids[i] = base + i;
321  }
322 
323  // We're not asking IdentifierMap to create consecutive
324  // IDs, so Zoltan2 GNOs will be the User's GIDs, and
325  // we will not have consecutive GNOs.
326 
327  testIdMap(comm, idMap, gnosAreGids, !consecutiveGids,
328  gids, remoteGids, verbose);
329 
330  delete idMap;
331 
333  // Ids are non-consecutive ordinals.
334  // IdentifierMap is asked to map them to consecutive.
335 
336  try{
337  idMap = new idmap_t(env, comm, gids, true);
338  }
339  catch (std::exception &e){
340  std::cerr << rank << ") " << e.what() << std::endl;
341  fail = 1;
342  }
343  TEST_FAIL_AND_EXIT(*comm, fail==0, "constructor second case", 1);
344 
345  // Because we're asking IdentifierMap to make the Zoltan2 GNOs
346  // consecutive, the GNOs will not be the same as the user GIDs.
347  // And because we specifically asked for consecutive GNOs, we
348  // will have consecutive global Ids.
349 
350  testIdMap(comm, idMap, !gnosAreGids, consecutiveGids,
351  gids, remoteGids, verbose);
352 
353  delete idMap;
354 
356  // Ids are consecutive ordinals.
357 
358  base = rank * numLocalObjects;
359  for (lno_t i=0; i < numLocalObjects; i++){
360  gids[i] = base + i;
361  }
362 
363  try{
364  idMap = new idmap_t(env, comm, gids, false);
365  }
366  catch (std::exception &e){
367  std::cerr << rank << ") " << e.what() << std::endl;
368  fail = 1;
369  }
370  TEST_FAIL_AND_EXIT(*comm, fail==0, "constructor third case", 1);
371 
372  if (nprocs > 1){
373  int remoteProc = (rank ? rank-1 : nprocs-1);
374  base = remoteProc * numLocalObjects;
375  for (int i=0; i < numRemoteObjects; i++)
376  remoteGids[i] = base + i;
377  }
378 
379  // Because the User GIDs are ordinals, the Zoltan2 GNOs will be
380  // the User GIDs. And since the User GIDs are already consecutive,
381  // the Zoltan2 GNOs are consecutive.
382 
383  testIdMap(comm, idMap, gnosAreGids, consecutiveGids,
384  gids, remoteGids, verbose);
385 
386  delete idMap;
387 
388 #if 0
389 
390  ArrayRCP<std::pair<int,int> > remoteGidPairs(
391  new std::pair<int,int> [numRemoteObjects], 0, numRemoteObjects, true);
392 
393  // TODO - there is a bug in the IdentifierMap constructor
394  // when GIDs are std::pair<int,int>
396  // Ids are not ordinals.
397  Zoltan2::BasicUserTypes<float, std::pair<int,int>, int, long> UserPairGids;
398 
399  ArrayRCP<std::pair<int,int> > nonOrdinalGids(
400  new std::pair<int,int> [numLocalObjects],
401  0, numLocalObjects, true);
402 
403  for (int i=0; i < numLocalObjects; i++){
404  nonOrdinalGids[i] = std::pair<int, int>(rank, i);
405  }
406 
407  typedef IdentifierMap<UserPairGids> mapPairGids_t;
408 
409  mapPairGids_t *idMap2 = NULL;
410 
411  try{
412  idMap2 = new mapPairGids_t(env, comm, nonOrdinalGids, false);
413  }
414  catch (std::exception &e){
415  std::cerr << rank << ") " << e.what() << std::endl;
416  fail = 1;
417  }
418  TEST_FAIL_AND_EXIT(*comm, fail==0, "constructor fourth case", 1);
419 
420  if (nprocs > 1){
421  int remoteProc = (rank ? rank-1 : nprocs-1);
422  base = remoteProc * numLocalObjects;
423  for (int i=0; i < numRemoteObjects; i++)
424  remoteGidPairs[i] = std::pair<int,int>(remoteProc,i);
425  }
426 
427  // Because the User's GIDs are not Teuchos Ordinals, they
428  // will not be used as Zoltan2 GNOs. When Zoltan2 creates
429  // the global Ids for the problem, it creates consecutive
430  // Ids that begin at 0.
431 
432  testIdMap(comm, idMap2, !gnosAreGids, consecutiveGids,
433  nonOrdinalGids, remoteGidPairs, verbose);
434 
435  delete idMap2;
436 #endif
437 
438  if (rank == 0)
439  std::cout << "PASS" << std::endl;
440 }
441 
An IdentifierMap manages a global space of object identifiers.
#define TEST_FAIL_AND_THROW(comm, ok, s)
int main(int argc, char *argv[])
Defines the IdentifierMap class.
static std::string stringify(int val)
convert internal ids to user ids
A simple class that can be the User template argument for an InputAdapter.
#define TEST_FAIL_AND_EXIT(comm, ok, s, code)
common code used by tests
convert user ids to internal ids
static std::string stringify(std::pair< int, int > p)
void testIdMap(RCP< const Comm< int > > &comm, IDMAP *map, bool gnosAreGids, bool gnosAreConsecutive, ArrayRCP< typename IDMAP::gid_t > &gids, ArrayRCP< typename IDMAP::gid_t > &remoteGids, bool verbose)
The user parameters, debug, timing and memory profiling output objects, and error checking methods...
static std::string stringify(T val)
static std::string stringify(long val)