[Beignet] [PATCH V2] Runtime: clEnqueueMapImage also need to maintain the mapped images.
Zhigang Gong
zhigang.gong at linux.intel.com
Fri Aug 30 02:21:38 PDT 2013
v2: merge with:
commit 0237652c579123436e5f48514f733e36c8b5264a
Author: Yang Rong <rong.r.yang at intel.com>
Date: Fri Aug 23 11:04:21 2013 +0800
Add clEnqueueMapBuffer and clEnqueueMapImage non-blocking map support.
Signed-off-by: Zhigang Gong <zhigang.gong at linux.intel.com>
---
src/cl_api.c | 139 +++++++++++++++++++++++++++++++---------------------------
1 file changed, 75 insertions(+), 64 deletions(-)
diff --git a/src/cl_api.c b/src/cl_api.c
index 853c720..7173fec 100644
--- a/src/cl_api.c
+++ b/src/cl_api.c
@@ -1563,6 +1563,70 @@ clEnqueueCopyBufferToImage(cl_command_queue command_queue,
return 0;
}
+static cl_int _cl_map_mem(cl_mem mem, void **ptr, void **mem_ptr, size_t offset, size_t size)
+{
+ cl_int slot = -1;
+ int err = CL_SUCCESS;
+ if (!(*ptr = cl_mem_map_gtt_unsync(mem))) {
+ err = CL_MAP_FAILURE;
+ goto error;
+ }
+ *ptr = (char*)(*ptr) + offset;
+ if(mem->flags & CL_MEM_USE_HOST_PTR) {
+ assert(mem->host_ptr);
+ //only calc ptr here, will do memcpy in enqueue
+ *mem_ptr = mem->host_ptr + 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;
+ }
+ memset(mem->mapped_ptr, 0, mem->mapped_ptr_sz * sizeof(cl_mapped_ptr));
+ slot = 0;
+ } else {
+ int i = 0;
+ for (; i < mem->mapped_ptr_sz; i++) {
+ if (mem->mapped_ptr[i].ptr == NULL) {
+ slot = i;
+ break;
+ }
+ }
+ if (i == mem->mapped_ptr_sz) {
+ cl_mapped_ptr *new_ptr = (cl_mapped_ptr *)malloc(
+ 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;
+ }
+ memset(new_ptr, 0, 2 * mem->mapped_ptr_sz * sizeof(cl_mapped_ptr));
+ memcpy(new_ptr, mem->mapped_ptr,
+ mem->mapped_ptr_sz * sizeof(cl_mapped_ptr));
+ slot = mem->mapped_ptr_sz;
+ mem->mapped_ptr_sz *= 2;
+ free(mem->mapped_ptr);
+ mem->mapped_ptr = new_ptr;
+ }
+ }
+ assert(slot != -1);
+ mem->mapped_ptr[slot].ptr = *mem_ptr;
+ mem->mapped_ptr[slot].v_ptr = *ptr;
+ mem->mapped_ptr[slot].size = size;
+ mem->map_ref++;
+error:
+ if (err != CL_SUCCESS)
+ *mem_ptr = NULL;
+ return err;
+}
+
void *
clEnqueueMapBuffer(cl_command_queue command_queue,
cl_mem buffer,
@@ -1578,7 +1642,6 @@ clEnqueueMapBuffer(cl_command_queue command_queue,
cl_int err = CL_SUCCESS;
void *ptr = NULL;
void *mem_ptr = NULL;
- cl_int slot = -1;
enqueue_data *data, no_wait_data = { 0 };
CHECK_QUEUE(command_queue);
@@ -1605,68 +1668,9 @@ clEnqueueMapBuffer(cl_command_queue command_queue,
goto error;
}
- if (!(ptr = cl_mem_map_gtt_unsync(buffer))) {
- err = CL_MAP_FAILURE;
+ err = _cl_map_mem(buffer, &ptr, &mem_ptr, offset, size);
+ if (err != CL_SUCCESS)
goto error;
- }
-
- ptr = (char*)ptr + offset;
-
- if(buffer->flags & CL_MEM_USE_HOST_PTR) {
- assert(buffer->host_ptr);
- //only calc ptr here, will do memcpy in enqueue
- mem_ptr = buffer->host_ptr + offset;
- } else {
- mem_ptr = ptr;
- }
-
- /* Record the mapped address. */
- if (!buffer->mapped_ptr_sz) {
- buffer->mapped_ptr_sz = 16;
- buffer->mapped_ptr = (cl_mapped_ptr *)malloc(
- sizeof(cl_mapped_ptr) * buffer->mapped_ptr_sz);
- if (!buffer->mapped_ptr) {
- cl_mem_unmap_gtt (buffer);
- err = CL_OUT_OF_HOST_MEMORY;
- ptr = NULL;
- goto error;
- }
-
- memset(buffer->mapped_ptr, 0, buffer->mapped_ptr_sz * sizeof(cl_mapped_ptr));
- slot = 0;
- } else {
- int i = 0;
- for (; i < buffer->mapped_ptr_sz; i++) {
- if (buffer->mapped_ptr[i].ptr == NULL) {
- slot = i;
- break;
- }
- }
-
- if (i == buffer->mapped_ptr_sz) {
- cl_mapped_ptr *new_ptr = (cl_mapped_ptr *)malloc(
- sizeof(cl_mapped_ptr) * buffer->mapped_ptr_sz * 2);
- if (!new_ptr) {
- cl_mem_unmap_gtt (buffer);
- err = CL_OUT_OF_HOST_MEMORY;
- ptr = NULL;
- goto error;
- }
- memset(new_ptr, 0, 2 * buffer->mapped_ptr_sz * sizeof(cl_mapped_ptr));
- memcpy(new_ptr, buffer->mapped_ptr,
- buffer->mapped_ptr_sz * sizeof(cl_mapped_ptr));
- slot = buffer->mapped_ptr_sz;
- buffer->mapped_ptr_sz *= 2;
- free(buffer->mapped_ptr);
- buffer->mapped_ptr = new_ptr;
- }
- }
-
- assert(slot != -1);
- buffer->mapped_ptr[slot].ptr = mem_ptr;
- buffer->mapped_ptr[slot].v_ptr = ptr;
- buffer->mapped_ptr[slot].size = size;
- buffer->map_ref++;
TRY(cl_event_check_waitlist, num_events_in_wait_list, event_wait_list, event, buffer->ctx);
@@ -1706,6 +1710,7 @@ clEnqueueMapImage(cl_command_queue command_queue,
{
cl_int err = CL_SUCCESS;
void *ptr = NULL;
+ void *mem_ptr = NULL;
enqueue_data *data, no_wait_data = { 0 };
CHECK_QUEUE(command_queue);
@@ -1747,8 +1752,13 @@ clEnqueueMapImage(cl_command_queue command_queue,
}
size_t offset = image->bpp*origin[0] + image->row_pitch*origin[1] + image->slice_pitch*origin[2];
- ptr = (char*)ptr + offset;
+ size_t size = image->depth == 1 ? image->row_pitch*image->h : image->slice_pitch*image->depth;
+
+ err = _cl_map_mem(mem, &ptr, &mem_ptr, offset, size);
+ if (err != CL_SUCCESS)
+ goto error;
+ ptr = (char*)ptr + offset;
TRY(cl_event_check_waitlist, num_events_in_wait_list, event_wait_list, event, mem->ctx);
data = &no_wait_data;
@@ -1760,6 +1770,7 @@ clEnqueueMapImage(cl_command_queue command_queue,
data->slice_pitch = *image_slice_pitch;
data->map_flags = map_flags;
data->ptr = ptr;
+ data->offset = offset;
if(handle_events(command_queue, num_events_in_wait_list, event_wait_list,
event, data, CL_COMMAND_READ_BUFFER) == CL_ENQUEUE_EXECUTE_IMM) {
@@ -1770,7 +1781,7 @@ clEnqueueMapImage(cl_command_queue command_queue,
error:
if (errcode_ret)
*errcode_ret = err;
- return ptr; //TODO: map and unmap first
+ return mem_ptr; //TODO: map and unmap first
}
cl_int
--
1.7.9.5
More information about the Beignet
mailing list