X-Git-Url: http://www.fmaj7b5.info/git?p=cuda.git;a=blobdiff_plain;f=libutils%2FMDView.cuh;fp=libutils%2FMDView.cuh;h=4035de4916e7e37807400618ef9de445cb6ebd9a;hp=0000000000000000000000000000000000000000;hb=869779783d0c06c34e02062ac7d23dac316e73a9;hpb=7b77a912a4a1202f677ae9dbff672758e2b945e4 diff --git a/libutils/MDView.cuh b/libutils/MDView.cuh new file mode 100644 index 0000000..4035de4 --- /dev/null +++ b/libutils/MDView.cuh @@ -0,0 +1,110 @@ +/* + Copyright (C) 2012 fmaj7b5.info + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ + +#ifndef FM7b5_MDVIEW_CUH +#define FM7b5_MDVIEW_CUH + +#include + +namespace FM7b5 +{ + namespace detail + { + //! copy const qualifier of From to To + template struct copy_const { typedef To type; }; + template struct copy_const { typedef typename std::add_const::type type; }; + + //! array subscript access for multi dimentional data + template + class MDView + { + public: + typedef T value_type; + typedef typename copy_const::type* byte_pointer_type; + typedef typename copy_const::type* void_pointer_type; + + protected: + //! temporal class for resolving the operator[] + template + class ref + { + byte_pointer_type const m_p; + const size_t* const m_extent; + + public: + typedef ref result_type; + + __device__ + ref(void_pointer_type p, const size_t* extent) + : m_p(static_cast(p)), m_extent(extent) + {} + + __device__ + result_type operator[](size_t i) const + { + return result_type(m_p + *m_extent * i, m_extent + 1); + } + }; + + template + class ref + { + byte_pointer_type const m_p; + const size_t* const m_extent; + + public: + typedef typename std::add_reference::type result_type; + + __device__ + ref(void_pointer_type p, const size_t* extent) + : m_p(static_cast(p)), m_extent(extent) + {} + + __device__ + result_type operator[](size_t i) const + { + return *reinterpret_cast(m_p + *m_extent * i); + } + }; + + byte_pointer_type m_data; + size_t m_extent[D]; + + public: + MDView() {} + + MDView(void_pointer_type p, const size_t extent[D]) + : m_data(static_cast(p)) + { + for (size_t i = 0; i < D; ++i) { + m_extent[i] = extent[i]; + } + } + + __device__ + typename ref::result_type operator[](size_t i) const + { + ref r(m_data, m_extent); + return r[i]; + } + }; + } + + using detail::MDView; +} + +#endif /* FM7b5_MDVIEW_CUH */