Skip to content

Commit 43023b2

Browse files
bsyrowikpcolberg
authored andcommitted
Pass buffer location property through OpenCL runtime for USM shared and host allocation calls.
1 parent 49d8874 commit 43023b2

File tree

4 files changed

+55
-11
lines changed

4 files changed

+55
-11
lines changed

include/acl_hal.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,8 @@ typedef struct {
230230

231231
/// Allocates USM host memory
232232
void *(*host_alloc)(const std::vector<cl_device_id> devices, size_t size,
233-
size_t alignment, int *error);
233+
size_t alignment, mem_properties_t *properties,
234+
int *error);
234235

235236
/// Frees allocated memory by the MMD
236237
int (*free)(cl_context context, void *ptr);

src/acl_hal_mmd.cpp

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,8 @@ void *acl_hal_mmd_shared_alloc(cl_device_id device, size_t size,
6565
size_t alignment, mem_properties_t *properties,
6666
int *error);
6767
void *acl_hal_mmd_host_alloc(const std::vector<cl_device_id> devices,
68-
size_t size, size_t alignment, int *error);
68+
size_t size, size_t alignment,
69+
mem_properties_t *properties, int *error);
6970
int acl_hal_mmd_free(cl_context context, void *mem);
7071
int acl_hal_mmd_try_devices(cl_uint num_devices, const cl_device_id *devices,
7172
cl_platform_id platform);
@@ -2519,7 +2520,7 @@ void *acl_hal_mmd_legacy_shared_alloc(cl_context context, size_t size,
25192520
int error = 0;
25202521
std::vector<cl_device_id> devices = std::vector<cl_device_id>(
25212522
context->device, context->device + context->num_devices);
2522-
void *mem = acl_hal_mmd_host_alloc(devices, size, 0, &error);
2523+
void *mem = acl_hal_mmd_host_alloc(devices, size, 0, nullptr, &error);
25232524
device_ptr_out = static_cast<unsigned long long *>(mem);
25242525
if (error) {
25252526
switch (error) {
@@ -2728,7 +2729,8 @@ void *acl_hal_mmd_shared_alloc(cl_device_id device, size_t size,
27282729
}
27292730

27302731
void *acl_hal_mmd_host_alloc(const std::vector<cl_device_id> devices,
2731-
size_t size, size_t alignment, int *error) {
2732+
size_t size, size_t alignment,
2733+
mem_properties_t *properties, int *error) {
27322734
// Note we do not support devices in the same context with different MMDs
27332735
// Safe to get the mmd handle from first device
27342736
void *result = NULL;
@@ -2749,8 +2751,9 @@ void *acl_hal_mmd_host_alloc(const std::vector<cl_device_id> devices,
27492751
handles[i] = device_info[physical_device_id].handle;
27502752
}
27512753

2752-
result = dispatch->aocl_mmd_host_alloc(handles, devices.size(), size,
2753-
alignment, nullptr, error);
2754+
result = dispatch->aocl_mmd_host_alloc(
2755+
handles, devices.size(), size, alignment,
2756+
(aocl_mmd_mem_properties_t *)properties, error);
27542757
acl_delete_arr(handles);
27552758

27562759
if (error) {

src/acl_usm.cpp

Lines changed: 41 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@
33

44
// System headers.
55
#include <algorithm>
6+
#include <array>
67
#include <cassert>
78
#include <cstdio>
89
#include <iostream>
10+
#include <optional>
911
#include <sstream>
1012
#include <string>
1113
#include <unordered_set>
@@ -28,6 +30,8 @@
2830
#include <acl_usm.h>
2931
#include <acl_util.h>
3032

33+
#include <MMD/aocl_mmd.h>
34+
3135
#ifdef __GNUC__
3236
#pragma GCC visibility push(protected)
3337
#endif
@@ -99,11 +103,15 @@ CL_API_ENTRY void *CL_API_CALL clHostMemAllocINTEL(
99103
// Iterate over properties.
100104
// The end of the properties list is specified with a zero.
101105
cl_mem_alloc_flags_intel alloc_flags = 0;
106+
std::optional<cl_uint> mem_id;
102107
while (properties != NULL && *properties != 0) {
103108
switch (*properties) {
104109
case CL_MEM_ALLOC_FLAGS_INTEL: {
105110
alloc_flags = *(properties + 1);
106111
} break;
112+
case CL_MEM_ALLOC_BUFFER_LOCATION_INTEL: {
113+
mem_id = (cl_uint) * (properties + 1);
114+
} break;
107115
default: {
108116
UNLOCK_BAIL_INFO(CL_INVALID_PROPERTY, context, "Invalid properties");
109117
}
@@ -138,6 +146,16 @@ CL_API_ENTRY void *CL_API_CALL clHostMemAllocINTEL(
138146
}
139147

140148
if (acl_get_hal()->host_alloc) {
149+
std::array<mem_properties_t, 3> mmd_properties;
150+
{
151+
auto mmd_properties_it = mmd_properties.begin();
152+
if (mem_id) {
153+
*mmd_properties_it++ = AOCL_MMD_MEM_PROPERTIES_BUFFER_LOCATION;
154+
*mmd_properties_it++ = *mem_id;
155+
}
156+
*mmd_properties_it++ = 0;
157+
}
158+
141159
acl_usm_allocation_t *usm_alloc =
142160
(acl_usm_allocation_t *)acl_malloc(sizeof(acl_usm_allocation_t));
143161

@@ -146,7 +164,8 @@ CL_API_ENTRY void *CL_API_CALL clHostMemAllocINTEL(
146164
}
147165

148166
int error = 0;
149-
void *mem = acl_get_hal()->host_alloc(devices, size, alignment, &error);
167+
void *mem = acl_get_hal()->host_alloc(devices, size, alignment,
168+
mmd_properties.data(), &error);
150169
if (error) {
151170
acl_free(usm_alloc);
152171
switch (error) {
@@ -380,6 +399,7 @@ clSharedMemAllocINTEL(cl_context context, cl_device_id device,
380399
// The end of the properties list is specified with a zero.
381400
cl_mem_alloc_flags_intel alloc_flags = 0;
382401
std::unordered_set<unsigned long long> seen_flags;
402+
std::optional<cl_uint> mem_id;
383403
while (properties != NULL && *properties != 0) {
384404
switch (*properties) {
385405
case CL_MEM_ALLOC_FLAGS_INTEL: {
@@ -396,6 +416,14 @@ clSharedMemAllocINTEL(cl_context context, cl_device_id device,
396416
}
397417
alloc_flags = *(properties + 1);
398418
} break;
419+
case CL_MEM_ALLOC_BUFFER_LOCATION_INTEL: {
420+
if (seen_flags.insert(CL_MEM_ALLOC_BUFFER_LOCATION_INTEL).second ==
421+
false) {
422+
UNLOCK_BAIL_INFO(CL_INVALID_PROPERTY, context,
423+
"Property specified multiple times");
424+
}
425+
mem_id = (cl_uint) * (properties + 1);
426+
} break;
399427
default: {
400428
UNLOCK_BAIL_INFO(CL_INVALID_PROPERTY, context, "Invalid properties");
401429
}
@@ -404,6 +432,16 @@ clSharedMemAllocINTEL(cl_context context, cl_device_id device,
404432
}
405433

406434
if (acl_get_hal()->shared_alloc) {
435+
std::array<mem_properties_t, 3> mmd_properties;
436+
{
437+
auto mmd_properties_it = mmd_properties.begin();
438+
if (mem_id) {
439+
*mmd_properties_it++ = AOCL_MMD_MEM_PROPERTIES_BUFFER_LOCATION;
440+
*mmd_properties_it++ = *mem_id;
441+
}
442+
*mmd_properties_it++ = 0;
443+
}
444+
407445
acl_usm_allocation_t *usm_alloc =
408446
(acl_usm_allocation_t *)acl_malloc(sizeof(acl_usm_allocation_t));
409447

@@ -412,8 +450,8 @@ clSharedMemAllocINTEL(cl_context context, cl_device_id device,
412450
}
413451

414452
int error;
415-
void *mem =
416-
acl_get_hal()->shared_alloc(device, size, alignment, nullptr, &error);
453+
void *mem = acl_get_hal()->shared_alloc(device, size, alignment,
454+
mmd_properties.data(), &error);
417455
if (mem == NULL) {
418456
acl_free(usm_alloc);
419457
switch (error) {

test/acl_hal_test.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,8 @@ void *acl_test_hal_shared_alloc(cl_device_id device, size_t size,
9191
size_t alignment, mem_properties_t *properties,
9292
int *error);
9393
void *acl_test_hal_host_alloc(const std::vector<cl_device_id> devices,
94-
size_t size, size_t alignment, int *error);
94+
size_t size, size_t alignment,
95+
mem_properties_t *properties, int *error);
9596
int acl_test_hal_free(cl_context context, void *ptr);
9697

9798
static acl_event_update_callback acltest_hal_event_callback = NULL;
@@ -662,7 +663,8 @@ void *acl_test_hal_shared_alloc(cl_device_id device, size_t size,
662663
}
663664

664665
void *acl_test_hal_host_alloc(const std::vector<cl_device_id> devices,
665-
size_t size, size_t alignment, int *error) {
666+
size_t size, size_t alignment,
667+
mem_properties_t *properties, int *error) {
666668
size = size;
667669
alignment = alignment;
668670
error = error;

0 commit comments

Comments
 (0)