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