|
| 1 | +/* |
| 2 | + * GridTools |
| 3 | + * |
| 4 | + * Copyright (c) 2014-2023, ETH Zurich |
| 5 | + * All rights reserved. |
| 6 | + * |
| 7 | + * Please, refer to the LICENSE file in the root directory. |
| 8 | + * SPDX-License-Identifier: BSD-3-Clause |
| 9 | + */ |
| 10 | +#pragma once |
| 11 | + |
| 12 | +#include <cstddef> |
| 13 | +#include <type_traits> |
| 14 | +#include <utility> |
| 15 | + |
| 16 | +#include "defs.hpp" |
| 17 | +#include "host_device.hpp" |
| 18 | + |
| 19 | +#ifdef GT_CUDACC |
| 20 | +#include "cuda_type_traits.hpp" |
| 21 | +#endif |
| 22 | + |
| 23 | +namespace gridtools { |
| 24 | + |
| 25 | +#ifdef GT_CUDACC |
| 26 | + namespace impl_ { |
| 27 | + |
| 28 | + template <class T> |
| 29 | + struct ldg_ptr { |
| 30 | + T const *m_ptr; |
| 31 | + |
| 32 | + static_assert(is_texture_type<T>::value); |
| 33 | + |
| 34 | + GT_FUNCTION constexpr T operator*() const { |
| 35 | +#ifdef GT_CUDA_ARCH |
| 36 | + return __ldg(m_ptr); |
| 37 | +#else |
| 38 | + return *m_ptr; |
| 39 | +#endif |
| 40 | + } |
| 41 | + |
| 42 | + GT_FUNCTION constexpr ldg_ptr &operator+=(std::ptrdiff_t diff) { |
| 43 | + m_ptr += diff; |
| 44 | + return *this; |
| 45 | + } |
| 46 | + |
| 47 | + GT_FUNCTION constexpr ldg_ptr &operator-=(std::ptrdiff_t diff) { |
| 48 | + m_ptr -= diff; |
| 49 | + return *this; |
| 50 | + } |
| 51 | + |
| 52 | + friend GT_FUNCTION constexpr bool operator==(ldg_ptr const &a, ldg_ptr const &b) { |
| 53 | + return a.m_ptr == b.m_ptr; |
| 54 | + } |
| 55 | + friend GT_FUNCTION constexpr bool operator==(ldg_ptr const &a, T const *b) { return a.m_ptr == b; } |
| 56 | + friend GT_FUNCTION constexpr bool operator==(T const *a, ldg_ptr const &b) { return a == b.m_ptr; } |
| 57 | + |
| 58 | + friend GT_FUNCTION constexpr bool operator!=(ldg_ptr const &a, ldg_ptr const &b) { |
| 59 | + return a.m_ptr != b.m_ptr; |
| 60 | + } |
| 61 | + friend GT_FUNCTION constexpr bool operator!=(ldg_ptr const &a, T const *b) { return a.m_ptr != b; } |
| 62 | + friend GT_FUNCTION constexpr bool operator!=(T const *a, ldg_ptr const &b) { return a != b.m_ptr; } |
| 63 | + |
| 64 | + friend GT_FUNCTION constexpr ldg_ptr &operator++(ldg_ptr &ptr) { |
| 65 | + ++ptr.m_ptr; |
| 66 | + return ptr; |
| 67 | + } |
| 68 | + |
| 69 | + friend GT_FUNCTION constexpr ldg_ptr &operator--(ldg_ptr &ptr) { |
| 70 | + --ptr.m_ptr; |
| 71 | + return ptr; |
| 72 | + } |
| 73 | + |
| 74 | + friend GT_FUNCTION constexpr ldg_ptr operator++(ldg_ptr &ptr, int) { |
| 75 | + ldg_ptr p = ptr; |
| 76 | + ++ptr.m_ptr; |
| 77 | + return p; |
| 78 | + } |
| 79 | + |
| 80 | + friend GT_FUNCTION constexpr ldg_ptr operator--(ldg_ptr &ptr, int) { |
| 81 | + ldg_ptr p = ptr; |
| 82 | + --ptr.m_ptr; |
| 83 | + return p; |
| 84 | + } |
| 85 | + |
| 86 | + friend GT_FUNCTION constexpr ldg_ptr operator+(ldg_ptr const &ptr, std::ptrdiff_t diff) { |
| 87 | + return {ptr.m_ptr + diff}; |
| 88 | + } |
| 89 | + |
| 90 | + friend GT_FUNCTION constexpr ldg_ptr operator-(ldg_ptr const &ptr, std::ptrdiff_t diff) { |
| 91 | + return {ptr.m_ptr - diff}; |
| 92 | + } |
| 93 | + |
| 94 | + friend GT_FUNCTION constexpr std::ptrdiff_t operator-(ldg_ptr const &ptr, ldg_ptr const &other) { |
| 95 | + return ptr.m_ptr - other.m_ptr; |
| 96 | + } |
| 97 | + }; |
| 98 | + } // namespace impl_ |
| 99 | + |
| 100 | + template <class T> |
| 101 | + GT_FUNCTION constexpr std::enable_if_t<is_texture_type<T>::value, impl_::ldg_ptr<T>> as_ldg_ptr(T const *ptr) { |
| 102 | + return {ptr}; |
| 103 | + } |
| 104 | + |
| 105 | +#endif |
| 106 | + |
| 107 | + template <class T> |
| 108 | + GT_FUNCTION constexpr T &&as_ldg_ptr(T &&value) { |
| 109 | + return std::forward<T>(value); |
| 110 | + } |
| 111 | + |
| 112 | +} // namespace gridtools |
0 commit comments