Skip to content

Commit 369419a

Browse files
committed
Fix build issue due to gcc knows nothing about OneAPI headers (detach MKL relating things into source file)
1 parent 9db5e4f commit 369419a

File tree

5 files changed

+106
-34
lines changed

5 files changed

+106
-34
lines changed

dpnp/backend/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,7 @@ set(DPNP_SRC
182182
src/memory_sycl.cpp
183183
src/queue_sycl.cpp
184184
src/verbose.cpp
185+
src/dpnp_random_state.cpp
185186
)
186187

187188
if(DPNP_STATIC_LIB_ENABLE)

dpnp/backend/kernels/dpnp_krnl_random.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2169,9 +2169,11 @@ DPCTLSyclEventRef dpnp_rng_uniform_c(DPCTLSyclQueueRef q_ref,
21692169
// set right bound of distribution
21702170
const _DataType b = (_DataType(high));
21712171

2172+
mkl_rng::mt19937 *engine = reinterpret_cast<mkl_rng::mt19937 *> (random_state->engine);
21722173
mkl_rng::uniform<_DataType> distribution(a, b);
2174+
21732175
// perform generation
2174-
auto event_out = mkl_rng::generate(distribution, *(random_state->engine), size, result1);
2176+
auto event_out = mkl_rng::generate(distribution, *engine, size, result1);
21752177
event_ref = reinterpret_cast<DPCTLSyclEventRef>(&event_out);
21762178

21772179
return DPCTLEvent_Copy(event_ref);
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
//*****************************************************************************
2+
// Copyright (c) 2022, 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+
#include "dpnp_random_state.hpp"
27+
#include <oneapi/mkl/rng.hpp>
28+
29+
namespace mkl_rng = oneapi::mkl::rng;
30+
31+
void MT19937_InitScalarSeed(mt19937_struct *mt19937, DPCTLSyclQueueRef q_ref, uint32_t seed)
32+
{
33+
sycl::queue q = *(reinterpret_cast<sycl::queue *>(q_ref));
34+
mt19937->engine = new mkl_rng::mt19937(q, seed);
35+
return;
36+
}
37+
38+
void MT19937_InitVectorSeed(mt19937_struct *mt19937, DPCTLSyclQueueRef q_ref, uint32_t *seed, unsigned int n) {
39+
sycl::queue q = *(reinterpret_cast<sycl::queue *>(q_ref));
40+
41+
switch (n) {
42+
case 1: mt19937->engine = new mkl_rng::mt19937(q, {seed[0]}); break;
43+
case 2: mt19937->engine = new mkl_rng::mt19937(q, {seed[0], seed[1]}); break;
44+
case 3: mt19937->engine = new mkl_rng::mt19937(q, {seed[0], seed[1], seed[2]}); break;
45+
default:
46+
// TODO need to get rid of the limitation for seed vector length
47+
throw std::runtime_error("Too long seed vector");
48+
}
49+
return;
50+
}
51+
52+
void MT19937_Delete(mt19937_struct *mt19937) {
53+
mkl_rng::mt19937 *engine = reinterpret_cast<mkl_rng::mt19937 *>(mt19937->engine);
54+
mt19937->engine = nullptr;
55+
delete engine;
56+
return;
57+
}
Lines changed: 44 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
//*****************************************************************************
2-
// Copyright (c) 2016-2022, Intel Corporation
2+
// Copyright (c) 2022, Intel Corporation
33
// All rights reserved.
44
//
55
// Redistribution and use in source and binary forms, with or without
@@ -23,58 +23,69 @@
2323
// THE POSSIBILITY OF SUCH DAMAGE.
2424
//*****************************************************************************
2525

26+
/*
27+
* This header file is for interface Cython with C++.
28+
* It should not contains any backend specific headers (like SYCL or math library) because
29+
* all included headers will be exposed in Cython compilation procedure
30+
*
31+
* We would like to avoid backend specific things in higher level Cython modules.
32+
* Any backend interface functions and types should be defined here.
33+
*
34+
* Also, this file should contains documentation on functions and types
35+
* which are used in the interface
36+
*/
37+
2638
#pragma once
2739
#ifndef BACKEND_RANDOM_STATE_H // Cython compatibility
2840
#define BACKEND_RANDOM_STATE_H
2941

30-
#include <CL/sycl.hpp>
31-
#include <oneapi/mkl/rng.hpp>
42+
#ifdef _WIN32
43+
#define INP_DLLEXPORT __declspec(dllexport)
44+
#else
45+
#define INP_DLLEXPORT
46+
#endif
3247

33-
namespace mkl_rng = oneapi::mkl::rng;
48+
#include <dpctl_sycl_interface.h>
3449

3550
// Structure storing MKL engine for MT199374x32x10 algorithm
3651
struct mt19937_struct
3752
{
38-
mkl_rng::mt19937* engine;
53+
void* engine;
3954
};
4055

4156
/**
42-
* @brief Create a MKL engine from scalar seed
57+
* @ingroup BACKEND_API
58+
* @brief Create a MKL engine from scalar seed.
4359
*
4460
* Invoke a common seed initialization of the engine for MT199374x32x10 algorithm.
61+
*
62+
* @param [in] mt19937 A structure with MKL engine which will be filled with generated value by MKL.
63+
* @param [in] q_ref A refference on SYCL queue which will be used to obtain random numbers.
64+
* @param [in] seed An initial condition of the generator state.
4565
*/
46-
void MT19937_InitScalarSeed(mt19937_struct *mt19937, DPCTLSyclQueueRef q_ref, uint32_t seed = 1)
47-
{
48-
sycl::queue q = *(reinterpret_cast<sycl::queue *>(q_ref));
49-
mt19937->engine = new mkl_rng::mt19937(q, seed);
50-
return;
51-
}
66+
INP_DLLEXPORT void MT19937_InitScalarSeed(mt19937_struct *mt19937, DPCTLSyclQueueRef q_ref, uint32_t seed = 1);
5267

5368
/**
54-
* @brief Create a MKL engine from seed vector
69+
* @ingroup BACKEND_API
70+
* @brief Create a MKL engine from seed vector.
5571
*
56-
* Invoke an extended seed initialization of the engine for MT199374x32x10 algorithm.
72+
* Invoke an extended seed initialization of the engine for MT199374x32x10 algorithm..
5773
*
58-
* @note the vector size is limited by length=3
74+
* @param [in] mt19937 A structure with MKL engine which will be filled with generated value by MKL.
75+
* @param [in] q_ref A refference on SYCL queue which will be used to obtain random numbers.
76+
* @param [in] seed A vector with the initial conditions of the generator state.
77+
* @param [in] n Length of the vector.
5978
*/
60-
void MT19937_InitVectorSeed(mt19937_struct *mt19937, DPCTLSyclQueueRef q_ref, uint32_t * seed, unsigned int n) {
61-
sycl::queue q = *(reinterpret_cast<sycl::queue *>(q_ref));
62-
63-
switch (n) {
64-
case 1: mt19937->engine = new mkl_rng::mt19937(q, {seed[0]}); break;
65-
case 2: mt19937->engine = new mkl_rng::mt19937(q, {seed[0], seed[1]}); break;
66-
case 3: mt19937->engine = new mkl_rng::mt19937(q, {seed[0], seed[1], seed[2]}); break;
67-
default:
68-
// TODO need to get rid of the limitation for seed vector length
69-
throw std::runtime_error("Too long seed vector");
70-
}
71-
return;
72-
}
79+
INP_DLLEXPORT void MT19937_InitVectorSeed(mt19937_struct *mt19937, DPCTLSyclQueueRef q_ref, uint32_t * seed, unsigned int n);
7380

74-
void MT19937_Delete(mt19937_struct *mt19937) {
75-
delete mt19937->engine;
76-
mt19937->engine = nullptr;
77-
return;
78-
}
81+
/**
82+
* @ingroup BACKEND_API
83+
* @brief Release a MKL engine.
84+
*
85+
* Release all resource required for storing of the MKL engine.
86+
*
87+
* @param [in] mt19937 A structure with the MKL engine.
88+
*/
89+
INP_DLLEXPORT void MT19937_Delete(mt19937_struct *mt19937);
7990

8091
#endif // BACKEND_RANDOM_STATE_H

utils/command_build_clib.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,7 @@
187187
"dpnp/backend/src/constants.cpp",
188188
"dpnp/backend/src/queue_sycl.cpp",
189189
"dpnp/backend/src/verbose.cpp",
190+
"dpnp/backend/src/dpnp_random_state.cpp"
190191
],
191192
}
192193
]

0 commit comments

Comments
 (0)