| 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 |
/// @author Willem Deconinck |
| 12 |
/// @date Sep 2014 |
| 13 |
|
| 14 |
#pragma once |
| 15 |
|
| 16 |
#include <functional> |
| 17 |
#include <string> |
| 18 |
#include <vector> |
| 19 |
#include <algorithm> |
| 20 |
#include <memory> |
| 21 |
|
| 22 |
#include "atlas/util/Object.h" |
| 23 |
|
| 24 |
#include "atlas/array.h" |
| 25 |
#include "atlas/array/ArrayDataStore.h" |
| 26 |
#include "atlas/array/DataType.h" |
| 27 |
#include "atlas/util/Metadata.h" |
| 28 |
#include "atlas/field/Halo.h" |
| 29 |
|
| 30 |
namespace eckit { |
| 31 |
class Parametrisation; |
| 32 |
} |
| 33 |
|
| 34 |
namespace atlas { |
| 35 |
class FunctionSpace; |
| 36 |
} // namespace atlas |
| 37 |
|
| 38 |
namespace atlas { |
| 39 |
namespace field { |
| 40 |
|
| 41 |
//---------------------------------------------------------------------------------------------------------------------- |
| 42 |
|
| 43 |
class FieldObserver; // Definition below |
| 44 |
|
| 45 |
//---------------------------------------------------------------------------------------------------------------------- |
| 46 |
|
| 47 |
class FieldImpl : public util::Object { |
| 48 |
public: // Static methods |
| 49 |
/// @brief Create field from parametrisation |
| 50 |
static FieldImpl* create(const eckit::Parametrisation&); |
| 51 |
|
| 52 |
/// @brief Create field with given name, Datatype and ArrayShape |
| 53 |
static FieldImpl* create(const std::string& name, array::DataType, const array::ArrayShape& = array::ArrayShape()); |
| 54 |
|
| 55 |
/// @brief Create field with given name, Datatype and ArrayShape |
| 56 |
static FieldImpl* create(const std::string& name, array::DataType, array::ArraySpec&&); |
| 57 |
|
| 58 |
/// @brief Create field with given name, Datatype of template and ArrayShape |
| 59 |
template <typename DATATYPE> |
| 60 |
static FieldImpl* create(const std::string& name, const array::ArrayShape& = array::ArrayShape()); |
| 61 |
|
| 62 |
/// @brief Create field with given name, and take ownership of given Array |
| 63 |
static FieldImpl* create(const std::string& name, array::Array*); |
| 64 |
|
| 65 |
/// @brief Create field by wrapping existing data, Datatype of template and |
| 66 |
/// ArraySpec |
| 67 |
template <typename DATATYPE> |
| 68 |
static FieldImpl* wrap(const std::string& name, DATATYPE* data, const array::ArraySpec&); |
| 69 |
|
| 70 |
/// @brief Create field by wrapping existing data, Datatype of template and |
| 71 |
/// ArrayShape |
| 72 |
template <typename DATATYPE> |
| 73 |
static FieldImpl* wrap(const std::string& name, DATATYPE* data, const array::ArrayShape&); |
| 74 |
|
| 75 |
private: // Private constructors to force use of static create functions |
| 76 |
/// Allocate new Array internally |
| 77 |
FieldImpl(const std::string& name, array::DataType, const array::ArrayShape&); |
| 78 |
|
| 79 |
/// Allocate new Array internally |
| 80 |
FieldImpl(const std::string& name, array::DataType, array::ArraySpec&&); |
| 81 |
|
| 82 |
/// Transfer ownership of Array |
| 83 |
FieldImpl(const std::string& name, array::Array*); |
| 84 |
|
| 85 |
public: // Destructor |
| 86 |
virtual ~FieldImpl(); |
| 87 |
|
| 88 |
// -- Conversion |
| 89 |
|
| 90 |
/// @brief Implicit conversion to Array |
| 91 |
operator const array::Array&() const { return *array_; } |
| 92 |
operator array::Array&() { return *array_; } |
| 93 |
|
| 94 |
const array::Array& array() const { return *array_; } |
| 95 |
array::Array& array() { return *array_; } |
| 96 |
|
| 97 |
// -- Accessors |
| 98 |
|
| 99 |
/// @brief Access to raw data |
| 100 |
void* storage() { return array_->storage(); } |
| 101 |
|
| 102 |
/// @brief Internal data type of field |
| 103 |
array::DataType datatype() const { return array_->datatype(); } |
| 104 |
|
| 105 |
/// @brief Name associated to this field |
| 106 |
const std::string& name() const; |
| 107 |
|
| 108 |
/// @brief Rename this field |
| 109 |
void rename(const std::string& name); |
| 110 |
|
| 111 |
/// @brief Access to metadata associated to this field |
| 112 |
const util::Metadata& metadata() const { return metadata_; } |
| 113 |
util::Metadata& metadata() { return metadata_; } |
| 114 |
|
| 115 |
/// @brief Resize field to given shape |
| 116 |
void resize(const array::ArrayShape&); |
| 117 |
|
| 118 |
void insert(idx_t idx1, idx_t size1); |
| 119 |
|
| 120 |
/// @brief Shape of this field in Fortran style (reverse order of C style) |
| 121 |
const std::vector<int>& shapef() const { return array_->shapef(); } |
| 122 |
|
| 123 |
/// @brief Strides of this field in Fortran style (reverse order of C style) |
| 124 |
const std::vector<int>& stridesf() const { return array_->stridesf(); } |
| 125 |
|
| 126 |
/// @brief Strides of this field on the device in Fortran style (reverse order of C style) |
| 127 |
const std::vector<int>& device_stridesf() const { return array_->device_stridesf(); } |
| 128 |
|
| 129 |
/// @brief Shape of this field (reverse order of Fortran style) |
| 130 |
const array::ArrayShape& shape() const { return array_->shape(); } |
| 131 |
|
| 132 |
/// @brief Strides of this field |
| 133 |
const array::ArrayStrides& strides() const { return array_->strides(); } |
| 134 |
|
| 135 |
/// @brief Shape of this field associated to index 'i' |
| 136 |
idx_t shape(idx_t i) const { return array_->shape(i); } |
| 137 |
|
| 138 |
/// @brief Stride of this field associated to index 'i' |
| 139 |
idx_t stride(idx_t i) const { return array_->stride(i); } |
| 140 |
|
| 141 |
/// @brief Number of values stored in this field |
| 142 |
size_t size() const { return array_->size(); } |
| 143 |
|
| 144 |
/// @brief Rank of field |
| 145 |
idx_t rank() const { return array_->rank(); } |
| 146 |
|
| 147 |
/// @brief Number of bytes occupied by the values of this field |
| 148 |
size_t bytes() const { return array_->bytes(); } |
| 149 |
|
| 150 |
/// @brief Output information of field |
| 151 |
friend std::ostream& operator<<(std::ostream& os, const FieldImpl& v); |
| 152 |
|
| 153 |
/// @brief Output information of field plus raw data |
| 154 |
void dump(std::ostream& os) const; |
| 155 |
|
| 156 |
/// Metadata that is more intrinsic to the Field, and queried often |
| 157 |
void set_levels(idx_t n) { metadata().set("levels", n); } |
| 158 |
void set_variables(idx_t n) { metadata().set("variables", n); } |
| 159 |
idx_t levels() const { return metadata().get<idx_t>("levels"); } |
| 160 |
idx_t variables() const { return metadata().get<idx_t>("variables"); } |
| 161 |
|
| 162 |
void set_horizontal_dimension(const std::vector<idx_t>& h_dim) { metadata().set("horizontal_dimension", h_dim); } |
| 163 |
std::vector<idx_t> horizontal_dimension() const { |
| 164 |
std::vector<idx_t> h_dim{0}; |
| 165 |
metadata().get("horizontal_dimension", h_dim); |
| 166 |
return h_dim; |
| 167 |
} |
| 168 |
|
| 169 |
void set_functionspace(const FunctionSpace&); |
| 170 |
const FunctionSpace& functionspace() const; |
| 171 |
|
| 172 |
/// @brief Return the memory footprint of the Field |
| 173 |
size_t footprint() const; |
| 174 |
|
| 175 |
bool dirty() const; |
| 176 |
|
| 177 |
void set_dirty(bool = true) const; |
| 178 |
|
| 179 |
// -- dangerous methods |
| 180 |
template <typename DATATYPE> |
| 181 |
DATATYPE const* host_data() const { |
| 182 |
return array_->host_data<DATATYPE>(); |
| 183 |
} |
| 184 |
template <typename DATATYPE> |
| 185 |
DATATYPE* host_data() { |
| 186 |
return array_->host_data<DATATYPE>(); |
| 187 |
} |
| 188 |
template <typename DATATYPE> |
| 189 |
DATATYPE const* device_data() const { |
| 190 |
return array_->device_data<DATATYPE>(); |
| 191 |
} |
| 192 |
template <typename DATATYPE> |
| 193 |
DATATYPE* device_data() { |
| 194 |
return array_->device_data<DATATYPE>(); |
| 195 |
} |
| 196 |
template <typename DATATYPE> |
| 197 |
DATATYPE const* data() const { |
| 198 |
return array_->host_data<DATATYPE>(); |
| 199 |
} |
| 200 |
template <typename DATATYPE> |
| 201 |
DATATYPE* data() { |
| 202 |
return array_->host_data<DATATYPE>(); |
| 203 |
} |
| 204 |
|
| 205 |
// -- Methods related to host-device synchronisation, requires |
| 206 |
// gridtools_storage |
| 207 |
void updateHost() const { array_->updateHost(); } |
| 208 |
void updateDevice() const { array_->updateDevice(); } |
| 209 |
void syncHostDevice() const { array_->syncHostDevice(); } |
| 210 |
void syncHost() const { array_->syncHost(); } |
| 211 |
void syncDevice() const { array_->syncDevice(); } |
| 212 |
bool deviceAllocated() const { return array_->deviceAllocated(); } |
| 213 |
void allocateDevice() const { array_->allocateDevice(); } |
| 214 |
void deallocateDevice() const { array_->deallocateDevice(); } |
| 215 |
bool hostNeedsUpdate() const { return array_->hostNeedsUpdate(); } |
| 216 |
bool deviceNeedsUpdate() const { return array_->deviceNeedsUpdate(); } |
| 217 |
void setHostNeedsUpdate(bool v) const { return array_->setHostNeedsUpdate(v); } |
| 218 |
void setDeviceNeedsUpdate(bool v) const { return array_->setDeviceNeedsUpdate(v); } |
| 219 |
void reactivateDeviceWriteViews() const { array_->reactivateDeviceWriteViews(); } |
| 220 |
void reactivateHostWriteViews() const { array_->reactivateHostWriteViews(); } |
| 221 |
|
| 222 |
void haloExchange(bool on_device = false) const; |
| 223 |
void adjointHaloExchange(bool on_device = false) const; |
| 224 |
Halo& halo() const; |
| 225 |
|
| 226 |
void attachObserver(FieldObserver&) const; |
| 227 |
void detachObserver(FieldObserver&) const; |
| 228 |
void callbackOnDestruction(std::function<void()>&& f) { callback_on_destruction_.emplace_back(std::move(f)); } |
| 229 |
|
| 230 |
private: // methods |
| 231 |
void print(std::ostream& os, bool dump = false) const; |
| 232 |
|
| 233 |
private: // members |
| 234 |
mutable std::string name_; |
| 235 |
util::Metadata metadata_; |
| 236 |
array::Array* array_; |
| 237 |
FunctionSpace* functionspace_; |
| 238 |
mutable std::vector<FieldObserver*> field_observers_; |
| 239 |
std::vector<std::function<void()>> callback_on_destruction_; |
| 240 |
mutable std::unique_ptr<Halo> halo_; |
| 241 |
}; |
| 242 |
|
| 243 |
//---------------------------------------------------------------------------------------------------------------------- |
| 244 |
|
| 245 |
class FieldObserver { |
| 246 |
private: |
| 247 |
std::vector<const FieldImpl*> registered_fields_; |
| 248 |
|
| 249 |
public: |
| 250 |
void registerField(const FieldImpl& field) { |
| 251 |
if (std::find(registered_fields_.begin(), registered_fields_.end(), &field) == registered_fields_.end()) { |
| 252 |
registered_fields_.push_back(&field); |
| 253 |
field.attachObserver(*this); |
| 254 |
} |
| 255 |
} |
| 256 |
void unregisterField(const FieldImpl& field) { |
| 257 |
auto found = std::find(registered_fields_.begin(), registered_fields_.end(), &field); |
| 258 |
if (found != registered_fields_.end()) { |
| 259 |
registered_fields_.erase(found); |
| 260 |
field.detachObserver(*this); |
| 261 |
} |
| 262 |
} |
| 263 |
virtual ~FieldObserver() { |
| 264 |
for (auto field : registered_fields_) { |
| 265 |
field->detachObserver(*this); |
| 266 |
} |
| 267 |
} |
| 268 |
|
| 269 |
virtual void onFieldRename(FieldImpl&) {} |
| 270 |
virtual void onFieldDestruction(FieldImpl&) {} |
| 271 |
}; |
| 272 |
|
| 273 |
//---------------------------------------------------------------------------------------------------------------------- |
| 274 |
|
| 275 |
template <typename DATATYPE> |
| 276 |
FieldImpl* FieldImpl::create(const std::string& name, const array::ArrayShape& shape) { |
| 277 |
return create(name, array::DataType::create<DATATYPE>(), shape); |
| 278 |
} |
| 279 |
|
| 280 |
template <typename DATATYPE> |
| 281 |
FieldImpl* FieldImpl::wrap(const std::string& name, DATATYPE* data, const array::ArraySpec& spec) { |
| 282 |
FieldImpl* wrapped = create(name, array::Array::wrap(data, spec)); |
| 283 |
wrapped->set_dirty(false); |
| 284 |
return wrapped; |
| 285 |
} |
| 286 |
|
| 287 |
template <typename DATATYPE> |
| 288 |
FieldImpl* FieldImpl::wrap(const std::string& name, DATATYPE* data, const array::ArrayShape& shape) { |
| 289 |
FieldImpl* wrapped = create(name, array::Array::wrap(data, shape)); |
| 290 |
wrapped->set_dirty(false); |
| 291 |
return wrapped; |
| 292 |
} |
| 293 |
|
| 294 |
//---------------------------------------------------------------------------------------------------------------------- |
| 295 |
|
| 296 |
} // namespace field |
| 297 |
} // namespace atlas |