Skip to content

Commit 695616f

Browse files
Added size_t DPCTLContext_Hash(CtxRef)
1 parent ea1eba9 commit 695616f

File tree

3 files changed

+27
-0
lines changed

3 files changed

+27
-0
lines changed

dpctl-capi/include/dpctl_sycl_context_interface.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,4 +161,15 @@ DPCTLContext_GetBackend(__dpctl_keep const DPCTLSyclContextRef CtxRef);
161161
DPCTL_API
162162
void DPCTLContext_Delete(__dpctl_take DPCTLSyclContextRef CtxRef);
163163

164+
/*!
165+
* @brief Wrapper over
166+
* std::hash<sycl::context>'s operator()
167+
*
168+
* @param CtxRef The DPCTLSyclContextRef pointer.
169+
* @return Hash value of the underlying sycl::context instance.
170+
* @ingroup ContextInterface
171+
*/
172+
DPCTL_API
173+
size_t DPCTLContext_Hash(__dpctl_take DPCTLSyclContextRef CtxRef);
174+
164175
DPCTL_C_EXTERN_C_END

dpctl-capi/source/dpctl_sycl_context_interface.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,3 +197,17 @@ DPCTLContext_GetBackend(__dpctl_keep const DPCTLSyclContextRef CtxRef)
197197
return DPCTL_UNKNOWN_BACKEND;
198198
}
199199
}
200+
201+
size_t DPCTLContext_Hash(__dpctl_keep const DPCTLSyclContextRef CtxRef)
202+
{
203+
if (CtxRef) {
204+
auto C = unwrap(CtxRef);
205+
std::hash<context> hash_fn;
206+
return hash_fn(*C);
207+
}
208+
else {
209+
std::cerr << "Argument CtxRef is null"
210+
<< "/n";
211+
return 0;
212+
}
213+
}

dpctl-capi/tests/test_sycl_context_interface.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,8 @@ TEST_P(TestDPCTLContextInterface, ChkAreEq)
177177
EXPECT_NO_FATAL_FAILURE(are_not_eq = DPCTLContext_AreEq(CRef1, CRef3));
178178
EXPECT_TRUE(are_eq);
179179
EXPECT_FALSE(are_not_eq);
180+
EXPECT_TRUE(DPCTLContext_Hash(CRef1) == DPCTLContext_Hash(CRef2));
181+
EXPECT_FALSE(DPCTLContext_Hash(CRef1) == DPCTLContext_Hash(CRef3));
180182

181183
EXPECT_NO_FATAL_FAILURE(DPCTLContext_Delete(CRef1));
182184
EXPECT_NO_FATAL_FAILURE(DPCTLContext_Delete(CRef2));

0 commit comments

Comments
 (0)