/* * (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 "pluto/pluto.h" #include "atlas/array.h" #include "atlas/array/ArrayView.h" #include "atlas/array/IndexView.h" #include "atlas/library/config.h" #include "atlas/runtime/Exception.h" namespace atlas { namespace array { namespace { template inline static void check_metadata(const Array& array) { if (array.rank() != Rank) { std::stringstream err; err << "Number of dimensions do not match: template argument " << Rank << " expected to be " << array.rank(); throw_Exception(err.str(), Here()); } if (array.datatype() != array::DataType::create()) { std::stringstream err; err << "Data Type does not match: template argument expected to be " << array.datatype().str(); throw_Exception(err.str(), Here()); } } } // namespace //------------------------------------------------------------------------------ template ArrayView make_host_view(Array& array) { return ArrayView(array.host_data(), array.shape(), array.strides()); } template ArrayView make_host_view(const Array& array) { return ArrayView(array.host_data(), array.shape(), array.strides()); } template ArrayView make_device_view(Array& array) { #if ATLAS_HAVE_GPU if (pluto::devices()) { ATLAS_ASSERT(array.deviceAllocated() || pluto::devices(), "make_device_view: Array not allocated on device"); } return ArrayView((array.device_data()), array.shape(), array.device_strides()); #else return make_host_view(array); #endif } template ArrayView make_device_view(const Array& array) { #if ATLAS_HAVE_GPU if (pluto::devices()) { ATLAS_ASSERT(array.deviceAllocated(), "make_device_view: Array not allocated on device"); } return ArrayView(array.device_data(), array.shape(), array.device_strides()); #else return make_host_view(array); #endif } template IndexView make_host_indexview(Array& array) { return IndexView((Value*)(array.storage()), array.shape().data()); } template IndexView make_host_indexview(const Array& array) { return IndexView((const Value*)(array.storage()), array.shape().data()); } template IndexView make_indexview(Array& array) { check_metadata(array); return make_host_indexview(array); } template IndexView make_indexview(const Array& array) { check_metadata(array); return make_host_indexview(array); } template ArrayView make_view(Array& array) { check_metadata(array); return make_host_view(array); } template ArrayView make_view(const Array& array) { check_metadata(array); return make_host_view(array); } // -------------------------------------------------------------------------------------------- } // namespace array } // namespace atlas //----------------------------------------------------------------------- // Explicit instantiation namespace atlas { namespace array { #define EXPLICIT_TEMPLATE_INSTANTIATION_TYPE_RANK(TYPE, RANK) \ template ArrayView make_view(Array&); \ template ArrayView make_view(Array&); \ template ArrayView make_view(const Array&); \ template ArrayView make_view(const Array&); \ \ template ArrayView make_host_view(Array&); \ template ArrayView make_host_view(Array&); \ template ArrayView make_host_view(const Array&); \ template ArrayView make_host_view(const Array&); \ \ template ArrayView make_device_view(Array&); \ template ArrayView make_device_view(Array&); \ template ArrayView make_device_view(const Array&); \ template ArrayView make_device_view(const Array&); #define EXPLICIT_TEMPLATE_INSTATIATION(RANK) \ EXPLICIT_TEMPLATE_INSTANTIATION_TYPE_RANK(int, RANK) \ EXPLICIT_TEMPLATE_INSTANTIATION_TYPE_RANK(long, RANK) \ EXPLICIT_TEMPLATE_INSTANTIATION_TYPE_RANK(float, RANK) \ EXPLICIT_TEMPLATE_INSTANTIATION_TYPE_RANK(double, RANK) \ EXPLICIT_TEMPLATE_INSTANTIATION_TYPE_RANK(unsigned int, RANK) \ EXPLICIT_TEMPLATE_INSTANTIATION_TYPE_RANK(unsigned long, RANK) EXPLICIT_TEMPLATE_INSTATIATION(1) EXPLICIT_TEMPLATE_INSTATIATION(2) EXPLICIT_TEMPLATE_INSTATIATION(3) EXPLICIT_TEMPLATE_INSTATIATION(4) EXPLICIT_TEMPLATE_INSTATIATION(5) EXPLICIT_TEMPLATE_INSTATIATION(6) EXPLICIT_TEMPLATE_INSTATIATION(7) EXPLICIT_TEMPLATE_INSTATIATION(8) EXPLICIT_TEMPLATE_INSTATIATION(9) #undef EXPLICIT_TEMPLATE_INSTATIATION_TYPE_RANK #undef EXPLICIT_TEMPLATE_INSTATIATION #define EXPLICIT_TEMPLATE_INSTANTIATION_INDEXVIEW_TYPE_RANK(TYPE, RANK) \ template IndexView make_indexview(Array&); \ template IndexView make_indexview(Array&); \ template IndexView make_indexview(const Array&); \ template IndexView make_indexview(const Array&); #define EXPLICIT_TEMPLATE_INSTANTIATION_INDEXVIEW(RANK) \ EXPLICIT_TEMPLATE_INSTANTIATION_INDEXVIEW_TYPE_RANK(int, RANK) \ EXPLICIT_TEMPLATE_INSTANTIATION_INDEXVIEW_TYPE_RANK(long, RANK) EXPLICIT_TEMPLATE_INSTANTIATION_INDEXVIEW(1) // EXPLICIT_TEMPLATE_INSTANTIATION_INDEXVIEW(2) #undef EXPLICIT_TEMPLATE_INSTANTIATION_INDEXVIEW #undef EXPLICIT_TEMPLATE_INSTANTIATION_INDEXVIEW } // namespace array } // namespace atlas