atlas
Cache.h
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 #include "eckit/linalg/SparseMatrix.h"
21 
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 
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 Interpolation& );
53  operator bool() const { return not cache_.empty(); }
54  virtual ~Cache();
55  size_t footprint() const {
56  size_t footprint{0};
57  for ( auto& entry : cache_ ) {
58  footprint += entry.second->footprint();
59  }
60  return footprint;
61  }
62  void add( const Cache& );
63 
64 protected:
65  Cache( std::shared_ptr<InterpolationCacheEntry> cache );
66 
67 public:
68  const InterpolationCacheEntry* get( const std::string& type ) const {
69  auto it = cache_.find( type );
70  if ( it != cache_.end() ) {
71  return it->second.get();
72  }
73  return nullptr;
74  }
75 
76 private:
77  std::map<std::string, std::shared_ptr<InterpolationCacheEntry>> cache_;
78 };
79 
80 //-----------------------------------------------------------------------------
81 
83 public:
84  using Matrix = eckit::linalg::SparseMatrix;
85  ~MatrixCacheEntry() override;
86  MatrixCacheEntry( const Matrix* matrix ) : matrix_{matrix} { ATLAS_ASSERT( matrix_ != nullptr ); }
87  const Matrix& matrix() const { return *matrix_; }
88  size_t footprint() const override { return matrix_->footprint(); }
89  operator bool() const { return not matrix_->empty(); }
90  static std::string static_type() { return "Matrix"; }
91  std::string type() const override { return static_type(); }
92 
93 private:
94  const Matrix* matrix_;
95 };
96 
97 //-----------------------------------------------------------------------------
98 
99 class MatrixCache final : public Cache {
100 public:
101  using Matrix = MatrixCacheEntry::Matrix;
102 
103 public:
104  MatrixCache() = default;
105  MatrixCache( const Cache& c );
106  MatrixCache( Matrix&& m );
107  MatrixCache( std::shared_ptr<const Matrix> m );
108  MatrixCache( const Matrix* m );
109  MatrixCache( const Interpolation& );
110  operator bool() const;
111  const Matrix& matrix() const;
112  size_t footprint() const;
113 
114 private:
115  MatrixCache( std::shared_ptr<InterpolationCacheEntry> entry );
116  const MatrixCacheEntry* matrix_{nullptr};
117 };
118 
119 //----------------------------------------------------------------------------------------------------------------------
120 
122 public:
124  IndexKDTreeCacheEntry( const IndexKDTree& tree ) : tree_{tree} { ATLAS_ASSERT( tree_ ); }
125  virtual ~IndexKDTreeCacheEntry() override;
126  const IndexKDTree& tree() const;
127  size_t footprint() const override { return tree().footprint(); }
128  operator bool() const { return bool( tree_ ); }
129  static std::string static_type() { return "IndexKDTree"; }
130  std::string type() const override { return static_type(); }
131 
132 private:
133  const IndexKDTree tree_;
134 };
135 
136 class IndexKDTreeCache final : public Cache {
137 public:
139 
140 public:
141  IndexKDTreeCache() = default;
142  IndexKDTreeCache( const Cache& c );
143  IndexKDTreeCache( const IndexKDTree& );
145  operator bool() const;
146  const IndexKDTree& tree() const;
147  size_t footprint() const;
148 
149 private:
150  const IndexKDTreeCacheEntry* tree_{nullptr};
151 };
152 
153 } // namespace interpolation
154 } // namespace atlas
Definition: Cache.h:99
Definition: Interpolation.h:35
k-dimensional tree constructable both with 2D (lon,lat) points as with 3D (x,y,z) points ...
Definition: KDTree.h:49
Contains all atlas classes and methods.
Definition: atlas-grids.cc:33
Definition: Cache.h:48