/* * (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. */ #pragma once #include #include #include #include #include "atlas/array.h" #include "atlas/array/ArrayDataStore.h" #include "atlas/array/DataType.h" #include "atlas/array/gridtools/GridToolsTraits.h" #include "atlas/array_fwd.h" #include "atlas/library/config.h" #include "atlas/runtime/Exception.h" #include "atlas/runtime/Log.h" //------------------------------------------------------------------------------ namespace atlas { namespace array { namespace gridtools { template std::array get_array_from_vector(std::vector const& values) { std::array array; std::copy(values.begin(), values.end(), array.begin()); return array; } template struct check_dimension_lengths_impl { template static void apply(ArrayShape const& shape, FirstDim first_dim, Dims... d) { if (first_dim < shape[Dim]) { std::stringstream err; err << "Attempt to resize array with original size for dimension " << Dim << " of " << shape[Dim] << " by " << first_dim << std::endl; throw_Exception(err.str(), Here()); } check_dimension_lengths_impl::apply(shape, d...); } }; template struct check_dimension_lengths_impl 1)>::type> { template static void apply(ArrayShape const& shape, FirstDim first_dim) { if (first_dim < shape[Dim]) { std::stringstream err; err << "Attempt to resize array with original size for dimension " << Dim - 1 << " of " << shape[Dim - 1] << " by " << first_dim << std::endl; throw_Exception(err.str(), Here()); } } }; template struct check_dimension_lengths_impl::type> { template static void apply(ArrayShape const& shape, FirstDim first_dim) { if (first_dim < shape[Dim]) { std::stringstream err; err << "Attempt to resize array with original size for dimension " << Dim << " of " << shape[Dim] << " by " << first_dim << std::endl; throw_Exception(err.str(), Here()); } } }; template void check_dimension_lengths(ArrayShape const& shape, Dims... d) { check_dimension_lengths_impl::apply(shape, d...); } template struct default_layout_t { template struct get_layout; template struct get_layout> { using type = ::gridtools::layout_map; }; using type = typename get_layout>::type; }; template struct get_layout_map_component { // TODO: static_assert( ::gridtools::is_layout_map(), "Error: not a // layout_map" ); template struct get_component { ATLAS_HOST_DEVICE constexpr get_component() {} ATLAS_HOST_DEVICE constexpr static Value apply() { return LayoutMap::template at(); } }; }; template struct get_stride_component { template struct get_component { ATLAS_HOST_DEVICE constexpr get_component() {} template ATLAS_HOST_DEVICE constexpr static Value apply(StorageInfoPtr a) { static_assert((::gridtools::is_storage_info::type>::value), "Error: not a storage_info"); return a->template stride(); } }; }; template struct get_shape_component { template struct get_component { ATLAS_HOST_DEVICE constexpr get_component() {} template ATLAS_HOST_DEVICE constexpr static Value apply(StorageInfoPtr a) { static_assert((::gridtools::is_storage_info::type>::value), "Error: not a storage_info"); return a->template total_length(); } }; }; // indirection around C++11 sizeof... since it is buggy for nvcc and cray template struct get_pack_size { using type = ::gridtools::static_uint; }; template struct gt_storage_t { using type = gridtools::storage_traits::data_store_t, Alignment>>; }; template typename gt_storage_t::type::value>::type* create_gt_storage( UInts... dims) { static_assert((sizeof...(dims) > 0), "Error: can not create storages without any dimension"); constexpr static unsigned int rank = get_pack_size::type::value; typedef gridtools::storage_traits::custom_layout_storage_info_align_t<0, LayoutMap, ::gridtools::zero_halo, Alignment> storage_info_ty; typedef gridtools::storage_traits::data_store_t data_store_t; data_store_t* ds; if (::gridtools::accumulate(::gridtools::multiplies(), dims...) == 0) { ds = new data_store_t(); } else { storage_info_ty si(dims...); ds = new data_store_t(si); } return ds; } template typename gt_storage_t, get_pack_size::type::value>::type* create_gt_storage(UInts... dims) { return create_gt_storage>(dims...); } template struct gt_wrap_storage_t { using type = gridtools::storage_traits::data_store_t>; }; template static typename gt_wrap_storage_t::type* wrap_gt_storage(Value* data, std::array&& shape, std::array&& strides) { static_assert((Rank > 0), "Error: can not create storages without any dimension"); typedef gridtools::storage_traits::storage_info_t<0, Rank, ::gridtools::zero_halo> storage_info_ty; typedef gridtools::storage_traits::data_store_t data_store_t; ::gridtools::array<::gridtools::uint_t, Rank> _shape; ::gridtools::array<::gridtools::uint_t, Rank> _strides; for (unsigned int i = 0; i < Rank; ++i) { _shape[i] = shape[i]; _strides[i] = strides[i]; } storage_info_ty si(_shape, _strides); data_store_t* ds = new data_store_t(si, data); return ds; } constexpr idx_t zero(idx_t) { return 0; } template ArrayStrides make_null_strides(std::integer_sequence) { return make_strides(zero(Is)...); } template struct my_apply_gt_integer_sequence { template class Lambda, typename... ExtraTypes> ATLAS_HOST_DEVICE static constexpr Container apply(ExtraTypes const&... args_) { static_assert((std::is_same::value), "ERROR: my_apply_gt_integer_sequence only accepts a " "std::integer_sequence type. Check the call"); return Container(args_...); } }; template struct my_apply_gt_integer_sequence> { /** @brief duplicated interface for the case in which the container is an aggregator */ template class Lambda, typename... ExtraTypes> ATLAS_HOST_DEVICE static constexpr Container apply(ExtraTypes const&... args_) { return Container{Lambda::apply(args_...)...}; } }; #ifndef __CUDACC__ template ArraySpec ATLAS_HOST make_spec(DataStore* gt_data_store_ptr, Dims... dims) { static_assert((::gridtools::is_data_store::value), "Internal Error: passing a non GT data store"); if (gt_data_store_ptr->valid()) { auto storage_info_ptr = gt_data_store_ptr->get_storage_info_ptr().get(); using Layout = typename DataStore::storage_info_t::layout_t; using Alignment = typename DataStore::storage_info_t::alignment_t; using seq = my_apply_gt_integer_sequence>; ArraySpec spec( ArrayShape{(idx_t)dims...}, seq::template apply::template get_component>(storage_info_ptr), seq::template apply::template get_component>(), ArrayAlignment(Alignment::value)); ATLAS_ASSERT(spec.allocatedSize() == storage_info_ptr->padded_total_length()); return spec; } else { return ArraySpec(make_shape({dims...}), make_null_strides(std::make_integer_sequence())); } } #endif //------------------------------------------------------------------------------ } // namespace gridtools } // namespace array } // namespace atlas