Skip to content

Commit 91e869d

Browse files
Add DPCTLEvent_Copy func (#504)
1 parent 8c099d8 commit 91e869d

File tree

3 files changed

+126
-1
lines changed

3 files changed

+126
-1
lines changed

dpctl-capi/include/dpctl_sycl_event_interface.h

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,15 @@ DPCTL_C_EXTERN_C_BEGIN
3737
* @defgroup EventInterface Event class C wrapper
3838
*/
3939

40+
/*!
41+
* @brief A wrapper for ``sycl::event`` contructor to construct a new event.
42+
*
43+
* @return An opaque DPCTLSyclEventRef pointer wrapping a ``sycl::event``.
44+
* @ingroup EventInterface
45+
*/
46+
DPCTL_API
47+
__dpctl_give DPCTLSyclEventRef DPCTLEvent_Create(void);
48+
4049
/*!
4150
* @brief C-API wrapper for sycl::event.wait.
4251
*
@@ -48,7 +57,7 @@ DPCTL_API
4857
void DPCTLEvent_Wait(__dpctl_keep DPCTLSyclEventRef ERef);
4958

5059
/*!
51-
* @brief Deletes the DPCTLSyclEventRef after casting it to a sycl::event.
60+
* @brief Deletes the DPCTLSyclEventRef after casting it to a ``sycl::event``.
5261
*
5362
* @param ERef An opaque DPCTLSyclEventRef pointer that would be
5463
* freed.
@@ -57,4 +66,16 @@ void DPCTLEvent_Wait(__dpctl_keep DPCTLSyclEventRef ERef);
5766
DPCTL_API
5867
void DPCTLEvent_Delete(__dpctl_take DPCTLSyclEventRef ERef);
5968

69+
/*!
70+
* @brief Returns a copy of the DPCTLSyclEventRef object.
71+
*
72+
* @param ERef DPCTLSyclEventRef object to be copied.
73+
* @return A new DPCTLSyclEventRef created by copying the passed in
74+
* DPCTLSyclEventRef object.
75+
* @ingroup EventInterface
76+
*/
77+
DPCTL_API
78+
__dpctl_give DPCTLSyclEventRef
79+
DPCTLEvent_Copy(__dpctl_keep const DPCTLSyclEventRef ERef);
80+
6081
DPCTL_C_EXTERN_C_END

dpctl-capi/source/dpctl_sycl_event_interface.cpp

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,18 @@ namespace
3636
DEFINE_SIMPLE_CONVERSION_FUNCTIONS(event, DPCTLSyclEventRef)
3737
} /* end of anonymous namespace */
3838

39+
__dpctl_give DPCTLSyclEventRef DPCTLEvent_Create()
40+
{
41+
DPCTLSyclEventRef ERef = nullptr;
42+
try {
43+
auto E = new event();
44+
ERef = wrap(E);
45+
} catch (std::bad_alloc const &ba) {
46+
std::cerr << ba.what() << '\n';
47+
}
48+
return ERef;
49+
}
50+
3951
void DPCTLEvent_Wait(__dpctl_keep DPCTLSyclEventRef ERef)
4052
{
4153
// \todo How to handle errors? E.g. when ERef is null or not a valid event.
@@ -47,3 +59,21 @@ void DPCTLEvent_Delete(__dpctl_take DPCTLSyclEventRef ERef)
4759
{
4860
delete unwrap(ERef);
4961
}
62+
63+
__dpctl_give DPCTLSyclEventRef
64+
DPCTLEvent_Copy(__dpctl_keep DPCTLSyclEventRef ERef)
65+
{
66+
auto SyclEvent = unwrap(ERef);
67+
if (!SyclEvent) {
68+
std::cerr << "Cannot copy DPCTLSyclEventRef as input is a nullptr\n";
69+
return nullptr;
70+
}
71+
try {
72+
auto CopiedSyclEvent = new event(*SyclEvent);
73+
return wrap(CopiedSyclEvent);
74+
} catch (std::bad_alloc const &ba) {
75+
// \todo log error
76+
std::cerr << ba.what() << '\n';
77+
return nullptr;
78+
}
79+
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
//===------ test_sycl_event_interface.cpp - Test cases for event interface ===//
2+
//
3+
// Data Parallel Control (dpctl)
4+
//
5+
// Copyright 2020-2021 Intel Corporation
6+
//
7+
// Licensed under the Apache License, Version 2.0 (the "License");
8+
// you may not use this file except in compliance with the License.
9+
// You may obtain a copy of the License at
10+
//
11+
// http://www.apache.org/licenses/LICENSE-2.0
12+
//
13+
// Unless required by applicable law or agreed to in writing, software
14+
// distributed under the License is distributed on an "AS IS" BASIS,
15+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
// See the License for the specific language governing permissions and
17+
// limitations under the License.
18+
//
19+
//===----------------------------------------------------------------------===//
20+
///
21+
/// \file
22+
/// This file has unit test cases for functions defined in
23+
/// dpctl_sycl_event_interface.h.
24+
///
25+
//===----------------------------------------------------------------------===//
26+
27+
#include "Support/CBindingWrapping.h"
28+
#include "dpctl_sycl_event_interface.h"
29+
#include <CL/sycl.hpp>
30+
#include <gtest/gtest.h>
31+
32+
using namespace cl::sycl;
33+
34+
namespace
35+
{
36+
DEFINE_SIMPLE_CONVERSION_FUNCTIONS(event, DPCTLSyclEventRef)
37+
} // namespace
38+
39+
struct TestDPCTLSyclEventInterface : public ::testing::Test
40+
{
41+
DPCTLSyclEventRef ERef = nullptr;
42+
43+
TestDPCTLSyclEventInterface()
44+
{
45+
EXPECT_NO_FATAL_FAILURE(ERef = DPCTLEvent_Create());
46+
}
47+
48+
void SetUp()
49+
{
50+
ASSERT_TRUE(ERef);
51+
}
52+
53+
~TestDPCTLSyclEventInterface()
54+
{
55+
EXPECT_NO_FATAL_FAILURE(DPCTLEvent_Delete(ERef));
56+
}
57+
};
58+
59+
TEST_F(TestDPCTLSyclEventInterface, CheckEvent_Copy)
60+
{
61+
DPCTLSyclEventRef Copied_ERef = nullptr;
62+
EXPECT_NO_FATAL_FAILURE(Copied_ERef = DPCTLEvent_Copy(ERef));
63+
EXPECT_TRUE(bool(Copied_ERef));
64+
EXPECT_NO_FATAL_FAILURE(DPCTLEvent_Delete(Copied_ERef));
65+
}
66+
67+
TEST_F(TestDPCTLSyclEventInterface, CheckCopy_Invalid)
68+
{
69+
DPCTLSyclEventRef E1 = nullptr;
70+
DPCTLSyclEventRef E2 = nullptr;
71+
EXPECT_NO_FATAL_FAILURE(E2 = DPCTLEvent_Copy(E1));
72+
EXPECT_NO_FATAL_FAILURE(DPCTLEvent_Delete(E1));
73+
EXPECT_NO_FATAL_FAILURE(DPCTLEvent_Delete(E2));
74+
}

0 commit comments

Comments
 (0)