| 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 <map> |
| 12 |
#include <mutex> |
| 13 |
#include <ostream> |
| 14 |
#include <string> |
| 15 |
#include <vector> |
| 16 |
|
| 17 |
#include "TransFactory.h" |
| 18 |
|
| 19 |
#include "atlas/functionspace.h" |
| 20 |
#include "atlas/grid/Grid.h" |
| 21 |
#include "atlas/library/config.h" |
| 22 |
#include "atlas/runtime/Exception.h" |
| 23 |
#include "atlas/runtime/Log.h" |
| 24 |
#include "atlas/trans/Cache.h" |
| 25 |
#include "atlas/trans/Trans.h" |
| 26 |
|
| 27 |
#include "atlas/trans/local/TransLocal.h" |
| 28 |
#if ATLAS_HAVE_TRANS |
| 29 |
#include "atlas/trans/ifs/TransIFS.h" |
| 30 |
#include "atlas/trans/ifs/TransIFSNodeColumns.h" |
| 31 |
#include "atlas/trans/ifs/TransIFSStructuredColumns.h" |
| 32 |
#endif |
| 33 |
|
| 34 |
namespace atlas { |
| 35 |
namespace trans { |
| 36 |
|
| 37 |
//---------------------------------------------------------------------------------------------------------------------- |
| 38 |
|
| 39 |
void force_link() { |
| 40 |
static struct Link { |
| 41 |
Link() { |
| 42 |
TransBuilderGrid<TransLocal>(); |
| 43 |
#if ATLAS_HAVE_TRANS |
| 44 |
TransBuilderGrid<TransIFS>(); |
| 45 |
TransBuilderFunctionSpace<TransIFSStructuredColumns>(); |
| 46 |
TransBuilderFunctionSpace<TransIFSNodeColumns>(); |
| 47 |
#endif |
| 48 |
} |
| 49 |
} link; |
| 50 |
} |
| 51 |
|
| 52 |
//---------------------------------------------------------------------------------------------------------------------- |
| 53 |
|
| 54 |
namespace { |
| 55 |
struct default_backend { |
| 56 |
#if ATLAS_HAVE_TRANS |
| 57 |
std::string value = "ectrans"; |
| 58 |
#else |
| 59 |
std::string value = "local"; |
| 60 |
#endif |
| 61 |
static default_backend instance() { |
| 62 |
static default_backend x; |
| 63 |
return x; |
| 64 |
} |
| 65 |
|
| 66 |
private: |
| 67 |
default_backend() = default; |
| 68 |
}; |
| 69 |
} // namespace |
| 70 |
|
| 71 |
class TransBackend { |
| 72 |
using lock_guard = std::lock_guard<std::mutex>; |
| 73 |
|
| 74 |
protected: |
| 75 |
TransBackend() { default_options_ = util::Config("type", default_backend::instance().value); } |
| 76 |
|
| 77 |
private: |
| 78 |
mutable std::mutex mutex_; |
| 79 |
std::map<std::string, int> backends_; |
| 80 |
|
| 81 |
util::Config default_options_; |
| 82 |
|
| 83 |
public: |
| 84 |
std::vector<std::string> keys() const { |
| 85 |
lock_guard lock(mutex_); |
| 86 |
std::vector<std::string> _keys; |
| 87 |
_keys.reserve(backends_.size()); |
| 88 |
for (const auto& key_value : backends_) { |
| 89 |
_keys.emplace_back(key_value.first); |
| 90 |
} |
| 91 |
return _keys; |
| 92 |
} |
| 93 |
void list(std::ostream& out) const { |
| 94 |
lock_guard lock(mutex_); |
| 95 |
const char* sep = ""; |
| 96 |
for (const auto& map_pair : backends_) { |
| 97 |
out << sep << map_pair.first; |
| 98 |
sep = ", "; |
| 99 |
} |
| 100 |
} |
| 101 |
bool has(const std::string& backend) const { |
| 102 |
lock_guard lock(mutex_); |
| 103 |
return (backends_.find(backend) != backends_.end()); |
| 104 |
} |
| 105 |
void add(const std::string& backend) { |
| 106 |
lock_guard lock(mutex_); |
| 107 |
if (backends_.find(backend) == backends_.end()) { |
| 108 |
backends_[backend] = 1; |
| 109 |
} |
| 110 |
else { |
| 111 |
++backends_[backend]; |
| 112 |
} |
| 113 |
} |
| 114 |
void remove(const std::string& backend) { |
| 115 |
lock_guard lock(mutex_); |
| 116 |
--backends_[backend]; |
| 117 |
if (backends_[backend] == 0) { |
| 118 |
backends_.erase(backend); |
| 119 |
} |
| 120 |
} |
| 121 |
|
| 122 |
void backend(const std::string& backend) { |
| 123 |
lock_guard lock(mutex_); |
| 124 |
default_options_.set("type", backend); |
| 125 |
} |
| 126 |
std::string backend() { return default_options_.getString("type"); } |
| 127 |
void config(const eckit::Configuration& config) { |
| 128 |
std::string type = default_options_.getString("type"); |
| 129 |
default_options_ = config; |
| 130 |
if (not config.has("type")) { |
| 131 |
default_options_.set("type", type); |
| 132 |
} |
| 133 |
} |
| 134 |
const eckit::Configuration& config() { return default_options_; } |
| 135 |
|
| 136 |
|
| 137 |
public: |
| 138 |
static TransBackend& instance() { |
| 139 |
static TransBackend env; |
| 140 |
return env; |
| 141 |
} |
| 142 |
}; |
| 143 |
|
| 144 |
|
| 145 |
TransFactory::TransFactory(const std::string& name, const std::string& backend): |
| 146 |
Factory(name), name_(name), backend_(backend) { |
| 147 |
TransBackend::instance().add(backend); |
| 148 |
} |
| 149 |
|
| 150 |
TransFactory::~TransFactory() { |
| 151 |
TransBackend::instance().remove(backend_); |
| 152 |
} |
| 153 |
|
| 154 |
void TransFactory::list(std::ostream& out) { |
| 155 |
TransBackend::instance().list(out); |
| 156 |
} |
| 157 |
|
| 158 |
bool TransFactory::has(const std::string& backend) { |
| 159 |
return TransBackend::instance().has(backend); |
| 160 |
} |
| 161 |
|
| 162 |
void TransFactory::backend(const std::string& backend) { |
| 163 |
TransBackend::instance().backend(backend); |
| 164 |
} |
| 165 |
|
| 166 |
std::string TransFactory::backend() { |
| 167 |
return TransBackend::instance().backend(); |
| 168 |
} |
| 169 |
|
| 170 |
const eckit::Configuration& TransFactory::config() { |
| 171 |
return TransBackend::instance().config(); |
| 172 |
} |
| 173 |
|
| 174 |
void TransFactory::config(const eckit::Configuration& config) { |
| 175 |
TransBackend::instance().config(config); |
| 176 |
} |
| 177 |
|
| 178 |
const TransImpl* TransFactory::build(const FunctionSpace& gp, const FunctionSpace& sp, |
| 179 |
const eckit::Configuration& config) { |
| 180 |
return build(Cache(), gp, sp, config); |
| 181 |
} |
| 182 |
|
| 183 |
const TransImpl* TransFactory::build(const Cache& cache, const FunctionSpace& gp, const FunctionSpace& sp, |
| 184 |
const eckit::Configuration& config) { |
| 185 |
force_link(); |
| 186 |
|
| 187 |
if (cache.trans()) { |
| 188 |
Log::debug() << "Creating Trans from cache, ignoring any other arguments" << std::endl; |
| 189 |
return cache.trans(); |
| 190 |
} |
| 191 |
|
| 192 |
util::Config options = TransBackend::instance().config(); |
| 193 |
options.set(eckit::LocalConfiguration(config)); |
| 194 |
|
| 195 |
std::string backend = options.getString("type"); |
| 196 |
|
| 197 |
Log::debug() << "Looking for TransFactory [" << backend << "]" << std::endl; |
| 198 |
if (!TransBackend::instance().has(backend)) { |
| 199 |
Log::error() << "No TransFactory for [" << backend << "]" << std::endl; |
| 200 |
Log::error() << "TransFactories are :" << std::endl; |
| 201 |
TransBackend::instance().list(Log::error()); |
| 202 |
Log::error() << std::endl; |
| 203 |
throw_Exception(std::string("No TransFactory called ") + backend); |
| 204 |
} |
| 205 |
|
| 206 |
std::string suffix("(" + gp.type() + "," + sp.type() + ")"); |
| 207 |
std::string builder = backend + suffix; |
| 208 |
|
| 209 |
Log::debug() << "Looking for TransFactory [" << builder << "]" << std::endl; |
| 210 |
auto factory = get(builder); |
| 211 |
return factory->make(cache, gp, sp, options); |
| 212 |
} |
| 213 |
|
| 214 |
const TransImpl* TransFactory::build(const Grid& grid, int truncation, const eckit::Configuration& config) { |
| 215 |
return build(Cache(), grid, truncation, config); |
| 216 |
} |
| 217 |
|
| 218 |
const TransImpl* TransFactory::build(const Grid& grid, const Domain& domain, int truncation, |
| 219 |
const eckit::Configuration& config) { |
| 220 |
return build(Cache(), grid, domain, truncation, config); |
| 221 |
} |
| 222 |
|
| 223 |
const TransImpl* TransFactory::build(const Cache& cache, const Grid& grid, int truncation, |
| 224 |
const eckit::Configuration& config) { |
| 225 |
return build(cache, grid, grid.domain(), truncation, config); |
| 226 |
} |
| 227 |
|
| 228 |
const TransImpl* TransFactory::build(const Cache& cache, const Grid& grid, const Domain& domain, int truncation, |
| 229 |
const eckit::Configuration& config) { |
| 230 |
force_link(); |
| 231 |
|
| 232 |
if (cache.trans()) { |
| 233 |
Log::debug() << "Creating Trans from cache, ignoring any other arguments" << std::endl; |
| 234 |
return cache.trans(); |
| 235 |
} |
| 236 |
util::Config options = TransBackend::instance().config(); |
| 237 |
options.set(eckit::LocalConfiguration(config)); |
| 238 |
|
| 239 |
std::string backend = options.getString("type"); |
| 240 |
|
| 241 |
Log::debug() << "Looking for TransFactory [" << backend << "]" << std::endl; |
| 242 |
if (!TransBackend::instance().has(backend)) { |
| 243 |
Log::error() << "No TransFactory for [" << backend << "]" << std::endl; |
| 244 |
Log::error() << "TransFactories are :" << std::endl; |
| 245 |
TransBackend::instance().list(Log::error()); |
| 246 |
Log::error() << std::endl; |
| 247 |
throw_Exception(std::string("No TransFactory called ") + backend); |
| 248 |
} |
| 249 |
std::string builder = backend; |
| 250 |
auto factory = get(builder); |
| 251 |
return factory->make(cache, grid, domain, truncation, options); |
| 252 |
} |
| 253 |
|
| 254 |
//---------------------------------------------------------------------------------------------------------------------- |
| 255 |
|
| 256 |
} // namespace trans |
| 257 |
} // namespace atlas |