| 1 |
/* |
| 2 |
* (C) Copyright 2013 ECMWF. |
| 3 |
* |
| 4 |
* This software is licensed under the terms of the Apache Licence Version 2.0 |
| 5 |
* which can be obtained at http://www.apache.org/licenses/LICENSE-2.0. |
| 6 |
* In applying this licence, ECMWF does not waive the privileges and immunities |
| 7 |
* granted to it by virtue of its status as an intergovernmental organisation |
| 8 |
* nor does it submit to any jurisdiction. |
| 9 |
*/ |
| 10 |
|
| 11 |
#include "atlas/grid/detail/partitioner/MatchingMeshPartitionerSphericalPolygon.h" |
| 12 |
|
| 13 |
#include <vector> |
| 14 |
|
| 15 |
#include "eckit/log/ProgressTimer.h" |
| 16 |
|
| 17 |
#include "atlas/grid/Grid.h" |
| 18 |
#include "atlas/grid/Iterator.h" |
| 19 |
#include "atlas/mesh/Nodes.h" |
| 20 |
#include "atlas/parallel/mpi/mpi.h" |
| 21 |
#include "atlas/parallel/omp/fill.h" |
| 22 |
#include "atlas/runtime/Exception.h" |
| 23 |
#include "atlas/runtime/Log.h" |
| 24 |
#include "atlas/util/CoordinateEnums.h" |
| 25 |
#include "atlas/util/SphericalPolygon.h" |
| 26 |
|
| 27 |
namespace atlas { |
| 28 |
namespace grid { |
| 29 |
namespace detail { |
| 30 |
namespace partitioner { |
| 31 |
|
| 32 |
namespace { |
| 33 |
PartitionerBuilder<MatchingMeshPartitionerSphericalPolygon> __builder("spherical-polygon"); |
| 34 |
} |
| 35 |
|
| 36 |
void MatchingMeshPartitionerSphericalPolygon::partition(const Grid& grid, int partitioning[]) const { |
| 37 |
const auto& comm = mpi::comm(prePartitionedMesh_.mpi_comm()); |
| 38 |
const int mpi_rank = int(comm.rank()); |
| 39 |
const int mpi_size = int(comm.size()); |
| 40 |
|
| 41 |
ATLAS_TRACE("MatchingMeshPartitionerSphericalPolygon::partition"); |
| 42 |
|
| 43 |
ATLAS_ASSERT(grid.domain().global()); |
| 44 |
|
| 45 |
if (mpi_size == 1) { |
| 46 |
Log::debug() << "MatchingMeshPartionerSphericalPolygon [mpi_size=0] --> trivial solution" << std::endl; |
| 47 |
omp::fill(partitioning, partitioning + grid.size(), 0); |
| 48 |
return; |
| 49 |
} |
| 50 |
|
| 51 |
// FIXME: THIS IS A HACK! the coordinates include North/South Pole (first/last |
| 52 |
// partitions only) |
| 53 |
bool includesNorthPole = (mpi_rank == 0); |
| 54 |
bool includesSouthPole = (mpi_rank == mpi_size - 1); |
| 55 |
|
| 56 |
if (prePartitionedMesh_.projection()) { |
| 57 |
ATLAS_NOTIMPLEMENTED; |
| 58 |
} |
| 59 |
const util::SphericalPolygon poly{prePartitionedMesh_.polygon(0)}; |
| 60 |
const double maxlat = poly.coordinatesMax()[LAT]; |
| 61 |
const double minlat = poly.coordinatesMin()[LAT]; |
| 62 |
auto at_the_pole = [&](const PointLonLat& P) { |
| 63 |
return (includesNorthPole && P[LAT] >= maxlat) || (includesSouthPole && P[LAT] < minlat); |
| 64 |
}; |
| 65 |
|
| 66 |
{ |
| 67 |
eckit::ProgressTimer timer("Partitioning", grid.size(), "point", double(10), atlas::Log::trace()); |
| 68 |
size_t i = 0; |
| 69 |
|
| 70 |
for (const PointLonLat& P : grid.lonlat()) { |
| 71 |
++timer; |
| 72 |
partitioning[i++] = at_the_pole(P) || poly.contains(P) ? mpi_rank : -1; |
| 73 |
} |
| 74 |
} |
| 75 |
|
| 76 |
// Synchronize partitioning, do a sanity check |
| 77 |
comm.allReduceInPlace(partitioning, grid.size(), eckit::mpi::Operation::MAX); |
| 78 |
const int min = *std::min_element(partitioning, partitioning + grid.size()); |
| 79 |
if (min < 0) { |
| 80 |
throw_Exception( |
| 81 |
"Could not find partition for target node (source " |
| 82 |
"mesh does not contain all target grid points)", |
| 83 |
Here()); |
| 84 |
} |
| 85 |
} |
| 86 |
|
| 87 |
} // namespace partitioner |
| 88 |
} // namespace detail |
| 89 |
} // namespace grid |
| 90 |
} // namespace atlas |