Skip to content

Commit 626b210

Browse files
Added DPCTLPlatform_GetDefaultContext and tests
1 parent 0168cec commit 626b210

File tree

3 files changed

+52
-0
lines changed

3 files changed

+52
-0
lines changed

libsyclinterface/include/dpctl_sycl_platform_interface.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,4 +142,16 @@ DPCTLPlatform_GetVersion(__dpctl_keep const DPCTLSyclPlatformRef PRef);
142142
DPCTL_API
143143
__dpctl_give DPCTLPlatformVectorRef DPCTLPlatform_GetPlatforms(void);
144144

145+
/*!
146+
* @brief Returns a DPCTLSyclContextRef for default platform context.
147+
*
148+
* @param PRef Opaque pointer to a sycl::platform
149+
* @return A DPCTLSyclContextRef value for the default platform associated
150+
* with this platform.
151+
* @ingroup PlatformInterface
152+
*/
153+
DPCTL_API
154+
__dpctl_give DPCTLSyclContextRef
155+
DPCTLPlatform_GetDefaultContext(__dpctl_keep const DPCTLSyclPlatformRef PRef);
156+
145157
DPCTL_C_EXTERN_C_END

libsyclinterface/source/dpctl_sycl_platform_interface.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ using namespace cl::sycl;
4141
namespace
4242
{
4343
DEFINE_SIMPLE_CONVERSION_FUNCTIONS(platform, DPCTLSyclPlatformRef);
44+
DEFINE_SIMPLE_CONVERSION_FUNCTIONS(context, DPCTLSyclContextRef);
4445
DEFINE_SIMPLE_CONVERSION_FUNCTIONS(device_selector, DPCTLSyclDeviceSelectorRef);
4546
DEFINE_SIMPLE_CONVERSION_FUNCTIONS(std::vector<DPCTLSyclPlatformRef>,
4647
DPCTLPlatformVectorRef);
@@ -202,3 +203,18 @@ __dpctl_give DPCTLPlatformVectorRef DPCTLPlatform_GetPlatforms()
202203
// the wrap function is defined inside dpctl_vector_templ.cpp
203204
return wrap(Platforms);
204205
}
206+
207+
__dpctl_give DPCTLSyclContextRef
208+
DPCTLPlatform_GetDefaultContext(__dpctl_keep const DPCTLSyclPlatformRef PRef)
209+
{
210+
auto P = unwrap(PRef);
211+
if (P) {
212+
auto default_ctx = P->ext_oneapi_get_default_context();
213+
return wrap(new context(default_ctx));
214+
}
215+
else {
216+
error_handler("Driver version cannot be looked up for a NULL platform.",
217+
__FILE__, __func__, __LINE__);
218+
return nullptr;
219+
}
220+
}

libsyclinterface/tests/test_sycl_platform_interface.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
//===----------------------------------------------------------------------===//
2626

2727
#include "Support/CBindingWrapping.h"
28+
#include "dpctl_sycl_context_interface.h"
2829
#include "dpctl_sycl_device_selector_interface.h"
2930
#include "dpctl_sycl_platform_interface.h"
3031
#include "dpctl_sycl_platform_manager.h"
@@ -82,6 +83,16 @@ void check_platform_backend(__dpctl_keep const DPCTLSyclPlatformRef PRef)
8283
}());
8384
}
8485

86+
void check_platform_default_context(
87+
__dpctl_keep const DPCTLSyclPlatformRef PRef)
88+
{
89+
DPCTLSyclContextRef CRef = nullptr;
90+
EXPECT_NO_FATAL_FAILURE(CRef = DPCTLPlatform_GetDefaultContext(PRef));
91+
EXPECT_TRUE(CRef != nullptr);
92+
93+
EXPECT_NO_FATAL_FAILURE(DPCTLContext_Delete(CRef));
94+
}
95+
8596
} // namespace
8697

8798
struct TestDPCTLSyclPlatformInterface
@@ -167,6 +178,14 @@ TEST_F(TestDPCTLSyclPlatformNull, ChkGetVersion)
167178
ASSERT_TRUE(version == nullptr);
168179
}
169180

181+
TEST_F(TestDPCTLSyclPlatformNull, ChkGetDefaultConext)
182+
{
183+
DPCTLSyclContextRef CRef = nullptr;
184+
185+
EXPECT_NO_FATAL_FAILURE(CRef = DPCTLPlatform_GetDefaultContext(NullPRef));
186+
EXPECT_TRUE(CRef == nullptr);
187+
}
188+
170189
struct TestDPCTLSyclDefaultPlatform : public ::testing::Test
171190
{
172191
DPCTLSyclPlatformRef PRef = nullptr;
@@ -207,6 +226,11 @@ TEST_P(TestDPCTLSyclPlatformInterface, ChkGetBackend)
207226
check_platform_backend(PRef);
208227
}
209228

229+
TEST_P(TestDPCTLSyclPlatformInterface, ChkGetDefaultContext)
230+
{
231+
check_platform_default_context(PRef);
232+
}
233+
210234
TEST_P(TestDPCTLSyclPlatformInterface, ChkCopy)
211235
{
212236
DPCTLSyclPlatformRef Copied_PRef = nullptr;

0 commit comments

Comments
 (0)