[Beignet] [PATCH v3 4/7] Add cl_mem_record_map_mem_for_kernel() for record map adress for TILE_Y image by kernel copying.

yan.wang at linux.intel.com yan.wang at linux.intel.com
Wed Jun 7 07:55:26 UTC 2017


From: Yan Wang <yan.wang at linux.intel.com>

Signed-off-by: Yan Wang <yan.wang at linux.intel.com>
---
 src/cl_mem.c | 109 +++++++++++++++++++++++++++++++++++++++++++++--------------
 src/cl_mem.h |   5 +++
 2 files changed, 88 insertions(+), 26 deletions(-)

diff --git a/src/cl_mem.c b/src/cl_mem.c
index a8543c9..3f41fd8 100644
--- a/src/cl_mem.c
+++ b/src/cl_mem.c
@@ -2650,38 +2650,17 @@ error:
   goto exit;
 }
 
-LOCAL cl_int
-cl_mem_record_map_mem(cl_mem mem, void *ptr, void **mem_ptr, size_t offset,
-                      size_t size, const size_t *origin, const size_t *region)
+static cl_int
+get_mapped_address(cl_mem mem)
 {
-  // TODO: Need to add MT safe logic.
-
   cl_int slot = -1;
-  int err = CL_SUCCESS;
-  size_t sub_offset = 0;
-
-  if(mem->type == CL_MEM_SUBBUFFER_TYPE) {
-    struct _cl_mem_buffer* buffer = (struct _cl_mem_buffer*)mem;
-    sub_offset = buffer->sub_offset;
-  }
-
-  ptr = (char*)ptr + offset + sub_offset;
-  if(mem->flags & CL_MEM_USE_HOST_PTR) {
-    assert(mem->host_ptr);
-    //only calc ptr here, will do memcpy in enqueue
-    *mem_ptr = (char *)mem->host_ptr + offset + sub_offset;
-  } else {
-    *mem_ptr = ptr;
-  }
-  /* Record the mapped address. */
   if (!mem->mapped_ptr_sz) {
     mem->mapped_ptr_sz = 16;
     mem->mapped_ptr = (cl_mapped_ptr *)malloc(
         sizeof(cl_mapped_ptr) * mem->mapped_ptr_sz);
     if (!mem->mapped_ptr) {
       cl_mem_unmap_auto(mem);
-      err = CL_OUT_OF_HOST_MEMORY;
-      goto error;
+      return slot;
     }
     memset(mem->mapped_ptr, 0, mem->mapped_ptr_sz * sizeof(cl_mapped_ptr));
     slot = 0;
@@ -2698,8 +2677,7 @@ cl_mem_record_map_mem(cl_mem mem, void *ptr, void **mem_ptr, size_t offset,
           sizeof(cl_mapped_ptr) * mem->mapped_ptr_sz * 2);
       if (!new_ptr) {
         cl_mem_unmap_auto(mem);
-        err = CL_OUT_OF_HOST_MEMORY;
-        goto error;
+        return slot;
       }
       memset(new_ptr, 0, 2 * mem->mapped_ptr_sz * sizeof(cl_mapped_ptr));
       memcpy(new_ptr, mem->mapped_ptr,
@@ -2710,7 +2688,86 @@ cl_mem_record_map_mem(cl_mem mem, void *ptr, void **mem_ptr, size_t offset,
       mem->mapped_ptr = new_ptr;
     }
   }
+
   assert(slot != -1);
+  return slot;
+}
+
+LOCAL cl_int
+cl_mem_record_map_mem_for_kernel(cl_mem mem, void *ptr, void **mem_ptr, size_t offset,
+                      size_t size, const size_t *origin, const size_t *region,
+                      cl_mem tmp_ker_buf, uint8_t write_map)
+{
+  // TODO: Need to add MT safe logic.
+
+  cl_int slot = -1;
+  int err = CL_SUCCESS;
+  size_t sub_offset = 0;
+
+  //ptr = (char*)ptr + offset + sub_offset;
+  if(mem->flags & CL_MEM_USE_HOST_PTR) {
+    assert(mem->host_ptr);
+    //only calc ptr here, will do memcpy in enqueue
+    *mem_ptr = (char*)ptr + offset + sub_offset;
+  } else {
+    *mem_ptr = ptr;
+  }
+  /* Record the mapped address. */
+  slot = get_mapped_address(mem);
+  if (slot == -1) {
+    err = CL_OUT_OF_HOST_MEMORY;
+    goto error;
+  }
+  mem->mapped_ptr[slot].ptr = *mem_ptr;
+  mem->mapped_ptr[slot].v_ptr = ptr;
+  mem->mapped_ptr[slot].size = size;
+  mem->mapped_ptr[slot].ker_write_map = write_map;
+  mem->mapped_ptr[slot].tmp_ker_buf = tmp_ker_buf;
+  if(origin) {
+    assert(region);
+    mem->mapped_ptr[slot].origin[0] = origin[0];
+    mem->mapped_ptr[slot].origin[1] = origin[1];
+    mem->mapped_ptr[slot].origin[2] = origin[2];
+    mem->mapped_ptr[slot].region[0] = region[0];
+    mem->mapped_ptr[slot].region[1] = region[1];
+    mem->mapped_ptr[slot].region[2] = region[2];
+  }
+  mem->map_ref++;
+error:
+  if (err != CL_SUCCESS)
+    *mem_ptr = NULL;
+  return err;
+}
+
+LOCAL cl_int
+cl_mem_record_map_mem(cl_mem mem, void *ptr, void **mem_ptr, size_t offset,
+                      size_t size, const size_t *origin, const size_t *region)
+{
+  // TODO: Need to add MT safe logic.
+
+  cl_int slot = -1;
+  int err = CL_SUCCESS;
+  size_t sub_offset = 0;
+
+  if(mem->type == CL_MEM_SUBBUFFER_TYPE) {
+    struct _cl_mem_buffer* buffer = (struct _cl_mem_buffer*)mem;
+    sub_offset = buffer->sub_offset;
+  }
+
+  ptr = (char*)ptr + offset + sub_offset;
+  if(mem->flags & CL_MEM_USE_HOST_PTR) {
+    assert(mem->host_ptr);
+    //only calc ptr here, will do memcpy in enqueue
+    *mem_ptr = (char *)mem->host_ptr + offset + sub_offset;
+  } else {
+    *mem_ptr = ptr;
+  }
+  /* Record the mapped address. */
+  slot = get_mapped_address(mem);
+  if (slot == -1) {
+    err = CL_OUT_OF_HOST_MEMORY;
+    goto error;
+  }
   mem->mapped_ptr[slot].ptr = *mem_ptr;
   mem->mapped_ptr[slot].v_ptr = ptr;
   mem->mapped_ptr[slot].size = size;
diff --git a/src/cl_mem.h b/src/cl_mem.h
index 0b33c31..ce1294d 100644
--- a/src/cl_mem.h
+++ b/src/cl_mem.h
@@ -61,6 +61,8 @@ typedef struct _cl_mapped_ptr {
   size_t size;
   size_t origin[3];  /* mapped origin */
   size_t region[3];  /* mapped region */
+  cl_mem tmp_ker_buf;       /* this object is tmp buffer for OCL kernel copying */
+  uint8_t ker_write_map;    /* this flag is used to indicate CL_MAP_WRITE for OCL kernel copying */
 }cl_mapped_ptr;
 
 typedef struct _cl_mem_dstr_cb {
@@ -368,6 +370,9 @@ extern cl_mem cl_mem_new_image_from_fd(cl_context ctx,
 extern cl_int cl_mem_record_map_mem(cl_mem mem, void *ptr, void **mem_ptr, size_t offset,
                       size_t size, const size_t *origin, const size_t *region);
 
+extern cl_int cl_mem_record_map_mem_for_kernel(cl_mem mem, void *ptr, void **mem_ptr, size_t offset,
+                      size_t size, const size_t *origin, const size_t *region, cl_mem tmp_ker_buf, uint8_t write_map);
+
 extern cl_int cl_mem_set_destructor_callback(cl_mem memobj,
                       void(CL_CALLBACK *pfn_notify)(cl_mem, void *), void *user_data);
 #endif /* __CL_MEM_H__ */
-- 
2.7.4



More information about the Beignet mailing list