| 1 |
// @HEADER |
| 2 |
// ***************************************************************************** |
| 3 |
// MueLu: A package for multigrid based preconditioning |
| 4 |
// |
| 5 |
// Copyright 2012 NTESS and the MueLu contributors. |
| 6 |
// SPDX-License-Identifier: BSD-3-Clause |
| 7 |
// ***************************************************************************** |
| 8 |
// @HEADER |
| 9 |
|
| 10 |
//#include <Tpetra_CrsMatrix_decl.hpp> |
| 11 |
//#include <Teuchos_RCPDecl.hpp> |
| 12 |
|
| 13 |
#include "neighbours.hpp" |
| 14 |
|
| 15 |
typedef Tpetra::CrsMatrix<double, int, int, KokkosClassic::DefaultNode::DefaultNodeType> tpetra_matrix_type; |
| 16 |
typedef Tpetra::MultiVector<typename Teuchos::ScalarTraits<Scalar>::magnitudeType, int, int, KokkosClassic::DefaultNode::DefaultNodeType> tpetra_multivector_type; |
| 17 |
typedef typename Teuchos::ArrayView<const int>::const_iterator iterator_type; |
| 18 |
typedef Tpetra::KokkosClassic::DefaultNode::DefaultNodeType node_type2; |
| 19 |
|
| 20 |
void BAP1D(Teuchos::RCP<tpetra_matrix_type> BAP, Teuchos::RCP<tpetra_matrix_type> tpetra_prolong, Teuchos::RCP<tpetra_multivector_type> BAP_shrunk, Teuchos::RCP<const Teuchos::Comm<int> > comm) { |
| 21 |
// INPUT: BAP = matrix where the uncompressed version of B_DD * A_h * Ptentative must be stored |
| 22 |
// INPUT: tpetra_prolong = Ptentative |
| 23 |
// INPUT: BAP_shrunk = Tpetra:MultiVector contatining the shrunk version of B_DD * A_h * Ptentative resulting from domain decomposition with coloring |
| 24 |
// INPUT: comm = MPI communicator (MPI_COMM_WORLD) |
| 25 |
|
| 26 |
Teuchos::ArrayView<const int> myLocalElements = BAP->getRowMap()->getLocalElementList(); |
| 27 |
int mypid = comm->getRank(); |
| 28 |
|
| 29 |
for (int color = 0; color < 3; ++color) { |
| 30 |
Teuchos::ArrayRCP<const double> localBAP = BAP_shrunk->getData(color); |
| 31 |
|
| 32 |
for (iterator_type it = myLocalElements.begin(); it != myLocalElements.end(); ++it) { |
| 33 |
const int i_local = *it; |
| 34 |
const int aux = BAP->getRowMap()->getLocalElement(i_local); |
| 35 |
|
| 36 |
std::vector<int> BAP_inds; |
| 37 |
std::vector<double> BAP_vals; |
| 38 |
|
| 39 |
int aux2; |
| 40 |
|
| 41 |
if ((mypid - 1) % 3 == color && (mypid - 1) >= 0 && (mypid - 1) < tpetra_prolong->getGlobalNumCols()) |
| 42 |
aux2 = BAP->getColMap()->getLocalElement(mypid - 1); |
| 43 |
else if ((mypid - 2) % 3 == color && (mypid - 2) >= 0 && (mypid - 2) < tpetra_prolong->getGlobalNumCols()) |
| 44 |
aux2 = BAP->getColMap()->getLocalElement(mypid - 2); |
| 45 |
else if ((mypid) % 3 == color && (mypid) >= 0 && (mypid) < tpetra_prolong->getGlobalNumCols()) |
| 46 |
aux2 = BAP->getColMap()->getLocalElement(mypid); |
| 47 |
|
| 48 |
if (aux2 >= 0) { |
| 49 |
BAP_inds.emplace_back(aux2); |
| 50 |
BAP_vals.emplace_back(localBAP[aux]); |
| 51 |
BAP->insertLocalValues(aux, BAP_inds, BAP_vals); |
| 52 |
} |
| 53 |
} |
| 54 |
} |
| 55 |
} |
| 56 |
|
| 57 |
void BAP2D(Teuchos::RCP<tpetra_matrix_type> BAP, Teuchos::RCP<tpetra_matrix_type> tpetra_prolong, Teuchos::RCP<tpetra_multivector_type> BAP_shrunk, Teuchos::RCP<const Teuchos::Comm<int> > comm, int ndx) { |
| 58 |
// INPUT: BAP = matrix where the uncompressed version of B_DD * A_h * Ptentative must be stored |
| 59 |
// INPUT: tpetra_prolong = Ptentative |
| 60 |
// INPUT: BAP_shrunk = Tpetra:MultiVector contatining the shrunk version of B_DD * A_h * Ptentative resulting from domain decomposition with coloring |
| 61 |
// INPUT: comm = MPI communicator (MPI_COMM_WORLD) |
| 62 |
// INPUT: ndx = number of domains along x-direction |
| 63 |
|
| 64 |
Teuchos::ArrayView<const int> myLocalElements = BAP->getRowMap()->getLocalElementList(); |
| 65 |
int mypid = comm->getRank(); |
| 66 |
int brick_id = mypid; |
| 67 |
int shifted_id = brick_id - 1; |
| 68 |
|
| 69 |
if (mypid > 0) { |
| 70 |
for (int color = 0; color < 9; ++color) { |
| 71 |
int neighbour = -1; |
| 72 |
Teuchos::ArrayRCP<const double> localBAP = BAP_shrunk->getData(color); |
| 73 |
|
| 74 |
// The following if statements control the neighbours of a subdomain in a 2D brick partitioned mesh |
| 75 |
if (coloring2D(brick_id, ndx) == color && (shifted_id) >= 0 && (shifted_id) < tpetra_prolong->getGlobalNumCols()) |
| 76 |
neighbour = (shifted_id); |
| 77 |
|
| 78 |
else if (coloring2D(brick_id - 1, ndx) == color && (shifted_id - 1) >= 0 && (shifted_id - 1) < tpetra_prolong->getGlobalNumCols()) |
| 79 |
neighbour = (shifted_id - 1); |
| 80 |
|
| 81 |
else if (coloring2D(brick_id + 1, ndx) == color && (shifted_id + 1) >= 0 && (shifted_id + 1) < tpetra_prolong->getGlobalNumCols()) |
| 82 |
neighbour = (shifted_id + 1); |
| 83 |
|
| 84 |
else if (coloring2D(brick_id - ndx, ndx) == color && (shifted_id - ndx) >= 0 && (shifted_id - ndx) < tpetra_prolong->getGlobalNumCols()) |
| 85 |
neighbour = (shifted_id - ndx); |
| 86 |
|
| 87 |
else if (coloring2D(brick_id - ndx - 1, ndx) == color && (shifted_id - ndx - 1) >= 0 && (shifted_id - ndx - 1) < tpetra_prolong->getGlobalNumCols()) |
| 88 |
neighbour = (shifted_id - ndx - 1); |
| 89 |
|
| 90 |
else if (coloring2D(brick_id - ndx + 1, ndx) == color && (shifted_id - ndx + 1) >= 0 && (shifted_id - ndx + 1) < tpetra_prolong->getGlobalNumCols()) |
| 91 |
neighbour = (shifted_id - ndx + 1); |
| 92 |
|
| 93 |
else if (coloring2D(brick_id + ndx, ndx) == color && (shifted_id + ndx) >= 0 && (shifted_id + ndx) < tpetra_prolong->getGlobalNumCols()) |
| 94 |
neighbour = (shifted_id + ndx); |
| 95 |
|
| 96 |
else if (coloring2D(brick_id + ndx - 1, ndx) == color && (shifted_id + ndx - 1) >= 0 && (shifted_id + ndx - 1) < tpetra_prolong->getGlobalNumCols()) |
| 97 |
neighbour = (shifted_id + ndx - 1); |
| 98 |
|
| 99 |
else if (coloring2D(brick_id + ndx + 1, ndx) == color && (shifted_id + ndx + 1) >= 0 && (shifted_id + ndx + 1) < tpetra_prolong->getGlobalNumCols()) |
| 100 |
neighbour = (shifted_id + ndx + 1); |
| 101 |
|
| 102 |
// in case neighbour>=0, it means that the current MPI processor (=subdomain) has a neighbour with the color analyzed at the current for-loop iteration |
| 103 |
// otherwise the current MPI processor sits on the boundary of the mesh and it has less than 8 neighbours. |
| 104 |
if (neighbour >= 0) { |
| 105 |
for (iterator_type it = myLocalElements.begin(); it != myLocalElements.end(); ++it) { |
| 106 |
const int i_local = *it; |
| 107 |
const int aux = BAP->getRowMap()->getLocalElement(i_local); |
| 108 |
|
| 109 |
std::vector<int> BAP_inds; |
| 110 |
std::vector<double> BAP_vals; |
| 111 |
|
| 112 |
int aux2; |
| 113 |
|
| 114 |
aux2 = BAP->getColMap()->getLocalElement(neighbour); |
| 115 |
|
| 116 |
if (aux2 >= 0) { |
| 117 |
BAP_inds.emplace_back(aux2); |
| 118 |
BAP_vals.emplace_back(localBAP[aux]); |
| 119 |
BAP->insertLocalValues(aux, BAP_inds, BAP_vals); |
| 120 |
} |
| 121 |
} |
| 122 |
} |
| 123 |
} |
| 124 |
} |
| 125 |
} |
| 126 |
|
| 127 |
void BAP3D(Teuchos::RCP<tpetra_matrix_type> BAP, Teuchos::RCP<tpetra_matrix_type> tpetra_prolong, Teuchos::RCP<tpetra_multivector_type> BAP_shrunk, Teuchos::RCP<const Teuchos::Comm<int> > comm, int ndx, int ndy) { |
| 128 |
// INPUT: BAP = matrix where the uncompressed version of B_DD * A_h * Ptentative must be stored |
| 129 |
// INPUT: tpetra_prolong = Ptentative |
| 130 |
// INPUT: BAP_shrunk = Tpetra:MultiVector contatining the shrunk version of B_DD * A_h * Ptentative resulting from domain decomposition with coloring |
| 131 |
// INPUT: comm = MPI communicator (MPI_COMM_WORLD) |
| 132 |
// INPUT: ndx = number of domains along x-direction |
| 133 |
// INPUT: ndy = number of domains along y-direction |
| 134 |
|
| 135 |
Teuchos::ArrayView<const int> myLocalElements = BAP->getRowMap()->getLocalElementList(); |
| 136 |
int mypid = comm->getRank(); |
| 137 |
int brick_id = mypid; |
| 138 |
int shifted_id = brick_id - 1; |
| 139 |
|
| 140 |
if (mypid > 0) { |
| 141 |
for (int color = 0; color < 27; ++color) { |
| 142 |
int neighbour = -1; |
| 143 |
Teuchos::ArrayRCP<const double> localBAP = BAP_shrunk->getData(color); |
| 144 |
|
| 145 |
// The following if statements control the neighbours of a subdomain in a 3D brick partitioned mesh |
| 146 |
// Each subdomains is incorporated in a 3x3x3 cube which is sliced into 3 squares living on three different planes |
| 147 |
// The neighbours of a subdomain are checked plane by plane: in total there are three planes to span |
| 148 |
// |
| 149 |
// Identification of neighbours that are on the current plane |
| 150 |
if (coloring3D(brick_id, ndx, ndy) == color && (shifted_id) >= 0 && (shifted_id) < tpetra_prolong->getGlobalNumCols()) |
| 151 |
neighbour = (shifted_id); |
| 152 |
else if (coloring3D(brick_id - 1, ndx, ndy) == color && (shifted_id - 1) >= 0 && (shifted_id - 1) < tpetra_prolong->getGlobalNumCols()) |
| 153 |
neighbour = (shifted_id - 1); |
| 154 |
else if (coloring3D(brick_id + 1, ndx, ndy) == color && (shifted_id + 1) >= 0 && (shifted_id + 1) < tpetra_prolong->getGlobalNumCols()) |
| 155 |
neighbour = (shifted_id + 1); |
| 156 |
else if (coloring3D(brick_id - ndx, ndx, ndy) == color && (shifted_id - ndx) >= 0 && (shifted_id - ndx) < tpetra_prolong->getGlobalNumCols()) |
| 157 |
neighbour = (shifted_id - ndx); |
| 158 |
else if (coloring3D(brick_id - ndx - 1, ndx, ndy) == color && (shifted_id - ndx - 1) >= 0 && (shifted_id - ndx - 1) < tpetra_prolong->getGlobalNumCols()) |
| 159 |
neighbour = (shifted_id - ndx - 1); |
| 160 |
else if (coloring3D(brick_id - ndx + 1, ndx, ndy) == color && (shifted_id - ndx + 1) >= 0 && (shifted_id - ndx + 1) < tpetra_prolong->getGlobalNumCols()) |
| 161 |
neighbour = (shifted_id - ndx + 1); |
| 162 |
else if (coloring3D(brick_id + ndx, ndx, ndy) == color && (shifted_id + ndx) >= 0 && (shifted_id + ndx) < tpetra_prolong->getGlobalNumCols()) |
| 163 |
neighbour = (shifted_id + ndx); |
| 164 |
else if (coloring3D(brick_id + ndx - 1, ndx, ndy) == color && (shifted_id + ndx - 1) >= 0 && (shifted_id + ndx - 1) < tpetra_prolong->getGlobalNumCols()) |
| 165 |
neighbour = (shifted_id + ndx - 1); |
| 166 |
else if (coloring3D(brick_id + ndx + 1, ndx, ndy) == color && (shifted_id + ndx + 1) >= 0 && (shifted_id + ndx + 1) < tpetra_prolong->getGlobalNumCols()) |
| 167 |
neighbour = (shifted_id + ndx + 1); |
| 168 |
|
| 169 |
// Identification of the neighbours that are on the plane below |
| 170 |
else if (coloring3D(brick_id - ndx * ndy, ndx, ndy) == color && (shifted_id - ndx * ndy) >= 0 && (shifted_id - ndx * ndy) < tpetra_prolong->getGlobalNumCols()) |
| 171 |
neighbour = (shifted_id - ndx * ndy); |
| 172 |
else if (coloring3D(brick_id - 1 - ndx * ndy, ndx, ndy) == color && (shifted_id - 1 - ndx * ndy) >= 0 && (shifted_id - 1 - ndx * ndy) < tpetra_prolong->getGlobalNumCols()) |
| 173 |
neighbour = (shifted_id - 1 - ndx * ndy); |
| 174 |
else if (coloring3D(brick_id + 1 - ndx * ndy, ndx, ndy) == color && (shifted_id + 1 - ndx * ndy) >= 0 && (shifted_id + 1 - ndx * ndy) < tpetra_prolong->getGlobalNumCols()) |
| 175 |
neighbour = (shifted_id + 1 - ndx * ndy); |
| 176 |
else if (coloring3D(brick_id - ndx - ndx * ndy, ndx, ndy) == color && (shifted_id - ndx - ndx * ndy) >= 0 && (shifted_id - ndx - ndx * ndy) < tpetra_prolong->getGlobalNumCols()) |
| 177 |
neighbour = (shifted_id - ndx - ndx * ndy); |
| 178 |
else if (coloring3D(brick_id - ndx - 1 - ndx * ndy, ndx, ndy) == color && (shifted_id - ndx - 1 - ndx * ndy) >= 0 && (shifted_id - ndx - 1 - ndx * ndy) < tpetra_prolong->getGlobalNumCols()) |
| 179 |
neighbour = (shifted_id - ndx - 1 - ndx * ndy); |
| 180 |
else if (coloring3D(brick_id - ndx + 1 - ndx * ndy, ndx, ndy) == color && (shifted_id - ndx + 1 - ndx * ndy) >= 0 && (shifted_id - ndx + 1 - ndx * ndy) < tpetra_prolong->getGlobalNumCols()) |
| 181 |
neighbour = (shifted_id - ndx + 1 - ndx * ndy); |
| 182 |
else if (coloring3D(brick_id + ndx - ndx * ndy, ndx, ndy) == color && (shifted_id + ndx - ndx * ndy) >= 0 && (shifted_id + ndx - ndx * ndy) < tpetra_prolong->getGlobalNumCols()) |
| 183 |
neighbour = (shifted_id + ndx - ndx * ndy); |
| 184 |
else if (coloring3D(brick_id + ndx - 1 - ndx * ndy, ndx, ndy) == color && (shifted_id + ndx - 1 - ndx * ndy) >= 0 && (shifted_id + ndx - 1 - ndx * ndy) < tpetra_prolong->getGlobalNumCols()) |
| 185 |
neighbour = (shifted_id + ndx - 1 - ndx * ndy); |
| 186 |
else if (coloring3D(brick_id + ndx + 1 - ndx * ndy, ndx, ndy) == color && (shifted_id + ndx + 1 - ndx * ndy) >= 0 && (shifted_id + ndx + 1 - ndx * ndy) < tpetra_prolong->getGlobalNumCols()) |
| 187 |
neighbour = (shifted_id + ndx + 1 - ndx * ndy); |
| 188 |
|
| 189 |
// Identification of the neighbours that are on the plane above |
| 190 |
else if (coloring3D(brick_id + ndx * ndy, ndx, ndy) == color && (shifted_id + ndx * ndy) >= 0 && (shifted_id + ndx * ndy) < tpetra_prolong->getGlobalNumCols()) |
| 191 |
neighbour = (shifted_id + ndx * ndy); |
| 192 |
else if (coloring3D(brick_id - 1 + ndx * ndy, ndx, ndy) == color && (shifted_id - 1 + ndx * ndy) >= 0 && (shifted_id - 1 + ndx * ndy) < tpetra_prolong->getGlobalNumCols()) |
| 193 |
neighbour = (shifted_id - 1 + ndx * ndy); |
| 194 |
else if (coloring3D(brick_id + 1 + ndx * ndy, ndx, ndy) == color && (shifted_id + 1 + ndx * ndy) >= 0 && (shifted_id + 1 + ndx * ndy) < tpetra_prolong->getGlobalNumCols()) |
| 195 |
neighbour = (shifted_id + 1 + ndx * ndy); |
| 196 |
else if (coloring3D(brick_id - ndx + ndx * ndy, ndx, ndy) == color && (shifted_id - ndx + ndx * ndy) >= 0 && (shifted_id - ndx + ndx * ndy) < tpetra_prolong->getGlobalNumCols()) |
| 197 |
neighbour = (shifted_id - ndx + ndx * ndy); |
| 198 |
else if (coloring3D(brick_id - ndx - 1 + ndx * ndy, ndx, ndy) == color && (shifted_id - ndx - 1 + ndx * ndy) >= 0 && (shifted_id - ndx - 1 + ndx * ndy) < tpetra_prolong->getGlobalNumCols()) |
| 199 |
neighbour = (shifted_id - ndx - 1 + ndx * ndy); |
| 200 |
else if (coloring3D(brick_id - ndx + 1 + ndx * ndy, ndx, ndy) == color && (shifted_id - ndx + 1 + ndx * ndy) >= 0 && (shifted_id - ndx + 1 + ndx * ndy) < tpetra_prolong->getGlobalNumCols()) |
| 201 |
neighbour = (shifted_id - ndx + 1 + ndx * ndy); |
| 202 |
else if (coloring3D(brick_id + ndx + ndx * ndy, ndx, ndy) == color && (shifted_id + ndx + ndx * ndy) >= 0 && (shifted_id + ndx + ndx * ndy) < tpetra_prolong->getGlobalNumCols()) |
| 203 |
neighbour = (shifted_id + ndx + ndx * ndy); |
| 204 |
else if (coloring3D(brick_id + ndx - 1 + ndx * ndy, ndx, ndy) == color && (shifted_id + ndx - 1 + ndx * ndy) >= 0 && (shifted_id + ndx - 1 + ndx * ndy) < tpetra_prolong->getGlobalNumCols()) |
| 205 |
neighbour = (shifted_id + ndx - 1 + ndx * ndy); |
| 206 |
else if (coloring3D(brick_id + ndx + 1 + ndx * ndy, ndx, ndy) == color && (shifted_id + ndx + 1 + ndx * ndy) >= 0 && (shifted_id + ndx + 1 + ndx * ndy) < tpetra_prolong->getGlobalNumCols()) |
| 207 |
neighbour = (shifted_id + ndx + 1 + ndx * ndy); |
| 208 |
|
| 209 |
// in case neighbour>=0, it means that the current MPI processor (=subdomain) has a neighbour with the color analyzed at the current for-loop iteration |
| 210 |
// otherwise the current MPI processor sits on the boundary of the mesh and it has less than 26 neighbours. |
| 211 |
if (neighbour >= 0) { |
| 212 |
for (iterator_type it = myLocalElements.begin(); it != myLocalElements.end(); ++it) { |
| 213 |
const int i_local = *it; |
| 214 |
const int aux = BAP->getRowMap()->getLocalElement(i_local); |
| 215 |
|
| 216 |
std::vector<int> BAP_inds; |
| 217 |
std::vector<double> BAP_vals; |
| 218 |
|
| 219 |
int aux2; |
| 220 |
|
| 221 |
aux2 = BAP->getColMap()->getLocalElement(neighbour); |
| 222 |
|
| 223 |
if (aux2 >= 0) { |
| 224 |
BAP_inds.emplace_back(aux2); |
| 225 |
BAP_vals.emplace_back(localBAP[aux]); |
| 226 |
BAP->insertLocalValues(aux, BAP_inds, BAP_vals); |
| 227 |
} else |
| 228 |
std::cout << "ID: " << mypid << " does not reach " << neighbour + 1 << std::endl; |
| 229 |
} |
| 230 |
} |
| 231 |
} |
| 232 |
} |
| 233 |
} |