15 #ifndef __TPETRA_DISTRIBUTION2D_HPP
16 #define __TPETRA_DISTRIBUTION2D_HPP
22 template <
typename gno_t,
typename scalar_t>
23 class Distribution2D :
public Distribution<gno_t,scalar_t> {
45 using Distribution<gno_t,scalar_t>::me;
46 using Distribution<gno_t,scalar_t>::np;
47 using Distribution<gno_t,scalar_t>::comm;
48 using Distribution<gno_t,scalar_t>::nrows;
49 using Distribution<gno_t,scalar_t>::Mine;
51 Distribution2D(
size_t nrows_,
52 const Teuchos::RCP<
const Teuchos::Comm<int> > &comm_,
53 const Teuchos::ParameterList ¶ms) :
54 Distribution<gno_t,scalar_t>(nrows_, comm_, params),
55 npRows(-1), npCols(-1)
59 const Teuchos::ParameterEntry *pe = params.getEntryPtr(
"nProcessorRows");
61 npRows = pe->getValue<
int>(&npRows);
65 const Teuchos::ParameterEntry *pe = params.getEntryPtr(
"nProcessorCols");
67 npCols = pe->getValue<
int>(&npCols);
72 if (npRows == -1 && npCols == -1) {
74 npRows = (int)(sqrt(np));
77 while (npRows * npCols != np) {
85 else if (npCols == -1)
88 if (npCols * npRows != np) {
89 TEUCHOS_TEST_FOR_EXCEPTION(npRows * npCols != np, std::logic_error,
90 "nProcessorCols " << npCols <<
91 " * nProcessorRows " << npRows <<
92 " = " << npCols * npRows <<
93 " must equal nProcessors " << np <<
94 " for 2D distribution");
98 std::cout <<
"\n2D Distribution: "
99 <<
"\n npRows = " << npRows
100 <<
"\n npCols = " << npCols
101 <<
"\n np = " << np << std::endl;
103 mypCol = this->TWODPCOL(me);
104 mypRow = this->TWODPROW(me);
107 virtual ~Distribution2D() {};
112 inline int TWODPROW(
int p) {
return (p % npRows);}
115 inline int TWODPCOL(
int p) {
return (p / npRows);}
118 inline int TWODPRANK(
int i,
int j) {
return (j * npRows + (j+i) % npRows);}
127 template <
typename gno_t,
typename scalar_t>
128 class Distribution2DLinear :
public Distribution2D<gno_t,scalar_t> {
131 using Distribution<gno_t,scalar_t>::me;
132 using Distribution<gno_t,scalar_t>::np;
133 using Distribution<gno_t,scalar_t>::nrows;
134 using Distribution2D<gno_t,scalar_t>::npRows;
135 using Distribution2D<gno_t,scalar_t>::npCols;
136 using Distribution2D<gno_t,scalar_t>::mypRow;
137 using Distribution2D<gno_t,scalar_t>::mypCol;
139 Distribution2DLinear(
size_t nrows_,
140 const Teuchos::RCP<
const Teuchos::Comm<int> > &comm_,
141 const Teuchos::ParameterList ¶ms) :
142 Distribution2D<gno_t,scalar_t>(nrows_, comm_, params)
145 entries.assign(np+1, nrows / np);
146 int nExtraEntries = nrows % np;
156 for (
int cnt = 0, i = 0; (cnt < nExtraEntries) && (i < npRows); i++) {
157 for (
int j = 0; (cnt < nExtraEntries) && (j < npCols); cnt++, j++) {
158 int rankForExtra = Distribution2D<gno_t,scalar_t>::TWODPRANK(i, j);
159 entries[rankForExtra+1]++;
166 for (
int i = 1; i <= np; i++)
167 entries[i] = entries[i-1] + entries[i];
171 int firstRank = mypCol * npRows;
172 myFirstCol = entries[firstRank];
175 for (
int i = firstRank; i < firstRank + npRows; i++)
176 nMyCols += entries[i+1] - entries[i];
177 myLastCol = myFirstCol + nMyCols - 1;
180 inline enum DistributionType DistType() {
return TwoDLinear; }
182 bool Mine(gno_t i, gno_t j) {
183 int idx = int(
float(i) *
float(np) /
float(nrows));
184 while (i < entries[idx]) idx--;
185 while (i >= entries[idx+1]) idx++;
186 return ((mypRow == Distribution2D<gno_t,scalar_t>::TWODPROW(idx))
187 && (j >= myFirstCol && j <= myLastCol));
189 inline bool Mine(gno_t i, gno_t j,
int p) {
return Mine(i,j);}
191 inline bool VecMine(gno_t i) {
192 return(i >= entries[me] && i < entries[me+1]);
197 std::vector<gno_t> entries;
204 template <
typename gno_t,
typename scalar_t>
205 class Distribution2DRandom :
public Distribution2D<gno_t,scalar_t> {
208 using Distribution<gno_t,scalar_t>::me;
209 using Distribution2D<gno_t,scalar_t>::mypRow;
210 using Distribution2D<gno_t,scalar_t>::mypCol;
212 Distribution2DRandom(
size_t nrows_,
213 const Teuchos::RCP<
const Teuchos::Comm<int> > &comm_,
214 const Teuchos::ParameterList ¶ms) :
215 Distribution2D<gno_t,scalar_t>(nrows_, comm_, params)
216 {
if (me == 0) std::cout <<
" randomize = true" << std::endl; }
218 inline enum DistributionType DistType() {
return TwoDRandom; }
220 inline bool Mine(gno_t i, gno_t j) {
221 return ((mypRow == this->TWODPROW(this->HashToProc(i))) &&
222 (mypCol == this->TWODPCOL(this->HashToProc(j))));
224 inline bool Mine(gno_t i, gno_t j,
int p) {
return Mine(i,j);}
226 inline bool VecMine(gno_t i) {
return (me == this->HashToProc(i)); }
232 template <
typename gno_t,
typename scalar_t>
233 class Distribution2DVec :
public Distribution2D<gno_t,scalar_t>
239 using Distribution<gno_t,scalar_t>::me;
240 using Distribution<gno_t,scalar_t>::np;
241 using Distribution<gno_t,scalar_t>::comm;
242 using Distribution<gno_t,scalar_t>::nrows;
243 using Distribution2D<gno_t,scalar_t>::npRows;
244 using Distribution2D<gno_t,scalar_t>::npCols;
246 Distribution2DVec(
size_t nrows_,
247 const Teuchos::RCP<
const Teuchos::Comm<int> > &comm_,
248 const Teuchos::ParameterList ¶ms,
249 std::string &distributionfile) :
250 Distribution2D<gno_t,scalar_t>(nrows_, comm_, params)
252 if (me == 0) std::cout <<
"\n 2DVec Distribution: "
253 <<
"\n np = " << np << std::endl;
256 fpin.open(distributionfile.c_str(), std::ios::in);
257 if (!fpin.is_open()) {
258 std::cout <<
"Error: distributionfile " << distributionfile
259 <<
" not found" << std::endl;
269 vecpart =
new int[nrows];
271 const int bcastsize = 1000000;
275 for (
size_t i = 0; i < nrows; i++) {
276 if (me == 0) fpin >> vecpart[i];
278 if (cnt == bcastsize || i == nrows-1) {
279 Teuchos::broadcast(*comm, 0, cnt, &(vecpart[start]));
285 if (me == 0) fpin.close();
288 ~Distribution2DVec() {
delete [] vecpart;}
290 inline enum DistributionType DistType() {
return TwoDVec; }
292 bool Mine(gno_t i, gno_t j) {
293 return (me == (vecpart[i] % npRows + (vecpart[j] / npRows) * npRows));
295 inline bool Mine(gno_t i, gno_t j,
int p) {
return Mine(i,j);}
297 inline bool VecMine(gno_t i) {
return(vecpart[i] == me); }
void start()
Start the deep_copy counter.