15 #ifndef __TPETRA_DISTRIBUTION2D_HPP
16 #define __TPETRA_DISTRIBUTION2D_HPP
21 template <
typename gno_t,
typename scalar_t>
22 class Distribution2D :
public Distribution<gno_t, scalar_t> {
44 using Distribution<gno_t, scalar_t>::me;
45 using Distribution<gno_t, scalar_t>::np;
46 using Distribution<gno_t, scalar_t>::comm;
47 using Distribution<gno_t, scalar_t>::nrows;
48 using Distribution<gno_t, scalar_t>::Mine;
50 Distribution2D(
size_t nrows_,
51 const Teuchos::RCP<
const Teuchos::Comm<int> > &comm_,
52 const Teuchos::ParameterList ¶ms)
53 : Distribution<gno_t, scalar_t>(nrows_, comm_, params)
57 const Teuchos::ParameterEntry *pe = params.getEntryPtr(
"nProcessorRows");
59 npRows = pe->getValue<
int>(&npRows);
63 const Teuchos::ParameterEntry *pe = params.getEntryPtr(
"nProcessorCols");
65 npCols = pe->getValue<
int>(&npCols);
70 if (npRows == -1 && npCols == -1) {
72 npRows = (int)(sqrt(np));
75 while (npRows * npCols != np) {
82 else if (npCols == -1)
85 if (npCols * npRows != np) {
86 TEUCHOS_TEST_FOR_EXCEPTION(npRows * npCols != np, std::logic_error,
87 "nProcessorCols " << npCols <<
" * nProcessorRows " << npRows <<
" = " << npCols * npRows <<
" must equal nProcessors " << np <<
" for 2D distribution");
91 std::cout <<
"\n2D Distribution: "
92 <<
"\n npRows = " << npRows
93 <<
"\n npCols = " << npCols
94 <<
"\n np = " << np << std::endl;
96 mypCol = this->TWODPCOL(me);
97 mypRow = this->TWODPROW(me);
100 virtual ~Distribution2D(){};
104 inline int TWODPROW(
int p) {
return (p % npRows); }
107 inline int TWODPCOL(
int p) {
return (p / npRows); }
110 inline int TWODPRANK(
int i,
int j) {
return (j * npRows + (j + i) % npRows); }
119 template <
typename gno_t,
typename scalar_t>
120 class Distribution2DLinear :
public Distribution2D<gno_t, scalar_t> {
122 using Distribution<gno_t, scalar_t>::me;
123 using Distribution<gno_t, scalar_t>::np;
124 using Distribution<gno_t, scalar_t>::nrows;
125 using Distribution2D<gno_t, scalar_t>::npRows;
126 using Distribution2D<gno_t, scalar_t>::npCols;
127 using Distribution2D<gno_t, scalar_t>::mypRow;
128 using Distribution2D<gno_t, scalar_t>::mypCol;
130 Distribution2DLinear(
size_t nrows_,
131 const Teuchos::RCP<
const Teuchos::Comm<int> > &comm_,
132 const Teuchos::ParameterList ¶ms)
133 : Distribution2D<gno_t, scalar_t>(nrows_, comm_, params) {
135 entries.assign(np + 1, nrows / np);
136 int nExtraEntries = nrows % np;
146 for (
int cnt = 0, i = 0; (cnt < nExtraEntries) && (i < npRows); i++) {
147 for (
int j = 0; (cnt < nExtraEntries) && (j < npCols); cnt++, j++) {
148 int rankForExtra = Distribution2D<gno_t, scalar_t>::TWODPRANK(i, j);
149 entries[rankForExtra + 1]++;
156 for (
int i = 1; i <= np; i++)
157 entries[i] = entries[i - 1] + entries[i];
161 int firstRank = mypCol * npRows;
162 myFirstCol = entries[firstRank];
165 for (
int i = firstRank; i < firstRank + npRows; i++)
166 nMyCols += entries[i + 1] - entries[i];
167 myLastCol = myFirstCol + nMyCols - 1;
170 inline enum DistributionType DistType() {
return TwoDLinear; }
172 bool Mine(gno_t i, gno_t j) {
173 int idx = int(
float(i) *
float(np) /
float(nrows));
174 while (i < entries[idx]) idx--;
175 while (i >= entries[idx + 1]) idx++;
176 return ((mypRow == Distribution2D<gno_t, scalar_t>::TWODPROW(idx))
177 && (j >= myFirstCol && j <= myLastCol));
179 inline bool Mine(gno_t i, gno_t j,
int p) {
return Mine(i, j); }
181 inline bool VecMine(gno_t i) {
182 return (i >= entries[me] && i < entries[me + 1]);
186 std::vector<gno_t> entries;
193 template <
typename gno_t,
typename scalar_t>
194 class Distribution2DRandom :
public Distribution2D<gno_t, scalar_t> {
196 using Distribution<gno_t, scalar_t>::me;
197 using Distribution2D<gno_t, scalar_t>::mypRow;
198 using Distribution2D<gno_t, scalar_t>::mypCol;
200 Distribution2DRandom(
size_t nrows_,
201 const Teuchos::RCP<
const Teuchos::Comm<int> > &comm_,
202 const Teuchos::ParameterList ¶ms)
203 : Distribution2D<gno_t, scalar_t>(nrows_, comm_, params) {
204 if (me == 0) std::cout <<
" randomize = true" << std::endl;
207 inline enum DistributionType DistType() {
return TwoDRandom; }
209 inline bool Mine(gno_t i, gno_t j) {
210 return ((mypRow == this->TWODPROW(this->HashToProc(i))) &&
211 (mypCol == this->TWODPCOL(this->HashToProc(j))));
213 inline bool Mine(gno_t i, gno_t j,
int p) {
return Mine(i, j); }
215 inline bool VecMine(gno_t i) {
return (me == this->HashToProc(i)); }
220 template <
typename gno_t,
typename scalar_t>
221 class Distribution2DVec :
public Distribution2D<gno_t, scalar_t> {
226 using Distribution<gno_t, scalar_t>::me;
227 using Distribution<gno_t, scalar_t>::np;
228 using Distribution<gno_t, scalar_t>::comm;
229 using Distribution<gno_t, scalar_t>::nrows;
230 using Distribution2D<gno_t, scalar_t>::npRows;
231 using Distribution2D<gno_t, scalar_t>::npCols;
233 Distribution2DVec(
size_t nrows_,
234 const Teuchos::RCP<
const Teuchos::Comm<int> > &comm_,
235 const Teuchos::ParameterList ¶ms,
236 std::string &distributionfile)
237 : Distribution2D<gno_t, scalar_t>(nrows_, comm_, params) {
238 if (me == 0) std::cout <<
"\n 2DVec Distribution: "
239 <<
"\n np = " << np << std::endl;
242 fpin.open(distributionfile.c_str(), std::ios::in);
243 if (!fpin.is_open()) {
244 std::cout <<
"Error: distributionfile " << distributionfile
245 <<
" not found" << std::endl;
255 vecpart =
new int[nrows];
257 const int bcastsize = 1000000;
261 for (
size_t i = 0; i < nrows; i++) {
262 if (me == 0) fpin >> vecpart[i];
264 if (cnt == bcastsize || i == nrows - 1) {
265 Teuchos::broadcast(*comm, 0, cnt, &(vecpart[start]));
271 if (me == 0) fpin.close();
274 ~Distribution2DVec() {
delete[] vecpart; }
276 inline enum DistributionType DistType() {
return TwoDVec; }
278 bool Mine(gno_t i, gno_t j) {
279 return (me == (vecpart[i] % npRows + (vecpart[j] / npRows) * npRows));
281 inline bool Mine(gno_t i, gno_t j,
int p) {
return Mine(i, j); }
283 inline bool VecMine(gno_t i) {
return (vecpart[i] == me); }
void start()
Start the deep_copy counter.