| 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 |
#pragma once |
| 12 |
|
| 13 |
#include <map> |
| 14 |
#include <memory> |
| 15 |
#include <string> |
| 16 |
|
| 17 |
#include "eckit/filesystem/PathName.h" |
| 18 |
#include "eckit/io/Buffer.h" |
| 19 |
|
| 20 |
|
| 21 |
#include "atlas/linalg/sparse.h" |
| 22 |
#include "atlas/runtime/Exception.h" |
| 23 |
#include "atlas/util/KDTree.h" |
| 24 |
|
| 25 |
//----------------------------------------------------------------------------- |
| 26 |
// Forward declarations |
| 27 |
|
| 28 |
namespace atlas { |
| 29 |
class Interpolation; |
| 30 |
} // namespace atlas |
| 31 |
|
| 32 |
//----------------------------------------------------------------------------- |
| 33 |
|
| 34 |
namespace atlas { |
| 35 |
namespace interpolation { |
| 36 |
|
| 37 |
//----------------------------------------------------------------------------- |
| 38 |
|
| 39 |
class InterpolationCacheEntry { |
| 40 |
public: |
| 41 |
virtual ~InterpolationCacheEntry(); |
| 42 |
virtual size_t footprint() const = 0; |
| 43 |
virtual std::string type() const = 0; |
| 44 |
}; |
| 45 |
|
| 46 |
//----------------------------------------------------------------------------- |
| 47 |
|
| 48 |
class Cache { |
| 49 |
public: |
| 50 |
Cache() = default; |
| 51 |
Cache(const Cache& other); |
| 52 |
Cache(const Cache& other, const std::string& filter); |
| 53 |
Cache(const Interpolation&); |
| 54 |
Cache& operator=(const Cache& other); |
| 55 |
Cache& operator=(Cache&& other); |
| 56 |
|
| 57 |
operator bool() const { return not cache_.empty(); } |
| 58 |
virtual ~Cache(); |
| 59 |
size_t footprint() const { |
| 60 |
size_t footprint{0}; |
| 61 |
for (auto& entry : cache_) { |
| 62 |
footprint += entry.second->footprint(); |
| 63 |
} |
| 64 |
return footprint; |
| 65 |
} |
| 66 |
void add(const Cache&); |
| 67 |
|
| 68 |
protected: |
| 69 |
Cache(std::shared_ptr<InterpolationCacheEntry> cache); |
| 70 |
|
| 71 |
public: |
| 72 |
const InterpolationCacheEntry* get(const std::string& type) const { |
| 73 |
auto it = cache_.find(type); |
| 74 |
if (it != cache_.end()) { |
| 75 |
return it->second.get(); |
| 76 |
} |
| 77 |
return nullptr; |
| 78 |
} |
| 79 |
|
| 80 |
private: |
| 81 |
std::map<std::string, std::shared_ptr<InterpolationCacheEntry>> cache_; |
| 82 |
}; |
| 83 |
|
| 84 |
//----------------------------------------------------------------------------- |
| 85 |
|
| 86 |
class MatrixCacheEntry : public InterpolationCacheEntry { |
| 87 |
public: |
| 88 |
using Matrix = atlas::linalg::SparseMatrixStorage; |
| 89 |
~MatrixCacheEntry() override; |
| 90 |
MatrixCacheEntry(const Matrix* matrix, const std::string& uid = ""): matrix_{matrix}, uid_(uid) { |
| 91 |
ATLAS_ASSERT(matrix_ != nullptr); |
| 92 |
} |
| 93 |
const Matrix& matrix() const { return *matrix_; } |
| 94 |
const std::string& uid() const { return uid_; } |
| 95 |
size_t footprint() const override { return matrix_->footprint(); } |
| 96 |
operator bool() const { return not matrix_->empty(); } |
| 97 |
static std::string static_type() { return "Matrix"; } |
| 98 |
std::string type() const override { return static_type(); } |
| 99 |
|
| 100 |
private: |
| 101 |
const Matrix* matrix_; |
| 102 |
const std::string uid_; |
| 103 |
}; |
| 104 |
|
| 105 |
//----------------------------------------------------------------------------- |
| 106 |
|
| 107 |
class MatrixCache final : public Cache { |
| 108 |
public: |
| 109 |
using Matrix = MatrixCacheEntry::Matrix; |
| 110 |
|
| 111 |
public: |
| 112 |
MatrixCache() = default; |
| 113 |
MatrixCache(const Cache& c); |
| 114 |
MatrixCache(Matrix&& m); |
| 115 |
MatrixCache(std::shared_ptr<const Matrix> m, const std::string& uid = ""); |
| 116 |
MatrixCache(const Matrix* m); |
| 117 |
MatrixCache(const Interpolation&); |
| 118 |
|
| 119 |
operator bool() const; |
| 120 |
const Matrix& matrix() const; |
| 121 |
const std::string& uid() const; |
| 122 |
size_t footprint() const; |
| 123 |
|
| 124 |
private: |
| 125 |
MatrixCache(std::shared_ptr<InterpolationCacheEntry> entry); |
| 126 |
const MatrixCacheEntry* matrix_{nullptr}; |
| 127 |
}; |
| 128 |
|
| 129 |
//---------------------------------------------------------------------------------------------------------------------- |
| 130 |
|
| 131 |
class IndexKDTreeCacheEntry : public InterpolationCacheEntry { |
| 132 |
public: |
| 133 |
using IndexKDTree = util::IndexKDTree; |
| 134 |
IndexKDTreeCacheEntry(const IndexKDTree& tree): tree_{tree} { ATLAS_ASSERT(tree_); } |
| 135 |
virtual ~IndexKDTreeCacheEntry() override; |
| 136 |
const IndexKDTree& tree() const; |
| 137 |
size_t footprint() const override { return tree().footprint(); } |
| 138 |
operator bool() const { return bool(tree_); } |
| 139 |
static std::string static_type() { return "IndexKDTree"; } |
| 140 |
std::string type() const override { return static_type(); } |
| 141 |
|
| 142 |
private: |
| 143 |
const IndexKDTree tree_; |
| 144 |
}; |
| 145 |
|
| 146 |
class IndexKDTreeCache final : public Cache { |
| 147 |
public: |
| 148 |
using IndexKDTree = IndexKDTreeCacheEntry::IndexKDTree; |
| 149 |
|
| 150 |
public: |
| 151 |
IndexKDTreeCache() = default; |
| 152 |
IndexKDTreeCache(const Cache& c); |
| 153 |
IndexKDTreeCache(const IndexKDTreeCache& c) : IndexKDTreeCache(Cache(c)) {} |
| 154 |
IndexKDTreeCache(const IndexKDTree&); |
| 155 |
IndexKDTreeCache(const Interpolation&); |
| 156 |
operator bool() const; |
| 157 |
const IndexKDTree& tree() const; |
| 158 |
size_t footprint() const; |
| 159 |
|
| 160 |
private: |
| 161 |
const IndexKDTreeCacheEntry* tree_{nullptr}; |
| 162 |
}; |
| 163 |
|
| 164 |
} // namespace interpolation |
| 165 |
} // namespace atlas |