Skip to content

Commit 91f552f

Browse files
Added dpctl_sycl_type_casters.hpp defining convertion functions
wrap/unwrap functions for conversions between opaque DPCTL types and underlying SYCL object pointers is provided in this new header. Use of the header is deployed to files in libsyclinterface/source/ and libsyclinterface/tests/ folders. Removed libsyclinterface/include/Support/CBindingWrapping.h
1 parent c487b2b commit 91f552f

25 files changed

+282
-187
lines changed

libsyclinterface/include/Support/CBindingWrapping.h

Lines changed: 0 additions & 55 deletions
This file was deleted.
Lines changed: 249 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,249 @@
1+
//===-- dpctl_sycl_type_casters.h - Defines casters between --------*-C++-*- =//
2+
// the opaque and the underlying types.
3+
//
4+
// Data Parallel Control (dpctl)
5+
//
6+
// Copyright 2020-2022 Intel Corporation
7+
//
8+
// Licensed under the Apache License, Version 2.0 (the "License");
9+
// you may not use this file except in compliance with the License.
10+
// You may obtain a copy of the License at
11+
//
12+
// http://www.apache.org/licenses/LICENSE-2.0
13+
//
14+
// Unless required by applicable law or agreed to in writing, software
15+
// distributed under the License is distributed on an "AS IS" BASIS,
16+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17+
// See the License for the specific language governing permissions and
18+
// limitations under the License.
19+
//
20+
//===----------------------------------------------------------------------===//
21+
///
22+
/// \file
23+
/// This file defines casters between opaque types and underlying SYCL types.
24+
///
25+
//===----------------------------------------------------------------------===//
26+
27+
#pragma once
28+
29+
#ifdef __cplusplus
30+
31+
#include "dpctl_sycl_types.h"
32+
#include <CL/sycl.hpp>
33+
#include <vector>
34+
35+
#if __SYCL_COMPILER_VERSION >= 20221020
36+
37+
class dpctl_device_selector
38+
{
39+
public:
40+
virtual ~dpctl_device_selector() = default;
41+
42+
virtual int operator()(const sycl::device &device) const = 0;
43+
};
44+
45+
class dpctl_accelerator_selector : public dpctl_device_selector
46+
{
47+
public:
48+
dpctl_accelerator_selector() = default;
49+
int operator()(const sycl::device &d) const
50+
{
51+
return sycl::accelerator_selector_v(d);
52+
}
53+
};
54+
55+
class dpctl_default_selector : public dpctl_device_selector
56+
{
57+
public:
58+
dpctl_default_selector() = default;
59+
int operator()(const sycl::device &d) const
60+
{
61+
return sycl::default_selector_v(d);
62+
}
63+
};
64+
65+
class dpctl_gpu_selector : public dpctl_device_selector
66+
{
67+
public:
68+
dpctl_gpu_selector() = default;
69+
int operator()(const sycl::device &d) const
70+
{
71+
return sycl::gpu_selector_v(d);
72+
}
73+
};
74+
75+
class dpctl_cpu_selector : public dpctl_device_selector
76+
{
77+
public:
78+
dpctl_cpu_selector() = default;
79+
int operator()(const sycl::device &d) const
80+
{
81+
return sycl::cpu_selector_v(d);
82+
}
83+
};
84+
85+
class dpctl_filter_selector : public dpctl_device_selector
86+
{
87+
public:
88+
dpctl_filter_selector(const std::string &fs) : _impl(fs) {}
89+
90+
int operator()(const sycl::device &d) const
91+
{
92+
return _impl(d);
93+
}
94+
95+
private:
96+
sycl::ext::oneapi::filter_selector _impl;
97+
};
98+
99+
class dpctl_host_selector : public dpctl_device_selector
100+
{
101+
public:
102+
dpctl_host_selector() = default;
103+
int operator()(const sycl::device &) const
104+
{
105+
return REJECTED_SCORE;
106+
}
107+
108+
private:
109+
constexpr static int REJECTED_SCORE = -1;
110+
};
111+
112+
#else
113+
114+
class dpctl_device_selector : public sycl::device_selector
115+
{
116+
public:
117+
virtual ~dpctl_device_selector() = default;
118+
119+
virtual int operator()(const sycl::device &device) const = 0;
120+
};
121+
122+
class dpctl_accelerator_selector : public dpctl_device_selector
123+
{
124+
public:
125+
dpctl_accelerator_selector() : _impl(){};
126+
int operator()(const sycl::device &d) const
127+
{
128+
return _impl(d);
129+
}
130+
131+
private:
132+
sycl::accelerator_selector _impl;
133+
};
134+
135+
class dpctl_default_selector : public dpctl_device_selector
136+
{
137+
public:
138+
dpctl_default_selector() : _impl(){};
139+
int operator()(const sycl::device &d) const
140+
{
141+
return _impl(d);
142+
}
143+
144+
private:
145+
sycl::default_selector _impl;
146+
};
147+
148+
class dpctl_gpu_selector : public dpctl_device_selector
149+
{
150+
public:
151+
dpctl_gpu_selector() : _impl(){};
152+
int operator()(const sycl::device &d) const
153+
{
154+
return _impl(d);
155+
}
156+
157+
private:
158+
sycl::gpu_selector _impl;
159+
};
160+
161+
class dpctl_cpu_selector : public dpctl_device_selector
162+
{
163+
public:
164+
dpctl_cpu_selector() : _impl(){};
165+
int operator()(const sycl::device &d) const
166+
{
167+
return _impl(d);
168+
}
169+
170+
private:
171+
sycl::cpu_selector _impl;
172+
};
173+
174+
class dpctl_filter_selector : public dpctl_device_selector
175+
{
176+
public:
177+
dpctl_filter_selector(const std::string &fs) : _impl(fs) {}
178+
179+
int operator()(const sycl::device &d) const
180+
{
181+
return _impl(d);
182+
}
183+
184+
private:
185+
sycl::ext::oneapi::filter_selector _impl;
186+
};
187+
188+
class dpctl_host_selector : public dpctl_device_selector
189+
{
190+
public:
191+
dpctl_host_selector() : _impl(){};
192+
int operator()(const sycl::device &d) const
193+
{
194+
return _impl(d);
195+
}
196+
197+
private:
198+
sycl::host_selector _impl;
199+
};
200+
201+
#endif
202+
203+
/*!
204+
@brief Creates two convenience functions to reinterpret_cast an opaque
205+
pointer to a pointer to a Sycl type and vice-versa.
206+
*/
207+
#define DEFINE_SIMPLE_CONVERSION_FUNCTIONS(ty, ref) \
208+
__attribute__((unused)) inline ty *unwrap(ref P) \
209+
{ \
210+
return reinterpret_cast<ty *>(P); \
211+
} \
212+
\
213+
__attribute__((unused)) inline ref wrap(const ty *P) \
214+
{ \
215+
return reinterpret_cast<ref>(const_cast<ty *>(P)); \
216+
} \
217+
template <typename T, \
218+
std::enable_if_t<std::is_same<T, ty>::value, bool> = true> \
219+
ref wrap(const ty *P) \
220+
{ \
221+
return reinterpret_cast<ref>(const_cast<ty *>(P)); \
222+
}
223+
224+
DEFINE_SIMPLE_CONVERSION_FUNCTIONS(dpctl_device_selector,
225+
DPCTLSyclDeviceSelectorRef)
226+
DEFINE_SIMPLE_CONVERSION_FUNCTIONS(sycl::device, DPCTLSyclDeviceRef)
227+
DEFINE_SIMPLE_CONVERSION_FUNCTIONS(sycl::context, DPCTLSyclContextRef)
228+
DEFINE_SIMPLE_CONVERSION_FUNCTIONS(sycl::queue, DPCTLSyclQueueRef)
229+
DEFINE_SIMPLE_CONVERSION_FUNCTIONS(void, DPCTLSyclUSMRef)
230+
DEFINE_SIMPLE_CONVERSION_FUNCTIONS(sycl::platform, DPCTLSyclPlatformRef)
231+
DEFINE_SIMPLE_CONVERSION_FUNCTIONS(sycl::event, DPCTLSyclEventRef)
232+
DEFINE_SIMPLE_CONVERSION_FUNCTIONS(sycl::kernel, DPCTLSyclKernelRef)
233+
DEFINE_SIMPLE_CONVERSION_FUNCTIONS(
234+
sycl::kernel_bundle<sycl::bundle_state::executable>,
235+
DPCTLSyclKernelBundleRef)
236+
237+
#include "dpctl_sycl_device_manager.h"
238+
DEFINE_SIMPLE_CONVERSION_FUNCTIONS(std::vector<DPCTLSyclDeviceRef>,
239+
DPCTLDeviceVectorRef)
240+
241+
#include "dpctl_sycl_platform_manager.h"
242+
DEFINE_SIMPLE_CONVERSION_FUNCTIONS(std::vector<DPCTLSyclPlatformRef>,
243+
DPCTLPlatformVectorRef)
244+
245+
#include "dpctl_sycl_event_interface.h"
246+
DEFINE_SIMPLE_CONVERSION_FUNCTIONS(std::vector<DPCTLSyclEventRef>,
247+
DPCTLEventVectorRef)
248+
249+
#endif

libsyclinterface/source/dpctl_sycl_context_interface.cpp

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,22 +25,13 @@
2525
//===----------------------------------------------------------------------===//
2626

2727
#include "dpctl_sycl_context_interface.h"
28-
#include "Support/CBindingWrapping.h"
2928
#include "dpctl_error_handlers.h"
29+
#include "dpctl_sycl_type_casters.hpp"
3030
#include <CL/sycl.hpp>
3131
#include <vector>
3232

3333
using namespace sycl;
3434

35-
namespace
36-
{
37-
// Create wrappers for C Binding types (see CBindingWrapping.h).
38-
DEFINE_SIMPLE_CONVERSION_FUNCTIONS(context, DPCTLSyclContextRef)
39-
DEFINE_SIMPLE_CONVERSION_FUNCTIONS(device, DPCTLSyclDeviceRef)
40-
DEFINE_SIMPLE_CONVERSION_FUNCTIONS(std::vector<DPCTLSyclDeviceRef>,
41-
DPCTLDeviceVectorRef)
42-
} /* end of anonymous namespace */
43-
4435
__dpctl_give DPCTLSyclContextRef
4536
DPCTLContext_Create(__dpctl_keep const DPCTLSyclDeviceRef DRef,
4637
error_handler_callback *handler,

libsyclinterface/source/dpctl_sycl_device_interface.cpp

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,10 @@
2525
//===----------------------------------------------------------------------===//
2626

2727
#include "dpctl_sycl_device_interface.h"
28-
#include "Support/CBindingWrapping.h"
2928
#include "dpctl_error_handlers.h"
3029
#include "dpctl_string_utils.hpp"
3130
#include "dpctl_sycl_device_manager.h"
31+
#include "dpctl_sycl_type_casters.hpp"
3232
#include "dpctl_utils_helper.h"
3333
#include <CL/sycl.hpp> /* SYCL headers */
3434
#include <algorithm>
@@ -39,12 +39,6 @@ using namespace sycl;
3939

4040
namespace
4141
{
42-
// Create wrappers for C Binding types (see CBindingWrapping.h).
43-
DEFINE_SIMPLE_CONVERSION_FUNCTIONS(device, DPCTLSyclDeviceRef)
44-
DEFINE_SIMPLE_CONVERSION_FUNCTIONS(device_selector, DPCTLSyclDeviceSelectorRef)
45-
DEFINE_SIMPLE_CONVERSION_FUNCTIONS(platform, DPCTLSyclPlatformRef)
46-
DEFINE_SIMPLE_CONVERSION_FUNCTIONS(std::vector<DPCTLSyclDeviceRef>,
47-
DPCTLDeviceVectorRef)
4842

4943
template <int dim>
5044
__dpctl_keep size_t *

libsyclinterface/source/dpctl_sycl_device_manager.cpp

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,10 @@
2424
//===----------------------------------------------------------------------===//
2525

2626
#include "dpctl_sycl_device_manager.h"
27-
#include "Support/CBindingWrapping.h"
2827
#include "dpctl_error_handlers.h"
2928
#include "dpctl_string_utils.hpp"
3029
#include "dpctl_sycl_enum_types.h"
30+
#include "dpctl_sycl_type_casters.hpp"
3131
#include "dpctl_utils_helper.h"
3232
#include <CL/sycl.hpp> /* SYCL headers */
3333
#include <iomanip>
@@ -40,10 +40,6 @@ using namespace sycl;
4040
namespace
4141
{
4242

43-
// Create wrappers for C Binding types (see CBindingWrapping.h).
44-
DEFINE_SIMPLE_CONVERSION_FUNCTIONS(device, DPCTLSyclDeviceRef)
45-
DEFINE_SIMPLE_CONVERSION_FUNCTIONS(context, DPCTLSyclContextRef)
46-
4743
/*
4844
* Helper function to print the metadata for a sycl::device.
4945
*/

0 commit comments

Comments
 (0)