[Beignet] [PATCH] Runtime: clEnqueueMapImage also need to maintain the mapped images.
Zhigang Gong
zhigang.gong at linux.intel.com
Thu Aug 29 23:22:09 PDT 2013
Signed-off-by: Zhigang Gong <zhigang.gong at linux.intel.com>
---
src/cl_enqueue.c | 90 ++++++++++++++++++++++++++----------------------------
1 file changed, 43 insertions(+), 47 deletions(-)
diff --git a/src/cl_enqueue.c b/src/cl_enqueue.c
index 1e8de66..e832446 100644
--- a/src/cl_enqueue.c
+++ b/src/cl_enqueue.c
@@ -153,100 +153,96 @@ error:
}
-cl_int cl_enqueue_map_buffer(enqueue_data *data)
+static cl_int _cl_map_mem(cl_mem mem, void **mem_ptr, size_t offset, size_t size)
{
-
- void *ptr = NULL;
- cl_int err = CL_SUCCESS;
- void *mem_ptr = NULL;
+ void *ptr;
cl_int slot = -1;
- cl_mem buffer = data->mem_obj;
+ int err = CL_SUCCESS;
- if (!(ptr = cl_mem_map_auto(buffer))) {
+ if (!(ptr = cl_mem_map_auto(mem))) {
err = CL_MAP_FAILURE;
+ goto error;
}
- ptr = (char*)ptr + data->offset;
+ ptr = (char*)ptr + offset;
- if(buffer->flags & CL_MEM_USE_HOST_PTR) {
- assert(buffer->host_ptr);
- memcpy(buffer->host_ptr + data->offset, ptr, data->size);
- mem_ptr = buffer->host_ptr + data->offset;
+ if(mem->flags & CL_MEM_USE_HOST_PTR) {
+ assert(mem->host_ptr);
+ memcpy(mem->host_ptr + offset, ptr, size);
+ *mem_ptr = mem->host_ptr + offset;
} else {
- mem_ptr = ptr;
+ *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_auto (buffer);
+ 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;
- ptr = NULL;
goto error;
}
- memset(buffer->mapped_ptr, 0, buffer->mapped_ptr_sz * sizeof(cl_mapped_ptr));
+ memset(mem->mapped_ptr, 0, mem->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) {
+ for (; i < mem->mapped_ptr_sz; i++) {
+ if (mem->mapped_ptr[i].ptr == NULL) {
slot = i;
break;
}
}
- if (i == buffer->mapped_ptr_sz) {
+ if (i == mem->mapped_ptr_sz) {
cl_mapped_ptr *new_ptr = (cl_mapped_ptr *)malloc(
- sizeof(cl_mapped_ptr) * buffer->mapped_ptr_sz * 2);
+ sizeof(cl_mapped_ptr) * mem->mapped_ptr_sz * 2);
if (!new_ptr) {
- cl_mem_unmap_auto (buffer);
+ cl_mem_unmap_auto (mem);
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;
+ 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);
- buffer->mapped_ptr[slot].ptr = mem_ptr;
- buffer->mapped_ptr[slot].v_ptr = ptr;
- buffer->mapped_ptr[slot].size = data->size;
- buffer->map_ref++;
-
- data->ptr = mem_ptr;
+ 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;
}
+cl_int cl_enqueue_map_buffer(enqueue_data *data)
+{
+ return _cl_map_mem(data->mem_obj, &data->ptr, data->offset, data->size);
+}
+
cl_int cl_enqueue_map_image(enqueue_data *data)
{
- void *ptr = NULL;
cl_int err = CL_SUCCESS;
cl_mem mem = data->mem_obj;
CHECK_IMAGE(mem);
const size_t *origin = data->origin;
- if (!(ptr = cl_mem_map_auto(mem))) {
- err = CL_MAP_FAILURE;
- goto error;
- }
-
size_t offset = image->bpp*origin[0] + image->row_pitch*origin[1] + image->slice_pitch*origin[2];
- data->ptr = (char*)ptr + offset;
+ size_t size = image->depth == 1 ? image->row_pitch*image->h : image->slice_pitch*image->depth;
+ return _cl_map_mem(mem, &data->ptr, offset, size);
error:
return err;
}
--
1.7.9.5
More information about the Beignet
mailing list