| 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 <cstring> |
| 12 |
|
| 13 |
#include "atlas/library/config.h" |
| 14 |
#include "atlas/field/Field.h" |
| 15 |
#include "atlas/field/detail/FieldImpl.h" |
| 16 |
#include "atlas/field/detail/FieldInterface.h" |
| 17 |
#include "atlas/runtime/Exception.h" |
| 18 |
|
| 19 |
#if ATLAS_HAVE_FUNCTIONSPACE |
| 20 |
#include "atlas/functionspace/FunctionSpace.h" |
| 21 |
#endif |
| 22 |
namespace atlas { |
| 23 |
namespace field { |
| 24 |
|
| 25 |
// ------------------------------------------------------------------ |
| 26 |
// C wrapper interfaces to C++ routines |
| 27 |
|
| 28 |
namespace { |
| 29 |
template <typename Value> |
| 30 |
void atlas__Field__data_specf(FieldImpl* This, Value*& data, int& rank, int*& shapef, int*& stridesf) { |
| 31 |
ATLAS_ASSERT(This != nullptr, "Cannot access data of uninitialised atlas_Field"); |
| 32 |
if (This->datatype() != array::make_datatype<Value>()) { |
| 33 |
throw_Exception("Datatype mismatch for accessing field data"); |
| 34 |
} |
| 35 |
data = This->data<Value>(); |
| 36 |
shapef = const_cast<int*>(This->shapef().data()); |
| 37 |
stridesf = const_cast<int*>(This->stridesf().data()); |
| 38 |
rank = This->shapef().size(); |
| 39 |
} |
| 40 |
|
| 41 |
template <typename Value> |
| 42 |
void atlas__Field__device_data_specf(FieldImpl* This, Value*& data, int& rank, int*& shapef, int*& stridesf) { |
| 43 |
ATLAS_ASSERT(This != nullptr, "Cannot access data of uninitialised atlas_Field"); |
| 44 |
if (This->datatype() != array::make_datatype<Value>()) { |
| 45 |
throw_Exception("Datatype mismatch for accessing field data"); |
| 46 |
} |
| 47 |
data = This->array().device_data<Value>(); |
| 48 |
shapef = const_cast<int*>(This->shapef().data()); |
| 49 |
if (data == This->array().host_data<Value>()) { |
| 50 |
stridesf = const_cast<int*>(This->stridesf().data()); |
| 51 |
} |
| 52 |
else { |
| 53 |
stridesf = const_cast<int*>(This->device_stridesf().data()); |
| 54 |
} |
| 55 |
rank = This->shapef().size(); |
| 56 |
} |
| 57 |
|
| 58 |
template <typename Value> |
| 59 |
FieldImpl* atlas__Field__wrap_specf(const char* name, Value data[], int rank, int shapef[], int stridesf[]) { |
| 60 |
array::ArrayShape shape; |
| 61 |
shape.resize(rank); |
| 62 |
array::ArrayStrides strides; |
| 63 |
strides.resize(rank); |
| 64 |
idx_t jf = rank - 1; |
| 65 |
for (int j = 0; j < rank; ++j) { |
| 66 |
shape[j] = shapef[jf]; |
| 67 |
strides[j] = stridesf[jf]; |
| 68 |
--jf; |
| 69 |
} |
| 70 |
FieldImpl* field; |
| 71 |
{ |
| 72 |
Field wrapped(std::string(name), data, array::ArraySpec(shape, strides)); |
| 73 |
field = wrapped.get(); |
| 74 |
field->attach(); |
| 75 |
} |
| 76 |
field->detach(); |
| 77 |
ATLAS_ASSERT(field); |
| 78 |
return field; |
| 79 |
} |
| 80 |
|
| 81 |
|
| 82 |
} // namespace |
| 83 |
|
| 84 |
extern "C" { |
| 85 |
|
| 86 |
FieldImpl* atlas__Field__wrap_int_specf(const char* name, int data[], int rank, int shapef[], int stridesf[]) { |
| 87 |
return atlas__Field__wrap_specf(name, data, rank, shapef, stridesf); |
| 88 |
} |
| 89 |
|
| 90 |
FieldImpl* atlas__Field__wrap_long_specf(const char* name, long data[], int rank, int shapef[], int stridesf[]) { |
| 91 |
return atlas__Field__wrap_specf(name, data, rank, shapef, stridesf); |
| 92 |
} |
| 93 |
|
| 94 |
FieldImpl* atlas__Field__wrap_float_specf(const char* name, float data[], int rank, int shapef[], int stridesf[]) { |
| 95 |
return atlas__Field__wrap_specf(name, data, rank, shapef, stridesf); |
| 96 |
} |
| 97 |
|
| 98 |
FieldImpl* atlas__Field__wrap_double_specf(const char* name, double data[], int rank, int shapef[], int stridesf[]) { |
| 99 |
return atlas__Field__wrap_specf(name, data, rank, shapef, stridesf); |
| 100 |
} |
| 101 |
|
| 102 |
FieldImpl* atlas__Field__create(eckit::Parametrisation* params) { |
| 103 |
ATLAS_ASSERT(params != nullptr); |
| 104 |
FieldImpl* field; |
| 105 |
{ |
| 106 |
Field f(*params); |
| 107 |
field = f.get(); |
| 108 |
field->attach(); |
| 109 |
} |
| 110 |
field->detach(); |
| 111 |
ATLAS_ASSERT(field); |
| 112 |
return field; |
| 113 |
} |
| 114 |
|
| 115 |
void atlas__Field__delete(FieldImpl* This) { |
| 116 |
delete This; |
| 117 |
} |
| 118 |
|
| 119 |
const char* atlas__Field__name(FieldImpl* This) { |
| 120 |
ATLAS_ASSERT(This != nullptr, "Cannot access name of uninitialised atlas_Field"); |
| 121 |
return This->name().c_str(); |
| 122 |
} |
| 123 |
|
| 124 |
void atlas__Field__datatype(FieldImpl* This, char*& datatype, int& size, int& allocated) { |
| 125 |
ATLAS_ASSERT(This != nullptr, "Cannot access datatype of uninitialised atlas_Field"); |
| 126 |
std::string s = This->datatype().str(); |
| 127 |
size = static_cast<int>(s.size()); |
| 128 |
datatype = new char[size + 1]; |
| 129 |
std::strncpy(datatype, s.c_str(), size + 1); |
| 130 |
allocated = true; |
| 131 |
} |
| 132 |
|
| 133 |
int atlas__Field__size(FieldImpl* This) { |
| 134 |
ATLAS_ASSERT(This != nullptr, "Cannot access size of uninitialised atlas_Field"); |
| 135 |
return This->size(); |
| 136 |
} |
| 137 |
|
| 138 |
int atlas__Field__rank(FieldImpl* This) { |
| 139 |
ATLAS_ASSERT(This != nullptr, "Cannot access rank of uninitialised atlas_Field"); |
| 140 |
return This->rank(); |
| 141 |
} |
| 142 |
|
| 143 |
int atlas__Field__kind(FieldImpl* This) { |
| 144 |
ATLAS_ASSERT(This != nullptr, "Cannot access data kind of uninitialised atlas_Field"); |
| 145 |
return This->datatype().kind(); |
| 146 |
} |
| 147 |
|
| 148 |
double atlas__Field__bytes(FieldImpl* This) { |
| 149 |
ATLAS_ASSERT(This != nullptr, "Cannot access bytes occupied by uninitialised atlas_Field"); |
| 150 |
return This->bytes(); |
| 151 |
} |
| 152 |
|
| 153 |
int atlas__Field__levels(FieldImpl* This) { |
| 154 |
ATLAS_ASSERT(This != nullptr, "Cannot access levels of uninitialised atlas_Field"); |
| 155 |
return This->levels(); |
| 156 |
} |
| 157 |
|
| 158 |
util::Metadata* atlas__Field__metadata(FieldImpl* This) { |
| 159 |
ATLAS_ASSERT(This != nullptr, "Cannot access metadata of uninitialised atlas_Field"); |
| 160 |
return &This->metadata(); |
| 161 |
} |
| 162 |
|
| 163 |
int atlas__Field__has_functionspace(FieldImpl* This) { |
| 164 |
ATLAS_ASSERT(This != nullptr, "Cannot access uninitialised atlas_Field"); |
| 165 |
#if ATLAS_HAVE_FUNCTIONSPACE |
| 166 |
return (This->functionspace() != 0); |
| 167 |
#else |
| 168 |
return 0; |
| 169 |
#endif |
| 170 |
} |
| 171 |
|
| 172 |
const functionspace::FunctionSpaceImpl* atlas__Field__functionspace(FieldImpl* This) { |
| 173 |
ATLAS_ASSERT(This != nullptr, "Cannot access uninitialised atlas_Field"); |
| 174 |
#if ATLAS_HAVE_FUNCTIONSPACE |
| 175 |
return This->functionspace().get(); |
| 176 |
#else |
| 177 |
throw_Exception("Atlas is not compiled with FunctionSpace support", Here()); |
| 178 |
#endif |
| 179 |
} |
| 180 |
|
| 181 |
void atlas__Field__shapef(FieldImpl* This, int*& shape, int& rank) { |
| 182 |
ATLAS_ASSERT(This != nullptr, "Cannot access bytes occupied by uninitialised atlas_Field"); |
| 183 |
shape = const_cast<int*>(&This->shapef().front()); |
| 184 |
rank = This->shapef().size(); |
| 185 |
} |
| 186 |
|
| 187 |
void atlas__Field__stridesf(FieldImpl* This, int*& shape, int& rank) { |
| 188 |
ATLAS_ASSERT(This != nullptr, "Cannot access bytes occupied by uninitialised atlas_Field"); |
| 189 |
shape = const_cast<int*>(&This->stridesf().front()); |
| 190 |
rank = This->stridesf().size(); |
| 191 |
} |
| 192 |
|
| 193 |
void atlas__Field__data_int_specf(FieldImpl* This, int*& data, int& rank, int*& shapef, int*& stridesf) { |
| 194 |
atlas__Field__data_specf(This, data, rank, shapef, stridesf); |
| 195 |
} |
| 196 |
|
| 197 |
void atlas__Field__data_long_specf(FieldImpl* This, long*& data, int& rank, int*& shapef, int*& stridesf) { |
| 198 |
atlas__Field__data_specf(This, data, rank, shapef, stridesf); |
| 199 |
} |
| 200 |
|
| 201 |
void atlas__Field__data_float_specf(FieldImpl* This, float*& data, int& rank, int*& shapef, int*& stridesf) { |
| 202 |
atlas__Field__data_specf(This, data, rank, shapef, stridesf); |
| 203 |
} |
| 204 |
|
| 205 |
void atlas__Field__data_double_specf(FieldImpl* This, double*& data, int& rank, int*& shapef, int*& stridesf) { |
| 206 |
atlas__Field__data_specf(This, data, rank, shapef, stridesf); |
| 207 |
} |
| 208 |
|
| 209 |
void atlas__Field__device_data_int_specf(FieldImpl* This, int*& data, int& rank, int*& shapef, int*& stridesf) { |
| 210 |
atlas__Field__device_data_specf(This, data, rank, shapef, stridesf); |
| 211 |
} |
| 212 |
|
| 213 |
void atlas__Field__device_data_long_specf(FieldImpl* This, long*& data, int& rank, int*& shapef, int*& stridesf) { |
| 214 |
atlas__Field__device_data_specf(This, data, rank, shapef, stridesf); |
| 215 |
} |
| 216 |
|
| 217 |
void atlas__Field__device_data_float_specf(FieldImpl* This, float*& data, int& rank, int*& shapef, int*& stridesf) { |
| 218 |
atlas__Field__device_data_specf(This, data, rank, shapef, stridesf); |
| 219 |
} |
| 220 |
|
| 221 |
void atlas__Field__device_data_double_specf(FieldImpl* This, double*& data, int& rank, int*& shapef, int*& stridesf) { |
| 222 |
atlas__Field__device_data_specf(This, data, rank, shapef, stridesf); |
| 223 |
} |
| 224 |
|
| 225 |
int atlas__Field__host_needs_update(const FieldImpl* This) { |
| 226 |
return This->hostNeedsUpdate(); |
| 227 |
} |
| 228 |
|
| 229 |
int atlas__Field__device_needs_update(const FieldImpl* This) { |
| 230 |
return This->deviceNeedsUpdate(); |
| 231 |
} |
| 232 |
|
| 233 |
int atlas__Field__device_allocated(const FieldImpl* This) { |
| 234 |
return This->deviceAllocated(); |
| 235 |
} |
| 236 |
|
| 237 |
void atlas__Field__rename(FieldImpl* This, const char* name) { |
| 238 |
ATLAS_ASSERT(This, "Cannot rename uninitialised atlas_Field"); |
| 239 |
This->rename(std::string(name)); |
| 240 |
} |
| 241 |
|
| 242 |
void atlas__Field__set_levels(FieldImpl* This, int levels) { |
| 243 |
ATLAS_ASSERT(This != nullptr, "Cannot set levels of uninitialised atlas_Field"); |
| 244 |
This->set_levels(levels); |
| 245 |
} |
| 246 |
|
| 247 |
# |
| 248 |
void atlas__Field__set_functionspace(FieldImpl* This, const functionspace::FunctionSpaceImpl* functionspace) { |
| 249 |
ATLAS_ASSERT(This != nullptr, "Cannot set functionspace in uninitialised atlas_Field"); |
| 250 |
ATLAS_ASSERT(functionspace != nullptr, "Cannot set uninitialised atlas_FunctionSpace in atlas_Field"); |
| 251 |
#if ATLAS_HAVE_FUNCTIONSPACE |
| 252 |
This->set_functionspace(functionspace); |
| 253 |
#else |
| 254 |
throw_Exception("Atlas is not compiled with FunctionSpace support", Here()); |
| 255 |
#endif |
| 256 |
} |
| 257 |
|
| 258 |
void atlas__Field__set_host_needs_update(const FieldImpl* This, int value) { |
| 259 |
ATLAS_ASSERT(This != nullptr, "Cannot set value for uninitialised atlas_Field"); |
| 260 |
This->setHostNeedsUpdate(value); |
| 261 |
} |
| 262 |
|
| 263 |
void atlas__Field__set_device_needs_update(const FieldImpl* This, int value) { |
| 264 |
ATLAS_ASSERT(This != nullptr, "Cannot set value for uninitialised atlas_Field"); |
| 265 |
This->setDeviceNeedsUpdate(value); |
| 266 |
} |
| 267 |
|
| 268 |
void atlas__Field__update_device(FieldImpl* This) { |
| 269 |
ATLAS_ASSERT(This != nullptr, "Cannot access uninitialised atlas_Field"); |
| 270 |
This->updateDevice(); |
| 271 |
} |
| 272 |
|
| 273 |
void atlas__Field__update_host(FieldImpl* This) { |
| 274 |
ATLAS_ASSERT(This != nullptr, "Cannot access uninitialised atlas_Field"); |
| 275 |
This->updateHost(); |
| 276 |
} |
| 277 |
|
| 278 |
void atlas__Field__sync_device(FieldImpl* This) { |
| 279 |
ATLAS_ASSERT(This != nullptr, "Cannot access uninitialised atlas_Field"); |
| 280 |
This->syncDevice(); |
| 281 |
} |
| 282 |
|
| 283 |
void atlas__Field__sync_host(FieldImpl* This) { |
| 284 |
ATLAS_ASSERT(This != nullptr, "Cannot access uninitialised atlas_Field"); |
| 285 |
This->syncHost(); |
| 286 |
} |
| 287 |
|
| 288 |
void atlas__Field__sync_host_device(FieldImpl* This) { |
| 289 |
ATLAS_ASSERT(This != nullptr, "Cannot access uninitialised atlas_Field"); |
| 290 |
This->syncHostDevice(); |
| 291 |
} |
| 292 |
|
| 293 |
void atlas__Field__allocate_device(FieldImpl* This) { |
| 294 |
ATLAS_ASSERT(This != nullptr, "Cannot access uninitialised atlas_Field"); |
| 295 |
This->allocateDevice(); |
| 296 |
} |
| 297 |
|
| 298 |
void atlas__Field__deallocate_device(FieldImpl* This) { |
| 299 |
ATLAS_ASSERT(This != nullptr, "Cannot access uninitialised atlas_Field"); |
| 300 |
This->deallocateDevice(); |
| 301 |
} |
| 302 |
|
| 303 |
void atlas__Field__set_dirty(FieldImpl* This, int value) { |
| 304 |
ATLAS_ASSERT(This != nullptr, "Cannot access uninitialised atlas_Field"); |
| 305 |
This->set_dirty(value); |
| 306 |
} |
| 307 |
|
| 308 |
int atlas__Field__dirty(FieldImpl* This) { |
| 309 |
ATLAS_ASSERT(This != nullptr, "Cannot access uninitialised atlas_Field"); |
| 310 |
return This->dirty(); |
| 311 |
} |
| 312 |
|
| 313 |
int atlas__Field__contiguous(FieldImpl* This) { |
| 314 |
ATLAS_ASSERT(This != nullptr, "Cannot access uninitialised atlas_Field"); |
| 315 |
return This->array().contiguous(); |
| 316 |
} |
| 317 |
|
| 318 |
void atlas__Field__halo_exchange(FieldImpl* This, int on_device) { |
| 319 |
ATLAS_ASSERT(This != nullptr, "Cannot halo-exchange uninitialised atlas_Field"); |
| 320 |
This->haloExchange(on_device); |
| 321 |
} |
| 322 |
|
| 323 |
void atlas__Field__adjoint_halo_exchange(FieldImpl* This, int on_device) { |
| 324 |
ATLAS_ASSERT(This != nullptr, "Cannot adjoint-halo-exchange uninitialised atlas_Field"); |
| 325 |
This->adjointHaloExchange(on_device); |
| 326 |
} |
| 327 |
} |
| 328 |
|
| 329 |
// ------------------------------------------------------------------ |
| 330 |
|
| 331 |
} // namespace field |
| 332 |
} // namespace atlas |