DPNP C++ backend kernel library 0.20.0dev0
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 static backend_sycl backend{};
71 return backend;
72 }
73
74 static sycl::queue &get_queue()
75 {
76 auto &be = backend_sycl::get();
77 return be.queue_;
78 }
79
80 static mkl_rng::mt19937 &get_rng_engine()
81 {
82 auto &be = backend_sycl::get();
83 return be.rng_mt19937_engine_;
84 }
85
86 static mkl_rng::mcg59 &get_rng_mcg59_engine()
87 {
88 auto &be = backend_sycl::get();
89 return be.rng_mcg59_engine_;
90 }
91
92 template <typename SeedT>
93 void set_rng_engines_seed(const SeedT &seed)
94 {
95 mkl_rng::mt19937 rng_eng_mt19937(queue_, seed);
96 mkl_rng::mcg59 rng_eng_mcg59(queue_, seed);
97
98 // now that instances are created, let's move them
99 rng_mt19937_engine_ = std::move(rng_eng_mt19937);
100 rng_mcg59_engine_ = std::move(rng_eng_mcg59);
101 }
102
103 bool backend_sycl_is_cpu() const
104 {
105 const auto &dev = queue_.get_device();
106 return dev.is_cpu();
107 }
108
109private:
110 static constexpr std::size_t default_seed = 1;
111
113 : queue_{sycl::default_selector_v,
114 (is_verbose_mode())
115 ? sycl::property_list{sycl::property::queue::
116 enable_profiling()}
117 : sycl::property_list{}},
118 rng_mt19937_engine_{queue_, default_seed}, rng_mcg59_engine_{
119 queue_, default_seed}
120 {
121 }
122
123 backend_sycl(backend_sycl const &) = default;
124 backend_sycl &operator=(backend_sycl const &) = default;
125 backend_sycl &operator=(backend_sycl &&) = default;
126
127 sycl::queue queue_;
128 mkl_rng::mt19937 rng_mt19937_engine_;
129 mkl_rng::mcg59 rng_mcg59_engine_;
130};
131
132#endif // QUEUE_SYCL_H