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_HPP
46 #define KOKKOS_SYCL_HPP
47 
48 #include <Kokkos_Macros.hpp>
49 
50 #ifdef KOKKOS_ENABLE_SYCL
51 #include <CL/sycl.hpp>
52 #include <Kokkos_SYCL_Space.hpp>
53 #include <Kokkos_Layout.hpp>
54 #include <Kokkos_ScratchSpace.hpp>
55 #include <impl/Kokkos_ExecSpaceInitializer.hpp>
56 #include <impl/Kokkos_Profiling_Interface.hpp>
57 #include <impl/Kokkos_HostSharedPtr.hpp>
58 
59 namespace Kokkos {
60 namespace Experimental {
61 namespace Impl {
62 class SYCLInternal;
63 }
64 
65 /// \class SYCL
66 /// \brief Kokkos device for multicore processors in the host memory space.
67 class SYCL {
68  public:
69   //------------------------------------
70   //! \name Type declarations that all Kokkos devices must provide.
71   //@{
72 
73   //! Tag this class as a kokkos execution space
74   using execution_space = SYCL;
75   using memory_space    = SYCLDeviceUSMSpace;
76   using device_type     = Kokkos::Device<execution_space, memory_space>;
77 
78   using array_layout = LayoutLeft;
79   using size_type    = memory_space::size_type;
80 
81   using scratch_memory_space = ScratchMemorySpace<SYCL>;
82 
83   SYCL();
84   explicit SYCL(const sycl::queue&);
85 
impl_instance_id() const86   uint32_t impl_instance_id() const noexcept { return 0; }
87 
sycl_context() const88   sycl::context sycl_context() const noexcept {
89     return m_space_instance->m_queue->get_context();
90   };
91 
92   //@}
93   //------------------------------------
94   //! \name Functions that all Kokkos devices must implement.
95   //@{
96 
in_parallel()97   KOKKOS_INLINE_FUNCTION static int in_parallel() {
98 #if defined(__SYCL_DEVICE_ONLY__)
99     return true;
100 #else
101     return false;
102 #endif
103   }
104 
105   /** \brief  Set the device in a "sleep" state. */
106   static bool sleep();
107 
108   /** \brief Wake the device from the 'sleep' state. A noop for OpenMP. */
109   static bool wake();
110 
111   /** \brief Wait until all dispatched functors complete. A noop for OpenMP. */
112   static void impl_static_fence();
113   void fence() const;
114 
115   /// \brief Print configuration information to the given output stream.
116   void print_configuration(std::ostream&, const bool detail = false);
117 
118   /// \brief Free any resources being consumed by the device.
119   static void impl_finalize();
120 
121   /** \brief  Initialize the device.
122    *
123    */
124 
125   struct SYCLDevice {
SYCLDeviceKokkos::Experimental::SYCL::SYCLDevice126     SYCLDevice() : SYCLDevice(sycl::default_selector()) {}
127     explicit SYCLDevice(sycl::device d);
128     explicit SYCLDevice(const sycl::device_selector& selector);
129     explicit SYCLDevice(size_t id);
130 
131     sycl::device get_device() const;
132 
operator <<(std::ostream & os,const SYCLDevice & that)133     friend std::ostream& operator<<(std::ostream& os, const SYCLDevice& that) {
134       return SYCL::impl_sycl_info(os, that.m_device);
135     }
136 
137    private:
138     sycl::device m_device;
139   };
140 
141   static void impl_initialize(SYCLDevice = SYCLDevice());
142 
143   int sycl_device() const;
144 
145   static bool impl_is_initialized();
146 
147   static int concurrency();
148   static const char* name();
149 
impl_internal_space_instance() const150   inline Impl::SYCLInternal* impl_internal_space_instance() const {
151     return m_space_instance.get();
152   }
153 
154  private:
155   static std::ostream& impl_sycl_info(std::ostream& os,
156                                       const sycl::device& device);
157 
158   Kokkos::Impl::HostSharedPtr<Impl::SYCLInternal> m_space_instance;
159 };
160 
161 namespace Impl {
162 
163 class SYCLSpaceInitializer : public Kokkos::Impl::ExecSpaceInitializerBase {
164  public:
165   void initialize(const InitArguments& args) final;
166   void finalize(const bool) final;
167   void fence() final;
168   void print_configuration(std::ostream& msg, const bool detail) final;
169 };
170 
171 }  // namespace Impl
172 }  // namespace Experimental
173 
174 namespace Tools {
175 namespace Experimental {
176 template <>
177 struct DeviceTypeTraits<Kokkos::Experimental::SYCL> {
178   /// \brief An ID to differentiate (for example) Serial from OpenMP in Tooling
179   static constexpr DeviceType id = DeviceType::SYCL;
180 };
181 }  // namespace Experimental
182 }  // namespace Tools
183 
184 }  // namespace Kokkos
185 
186 #endif
187 #endif
188