/* * (C) Copyright 2013 ECMWF. * * This software is licensed under the terms of the Apache Licence Version 2.0 * which can be obtained at http://www.apache.org/licenses/LICENSE-2.0. * In applying this licence, ECMWF does not waive the privileges and immunities * granted to it by virtue of its status as an intergovernmental organisation * nor does it submit to any jurisdiction. */ #include #include "atlas/array.h" #include "atlas/array/ArrayDataStore.h" #include "atlas/array/MakeView.h" #include "atlas/array/helpers/ArrayInitializer.h" #include "atlas/array/helpers/ArrayWriter.h" #include "atlas/array/native/NativeDataStore.h" #include "atlas/runtime/Exception.h" using namespace atlas::array::helpers; namespace atlas { namespace array { template Array* Array::create(idx_t dim0) { return new ArrayT(dim0); } template Array* Array::create(idx_t dim0, idx_t dim1) { return new ArrayT(dim0, dim1); } template Array* Array::create(idx_t dim0, idx_t dim1, idx_t dim2) { return new ArrayT(dim0, dim1, dim2); } template Array* Array::create(idx_t dim0, idx_t dim1, idx_t dim2, idx_t dim3) { return new ArrayT(dim0, dim1, dim2, dim3); } template Array* Array::create(idx_t dim0, idx_t dim1, idx_t dim2, idx_t dim3, idx_t dim4) { return new ArrayT(dim0, dim1, dim2, dim3, dim4); } template Array* Array::create(const ArrayShape& shape) { return new ArrayT(shape); } template Array* Array::create(const ArrayShape& shape, const ArrayLayout& layout) { return new ArrayT(shape, layout); } template Array* Array::wrap(Value* data, const ArrayShape& shape) { size_t size = 1; for (int i = 0; i < shape.size(); ++i) { size *= shape[i]; } return new ArrayT(new native::WrappedDataStore(data, size), shape); } template Array* Array::wrap(Value* data, const ArraySpec& spec) { return new ArrayT(new native::WrappedDataStore(data, spec), spec); } Array::~Array() = default; Array* Array::create(DataType datatype, const ArrayShape& shape) { switch (datatype.kind()) { case DataType::KIND_REAL64: return new ArrayT(shape); case DataType::KIND_REAL32: return new ArrayT(shape); case DataType::KIND_INT32: return new ArrayT(shape); case DataType::KIND_INT64: return new ArrayT(shape); case DataType::KIND_UINT32: return new ArrayT(shape); case DataType::KIND_UINT64: return new ArrayT(shape); default: { std::stringstream err; err << "data kind " << datatype.kind() << " not recognised."; throw_NotImplemented(err.str(), Here()); } } } Array* Array::create(DataType datatype, ArraySpec&& spec) { switch (datatype.kind()) { case DataType::KIND_REAL64: return new ArrayT(std::move(spec)); case DataType::KIND_REAL32: return new ArrayT(std::move(spec)); case DataType::KIND_INT32: return new ArrayT(std::move(spec)); case DataType::KIND_INT64: return new ArrayT(std::move(spec)); case DataType::KIND_UINT32: return new ArrayT(std::move(spec)); case DataType::KIND_UINT64: return new ArrayT(std::move(spec)); default: { std::stringstream err; err << "data kind " << datatype.kind() << " not recognised."; throw_NotImplemented(err.str(), Here()); } } } Array* Array::create(ArraySpec&& spec) { return create(spec.datatype(), std::move(spec)); } template ArrayT::ArrayT(ArrayDataStore* ds, const ArraySpec& spec) { data_store_ = std::unique_ptr(ds); spec_ = spec; } template ArrayT::ArrayT(idx_t dim0) { spec_ = ArraySpec(make_shape(dim0)); data_store_ = std::make_unique>(spec_.size()); } template ArrayT::ArrayT(idx_t dim0, idx_t dim1) { spec_ = ArraySpec(make_shape(dim0, dim1)); data_store_ = std::make_unique>(spec_.size()); } template ArrayT::ArrayT(idx_t dim0, idx_t dim1, idx_t dim2) { spec_ = ArraySpec(make_shape(dim0, dim1, dim2)); data_store_ = std::make_unique>(spec_.size()); } template ArrayT::ArrayT(idx_t dim0, idx_t dim1, idx_t dim2, idx_t dim3) { spec_ = ArraySpec(make_shape(dim0, dim1, dim2, dim3)); data_store_ = std::make_unique>(spec_.size()); } template ArrayT::ArrayT(idx_t dim0, idx_t dim1, idx_t dim2, idx_t dim3, idx_t dim4) { spec_ = ArraySpec(make_shape(dim0, dim1, dim2, dim3, dim4)); data_store_ = std::make_unique>(spec_.size()); } template ArrayT::ArrayT(const ArrayShape& shape) { ATLAS_ASSERT(shape.size() > 0); size_t size = 1; for (size_t j = 0; j < shape.size(); ++j) { size *= size_t(shape[j]); } data_store_ = std::make_unique>(size); spec_ = ArraySpec(shape); } template ArrayT::ArrayT(const ArrayShape& shape, const ArrayAlignment& alignment) { spec_ = ArraySpec(shape, alignment); data_store_ = std::make_unique>(spec_.allocatedSize()); } template ArrayT::ArrayT(const ArrayShape& shape, const ArrayLayout& layout) { spec_ = ArraySpec(shape); data_store_ = std::make_unique>(spec_.size()); for (size_t j = 0; j < layout.size(); ++j) { ATLAS_ASSERT(spec_.layout()[j] == layout[j]); } } template ArrayT::ArrayT(ArraySpec&& spec): Array(std::move(spec)) { data_store_ = std::make_unique>(spec_.allocatedSize()); } template void ArrayT::resize(const ArrayShape& _shape) { if (rank() != static_cast(_shape.size())) { std::stringstream msg; msg << "Cannot resize existing Array with rank " << rank() << " with a shape of rank " << _shape.size(); throw_Exception(msg.str(), Here()); } Array* resized = Array::create(_shape); array_initializer::apply(*this,*resized); replace(*resized); delete resized; } template void ArrayT::copy(const Array& other, const CopyPolicy&) { array_initializer::apply(other,*this); } template void ArrayT::insert(idx_t idx1, idx_t size1) { ArrayShape nshape = shape(); if (idx1 > nshape[0]) { throw_Exception("Cannot insert into an array at a position beyond its size", Here()); } nshape[0] += size1; Array* resized = Array::create(nshape); array_initializer_partitioned<0>::apply(*this, *resized, idx1, size1); replace(*resized); delete resized; } template void ArrayT::resize(idx_t size1) { resize(make_shape(size1)); } template void ArrayT::resize(idx_t size1, idx_t size2) { resize(make_shape(size1, size2)); } template void ArrayT::resize(idx_t size1, idx_t size2, idx_t size3) { resize(make_shape(size1, size2, size3)); } template void ArrayT::resize(idx_t size1, idx_t size2, idx_t size3, idx_t size4) { resize(make_shape(size1, size2, size3, size4)); } template void ArrayT::resize(idx_t size1, idx_t size2, idx_t size3, idx_t size4, idx_t size5) { resize(make_shape(size1, size2, size3, size4, size5)); } template void ArrayT::dump(std::ostream& out) const { switch (rank()) { case 1: make_host_view(*this).dump(out); break; case 2: make_host_view(*this).dump(out); break; case 3: make_host_view(*this).dump(out); break; case 4: make_host_view(*this).dump(out); break; case 5: make_host_view(*this).dump(out); break; case 6: make_host_view(*this).dump(out); break; case 7: make_host_view(*this).dump(out); break; case 8: make_host_view(*this).dump(out); break; case 9: make_host_view(*this).dump(out); break; default: ATLAS_NOTIMPLEMENTED; } } //------------------------------------------------------------------------------ template size_t ArrayT::footprint() const { size_t size = sizeof(*this); size += bytes(); if (not contiguous()) { ATLAS_NOTIMPLEMENTED; } return size; } template void ArrayT::accMap() const { data_store_->accMap(); } template void ArrayT::accUnmap() const { data_store_->accUnmap(); } template bool ArrayT::accMapped() const { return data_store_->accMapped(); } //------------------------------------------------------------------------------ template Array* Array::create(idx_t); template Array* Array::create(idx_t); template Array* Array::create(idx_t); template Array* Array::create(idx_t); template Array* Array::create(idx_t); template Array* Array::create(idx_t); template Array* Array::create(idx_t, idx_t); template Array* Array::create(idx_t, idx_t); template Array* Array::create(idx_t, idx_t); template Array* Array::create(idx_t, idx_t); template Array* Array::create(idx_t, idx_t); template Array* Array::create(idx_t, idx_t); template Array* Array::create(idx_t, idx_t, idx_t); template Array* Array::create(idx_t, idx_t, idx_t); template Array* Array::create(idx_t, idx_t, idx_t); template Array* Array::create(idx_t, idx_t, idx_t); template Array* Array::create(idx_t, idx_t, idx_t); template Array* Array::create(idx_t, idx_t, idx_t, idx_t); template Array* Array::create(idx_t, idx_t, idx_t, idx_t); template Array* Array::create(idx_t, idx_t, idx_t, idx_t); template Array* Array::create(idx_t, idx_t, idx_t, idx_t); template Array* Array::create(idx_t, idx_t, idx_t, idx_t); template Array* Array::create(idx_t, idx_t, idx_t, idx_t, idx_t); template Array* Array::create(idx_t, idx_t, idx_t, idx_t, idx_t); template Array* Array::create(idx_t, idx_t, idx_t, idx_t, idx_t); template Array* Array::create(idx_t, idx_t, idx_t, idx_t, idx_t); template Array* Array::create(idx_t, idx_t, idx_t, idx_t, idx_t); template Array* Array::create(const ArrayShape&); template Array* Array::create(const ArrayShape&); template Array* Array::create(const ArrayShape&); template Array* Array::create(const ArrayShape&); template Array* Array::create(const ArrayShape&); template Array* Array::create(const ArrayShape&); template Array* Array::create(const ArrayShape&, const ArrayLayout&); template Array* Array::create(const ArrayShape&, const ArrayLayout&); template Array* Array::create(const ArrayShape&, const ArrayLayout&); template Array* Array::create(const ArrayShape&, const ArrayLayout&); template Array* Array::create(const ArrayShape&, const ArrayLayout&); template Array* Array::create(const ArrayShape&, const ArrayLayout&); template Array* Array::wrap(int*, const ArrayShape&); template Array* Array::wrap(long*, const ArrayShape&); template Array* Array::wrap(float*, const ArrayShape&); template Array* Array::wrap(double*, const ArrayShape&); template Array* Array::wrap(long unsigned*, const ArrayShape&); template Array* Array::wrap(unsigned int*, const ArrayShape&); template Array* Array::wrap(int*, const ArraySpec&); template Array* Array::wrap(long*, const ArraySpec&); template Array* Array::wrap(float*, const ArraySpec&); template Array* Array::wrap(double*, const ArraySpec&); template Array* Array::wrap(long unsigned*, const ArraySpec&); template Array* Array::wrap(unsigned int*, const ArraySpec&); template class ArrayT; template class ArrayT; template class ArrayT; template class ArrayT; template class ArrayT; template class ArrayT; } // namespace array } // namespace atlas