| 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/Partitioner.h" |
| 12 |
#include "atlas/functionspace/FunctionSpace.h" |
| 13 |
#include "atlas/grid/Distribution.h" |
| 14 |
#include "atlas/grid/Grid.h" |
| 15 |
#include "atlas/grid/detail/distribution/DistributionImpl.h" |
| 16 |
#include "atlas/grid/detail/partitioner/Partitioner.h" |
| 17 |
#include "atlas/mesh/Mesh.h" |
| 18 |
#include "atlas/option.h" |
| 19 |
#include "atlas/parallel/mpi/mpi.h" |
| 20 |
#include "atlas/runtime/Exception.h" |
| 21 |
#include "atlas/runtime/Trace.h" |
| 22 |
#include "atlas/util/Config.h" |
| 23 |
|
| 24 |
namespace atlas { |
| 25 |
namespace grid { |
| 26 |
|
| 27 |
using Factory = detail::partitioner::PartitionerFactory; |
| 28 |
|
| 29 |
bool Partitioner::exists(const std::string& type) { |
| 30 |
return Factory::has(type); |
| 31 |
} |
| 32 |
|
| 33 |
Partitioner::Partitioner(const std::string& type): Handle(Factory::build(type)) {} |
| 34 |
|
| 35 |
Partitioner::Partitioner(const std::string& type, const idx_t nb_partitions): |
| 36 |
Handle(Factory::build(type, nb_partitions)) {} |
| 37 |
|
| 38 |
namespace { |
| 39 |
detail::partitioner::Partitioner* partitioner_from_config(const std::string& type, const Partitioner::Config& config) { |
| 40 |
if (detail::partitioner::Partitioner::extract_nb_partitions(config) == 1) { |
| 41 |
return Factory::build("serial",config); |
| 42 |
} |
| 43 |
return Factory::build(type, config); |
| 44 |
} |
| 45 |
detail::partitioner::Partitioner* partitioner_from_config(const Partitioner::Config& config) { |
| 46 |
std::string type; |
| 47 |
if (not config.get("type", type)) { |
| 48 |
throw_Exception("'type' missing in configuration for Partitioner", Here()); |
| 49 |
} |
| 50 |
return partitioner_from_config(type, config); |
| 51 |
} |
| 52 |
} // namespace |
| 53 |
|
| 54 |
Partitioner::Partitioner(const std::string& type, const Config& config): |
| 55 |
Handle(partitioner_from_config(type, config)) {} |
| 56 |
|
| 57 |
Partitioner::Partitioner(const Config& config): Handle(partitioner_from_config(config)) {} |
| 58 |
|
| 59 |
void Partitioner::partition(const Grid& grid, int part[]) const { |
| 60 |
ATLAS_TRACE("Partitioner::partition [type=" + get()->type() + "]"); |
| 61 |
get()->partition(grid, part); |
| 62 |
} |
| 63 |
|
| 64 |
Distribution Partitioner::partition(const Grid& grid) const { |
| 65 |
return get()->partition(grid); |
| 66 |
} |
| 67 |
|
| 68 |
idx_t Partitioner::nb_partitions() const { |
| 69 |
return get()->nb_partitions(); |
| 70 |
} |
| 71 |
|
| 72 |
std::string Partitioner::type() const { |
| 73 |
return get()->type(); |
| 74 |
} |
| 75 |
|
| 76 |
std::string Partitioner::mpi_comm() const { |
| 77 |
return get()->mpi_comm(); |
| 78 |
} |
| 79 |
|
| 80 |
MatchingPartitioner::MatchingPartitioner(): Partitioner() {} |
| 81 |
|
| 82 |
grid::detail::partitioner::Partitioner* matching_mesh_partititioner(const Mesh& mesh, |
| 83 |
const Partitioner::Config& config) { |
| 84 |
std::string type("lonlat-polygon"); |
| 85 |
config.get("type", type); |
| 86 |
return grid::detail::partitioner::MatchingPartitionerFactory::build(type, mesh, config); |
| 87 |
} |
| 88 |
|
| 89 |
MatchingPartitioner::MatchingPartitioner(const Mesh& mesh): MatchingPartitioner(mesh, util::NoConfig()) {} |
| 90 |
|
| 91 |
|
| 92 |
MatchingPartitioner::MatchingPartitioner(const Mesh& mesh, const Config& config): |
| 93 |
Partitioner(matching_mesh_partititioner(mesh, config)) {} |
| 94 |
|
| 95 |
|
| 96 |
grid::detail::partitioner::Partitioner* matching_functionspace_partititioner(const FunctionSpace& functionspace, |
| 97 |
const Partitioner::Config& config) { |
| 98 |
std::string type("lonlat-polygon"); |
| 99 |
config.get("type", type); |
| 100 |
return grid::detail::partitioner::MatchingPartitionerFactory::build(type, functionspace, config); |
| 101 |
} |
| 102 |
|
| 103 |
MatchingPartitioner::MatchingPartitioner(const FunctionSpace& functionspace): |
| 104 |
MatchingPartitioner(functionspace, util::NoConfig()) {} |
| 105 |
|
| 106 |
|
| 107 |
MatchingPartitioner::MatchingPartitioner(const FunctionSpace& functionspace, const Config& config): |
| 108 |
Partitioner(matching_functionspace_partititioner(functionspace, config)) {} |
| 109 |
|
| 110 |
|
| 111 |
extern "C" { |
| 112 |
|
| 113 |
detail::partitioner::Partitioner* atlas__grid__Partitioner__new(const Partitioner::Config* config) { |
| 114 |
detail::partitioner::Partitioner* p; |
| 115 |
{ |
| 116 |
Partitioner partitioner(*config); |
| 117 |
p = const_cast<detail::partitioner::Partitioner*>(partitioner.get()); |
| 118 |
p->attach(); |
| 119 |
} |
| 120 |
p->detach(); |
| 121 |
return p; |
| 122 |
} |
| 123 |
|
| 124 |
detail::partitioner::Partitioner* atlas__grid__Partitioner__new_type(const char* type) { |
| 125 |
detail::partitioner::Partitioner* p; |
| 126 |
{ |
| 127 |
Partitioner partitioner{option::type(type)}; |
| 128 |
p = const_cast<detail::partitioner::Partitioner*>(partitioner.get()); |
| 129 |
p->attach(); |
| 130 |
} |
| 131 |
p->detach(); |
| 132 |
return p; |
| 133 |
} |
| 134 |
|
| 135 |
|
| 136 |
detail::partitioner::Partitioner* atlas__grid__MatchingMeshPartitioner__new(const Mesh::Implementation* mesh, |
| 137 |
const Partitioner::Config* config) { |
| 138 |
detail::partitioner::Partitioner* p; |
| 139 |
{ |
| 140 |
MatchingPartitioner partitioner(Mesh(mesh), *config); |
| 141 |
p = const_cast<detail::partitioner::Partitioner*>(partitioner.get()); |
| 142 |
p->attach(); |
| 143 |
} |
| 144 |
p->detach(); |
| 145 |
return p; |
| 146 |
} |
| 147 |
|
| 148 |
detail::partitioner::Partitioner* atlas__grid__MatchingFunctionSpacePartitioner__new( |
| 149 |
const FunctionSpace::Implementation* functionspace, const Partitioner::Config* config) { |
| 150 |
detail::partitioner::Partitioner* p; |
| 151 |
{ |
| 152 |
MatchingPartitioner partitioner(FunctionSpace(functionspace), *config); |
| 153 |
p = const_cast<detail::partitioner::Partitioner*>(partitioner.get()); |
| 154 |
p->attach(); |
| 155 |
} |
| 156 |
p->detach(); |
| 157 |
return p; |
| 158 |
} |
| 159 |
|
| 160 |
Distribution::Implementation* atlas__grid__Partitioner__partition(const Partitioner::Implementation* This, |
| 161 |
const Grid::Implementation* grid) { |
| 162 |
Distribution::Implementation* d; |
| 163 |
{ |
| 164 |
Distribution distribution = This->partition(Grid(grid)); |
| 165 |
d = const_cast<Distribution::Implementation*>(distribution.get()); |
| 166 |
d->attach(); |
| 167 |
} |
| 168 |
d->detach(); |
| 169 |
return d; |
| 170 |
} |
| 171 |
|
| 172 |
void atlas__grid__Partitioner__delete(detail::partitioner::Partitioner* This) { |
| 173 |
delete This; |
| 174 |
} |
| 175 |
|
| 176 |
} // extern "C" |
| 177 |
|
| 178 |
} // namespace grid |
| 179 |
} // namespace atlas |