Skip to content

Buffer location property for USM device allocation #46

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion src/acl_usm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -239,11 +239,15 @@ clDeviceMemAllocINTEL(cl_context context, cl_device_id device,
// Iterate over properties.
// The end of the properties list is specified with a zero.
cl_mem_alloc_flags_intel alloc_flags = 0;
cl_uint mem_id = acl_get_default_memory(device->def);
while (properties != NULL && *properties != 0) {
switch (*properties) {
case CL_MEM_ALLOC_FLAGS_INTEL: {
alloc_flags = *(properties + 1);
} break;
case CL_MEM_ALLOC_BUFFER_LOCATION_INTEL: {
mem_id = (cl_uint) * (properties + 1);
} break;
default: {
UNLOCK_BAIL_INFO(CL_INVALID_DEVICE, context, "Invalid properties");
}
Expand All @@ -254,8 +258,10 @@ clDeviceMemAllocINTEL(cl_context context, cl_device_id device,
cl_int status;

// Use cl_mem for convenience
cl_mem_properties_intel props[] = {CL_MEM_ALLOC_BUFFER_LOCATION_INTEL, mem_id,
0};
cl_mem usm_device_buffer = clCreateBufferWithPropertiesINTEL(
context, NULL, CL_MEM_READ_WRITE, size, NULL, &status);
context, props, CL_MEM_READ_WRITE, size, NULL, &status);
if (status != CL_SUCCESS) {
UNLOCK_BAIL_INFO(status, context, "Failed to allocate device memory");
}
Expand Down Expand Up @@ -605,6 +611,14 @@ CL_API_ENTRY cl_int CL_API_CALL clGetMemAllocInfoINTEL(
}
} break;

case CL_MEM_ALLOC_BUFFER_LOCATION_INTEL: {
if (usm_alloc) {
RESULT_UINT(usm_alloc->mem->mem_id);
} else {
RESULT_UINT(0);
}
} break;

case CL_MEM_ALLOC_BASE_PTR_INTEL: {
void *base_ptr = NULL;
if (usm_alloc) {
Expand Down
56 changes: 56 additions & 0 deletions test/acl_usm_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -388,6 +388,62 @@ MT_TEST(acl_usm, alloc_and_free_device_usm) {
ACL_LOCKED(acl_print_debug_msg("end alloc_and_free_device_usm\n"));
}

MT_TEST(acl_usm, buffer_location_usm) {
ACL_LOCKED(acl_print_debug_msg("begin buffer_location_usm\n"));
const int alignment = ACL_MEM_ALIGN;
cl_int status;
this->yeah = true;

acl_usm_allocation_t *test_device_alloc;

cl_mem_properties_intel good_property[3] = {
CL_MEM_ALLOC_BUFFER_LOCATION_INTEL, 0, 0};

ACL_LOCKED(CHECK(acl_context_is_valid(m_context)));

// Correct USM buffer allocation
void *test_ptr = clDeviceMemAllocINTEL(
m_context, m_device[0], &(good_property[0]), 8, alignment, &status);
CHECK_EQUAL(status, CL_SUCCESS);
CHECK(ACL_DEVICE_ALLOCATION(test_ptr));
CHECK(test_ptr != NULL);
CHECK(!m_context->usm_allocation.empty());
ACL_LOCKED(CHECK_EQUAL(acl_usm_ptr_belongs_to_context(m_context, test_ptr),
CL_TRUE));
ACL_LOCKED(test_device_alloc =
acl_get_usm_alloc_from_ptr(m_context, test_ptr));
assert(test_device_alloc);
CHECK_EQUAL(test_device_alloc->range.begin, test_ptr);

// Check alloc information
cl_uint read_mem_id = 4;
size_t ret_size = 9;
status = clGetMemAllocInfoINTEL(m_context, test_ptr,
CL_MEM_ALLOC_BUFFER_LOCATION_INTEL,
sizeof(cl_uint), &read_mem_id, &ret_size);

CHECK_EQUAL(CL_SUCCESS, status);
CHECK_EQUAL(0, read_mem_id);
CHECK_EQUAL(sizeof(cl_uint), ret_size);

status = clMemFreeINTEL(m_context, test_ptr);
ACL_LOCKED(CHECK_EQUAL(acl_usm_ptr_belongs_to_context(m_context, test_ptr),
CL_FALSE));
CHECK(m_context->usm_allocation.empty());

// Check when given pointer is already freed
status = clGetMemAllocInfoINTEL(m_context, test_ptr,
CL_MEM_ALLOC_BUFFER_LOCATION_INTEL,
sizeof(cl_uint), &read_mem_id, &ret_size);

CHECK_EQUAL(CL_SUCCESS, status);
CHECK_EQUAL(0, read_mem_id);
CHECK_EQUAL(sizeof(cl_uint), ret_size);
CHECK_EQUAL(status, CL_SUCCESS);

ACL_LOCKED(acl_print_debug_msg("end buffer_location_usm\n"));
}

MT_TEST(acl_usm, alloc_and_free_shared_usm) {
ACL_LOCKED(acl_print_debug_msg("begin alloc_and_free_shared_usm\n"));
const int alignment = 16;
Expand Down