47 #ifndef __TPETRA_DISTRIBUTION2D_HPP
48 #define __TPETRA_DISTRIBUTION2D_HPP
54 template <
typename gno_t,
typename scalar_t>
55 class Distribution2D :
public Distribution<gno_t,scalar_t> {
77 using Distribution<gno_t,scalar_t>::me;
78 using Distribution<gno_t,scalar_t>::np;
79 using Distribution<gno_t,scalar_t>::comm;
80 using Distribution<gno_t,scalar_t>::nrows;
81 using Distribution<gno_t,scalar_t>::Mine;
83 Distribution2D(
size_t nrows_,
84 const Teuchos::RCP<
const Teuchos::Comm<int> > &comm_,
85 const Teuchos::ParameterList ¶ms) :
86 Distribution<gno_t,scalar_t>(nrows_, comm_, params),
87 npRows(-1), npCols(-1)
91 const Teuchos::ParameterEntry *pe = params.getEntryPtr(
"nProcessorRows");
93 npRows = pe->getValue<
int>(&npRows);
97 const Teuchos::ParameterEntry *pe = params.getEntryPtr(
"nProcessorCols");
99 npCols = pe->getValue<
int>(&npCols);
104 if (npRows == -1 && npCols == -1) {
106 npRows = (int)(sqrt(np));
107 npCols = np / npRows;
109 while (npRows * npCols != np) {
111 npCols = np / npRows;
116 npRows = np / npCols;
117 else if (npCols == -1)
118 npCols = np / npRows;
120 if (npCols * npRows != np) {
121 TEUCHOS_TEST_FOR_EXCEPTION(npRows * npCols != np, std::logic_error,
122 "nProcessorCols " << npCols <<
123 " * nProcessorRows " << npRows <<
124 " = " << npCols * npRows <<
125 " must equal nProcessors " << np <<
126 " for 2D distribution");
130 std::cout <<
"\n2D Distribution: "
131 <<
"\n npRows = " << npRows
132 <<
"\n npCols = " << npCols
133 <<
"\n np = " << np << std::endl;
135 mypCol = this->TWODPCOL(me);
136 mypRow = this->TWODPROW(me);
139 virtual ~Distribution2D() {};
142 virtual bool Mine(gno_t i, gno_t j) = 0;
143 inline bool Mine(gno_t i, gno_t j,
int p) {
return Mine(i,j);}
148 inline int TWODPROW(
int p) {
return (p % npRows);}
151 inline int TWODPCOL(
int p) {
return (p / npRows);}
154 inline int TWODPRANK(
int i,
int j) {
return (j * npRows + (j+i) % npRows);}
163 template <
typename gno_t,
typename scalar_t>
164 class Distribution2DLinear :
public Distribution2D<gno_t,scalar_t> {
167 using Distribution<gno_t,scalar_t>::me;
168 using Distribution<gno_t,scalar_t>::np;
169 using Distribution<gno_t,scalar_t>::nrows;
170 using Distribution2D<gno_t,scalar_t>::npRows;
171 using Distribution2D<gno_t,scalar_t>::npCols;
172 using Distribution2D<gno_t,scalar_t>::mypRow;
173 using Distribution2D<gno_t,scalar_t>::mypCol;
175 Distribution2DLinear(
size_t nrows_,
176 const Teuchos::RCP<
const Teuchos::Comm<int> > &comm_,
177 const Teuchos::ParameterList ¶ms) :
178 Distribution2D<gno_t,scalar_t>(nrows_, comm_, params)
181 entries.assign(np+1, nrows / np);
182 int nExtraEntries = nrows % np;
192 for (
int cnt = 0, i = 0; (cnt < nExtraEntries) && (i < npRows); i++) {
193 for (
int j = 0; (cnt < nExtraEntries) && (j < npCols); cnt++, j++) {
194 int rankForExtra = Distribution2D<gno_t,scalar_t>::TWODPRANK(i, j);
195 entries[rankForExtra+1]++;
202 for (
int i = 1; i <= np; i++)
203 entries[i] = entries[i-1] + entries[i];
207 int firstRank = mypCol * npRows;
208 myFirstCol = entries[firstRank];
211 for (
int i = firstRank; i < firstRank + npRows; i++)
212 nMyCols += entries[i+1] - entries[i];
213 myLastCol = myFirstCol + nMyCols - 1;
216 inline enum DistributionType DistType() {
return TwoDLinear; }
218 bool Mine(gno_t i, gno_t j) {
219 int idx = int(
float(i) *
float(np) /
float(nrows));
220 while (i < entries[idx]) idx--;
221 while (i >= entries[idx+1]) idx++;
222 return ((mypRow == Distribution2D<gno_t,scalar_t>::TWODPROW(idx))
223 && (j >= myFirstCol && j <= myLastCol));
226 inline bool VecMine(gno_t i) {
227 return(i >= entries[me] && i < entries[me+1]);
232 std::vector<gno_t> entries;
239 template <
typename gno_t,
typename scalar_t>
240 class Distribution2DRandom :
public Distribution2D<gno_t,scalar_t> {
243 using Distribution<gno_t,scalar_t>::me;
244 using Distribution2D<gno_t,scalar_t>::mypRow;
245 using Distribution2D<gno_t,scalar_t>::mypCol;
247 Distribution2DRandom(
size_t nrows_,
248 const Teuchos::RCP<
const Teuchos::Comm<int> > &comm_,
249 const Teuchos::ParameterList ¶ms) :
250 Distribution2D<gno_t,scalar_t>(nrows_, comm_, params)
251 {
if (me == 0) std::cout <<
" randomize = true" << std::endl; }
253 inline enum DistributionType DistType() {
return TwoDRandom; }
255 inline bool Mine(gno_t i, gno_t j) {
256 return ((mypRow == this->TWODPROW(this->HashToProc(i))) &&
257 (mypCol == this->TWODPCOL(this->HashToProc(j))));
260 inline bool VecMine(gno_t i) {
return (me == this->HashToProc(i)); }
266 template <
typename gno_t,
typename scalar_t>
267 class Distribution2DVec :
public Distribution2D<gno_t,scalar_t>
273 using Distribution<gno_t,scalar_t>::me;
274 using Distribution<gno_t,scalar_t>::np;
275 using Distribution<gno_t,scalar_t>::comm;
276 using Distribution<gno_t,scalar_t>::nrows;
277 using Distribution2D<gno_t,scalar_t>::npRows;
278 using Distribution2D<gno_t,scalar_t>::npCols;
280 Distribution2DVec(
size_t nrows_,
281 const Teuchos::RCP<
const Teuchos::Comm<int> > &comm_,
282 const Teuchos::ParameterList ¶ms,
283 std::string &distributionfile) :
284 Distribution2D<gno_t,scalar_t>(nrows_, comm_, params)
286 if (me == 0) std::cout <<
"\n 2DVec Distribution: "
287 <<
"\n np = " << np << std::endl;
290 fpin.open(distributionfile.c_str(), std::ios::in);
291 if (!fpin.is_open()) {
292 std::cout <<
"Error: distributionfile " << distributionfile
293 <<
" not found" << std::endl;
303 vecpart =
new int[nrows];
305 const int bcastsize = 1000000;
309 for (
size_t i = 0; i < nrows; i++) {
310 if (me == 0) fpin >> vecpart[i];
312 if (cnt == bcastsize || i == nrows-1) {
313 Teuchos::broadcast(*comm, 0, cnt, &(vecpart[start]));
319 if (me == 0) fpin.close();
322 ~Distribution2DVec() {
delete [] vecpart;}
324 inline enum DistributionType DistType() {
return TwoDVec; }
326 bool Mine(gno_t i, gno_t j) {
327 return (me == (vecpart[i] % npRows + (vecpart[j] / npRows) * npRows));
330 inline bool VecMine(gno_t i) {
return(vecpart[i] == me); }
Namespace Tpetra contains the class and methods constituting the Tpetra library.