| 1 | #ifdef HAVE_CONFIG_H
|
|---|
| 2 | #include <config.h>
|
|---|
| 3 | #endif
|
|---|
| 4 |
|
|---|
| 5 | #ifdef HAVE_MPI
|
|---|
| 6 | #include <mpi.h>
|
|---|
| 7 | #ifdef HAVE_MARMOT
|
|---|
| 8 | #include <enhancempicalls.h>
|
|---|
| 9 | #include <sourceinfompicalls.h>
|
|---|
| 10 | #endif
|
|---|
| 11 | #else
|
|---|
| 12 | #error MPI is needed to compile CommMPISettings
|
|---|
| 13 | #endif
|
|---|
| 14 |
|
|---|
| 15 | #include <cassert>
|
|---|
| 16 |
|
|---|
| 17 | #include "comm/comm.hpp"
|
|---|
| 18 | #include "comm/mpi/settings.hpp"
|
|---|
| 19 | #include "grid/multigrid.hpp"
|
|---|
| 20 | #include "grid/tempgrid.hpp"
|
|---|
| 21 | #include "mg.hpp"
|
|---|
| 22 |
|
|---|
| 23 | using namespace VMG;
|
|---|
| 24 |
|
|---|
| 25 | Grid& VMG::MPI::Settings::FinerGrid(const Grid& grid)
|
|---|
| 26 | {
|
|---|
| 27 | assert(finer_grids.find(&grid) != finer_grids.end());
|
|---|
| 28 | return *finer_grids.find(&grid)->second;
|
|---|
| 29 | }
|
|---|
| 30 |
|
|---|
| 31 | Grid& VMG::MPI::Settings::CoarserGrid(const Grid& grid)
|
|---|
| 32 | {
|
|---|
| 33 | assert(coarser_grids.find(&grid) != coarser_grids.end());
|
|---|
| 34 | return *coarser_grids.find(&grid)->second;
|
|---|
| 35 | }
|
|---|
| 36 |
|
|---|
| 37 | MPI_Comm VMG::MPI::Settings::CommunicatorGlobal(const Grid& grid) const
|
|---|
| 38 | {
|
|---|
| 39 | assert(communicators_global.find(grid.Level()) != communicators_global.end());
|
|---|
| 40 | return communicators_global.find(grid.Level())->second;
|
|---|
| 41 | }
|
|---|
| 42 |
|
|---|
| 43 | MPI_Comm VMG::MPI::Settings::CommunicatorLocal(const Grid& grid) const
|
|---|
| 44 | {
|
|---|
| 45 | KeyUnsorted k(grid, 0);
|
|---|
| 46 | assert(communicators_local.find(k) != communicators_local.end());
|
|---|
| 47 | return communicators_local.find(k)->second;
|
|---|
| 48 | }
|
|---|
| 49 |
|
|---|
| 50 | VMG::MPI::DatatypesGlobal& VMG::MPI::Settings::DatatypesGlobal(const Grid& grid_old, const Grid& grid_new, const int& direction)
|
|---|
| 51 | {
|
|---|
| 52 | KeyUnsorted k(grid_old, grid_new, direction);
|
|---|
| 53 | assert(datatypes_global.find(k) != datatypes_global.end());
|
|---|
| 54 | return datatypes_global.find(k)->second;
|
|---|
| 55 | }
|
|---|
| 56 |
|
|---|
| 57 | VMG::MPI::DatatypesLocal& VMG::MPI::Settings::DatatypesLocal(const Grid& grid)
|
|---|
| 58 | {
|
|---|
| 59 | KeyUnsorted k(grid, 0);
|
|---|
| 60 | assert(datatypes_local.find(k) != datatypes_local.end());
|
|---|
| 61 | return datatypes_local.find(k)->second;
|
|---|
| 62 | }
|
|---|
| 63 |
|
|---|
| 64 | void VMG::MPI::Settings::ComputeSettings(Multigrid& sol, Multigrid& rhs, MPI_Comm& comm_global)
|
|---|
| 65 | {
|
|---|
| 66 |
|
|---|
| 67 | std::map<const Grid*, Grid*>::const_iterator grid_iter;
|
|---|
| 68 |
|
|---|
| 69 | Comm& comm = *MG::GetComm();
|
|---|
| 70 |
|
|---|
| 71 | /*
|
|---|
| 72 | * Create coarser grids
|
|---|
| 73 | */
|
|---|
| 74 | for (int i=sol.MaxLevel(); i>sol.MinLevel(); --i) {
|
|---|
| 75 |
|
|---|
| 76 | TempGrid* temp_grid = new TempGrid();
|
|---|
| 77 | temp_grid->SetPropertiesToCoarser(sol(i), comm.BoundaryConditions());
|
|---|
| 78 |
|
|---|
| 79 | if (temp_grid->Global().LocalBegin().IsComponentwiseGreaterOrEqual(sol(i-1).Global().LocalBegin()) &&
|
|---|
| 80 | temp_grid->Global().LocalBegin().IsComponentwiseLessOrEqual(sol(i-1).Global().LocalEnd()) &&
|
|---|
| 81 | temp_grid->Global().LocalEnd().IsComponentwiseGreaterOrEqual(sol(i-1).Global().LocalBegin()) &&
|
|---|
| 82 | temp_grid->Global().LocalEnd().IsComponentwiseLessOrEqual(sol(i-1).Global().LocalEnd())) {
|
|---|
| 83 | delete temp_grid;
|
|---|
| 84 | coarser_grids.insert(std::make_pair(&sol(i), &sol(i-1)));
|
|---|
| 85 | }else {
|
|---|
| 86 | coarser_grids.insert(std::make_pair(&sol(i), temp_grid));
|
|---|
| 87 | }
|
|---|
| 88 |
|
|---|
| 89 | }
|
|---|
| 90 |
|
|---|
| 91 | for (int i=rhs.MaxLevel(); i>rhs.MinLevel(); --i) {
|
|---|
| 92 |
|
|---|
| 93 | TempGrid* temp_grid = new TempGrid();
|
|---|
| 94 | temp_grid->SetPropertiesToCoarser(rhs(i), comm.BoundaryConditions());
|
|---|
| 95 |
|
|---|
| 96 | if (temp_grid->Global().LocalBegin().IsComponentwiseGreaterOrEqual(sol(i-1).Global().LocalBegin()) &&
|
|---|
| 97 | temp_grid->Global().LocalBegin().IsComponentwiseLessOrEqual(sol(i-1).Global().LocalEnd()) &&
|
|---|
| 98 | temp_grid->Global().LocalEnd().IsComponentwiseGreaterOrEqual(sol(i-1).Global().LocalBegin()) &&
|
|---|
| 99 | temp_grid->Global().LocalEnd().IsComponentwiseLessOrEqual(sol(i-1).Global().LocalEnd())) {
|
|---|
| 100 | delete temp_grid;
|
|---|
| 101 | coarser_grids.insert(std::make_pair(&rhs(i), &rhs(i-1)));
|
|---|
| 102 | }else {
|
|---|
| 103 | coarser_grids.insert(std::make_pair(&rhs(i), temp_grid));
|
|---|
| 104 | }
|
|---|
| 105 |
|
|---|
| 106 | }
|
|---|
| 107 |
|
|---|
| 108 | /*
|
|---|
| 109 | * Create finer grids
|
|---|
| 110 | */
|
|---|
| 111 | for (int i=sol.MinLevel(); i<sol.MaxLevel(); ++i) {
|
|---|
| 112 |
|
|---|
| 113 | TempGrid* temp_grid = new TempGrid();
|
|---|
| 114 | temp_grid->SetPropertiesToFiner(sol(i), comm.BoundaryConditions());
|
|---|
| 115 |
|
|---|
| 116 | if (temp_grid->Global().LocalBegin() == sol(i+1).Global().LocalBegin() &&
|
|---|
| 117 | temp_grid->Global().LocalEnd() == sol(i+1).Global().LocalEnd()) {
|
|---|
| 118 | delete temp_grid;
|
|---|
| 119 | finer_grids.insert(std::make_pair(&sol(i), &sol(i+1)));
|
|---|
| 120 | }else {
|
|---|
| 121 | finer_grids.insert(std::make_pair(&sol(i), temp_grid));
|
|---|
| 122 | }
|
|---|
| 123 |
|
|---|
| 124 | }
|
|---|
| 125 |
|
|---|
| 126 | for (int i=rhs.MinLevel(); i<rhs.MaxLevel(); ++i) {
|
|---|
| 127 |
|
|---|
| 128 | TempGrid* temp_grid = new TempGrid();
|
|---|
| 129 | temp_grid->SetPropertiesToFiner(rhs(i), comm.BoundaryConditions());
|
|---|
| 130 |
|
|---|
| 131 | if (temp_grid->Global().LocalBegin() == rhs(i+1).Global().LocalBegin() &&
|
|---|
| 132 | temp_grid->Global().LocalEnd() == rhs(i+1).Global().LocalEnd()) {
|
|---|
| 133 | delete temp_grid;
|
|---|
| 134 | finer_grids.insert(std::make_pair(&rhs(i), &rhs(i+1)));
|
|---|
| 135 | }else {
|
|---|
| 136 | finer_grids.insert(std::make_pair(&rhs(i), temp_grid));
|
|---|
| 137 | }
|
|---|
| 138 |
|
|---|
| 139 | }
|
|---|
| 140 |
|
|---|
| 141 | /*
|
|---|
| 142 | * Create global communicators
|
|---|
| 143 | */
|
|---|
| 144 | for (int i=sol.MinLevel()+1; i<sol.MaxLevel(); ++i)
|
|---|
| 145 | CreateGlobalCommunicator(comm_global, &sol(i), &CoarserGrid(sol(i+1)), &FinerGrid(sol(i-1)));
|
|---|
| 146 |
|
|---|
| 147 | CreateGlobalCommunicator(comm_global, &sol(sol.MinLevel()), &CoarserGrid(sol(sol.MinLevel()+1)));
|
|---|
| 148 | CreateGlobalCommunicator(comm_global, &sol(sol.MaxLevel()), &FinerGrid(sol(sol.MaxLevel()-1)));
|
|---|
| 149 |
|
|---|
| 150 | MPI_Comm my_comm_global;
|
|---|
| 151 | MPI_Comm_dup(comm_global, &my_comm_global);
|
|---|
| 152 | communicators_global.insert(std::make_pair(0, my_comm_global));
|
|---|
| 153 |
|
|---|
| 154 | /*
|
|---|
| 155 | * Create local communicators
|
|---|
| 156 | */
|
|---|
| 157 | for (int i=sol.MinLevel(); i<=sol.MaxLevel(); ++i)
|
|---|
| 158 | CreateLocalCommunicator(comm_global, sol(i));
|
|---|
| 159 |
|
|---|
| 160 | for (int i=sol.MinLevel(); i<sol.MaxLevel(); ++i)
|
|---|
| 161 | CreateLocalCommunicator(comm_global, FinerGrid(sol(i)));
|
|---|
| 162 |
|
|---|
| 163 | for (int i=sol.MaxLevel(); i>sol.MinLevel(); --i)
|
|---|
| 164 | CreateLocalCommunicator(comm_global, CoarserGrid(sol(i)));
|
|---|
| 165 |
|
|---|
| 166 | if (MG::GetFactory().TestObject("PARTICLE_NEAR_FIELD_CELLS"))
|
|---|
| 167 | CreateLocalCommunicator(comm_global, comm.GetParticleGrid());
|
|---|
| 168 |
|
|---|
| 169 | /*
|
|---|
| 170 | * Create single grid datatypes
|
|---|
| 171 | */
|
|---|
| 172 | for (int i=sol.MinLevel(); i<=sol.MaxLevel(); ++i)
|
|---|
| 173 | datatypes_local.insert(std::make_pair(KeyUnsorted(sol(i), 0), VMG::MPI::DatatypesLocal(sol(i), CommunicatorLocal(sol(i)), true)));
|
|---|
| 174 |
|
|---|
| 175 | for (grid_iter=finer_grids.begin(); grid_iter!=finer_grids.end(); ++grid_iter)
|
|---|
| 176 | datatypes_local.insert(std::make_pair(KeyUnsorted(*grid_iter->second, 0), VMG::MPI::DatatypesLocal(*grid_iter->second, CommunicatorLocal(*grid_iter->second), true)));
|
|---|
| 177 |
|
|---|
| 178 | for (grid_iter=coarser_grids.begin(); grid_iter!=coarser_grids.end(); ++grid_iter)
|
|---|
| 179 | datatypes_local.insert(std::make_pair(KeyUnsorted(*grid_iter->second, 0), VMG::MPI::DatatypesLocal(*grid_iter->second, CommunicatorLocal(*grid_iter->second), true)));
|
|---|
| 180 |
|
|---|
| 181 | if (MG::GetFactory().TestObject("PARTICLE_NEAR_FIELD_CELLS"))
|
|---|
| 182 | datatypes_local.insert(std::make_pair(KeyUnsorted(comm.GetParticleGrid(), 0), VMG::MPI::DatatypesLocal(comm.GetParticleGrid(), CommunicatorLocal(comm.GetParticleGrid()), true)));
|
|---|
| 183 |
|
|---|
| 184 | /*
|
|---|
| 185 | * Create two grid datatypes
|
|---|
| 186 | */
|
|---|
| 187 | for (int i=sol.MinLevel(); i<sol.MaxLevel(); ++i) {
|
|---|
| 188 | AddDatatypeGlobal(sol(i), CoarserGrid(sol(i+1)), 0);
|
|---|
| 189 | AddDatatypeGlobal(CoarserGrid(sol(i+1)), sol(i), 1);
|
|---|
| 190 | }
|
|---|
| 191 |
|
|---|
| 192 | for (int i=sol.MaxLevel(); i>sol.MinLevel(); --i) {
|
|---|
| 193 | AddDatatypeGlobal(sol(i), FinerGrid(sol(i-1)), 0);
|
|---|
| 194 | AddDatatypeGlobal(FinerGrid(sol(i-1)), sol(i), 1);
|
|---|
| 195 | }
|
|---|
| 196 | }
|
|---|
| 197 |
|
|---|
| 198 | void VMG::MPI::Settings::CreateGlobalCommunicator(MPI_Comm& comm_global, const Grid* grid_1, const Grid* grid_2, const Grid* grid_3)
|
|---|
| 199 | {
|
|---|
| 200 | int rank;
|
|---|
| 201 | MPI_Comm comm_new;
|
|---|
| 202 |
|
|---|
| 203 | const bool in_communicator = (grid_1->Global().LocalSize().Product() > 0) ||
|
|---|
| 204 | (grid_2 && grid_2->Global().LocalSize().Product() > 0) ||
|
|---|
| 205 | (grid_3 && grid_3->Global().LocalSize().Product() > 0);
|
|---|
| 206 |
|
|---|
| 207 | MPI_Comm_rank(comm_global, &rank);
|
|---|
| 208 |
|
|---|
| 209 | if (in_communicator) {
|
|---|
| 210 | Index dims, periods, coords;
|
|---|
| 211 | MPI_Comm comm_temp;
|
|---|
| 212 | MPI_Cart_get(comm_global, 3, dims.vec(), periods.vec(), coords.vec());
|
|---|
| 213 | MPI_Comm_split(comm_global, 1, rank, &comm_temp);
|
|---|
| 214 | dims = GlobalDims(comm_temp, coords);
|
|---|
| 215 | MPI_Cart_create(comm_temp, 3, dims.vec(), periods.vec(), 0, &comm_new);
|
|---|
| 216 | MPI_Comm_free(&comm_temp);
|
|---|
| 217 | }else {
|
|---|
| 218 | MPI_Comm_split(comm_global, MPI_UNDEFINED, rank, &comm_new);
|
|---|
| 219 | }
|
|---|
| 220 |
|
|---|
| 221 | communicators_global.insert(std::make_pair(grid_1->Level(), comm_new));
|
|---|
| 222 | }
|
|---|
| 223 |
|
|---|
| 224 | void VMG::MPI::Settings::CreateLocalCommunicator(MPI_Comm& comm_global, const Grid& grid)
|
|---|
| 225 | {
|
|---|
| 226 | int rank, comm_equal;
|
|---|
| 227 | MPI_Comm comm_new;
|
|---|
| 228 | std::set<MPI_Comm>::iterator iter;
|
|---|
| 229 |
|
|---|
| 230 | MPI_Comm_rank(comm_global, &rank);
|
|---|
| 231 |
|
|---|
| 232 | if (grid.Global().LocalSize().Product() > 0) {
|
|---|
| 233 | Index dims, periods, coords;
|
|---|
| 234 | MPI_Comm comm_temp;
|
|---|
| 235 | MPI_Cart_get(comm_global, 3, dims.vec(), periods.vec(), coords.vec());
|
|---|
| 236 | MPI_Comm_split(comm_global, 1, rank, &comm_temp);
|
|---|
| 237 | dims = GlobalDims(comm_temp, coords);
|
|---|
| 238 | MPI_Cart_create(comm_temp, 3, dims.vec(), periods.vec(), 0, &comm_new);
|
|---|
| 239 | MPI_Comm_free(&comm_temp);
|
|---|
| 240 | }else {
|
|---|
| 241 | MPI_Comm_split(comm_global, MPI_UNDEFINED, rank, &comm_new);
|
|---|
| 242 | }
|
|---|
| 243 |
|
|---|
| 244 | if (comm_new != MPI_COMM_NULL) {
|
|---|
| 245 | for (iter=communicators_local_unique.begin(); iter!=communicators_local_unique.end(); ++iter) {
|
|---|
| 246 | if (*iter != MPI_COMM_NULL) {
|
|---|
| 247 | MPI_Comm_compare(comm_new, *iter, &comm_equal);
|
|---|
| 248 | assert(comm_equal != MPI_SIMILAR);
|
|---|
| 249 | if (comm_equal == MPI_IDENT || comm_equal == MPI_CONGRUENT) {
|
|---|
| 250 | MPI_Comm_free(&comm_new);
|
|---|
| 251 | comm_new = *iter;
|
|---|
| 252 | break;
|
|---|
| 253 | }
|
|---|
| 254 | }
|
|---|
| 255 | }
|
|---|
| 256 | }
|
|---|
| 257 |
|
|---|
| 258 | std::pair<std::set<MPI_Comm>::iterator, bool> insert_result = communicators_local_unique.insert(comm_new);
|
|---|
| 259 | communicators_local.insert(std::make_pair(KeyUnsorted(grid, 0), *insert_result.first));
|
|---|
| 260 | }
|
|---|
| 261 |
|
|---|
| 262 | void VMG::MPI::Settings::AddDatatypeGlobal(const Grid& grid_old, const Grid& grid_new, const int& direction)
|
|---|
| 263 | {
|
|---|
| 264 | MPI_Comm comm = CommunicatorGlobal(grid_old);
|
|---|
| 265 | bool dt_is_new = true;
|
|---|
| 266 |
|
|---|
| 267 | // Insert into map
|
|---|
| 268 | std::pair< std::map<VMG::MPI::KeyUnsorted, VMG::MPI::DatatypesGlobal>::iterator, bool > insert_result =
|
|---|
| 269 | datatypes_global.insert(std::make_pair(VMG::MPI::KeyUnsorted(grid_old, grid_new, direction), VMG::MPI::DatatypesGlobal()));
|
|---|
| 270 | VMG::MPI::DatatypesGlobal& dt_global = insert_result.first->second;
|
|---|
| 271 | dt_is_new = insert_result.second;
|
|---|
| 272 |
|
|---|
| 273 |
|
|---|
| 274 | if (comm != MPI_COMM_NULL) {
|
|---|
| 275 |
|
|---|
| 276 | Index begin, end, offset_old, offset_new;
|
|---|
| 277 | int rank, size;
|
|---|
| 278 |
|
|---|
| 279 | MPI_Comm_rank(comm, &rank);
|
|---|
| 280 | MPI_Comm_size(comm, &size);
|
|---|
| 281 |
|
|---|
| 282 | std::vector<int> buffer;
|
|---|
| 283 | buffer.resize(6*size);
|
|---|
| 284 |
|
|---|
| 285 | // Compute local offset for ghost cells
|
|---|
| 286 | for (int i=0; i<3; ++i) {
|
|---|
| 287 | offset_old[i] = (grid_old.Local().HaloSize1()[i] > 0 ? grid_old.Local().Begin()[i] : 0);
|
|---|
| 288 | offset_new[i] = (grid_new.Local().HaloSize1()[i] > 0 ? grid_new.Local().Begin()[i] : 0);
|
|---|
| 289 | }
|
|---|
| 290 |
|
|---|
| 291 | // Publish which grid part this process can offer
|
|---|
| 292 | if (&grid_old == &grid_new) {
|
|---|
| 293 | for (int i=0; i<6; ++i)
|
|---|
| 294 | buffer[6*rank+i] = 0;
|
|---|
| 295 | }else {
|
|---|
| 296 | for (int i=0; i<3; ++i) {
|
|---|
| 297 | buffer[6*rank+i] = grid_old.Global().LocalBegin()[i];
|
|---|
| 298 | buffer[6*rank+i+3] = grid_old.Global().LocalEnd()[i];
|
|---|
| 299 | }
|
|---|
| 300 | }
|
|---|
| 301 |
|
|---|
| 302 | MPI_Allgather(MPI_IN_PLACE, 6, MPI_INT, &buffer.front(), 6, MPI_INT, comm);
|
|---|
| 303 |
|
|---|
| 304 | if (dt_is_new) {
|
|---|
| 305 |
|
|---|
| 306 | // Decide who offers a useful grid part
|
|---|
| 307 | for (int i=0; i<size; ++i) {
|
|---|
| 308 | for (int j=0; j<3; ++j) {
|
|---|
| 309 | begin[j] = buffer[6*i+j];
|
|---|
| 310 | end[j] = buffer[6*i+j+3];
|
|---|
| 311 | }
|
|---|
| 312 |
|
|---|
| 313 | begin = begin.Clamp(grid_new.Global().LocalBegin(), grid_new.Global().LocalEnd());
|
|---|
| 314 | end = end.Clamp(grid_new.Global().LocalBegin(), grid_new.Global().LocalEnd());
|
|---|
| 315 |
|
|---|
| 316 | if ((end-begin).Product() > 0) {
|
|---|
| 317 | // This process has a useful part
|
|---|
| 318 | dt_global.Receive().push_back(VMG::MPI::Datatype(grid_new.Local().SizeTotal(),
|
|---|
| 319 | end - begin,
|
|---|
| 320 | begin - grid_new.Global().LocalBegin() + offset_new,
|
|---|
| 321 | i, 0, 0, true));
|
|---|
| 322 | }
|
|---|
| 323 | }
|
|---|
| 324 | }
|
|---|
| 325 |
|
|---|
| 326 | // Publish which grid parts this process needs
|
|---|
| 327 | for (int i=0; i<3; ++i) {
|
|---|
| 328 | buffer[6*rank+i] = grid_new.Global().LocalBegin()[i];
|
|---|
| 329 | buffer[6*rank+i+3] = grid_new.Global().LocalEnd()[i];
|
|---|
| 330 | }
|
|---|
| 331 |
|
|---|
| 332 | MPI_Allgather(MPI_IN_PLACE, 6, MPI_INT, &buffer.front(), 6, MPI_INT, comm);
|
|---|
| 333 |
|
|---|
| 334 | if (dt_is_new) {
|
|---|
| 335 |
|
|---|
| 336 | // Decide who needs a part of my grid
|
|---|
| 337 | for (int i=0; i<size; ++i) {
|
|---|
| 338 |
|
|---|
| 339 | if ((i == rank) && (&grid_old == &grid_new))
|
|---|
| 340 | continue;
|
|---|
| 341 |
|
|---|
| 342 | for (int j=0; j<3; ++j) {
|
|---|
| 343 | begin[j] = buffer[6*i+j];
|
|---|
| 344 | end[j] = buffer[6*i+j+3];
|
|---|
| 345 | }
|
|---|
| 346 |
|
|---|
| 347 | begin = begin.Clamp(grid_old.Global().LocalBegin(), grid_old.Global().LocalEnd());
|
|---|
| 348 | end = end.Clamp(grid_old.Global().LocalBegin(), grid_old.Global().LocalEnd());
|
|---|
| 349 |
|
|---|
| 350 | if ((end-begin).Product() > 0) {
|
|---|
| 351 | // This process needs one of my parts
|
|---|
| 352 | dt_global.Send().push_back(VMG::MPI::Datatype(grid_old.Local().SizeTotal(),
|
|---|
| 353 | end - begin,
|
|---|
| 354 | begin - grid_old.Global().LocalBegin() + offset_old,
|
|---|
| 355 | i, 0, 0, true));
|
|---|
| 356 | }
|
|---|
| 357 | }
|
|---|
| 358 | }
|
|---|
| 359 | }
|
|---|
| 360 | }
|
|---|
| 361 |
|
|---|
| 362 | MPI_Datatype& VMG::MPI::Settings::Datatype(const Index& begin, const Index& end,
|
|---|
| 363 | const Index& size_local, const Index& size_global,
|
|---|
| 364 | const int& level)
|
|---|
| 365 | {
|
|---|
| 366 | KeyUnsorted k(begin, end, size_local, size_global, level, 0);
|
|---|
| 367 | std::map<KeyUnsorted, MPI_Datatype>::iterator iter = datatypes.find(k);
|
|---|
| 368 |
|
|---|
| 369 | if (iter != datatypes.end())
|
|---|
| 370 | return iter->second;
|
|---|
| 371 |
|
|---|
| 372 | MPI_Datatype dt;
|
|---|
| 373 | Index sizes = size_local;
|
|---|
| 374 | Index subsizes = end - begin;
|
|---|
| 375 | Index starts = begin;
|
|---|
| 376 |
|
|---|
| 377 | MPI_Type_create_subarray(3, sizes.vec(), subsizes.vec(), starts.vec(), MPI_ORDER_C, MPI_DOUBLE, &dt);
|
|---|
| 378 | MPI_Type_commit(&dt);
|
|---|
| 379 |
|
|---|
| 380 | return datatypes.insert(std::make_pair(k, dt)).first->second;
|
|---|
| 381 | }
|
|---|
| 382 |
|
|---|
| 383 | Index VMG::MPI::Settings::GlobalDims(MPI_Comm comm, Index pos)
|
|---|
| 384 | {
|
|---|
| 385 | std::set<int> unique_set[3];
|
|---|
| 386 | Index dims;
|
|---|
| 387 |
|
|---|
| 388 | int size;
|
|---|
| 389 | MPI_Comm_size(comm, &size);
|
|---|
| 390 |
|
|---|
| 391 | int* coordinates = new int[3*size];
|
|---|
| 392 |
|
|---|
| 393 | MPI_Allgather(pos.vec(), 3, MPI_INT, coordinates, 3, MPI_INT, comm);
|
|---|
| 394 |
|
|---|
| 395 | for (int i=0; i<size; ++i)
|
|---|
| 396 | for (int j=0; j<3; ++j)
|
|---|
| 397 | unique_set[j].insert(coordinates[3*i+j]);
|
|---|
| 398 |
|
|---|
| 399 | for (int j=0; j<3; ++j)
|
|---|
| 400 | dims[j] = static_cast<int>(unique_set[j].size());
|
|---|
| 401 |
|
|---|
| 402 | delete [] coordinates;
|
|---|
| 403 |
|
|---|
| 404 | return dims;
|
|---|
| 405 | }
|
|---|
| 406 |
|
|---|
| 407 | VMG::MPI::Settings::Settings()
|
|---|
| 408 | {
|
|---|
| 409 | }
|
|---|
| 410 |
|
|---|
| 411 | VMG::MPI::Settings::~Settings()
|
|---|
| 412 | {
|
|---|
| 413 | std::map<int, MPI_Comm>::iterator iter_comm_global;
|
|---|
| 414 | for (iter_comm_global=communicators_global.begin(); iter_comm_global!=communicators_global.end(); ++iter_comm_global)
|
|---|
| 415 | if (iter_comm_global->second != MPI_COMM_NULL)
|
|---|
| 416 | MPI_Comm_free(&iter_comm_global->second);
|
|---|
| 417 |
|
|---|
| 418 | /*
|
|---|
| 419 | * We simply copied some communicators so we have to make sure that we free
|
|---|
| 420 | * each communicator exactly once
|
|---|
| 421 | */
|
|---|
| 422 | std::set<MPI_Comm>::iterator iter_comm_set;
|
|---|
| 423 | for (iter_comm_set=communicators_local_unique.begin(); iter_comm_set!=communicators_local_unique.end(); ++iter_comm_set)
|
|---|
| 424 | if (*iter_comm_set != MPI_COMM_NULL) {
|
|---|
| 425 | MPI_Comm comm_temp = *iter_comm_set;
|
|---|
| 426 | MPI_Comm_free(&comm_temp);
|
|---|
| 427 | }
|
|---|
| 428 |
|
|---|
| 429 | std::map<KeyUnsorted, MPI_Datatype>::iterator iter_dt;
|
|---|
| 430 | for (iter_dt=datatypes.begin(); iter_dt!=datatypes.end(); ++iter_dt)
|
|---|
| 431 | if (iter_dt->second != MPI_DATATYPE_NULL)
|
|---|
| 432 | MPI_Type_free(&iter_dt->second);
|
|---|
| 433 |
|
|---|
| 434 | std::map<const Grid*, Grid*>::iterator iter_grid;
|
|---|
| 435 | for (iter_grid=finer_grids.begin(); iter_grid!=finer_grids.end(); ++iter_grid)
|
|---|
| 436 | if (iter_grid->second->Father() == NULL)
|
|---|
| 437 | delete iter_grid->second;
|
|---|
| 438 |
|
|---|
| 439 | for (iter_grid=coarser_grids.begin(); iter_grid!=coarser_grids.end(); ++iter_grid)
|
|---|
| 440 | if (iter_grid->second->Father() == NULL)
|
|---|
| 441 | delete iter_grid->second;
|
|---|
| 442 | }
|
|---|