| 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 |
// file deepcode ignore CppMemoryLeak: static pointers for global registry are OK and will be cleaned up at end |
| 12 |
|
| 13 |
#include "eckit/thread/AutoLock.h" |
| 14 |
#include "eckit/thread/Mutex.h" |
| 15 |
|
| 16 |
#include "atlas/grid/Grid.h" |
| 17 |
#include "atlas/library/defines.h" |
| 18 |
#include "atlas/runtime/Exception.h" |
| 19 |
#include "atlas/runtime/Log.h" |
| 20 |
#include "atlas/trans/LegendreCacheCreator.h" |
| 21 |
|
| 22 |
// For factory registration only: |
| 23 |
#if ATLAS_HAVE_TRANS |
| 24 |
#include "atlas/trans/ifs/LegendreCacheCreatorIFS.h" |
| 25 |
#endif |
| 26 |
#include "atlas/trans/local/LegendreCacheCreatorLocal.h" |
| 27 |
|
| 28 |
namespace atlas { |
| 29 |
namespace trans { |
| 30 |
|
| 31 |
LegendreCacheCreatorImpl::~LegendreCacheCreatorImpl() = default; |
| 32 |
|
| 33 |
namespace { |
| 34 |
|
| 35 |
static eckit::Mutex* local_mutex = nullptr; |
| 36 |
static std::map<std::string, LegendreCacheCreatorFactory*>* m = nullptr; |
| 37 |
static pthread_once_t once = PTHREAD_ONCE_INIT; |
| 38 |
|
| 39 |
static void init() { |
| 40 |
local_mutex = new eckit::Mutex(); |
| 41 |
m = new std::map<std::string, LegendreCacheCreatorFactory*>(); |
| 42 |
} |
| 43 |
|
| 44 |
template <typename T> |
| 45 |
void load_builder() { |
| 46 |
LegendreCacheCreatorBuilder<T>("tmp"); |
| 47 |
} |
| 48 |
|
| 49 |
struct force_link { |
| 50 |
force_link() { |
| 51 |
#if ATLAS_HAVE_TRANS |
| 52 |
load_builder<LegendreCacheCreatorIFS>(); |
| 53 |
#endif |
| 54 |
load_builder<LegendreCacheCreatorLocal>(); |
| 55 |
} |
| 56 |
}; |
| 57 |
|
| 58 |
static LegendreCacheCreatorFactory& factory(const std::string& name) { |
| 59 |
ATLAS_ASSERT(m); |
| 60 |
std::map<std::string, LegendreCacheCreatorFactory*>::const_iterator j = m->find(name); |
| 61 |
if (j == m->end()) { |
| 62 |
Log::error() << "No LegendreCacheCreatorFactory for [" << name << "]" << std::endl; |
| 63 |
Log::error() << "TransFactories are:" << std::endl; |
| 64 |
for (j = m->begin(); j != m->end(); ++j) { |
| 65 |
Log::error() << " " << (*j).first << std::endl; |
| 66 |
} |
| 67 |
throw_Exception(std::string("No LegendreCacheCreatorFactory called ") + name); |
| 68 |
} |
| 69 |
return *j->second; |
| 70 |
} |
| 71 |
|
| 72 |
} // namespace |
| 73 |
|
| 74 |
LegendreCacheCreatorFactory::LegendreCacheCreatorFactory(const std::string& name): name_(name) { |
| 75 |
pthread_once(&once, init); |
| 76 |
|
| 77 |
eckit::AutoLock<eckit::Mutex> lock(local_mutex); |
| 78 |
|
| 79 |
ATLAS_ASSERT(m); |
| 80 |
ATLAS_ASSERT(m->find(name) == m->end()); |
| 81 |
(*m)[name] = this; |
| 82 |
} |
| 83 |
|
| 84 |
LegendreCacheCreatorFactory::~LegendreCacheCreatorFactory() { |
| 85 |
eckit::AutoLock<eckit::Mutex> lock(local_mutex); |
| 86 |
ATLAS_ASSERT(m); |
| 87 |
m->erase(name_); |
| 88 |
} |
| 89 |
|
| 90 |
bool LegendreCacheCreatorFactory::has(const std::string& name) { |
| 91 |
pthread_once(&once, init); |
| 92 |
|
| 93 |
eckit::AutoLock<eckit::Mutex> lock(local_mutex); |
| 94 |
|
| 95 |
static force_link static_linking; |
| 96 |
|
| 97 |
ATLAS_ASSERT(m); |
| 98 |
return (m->find(name) != m->end()); |
| 99 |
} |
| 100 |
|
| 101 |
void LegendreCacheCreatorFactory::list(std::ostream& out) { |
| 102 |
pthread_once(&once, init); |
| 103 |
|
| 104 |
eckit::AutoLock<eckit::Mutex> lock(local_mutex); |
| 105 |
|
| 106 |
static force_link static_linking; |
| 107 |
|
| 108 |
ATLAS_ASSERT(m); |
| 109 |
const char* sep = ""; |
| 110 |
for (std::map<std::string, LegendreCacheCreatorFactory*>::const_iterator j = m->begin(); j != m->end(); ++j) { |
| 111 |
out << sep << (*j).first; |
| 112 |
sep = ", "; |
| 113 |
} |
| 114 |
} |
| 115 |
|
| 116 |
LegendreCacheCreator::Implementation* LegendreCacheCreatorFactory::build(const Grid& grid, int truncation, |
| 117 |
const eckit::Configuration& config) { |
| 118 |
pthread_once(&once, init); |
| 119 |
|
| 120 |
eckit::AutoLock<eckit::Mutex> lock(local_mutex); |
| 121 |
|
| 122 |
static force_link static_linking; |
| 123 |
|
| 124 |
util::Config options = Trans::config(); |
| 125 |
options.set(eckit::LocalConfiguration(config)); |
| 126 |
|
| 127 |
std::string name = options.getString("type"); |
| 128 |
|
| 129 |
Log::debug() << "Looking for LegendreCacheCreatorFactory [" << name << "]" << std::endl; |
| 130 |
|
| 131 |
return factory(name).make(grid, truncation, options); |
| 132 |
} |
| 133 |
|
| 134 |
|
| 135 |
LegendreCacheCreator::LegendreCacheCreator(const Grid& grid, int truncation, const eckit::Configuration& config): |
| 136 |
Handle(LegendreCacheCreatorFactory::build(grid, truncation, config)) {} |
| 137 |
|
| 138 |
|
| 139 |
bool LegendreCacheCreator::supported() const { |
| 140 |
return get()->supported(); |
| 141 |
} |
| 142 |
|
| 143 |
std::string LegendreCacheCreator::uid() const { |
| 144 |
return get()->uid(); |
| 145 |
} |
| 146 |
|
| 147 |
void LegendreCacheCreator::create(const std::string& path) const { |
| 148 |
get()->create(path); |
| 149 |
} |
| 150 |
|
| 151 |
Cache LegendreCacheCreator::create() const { |
| 152 |
return get()->create(); |
| 153 |
} |
| 154 |
|
| 155 |
size_t LegendreCacheCreator::estimate() const { |
| 156 |
return get()->estimate(); |
| 157 |
} |
| 158 |
|
| 159 |
} // namespace trans |
| 160 |
} // namespace atlas |