| 1 |
/* |
| 2 |
* (C) Crown Copyright 2021 Met Office |
| 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 |
*/ |
| 7 |
|
| 8 |
#pragma once |
| 9 |
|
| 10 |
#include <vector> |
| 11 |
|
| 12 |
#include "atlas/redistribution/detail/RedistributionImpl.h" |
| 13 |
#include "atlas/redistribution/detail/RedistributionImplFactory.h" |
| 14 |
|
| 15 |
|
| 16 |
namespace atlas { |
| 17 |
|
| 18 |
class Field; |
| 19 |
class FieldSet; |
| 20 |
class FunctionSpace; |
| 21 |
|
| 22 |
namespace functionspace { |
| 23 |
class StructuredColumns; |
| 24 |
} // namespace functionspace |
| 25 |
} // namespace atlas |
| 26 |
|
| 27 |
namespace atlas { |
| 28 |
namespace redistribution { |
| 29 |
namespace detail { |
| 30 |
|
| 31 |
// Forward declarations. |
| 32 |
class RedistributeStructuredColumns; |
| 33 |
class StructuredIndexRange; |
| 34 |
|
| 35 |
// Type aliases. |
| 36 |
class StructuredIndexRange; |
| 37 |
using idxPair = std::pair<idx_t, idx_t>; |
| 38 |
using idxPairVector = std::vector<idxPair>; |
| 39 |
using StructuredIndexRangeVector = std::vector<StructuredIndexRange>; |
| 40 |
|
| 41 |
/// \brief Concrete redistributor class for StructuredColumns to |
| 42 |
/// StructuredColumns. |
| 43 |
/// |
| 44 |
/// \details Class to map two function spaces with the same grid but |
| 45 |
/// different partitioners. |
| 46 |
class RedistributeStructuredColumns : public RedistributionImpl { |
| 47 |
public: |
| 48 |
RedistributeStructuredColumns() = default; |
| 49 |
|
| 50 |
/// \brief Initialises the redistributor. |
| 51 |
void do_setup() override; |
| 52 |
|
| 53 |
static std::string static_type() { return "RedistributeStructuredColumns"; } |
| 54 |
|
| 55 |
std::string type() const override { return static_type(); } |
| 56 |
|
| 57 |
/// \brief Redistributes source field to target field. |
| 58 |
/// |
| 59 |
/// \details Transfers source field to target field via an |
| 60 |
/// MPI_Alltoallv. Function space of source field must match |
| 61 |
/// sourceFunctionSpace supplied to the constructor. Same |
| 62 |
/// applies to target field. |
| 63 |
/// |
| 64 |
/// \param[in] source input field matching sourceFunctionSpace. |
| 65 |
/// \param[out] target output field matching targetFunctionSpace. |
| 66 |
void execute(const Field& source, Field& target) const override; |
| 67 |
|
| 68 |
/// \brief Redistributes source field set to target fields set. |
| 69 |
/// |
| 70 |
/// \details Transfers source field set to target field set via |
| 71 |
/// multiple invocations of execute(sourceField, targetField). |
| 72 |
/// |
| 73 |
/// \param[in] source input field set. |
| 74 |
/// \param[out] target output field set. |
| 75 |
void execute(const FieldSet& source, FieldSet& target) const override; |
| 76 |
|
| 77 |
private: |
| 78 |
// Generic execute call to handle different field types. |
| 79 |
template <typename fieldType> |
| 80 |
void do_execute(const Field& source, Field& target) const; |
| 81 |
|
| 82 |
// FunctionSpaces recast to StructuredColumns. |
| 83 |
functionspace::StructuredColumns source_; |
| 84 |
functionspace::StructuredColumns target_; |
| 85 |
|
| 86 |
// Vectors of index range intersection objects. |
| 87 |
StructuredIndexRangeVector sendIntersections_{}; |
| 88 |
StructuredIndexRangeVector recvIntersections_{}; |
| 89 |
|
| 90 |
// Counts and displacements for MPI communications. |
| 91 |
std::vector<int> sendCounts_{}; |
| 92 |
std::vector<int> sendDisplacements_{}; |
| 93 |
std::vector<int> recvCounts_{}; |
| 94 |
std::vector<int> recvDisplacements_{}; |
| 95 |
|
| 96 |
std::string mpi_comm_; |
| 97 |
}; |
| 98 |
|
| 99 |
/// \brief Helper class for function space intersections. |
| 100 |
class StructuredIndexRange { |
| 101 |
public: |
| 102 |
/// \brief Default Constructor. |
| 103 |
StructuredIndexRange() = default; |
| 104 |
|
| 105 |
/// \brief Constructor. |
| 106 |
StructuredIndexRange(const functionspace::StructuredColumns&); |
| 107 |
|
| 108 |
/// \brief Get index ranges from all PEs. |
| 109 |
StructuredIndexRangeVector getStructuredIndexRanges() const; |
| 110 |
|
| 111 |
/// \brief Count number of elements. |
| 112 |
idx_t getElemCount() const; |
| 113 |
|
| 114 |
/// \brief Intersection operator. |
| 115 |
StructuredIndexRange operator&(const StructuredIndexRange&) const; |
| 116 |
|
| 117 |
/// \brief Iterate over all indices and do something with functor. |
| 118 |
template <typename functorType> |
| 119 |
void forEach(const functorType&) const; |
| 120 |
|
| 121 |
const std::string& mpi_comm() const { return mpi_comm_; } |
| 122 |
|
| 123 |
private: |
| 124 |
// Begin and end of j range. |
| 125 |
idxPair jBeginEnd_{}; |
| 126 |
|
| 127 |
// Begin and end of i range for each j. |
| 128 |
idxPairVector iBeginEnd_{}; |
| 129 |
|
| 130 |
std::string mpi_comm_; |
| 131 |
}; |
| 132 |
|
| 133 |
} // namespace detail |
| 134 |
} // namespace redistribution |
| 135 |
} // namespace atlas |