Browse Source

src/drivers/opencl/driver_opencl.c: Fix a bug that prevented memory from being reclaimed when using OpenCL.

OpenCL uses lazy memory allocation, which means that the allocation methods
defined in src/datawizard/interfaces can never return -ENOMEM when using OpenCL.
Programs needing more memory than what is available on the GPU therefore crash
when trying to write data on the GPU.

starpu_opencl_allocate_memory() now performs a dummy copy from the host to the
device, in order to ensure that the memory was really allocated.
Cyril Roelandt 13 years ago
parent
commit
6019c3c63a
1 changed files with 14 additions and 0 deletions
  1. 14 0
      src/drivers/opencl/driver_opencl.c

+ 14 - 0
src/drivers/opencl/driver_opencl.c

@@ -195,6 +195,20 @@ cl_int starpu_opencl_allocate_memory(cl_mem *mem, size_t size, cl_mem_flags flag
 	if (err == CL_OUT_OF_HOST_MEMORY) return err;
         if (err != CL_SUCCESS) STARPU_OPENCL_REPORT_ERROR(err);
 
+	/*
+	 * OpenCL uses lazy memory allocation: we will only know if the
+	 * allocation failed when trying to copy data onto the device. But we
+	 * want to know this __now__, so we just perform a dummy copy.
+	 */
+	char dummy = 0;
+	err = clEnqueueWriteBuffer(queues[worker->devid], memory, CL_TRUE,
+				0, sizeof(dummy), &dummy,
+				0, NULL, NULL);
+	if (err == CL_MEM_OBJECT_ALLOCATION_FAILURE)
+		return err;
+	if (err != CL_SUCCESS)
+		STARPU_OPENCL_REPORT_ERROR(err);
+
         *mem = memory;
         return CL_SUCCESS;
 }