[Beignet] [PATCH V2] use posix_memalign instead of aligned_alloc to be more compatible

Guo Yejun yejun.guo at intel.com
Mon Nov 10 00:02:37 PST 2014


At some systems, function aligned_alloc is not supported.
>From Linux Programmer's Manual:
The function aligned_alloc() was added to glibc in version 2.16.
The function posix_memalign() is available since glibc 2.1.91.

V2: add check for return value of posix_memalign
Signed-off-by: Guo Yejun <yejun.guo at intel.com>
---
 src/cl_device_id.c                     | 18 +++++++++++-------
 utests/runtime_use_host_ptr_buffer.cpp |  5 ++++-
 2 files changed, 15 insertions(+), 8 deletions(-)

diff --git a/src/cl_device_id.c b/src/cl_device_id.c
index e3b0c64..2031592 100644
--- a/src/cl_device_id.c
+++ b/src/cl_device_id.c
@@ -407,14 +407,18 @@ brw_gt3_break:
   cl_buffer_mgr bufmgr = cl_driver_get_bufmgr(dummy);
 
   const size_t sz = 4096;
-  char* host_ptr = (char*)aligned_alloc(4096, sz);
-  cl_buffer bo = cl_buffer_alloc_userptr(bufmgr, "CL memory object", host_ptr, sz, 0);
-  if (bo == NULL)
-    ret->host_unified_memory = CL_FALSE;
+  void* host_ptr = NULL;
+  int err = posix_memalign(&host_ptr, 4096, sz);
+  if (err == 0) {
+    cl_buffer bo = cl_buffer_alloc_userptr(bufmgr, "CL memory object", host_ptr, sz, 0);
+    if (bo == NULL)
+      ret->host_unified_memory = CL_FALSE;
+    else
+      cl_buffer_unreference(bo);
+    free(host_ptr);
+  }
   else
-    cl_buffer_unreference(bo);
-
-  free(host_ptr);
+    ret->host_unified_memory = CL_FALSE;
   cl_driver_delete(dummy);
 #endif
 
diff --git a/utests/runtime_use_host_ptr_buffer.cpp b/utests/runtime_use_host_ptr_buffer.cpp
index ca06f4b..4603f90 100644
--- a/utests/runtime_use_host_ptr_buffer.cpp
+++ b/utests/runtime_use_host_ptr_buffer.cpp
@@ -6,7 +6,10 @@ static void runtime_use_host_ptr_buffer(void)
 
   // Setup kernel and buffers
   OCL_CREATE_KERNEL("runtime_use_host_ptr_buffer");
-  buf_data[0] = (uint32_t*) aligned_alloc(4096, sizeof(uint32_t) * n);
+
+  int ret = posix_memalign(&buf_data[0], 4096, sizeof(uint32_t) * n);
+  OCL_ASSERT(ret == 0);
+
   for (uint32_t i = 0; i < n; ++i) ((uint32_t*)buf_data[0])[i] = i;
   OCL_CREATE_BUFFER(buf[0], CL_MEM_USE_HOST_PTR, n * sizeof(uint32_t), buf_data[0]);
 
-- 
2.1.0



More information about the Beignet mailing list