DPC++ Runtime
Runtime libraries for oneAPI Data Parallel C++
program_impl.hpp
Go to the documentation of this file.
1 //==----- program_impl.hpp --- SYCL program implementation -----------------==//
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 #pragma once
9 
10 #include <CL/sycl/context.hpp>
13 #include <CL/sycl/device.hpp>
14 #include <CL/sycl/program.hpp>
16 #include <CL/sycl/stl.hpp>
17 #include <detail/context_impl.hpp>
20 
21 #include <algorithm>
22 #include <cassert>
23 #include <fstream>
24 #include <memory>
25 #include <mutex>
26 
28 namespace sycl {
29 
30 // Forward declarations
31 class kernel;
32 
33 namespace detail {
34 
35 using ContextImplPtr = std::shared_ptr<detail::context_impl>;
36 
37 class program_impl {
38 public:
39  program_impl() = delete;
40 
49  explicit program_impl(ContextImplPtr Context, const property_list &PropList);
50 
60  program_impl(ContextImplPtr Context, std::vector<device> DeviceList,
61  const property_list &PropList);
62 
78  program_impl(std::vector<std::shared_ptr<program_impl>> ProgramList,
79  std::string LinkOptions, const property_list &PropList);
80 
94  program_impl(ContextImplPtr Context, pi_native_handle InteropProgram);
95 
101  program_impl(ContextImplPtr Context, RT::PiKernel Kernel);
102 
103  ~program_impl();
104 
108  template <typename propertyT> bool has_property() const {
109  return MPropList.has_property<propertyT>();
110  }
111 
118  template <typename propertyT> propertyT get_property() const {
119  return MPropList.get_property<propertyT>();
120  }
121 
129  cl_program get() const;
130 
133  RT::PiProgram &getHandleRef() { return MProgram; }
136  const RT::PiProgram &getHandleRef() const { return MProgram; }
137 
139  bool is_host() const { return MContext->is_host(); }
140 
155  void compile_with_kernel_name(std::string KernelName,
156  std::string CompileOptions,
157  OSModuleHandle Module);
158 
172  void compile_with_source(std::string KernelSource,
173  std::string CompileOptions = "");
174 
190  void build_with_kernel_name(std::string KernelName, std::string BuildOptions,
191  OSModuleHandle M);
192 
206  void build_with_source(std::string KernelSource,
207  std::string BuildOptions = "");
208 
220  void link(std::string LinkOptions = "");
221 
228  bool has_kernel(std::string KernelName, bool IsCreatedFromSource) const;
229 
237  kernel get_kernel(std::string KernelName,
238  std::shared_ptr<program_impl> PtrToSelf,
239  bool IsCreatedFromSource) const;
240 
244  template <info::program param>
246  get_info() const;
247 
256  std::vector<std::vector<char>> get_binaries() const;
257 
260  if (is_host())
261  return context();
262  return createSyclObjFromImpl<context>(MContext);
263  }
264 
266  const plugin &getPlugin() const {
267  assert(!is_host() && "Plugin is not available for Host.");
268  return MContext->getPlugin();
269  }
270 
272  std::vector<device> get_devices() const { return MDevices; }
273 
284  std::string get_compile_options() const { return MCompileOptions; }
285 
300  std::string get_link_options() const { return MLinkOptions; }
301 
310  std::string get_build_options() const { return MBuildOptions; }
311 
313  program_state get_state() const { return MState; }
314 
315  void set_spec_constant_impl(const char *Name, const void *ValAddr,
316  size_t ValSize);
317 
326  void flush_spec_constants(const RTDeviceBinaryImage &Img,
327  RT::PiProgram NativePrg = nullptr) const;
328 
334  OSModuleHandle getOSModuleHandle() const { return MProgramModuleHandle; }
335 
337  detail::stableSerializeSpecConstRegistry(SpecConstRegistry, Dst);
338  }
339 
341  bool hasSetSpecConstants() const { return !SpecConstRegistry.empty(); }
342 
344  bool is_cacheable() const { return MProgramAndKernelCachingAllowed; }
345 
347  pi_native_handle getNative() const;
348 
349  bool isInterop() const { return MIsInterop; }
350 
351 private:
352  // Deligating Constructor used in Implementation.
353  program_impl(ContextImplPtr Context, pi_native_handle InteropProgram,
354  RT::PiProgram Program);
361  template <info::device param>
362  void check_device_feature_support(const std::vector<device> &Devices) {
363  for (const auto &Device : Devices) {
364  if (!Device.get_info<param>()) {
365  throw feature_not_supported(
366  "Online compilation is not supported by this device",
368  }
369  }
370  }
371 
378  void
379  create_pi_program_with_kernel_name(OSModuleHandle Module,
380  const std::string &KernelName,
381  bool JITCompilationIsRequired = false);
382 
386  void create_cl_program_with_source(const std::string &Source);
387 
391  void compile(const std::string &Options);
392 
396  void build(const std::string &Options);
397 
399  std::vector<RT::PiDevice> get_pi_devices() const;
400 
403  static bool is_cacheable_with_options(const std::string &Options) {
404  return Options.empty();
405  }
406 
409  bool has_cl_kernel(const std::string &KernelName) const;
410 
414  RT::PiKernel get_pi_kernel(const std::string &KernelName) const;
415 
417  std::vector<device> sort_devices_by_cl_device_id(std::vector<device> Devices);
418 
423  void throw_if_state_is(program_state State) const;
424 
429  void throw_if_state_is_not(program_state State) const;
430 
431  RT::PiProgram MProgram = nullptr;
432  program_state MState = program_state::none;
433  std::mutex MMutex;
434  ContextImplPtr MContext;
435  bool MLinkable = false;
436  std::vector<device> MDevices;
437  property_list MPropList;
438  std::string MCompileOptions;
439  std::string MLinkOptions;
440  std::string MBuildOptions;
441  OSModuleHandle MProgramModuleHandle = OSUtil::ExeModuleHandle;
442 
443  // Keeps specialization constant map for this program. Spec constant name
444  // resolution to actual SPIR-V integer ID happens at build time, where the
445  // device binary image is available. Access is guarded by this context's
446  // program cache lock.
447  SpecConstRegistryT SpecConstRegistry;
448 
452  bool MProgramAndKernelCachingAllowed = false;
453 
454  bool MIsInterop = false;
455 };
456 
457 template <>
458 cl_uint program_impl::get_info<info::program::reference_count>() const;
459 
460 template <> context program_impl::get_info<info::program::context>() const;
461 
462 template <>
463 std::vector<device> program_impl::get_info<info::program::devices>() const;
464 
465 } // namespace detail
466 } // namespace sycl
467 } // __SYCL_INLINE_NAMESPACE(cl)
cl::sycl::detail::program_impl::get_context
context get_context() const
Definition: program_impl.hpp:259
property_list.hpp
cl::sycl::detail::SerializedObj
std::vector< unsigned char > SerializedObj
Definition: util.hpp:56
cl::sycl::detail::ContextImplPtr
std::shared_ptr< detail::context_impl > ContextImplPtr
Definition: memory_manager.hpp:31
cl::sycl::detail::program_impl::getPlugin
const plugin & getPlugin() const
Definition: program_impl.hpp:266
context_impl.hpp
cl::sycl::detail::program_impl::isInterop
bool isInterop() const
Definition: program_impl.hpp:349
cl::sycl::info::param_traits
Definition: info_desc.hpp:300
cl::sycl::detail::RTDeviceBinaryImage
Definition: device_binary_image.hpp:20
cl::sycl::build
kernel_bundle< bundle_state::executable > build(const kernel_bundle< bundle_state::input > &InputBundle, const std::vector< device > &Devs, const property_list &PropList={})
Definition: kernel_bundle.hpp:694
PI_COMPILER_NOT_AVAILABLE
@ PI_COMPILER_NOT_AVAILABLE
Definition: pi.h:105
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:647
stl.hpp
device.hpp
cl::sycl::detail::program_impl::get_property
propertyT get_property() const
Gets the specified property of this program_impl.
Definition: program_impl.hpp:118
context.hpp
cl::sycl::errc::feature_not_supported
@ feature_not_supported
cl::sycl::detail::program_impl
Definition: program_impl.hpp:37
cl::sycl::info::kernel
kernel
Definition: info_desc.hpp:229
cl::sycl::property_list
Objects of the property_list class are containers for the SYCL properties.
Definition: property_list.hpp:26
_pi_kernel
Implementation of a PI Kernel for CUDA.
Definition: pi_cuda.hpp:578
cl::sycl::detail::program_impl::getHandleRef
const RT::PiProgram & getHandleRef() const
Definition: program_impl.hpp:136
cl::sycl::detail::program_impl::is_cacheable
bool is_cacheable() const
Definition: program_impl.hpp:344
program.hpp
cl::sycl::compile
kernel_bundle< bundle_state::object > compile(const kernel_bundle< bundle_state::input > &InputBundle, const std::vector< device > &Devs, const property_list &PropList={})
Definition: kernel_bundle.hpp:612
cl::sycl::detail::program_impl::get_state
program_state get_state() const
Definition: program_impl.hpp:313
cl::sycl::detail::stableSerializeSpecConstRegistry
void stableSerializeSpecConstRegistry(const SpecConstRegistryT &Reg, SerializedObj &Dst)
Definition: spec_constant_impl.cpp:30
cl::sycl::detail::program_impl::get_devices
std::vector< device > get_devices() const
Definition: program_impl.hpp:272
cl::sycl::detail::program_impl::has_property
bool has_property() const
Checks if this program_impl has a property of type propertyT.
Definition: program_impl.hpp:108
cl
We provide new interfaces for matrix muliply in this patch:
Definition: access.hpp:13
cl::sycl::detail::program_impl::get_build_options
std::string get_build_options() const
Returns the compile, link, or build options, from whichever of those operations was performed most re...
Definition: program_impl.hpp:310
cl::sycl::detail::program_impl::get_compile_options
std::string get_compile_options() const
Returns compile options that were provided when the encapsulated program was explicitly compiled.
Definition: program_impl.hpp:284
_pi_program
Implementation of PI Program on CUDA Module object.
Definition: pi_cuda.hpp:523
cl::sycl::detail::program_impl::stableSerializeSpecConstRegistry
void stableSerializeSpecConstRegistry(SerializedObj &Dst) const
Definition: program_impl.hpp:336
cl::sycl::detail::plugin
The plugin class provides a unified interface to the underlying low-level runtimes for the device-agn...
Definition: plugin.hpp:89
program_manager.hpp
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
kernel_desc.hpp
cl::sycl::detail::program_impl::hasSetSpecConstants
bool hasSetSpecConstants() const
Tells whether a specialization constant has been set for this program.
Definition: program_impl.hpp:341
cl::sycl::context
The context class represents a SYCL context on which kernel functions may be executed.
Definition: context.hpp:35
cl::sycl::cl_uint
std::uint32_t cl_uint
Definition: aliases.hpp:83
cl::sycl::detail::program_impl::get_link_options
std::string get_link_options() const
Returns compile options that were provided to the most recent invocation of link member function.
Definition: program_impl.hpp:300
cl::sycl::info::context
context
Definition: info_desc.hpp:41
cl::sycl::detail::program_impl::is_host
bool is_host() const
Definition: program_impl.hpp:139
spec_constant_impl.hpp
cl::sycl::detail::program_impl::getHandleRef
RT::PiProgram & getHandleRef()
Definition: program_impl.hpp:133
cl::sycl::detail::program_impl::getOSModuleHandle
OSModuleHandle getOSModuleHandle() const
Returns the OS module handle this program belongs to.
Definition: program_impl.hpp:334
common_info.hpp
cl::sycl::kernel
Provides an abstraction of a SYCL kernel.
Definition: kernel.hpp:67
cl::sycl::detail::SpecConstRegistryT
std::map< std::string, spec_constant_impl > SpecConstRegistryT
Definition: spec_constant_impl.hpp:44
__SYCL_INLINE_NAMESPACE
#define __SYCL_INLINE_NAMESPACE(X)
Definition: defines_elementary.hpp:12