[gegl/gsoc2011-opencl] platform, device detection and context creation in gegl_cl_init



commit 444b8c7ce52ba2df58bf5d68501d5542e14a7bfe
Author: Victor Oliveira <victormatheus gmail com>
Date:   Fri May 20 22:56:15 2011 -0300

    platform, device detection and context creation in gegl_cl_init

 gegl/opencl/gegl-cl-init.c |   54 ++++++++++++++++++++++++++++---------------
 gegl/opencl/gegl-cl-init.h |    6 +++++
 operations/common/over.c   |   22 +++++------------
 3 files changed, 48 insertions(+), 34 deletions(-)
---
diff --git a/gegl/opencl/gegl-cl-init.c b/gegl/opencl/gegl-cl-init.c
index 36fc48b..cd31562 100644
--- a/gegl/opencl/gegl-cl-init.c
+++ b/gegl/opencl/gegl-cl-init.c
@@ -61,12 +61,34 @@ char *gegl_cl_errstring(cl_int err) {
 
 static gboolean cl_is_accelerated  = FALSE;
 
+static cl_platform_id platform = NULL;
+static cl_device_id   device   = NULL;
+static cl_context     ctx      = NULL;
+
 gboolean
 gegl_cl_is_accelerated (void)
 {
   return cl_is_accelerated;
 }
 
+cl_platform_id
+gegl_cl_get_platform (void)
+{
+  return platform;
+}
+
+cl_device_id
+gegl_cl_get_device (void)
+{
+  return device;
+}
+
+cl_context
+gegl_cl_get_context (void)
+{
+  return ctx;
+}
+
 #define CL_LOAD_FUNCTION(func)                                                    \
 if (!g_module_symbol (module, #func, (gpointer *)& gegl_##func))                  \
   {                                                                               \
@@ -90,11 +112,6 @@ gegl_cl_init (GError **error)
   GModule *module;
 
   char buffer[65536];
-  cl_platform_id platforms[1024];
-  cl_device_id device;
-  cl_uint num_platforms;
-
-  int i;
 
   if (!cl_is_accelerated)
     {
@@ -136,20 +153,19 @@ gegl_cl_init (GError **error)
 
       cl_is_accelerated = TRUE;
 
-      gegl_clGetPlatformIDs (1024, platforms, &num_platforms);
-      for (i=0; i<num_platforms; i++)
-        {
-          gegl_clGetPlatformInfo (platforms[i], CL_PLATFORM_NAME, sizeof(buffer), buffer, NULL);
-          printf("Platform ID:%u\n%s\n", i, buffer);
-          gegl_clGetPlatformInfo (platforms[i], CL_PLATFORM_VERSION, sizeof(buffer), buffer, NULL);
-          printf("Version:%s\n", buffer);
-          gegl_clGetPlatformInfo (platforms[i], CL_PLATFORM_EXTENSIONS, sizeof(buffer), buffer, NULL);
-          printf("Extensions:%s\n", buffer);
-
-          gegl_clGetDeviceIDs(platforms[i], CL_DEVICE_TYPE_DEFAULT, 1, &device, NULL);
-          gegl_clGetDeviceInfo(device, CL_DEVICE_NAME, sizeof(buffer), buffer, NULL);
-          printf("Default Device Name:%s\n", buffer);
-        }
+      gegl_clGetPlatformIDs (1, &platform, NULL);
+      gegl_clGetPlatformInfo (platform, CL_PLATFORM_NAME, sizeof(buffer), buffer, NULL);
+      printf("Platform Name:%s\n", buffer);
+      gegl_clGetPlatformInfo (platform, CL_PLATFORM_VERSION, sizeof(buffer), buffer, NULL);
+      printf("Version:%s\n", buffer);
+      gegl_clGetPlatformInfo (platform, CL_PLATFORM_EXTENSIONS, sizeof(buffer), buffer, NULL);
+      printf("Extensions:%s\n", buffer);
+
+      gegl_clGetDeviceIDs(platform, CL_DEVICE_TYPE_DEFAULT, 1, &device, NULL);
+      gegl_clGetDeviceInfo(device, CL_DEVICE_NAME, sizeof(buffer), buffer, NULL);
+      printf("Default Device Name:%s\n", buffer);
+
+      ctx = gegl_clCreateContext(0, 1, &device, NULL, NULL, NULL);
     }
 
   return TRUE;
diff --git a/gegl/opencl/gegl-cl-init.h b/gegl/opencl/gegl-cl-init.h
index 440bf75..f506288 100644
--- a/gegl/opencl/gegl-cl-init.h
+++ b/gegl/opencl/gegl-cl-init.h
@@ -10,6 +10,12 @@ gboolean gegl_cl_init (GError **error);
 
 gboolean gegl_cl_is_accelerated (void);
 
+cl_platform_id gegl_cl_get_platform (void);
+
+cl_device_id gegl_cl_get_device (void);
+
+cl_context gegl_cl_get_context (void);
+
 #ifdef __GEGL_CL_INIT_MAIN__
 t_clGetPlatformIDs  gegl_clGetPlatformIDs  = NULL;
 t_clGetPlatformInfo gegl_clGetPlatformInfo = NULL;
diff --git a/operations/common/over.c b/operations/common/over.c
index c3d2358..05ab7a7 100644
--- a/operations/common/over.c
+++ b/operations/common/over.c
@@ -83,13 +83,9 @@ cl_process (GeglOperation        *op,
   "}                                                             \n",
   };
 
-  size_t len;
   char buffer[16384];
 
   cl_int errcode;
-  cl_platform_id platform;
-  cl_device_id device;
-  cl_context ctx;
   cl_command_queue cq;
   cl_program program;
   cl_kernel kernel;
@@ -106,23 +102,20 @@ cl_process (GeglOperation        *op,
 
   /* -- Configuration -- */
 
-  CL_SAFE_CALL( errcode = gegl_clGetPlatformIDs (1, &platform, NULL) );
-  CL_SAFE_CALL( errcode = gegl_clGetDeviceIDs(platform, CL_DEVICE_TYPE_DEFAULT, 1, &device, NULL) );
-  CL_SAFE_CALL( ctx     = gegl_clCreateContext(0, 1, &device, NULL, NULL, &errcode) );
-  CL_SAFE_CALL( cq      = gegl_clCreateCommandQueue(ctx, device, 0, &errcode) );
-  CL_SAFE_CALL( program = gegl_clCreateProgramWithSource(ctx, 16, (const char **)&kernel_source, NULL, &errcode) );
+  CL_SAFE_CALL( cq      = gegl_clCreateCommandQueue(gegl_cl_get_context(), gegl_cl_get_device(), 0, &errcode) );
+  CL_SAFE_CALL( program = gegl_clCreateProgramWithSource(gegl_cl_get_context(), 16, (const char **)&kernel_source, NULL, &errcode) );
   errcode = gegl_clBuildProgram(program, 0, NULL, NULL, NULL, NULL);
   if (errcode != CL_SUCCESS)
     {
-      CL_SAFE_CALL( errcode = gegl_clGetProgramBuildInfo(program, device, CL_PROGRAM_BUILD_LOG, sizeof(buffer), buffer, NULL) );
+      CL_SAFE_CALL( errcode = gegl_clGetProgramBuildInfo(program, gegl_cl_get_device(), CL_PROGRAM_BUILD_LOG, sizeof(buffer), buffer, NULL) );
       g_warning("OpenCL Build Error in Line %u in file %s\nError:%s\n%s",
                 __LINE__, __FILE__, gegl_cl_errstring(errcode), buffer);
       return FALSE;
     }
 
-  CL_SAFE_CALL( d_in  = gegl_clCreateBuffer(ctx, CL_MEM_READ_ONLY,  sizeof(cl_float4) * _n_pixels, NULL, &errcode) );
-  CL_SAFE_CALL( d_aux = gegl_clCreateBuffer(ctx, CL_MEM_READ_ONLY,  sizeof(cl_float4) * _n_pixels, NULL, &errcode) );
-  CL_SAFE_CALL( d_out = gegl_clCreateBuffer(ctx, CL_MEM_WRITE_ONLY, sizeof(cl_float4) * _n_pixels, NULL, &errcode) );
+  CL_SAFE_CALL( d_in  = gegl_clCreateBuffer(gegl_cl_get_context(), CL_MEM_READ_ONLY,  sizeof(cl_float4) * _n_pixels, NULL, &errcode) );
+  CL_SAFE_CALL( d_aux = gegl_clCreateBuffer(gegl_cl_get_context(), CL_MEM_READ_ONLY,  sizeof(cl_float4) * _n_pixels, NULL, &errcode) );
+  CL_SAFE_CALL( d_out = gegl_clCreateBuffer(gegl_cl_get_context(), CL_MEM_WRITE_ONLY, sizeof(cl_float4) * _n_pixels, NULL, &errcode) );
 
   CL_SAFE_CALL( kernel  = gegl_clCreateKernel(program, "kernel_over", &errcode) );
   CL_SAFE_CALL( errcode = gegl_clSetKernelArg(kernel, 0, sizeof(cl_mem), (void*)&d_in) );
@@ -132,7 +125,7 @@ cl_process (GeglOperation        *op,
 
   /* -- Running -- */
 
-  CL_SAFE_CALL( errcode = gegl_clGetKernelWorkGroupInfo(kernel, device, CL_KERNEL_WORK_GROUP_SIZE, sizeof(size_t), &local_worksize, NULL) );
+  CL_SAFE_CALL( errcode = gegl_clGetKernelWorkGroupInfo(kernel, gegl_cl_get_device(), CL_KERNEL_WORK_GROUP_SIZE, sizeof(size_t), &local_worksize, NULL) );
   global_worksize = MAX( ((_n_pixels+local_worksize-1) / local_worksize) * local_worksize, local_worksize );
 
   CL_SAFE_CALL( errcode = gegl_clEnqueueWriteBuffer(cq, d_in,  CL_FALSE, 0, sizeof(cl_float4) * _n_pixels, in,  0, NULL, NULL) );
@@ -146,7 +139,6 @@ cl_process (GeglOperation        *op,
 
   CL_SAFE_CALL( errcode = gegl_clReleaseProgram(program) );
   CL_SAFE_CALL( errcode = gegl_clReleaseCommandQueue(cq) );
-  CL_SAFE_CALL( errcode = gegl_clReleaseContext(ctx) );
 
   CL_SAFE_CALL( errcode = gegl_clReleaseKernel(kernel) );
   CL_SAFE_CALL( errcode = gegl_clReleaseMemObject(d_in) );



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]