1 /* 2 //@HEADER 3 // ************************************************************************ 4 // 5 // Kokkos v. 3.0 6 // Copyright (2020) National Technology & Engineering 7 // Solutions of Sandia, LLC (NTESS). 8 // 9 // Under the terms of Contract DE-NA0003525 with NTESS, 10 // the U.S. Government retains certain rights in this software. 11 // 12 // Redistribution and use in source and binary forms, with or without 13 // modification, are permitted provided that the following conditions are 14 // met: 15 // 16 // 1. Redistributions of source code must retain the above copyright 17 // notice, this list of conditions and the following disclaimer. 18 // 19 // 2. Redistributions in binary form must reproduce the above copyright 20 // notice, this list of conditions and the following disclaimer in the 21 // documentation and/or other materials provided with the distribution. 22 // 23 // 3. Neither the name of the Corporation nor the names of the 24 // contributors may be used to endorse or promote products derived from 25 // this software without specific prior written permission. 26 // 27 // THIS SOFTWARE IS PROVIDED BY NTESS "AS IS" AND ANY 28 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 29 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 30 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL NTESS OR THE 31 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 32 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 33 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 34 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 35 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 36 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 37 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 38 // 39 // Questions? Contact Christian R. Trott (crtrott@sandia.gov) 40 // 41 // ************************************************************************ 42 //@HEADER 43 */ 44 45 #ifndef KOKKOS_SYCL_UNIQUE_TOKEN_HPP 46 #define KOKKOS_SYCL_UNIQUE_TOKEN_HPP 47 48 #include <impl/Kokkos_ConcurrentBitset.hpp> 49 #include <Kokkos_SYCL_Space.hpp> 50 #include <Kokkos_UniqueToken.hpp> 51 52 namespace Kokkos { 53 namespace Experimental { 54 55 // both global and instance Unique Tokens are implemented in the same way 56 template <> 57 class UniqueToken<SYCL, UniqueTokenScope::Global> { 58 protected: 59 uint32_t volatile* m_buffer; 60 uint32_t m_count; 61 62 public: 63 using execution_space = SYCL; 64 using size_type = int32_t; 65 UniqueToken(execution_space const &=execution_space ())66 explicit UniqueToken(execution_space const& = execution_space()) 67 : m_buffer(Impl::SYCLInternal::singleton().m_scratchConcurrentBitset), 68 m_count(SYCL::concurrency()) {} 69 70 KOKKOS_DEFAULTED_FUNCTION 71 UniqueToken(const UniqueToken&) = default; 72 73 KOKKOS_DEFAULTED_FUNCTION 74 UniqueToken(UniqueToken&&) = default; 75 76 KOKKOS_DEFAULTED_FUNCTION 77 UniqueToken& operator=(const UniqueToken&) = default; 78 79 KOKKOS_DEFAULTED_FUNCTION 80 UniqueToken& operator=(UniqueToken&&) = default; 81 82 /// \brief upper bound for acquired values, i.e. 0 <= value < size() 83 KOKKOS_INLINE_FUNCTION size() const84 size_type size() const noexcept { return m_count; } 85 86 /// \brief acquire value such that 0 <= value < size() 87 KOKKOS_INLINE_FUNCTION acquire() const88 size_type acquire() const { 89 const Kokkos::pair<int, int> result = 90 Kokkos::Impl::concurrent_bitset::acquire_bounded( 91 m_buffer, m_count 92 #if defined(KOKKOS_ARCH_INTEL_GEN) 93 , 94 Kokkos::Impl::clock_tic() % m_count 95 #endif 96 ); 97 98 if (result.first < 0) { 99 Kokkos::abort( 100 "UniqueToken<SYCL> failure to acquire tokens, no tokens available"); 101 } 102 103 return result.first; 104 } 105 106 /// \brief release an acquired value 107 KOKKOS_INLINE_FUNCTION release(size_type i) const108 void release(size_type i) const noexcept { 109 Kokkos::Impl::concurrent_bitset::release(m_buffer, i); 110 } 111 }; 112 113 template <> 114 class UniqueToken<SYCL, UniqueTokenScope::Instance> 115 : public UniqueToken<SYCL, UniqueTokenScope::Global> { 116 View<uint32_t*, SYCLDeviceUSMSpace> m_buffer_view; 117 118 public: UniqueToken(execution_space const & arg=execution_space ())119 explicit UniqueToken(execution_space const& arg = execution_space()) 120 : UniqueToken<SYCL, UniqueTokenScope::Global>(arg) {} 121 UniqueToken(size_type max_size,execution_space const &=execution_space ())122 UniqueToken(size_type max_size, execution_space const& = execution_space()) 123 : m_buffer_view( 124 "UniqueToken::m_buffer_view", 125 ::Kokkos::Impl::concurrent_bitset::buffer_bound(max_size)) { 126 m_buffer = m_buffer_view.data(); 127 m_count = max_size; 128 } 129 }; 130 131 } // namespace Experimental 132 } // namespace Kokkos 133 134 #endif 135