DPNP C++ backend kernel library 0.18.0dev0
Data Parallel Extension for NumPy*
Loading...
Searching...
No Matches
queue_sycl.hpp
1//*****************************************************************************
2// Copyright (c) 2016-2025, 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//
13// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
14// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
17// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23// THE POSSIBILITY OF SUCH DAMAGE.
24//*****************************************************************************
25
26#pragma once
27#ifndef QUEUE_SYCL_H // Cython compatibility
28#define QUEUE_SYCL_H
29
30//#pragma clang diagnostic push
31//#pragma clang diagnostic ignored "-Wpass-failed"
32#include <sycl/sycl.hpp>
33//#pragma clang diagnostic pop
34
35#pragma clang diagnostic push
36#pragma clang diagnostic ignored "-Wunused-parameter"
37#pragma clang diagnostic ignored "-Wreorder-ctor"
38#include <oneapi/mkl.hpp>
39#pragma clang diagnostic pop
40
41#include <utility>
42
43#include "dpnp_pstl.hpp" // this header must be included after <mkl.hpp>
44
45#include "verbose.hpp"
46
47namespace mkl_rng = oneapi::mkl::rng;
48
49#define DPNP_QUEUE backend_sycl::get_queue()
50#define DPNP_RNG_ENGINE backend_sycl::get_rng_engine()
51#define DPNP_RNG_MCG59_ENGINE backend_sycl::get_rng_mcg59_engine()
52
61{
62public:
63 ~backend_sycl() {}
64
65 static backend_sycl &get()
66 {
67 static backend_sycl backend{};
68 return backend;
69 }
70
71 static sycl::queue &get_queue()
72 {
73 auto &be = backend_sycl::get();
74 return be.queue_;
75 }
76
77 static mkl_rng::mt19937 &get_rng_engine()
78 {
79 auto &be = backend_sycl::get();
80 return be.rng_mt19937_engine_;
81 }
82
83 static mkl_rng::mcg59 &get_rng_mcg59_engine()
84 {
85 auto &be = backend_sycl::get();
86 return be.rng_mcg59_engine_;
87 }
88
89 template <typename SeedT>
90 void set_rng_engines_seed(const SeedT &seed)
91 {
92 mkl_rng::mt19937 rng_eng_mt19937(queue_, seed);
93 mkl_rng::mcg59 rng_eng_mcg59(queue_, seed);
94
95 // now that instances are created, let's move them
96 rng_mt19937_engine_ = std::move(rng_eng_mt19937);
97 rng_mcg59_engine_ = std::move(rng_eng_mcg59);
98 }
99
100 bool backend_sycl_is_cpu() const
101 {
102 const auto &dev = queue_.get_device();
103 return dev.is_cpu();
104 }
105
106private:
107 static constexpr std::size_t default_seed = 1;
108
110 : queue_{sycl::default_selector_v,
111 (is_verbose_mode())
112 ? sycl::property_list{sycl::property::queue::
113 enable_profiling()}
114 : sycl::property_list{}},
115 rng_mt19937_engine_{queue_, default_seed}, rng_mcg59_engine_{
116 queue_, default_seed}
117 {
118 }
119
120 backend_sycl(backend_sycl const &) = default;
121 backend_sycl &operator=(backend_sycl const &) = default;
122 backend_sycl &operator=(backend_sycl &&) = default;
123
124 sycl::queue queue_;
125 mkl_rng::mt19937 rng_mt19937_engine_;
126 mkl_rng::mcg59 rng_mcg59_engine_;
127};
128
129#endif // QUEUE_SYCL_H