Debian Codegraph @

packages/belos/epetra/test/Epetra64Driver/build_maps.hpp

raw
1 // @HEADER
2 // *****************************************************************************
3 // Belos: Block Linear Solvers Package
4 //
5 // Copyright 2004-2016 NTESS and the Belos contributors.
6 // SPDX-License-Identifier: BSD-3-Clause
7 // *****************************************************************************
8 // @HEADER
9
10 // Build maps for 1D or 2D matrix distribution
11 // Karen Devine, SNL
12 //
13
14 #ifndef __BUILD_MAPS_HPP
15 #define __BUILD_MAPS_HPP
16
17 #include <cstdio>
18 #include <cstdlib>
19 #include <iostream>
20 #ifndef __cplusplus
21 #define __cplusplus
22 #endif
23
24 #include "Epetra_Comm.h"
25 #ifdef EPETRA_MPI
26 #include "mpi.h"
27 #include "Epetra_MpiComm.h"
28 #else
29 #include "Epetra_SerialComm.h"
30 #endif
31
32 #include "Epetra_Map.h"
33
34 #define TWODPROW(p, npRows, npCols) ((p) % (npRows))
35 #define TWODPCOL(p, npRows, npCols) ((p) / (npRows))
36 #define TWODPRANK(i, j, npRows, npCols) ((j) * (npRows) + ((j)+(i)) % (npRows))
37 #define MIN(i,j) ((i) < (j) ? (i) : (j))
38
39 ///////////////////////////////////////////////////////////////////////////////
40 template <typename itype>
41 void random_distribution_1D(
42 itype nrows, // Number of global matrix rows
43 Epetra_Comm &comm, // Epetra communicator to be used in maps
44 Epetra_Map **rowMap, // OUTPUT: pointer to row map to be created
45 long long offsetEpetra64
46 )
47 {
48 // Randomly assign matrix rows to processor's row Map.
49
50 int me = comm.MyPID();
51 int np = comm.NumProc();
52
53 std::vector<itype> myGlobalElements(1.2 * (nrows / np) + 1);
54 int nMyRows = 0;
55 srandom(1);
56 double denom = (double) RAND_MAX + 1.;
57 for (itype i = 0; i < nrows; i++) {
58 int p = (int) ((double) np * (double) random() / denom);
59 if (p == me) {
60 if (nMyRows >= myGlobalElements.size())
61 myGlobalElements.resize(1.5*myGlobalElements.size());
62 myGlobalElements[nMyRows] = i + offsetEpetra64;
63 nMyRows++;
64 }
65 }
66 *rowMap = new Epetra_Map(nrows, nMyRows, &myGlobalElements[0], 0, comm);
67 }
68
69 ///////////////////////////////////////////////////////////////////////////////
70 template <typename itype>
71 void random_distribution_2D(
72 int mypRow, // processor's row (in 2D)
73 int mypCol, // processor's col (in 2D)
74 int npRows, // number of processor rows
75 int npCols, // number of processor cols
76 itype nrows, // Number of global matrix rows
77 Epetra_Comm &comm, // Epetra communicator to be used in maps
78 Epetra_Map **vectorMap, // OUTPUT: Map to be used for the vector
79 Epetra_Map **rowMap, // OUTPUT: Map to be used for the matrix rows
80 Epetra_Map **colMap, // OUTPUT: Map to be used for the matrix cols
81 long long offsetEpetra64
82 )
83 {
84 // Randomly assign matrix rows to processor's vector Map.
85 // Build appropriate GlobalElements lists for row and column maps at the
86 // same time.
87
88 int me = comm.MyPID();
89 int np = comm.NumProc();
90
91 int nMyEntries = 0;
92 int nMyRows = 0;
93 int nMyCols = 0;
94 std::vector<itype> myGlobalElements(1.2 * nrows / np + 1);
95 std::vector<itype> myGlobalRowElements(1.2 * nrows / npRows + 1);
96 std::vector<itype> myGlobalColElements(1.2 * nrows / npCols + 1);
97
98 srandom(1);
99 double denom = (double) RAND_MAX + 1.;
100
101 for (itype i = 0; i < nrows; i++) {
102 // Compute rank to receive the vector entry i
103 int p = (int) ((double) np * (double) random() / denom);
104
105 if (p == me) {
106 // Add entry i to my vector map
107 if (nMyEntries >= myGlobalElements.size())
108 myGlobalElements.resize(1.5*myGlobalElements.size());
109 myGlobalElements[nMyEntries] = i + offsetEpetra64;
110 nMyEntries++;
111 }
112
113 if (mypRow == TWODPROW(p, npRows, npCols)) {
114 // Add entry i to my row map
115 if (nMyRows >= myGlobalRowElements.size())
116 myGlobalRowElements.resize(1.5*myGlobalRowElements.size());
117 myGlobalRowElements[nMyRows] = i + offsetEpetra64;
118 nMyRows++;
119 }
120
121 if (mypCol == TWODPCOL(p, npRows, npCols)) {
122 // Add entry i to my col map
123 if (nMyCols >= myGlobalColElements.size())
124 myGlobalColElements.resize(1.5*myGlobalColElements.size());
125 myGlobalColElements[nMyCols] = i + offsetEpetra64;
126 nMyCols++;
127 }
128 }
129
130 *vectorMap = new Epetra_Map(-1, nMyEntries, &myGlobalElements[0], 0, comm);
131 *rowMap = new Epetra_Map(-1, nMyRows, &myGlobalRowElements[0], 0, comm);
132 *colMap = new Epetra_Map(-1, nMyCols, &myGlobalColElements[0], 0, comm);
133 }
134
135 ///////////////////////////////////////////////////////////////////////////////
136 template <typename itype>
137 void linear_distribution_2D(
138 int mypRow, // processor's row (in 2D)
139 int mypCol, // processor's col (in 2D)
140 int npRows, // number of processor rows
141 int npCols, // number of processor cols
142 itype nrows, // Number of global matrix rows
143 Epetra_Comm &comm, // Epetra communicator to be used in maps
144 Epetra_Map **vectorMap, // OUTPUT: Map to be used for the vector
145 Epetra_Map **rowMap, // OUTPUT: Map to be used for the matrix rows
146 Epetra_Map **colMap, // OUTPUT: Map to be used for the matrix cols
147 long long offsetEpetra64
148 )
149 {
150 // The vector will be distributed linearly:
151 // [0 1 2 3 4 5 6 7 8]
152 //
153 // If nrows is not divisible by np, extra rows will be distributed among
154 // the processors. (See below.)
155
156 int me = comm.MyPID();
157 int np = comm.NumProc();
158
159 // Create vector map first
160
161 std::vector<itype> entries(np+1, nrows / np); // Initial # entries per proc
162 int nExtraEntries = nrows % np;
163
164 // Distribute the extra entries evenly among processors.
165 // To evenly distribute them extra entries among processor rows and
166 // columns, we distribute them along diagonals of the matrix distribution.
167 // For our example, assume we have seven extra values (the max possible
168 // with np=8). Then we give one extra entry each to ranks
169 // [0, 3, 4, 7, 1, 2, 5]. For fewer extra entries, we follow the same
170 // order of assignment, and just stop early.
171
172 for (int cnt = 0, i = 0; (cnt < nExtraEntries) && (i < npRows); i++) {
173 for (int j = 0; (cnt < nExtraEntries) && (j < npCols); cnt++, j++) {
174 int rankForExtra = TWODPRANK(i, j, npRows, npCols);
175 entries[rankForExtra+1]++; // Store in rankForExtra+1 to simply later
176 // prefix sum.
177 }
178 }
179
180 // Perform prefix sum of entries.
181 entries[0] = 0;
182 for (int i = 1; i <= np; i++)
183 entries[i] = entries[i-1] + entries[i];
184 // Now entries contains the first vector entry for each rank.
185
186 // Create the global elements for the vector.
187 int nMyGlobalElements = entries[me+1]-entries[me];
188 std::vector<itype> myGlobalElements(nMyGlobalElements+1);
189
190 for (int i = 0; i < nMyGlobalElements; i++)
191 myGlobalElements[i] = entries[me] + i + offsetEpetra64;
192
193 *vectorMap = new Epetra_Map(-1, nMyGlobalElements, &myGlobalElements[0],
194 0, comm);
195
196 // Column map: Easy; consecutive entries for all ranks in column.
197 int firstRank = mypCol * npRows; // First rank in my column
198 nMyGlobalElements = 0;
199 for (int i = firstRank; i < firstRank + npRows; i++)
200 nMyGlobalElements += entries[i+1] - entries[i];
201
202 itype myFirstCol = entries[firstRank];
203 myGlobalElements.resize(nMyGlobalElements+1);
204 for (int i = 0; i < nMyGlobalElements; i++)
205 myGlobalElements[i] = myFirstCol + i + offsetEpetra64;
206
207 *colMap = new Epetra_Map(-1, nMyGlobalElements, &myGlobalElements[0],
208 0, comm);
209
210
211 // Row map: trickier since corresponding vector entries are not
212 // consecutive
213 firstRank = mypRow; // First rank in my row
214 nMyGlobalElements = 0;
215 for (int i = 0; i < npCols; i++) {
216 int rank = firstRank + i * npRows;
217 nMyGlobalElements += entries[rank+1] - entries[rank];
218 }
219 myGlobalElements.resize(nMyGlobalElements+1);
220 for (int cnt = 0, i = 0; i < npCols; i++) {
221 int rank = firstRank + i * npRows;
222 for (itype j = entries[rank]; j < entries[rank+1]; j++)
223 myGlobalElements[cnt++] = j + offsetEpetra64;
224 }
225
226 *rowMap = new Epetra_Map(-1, nMyGlobalElements, &myGlobalElements[0],
227 0, comm);
228 }
229
230 ///////////////////////////////////////////////////////////////////////////////
231 template <typename itype>
232 void build_maps(
233 itype nrows, // Number of global matrix rows
234 bool testEpetra64,// Flag indicating whether to adjust global row/column
235 // indices to exercise Epetra64 capability.
236 Epetra_Comm &comm, // Epetra communicator to be used in maps
237 Epetra_Map **vectorMap, // OUTPUT: Map to be used for the vector
238 Epetra_Map **rowMap, // OUTPUT: Map to be used for the matrix rows
239 Epetra_Map **colMap, // OUTPUT: Map to be used for the matrix cols
240 long long &offsetEpetra64, // OUTPUT for testing Epetra64: add offsetEpetra64
241 // to all row/column indices.
242 bool verbose // print out generated maps
243 )
244 {
245 // Function to build the maps for 1D or 2D matrix distribution.
246 // Output for 1D includes rowMap and NULL colMap and vectorMap.
247 // Output for 2D includes rowMap, colMap and vectorMap.
248
249 int me = comm.MyPID();
250 int np = comm.NumProc();
251
252 *rowMap = NULL;
253 *colMap = NULL;
254 *vectorMap = NULL;
255
256 // offsetEpetra64 = (testEpetra64 ? (long long) INT_MAX - (long long) 5 : 0);
257 offsetEpetra64 = (testEpetra64 ? (long long) 2 * INT_MAX : 0);
258
259 // Generate 1D row-based decomposition.
260
261 if ((me == 0) && verbose)
262 std::cout << std::endl
263 << "1D Distribution: " << std::endl
264 << " np = " << np << std::endl;
265
266 // Linear map similar to Trilinos default.
267 itype nMyRows = nrows / np + (nrows % np > me);
268 itype myFirstRow = me * (nrows / np) + MIN(nrows % np, me);
269 itype *myGlobalRows = new itype[nMyRows];
270 for (itype i = 0; i < nMyRows; i++)
271 myGlobalRows[i] = i + myFirstRow + offsetEpetra64;
272 *rowMap = new Epetra_Map(nrows, nMyRows, &myGlobalRows[0], 0, comm);
273 delete [] myGlobalRows;
274 }
275
276 #endif