| 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 <iostream> |
| 12 |
|
| 13 |
#include "atlas/array.h" |
| 14 |
#include "atlas/array/ArrayDataStore.h" |
| 15 |
#include "atlas/array/MakeView.h" |
| 16 |
#include "atlas/array/helpers/ArrayInitializer.h" |
| 17 |
#include "atlas/array/helpers/ArrayWriter.h" |
| 18 |
#include "atlas/array/native/NativeDataStore.h" |
| 19 |
#include "atlas/runtime/Exception.h" |
| 20 |
|
| 21 |
using namespace atlas::array::helpers; |
| 22 |
|
| 23 |
namespace atlas { |
| 24 |
namespace array { |
| 25 |
|
| 26 |
template <typename Value> |
| 27 |
Array* Array::create(idx_t dim0) { |
| 28 |
return new ArrayT<Value>(dim0); |
| 29 |
} |
| 30 |
template <typename Value> |
| 31 |
Array* Array::create(idx_t dim0, idx_t dim1) { |
| 32 |
return new ArrayT<Value>(dim0, dim1); |
| 33 |
} |
| 34 |
template <typename Value> |
| 35 |
Array* Array::create(idx_t dim0, idx_t dim1, idx_t dim2) { |
| 36 |
return new ArrayT<Value>(dim0, dim1, dim2); |
| 37 |
} |
| 38 |
template <typename Value> |
| 39 |
Array* Array::create(idx_t dim0, idx_t dim1, idx_t dim2, idx_t dim3) { |
| 40 |
return new ArrayT<Value>(dim0, dim1, dim2, dim3); |
| 41 |
} |
| 42 |
template <typename Value> |
| 43 |
Array* Array::create(idx_t dim0, idx_t dim1, idx_t dim2, idx_t dim3, idx_t dim4) { |
| 44 |
return new ArrayT<Value>(dim0, dim1, dim2, dim3, dim4); |
| 45 |
} |
| 46 |
template <typename Value> |
| 47 |
Array* Array::create(const ArrayShape& shape) { |
| 48 |
return new ArrayT<Value>(shape); |
| 49 |
} |
| 50 |
template <typename Value> |
| 51 |
Array* Array::create(const ArrayShape& shape, const ArrayLayout& layout) { |
| 52 |
return new ArrayT<Value>(shape, layout); |
| 53 |
} |
| 54 |
template <typename Value> |
| 55 |
Array* Array::wrap(Value* data, const ArrayShape& shape) { |
| 56 |
size_t size = 1; |
| 57 |
for (int i = 0; i < shape.size(); ++i) { |
| 58 |
size *= shape[i]; |
| 59 |
} |
| 60 |
return new ArrayT<Value>(new native::WrappedDataStore<Value>(data, size), shape); |
| 61 |
} |
| 62 |
template <typename Value> |
| 63 |
Array* Array::wrap(Value* data, const ArraySpec& spec) { |
| 64 |
return new ArrayT<Value>(new native::WrappedDataStore<Value>(data, spec), spec); |
| 65 |
} |
| 66 |
|
| 67 |
Array::~Array() = default; |
| 68 |
|
| 69 |
Array* Array::create(DataType datatype, const ArrayShape& shape) { |
| 70 |
switch (datatype.kind()) { |
| 71 |
case DataType::KIND_REAL64: |
| 72 |
return new ArrayT<double>(shape); |
| 73 |
case DataType::KIND_REAL32: |
| 74 |
return new ArrayT<float>(shape); |
| 75 |
case DataType::KIND_INT32: |
| 76 |
return new ArrayT<int>(shape); |
| 77 |
case DataType::KIND_INT64: |
| 78 |
return new ArrayT<long>(shape); |
| 79 |
case DataType::KIND_UINT32: |
| 80 |
return new ArrayT<unsigned int>(shape); |
| 81 |
case DataType::KIND_UINT64: |
| 82 |
return new ArrayT<unsigned long>(shape); |
| 83 |
default: { |
| 84 |
std::stringstream err; |
| 85 |
err << "data kind " << datatype.kind() << " not recognised."; |
| 86 |
throw_NotImplemented(err.str(), Here()); |
| 87 |
} |
| 88 |
} |
| 89 |
} |
| 90 |
|
| 91 |
Array* Array::create(DataType datatype, ArraySpec&& spec) { |
| 92 |
switch (datatype.kind()) { |
| 93 |
case DataType::KIND_REAL64: |
| 94 |
return new ArrayT<double>(std::move(spec)); |
| 95 |
case DataType::KIND_REAL32: |
| 96 |
return new ArrayT<float>(std::move(spec)); |
| 97 |
case DataType::KIND_INT32: |
| 98 |
return new ArrayT<int>(std::move(spec)); |
| 99 |
case DataType::KIND_INT64: |
| 100 |
return new ArrayT<long>(std::move(spec)); |
| 101 |
case DataType::KIND_UINT32: |
| 102 |
return new ArrayT<unsigned int>(std::move(spec)); |
| 103 |
case DataType::KIND_UINT64: |
| 104 |
return new ArrayT<unsigned long>(std::move(spec)); |
| 105 |
default: { |
| 106 |
std::stringstream err; |
| 107 |
err << "data kind " << datatype.kind() << " not recognised."; |
| 108 |
throw_NotImplemented(err.str(), Here()); |
| 109 |
} |
| 110 |
} |
| 111 |
} |
| 112 |
|
| 113 |
Array* Array::create(ArraySpec&& spec) { |
| 114 |
return create(spec.datatype(), std::move(spec)); |
| 115 |
} |
| 116 |
|
| 117 |
|
| 118 |
template <typename Value> |
| 119 |
ArrayT<Value>::ArrayT(ArrayDataStore* ds, const ArraySpec& spec) { |
| 120 |
data_store_ = std::unique_ptr<ArrayDataStore>(ds); |
| 121 |
spec_ = spec; |
| 122 |
} |
| 123 |
|
| 124 |
template <typename Value> |
| 125 |
ArrayT<Value>::ArrayT(idx_t dim0) { |
| 126 |
spec_ = ArraySpec(make_shape(dim0)); |
| 127 |
data_store_ = std::make_unique<native::DataStore<Value>>(spec_.size()); |
| 128 |
} |
| 129 |
template <typename Value> |
| 130 |
ArrayT<Value>::ArrayT(idx_t dim0, idx_t dim1) { |
| 131 |
spec_ = ArraySpec(make_shape(dim0, dim1)); |
| 132 |
data_store_ = std::make_unique<native::DataStore<Value>>(spec_.size()); |
| 133 |
} |
| 134 |
template <typename Value> |
| 135 |
ArrayT<Value>::ArrayT(idx_t dim0, idx_t dim1, idx_t dim2) { |
| 136 |
spec_ = ArraySpec(make_shape(dim0, dim1, dim2)); |
| 137 |
data_store_ = std::make_unique<native::DataStore<Value>>(spec_.size()); |
| 138 |
} |
| 139 |
template <typename Value> |
| 140 |
ArrayT<Value>::ArrayT(idx_t dim0, idx_t dim1, idx_t dim2, idx_t dim3) { |
| 141 |
spec_ = ArraySpec(make_shape(dim0, dim1, dim2, dim3)); |
| 142 |
data_store_ = std::make_unique<native::DataStore<Value>>(spec_.size()); |
| 143 |
} |
| 144 |
template <typename Value> |
| 145 |
ArrayT<Value>::ArrayT(idx_t dim0, idx_t dim1, idx_t dim2, idx_t dim3, idx_t dim4) { |
| 146 |
spec_ = ArraySpec(make_shape(dim0, dim1, dim2, dim3, dim4)); |
| 147 |
data_store_ = std::make_unique<native::DataStore<Value>>(spec_.size()); |
| 148 |
} |
| 149 |
|
| 150 |
template <typename Value> |
| 151 |
ArrayT<Value>::ArrayT(const ArrayShape& shape) { |
| 152 |
ATLAS_ASSERT(shape.size() > 0); |
| 153 |
size_t size = 1; |
| 154 |
for (size_t j = 0; j < shape.size(); ++j) { |
| 155 |
size *= size_t(shape[j]); |
| 156 |
} |
| 157 |
data_store_ = std::make_unique<native::DataStore<Value>>(size); |
| 158 |
spec_ = ArraySpec(shape); |
| 159 |
} |
| 160 |
|
| 161 |
template <typename Value> |
| 162 |
ArrayT<Value>::ArrayT(const ArrayShape& shape, const ArrayAlignment& alignment) { |
| 163 |
spec_ = ArraySpec(shape, alignment); |
| 164 |
data_store_ = std::make_unique<native::DataStore<Value>>(spec_.allocatedSize()); |
| 165 |
} |
| 166 |
|
| 167 |
template <typename Value> |
| 168 |
ArrayT<Value>::ArrayT(const ArrayShape& shape, const ArrayLayout& layout) { |
| 169 |
spec_ = ArraySpec(shape); |
| 170 |
data_store_ = std::make_unique<native::DataStore<Value>>(spec_.size()); |
| 171 |
for (size_t j = 0; j < layout.size(); ++j) { |
| 172 |
ATLAS_ASSERT(spec_.layout()[j] == layout[j]); |
| 173 |
} |
| 174 |
} |
| 175 |
|
| 176 |
template <typename Value> |
| 177 |
ArrayT<Value>::ArrayT(ArraySpec&& spec): Array(std::move(spec)) { |
| 178 |
data_store_ = std::make_unique<native::DataStore<Value>>(spec_.allocatedSize()); |
| 179 |
} |
| 180 |
|
| 181 |
template <typename Value> |
| 182 |
void ArrayT<Value>::resize(const ArrayShape& _shape) { |
| 183 |
if (rank() != static_cast<idx_t>(_shape.size())) { |
| 184 |
std::stringstream msg; |
| 185 |
msg << "Cannot resize existing Array with rank " << rank() << " with a shape of rank " << _shape.size(); |
| 186 |
throw_Exception(msg.str(), Here()); |
| 187 |
} |
| 188 |
|
| 189 |
Array* resized = Array::create<Value>(_shape); |
| 190 |
|
| 191 |
array_initializer::apply(*this,*resized); |
| 192 |
|
| 193 |
replace(*resized); |
| 194 |
delete resized; |
| 195 |
} |
| 196 |
|
| 197 |
|
| 198 |
template <typename Value> |
| 199 |
void ArrayT<Value>::copy(const Array& other, const CopyPolicy&) { |
| 200 |
array_initializer::apply(other,*this); |
| 201 |
} |
| 202 |
|
| 203 |
template <typename Value> |
| 204 |
void ArrayT<Value>::insert(idx_t idx1, idx_t size1) { |
| 205 |
ArrayShape nshape = shape(); |
| 206 |
if (idx1 > nshape[0]) { |
| 207 |
throw_Exception("Cannot insert into an array at a position beyond its size", Here()); |
| 208 |
} |
| 209 |
nshape[0] += size1; |
| 210 |
|
| 211 |
Array* resized = Array::create<Value>(nshape); |
| 212 |
|
| 213 |
array_initializer_partitioned<0>::apply(*this, *resized, idx1, size1); |
| 214 |
replace(*resized); |
| 215 |
delete resized; |
| 216 |
} |
| 217 |
|
| 218 |
template <typename Value> |
| 219 |
void ArrayT<Value>::resize(idx_t size1) { |
| 220 |
resize(make_shape(size1)); |
| 221 |
} |
| 222 |
|
| 223 |
template <typename Value> |
| 224 |
void ArrayT<Value>::resize(idx_t size1, idx_t size2) { |
| 225 |
resize(make_shape(size1, size2)); |
| 226 |
} |
| 227 |
|
| 228 |
template <typename Value> |
| 229 |
void ArrayT<Value>::resize(idx_t size1, idx_t size2, idx_t size3) { |
| 230 |
resize(make_shape(size1, size2, size3)); |
| 231 |
} |
| 232 |
|
| 233 |
template <typename Value> |
| 234 |
void ArrayT<Value>::resize(idx_t size1, idx_t size2, idx_t size3, idx_t size4) { |
| 235 |
resize(make_shape(size1, size2, size3, size4)); |
| 236 |
} |
| 237 |
|
| 238 |
template <typename Value> |
| 239 |
void ArrayT<Value>::resize(idx_t size1, idx_t size2, idx_t size3, idx_t size4, idx_t size5) { |
| 240 |
resize(make_shape(size1, size2, size3, size4, size5)); |
| 241 |
} |
| 242 |
|
| 243 |
template <typename Value> |
| 244 |
void ArrayT<Value>::dump(std::ostream& out) const { |
| 245 |
switch (rank()) { |
| 246 |
case 1: |
| 247 |
make_host_view<const Value, 1>(*this).dump(out); |
| 248 |
break; |
| 249 |
case 2: |
| 250 |
make_host_view<const Value, 2>(*this).dump(out); |
| 251 |
break; |
| 252 |
case 3: |
| 253 |
make_host_view<const Value, 3>(*this).dump(out); |
| 254 |
break; |
| 255 |
case 4: |
| 256 |
make_host_view<const Value, 4>(*this).dump(out); |
| 257 |
break; |
| 258 |
case 5: |
| 259 |
make_host_view<const Value, 5>(*this).dump(out); |
| 260 |
break; |
| 261 |
case 6: |
| 262 |
make_host_view<const Value, 6>(*this).dump(out); |
| 263 |
break; |
| 264 |
case 7: |
| 265 |
make_host_view<const Value, 7>(*this).dump(out); |
| 266 |
break; |
| 267 |
case 8: |
| 268 |
make_host_view<const Value, 8>(*this).dump(out); |
| 269 |
break; |
| 270 |
case 9: |
| 271 |
make_host_view<const Value, 9>(*this).dump(out); |
| 272 |
break; |
| 273 |
default: |
| 274 |
ATLAS_NOTIMPLEMENTED; |
| 275 |
} |
| 276 |
} |
| 277 |
|
| 278 |
//------------------------------------------------------------------------------ |
| 279 |
|
| 280 |
template <typename Value> |
| 281 |
size_t ArrayT<Value>::footprint() const { |
| 282 |
size_t size = sizeof(*this); |
| 283 |
size += bytes(); |
| 284 |
if (not contiguous()) { |
| 285 |
ATLAS_NOTIMPLEMENTED; |
| 286 |
} |
| 287 |
return size; |
| 288 |
} |
| 289 |
|
| 290 |
template <typename Value> |
| 291 |
void ArrayT<Value>::accMap() const { |
| 292 |
data_store_->accMap(); |
| 293 |
} |
| 294 |
|
| 295 |
template <typename Value> |
| 296 |
void ArrayT<Value>::accUnmap() const { |
| 297 |
data_store_->accUnmap(); |
| 298 |
} |
| 299 |
|
| 300 |
template <typename Value> |
| 301 |
bool ArrayT<Value>::accMapped() const { |
| 302 |
return data_store_->accMapped(); |
| 303 |
} |
| 304 |
|
| 305 |
//------------------------------------------------------------------------------ |
| 306 |
|
| 307 |
template Array* Array::create<int>(idx_t); |
| 308 |
template Array* Array::create<long>(idx_t); |
| 309 |
template Array* Array::create<float>(idx_t); |
| 310 |
template Array* Array::create<double>(idx_t); |
| 311 |
template Array* Array::create<long unsigned>(idx_t); |
| 312 |
template Array* Array::create<unsigned int>(idx_t); |
| 313 |
|
| 314 |
template Array* Array::create<int>(idx_t, idx_t); |
| 315 |
template Array* Array::create<long>(idx_t, idx_t); |
| 316 |
template Array* Array::create<float>(idx_t, idx_t); |
| 317 |
template Array* Array::create<double>(idx_t, idx_t); |
| 318 |
template Array* Array::create<long unsigned>(idx_t, idx_t); |
| 319 |
template Array* Array::create<unsigned int>(idx_t, idx_t); |
| 320 |
|
| 321 |
template Array* Array::create<int>(idx_t, idx_t, idx_t); |
| 322 |
template Array* Array::create<long>(idx_t, idx_t, idx_t); |
| 323 |
template Array* Array::create<float>(idx_t, idx_t, idx_t); |
| 324 |
template Array* Array::create<double>(idx_t, idx_t, idx_t); |
| 325 |
template Array* Array::create<unsigned int>(idx_t, idx_t, idx_t); |
| 326 |
|
| 327 |
template Array* Array::create<int>(idx_t, idx_t, idx_t, idx_t); |
| 328 |
template Array* Array::create<long>(idx_t, idx_t, idx_t, idx_t); |
| 329 |
template Array* Array::create<float>(idx_t, idx_t, idx_t, idx_t); |
| 330 |
template Array* Array::create<double>(idx_t, idx_t, idx_t, idx_t); |
| 331 |
template Array* Array::create<unsigned int>(idx_t, idx_t, idx_t, idx_t); |
| 332 |
|
| 333 |
template Array* Array::create<int>(idx_t, idx_t, idx_t, idx_t, idx_t); |
| 334 |
template Array* Array::create<long>(idx_t, idx_t, idx_t, idx_t, idx_t); |
| 335 |
template Array* Array::create<float>(idx_t, idx_t, idx_t, idx_t, idx_t); |
| 336 |
template Array* Array::create<double>(idx_t, idx_t, idx_t, idx_t, idx_t); |
| 337 |
template Array* Array::create<unsigned int>(idx_t, idx_t, idx_t, idx_t, idx_t); |
| 338 |
|
| 339 |
template Array* Array::create<int>(const ArrayShape&); |
| 340 |
template Array* Array::create<long>(const ArrayShape&); |
| 341 |
template Array* Array::create<float>(const ArrayShape&); |
| 342 |
template Array* Array::create<double>(const ArrayShape&); |
| 343 |
template Array* Array::create<long unsigned>(const ArrayShape&); |
| 344 |
template Array* Array::create<unsigned int>(const ArrayShape&); |
| 345 |
|
| 346 |
template Array* Array::create<int>(const ArrayShape&, const ArrayLayout&); |
| 347 |
template Array* Array::create<long>(const ArrayShape&, const ArrayLayout&); |
| 348 |
template Array* Array::create<float>(const ArrayShape&, const ArrayLayout&); |
| 349 |
template Array* Array::create<double>(const ArrayShape&, const ArrayLayout&); |
| 350 |
template Array* Array::create<long unsigned>(const ArrayShape&, const ArrayLayout&); |
| 351 |
template Array* Array::create<unsigned int>(const ArrayShape&, const ArrayLayout&); |
| 352 |
|
| 353 |
template Array* Array::wrap<int>(int*, const ArrayShape&); |
| 354 |
template Array* Array::wrap<long>(long*, const ArrayShape&); |
| 355 |
template Array* Array::wrap<float>(float*, const ArrayShape&); |
| 356 |
template Array* Array::wrap<double>(double*, const ArrayShape&); |
| 357 |
template Array* Array::wrap<long unsigned>(long unsigned*, const ArrayShape&); |
| 358 |
template Array* Array::wrap<unsigned int>(unsigned int*, const ArrayShape&); |
| 359 |
|
| 360 |
template Array* Array::wrap<int>(int*, const ArraySpec&); |
| 361 |
template Array* Array::wrap<long>(long*, const ArraySpec&); |
| 362 |
template Array* Array::wrap<float>(float*, const ArraySpec&); |
| 363 |
template Array* Array::wrap<double>(double*, const ArraySpec&); |
| 364 |
template Array* Array::wrap<long unsigned>(long unsigned*, const ArraySpec&); |
| 365 |
template Array* Array::wrap<unsigned int>(unsigned int*, const ArraySpec&); |
| 366 |
|
| 367 |
template class ArrayT<int>; |
| 368 |
template class ArrayT<long>; |
| 369 |
template class ArrayT<float>; |
| 370 |
template class ArrayT<double>; |
| 371 |
template class ArrayT<unsigned int>; |
| 372 |
template class ArrayT<unsigned long>; |
| 373 |
|
| 374 |
} // namespace array |
| 375 |
} // namespace atlas |