DPNP C++ backend kernel library 0.20.0dev6
Data Parallel Extension for NumPy*
Loading...
Searching...
No Matches
queue_sycl.hpp
1//*****************************************************************************
2// Copyright (c) 2016, Intel Corporation
3// All rights reserved.
4//
5// Redistribution and use in source and binary forms, with or without
6// modification, are permitted provided that the following conditions are met:
7// - Redistributions of source code must retain the above copyright notice,
8// this list of conditions and the following disclaimer.
9// - Redistributions in binary form must reproduce the above copyright notice,
10// this list of conditions and the following disclaimer in the documentation
11// and/or other materials provided with the distribution.
12// - Neither the name of the copyright holder nor the names of its contributors
13// may be used to endorse or promote products derived from this software
14// without specific prior written permission.
15//
16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
26// THE POSSIBILITY OF SUCH DAMAGE.
27//*****************************************************************************
28
29#pragma once
30#ifndef QUEUE_SYCL_H // Cython compatibility
31#define QUEUE_SYCL_H
32
33// #pragma clang diagnostic push
34// #pragma clang diagnostic ignored "-Wpass-failed"
35#include <sycl/sycl.hpp>
36// #pragma clang diagnostic pop
37
38#pragma clang diagnostic push
39#pragma clang diagnostic ignored "-Wunused-parameter"
40#pragma clang diagnostic ignored "-Wreorder-ctor"
41#include <oneapi/mkl.hpp>
42#pragma clang diagnostic pop
43
44#include <utility>
45
46#include "dpnp_pstl.hpp" // this header must be included after <mkl.hpp>
47
48#include "verbose.hpp"
49
50namespace mkl_rng = oneapi::mkl::rng;
51
52#define DPNP_QUEUE backend_sycl::get_queue()
53#define DPNP_RNG_ENGINE backend_sycl::get_rng_engine()
54#define DPNP_RNG_MCG59_ENGINE backend_sycl::get_rng_mcg59_engine()
55
64{
65public:
66 ~backend_sycl() {}
67
68 static backend_sycl &get()
69 {
70#if defined(_WIN32) && INTEL_MKL_VERSION == 20260000
71 // TODO: remove once MKLD-19835 is resolved
72 // mt19937 (oneMKL 2026.0) destructor crashes during DLL_PROCESS_DETACH
73 // on Windows (Battlemage/Level Zero). Use a heap-allocated
74 // process-lifetime singleton to skip destructor; OS reclaims memory.
75 static backend_sycl *backend = new backend_sycl{};
76 return *backend;
77#else
78 static backend_sycl backend{};
79 return backend;
80#endif
81 }
82
83 static sycl::queue &get_queue()
84 {
85 auto &be = backend_sycl::get();
86 return be.queue_;
87 }
88
89 static mkl_rng::mt19937 &get_rng_engine()
90 {
91 auto &be = backend_sycl::get();
92 return be.rng_mt19937_engine_;
93 }
94
95 static mkl_rng::mcg59 &get_rng_mcg59_engine()
96 {
97 auto &be = backend_sycl::get();
98 return be.rng_mcg59_engine_;
99 }
100
101 template <typename SeedT>
102 void set_rng_engines_seed(const SeedT &seed)
103 {
104 mkl_rng::mt19937 rng_eng_mt19937(queue_, seed);
105 mkl_rng::mcg59 rng_eng_mcg59(queue_, seed);
106
107 // now that instances are created, let's move them
108 rng_mt19937_engine_ = std::move(rng_eng_mt19937);
109 rng_mcg59_engine_ = std::move(rng_eng_mcg59);
110 }
111
112 bool backend_sycl_is_cpu() const
113 {
114 const auto &dev = queue_.get_device();
115 return dev.is_cpu();
116 }
117
118private:
119 static constexpr std::size_t default_seed = 1;
120
122 : queue_{sycl::default_selector_v,
123 (is_verbose_mode())
124 ? sycl::property_list{sycl::property::queue::
125 enable_profiling()}
126 : sycl::property_list{}},
127 rng_mt19937_engine_{queue_, default_seed},
128 rng_mcg59_engine_{queue_, default_seed}
129 {
130 }
131
132 backend_sycl(backend_sycl const &) = default;
133 backend_sycl &operator=(backend_sycl const &) = default;
134 backend_sycl &operator=(backend_sycl &&) = default;
135
136 sycl::queue queue_;
137 mkl_rng::mt19937 rng_mt19937_engine_;
138 mkl_rng::mcg59 rng_mcg59_engine_;
139};
140
141#endif // QUEUE_SYCL_H