| 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 <memory> |
| 12 |
#include <sstream> |
| 13 |
|
| 14 |
#include "atlas/library/config.h" |
| 15 |
|
| 16 |
#include "atlas/array/MakeView.h" |
| 17 |
#include "atlas/field/FieldCreator.h" |
| 18 |
#include "atlas/field/detail/FieldImpl.h" |
| 19 |
#include "atlas/runtime/Exception.h" |
| 20 |
#include "atlas/runtime/Log.h" |
| 21 |
|
| 22 |
#if ATLAS_HAVE_FUNCTIONSPACE |
| 23 |
#include "atlas/functionspace/FunctionSpace.h" |
| 24 |
#endif |
| 25 |
|
| 26 |
namespace atlas { |
| 27 |
namespace field { |
| 28 |
|
| 29 |
// ------------------------------------------------------------------------- |
| 30 |
// Static functions |
| 31 |
|
| 32 |
FieldImpl* FieldImpl::create(const eckit::Parametrisation& params) { |
| 33 |
std::string creator_factory; |
| 34 |
if (params.get("creator", creator_factory)) { |
| 35 |
std::unique_ptr<field::FieldCreator> creator(field::FieldCreatorFactory::build(creator_factory, params)); |
| 36 |
return creator->createField(params); |
| 37 |
} |
| 38 |
else { |
| 39 |
throw_Exception( |
| 40 |
"Could not find parameter 'creator' " |
| 41 |
"in Parametrisation for call to FieldImpl::create()"); |
| 42 |
} |
| 43 |
} |
| 44 |
|
| 45 |
FieldImpl* FieldImpl::create(const std::string& name, array::DataType datatype, const array::ArrayShape& shape) { |
| 46 |
return new FieldImpl(name, datatype, shape); |
| 47 |
} |
| 48 |
|
| 49 |
FieldImpl* FieldImpl::create(const std::string& name, array::DataType datatype, array::ArraySpec&& spec) { |
| 50 |
return new FieldImpl(name, datatype, std::move(spec)); |
| 51 |
} |
| 52 |
|
| 53 |
FieldImpl* FieldImpl::create(const std::string& name, array::Array* array) { |
| 54 |
return new FieldImpl(name, array); |
| 55 |
} |
| 56 |
|
| 57 |
// ------------------------------------------------------------------------- |
| 58 |
|
| 59 |
FieldImpl::FieldImpl(const std::string& name, array::DataType datatype, const array::ArrayShape& shape) |
| 60 |
#if ATLAS_HAVE_FUNCTIONSPACE |
| 61 |
:functionspace_(new FunctionSpace()) |
| 62 |
#endif |
| 63 |
{ |
| 64 |
array::label label(name); |
| 65 |
array_ = array::Array::create(datatype, shape); |
| 66 |
array_->attach(); |
| 67 |
rename(name); |
| 68 |
set_levels(0); |
| 69 |
set_variables(0); |
| 70 |
} |
| 71 |
|
| 72 |
FieldImpl::FieldImpl(const std::string& name, array::DataType datatype, array::ArraySpec&& spec) |
| 73 |
#if ATLAS_HAVE_FUNCTIONSPACE |
| 74 |
:functionspace_(new FunctionSpace()) |
| 75 |
#endif |
| 76 |
{ |
| 77 |
array::label label(name); |
| 78 |
array_ = array::Array::create(datatype, std::move(spec)); |
| 79 |
array_->attach(); |
| 80 |
rename(name); |
| 81 |
set_levels(0); |
| 82 |
set_variables(0); |
| 83 |
} |
| 84 |
|
| 85 |
|
| 86 |
FieldImpl::FieldImpl(const std::string& name, array::Array* array) |
| 87 |
#if ATLAS_HAVE_FUNCTIONSPACE |
| 88 |
:functionspace_(new FunctionSpace()) |
| 89 |
#endif |
| 90 |
{ |
| 91 |
array_ = array; |
| 92 |
array_->attach(); |
| 93 |
rename(name); |
| 94 |
set_levels(0); |
| 95 |
set_variables(0); |
| 96 |
} |
| 97 |
|
| 98 |
FieldImpl::~FieldImpl() { |
| 99 |
for (FieldObserver* observer : field_observers_) { |
| 100 |
observer->onFieldDestruction(*this); |
| 101 |
} |
| 102 |
array_->detach(); |
| 103 |
if (array_->owners() == 0) { |
| 104 |
for (auto& f : callback_on_destruction_) { |
| 105 |
f(); |
| 106 |
} |
| 107 |
delete array_; |
| 108 |
} |
| 109 |
#if ATLAS_HAVE_FUNCTIONSPACE |
| 110 |
delete functionspace_; |
| 111 |
#endif |
| 112 |
} |
| 113 |
|
| 114 |
size_t FieldImpl::footprint() const { |
| 115 |
size_t size = sizeof(*this); |
| 116 |
#if ATLAS_HAVE_FUNCTIONSPACE |
| 117 |
size += functionspace_->footprint(); |
| 118 |
#endif |
| 119 |
size += array_->footprint(); |
| 120 |
size += metadata_.footprint(); |
| 121 |
size += name_.capacity() * sizeof(std::string::value_type); |
| 122 |
return size; |
| 123 |
} |
| 124 |
|
| 125 |
bool FieldImpl::dirty() const { |
| 126 |
return not metadata().getBool("halo_updated", false); |
| 127 |
} |
| 128 |
|
| 129 |
void FieldImpl::set_dirty(bool value) const { |
| 130 |
const_cast<FieldImpl&>(*this).metadata().set("halo_updated", !value); |
| 131 |
} |
| 132 |
|
| 133 |
void FieldImpl::dump(std::ostream& os) const { |
| 134 |
print(os, true); |
| 135 |
} |
| 136 |
|
| 137 |
Halo& FieldImpl::halo() const { |
| 138 |
if (not halo_) { |
| 139 |
halo_.reset(new Halo(*const_cast<FieldImpl*>(this))); |
| 140 |
} |
| 141 |
return *halo_; |
| 142 |
} |
| 143 |
|
| 144 |
namespace { |
| 145 |
|
| 146 |
template <typename T> |
| 147 |
std::string vector_to_str(const std::vector<T>& t) { |
| 148 |
std::stringstream s; |
| 149 |
s << '['; |
| 150 |
for (size_t i = 0; i < t.size(); i++) { |
| 151 |
if (i != 0) { |
| 152 |
s << ','; |
| 153 |
} |
| 154 |
s << t[i]; |
| 155 |
} |
| 156 |
s << ']'; |
| 157 |
return s.str(); |
| 158 |
} |
| 159 |
|
| 160 |
} // namespace |
| 161 |
|
| 162 |
void FieldImpl::rename(const std::string& name) { |
| 163 |
metadata().set("name", name); |
| 164 |
for (FieldObserver* observer : field_observers_) { |
| 165 |
observer->onFieldRename(*this); |
| 166 |
} |
| 167 |
} |
| 168 |
|
| 169 |
const std::string& FieldImpl::name() const { |
| 170 |
name_ = metadata().get<std::string>("name"); |
| 171 |
return name_; |
| 172 |
} |
| 173 |
|
| 174 |
void FieldImpl::print(std::ostream& os, bool dump) const { |
| 175 |
os << "FieldImpl[name=" << name() << ",datatype=" << datatype().str() << ",size=" << size() |
| 176 |
<< ",shape=" << vector_to_str(shape()) << ",strides=" << vector_to_str(strides()) |
| 177 |
#if !ATLAS_HAVE_GRIDTOOLS_STORAGE |
| 178 |
<< ",bytes=" << bytes() |
| 179 |
#endif |
| 180 |
<< ",metadata=" << metadata(); |
| 181 |
if (dump) { |
| 182 |
os << ",array=["; |
| 183 |
array_->dump(os); |
| 184 |
os << "]"; |
| 185 |
} |
| 186 |
os << "]"; |
| 187 |
} |
| 188 |
|
| 189 |
std::ostream& operator<<(std::ostream& os, const FieldImpl& f) { |
| 190 |
f.print(os); |
| 191 |
return os; |
| 192 |
} |
| 193 |
|
| 194 |
void FieldImpl::resize(const array::ArrayShape& shape) { |
| 195 |
array_->resize(shape); |
| 196 |
} |
| 197 |
|
| 198 |
void FieldImpl::insert(idx_t idx1, idx_t size1) { |
| 199 |
array_->insert(idx1, size1); |
| 200 |
} |
| 201 |
|
| 202 |
void FieldImpl::set_functionspace(const FunctionSpace& functionspace) { |
| 203 |
#if ATLAS_HAVE_FUNCTIONSPACE |
| 204 |
*functionspace_ = functionspace; |
| 205 |
#else |
| 206 |
throw_Exception("Atlas is compiled without FunctionSpace support", Here()); |
| 207 |
#endif |
| 208 |
} |
| 209 |
|
| 210 |
const FunctionSpace& FieldImpl::functionspace() const { |
| 211 |
#if ATLAS_HAVE_FUNCTIONSPACE |
| 212 |
return *functionspace_; |
| 213 |
#else |
| 214 |
throw_Exception("Atlas is compiled without FunctionSpace support", Here()); |
| 215 |
#endif |
| 216 |
} |
| 217 |
|
| 218 |
void FieldImpl::haloExchange(bool on_device) const { |
| 219 |
if (dirty()) { |
| 220 |
#if ATLAS_HAVE_FUNCTIONSPACE |
| 221 |
ATLAS_ASSERT(functionspace()); |
| 222 |
functionspace().haloExchange(Field(this), on_device); |
| 223 |
set_dirty(false); |
| 224 |
#else |
| 225 |
throw_Exception("Atlas is compiled without FunctionSpace support", Here()); |
| 226 |
#endif |
| 227 |
} |
| 228 |
} |
| 229 |
void FieldImpl::adjointHaloExchange(bool on_device) const { |
| 230 |
#if ATLAS_HAVE_FUNCTIONSPACE |
| 231 |
set_dirty(); |
| 232 |
ATLAS_ASSERT(functionspace()); |
| 233 |
functionspace().adjointHaloExchange(Field(this), on_device); |
| 234 |
#else |
| 235 |
throw_Exception("Atlas is compiled without FunctionSpace support", Here()); |
| 236 |
#endif |
| 237 |
} |
| 238 |
|
| 239 |
void FieldImpl::attachObserver(FieldObserver& observer) const { |
| 240 |
if (std::find(field_observers_.begin(), field_observers_.end(), &observer) == field_observers_.end()) { |
| 241 |
field_observers_.push_back(&observer); |
| 242 |
} |
| 243 |
} |
| 244 |
|
| 245 |
void FieldImpl::detachObserver(FieldObserver& observer) const { |
| 246 |
field_observers_.erase(std::remove(field_observers_.begin(), field_observers_.end(), &observer), |
| 247 |
field_observers_.end()); |
| 248 |
} |
| 249 |
|
| 250 |
|
| 251 |
// ------------------------------------------------------------------ |
| 252 |
|
| 253 |
} // namespace field |
| 254 |
} // namespace atlas |