atlas
FieldImpl.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 
13 
14 #pragma once
15 
16 #include <functional>
17 #include <string>
18 #include <vector>
19 
20 #include "atlas/util/Object.h"
21 
22 #include "atlas/array.h"
23 #include "atlas/array/ArrayUtil.h"
24 #include "atlas/array/DataType.h"
25 #include "atlas/util/Metadata.h"
26 
27 namespace eckit {
28 class Parametrisation;
29 }
30 
31 namespace atlas {
32 class FunctionSpace;
33 } // namespace atlas
34 
35 namespace atlas {
36 namespace field {
37 
38 //----------------------------------------------------------------------------------------------------------------------
39 
40 class FieldImpl : public util::Object {
41 public: // Static methods
43  static FieldImpl* create( const eckit::Parametrisation& );
44 
46  static FieldImpl* create( const std::string& name, array::DataType,
48 
50  static FieldImpl* create( const std::string& name, array::DataType, array::ArraySpec&& );
51 
53  template <typename DATATYPE>
54  static FieldImpl* create( const std::string& name, const array::ArrayShape& = array::ArrayShape() );
55 
57  static FieldImpl* create( const std::string& name, array::Array* );
58 
61  template <typename DATATYPE>
62  static FieldImpl* wrap( const std::string& name, DATATYPE* data, const array::ArraySpec& );
63 
66  template <typename DATATYPE>
67  static FieldImpl* wrap( const std::string& name, DATATYPE* data, const array::ArrayShape& );
68 
69 private: // Private constructors to force use of static create functions
71  FieldImpl( const std::string& name, array::DataType, const array::ArrayShape& );
72 
74  FieldImpl( const std::string& name, array::DataType, array::ArraySpec&& );
75 
77  FieldImpl( const std::string& name, array::Array* );
78 
79 public: // Destructor
80  virtual ~FieldImpl();
81 
82  // -- Conversion
83 
85  operator const array::Array&() const { return *array_; }
86  operator array::Array&() { return *array_; }
87 
88  const array::Array& array() const { return *array_; }
89  array::Array& array() { return *array_; }
90 
91  // -- Accessors
92 
94  void* storage() { return array_->storage(); }
95 
97  array::DataType datatype() const { return array_->datatype(); }
98 
100  const std::string& name() const;
101 
103  void rename( const std::string& name ) { metadata().set( "name", name ); }
104 
106  const util::Metadata& metadata() const { return metadata_; }
107  util::Metadata& metadata() { return metadata_; }
108 
110  void resize( const array::ArrayShape& );
111 
112  void insert( idx_t idx1, idx_t size1 );
113 
115  const std::vector<int>& shapef() const { return array_->shapef(); }
116 
118  const std::vector<int>& stridesf() const { return array_->stridesf(); }
119 
121  const array::ArrayShape& shape() const { return array_->shape(); }
122 
124  const array::ArrayStrides& strides() const { return array_->strides(); }
125 
127  idx_t shape( idx_t i ) const { return array_->shape( i ); }
128 
130  idx_t stride( idx_t i ) const { return array_->stride( i ); }
131 
133  size_t size() const { return array_->size(); }
134 
136  idx_t rank() const { return array_->rank(); }
137 
139  size_t bytes() const { return array_->bytes(); }
140 
142  friend std::ostream& operator<<( std::ostream& os, const FieldImpl& v );
143 
145  void dump( std::ostream& os ) const;
146 
148  void set_levels( idx_t n ) { metadata().set( "levels", n ); }
149  void set_variables( idx_t n ) { metadata().set( "variables", n ); }
150  idx_t levels() const { return metadata().get<idx_t>( "levels" ); }
151  idx_t variables() const { return metadata().get<idx_t>( "variables" ); }
152 
153  void set_functionspace( const FunctionSpace& );
154  const FunctionSpace& functionspace() const;
155 
157  size_t footprint() const;
158 
159  bool dirty() const;
160 
161  void set_dirty( bool = true ) const;
162 
163  // -- dangerous methods
164  template <typename DATATYPE>
165  DATATYPE const* host_data() const {
166  return array_->host_data<DATATYPE>();
167  }
168  template <typename DATATYPE>
169  DATATYPE* host_data() {
170  return array_->host_data<DATATYPE>();
171  }
172  template <typename DATATYPE>
173  DATATYPE const* device_data() const {
174  return array_->device_data<DATATYPE>();
175  }
176  template <typename DATATYPE>
177  DATATYPE* device_data() {
178  return array_->device_data<DATATYPE>();
179  }
180  template <typename DATATYPE>
181  DATATYPE const* data() const {
182  return array_->host_data<DATATYPE>();
183  }
184  template <typename DATATYPE>
185  DATATYPE* data() {
186  return array_->host_data<DATATYPE>();
187  }
188 
189  // -- Methods related to host-device synchronisation, requires
190  // gridtools_storage
191  void updateHost() const { array_->updateHost(); }
192  void updateDevice() const { array_->updateDevice(); }
193  void syncHostDevice() const { array_->syncHostDevice(); }
194  bool hostNeedsUpdate() const { return array_->hostNeedsUpdate(); }
195  bool deviceNeedsUpdate() const { return array_->deviceNeedsUpdate(); }
196  void reactivateDeviceWriteViews() const { array_->reactivateDeviceWriteViews(); }
197  void reactivateHostWriteViews() const { array_->reactivateHostWriteViews(); }
198 
199  void haloExchange( bool on_device = false ) const;
200  void adjointHaloExchange( bool on_device = false ) const;
201 
202 
203  void callbackOnDestruction( std::function<void()>&& f ) { callback_on_destruction_.emplace_back( std::move( f ) ); }
204 
205 private: // methods
206  void print( std::ostream& os, bool dump = false ) const;
207 
208 private: // members
209  mutable std::string name_;
210  util::Metadata metadata_;
211  array::Array* array_;
212  FunctionSpace* functionspace_;
213  std::vector<std::function<void()>> callback_on_destruction_;
214 };
215 
216 //----------------------------------------------------------------------------------------------------------------------
217 
218 template <typename DATATYPE>
219 FieldImpl* FieldImpl::create( const std::string& name, const array::ArrayShape& shape ) {
220  return create( name, array::DataType::create<DATATYPE>(), shape );
221 }
222 
223 template <typename DATATYPE>
224 FieldImpl* FieldImpl::wrap( const std::string& name, DATATYPE* data, const array::ArraySpec& spec ) {
225  FieldImpl* wrapped = create( name, array::Array::wrap( data, spec ) );
226  wrapped->set_dirty( false );
227  return wrapped;
228 }
229 
230 template <typename DATATYPE>
231 FieldImpl* FieldImpl::wrap( const std::string& name, DATATYPE* data, const array::ArrayShape& shape ) {
232  FieldImpl* wrapped = create( name, array::Array::wrap( data, shape ) );
233  wrapped->set_dirty( false );
234  return wrapped;
235 }
236 
237 //----------------------------------------------------------------------------------------------------------------------
238 
239 } // namespace field
240 } // namespace atlas
array::DataType datatype() const
Internal data type of field.
Definition: FieldImpl.h:97
void * storage()
Access to raw data.
Definition: FieldImpl.h:94
Definition: ArrayShape.h:33
idx_t shape(idx_t i) const
Shape of this field associated to index &#39;i&#39;.
Definition: FieldImpl.h:127
const array::ArrayStrides & strides() const
Strides of this field.
Definition: FieldImpl.h:124
const std::vector< int > & stridesf() const
Strides of this field in Fortran style (reverse order of C style)
Definition: FieldImpl.h:118
Definition: ArraySpec.h:27
const util::Metadata & metadata() const
Access to metadata associated to this field.
Definition: FieldImpl.h:106
Definition: ArrayStrides.h:23
Definition: Object.h:18
Definition: Domain.h:19
void set_levels(idx_t n)
Metadata that is more intrinsic to the Field, and queried often.
Definition: FieldImpl.h:148
idx_t stride(idx_t i) const
Stride of this field associated to index &#39;i&#39;.
Definition: FieldImpl.h:130
Definition: DataType.h:40
size_t bytes() const
Number of bytes occupied by the values of this field.
Definition: FieldImpl.h:139
Definition: Metadata.h:23
size_t size() const
Number of values stored in this field.
Definition: FieldImpl.h:133
idx_t rank() const
Rank of field.
Definition: FieldImpl.h:136
const std::vector< int > & shapef() const
Shape of this field in Fortran style (reverse order of C style)
Definition: FieldImpl.h:115
Definition: Array.h:37
Contains all atlas classes and methods.
Definition: atlas-grids.cc:33
void rename(const std::string &name)
Rename this field.
Definition: FieldImpl.h:103
Definition: FieldImpl.h:40
long idx_t
Integer type for indices in connectivity tables.
Definition: config.h:42
const array::ArrayShape & shape() const
Shape of this field (reverse order of Fortran style)
Definition: FieldImpl.h:121
Definition: FunctionSpace.h:39