Skip to content

Commit 97e11c4

Browse files
Add DPCTLEvent_GetWaitList func
1 parent 3475ed4 commit 97e11c4

File tree

3 files changed

+107
-1
lines changed

3 files changed

+107
-1
lines changed

dpctl-capi/include/dpctl_sycl_event_interface.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,18 @@
3131
#include "dpctl_data_types.h"
3232
#include "dpctl_sycl_enum_types.h"
3333
#include "dpctl_sycl_types.h"
34+
#include "dpctl_vector.h"
3435

3536
DPCTL_C_EXTERN_C_BEGIN
3637

3738
/**
3839
* @defgroup EventInterface Event class C wrapper
3940
*/
4041

42+
// Declares a set of types and functions to deal with vectors of
43+
// DPCTLSyclEventRef. Refer dpctl_vector_macros.h
44+
DPCTL_DECLARE_VECTOR(Event)
45+
4146
/*!
4247
* @brief A wrapper for ``sycl::event`` contructor to construct a new event.
4348
*
@@ -91,4 +96,16 @@ DPCTLEvent_Copy(__dpctl_keep const DPCTLSyclEventRef ERef);
9196
DPCTL_API
9297
DPCTLSyclBackendType DPCTLEvent_GetBackend(__dpctl_keep DPCTLSyclEventRef ERef);
9398

99+
/*!
100+
* @brief C-API wrapper for ``sycl::event::get_wait_list``.
101+
* Returns a vector of events that this event still waits for.
102+
*
103+
* @param ERef Opaque pointer to a ``sycl::event``.
104+
* @return A DPCTLEventVectorRef of DPCTLSyclEventRef objects.
105+
* @ingroup EventInterface
106+
*/
107+
DPCTL_API
108+
__dpctl_give DPCTLEventVectorRef
109+
DPCTLEvent_GetWaitList(__dpctl_keep DPCTLSyclEventRef ERef);
110+
94111
DPCTL_C_EXTERN_C_END

dpctl-capi/source/dpctl_sycl_event_interface.cpp

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,11 @@ namespace
3737
DEFINE_SIMPLE_CONVERSION_FUNCTIONS(event, DPCTLSyclEventRef)
3838
} /* end of anonymous namespace */
3939

40+
#undef EL
41+
#define EL Event
42+
#include "dpctl_vector_templ.cpp"
43+
#undef EL
44+
4045
__dpctl_give DPCTLSyclEventRef DPCTLEvent_Create()
4146
{
4247
DPCTLSyclEventRef ERef = nullptr;
@@ -98,3 +103,39 @@ DPCTLSyclBackendType DPCTLEvent_GetBackend(__dpctl_keep DPCTLSyclEventRef ERef)
98103
}
99104
return BTy;
100105
}
106+
107+
__dpctl_give DPCTLEventVectorRef
108+
DPCTLEvent_GetWaitList(__dpctl_keep DPCTLSyclEventRef ERef)
109+
{
110+
auto E = unwrap(ERef);
111+
if (!E) {
112+
std::cerr << "Cannot get wait list as input is a nullptr\n";
113+
return nullptr;
114+
}
115+
vector_class<DPCTLSyclEventRef> *EventsVectorPtr = nullptr;
116+
try {
117+
EventsVectorPtr = new vector_class<DPCTLSyclEventRef>();
118+
} catch (std::bad_alloc const &ba) {
119+
// \todo log error
120+
std::cerr << ba.what() << '\n';
121+
return nullptr;
122+
}
123+
try {
124+
auto Events = E->get_wait_list();
125+
EventsVectorPtr->reserve(Events.size());
126+
for (const auto &Ev : Events) {
127+
EventsVectorPtr->emplace_back(wrap(new event(Ev)));
128+
}
129+
return wrap(EventsVectorPtr);
130+
} catch (std::bad_alloc const &ba) {
131+
delete EventsVectorPtr;
132+
// \todo log error
133+
std::cerr << ba.what() << '\n';
134+
return nullptr;
135+
} catch (const runtime_error &re) {
136+
delete EventsVectorPtr;
137+
// \todo log error
138+
std::cerr << re.what() << '\n';
139+
return nullptr;
140+
}
141+
}

dpctl-capi/tests/test_sycl_event_interface.cpp

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,39 @@
2626

2727
#include "Support/CBindingWrapping.h"
2828
#include "dpctl_sycl_event_interface.h"
29+
#include "dpctl_sycl_types.h"
2930
#include <CL/sycl.hpp>
3031
#include <gtest/gtest.h>
3132

3233
using namespace cl::sycl;
3334

3435
namespace
3536
{
36-
DEFINE_SIMPLE_CONVERSION_FUNCTIONS(event, DPCTLSyclEventRef)
37+
DEFINE_SIMPLE_CONVERSION_FUNCTIONS(event, DPCTLSyclEventRef);
38+
DEFINE_SIMPLE_CONVERSION_FUNCTIONS(vector_class<DPCTLSyclEventRef>,
39+
DPCTLEventVectorRef);
40+
41+
#ifndef DPCTL_COVERAGE
42+
sycl::event produce_event(sycl::queue &Q, sycl::buffer<int> &data)
43+
{
44+
int N = data.get_range()[0];
45+
46+
auto e1 = Q.submit([&](sycl::handler &h) {
47+
sycl::accessor a{data, h, sycl::write_only, sycl::noinit};
48+
h.parallel_for(N, [=](sycl::id<1> i) { a[i] = 1; });
49+
});
50+
51+
auto e2 = Q.submit([&](sycl::handler &h) {
52+
sycl::accessor a{data, h};
53+
h.single_task([=]() {
54+
for (int i = 1; i < N; i++)
55+
a[0] += a[i];
56+
});
57+
});
58+
59+
return e2;
60+
}
61+
#endif
3762
} // namespace
3863

3964
struct TestDPCTLSyclEventInterface : public ::testing::Test
@@ -112,3 +137,26 @@ TEST_F(TestDPCTLSyclEventInterface, CheckGetBackend_Invalid)
112137
EXPECT_NO_FATAL_FAILURE(Bty = DPCTLEvent_GetBackend(E));
113138
EXPECT_TRUE(Bty == DPCTL_UNKNOWN_BACKEND);
114139
}
140+
141+
TEST_F(TestDPCTLSyclEventInterface, CheckGetWaitList)
142+
{
143+
DPCTLEventVectorRef EVRef = nullptr;
144+
EXPECT_NO_FATAL_FAILURE(EVRef = DPCTLEvent_GetWaitList(ERef));
145+
ASSERT_TRUE(EVRef);
146+
EXPECT_NO_FATAL_FAILURE(DPCTLEventVector_Clear(EVRef));
147+
EXPECT_NO_FATAL_FAILURE(DPCTLEventVector_Delete(EVRef));
148+
}
149+
150+
#ifndef DPCTL_COVERAGE
151+
TEST_F(TestDPCTLSyclEventInterface, CheckGetWaitListSYCL)
152+
{
153+
sycl::queue q;
154+
sycl::buffer<int> data{42};
155+
156+
auto eD = produce_event(q, data);
157+
DPCTLSyclEventRef ERef = reinterpret_cast<DPCTLSyclEventRef>(&eD);
158+
auto EVRef = DPCTLEvent_GetWaitList(ERef);
159+
ASSERT_TRUE(DPCTLEventVector_Size(EVRef) > 0);
160+
DPCTLEventVector_Delete(EVRef);
161+
}
162+
#endif

0 commit comments

Comments
 (0)