Skip to content

Commit c077125

Browse files
uwedolinskymartygrant
authored andcommitted
[SYCL][NATIVECPU] more ur adapter implementations (#10972)
Adding more UR adapter functionality to pass more tests related to buffers and subbuffers, and to enable `sycl-ls --verbose`.
1 parent 0d302ab commit c077125

File tree

9 files changed

+169
-38
lines changed

9 files changed

+169
-38
lines changed

sycl/plugins/unified_runtime/ur/adapters/native_cpu/common.hpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,3 +40,16 @@ extern thread_local char ErrorMessage[MaxMessageSize];
4040
<< std::endl; \
4141
} \
4242
return UR_RESULT_ERROR_UNSUPPORTED_FEATURE;
43+
44+
// Base class to store common data
45+
struct _ur_object {
46+
ur_shared_mutex Mutex;
47+
};
48+
49+
struct RefCounted {
50+
std::atomic_uint32_t _refCount;
51+
void incrementReferenceCount() { _refCount++; }
52+
void decrementReferenceCount() { _refCount--; }
53+
RefCounted() : _refCount{1} {}
54+
uint32_t getReferenceCount() const { return _refCount; }
55+
};

sycl/plugins/unified_runtime/ur/adapters/native_cpu/device.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,10 +73,17 @@ UR_APIEXPORT ur_result_t UR_APICALL urDeviceGetInfo(ur_device_handle_t hDevice,
7373
return ReturnValue("0.0.0");
7474
case UR_DEVICE_INFO_VENDOR:
7575
return ReturnValue("Intel(R) Corporation");
76+
case UR_DEVICE_INFO_BACKEND_RUNTIME_VERSION:
77+
// TODO : CHECK
78+
return ReturnValue("0.0.0");
7679
case UR_DEVICE_INFO_IMAGE2D_MAX_WIDTH:
7780
return ReturnValue(size_t{8192});
7881
case UR_DEVICE_INFO_IMAGE2D_MAX_HEIGHT:
7982
return ReturnValue(size_t{8192});
83+
case UR_DEVICE_INFO_IMAGE_MAX_BUFFER_SIZE:
84+
return ReturnValue(size_t(65536 /*todo: min if aspect::image*/));
85+
case UR_DEVICE_INFO_MAX_SAMPLERS:
86+
return ReturnValue(uint32_t{16 /*todo: min if aspect::image*/});
8087
case UR_DEVICE_INFO_HOST_UNIFIED_MEMORY:
8188
return ReturnValue(bool{1});
8289
case UR_DEVICE_INFO_EXTENSIONS:
@@ -114,6 +121,20 @@ UR_APIEXPORT ur_result_t UR_APICALL urDeviceGetInfo(ur_device_handle_t hDevice,
114121
case UR_DEVICE_INFO_IMAGE3D_MAX_DEPTH:
115122
// Default minimum values required by the SYCL specification.
116123
return ReturnValue(size_t{2048});
124+
case UR_DEVICE_INFO_HALF_FP_CONFIG: {
125+
// todo:
126+
ur_device_fp_capability_flags_t HalfFPValue = 0;
127+
return ReturnValue(HalfFPValue);
128+
}
129+
case UR_DEVICE_INFO_SINGLE_FP_CONFIG: {
130+
// todo
131+
ur_device_fp_capability_flags_t SingleFPValue = 0;
132+
return ReturnValue(SingleFPValue);
133+
}
134+
case UR_DEVICE_INFO_DOUBLE_FP_CONFIG: {
135+
ur_device_fp_capability_flags_t DoubleFPValue = 0;
136+
return ReturnValue(DoubleFPValue);
137+
}
117138
case UR_DEVICE_INFO_MAX_WORK_ITEM_DIMENSIONS:
118139
return ReturnValue(uint32_t{3});
119140
case UR_DEVICE_INFO_PARTITION_TYPE:
@@ -193,6 +214,9 @@ UR_APIEXPORT ur_result_t UR_APICALL urDeviceGetInfo(ur_device_handle_t hDevice,
193214
case UR_DEVICE_INFO_GLOBAL_MEM_SIZE:
194215
// TODO : CHECK
195216
return ReturnValue(uint64_t{0});
217+
case UR_DEVICE_INFO_LOCAL_MEM_SIZE:
218+
// TODO : CHECK
219+
return ReturnValue(uint64_t{0});
196220
case UR_DEVICE_INFO_MAX_CONSTANT_BUFFER_SIZE:
197221
// TODO : CHECK
198222
return ReturnValue(uint64_t{0});

sycl/plugins/unified_runtime/ur/adapters/native_cpu/kernel.cpp

Lines changed: 51 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -69,10 +69,31 @@ UR_APIEXPORT ur_result_t UR_APICALL urKernelGetInfo(ur_kernel_handle_t hKernel,
6969
size_t *pPropSizeRet) {
7070
std::ignore = hKernel;
7171
std::ignore = propName;
72-
std::ignore = propSize;
7372
std::ignore = pPropValue;
74-
std::ignore = pPropSizeRet;
7573

74+
UrReturnHelper ReturnValue(propSize, pPropValue, pPropSizeRet);
75+
// todo: check if we need this
76+
// std::shared_lock<ur_shared_mutex> Guard(hKernel->Mutex);
77+
switch (propName) {
78+
// case UR_KERNEL_INFO_CONTEXT:
79+
// return ReturnValue(ur_context_handle_t{ hKernel->Program->Context });
80+
// case UR_KERNEL_INFO_PROGRAM:
81+
// return ReturnValue(ur_program_handle_t{ Kernel->Program });
82+
case UR_KERNEL_INFO_FUNCTION_NAME:
83+
if (hKernel->_name) {
84+
return ReturnValue(hKernel->_name);
85+
}
86+
return UR_RESULT_ERROR_INVALID_FUNCTION_NAME;
87+
// case UR_KERNEL_INFO_NUM_ARGS:
88+
// return ReturnValue(uint32_t{ Kernel->ZeKernelProperties->numKernelArgs
89+
// });
90+
case UR_KERNEL_INFO_REFERENCE_COUNT:
91+
return ReturnValue(uint32_t{hKernel->getReferenceCount()});
92+
case UR_KERNEL_INFO_ATTRIBUTES:
93+
return ReturnValue("");
94+
default:
95+
return UR_RESULT_ERROR_INVALID_VALUE;
96+
}
7697
DIE_NO_IMPLEMENTATION
7798
}
7899

@@ -92,7 +113,8 @@ urKernelGetGroupInfo(ur_kernel_handle_t hKernel, ur_device_handle_t hDevice,
92113
return returnValue(global_work_size, 3);
93114
}
94115
case UR_KERNEL_GROUP_INFO_WORK_GROUP_SIZE: {
95-
size_t max_threads = 0;
116+
// todo: set proper values
117+
size_t max_threads = 128;
96118
return returnValue(max_threads);
97119
}
98120
case UR_KERNEL_GROUP_INFO_COMPILE_WORK_GROUP_SIZE: {
@@ -104,7 +126,8 @@ urKernelGetGroupInfo(ur_kernel_handle_t hKernel, ur_device_handle_t hDevice,
104126
return returnValue(static_cast<uint64_t>(bytes));
105127
}
106128
case UR_KERNEL_GROUP_INFO_PREFERRED_WORK_GROUP_SIZE_MULTIPLE: {
107-
int warpSize = 0;
129+
// todo: set proper values
130+
int warpSize = 16;
108131
return returnValue(static_cast<size_t>(warpSize));
109132
}
110133
case UR_KERNEL_GROUP_INFO_PRIVATE_MEM_SIZE: {
@@ -125,17 +148,34 @@ urKernelGetSubGroupInfo(ur_kernel_handle_t hKernel, ur_device_handle_t hDevice,
125148
void *pPropValue, size_t *pPropSizeRet) {
126149
std::ignore = hKernel;
127150
std::ignore = hDevice;
128-
std::ignore = propName;
129-
std::ignore = propSize;
130-
std::ignore = pPropValue;
131-
std::ignore = pPropSizeRet;
132151

133-
DIE_NO_IMPLEMENTATION
152+
UrReturnHelper ReturnValue(propSize, pPropValue, pPropSizeRet);
153+
switch (propName) {
154+
case UR_KERNEL_SUB_GROUP_INFO_MAX_SUB_GROUP_SIZE: {
155+
// todo: set proper values
156+
int WarpSize = 8;
157+
return ReturnValue(static_cast<uint32_t>(WarpSize));
158+
}
159+
case UR_KERNEL_SUB_GROUP_INFO_MAX_NUM_SUB_GROUPS: {
160+
// todo: set proper values
161+
int MaxWarps = 32;
162+
return ReturnValue(static_cast<uint32_t>(MaxWarps));
163+
}
164+
case UR_KERNEL_SUB_GROUP_INFO_COMPILE_NUM_SUB_GROUPS: {
165+
// todo: set proper values
166+
return ReturnValue(0);
167+
}
168+
case UR_KERNEL_SUB_GROUP_INFO_SUB_GROUP_SIZE_INTEL: {
169+
// todo: set proper values
170+
return ReturnValue(0);
171+
}
172+
}
173+
DIE_NO_IMPLEMENTATION;
134174
}
135175

136176
UR_APIEXPORT ur_result_t UR_APICALL urKernelRetain(ur_kernel_handle_t hKernel) {
137-
std::ignore = hKernel;
138-
DIE_NO_IMPLEMENTATION
177+
hKernel->incrementReferenceCount();
178+
return UR_RESULT_SUCCESS;
139179
}
140180

141181
UR_APIEXPORT ur_result_t UR_APICALL

sycl/plugins/unified_runtime/ur/adapters/native_cpu/kernel.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
#pragma once
1010

11+
#include "common.hpp"
1112
#include <sycl/detail/native_cpu.hpp>
1213
#include <ur_api.h>
1314

@@ -16,7 +17,7 @@ using nativecpu_kernel_t = void(const sycl::detail::NativeCPUArgDesc *,
1617
using nativecpu_ptr_t = nativecpu_kernel_t *;
1718
using nativecpu_task_t = std::function<nativecpu_kernel_t>;
1819

19-
struct ur_kernel_handle_t_ {
20+
struct ur_kernel_handle_t_ : RefCounted {
2021

2122
ur_kernel_handle_t_(const char *name, nativecpu_task_t subhandler)
2223
: _name{name}, _subhandler{subhandler} {}

sycl/plugins/unified_runtime/ur/adapters/native_cpu/memory.cpp

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,11 @@ UR_APIEXPORT ur_result_t UR_APICALL urMemBufferCreate(
4949
ur_mem_handle_t_ *retMem;
5050

5151
if (useHostPtr) {
52-
retMem = new ur_mem_handle_t_(pProperties->pHost);
52+
retMem = new _ur_buffer(hContext, pProperties->pHost);
5353
} else if (copyHostPtr) {
54-
retMem = new ur_mem_handle_t_(pProperties->pHost, size);
54+
retMem = new _ur_buffer(hContext, pProperties->pHost, size);
5555
} else {
56-
retMem = new ur_mem_handle_t_(size);
56+
retMem = new _ur_buffer(hContext, size);
5757
}
5858

5959
*phBuffer = retMem;
@@ -82,13 +82,30 @@ UR_APIEXPORT ur_result_t UR_APICALL urMemBufferPartition(
8282
ur_mem_handle_t hBuffer, ur_mem_flags_t flags,
8383
ur_buffer_create_type_t bufferCreateType, const ur_buffer_region_t *pRegion,
8484
ur_mem_handle_t *phMem) {
85-
std::ignore = hBuffer;
86-
std::ignore = flags;
85+
8786
std::ignore = bufferCreateType;
88-
std::ignore = pRegion;
89-
std::ignore = phMem;
87+
UR_ASSERT(hBuffer && !hBuffer->isImage() &&
88+
!(static_cast<_ur_buffer *>(hBuffer))->isSubBuffer(),
89+
UR_RESULT_ERROR_INVALID_MEM_OBJECT);
9090

91-
DIE_NO_IMPLEMENTATION
91+
std::shared_lock<ur_shared_mutex> Guard(hBuffer->Mutex);
92+
93+
if (flags != UR_MEM_FLAG_READ_WRITE) {
94+
die("urMemBufferPartition: NativeCPU implements only read-write buffer,"
95+
"no read-only or write-only yet.");
96+
}
97+
98+
try {
99+
auto partitionedBuffer = new _ur_buffer(static_cast<_ur_buffer *>(hBuffer),
100+
pRegion->origin, pRegion->size);
101+
*phMem = reinterpret_cast<ur_mem_handle_t>(partitionedBuffer);
102+
} catch (const std::bad_alloc &) {
103+
return UR_RESULT_ERROR_OUT_OF_HOST_MEMORY;
104+
} catch (...) {
105+
return UR_RESULT_ERROR_UNKNOWN;
106+
}
107+
108+
return UR_RESULT_SUCCESS;
92109
}
93110

94111
UR_APIEXPORT ur_result_t UR_APICALL

sycl/plugins/unified_runtime/ur/adapters/native_cpu/memory.hpp

Lines changed: 43 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,23 @@
1212
#include <cstdlib>
1313
#include <cstring>
1414

15-
struct ur_mem_handle_t_ {
16-
ur_mem_handle_t_(size_t Size)
17-
: _mem{static_cast<char *>(malloc(Size))}, _ownsMem{true}, _refCount{1} {}
15+
#include "common.hpp"
16+
#include "context.hpp"
1817

19-
ur_mem_handle_t_(void *HostPtr, size_t Size)
20-
: _mem{static_cast<char *>(malloc(Size))}, _ownsMem{true}, _refCount{1} {
18+
struct ur_mem_handle_t_ : _ur_object {
19+
ur_mem_handle_t_(size_t Size, bool _IsImage)
20+
: _mem{static_cast<char *>(malloc(Size))}, _ownsMem{true},
21+
IsImage{_IsImage} {}
22+
23+
ur_mem_handle_t_(void *HostPtr, size_t Size, bool _IsImage)
24+
: _mem{static_cast<char *>(malloc(Size))}, _ownsMem{true},
25+
IsImage{_IsImage} {
2126
memcpy(_mem, HostPtr, Size);
2227
}
2328

24-
ur_mem_handle_t_(void *HostPtr)
25-
: _mem{static_cast<char *>(HostPtr)}, _ownsMem{false}, _refCount{1} {}
29+
ur_mem_handle_t_(void *HostPtr, bool _IsImage)
30+
: _mem{static_cast<char *>(HostPtr)}, _ownsMem{false}, IsImage{_IsImage} {
31+
}
2632

2733
~ur_mem_handle_t_() {
2834
if (_ownsMem) {
@@ -32,7 +38,36 @@ struct ur_mem_handle_t_ {
3238

3339
void decrementRefCount() noexcept { _refCount--; }
3440

41+
// Method to get type of the derived object (image or buffer)
42+
bool isImage() const { return this->IsImage; }
43+
3544
char *_mem;
3645
bool _ownsMem;
37-
std::atomic_uint32_t _refCount;
46+
std::atomic_uint32_t _refCount = {1};
47+
48+
private:
49+
const bool IsImage;
50+
};
51+
52+
struct _ur_buffer final : ur_mem_handle_t_ {
53+
// Buffer constructor
54+
_ur_buffer(ur_context_handle_t /* Context*/, void *HostPtr)
55+
: ur_mem_handle_t_(HostPtr, false) {}
56+
_ur_buffer(ur_context_handle_t /* Context*/, void *HostPtr, size_t Size)
57+
: ur_mem_handle_t_(HostPtr, Size, false) {}
58+
_ur_buffer(ur_context_handle_t /* Context*/, size_t Size)
59+
: ur_mem_handle_t_(Size, false) {}
60+
_ur_buffer(_ur_buffer *b, size_t Offset, size_t Size)
61+
: ur_mem_handle_t_(b->_mem + Offset, false), SubBuffer(b) {
62+
SubBuffer.Origin = Offset;
63+
}
64+
65+
bool isSubBuffer() const { return SubBuffer.Parent != nullptr; }
66+
67+
struct BB {
68+
BB(_ur_buffer *b) : Parent(b) {}
69+
BB() : BB(nullptr) {}
70+
_ur_buffer *const Parent;
71+
size_t Origin; // only valid if Parent != nullptr
72+
} SubBuffer;
3873
};

sycl/plugins/unified_runtime/ur/adapters/native_cpu/program.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,9 +87,8 @@ urProgramLink(ur_context_handle_t hContext, uint32_t count,
8787

8888
UR_APIEXPORT ur_result_t UR_APICALL
8989
urProgramRetain(ur_program_handle_t hProgram) {
90-
std::ignore = hProgram;
91-
92-
DIE_NO_IMPLEMENTATION
90+
hProgram->incrementReferenceCount();
91+
return UR_RESULT_SUCCESS;
9392
}
9493

9594
UR_APIEXPORT ur_result_t UR_APICALL

sycl/plugins/unified_runtime/ur/adapters/native_cpu/program.hpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,14 @@
1313
#include "context.hpp"
1414
#include <map>
1515

16-
struct ur_program_handle_t_ {
16+
struct ur_program_handle_t_ : RefCounted {
1717
ur_program_handle_t_(ur_context_handle_t ctx, const unsigned char *pBinary)
18-
: _ctx{ctx}, _ptr{pBinary}, _refCount{1} {}
18+
: _ctx{ctx}, _ptr{pBinary} {}
1919

2020
uint32_t getReferenceCount() const noexcept { return _refCount; }
2121

2222
ur_context_handle_t _ctx;
2323
const unsigned char *_ptr;
24-
std::atomic_uint32_t _refCount;
25-
2624
struct _compare {
2725
bool operator()(char const *a, char const *b) const {
2826
return std::strcmp(a, b) < 0;

sycl/plugins/unified_runtime/ur/adapters/native_cpu/usm.cpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,14 @@ urUSMSharedAlloc(ur_context_handle_t hContext, ur_device_handle_t hDevice,
5252
std::ignore = hDevice;
5353
std::ignore = pUSMDesc;
5454
std::ignore = pool;
55-
std::ignore = size;
56-
std::ignore = ppMem;
5755

58-
DIE_NO_IMPLEMENTATION;
56+
UR_ASSERT(ppMem, UR_RESULT_ERROR_INVALID_NULL_POINTER);
57+
// TODO: Check Max size when UR_DEVICE_INFO_MAX_MEM_ALLOC_SIZE is implemented
58+
UR_ASSERT(size > 0, UR_RESULT_ERROR_INVALID_USM_SIZE);
59+
60+
*ppMem = malloc(size);
61+
62+
return UR_RESULT_SUCCESS;
5963
}
6064

6165
UR_APIEXPORT ur_result_t UR_APICALL urUSMFree(ur_context_handle_t hContext,

0 commit comments

Comments
 (0)