/* * (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 "atlas/array/gridtools/GridToolsMakeView.h" #include #include #include "atlas/array/Array.h" #include "atlas/array/ArrayView.h" #include "atlas/array/ArrayViewDefs.h" #include "atlas/array/IndexView.h" #include "atlas/array/gridtools/GridToolsTraits.h" #include "atlas/library/config.h" #include "atlas/runtime/Exception.h" //------------------------------------------------------------------------------ namespace atlas { namespace array { namespace { template 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 namespace gridtools { template typename gt_view::type make_gt_host_view(const Array& array) { using value_t = typename std::remove_const::type; using storage_info_ty = storage_traits::storage_info_t<0, Rank>; using data_store_t = storage_traits::data_store_t; data_store_t* ds = reinterpret_cast(const_cast(array.storage())); return ::gridtools::make_host_view()>(*ds); } template typename gt_view::type make_gt_device_view(const Array& array) { using value_t = typename std::remove_const::type; using storage_info_ty = storage_traits::storage_info_t<0, Rank>; using data_store_t = storage_traits::data_store_t; data_store_t* ds = reinterpret_cast(const_cast(array.storage())); #if ATLAS_GRIDTOOLS_STORAGE_BACKEND_CUDA return ::gridtools::make_device_view()>(*ds); #else return ::gridtools::make_host_view()>(*ds); #endif } } // namespace gridtools constexpr bool host_view = false; constexpr bool device_view = true; template ArrayView make_host_view(Array& array) { check_metadata(array); return ArrayView(array, host_view); } template ArrayView make_host_view(const Array& array) { check_metadata(array); return ArrayView(array, host_view); } template ArrayView make_device_view(Array& array) { check_metadata(array); return ArrayView(array, device_view); } template ArrayView make_device_view(const Array& array) { check_metadata(array); return ArrayView(array, device_view); } template ArrayView make_view(Array& array) { return make_host_view(array); } template ArrayView make_view(const Array& array) { return make_host_view(array); } //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 IndexView make_host_indexview(Array& array) { // using value_t = typename std::remove_const::type; // using storage_info_ty = gridtools::storage_traits::storage_info_t<0, Rank>; // using data_store_t = gridtools::storage_traits::data_store_t; // data_store_t* ds = reinterpret_cast(const_cast(array.storage())); // return IndexView(::gridtools::make_host_view()>(*ds)); check_metadata(array); constexpr bool device_view = false; return IndexView(array, device_view); } template IndexView make_host_indexview(const Array& array) { // using value_t = typename std::remove_const::type; // using storage_info_ty = gridtools::storage_traits::storage_info_t<0, Rank>; // using data_store_t = gridtools::storage_traits::data_store_t; // data_store_t* ds = reinterpret_cast(const_cast(array.storage())); // return IndexView(::gridtools::make_host_view()>(*ds)); check_metadata(array); constexpr bool device_view = false; return IndexView(array, device_view); } // -------------------------------------------------------------------------------------------- template IndexView make_indexview(Array& array) { // check_metadata(array); // constexpr bool device_view = false; // return IndexView(array, device_view); return make_host_indexview(array); } template IndexView make_indexview(const Array& array) { return make_host_indexview(array); } } // namespace array } // namespace atlas //----------------------------------------------------------------------- // Explicit instantiation namespace atlas { namespace array { //#define EXPLICIT_TEMPLATE_INSTANTIATION( RANK ) \ // namespace gridtools { \ // template typename gt_view::type \ // make_gt_host_view( const Array& array ); \ // template typename gt_view::type \ // make_gt_host_view( const Array& array ); \ // template typename gt_view::type make_gt_host_view( \ // const Array& array ); \ // template typename gt_view::type make_gt_host_view( \ // const Array& array ); \ // template typename gt_view::type make_gt_host_view( \ // const Array& array ); \ // template typename gt_view::type \ // make_gt_host_view( const Array& array ); \ // \ // template typename gt_view::type make_gt_device_view( \ // const Array& array ); \ // template typename gt_view::type make_gt_device_view( \ // const Array& array ); \ // template typename gt_view::type make_gt_device_view( \ // const Array& array ); \ // template typename gt_view::type make_gt_device_view( \ // const Array& array ); \ // template typename gt_view::type \ // make_gt_device_view( const Array& array ); \ // template typename gt_view::type \ // make_gt_device_view( const Array& array ); \ // template typename gt_view::type make_gt_device_view( \ // const Array& array ); \ // template typename gt_view::type \ // make_gt_device_view( const Array& array ); \ // template typename gt_view::type \ // make_gt_device_view( const Array& array ); \ // template typename gt_view::type \ // make_gt_device_view( const Array& array ); \ // } //// For each Rank in [1..9] //EXPLICIT_TEMPLATE_INSTANTIATION( 1 ) //EXPLICIT_TEMPLATE_INSTANTIATION( 2 ) //EXPLICIT_TEMPLATE_INSTANTIATION( 3 ) //EXPLICIT_TEMPLATE_INSTANTIATION( 4 ) //EXPLICIT_TEMPLATE_INSTANTIATION( 5 ) //EXPLICIT_TEMPLATE_INSTANTIATION( 6 ) //EXPLICIT_TEMPLATE_INSTANTIATION( 7 ) //EXPLICIT_TEMPLATE_INSTANTIATION( 8 ) //EXPLICIT_TEMPLATE_INSTANTIATION( 9 ) //#undef EXPLICIT_TEMPLATE_INSTANTIATION #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&); \ \ namespace gridtools { \ template typename gt_view::type make_gt_host_view(const Array& array); \ template typename gt_view::type make_gt_host_view(const Array& array); \ template typename gt_view::type make_gt_device_view(const Array& array); \ template typename gt_view::type make_gt_device_view(const Array& 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 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_host_indexview(Array&); \ template IndexView make_host_indexview(Array&); \ template IndexView make_host_indexview(const Array&); \ template IndexView make_host_indexview(const Array&); \ \ 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_TYPE_RANK } // namespace array } // namespace atlas