Zoltan2
Loading...
Searching...
No Matches
Zoltan2_AlgTpetraMapping.hpp
Go to the documentation of this file.
1// @HEADER
2// *****************************************************************************
3// Zoltan2: A package of combinatorial algorithms for scientific computing
4//
5// Copyright 2012 NTESS and the Zoltan2 contributors.
6// SPDX-License-Identifier: BSD-3-Clause
7// *****************************************************************************
8// @HEADER
9
10#ifndef _ZOLTAN2_ALGDEFAULTMAPPING_HPP_
11#define _ZOLTAN2_ALGDEFAULTMAPPING_HPP_
12
13#include <Zoltan2_Standards.hpp>
14#include <Zoltan2_Algorithm.hpp>
16#include <Zoltan2_Util.hpp>
17
18
22//
24
25namespace Zoltan2 {
26
27template <typename Adapter>
28class AlgDefaultMapping : public Algorithm<Adapter>
29{
30
31private:
32
33 typedef typename Adapter::lno_t lno_t;
34 typedef typename Adapter::gno_t gno_t;
35 typedef typename Adapter::scalar_t scalar_t;
36 typedef typename Adapter::part_t part_t;
37 typedef typename Adapter::user_t user_t;
38 typedef typename Adapter::userCoord_t userCoord_t;
39
40 const RCP<const Environment> env;
41 const RCP<const Comm<int> > problemComm;
42 const RCP<const typename Adapter::base_adapter_t> adapter;
43
44public:
45
52 const Teuchos::RCP <const Teuchos::Comm<int> > &comm_,
53 const Teuchos::RCP <const MachineRepresentation<pcoord_t,part_t> > &machine_,
54 const Teuchos::RCP <const Adapter> &adapter_,
55 const Teuchos::RCP <const Zoltan2::PartitioningSolution<Adapter> > &psoln_,
56 const Teuchos::RCP <const Environment> &envConst):
57 env(env__), comm(comm_), adapter(adapter_),
58 partIsRank(false), haveContigousParts(false)
59 rankForPart(Teuchos::null)
60 {
61 int nRanks = comm->getSize();
62
63 // Get set of parts from partitioning solution or adapter, if provided
64 // Otherwise, we'll assume set of parts == set of ranks
65 const *partList;
66 if (psoln_ != Teuchos::null) {
67 partList = psoln_->getPartListView();
68 }
69 else {
70 adapter->getPartsView(partList);
71 }
72
73 // Compute maxPart, nParts
74 typedef typename Tpetra::Map<lno_t, part_t> tpetraMap_t
75 Teuchos::RCP<tpetraMap_t> tmap;
76
77 part_t minPart, maxPart;
78
79 if (partList == NULL) {
80 // Parts for IDs are the same as ranks
81 nParts = nRanks;
82 maxPart = nRanks-1;
83 minPart = 0;
84 }
85 else {
86 // Parts were provided by partitioning solution or input adapter
87
88 // Find unique parts on this rank,
89 // as Tpetra::Map does not like duplicate GIDs within a rank
90
91 size_t nLocal = adapter->getLocalNumIDs();
92
93 std::set<part_t> unique(nLocal);
94 for (size_t i; i < adapter->getLocalNumIDs(); i++)
95 unique.insert(partList[i]);
96
97 size_t nUnique = unique.size();
98 Array<const part_t> uniquePartList(nUnique);
99 size_t k = 0;
100 for (typename std::set<part_t>::iterator it = set.begin();
101 it != set.end(); it++)
102 uniquePartList[k++] = *it;
103
104 // Use Tpetra::Map to find the max, min, total part.
105
106 global_size_t nGlobalElts =
107 Teuchos::OrdinalTraits<Tpetra::global_size_t>::invalid();
108 tmap = rcp(new tpetraMap_t(nGlobalElts, uniquePartList(), 0, comm));
109
110 nParts = as<part_t>(tmap->getGlobalNumElements());
111 minPart = tmap->getMinAllGlobalIndex();
112 maxPart = tmap->getMaxAllGlobalIndex();
113 }
114
115 nParts_Div_nRanks = nParts / nRanks;
116 nParts_Mod_nRanks = nParts % nRanks;
117
118 // Determine number of unique parts, as well as min and max part
119 // Can probably use a Tpetra Map.
120
121 if (maxPart < nRanks)
122 partIsRank = true; // Most common case
123
124 if ((minPart == 0) && (maxPart == nParts-1))
125 // Have a contiguous set of parts; can use simplest default mapping
126 haveContiguousParts = true;
127
128 if (!partIsRank && !haveContiguousParts) {
129 // Use Tpetra createOneToOne to map parts to ranks
130 Teuchos::RCP<tpetraMap_t> oneToOneMap = Tpetra::createOneToOne(tmap);
131
132 // Each rank needs names of all parts
133 // Should we gather map to one rank, or copy to all?
134 // I don't know how to do it.
135 Teuchos::RCP<tpetraMap_t> gatheredMap = ;
136
137 Teuchos::ArrayView<const part_t> allParts =
138 gatheredMap->getLocalElementList();
139
140 // Look up the rank for all parts assigned by oneToOneMap
141 Teuchos::Array<int> allRanks(allParts.size());
142 oneToOneMap->getRemoveIndexList(allParts, allRanks());
143
144 for (size_t i = 0; i < allPart.size())
145 (*rankForPart)[allParts[i]] = allRanks[i];
146 }
147 }
148
149 void map(Teuchos::RCP<MappingSolution<Adapter> > msoln) {
150 // Mapping was computed in constructor;
151 // nothing to do here sine we implement getRankForPart and
152 // getPartsForRankView. (If we didn't we would need to store
153 // the mapping in the soln here.)
154 }
155
156 int getRankForPart(part_t p) {
157 if (p < 0 || p > maxPart)
158 throw std::runtime_error("Invalid part in getRankForPart");
159
160 // Most common case: at most one part per rank
161 if (partIsRank)
162 return as<int>(p);
163
164 // Multiple parts per rank
165 if (haveContiguousParts) {
166 int tmp = p / nParts_Div_nRanks;
167 while (firstContiguousPart(tmp) > p) tmp--;
168 while (firstContiguousPart(tmp+1) < p) tmp++;
169 return tmp;
170 }
171
172 // None of the simple schemes apply; use the unordered_map.
173 return rankForPart[p];
174 }
175
176 void getPartsForRankView(int rank, part_t &numParts, part_t *&parts)
177 {
178 // Will need to construct the arrays if this function is called.
179 // Will not work if this is a const method.
180 partsForRankIdx = rcp(new part_t[nRanks+1]);
181 partsForRank = rcp(new part_t[nParts]);
182
183 }
184
185 void map(const RCP<PartitioningSolution<Adapter> > &solution);
186
187private:
188
189 inline part_t firstContiguousPart(int rank) {
190 // For contiguous part assignments, the first part assigned to rank
191 return (rank * nParts_Div_nRanks + min(rank, nParts_Mod_nRanks));
192 }
193};
194
195
196} // namespace Zoltan2
197
198#endif
#define nParts
Defines the PartitioningSolution class.
Gathering definitions used in software development.
A gathering of useful namespace methods.
void getPartsForRankView(int rank, part_t &numParts, part_t *&parts)
void map(const RCP< PartitioningSolution< Adapter > > &solution)
AlgDefaultMapping(const Teuchos::RCP< const Teuchos::Comm< int > > &comm_, const Teuchos::RCP< const MachineRepresentation< pcoord_t, part_t > > &machine_, const Teuchos::RCP< const Adapter > &adapter_, const Teuchos::RCP< const Zoltan2::PartitioningSolution< Adapter > > &psoln_, const Teuchos::RCP< const Environment > &envConst)
int getRankForPart(part_t p)
In mapping, returns the rank to which a part is assigned.
void map(Teuchos::RCP< MappingSolution< Adapter > > msoln)
MachineRepresentation Class Base class for representing machine coordinates, networks,...
PartitionMapping maps a solution or an input distribution to ranks.
A PartitioningSolution is a solution to a partitioning problem.
map_t::local_ordinal_type lno_t
map_t::global_ordinal_type gno_t
Created by mbenlioglu on Aug 31, 2020.
Tpetra::global_size_t global_size_t
SparseMatrixAdapter_t::part_t part_t