Skip to content

Commit f1239b9

Browse files
Implementation of memset function in libSyclInterface (#812)
1 parent 4014a22 commit f1239b9

File tree

3 files changed

+86
-0
lines changed

3 files changed

+86
-0
lines changed

libsyclinterface/include/dpctl_sycl_queue_interface.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -385,4 +385,21 @@ DPCTLSyclEventRef DPCTLQueue_SubmitBarrierForEvents(
385385
__dpctl_keep const DPCTLSyclEventRef *DepEvents,
386386
size_t NDepEvents);
387387

388+
/*!
389+
* @brief C-API wrapper for ``sycl::queue::memset``.
390+
*
391+
* @param QRef An opaque pointer to the ``sycl::queue``.
392+
* @param USMRef An USM pointer to the memory to fill.
393+
* @param Value A value to fill.
394+
* @param Count A number of uint8_t elements to fill.
395+
* @return An opaque pointer to the ``sycl::event`` returned by the
396+
* ``sycl::queue::fill`` function.
397+
* @ingroup QueueInterface
398+
*/
399+
DPCTL_API
400+
DPCTLSyclEventRef DPCTLQueue_Memset(__dpctl_keep const DPCTLSyclQueueRef QRef,
401+
void *USMRef,
402+
uint8_t Value,
403+
size_t Count);
404+
388405
DPCTL_C_EXTERN_C_END

libsyclinterface/source/dpctl_sycl_queue_interface.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -612,3 +612,26 @@ DPCTLQueue_SubmitBarrier(__dpctl_keep const DPCTLSyclQueueRef QRef)
612612
{
613613
return DPCTLQueue_SubmitBarrierForEvents(QRef, nullptr, 0);
614614
}
615+
616+
DPCTLSyclEventRef DPCTLQueue_Memset(__dpctl_keep const DPCTLSyclQueueRef QRef,
617+
void *USMRef,
618+
uint8_t Value,
619+
size_t Count)
620+
{
621+
auto Q = unwrap(QRef);
622+
if (Q && USMRef) {
623+
sycl::event ev;
624+
try {
625+
ev = Q->memset(USMRef, static_cast<int>(Value), Count);
626+
} catch (std::exception const &e) {
627+
error_handler(e, __FILE__, __func__, __LINE__);
628+
return nullptr;
629+
}
630+
return wrap(new event(ev));
631+
}
632+
else {
633+
error_handler("QRef or USMRef passed to fill8 were NULL.", __FILE__,
634+
__func__, __LINE__);
635+
return nullptr;
636+
}
637+
}

libsyclinterface/tests/test_sycl_queue_interface.cpp

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
#include "dpctl_sycl_event_interface.h"
3333
#include "dpctl_sycl_queue_interface.h"
3434
#include "dpctl_sycl_queue_manager.h"
35+
#include "dpctl_sycl_usm_interface.h"
3536
#include <CL/sycl.hpp>
3637
#include <gtest/gtest.h>
3738

@@ -407,6 +408,51 @@ TEST_P(TestDPCTLQueueMemberFunctions, CheckMemOpsNullPtr)
407408
}
408409
}
409410

411+
TEST(TestDPCTLSyclQueueInterface, CheckMemsetNullQRef)
412+
{
413+
DPCTLSyclQueueRef QRef = nullptr;
414+
void *p = nullptr;
415+
uint8_t val8 = 0;
416+
DPCTLSyclEventRef ERef = nullptr;
417+
418+
ASSERT_NO_FATAL_FAILURE(ERef = DPCTLQueue_Memset(QRef, p, val8, 1));
419+
ASSERT_FALSE(bool(ERef));
420+
}
421+
422+
TEST_P(TestDPCTLQueueMemberFunctions, CheckMemset)
423+
{
424+
DPCTLSyclUSMRef p = nullptr;
425+
DPCTLSyclEventRef ERef = nullptr;
426+
uint8_t val = 73;
427+
size_t nbytes = 256;
428+
uint8_t *host_arr = new uint8_t[nbytes];
429+
430+
ASSERT_FALSE(host_arr == nullptr);
431+
432+
ASSERT_NO_FATAL_FAILURE(p = DPCTLmalloc_device(nbytes, QRef));
433+
ASSERT_FALSE(p == nullptr);
434+
435+
ASSERT_NO_FATAL_FAILURE(
436+
ERef = DPCTLQueue_Memset(QRef, (void *)p, val, nbytes));
437+
ASSERT_NO_FATAL_FAILURE(DPCTLEvent_Wait(ERef));
438+
ASSERT_NO_FATAL_FAILURE(DPCTLEvent_Delete(ERef));
439+
440+
ERef = nullptr;
441+
442+
ASSERT_NO_FATAL_FAILURE(ERef =
443+
DPCTLQueue_Memcpy(QRef, host_arr, p, nbytes));
444+
ASSERT_NO_FATAL_FAILURE(DPCTLEvent_Wait(ERef));
445+
ASSERT_NO_FATAL_FAILURE(DPCTLEvent_Delete(ERef));
446+
447+
ASSERT_NO_FATAL_FAILURE(DPCTLfree_with_queue(p, QRef));
448+
449+
bool equal = true;
450+
for (size_t i = 0; i < nbytes; ++i) {
451+
ASSERT_TRUE(host_arr[i] == val);
452+
}
453+
delete[] host_arr;
454+
}
455+
410456
INSTANTIATE_TEST_SUITE_P(
411457
DPCTLQueueMemberFuncTests,
412458
TestDPCTLQueueMemberFunctions,

0 commit comments

Comments
 (0)