Zoltan2
Loading...
Searching...
No Matches
vecWithCopies.cpp
Go to the documentation of this file.
1
7
8#include "Teuchos_CommHelpers.hpp"
9#include "Teuchos_DefaultComm.hpp"
10#include "Teuchos_RCP.hpp"
11#include "Teuchos_Array.hpp"
12#include "Tpetra_Core.hpp"
13#include "Tpetra_Map.hpp"
14#include "Tpetra_Vector.hpp"
15
16#include <string>
17#include <sstream>
18#include <iostream>
19
21
22int main(int narg, char **arg)
23{
24 typedef Tpetra::Map<> map_t;
25 typedef map_t::local_ordinal_type lno_t;
26 typedef map_t::global_ordinal_type gno_t;
27 typedef int scalar_t;
28
29 Tpetra::ScopeGuard tscope(&narg, &arg);
30 Teuchos::RCP<const Teuchos::Comm<int> > comm = Tpetra::getDefaultComm();
31 int me = comm->getRank();
32
33 // Create a map with duplicated entries (mapWithCopies)
34 // Each rank has 15 IDs, the last five of which overlap with the next rank.
35
36
37 lno_t numLocalCoords = 15;
38 lno_t offset = me * 10;
39
40 Teuchos::Array<gno_t> gids(numLocalCoords);
41 for (lno_t i = 0 ; i < numLocalCoords; i++)
42 gids[i] = static_cast<gno_t> (offset + i);
43
44 Tpetra::global_size_t numGlobalCoords =
45 Teuchos::OrdinalTraits<Tpetra::global_size_t>::invalid();
46 Teuchos::RCP<const map_t> mapWithCopies =
47 rcp(new map_t(numGlobalCoords, gids(), 0, comm));
48
49 // Create a new map with IDs uniquely assigned to ranks (oneToOneMap)
50 Teuchos::RCP<const map_t> oneToOneMap =
51 Tpetra::createOneToOne<lno_t, gno_t>(mapWithCopies);
52
53 // Create vectors with each map
54 typedef Tpetra::Vector<scalar_t, lno_t, gno_t> vector_t;
55
56 vector_t vecWithCopies(mapWithCopies);
57 vector_t oneToOneVec(oneToOneMap);
58
59 // Set values in oneToOneVec: each entry == rank
60 for (lno_t i = 0; i < lno_t(oneToOneMap->getLocalNumElements()); i++)
61 oneToOneVec.replaceLocalValue(i, me);
62
63 // Now import oneToOneVec's values back to vecWithCopies
64 Teuchos::RCP<const Tpetra::Import<lno_t, gno_t> > importer =
65 Tpetra::createImport<lno_t, gno_t>(oneToOneMap, mapWithCopies);
66 vecWithCopies.doImport(oneToOneVec, *importer, Tpetra::REPLACE);
67
68 // Print the entries of each vector
69 std::cout << me << " ONE TO ONE VEC ("
70 << oneToOneMap->getGlobalNumElements() << "): ";
71 lno_t nlocal = lno_t(oneToOneMap->getLocalNumElements());
72 for (lno_t i = 0; i < nlocal; i++)
73 std::cout << "[" << oneToOneMap->getGlobalElement(i) << " "
74 << oneToOneVec.getData()[i] << "] ";
75 std::cout << std::endl;
76
77 // Should see copied vector values when print VEC WITH COPIES
78 std::cout << me << " VEC WITH COPIES ("
79 << mapWithCopies->getGlobalNumElements() << "): ";
80 nlocal = lno_t(mapWithCopies->getLocalNumElements());
81 for (lno_t i = 0; i < nlocal; i++)
82 std::cout << "[" << mapWithCopies->getGlobalElement(i) << " "
83 << vecWithCopies.getData()[i] << "] ";
84 std::cout << std::endl;
85
86 return 0;
87}
int main()
map_t::local_ordinal_type lno_t
Definition: mapRemotes.cpp:17
map_t::global_ordinal_type gno_t
Definition: mapRemotes.cpp:18
Tpetra::Map map_t
Definition: mapRemotes.cpp:16