Skip to content

Commit f79a9c2

Browse files
sherry-yuanpcolberg
authored andcommitted
Add USM buffer location property for clDeviceMemAlloc
1 parent 1264543 commit f79a9c2

File tree

2 files changed

+71
-1
lines changed

2 files changed

+71
-1
lines changed

src/acl_usm.cpp

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -239,11 +239,15 @@ clDeviceMemAllocINTEL(cl_context context, cl_device_id device,
239239
// Iterate over properties.
240240
// The end of the properties list is specified with a zero.
241241
cl_mem_alloc_flags_intel alloc_flags = 0;
242+
cl_uint mem_id = acl_get_default_memory(device->def);
242243
while (properties != NULL && *properties != 0) {
243244
switch (*properties) {
244245
case CL_MEM_ALLOC_FLAGS_INTEL: {
245246
alloc_flags = *(properties + 1);
246247
} break;
248+
case CL_MEM_ALLOC_BUFFER_LOCATION_INTEL: {
249+
mem_id = (cl_uint) * (properties + 1);
250+
} break;
247251
default: {
248252
UNLOCK_BAIL_INFO(CL_INVALID_DEVICE, context, "Invalid properties");
249253
}
@@ -254,8 +258,10 @@ clDeviceMemAllocINTEL(cl_context context, cl_device_id device,
254258
cl_int status;
255259

256260
// Use cl_mem for convenience
261+
cl_mem_properties_intel props[] = {CL_MEM_ALLOC_BUFFER_LOCATION_INTEL, mem_id,
262+
0};
257263
cl_mem usm_device_buffer = clCreateBufferWithPropertiesINTEL(
258-
context, NULL, CL_MEM_READ_WRITE, size, NULL, &status);
264+
context, props, CL_MEM_READ_WRITE, size, NULL, &status);
259265
if (status != CL_SUCCESS) {
260266
UNLOCK_BAIL_INFO(status, context, "Failed to allocate device memory");
261267
}
@@ -605,6 +611,14 @@ CL_API_ENTRY cl_int CL_API_CALL clGetMemAllocInfoINTEL(
605611
}
606612
} break;
607613

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

test/acl_usm_test.cpp

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -388,6 +388,62 @@ 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 = 9;
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+
CHECK_EQUAL(sizeof(cl_uint), ret_size);
428+
429+
status = clMemFreeINTEL(m_context, test_ptr);
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+
// Check when given pointer is already freed
435+
status = clGetMemAllocInfoINTEL(m_context, test_ptr,
436+
CL_MEM_ALLOC_BUFFER_LOCATION_INTEL,
437+
sizeof(cl_uint), &read_mem_id, &ret_size);
438+
439+
CHECK_EQUAL(CL_SUCCESS, status);
440+
CHECK_EQUAL(0, read_mem_id);
441+
CHECK_EQUAL(sizeof(cl_uint), ret_size);
442+
CHECK_EQUAL(status, CL_SUCCESS);
443+
444+
ACL_LOCKED(acl_print_debug_msg("end buffer_location_usm\n"));
445+
}
446+
391447
MT_TEST(acl_usm, alloc_and_free_shared_usm) {
392448
ACL_LOCKED(acl_print_debug_msg("begin alloc_and_free_shared_usm\n"));
393449
const int alignment = 16;

0 commit comments

Comments
 (0)