| 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/trans/Cache.h" |
| 12 |
|
| 13 |
#include "pluto/pluto.h" |
| 14 |
|
| 15 |
#include "eckit/io/DataHandle.h" |
| 16 |
|
| 17 |
#include "atlas/runtime/Exception.h" |
| 18 |
#include "atlas/runtime/Log.h" |
| 19 |
#include "atlas/runtime/Trace.h" |
| 20 |
#include "atlas/trans/Trans.h" |
| 21 |
|
| 22 |
namespace atlas { |
| 23 |
namespace trans { |
| 24 |
|
| 25 |
TransCacheFileEntry::TransCacheFileEntry(const eckit::PathName& path) { |
| 26 |
ATLAS_TRACE(); |
| 27 |
Log::debug() << "Loading cache from file " << path << std::endl; |
| 28 |
std::unique_ptr<eckit::DataHandle> dh(path.fileHandle()); |
| 29 |
|
| 30 |
size_ = path.size(); |
| 31 |
data_ = pluto::host_resource()->allocate(size_, 256); |
| 32 |
|
| 33 |
dh->openForRead(); |
| 34 |
dh->read(data_, size_); |
| 35 |
dh->close(); |
| 36 |
} |
| 37 |
|
| 38 |
TransCacheFileEntry::~TransCacheFileEntry() { |
| 39 |
pluto::host_resource()->deallocate(data_, size_, 256); |
| 40 |
} |
| 41 |
|
| 42 |
TransCacheMemoryEntry::TransCacheMemoryEntry(const void* data, size_t size): data_(data), size_(size) { |
| 43 |
ATLAS_ASSERT(data_); |
| 44 |
ATLAS_ASSERT(size_); |
| 45 |
} |
| 46 |
|
| 47 |
LegendreFFTCache::LegendreFFTCache(const void* legendre_address, size_t legendre_size, const void* fft_address, |
| 48 |
size_t fft_size): |
| 49 |
Cache(std::make_shared<TransCacheMemoryEntry>(legendre_address, legendre_size), |
| 50 |
std::make_shared<TransCacheMemoryEntry>(fft_address, fft_size)) {} |
| 51 |
|
| 52 |
static std::shared_ptr<TransCacheEntry> read_legendre_cache(const eckit::PathName& legendre_path) { |
| 53 |
ATLAS_TRACE(); |
| 54 |
return std::make_shared<TransCacheFileEntry>(legendre_path); |
| 55 |
} |
| 56 |
static std::shared_ptr<TransCacheEntry> read_fft_cache(const eckit::PathName& fft_path) { |
| 57 |
ATLAS_TRACE(); |
| 58 |
return std::make_shared<TransCacheFileEntry>(fft_path); |
| 59 |
} |
| 60 |
LegendreFFTCache::LegendreFFTCache(const eckit::PathName& legendre_path, const eckit::PathName& fft_path): |
| 61 |
Cache(read_legendre_cache(legendre_path), read_fft_cache(fft_path)) {} |
| 62 |
|
| 63 |
LegendreCache::LegendreCache(const eckit::PathName& path): |
| 64 |
Cache(std::shared_ptr<TransCacheEntry>(new TransCacheFileEntry(path))) {} |
| 65 |
|
| 66 |
LegendreCache::LegendreCache(size_t size): Cache(std::make_shared<TransCacheOwnedMemoryEntry>(size)) {} |
| 67 |
|
| 68 |
LegendreCache::LegendreCache(const void* address, size_t size): |
| 69 |
Cache(std::make_shared<TransCacheMemoryEntry>(address, size)) {} |
| 70 |
|
| 71 |
Cache::Cache(const std::shared_ptr<TransCacheEntry>& legendre): |
| 72 |
trans_(nullptr), legendre_(legendre), fft_(new EmptyCacheEntry()) {} |
| 73 |
|
| 74 |
Cache::Cache(const std::shared_ptr<TransCacheEntry>& legendre, const std::shared_ptr<TransCacheEntry>& fft): |
| 75 |
trans_(nullptr), legendre_(legendre), fft_(fft) {} |
| 76 |
|
| 77 |
Cache::Cache(const TransImpl* trans): trans_(trans), legendre_(new EmptyCacheEntry()), fft_(new EmptyCacheEntry()) {} |
| 78 |
|
| 79 |
Cache::Cache(): trans_(nullptr), legendre_(new EmptyCacheEntry()), fft_(new EmptyCacheEntry()) {} |
| 80 |
|
| 81 |
Cache::operator bool() const { |
| 82 |
return trans_ || bool(legendre()); |
| 83 |
} |
| 84 |
|
| 85 |
Cache::~Cache() = default; |
| 86 |
|
| 87 |
TransCache::TransCache(const Trans& trans): Cache(trans.get()) {} |
| 88 |
|
| 89 |
TransCacheOwnedMemoryEntry::TransCacheOwnedMemoryEntry(size_t size): size_(size) { |
| 90 |
if (size_) { |
| 91 |
data_ = pluto::host_resource()->allocate(size_, 256); |
| 92 |
} |
| 93 |
} |
| 94 |
|
| 95 |
TransCacheOwnedMemoryEntry::~TransCacheOwnedMemoryEntry() { |
| 96 |
if (size_) { |
| 97 |
pluto::host_resource()->deallocate(data_, size_, 256); |
| 98 |
} |
| 99 |
} |
| 100 |
|
| 101 |
} // namespace trans |
| 102 |
} // namespace atlas |