Move the memory allocate size check to the callee.
authorYang Rong <rong.r.yang@intel.com>
Tue, 7 Jan 2014 03:30:54 +0000 (11:30 +0800)
committerZhigang Gong <zhigang.gong@intel.com>
Wed, 8 Jan 2014 05:23:26 +0000 (13:23 +0800)
Because image's alignment, the alloc size may exceed the CL_DEVICE_MAX_MEM_ALLOC_SIZE if the
image's size is calculate from it. So move the size check from cl_mem_allocate to the callee, and
slightly enlarge the limit size when check in allocate image.

Signed-off-by: Yang Rong <rong.r.yang@intel.com>
Reviewed-by: Zhigang Gong <zhigang.gong@linux.intel.com>
src/cl_gt_device.h
src/cl_mem.c

index 30692f8..110988a 100644 (file)
@@ -51,7 +51,7 @@
 .min_data_type_align_size = sizeof(cl_long) * 16,
 .single_fp_config = 0, /* XXX */
 .global_mem_cache_type = CL_READ_WRITE_CACHE,
-.global_mem_size = 256 * 1024 * 1024,
+.global_mem_size = 128 * 1024 * 1024,
 .max_constant_buffer_size = 512 << 10,
 .max_constant_args = 8,
 .error_correction_support = CL_FALSE,
index 5c4b197..40e0a99 100644 (file)
@@ -200,23 +200,9 @@ cl_mem_allocate(enum cl_mem_type type,
   cl_mem mem = NULL;
   cl_int err = CL_SUCCESS;
   size_t alignment = 64;
-  cl_ulong max_mem_size;
 
   assert(ctx);
 
-  /* Due to alignment, the image size may exceed alloc max size, check global mem instead */
-  if ((err = cl_get_device_info(ctx->device,
-                                CL_DEVICE_GLOBAL_MEM_SIZE,
-                                sizeof(max_mem_size),
-                                &max_mem_size,
-                                NULL)) != CL_SUCCESS) {
-    goto error;
-  }
-  if (UNLIKELY(sz > max_mem_size)) {
-    err = CL_INVALID_BUFFER_SIZE;
-    goto error;
-  }
-
   /* Allocate and inialize the structure itself */
   if (type == CL_MEM_IMAGE_TYPE) {
     struct _cl_mem_image *image = NULL;
@@ -289,6 +275,7 @@ cl_mem_new_buffer(cl_context ctx,
 
   cl_int err = CL_SUCCESS;
   cl_mem mem = NULL;
+  cl_ulong max_mem_size;
 
   if (UNLIKELY(sz == 0)) {
     err = CL_INVALID_BUFFER_SIZE;
@@ -334,6 +321,19 @@ cl_mem_new_buffer(cl_context ctx,
     goto error;
   }
 
+  if ((err = cl_get_device_info(ctx->device,
+                                CL_DEVICE_MAX_MEM_ALLOC_SIZE,
+                                sizeof(max_mem_size),
+                                &max_mem_size,
+                                NULL)) != CL_SUCCESS) {
+    goto error;
+  }
+
+  if (UNLIKELY(sz > max_mem_size)) {
+    err = CL_INVALID_BUFFER_SIZE;
+    goto error;
+  }
+
   /* Create the buffer in video memory */
   mem = cl_mem_allocate(CL_MEM_BUFFER_TYPE, ctx, flags, sz, CL_FALSE, &err);
   if (mem == NULL || err != CL_SUCCESS)
@@ -596,6 +596,7 @@ _cl_mem_new_image(cl_context ctx,
   }
 
   sz = aligned_pitch * aligned_h * depth;
+
   mem = cl_mem_allocate(CL_MEM_IMAGE_TYPE, ctx, flags, sz, tiling != CL_NO_TILE, &err);
   if (mem == NULL || err != CL_SUCCESS)
     goto error;