DPC++ Runtime
Runtime libraries for oneAPI Data Parallel C++
program.hpp
Go to the documentation of this file.
1 //==--------------- program.hpp --- SYCL program ---------------------------==//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #pragma once
10 
11 #include <CL/sycl/context.hpp>
16 #include <CL/sycl/kernel.hpp>
18 #include <CL/sycl/stl.hpp>
20 
22 namespace sycl {
23 
24 // Forward declarations
25 class context;
26 class device;
27 namespace detail {
28 class program_impl;
29 }
30 
31 enum class program_state { none = 0, compiled = 1, linked = 2 };
32 
39 class __SYCL_EXPORT __SYCL2020_DEPRECATED(
40  "program class is deprecated, use kernel_bundle instead") program {
41 public:
42  program() = delete;
43 
52  explicit program(const context &Context, const property_list &PropList = {});
53 
63  program(const context &Context, std::vector<device> DeviceList,
64  const property_list &PropList = {});
65 
78  program(std::vector<program> ProgramList, const property_list &PropList = {});
79 
93  program(std::vector<program> ProgramList, std::string LinkOptions,
94  const property_list &PropList = {});
95 
107  program(const context &Context, cl_program ClProgram);
108 
109  program(const program &rhs) = default;
110 
111  program(program &&rhs) = default;
112 
113  program &operator=(const program &rhs) = default;
114 
115  program &operator=(program &&rhs) = default;
116 
117  bool operator==(const program &rhs) const { return impl == rhs.impl; }
118 
119  bool operator!=(const program &rhs) const { return impl != rhs.impl; }
120 
124  template <typename propertyT> bool has_property() const;
125 
132  template <typename propertyT> propertyT get_property() const;
133 
141  cl_program get() const;
142 
144  bool is_host() const;
145 
158  template <typename KernelT>
159  void compile_with_kernel_type(std::string CompileOptions = "") {
160  detail::OSModuleHandle M = detail::OSUtil::getOSModuleHandle(
162  compile_with_kernel_name(detail::KernelInfo<KernelT>::getName(),
163  CompileOptions, M);
164  }
165 
179  void compile_with_source(std::string KernelSource,
180  std::string CompileOptions = "");
181 
195  template <typename KernelT>
196  void build_with_kernel_type(std::string BuildOptions = "") {
197  detail::OSModuleHandle M = detail::OSUtil::getOSModuleHandle(
199  build_with_kernel_name(detail::KernelInfo<KernelT>::getName(), BuildOptions,
200  M);
201  }
202 
216  void build_with_source(std::string KernelSource,
217  std::string BuildOptions = "");
218 
230  void link(std::string LinkOptions = "");
231 
238  template <typename KernelT> bool has_kernel() const {
239  return has_kernel(detail::KernelInfo<KernelT>::getName(),
240  /*IsCreatedFromSource*/ false);
241  }
242 
252  bool has_kernel(std::string KernelName) const;
253 
260  template <typename KernelT> kernel get_kernel() const {
261  return get_kernel(detail::KernelInfo<KernelT>::getName(),
262  /*IsCreatedFromSource*/ false);
263  }
264 
272  kernel get_kernel(std::string KernelName) const;
273 
277  template <info::program param>
279  get_info() const;
280 
289  std::vector<std::vector<char>> get_binaries() const;
290 
292  context get_context() const;
293 
295  std::vector<device> get_devices() const;
296 
307  std::string get_compile_options() const;
308 
323  std::string get_link_options() const;
324 
333  std::string get_build_options() const;
334 
336  program_state get_state() const;
337 
343  template <typename ID, typename T>
344  ext::oneapi::experimental::spec_constant<T, ID> set_spec_constant(T Cst) {
345  constexpr const char *Name = detail::SpecConstantInfo<ID>::getName();
346  static_assert(std::is_arithmetic<T>::value ||
347  (std::is_class<T>::value && std::is_pod<T>::value),
348  "unsupported specialization constant type");
349 #ifdef __SYCL_DEVICE_ONLY__
350  (void)Cst;
351  (void)Name;
352  return ext::oneapi::experimental::spec_constant<T, ID>();
353 #else
354  set_spec_constant_impl(Name, &Cst, sizeof(T));
355  return ext::oneapi::experimental::spec_constant<T, ID>(Cst);
356 #endif // __SYCL_DEVICE_ONLY__
357  }
358 
362  backend get_backend() const noexcept;
363 
367  template <backend BackendName>
368  __SYCL_DEPRECATED("Use SYCL 2020 sycl::get_native free function")
369  auto get_native() const -> typename interop<BackendName, program>::type {
370  return reinterpret_cast<typename interop<BackendName, program>::type>(
371  getNative());
372  }
373 
374 private:
375  pi_native_handle getNative() const;
376  program(std::shared_ptr<detail::program_impl> impl);
377 
384  kernel get_kernel(std::string KernelName, bool IsCreatedFromSource) const;
385 
392  bool has_kernel(std::string KernelName, bool IsCreatedFromSource) const;
393 
399  void compile_with_kernel_name(std::string KernelName,
400  std::string CompileOptions,
402 
408  void build_with_kernel_name(std::string KernelName, std::string buildOptions,
410 
411  void set_spec_constant_impl(const char *Name, void *Data, size_t Size);
412 
413  std::shared_ptr<detail::program_impl> impl;
414 
415  template <class Obj>
416  friend decltype(Obj::impl) detail::getSyclObjImpl(const Obj &SyclObject);
417  template <class T>
418  friend T detail::createSyclObjFromImpl(decltype(T::impl) ImplObj);
419 };
420 } // namespace sycl
421 } // __SYCL_INLINE_NAMESPACE(cl)
422 
423 namespace std {
424 template <> struct hash<cl::sycl::program> {
425  size_t operator()(const cl::sycl::program &prg) const {
426  return hash<std::shared_ptr<cl::sycl::detail::program_impl>>()(
428  }
429 };
430 } // namespace std
cl::sycl::backend
backend
Definition: backend_types.hpp:21
property_list.hpp
cl::sycl::kernel_bundle
The kernel_bundle class represents collection of device images in a particular state.
Definition: kernel.hpp:26
cl::sycl::info::device
device
Definition: info_desc.hpp:47
cl::sycl::info::param_traits
Definition: info_desc.hpp:285
cl::sycl::program_state::compiled
@ compiled
__SYCL2020_DEPRECATED
#define __SYCL2020_DEPRECATED(message)
Definition: defines_elementary.hpp:54
cl::sycl::get_native
auto get_native(const SyclObjectT &Obj) -> backend_return_t< BackendName, SyclObjectT >
Definition: backend.hpp:69
cl::sycl::link
kernel_bundle< bundle_state::executable > link(const std::vector< kernel_bundle< bundle_state::object >> &ObjectBundles, const std::vector< device > &Devs, const property_list &PropList={})
Definition: kernel_bundle.hpp:636
cl::sycl::operator!=
bool operator!=(const multi_ptr< ElementType, Space > &lhs, const multi_ptr< ElementType, Space > &rhs)
Definition: multi_ptr.hpp:605
stl.hpp
cl::sycl::detail::createSyclObjFromImpl
T createSyclObjFromImpl(decltype(T::impl) ImplObj)
Definition: common.hpp:180
__SYCL_DEPRECATED
#define __SYCL_DEPRECATED(message)
Definition: defines_elementary.hpp:43
context.hpp
os_util.hpp
cl::sycl::property_list
Objects of the property_list class are containers for the SYCL properties.
Definition: property_list.hpp:25
operator==
bool operator==(const Slab &Lhs, const Slab &Rhs)
Definition: usm_allocator.cpp:343
export.hpp
cl::sycl::program_state
program_state
Definition: program.hpp:31
kernel.hpp
std::hash< cl::sycl::program >::operator()
size_t operator()(const cl::sycl::program &prg) const
Definition: program.hpp:425
cl
We provide new interfaces for matrix muliply in this patch:
Definition: access.hpp:13
cl::sycl::program_state::linked
@ linked
cl::sycl::interop
Definition: backend_types.hpp:33
cl::sycl::detail::SpecConstantInfo
Definition: kernel_desc.hpp:50
cl::sycl::detail::KernelInfo
Definition: kernel_desc.hpp:70
pi_native_handle
uintptr_t pi_native_handle
Definition: pi.h:72
std::get
constexpr tuple_element< I, tuple< Types... > >::type & get(cl::sycl::detail::tuple< Types... > &Arg) noexcept
Definition: tuple.hpp:199
cl::sycl::detail::OSModuleHandle
intptr_t OSModuleHandle
Uniquely identifies an operating system module (executable or a dynamic library)
Definition: os_util.hpp:48
cl::sycl::detail::getSyclObjImpl
decltype(Obj::impl) getSyclObjImpl(const Obj &SyclObject)
Definition: common.hpp:163
std
Definition: accessor.hpp:2356
kernel_desc.hpp
cl::sycl::context
The context class represents a SYCL context on which kernel functions may be executed.
Definition: context.hpp:35
info_desc.hpp
cl::sycl::instead
std::uint8_t instead
Definition: aliases.hpp:71
cl::sycl::info::context
context
Definition: info_desc.hpp:38
cl::sycl::kernel
Provides an abstraction of a SYCL kernel.
Definition: kernel.hpp:65
spec_constant.hpp
cl::sycl::info::program
program
Definition: info_desc.hpp:260
__SYCL_INLINE_NAMESPACE
#define __SYCL_INLINE_NAMESPACE(X)
Definition: defines_elementary.hpp:12