Skip to content

Commit 397a189

Browse files
committed
Add USM buffer location property for clDeviceMemAlloc
1 parent b08e0af commit 397a189

File tree

2 files changed

+61
-1
lines changed

2 files changed

+61
-1
lines changed

src/acl_usm.cpp

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -240,11 +240,15 @@ clDeviceMemAllocINTEL(cl_context context, cl_device_id device,
240240
// Iterate over properties.
241241
// The end of the properties list is specified with a zero.
242242
cl_mem_alloc_flags_intel alloc_flags = 0;
243+
cl_uint mem_id = 0;
243244
while (properties != NULL && *properties != 0) {
244245
switch (*properties) {
245246
case CL_MEM_ALLOC_FLAGS_INTEL: {
246247
alloc_flags = *(properties + 1);
247248
} break;
249+
case CL_MEM_ALLOC_BUFFER_LOCATION_INTEL: {
250+
mem_id = (cl_uint) * (properties + 1);
251+
} break;
248252
default: {
249253
UNLOCK_BAIL_INFO(CL_INVALID_DEVICE, context, "Invalid properties");
250254
}
@@ -255,8 +259,10 @@ clDeviceMemAllocINTEL(cl_context context, cl_device_id device,
255259
cl_int status;
256260

257261
// Use cl_mem for convenience
262+
cl_mem_properties_intel props[] = {CL_MEM_ALLOC_BUFFER_LOCATION_INTEL, mem_id,
263+
0};
258264
cl_mem usm_device_buffer = clCreateBufferWithPropertiesINTEL(
259-
context, NULL, CL_MEM_READ_WRITE, size, NULL, &status);
265+
context, props, CL_MEM_READ_WRITE, size, NULL, &status);
260266
if (status != CL_SUCCESS) {
261267
UNLOCK_BAIL_INFO(status, context, "Failed to allocate device memory");
262268
}
@@ -606,6 +612,14 @@ CL_API_ENTRY cl_int CL_API_CALL clGetMemAllocInfoINTEL(
606612
}
607613
} break;
608614

615+
case CL_MEM_ALLOC_BUFFER_LOCATION_INTEL: {
616+
if (usm_alloc) {
617+
RESULT_UINT(usm_alloc->mem->mem_id);
618+
} else {
619+
RESULT_UINT(CL_MEM_TYPE_UNKNOWN_INTEL);
620+
}
621+
} break;
622+
609623
case CL_MEM_ALLOC_BASE_PTR_INTEL: {
610624
void *base_ptr = NULL;
611625
if (usm_alloc) {

test/acl_usm_test.cpp

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -388,6 +388,52 @@ MT_TEST(acl_usm, alloc_and_free_device_usm) {
388388
ACL_LOCKED(acl_print_debug_msg("end alloc_and_free_device_usm\n"));
389389
}
390390

391+
MT_TEST(acl_usm, buffer_location_usm) {
392+
ACL_LOCKED(acl_print_debug_msg("begin buffer_location_usm\n"));
393+
const int alignment = ACL_MEM_ALIGN;
394+
cl_int status;
395+
this->yeah = true;
396+
397+
acl_usm_allocation_t *test_device_alloc;
398+
399+
cl_mem_properties_intel good_property[3] = {
400+
CL_MEM_ALLOC_BUFFER_LOCATION_INTEL, 0, 0};
401+
402+
ACL_LOCKED(CHECK(acl_context_is_valid(m_context)));
403+
404+
// Correct USM buffer allocation
405+
void *test_ptr = clDeviceMemAllocINTEL(
406+
m_context, m_device[0], &(good_property[0]), 8, alignment, &status);
407+
CHECK_EQUAL(status, CL_SUCCESS);
408+
CHECK(ACL_DEVICE_ALLOCATION(test_ptr));
409+
CHECK(test_ptr != NULL);
410+
CHECK(!m_context->usm_allocation.empty());
411+
ACL_LOCKED(CHECK_EQUAL(acl_usm_ptr_belongs_to_context(m_context, test_ptr),
412+
CL_TRUE));
413+
ACL_LOCKED(test_device_alloc =
414+
acl_get_usm_alloc_from_ptr(m_context, test_ptr));
415+
assert(test_device_alloc);
416+
CHECK_EQUAL(test_device_alloc->range.begin, test_ptr);
417+
418+
// Check alloc information
419+
cl_uint read_mem_id = 4;
420+
size_t ret_size;
421+
status = clGetMemAllocInfoINTEL(m_context, test_ptr,
422+
CL_MEM_ALLOC_BUFFER_LOCATION_INTEL,
423+
sizeof(cl_uint), &read_mem_id, &ret_size);
424+
425+
CHECK_EQUAL(CL_SUCCESS, status);
426+
CHECK_EQUAL(0, read_mem_id);
427+
428+
status = clMemFreeINTEL(m_context, test_ptr);
429+
CHECK_EQUAL(status, CL_SUCCESS);
430+
ACL_LOCKED(CHECK_EQUAL(acl_usm_ptr_belongs_to_context(m_context, test_ptr),
431+
CL_FALSE));
432+
CHECK(m_context->usm_allocation.empty());
433+
434+
ACL_LOCKED(acl_print_debug_msg("end buffer_location_usm\n"));
435+
}
436+
391437
MT_TEST(acl_usm, alloc_and_free_shared_usm) {
392438
ACL_LOCKED(acl_print_debug_msg("begin alloc_and_free_shared_usm\n"));
393439
const int alignment = 16;

0 commit comments

Comments
 (0)