Skip to content

Commit d599d13

Browse files
committed
[OpenCL] Add usm_alloc_location_desc struct and handle it in the adapter.
This is the equivalent of buffer_alloc_location_properties_t, it turns out the CL extension flag is applicable to both buffer creation and USM allocations.
1 parent 41228d3 commit d599d13

File tree

9 files changed

+193
-72
lines changed

9 files changed

+193
-72
lines changed

include/ur.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,7 @@ class ur_structure_type_v(IntEnum):
242242
KERNEL_EXEC_INFO_PROPERTIES = 31 ## ::ur_kernel_exec_info_properties_t
243243
KERNEL_ARG_VALUE_PROPERTIES = 32 ## ::ur_kernel_arg_value_properties_t
244244
KERNEL_ARG_LOCAL_PROPERTIES = 33 ## ::ur_kernel_arg_local_properties_t
245+
USM_ALLOC_LOCATION_DESC = 35 ## ::ur_usm_alloc_location_desc_t
245246
EXP_COMMAND_BUFFER_DESC = 0x1000 ## ::ur_exp_command_buffer_desc_t
246247
EXP_SAMPLER_MIP_PROPERTIES = 0x2000 ## ::ur_exp_sampler_mip_properties_t
247248
EXP_INTEROP_MEM_DESC = 0x2001 ## ::ur_exp_interop_mem_desc_t
@@ -1531,6 +1532,25 @@ class ur_usm_device_desc_t(Structure):
15311532
("flags", ur_usm_device_mem_flags_t) ## [in] device memory allocation flags.
15321533
]
15331534

1535+
###############################################################################
1536+
## @brief USM allocation location desc
1537+
##
1538+
## @details
1539+
## - Specify these properties in ::urUSMHostAlloc, ::urUSMDeviceAlloc and
1540+
## ::urUSMSharedAlloc via ::ur_usm_desc_t as part of a `pNext` chain.
1541+
##
1542+
## @remarks
1543+
## _Analogues_
1544+
## - cl_intel_mem_alloc_buffer_location
1545+
class ur_usm_alloc_location_desc_t(Structure):
1546+
_fields_ = [
1547+
("stype", ur_structure_type_t), ## [in] type of this structure, must be
1548+
## ::UR_STRUCTURE_TYPE_USM_ALLOC_LOCATION_DESC
1549+
("pNext", c_void_p), ## [in][optional] pointer to extension-specific structure
1550+
("location", c_ulong) ## [in] Identifies the ID of global memory partition to which the memory
1551+
## should be allocated.
1552+
]
1553+
15341554
###############################################################################
15351555
## @brief USM pool descriptor type
15361556
class ur_usm_pool_desc_t(Structure):

include/ur_api.h

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,7 @@ typedef enum ur_structure_type_t {
251251
UR_STRUCTURE_TYPE_KERNEL_EXEC_INFO_PROPERTIES = 31, ///< ::ur_kernel_exec_info_properties_t
252252
UR_STRUCTURE_TYPE_KERNEL_ARG_VALUE_PROPERTIES = 32, ///< ::ur_kernel_arg_value_properties_t
253253
UR_STRUCTURE_TYPE_KERNEL_ARG_LOCAL_PROPERTIES = 33, ///< ::ur_kernel_arg_local_properties_t
254+
UR_STRUCTURE_TYPE_USM_ALLOC_LOCATION_DESC = 35, ///< ::ur_usm_alloc_location_desc_t
254255
UR_STRUCTURE_TYPE_EXP_COMMAND_BUFFER_DESC = 0x1000, ///< ::ur_exp_command_buffer_desc_t
255256
UR_STRUCTURE_TYPE_EXP_SAMPLER_MIP_PROPERTIES = 0x2000, ///< ::ur_exp_sampler_mip_properties_t
256257
UR_STRUCTURE_TYPE_EXP_INTEROP_MEM_DESC = 0x2001, ///< ::ur_exp_interop_mem_desc_t
@@ -3221,6 +3222,25 @@ typedef struct ur_usm_device_desc_t {
32213222

32223223
} ur_usm_device_desc_t;
32233224

3225+
///////////////////////////////////////////////////////////////////////////////
3226+
/// @brief USM allocation location desc
3227+
///
3228+
/// @details
3229+
/// - Specify these properties in ::urUSMHostAlloc, ::urUSMDeviceAlloc and
3230+
/// ::urUSMSharedAlloc via ::ur_usm_desc_t as part of a `pNext` chain.
3231+
///
3232+
/// @remarks
3233+
/// _Analogues_
3234+
/// - cl_intel_mem_alloc_buffer_location
3235+
typedef struct ur_usm_alloc_location_desc_t {
3236+
ur_structure_type_t stype; ///< [in] type of this structure, must be
3237+
///< ::UR_STRUCTURE_TYPE_USM_ALLOC_LOCATION_DESC
3238+
const void *pNext; ///< [in][optional] pointer to extension-specific structure
3239+
uint32_t location; ///< [in] Identifies the ID of global memory partition to which the memory
3240+
///< should be allocated.
3241+
3242+
} ur_usm_alloc_location_desc_t;
3243+
32243244
///////////////////////////////////////////////////////////////////////////////
32253245
/// @brief USM pool descriptor type
32263246
typedef struct ur_usm_pool_desc_t {
@@ -3258,6 +3278,7 @@ typedef struct ur_usm_pool_limits_desc_t {
32583278
/// - Any flags/hints passed through pUSMDesc only affect the single
32593279
/// allocation.
32603280
/// - See also ::ur_usm_host_desc_t.
3281+
/// - See also ::ur_usm_alloc_location_desc_t.
32613282
///
32623283
/// @returns
32633284
/// - ::UR_RESULT_SUCCESS
@@ -3301,6 +3322,7 @@ urUSMHostAlloc(
33013322
/// - Any flags/hints passed through pUSMDesc only affect the single
33023323
/// allocation.
33033324
/// - See also ::ur_usm_device_desc_t.
3325+
/// - See also ::ur_usm_alloc_location_desc_t.
33043326
///
33053327
/// @returns
33063328
/// - ::UR_RESULT_SUCCESS
@@ -3347,6 +3369,7 @@ urUSMDeviceAlloc(
33473369
/// allocation.
33483370
/// - See also ::ur_usm_host_desc_t.
33493371
/// - See also ::ur_usm_device_desc_t.
3372+
/// - See also ::ur_usm_alloc_location_desc_t.
33503373
///
33513374
/// @returns
33523375
/// - ::UR_RESULT_SUCCESS

scripts/core/registry.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -645,3 +645,6 @@ etors:
645645
- name: KERNEL_ARG_LOCAL_PROPERTIES
646646
desc: $x_kernel_arg_local_properties_t
647647
value: '33'
648+
- name: USM_ALLOC_LOCATION_DESC
649+
desc: $x_usm_alloc_location_desc_t
650+
value: '35'

scripts/core/usm.yml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,23 @@ members:
175175
desc: "[in] device memory allocation flags."
176176
--- #--------------------------------------------------------------------------
177177
type: struct
178+
desc: "USM allocation location desc"
179+
details:
180+
- Specify these properties in $xUSMHostAlloc, $xUSMDeviceAlloc and
181+
$xUSMSharedAlloc via $x_usm_desc_t as part of a `pNext` chain.
182+
analogue:
183+
- "cl_intel_mem_alloc_buffer_location"
184+
class: $xUSM
185+
name: $x_usm_alloc_location_desc_t
186+
base: $x_base_desc_t
187+
members:
188+
- type: uint32_t
189+
name: location
190+
desc: >
191+
[in] Identifies the ID of global memory partition to which the memory
192+
should be allocated.
193+
--- #--------------------------------------------------------------------------
194+
type: struct
178195
desc: "USM pool descriptor type"
179196
class: $xUSM
180197
name: $x_usm_pool_desc_t
@@ -212,6 +229,7 @@ details:
212229
- "Allocations served from different memory pools must be isolated and must not reside on the same page."
213230
- "Any flags/hints passed through pUSMDesc only affect the single allocation."
214231
- "See also $x_usm_host_desc_t."
232+
- "See also $x_usm_alloc_location_desc_t."
215233
params:
216234
- type: $x_context_handle_t
217235
name: hContext
@@ -253,6 +271,7 @@ details:
253271
- "Allocations served from different memory pools must be isolated and must not reside on the same page."
254272
- "Any flags/hints passed through pUSMDesc only affect the single allocation."
255273
- "See also $x_usm_device_desc_t."
274+
- "See also $x_usm_alloc_location_desc_t."
256275
params:
257276
- type: $x_context_handle_t
258277
name: hContext
@@ -298,6 +317,7 @@ details:
298317
- "Any flags/hints passed through pUSMDesc only affect the single allocation."
299318
- "See also $x_usm_host_desc_t."
300319
- "See also $x_usm_device_desc_t."
320+
- "See also $x_usm_alloc_location_desc_t."
301321
params:
302322
- type: $x_context_handle_t
303323
name: hContext

source/adapters/opencl/usm.cpp

Lines changed: 88 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -10,30 +10,86 @@
1010

1111
#include "common.hpp"
1212

13+
inline cl_mem_alloc_flags_intel
14+
hostDescToClFlags(const ur_usm_host_desc_t &desc) {
15+
cl_mem_alloc_flags_intel allocFlags = 0;
16+
if (desc.flags & UR_USM_HOST_MEM_FLAG_INITIAL_PLACEMENT) {
17+
allocFlags |= CL_MEM_ALLOC_INITIAL_PLACEMENT_HOST_INTEL;
18+
}
19+
return allocFlags;
20+
}
21+
22+
inline cl_mem_alloc_flags_intel
23+
deviceDescToClFlags(const ur_usm_device_desc_t &desc) {
24+
cl_mem_alloc_flags_intel allocFlags = 0;
25+
if (desc.flags & UR_USM_DEVICE_MEM_FLAG_INITIAL_PLACEMENT) {
26+
allocFlags |= CL_MEM_ALLOC_INITIAL_PLACEMENT_DEVICE_INTEL;
27+
}
28+
if (desc.flags & UR_USM_DEVICE_MEM_FLAG_WRITE_COMBINED) {
29+
allocFlags |= CL_MEM_ALLOC_WRITE_COMBINED_INTEL;
30+
}
31+
return allocFlags;
32+
}
33+
34+
ur_result_t
35+
usmDescToCLMemProperties(const ur_base_desc_t *Desc,
36+
std::vector<cl_mem_properties_intel> &Properties) {
37+
cl_mem_alloc_flags_intel AllocFlags = 0;
38+
const auto *Next = Desc;
39+
do {
40+
switch (Next->stype) {
41+
case UR_STRUCTURE_TYPE_USM_HOST_DESC: {
42+
auto HostDesc = reinterpret_cast<const ur_usm_host_desc_t *>(Next);
43+
if (UR_USM_HOST_MEM_FLAGS_MASK & HostDesc->flags) {
44+
return UR_RESULT_ERROR_INVALID_ENUMERATION;
45+
}
46+
AllocFlags |= hostDescToClFlags(*HostDesc);
47+
break;
48+
}
49+
case UR_STRUCTURE_TYPE_USM_DEVICE_DESC: {
50+
auto DeviceDesc = reinterpret_cast<const ur_usm_device_desc_t *>(Next);
51+
if (UR_USM_HOST_MEM_FLAGS_MASK & DeviceDesc->flags) {
52+
return UR_RESULT_ERROR_INVALID_ENUMERATION;
53+
}
54+
AllocFlags |= deviceDescToClFlags(*DeviceDesc);
55+
break;
56+
}
57+
case UR_STRUCTURE_TYPE_USM_ALLOC_LOCATION_DESC: {
58+
auto LocationDesc =
59+
reinterpret_cast<const ur_usm_alloc_location_desc_t *>(Next);
60+
Properties.push_back(CL_MEM_ALLOC_BUFFER_LOCATION_INTEL);
61+
// CL bitfields are cl_ulong
62+
Properties.push_back(static_cast<cl_ulong>(LocationDesc->location));
63+
break;
64+
}
65+
default:
66+
return UR_RESULT_ERROR_INVALID_VALUE;
67+
}
68+
69+
Next = Next->pNext ? static_cast<const ur_base_desc_t *>(Next->pNext)
70+
: nullptr;
71+
} while (Next);
72+
73+
if (AllocFlags) {
74+
Properties.push_back(CL_MEM_ALLOC_FLAGS_INTEL);
75+
Properties.push_back(AllocFlags);
76+
}
77+
Properties.push_back(0);
78+
79+
return UR_RESULT_SUCCESS;
80+
}
81+
1382
UR_APIEXPORT ur_result_t UR_APICALL
1483
urUSMHostAlloc(ur_context_handle_t hContext, const ur_usm_desc_t *pUSMDesc,
1584
ur_usm_pool_handle_t, size_t size, void **ppMem) {
1685

1786
void *Ptr = nullptr;
1887
uint32_t Alignment = pUSMDesc ? pUSMDesc->align : 0;
1988

20-
cl_mem_alloc_flags_intel Flags = 0;
21-
cl_mem_properties_intel Properties[3];
22-
23-
if (pUSMDesc && pUSMDesc->pNext &&
24-
static_cast<const ur_base_desc_t *>(pUSMDesc->pNext)->stype ==
25-
UR_STRUCTURE_TYPE_USM_HOST_DESC) {
26-
const auto *HostDesc =
27-
static_cast<const ur_usm_host_desc_t *>(pUSMDesc->pNext);
28-
29-
if (HostDesc->flags & UR_USM_HOST_MEM_FLAG_INITIAL_PLACEMENT) {
30-
Flags |= CL_MEM_ALLOC_INITIAL_PLACEMENT_HOST_INTEL;
31-
}
32-
Properties[0] = CL_MEM_ALLOC_FLAGS_INTEL;
33-
Properties[1] = Flags;
34-
Properties[2] = 0;
35-
} else {
36-
Properties[0] = 0;
89+
std::vector<cl_mem_properties_intel> AllocProperties;
90+
if (pUSMDesc && pUSMDesc->pNext) {
91+
UR_RETURN_ON_FAILURE(usmDescToCLMemProperties(
92+
static_cast<const ur_base_desc_t *>(pUSMDesc->pNext), AllocProperties));
3793
}
3894

3995
// First we need to look up the function pointer
@@ -47,7 +103,9 @@ urUSMHostAlloc(ur_context_handle_t hContext, const ur_usm_desc_t *pUSMDesc,
47103

48104
if (FuncPtr) {
49105
cl_int ClResult = CL_SUCCESS;
50-
Ptr = FuncPtr(CLContext, Properties, size, Alignment, &ClResult);
106+
Ptr = FuncPtr(CLContext,
107+
AllocProperties.empty() ? nullptr : AllocProperties.data(),
108+
size, Alignment, &ClResult);
51109
if (ClResult == CL_INVALID_BUFFER_SIZE) {
52110
return UR_RESULT_ERROR_INVALID_USM_SIZE;
53111
}
@@ -71,25 +129,10 @@ urUSMDeviceAlloc(ur_context_handle_t hContext, ur_device_handle_t hDevice,
71129
void *Ptr = nullptr;
72130
uint32_t Alignment = pUSMDesc ? pUSMDesc->align : 0;
73131

74-
cl_mem_alloc_flags_intel Flags = 0;
75-
cl_mem_properties_intel Properties[3];
76-
if (pUSMDesc && pUSMDesc->pNext &&
77-
static_cast<const ur_base_desc_t *>(pUSMDesc->pNext)->stype ==
78-
UR_STRUCTURE_TYPE_USM_DEVICE_DESC) {
79-
const auto *HostDesc =
80-
static_cast<const ur_usm_device_desc_t *>(pUSMDesc->pNext);
81-
82-
if (HostDesc->flags & UR_USM_DEVICE_MEM_FLAG_INITIAL_PLACEMENT) {
83-
Flags |= CL_MEM_ALLOC_INITIAL_PLACEMENT_DEVICE_INTEL;
84-
}
85-
if (HostDesc->flags & UR_USM_DEVICE_MEM_FLAG_WRITE_COMBINED) {
86-
Flags |= CL_MEM_ALLOC_WRITE_COMBINED_INTEL;
87-
}
88-
Properties[0] = CL_MEM_ALLOC_FLAGS_INTEL;
89-
Properties[1] = Flags;
90-
Properties[2] = 0;
91-
} else {
92-
Properties[0] = 0;
132+
std::vector<cl_mem_properties_intel> AllocProperties;
133+
if (pUSMDesc && pUSMDesc->pNext) {
134+
UR_RETURN_ON_FAILURE(usmDescToCLMemProperties(
135+
static_cast<const ur_base_desc_t *>(pUSMDesc->pNext), AllocProperties));
93136
}
94137

95138
// First we need to look up the function pointer
@@ -104,8 +147,8 @@ urUSMDeviceAlloc(ur_context_handle_t hContext, ur_device_handle_t hDevice,
104147
if (FuncPtr) {
105148
cl_int ClResult = CL_SUCCESS;
106149
Ptr = FuncPtr(CLContext, cl_adapter::cast<cl_device_id>(hDevice),
107-
cl_adapter::cast<cl_mem_properties_intel *>(Properties), size,
108-
Alignment, &ClResult);
150+
AllocProperties.empty() ? nullptr : AllocProperties.data(),
151+
size, Alignment, &ClResult);
109152
if (ClResult == CL_INVALID_BUFFER_SIZE) {
110153
return UR_RESULT_ERROR_INVALID_USM_SIZE;
111154
}
@@ -129,35 +172,10 @@ urUSMSharedAlloc(ur_context_handle_t hContext, ur_device_handle_t hDevice,
129172
void *Ptr = nullptr;
130173
uint32_t Alignment = pUSMDesc ? pUSMDesc->align : 0;
131174

132-
cl_mem_alloc_flags_intel Flags = 0;
133-
const auto *NextStruct =
134-
(pUSMDesc ? static_cast<const ur_base_desc_t *>(pUSMDesc->pNext)
135-
: nullptr);
136-
while (NextStruct) {
137-
if (NextStruct->stype == UR_STRUCTURE_TYPE_USM_HOST_DESC) {
138-
const auto *HostDesc =
139-
reinterpret_cast<const ur_usm_host_desc_t *>(NextStruct);
140-
if (HostDesc->flags & UR_USM_HOST_MEM_FLAG_INITIAL_PLACEMENT) {
141-
Flags |= CL_MEM_ALLOC_INITIAL_PLACEMENT_HOST_INTEL;
142-
}
143-
} else if (NextStruct->stype == UR_STRUCTURE_TYPE_USM_DEVICE_DESC) {
144-
const auto *DevDesc =
145-
reinterpret_cast<const ur_usm_device_desc_t *>(NextStruct);
146-
if (DevDesc->flags & UR_USM_DEVICE_MEM_FLAG_INITIAL_PLACEMENT) {
147-
Flags |= CL_MEM_ALLOC_INITIAL_PLACEMENT_DEVICE_INTEL;
148-
}
149-
if (DevDesc->flags & UR_USM_DEVICE_MEM_FLAG_WRITE_COMBINED) {
150-
Flags |= CL_MEM_ALLOC_WRITE_COMBINED_INTEL;
151-
}
152-
}
153-
NextStruct = static_cast<const ur_base_desc_t *>(NextStruct->pNext);
154-
}
155-
156-
cl_mem_properties_intel Properties[3] = {CL_MEM_ALLOC_FLAGS_INTEL, Flags, 0};
157-
158-
// Passing a flags value of 0 doesn't work, so truncate the properties
159-
if (Flags == 0) {
160-
Properties[0] = 0;
175+
std::vector<cl_mem_properties_intel> AllocProperties;
176+
if (pUSMDesc && pUSMDesc->pNext) {
177+
UR_RETURN_ON_FAILURE(usmDescToCLMemProperties(
178+
static_cast<const ur_base_desc_t *>(pUSMDesc->pNext), AllocProperties));
161179
}
162180

163181
// First we need to look up the function pointer
@@ -172,8 +190,8 @@ urUSMSharedAlloc(ur_context_handle_t hContext, ur_device_handle_t hDevice,
172190
if (FuncPtr) {
173191
cl_int ClResult = CL_SUCCESS;
174192
Ptr = FuncPtr(CLContext, cl_adapter::cast<cl_device_id>(hDevice),
175-
cl_adapter::cast<cl_mem_properties_intel *>(Properties), size,
176-
Alignment, cl_adapter::cast<cl_int *>(&ClResult));
193+
AllocProperties.empty() ? nullptr : AllocProperties.data(),
194+
size, Alignment, cl_adapter::cast<cl_int *>(&ClResult));
177195
if (ClResult == CL_INVALID_BUFFER_SIZE) {
178196
return UR_RESULT_ERROR_INVALID_USM_SIZE;
179197
}

0 commit comments

Comments
 (0)