Skip to content

Commit

Permalink
Added DPCTLPlatform_GetDefaultContext and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
oleksandr-pavlyk committed May 2, 2022
1 parent 0168cec commit 626b210
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 0 deletions.
12 changes: 12 additions & 0 deletions libsyclinterface/include/dpctl_sycl_platform_interface.h
Original file line number Diff line number Diff line change
Expand Up @@ -142,4 +142,16 @@ DPCTLPlatform_GetVersion(__dpctl_keep const DPCTLSyclPlatformRef PRef);
DPCTL_API
__dpctl_give DPCTLPlatformVectorRef DPCTLPlatform_GetPlatforms(void);

/*!
* @brief Returns a DPCTLSyclContextRef for default platform context.
*
* @param PRef Opaque pointer to a sycl::platform
* @return A DPCTLSyclContextRef value for the default platform associated
* with this platform.
* @ingroup PlatformInterface
*/
DPCTL_API
__dpctl_give DPCTLSyclContextRef
DPCTLPlatform_GetDefaultContext(__dpctl_keep const DPCTLSyclPlatformRef PRef);

DPCTL_C_EXTERN_C_END
16 changes: 16 additions & 0 deletions libsyclinterface/source/dpctl_sycl_platform_interface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ using namespace cl::sycl;
namespace
{
DEFINE_SIMPLE_CONVERSION_FUNCTIONS(platform, DPCTLSyclPlatformRef);
DEFINE_SIMPLE_CONVERSION_FUNCTIONS(context, DPCTLSyclContextRef);
DEFINE_SIMPLE_CONVERSION_FUNCTIONS(device_selector, DPCTLSyclDeviceSelectorRef);
DEFINE_SIMPLE_CONVERSION_FUNCTIONS(std::vector<DPCTLSyclPlatformRef>,
DPCTLPlatformVectorRef);
Expand Down Expand Up @@ -202,3 +203,18 @@ __dpctl_give DPCTLPlatformVectorRef DPCTLPlatform_GetPlatforms()
// the wrap function is defined inside dpctl_vector_templ.cpp
return wrap(Platforms);
}

__dpctl_give DPCTLSyclContextRef
DPCTLPlatform_GetDefaultContext(__dpctl_keep const DPCTLSyclPlatformRef PRef)
{
auto P = unwrap(PRef);
if (P) {
auto default_ctx = P->ext_oneapi_get_default_context();
return wrap(new context(default_ctx));
}
else {
error_handler("Driver version cannot be looked up for a NULL platform.",
__FILE__, __func__, __LINE__);
return nullptr;
}
}
24 changes: 24 additions & 0 deletions libsyclinterface/tests/test_sycl_platform_interface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
//===----------------------------------------------------------------------===//

#include "Support/CBindingWrapping.h"
#include "dpctl_sycl_context_interface.h"
#include "dpctl_sycl_device_selector_interface.h"
#include "dpctl_sycl_platform_interface.h"
#include "dpctl_sycl_platform_manager.h"
Expand Down Expand Up @@ -82,6 +83,16 @@ void check_platform_backend(__dpctl_keep const DPCTLSyclPlatformRef PRef)
}());
}

void check_platform_default_context(
__dpctl_keep const DPCTLSyclPlatformRef PRef)
{
DPCTLSyclContextRef CRef = nullptr;
EXPECT_NO_FATAL_FAILURE(CRef = DPCTLPlatform_GetDefaultContext(PRef));
EXPECT_TRUE(CRef != nullptr);

EXPECT_NO_FATAL_FAILURE(DPCTLContext_Delete(CRef));
}

} // namespace

struct TestDPCTLSyclPlatformInterface
Expand Down Expand Up @@ -167,6 +178,14 @@ TEST_F(TestDPCTLSyclPlatformNull, ChkGetVersion)
ASSERT_TRUE(version == nullptr);
}

TEST_F(TestDPCTLSyclPlatformNull, ChkGetDefaultConext)
{
DPCTLSyclContextRef CRef = nullptr;

EXPECT_NO_FATAL_FAILURE(CRef = DPCTLPlatform_GetDefaultContext(NullPRef));
EXPECT_TRUE(CRef == nullptr);
}

struct TestDPCTLSyclDefaultPlatform : public ::testing::Test
{
DPCTLSyclPlatformRef PRef = nullptr;
Expand Down Expand Up @@ -207,6 +226,11 @@ TEST_P(TestDPCTLSyclPlatformInterface, ChkGetBackend)
check_platform_backend(PRef);
}

TEST_P(TestDPCTLSyclPlatformInterface, ChkGetDefaultContext)
{
check_platform_default_context(PRef);
}

TEST_P(TestDPCTLSyclPlatformInterface, ChkCopy)
{
DPCTLSyclPlatformRef Copied_PRef = nullptr;
Expand Down

0 comments on commit 626b210

Please sign in to comment.