[gegl/gsoc2011-opencl: 2/19] Dynamic loading of OpenCL libs using gmodule.



commit 2ae907cb8e9b602092a7272a426a60bd132e15f0
Author: Victor Oliveira <victormatheus gmail com>
Date:   Sun May 8 20:16:28 2011 -0300

    Dynamic loading of OpenCL libs using gmodule.

 gegl/opencl/gegl-cl-init.c  |   60 +++++++++++++++++++++++++++++++++++++++++++
 gegl/opencl/gegl-cl-init.h  |   19 +++++++++++++
 gegl/opencl/gegl-cl-types.h |   21 +++++++++++++++
 3 files changed, 100 insertions(+), 0 deletions(-)
---
diff --git a/gegl/opencl/gegl-cl-init.c b/gegl/opencl/gegl-cl-init.c
new file mode 100644
index 0000000..6a14463
--- /dev/null
+++ b/gegl/opencl/gegl-cl-init.c
@@ -0,0 +1,60 @@
+#define __GEGL_CL_INIT_MAIN__
+#include "gegl-cl-init.h"
+#undef __GEGL_CL_INIT_MAIN__
+
+#include <gmodule.h>
+#include <stdio.h>
+
+static gboolean cl_is_accelerated  = FALSE;
+
+gboolean
+gegl_cl_is_accelerated (void)
+{
+  return cl_is_accelerated;
+}
+
+#define CL_LOAD_FUNCTION(func)                                                    \
+if (!g_module_symbol (module, #func, (gpointer *)& gegl_##func))                  \
+  {                                                                               \
+    g_set_error (error, 0, 0,                                \
+                 "%s: %s", "libOpenCL.so", g_module_error ());                    \
+    if (!g_module_close (module))                                                 \
+      g_warning ("%s: %s", "libOpenCL.so", g_module_error ());                    \
+    return FALSE;                                                                 \
+  }                                                                               \
+if (gegl_##func == NULL)                                                          \
+  {                                                                               \
+    g_set_error (error, 0, 0, "symbol gegl_##func is NULL"); \
+    if (!g_module_close (module))                                                 \
+      g_warning ("%s: %s", "libOpenCL.so", g_module_error ());                    \
+    return FALSE;                                                                 \
+  }
+
+gboolean
+gegl_cl_init (GError **error)
+{
+  GModule *module;
+
+  char buffer[65536];
+
+  if (!cl_is_accelerated)
+    {
+      module = g_module_open ("libOpenCL.so", G_MODULE_BIND_LAZY);
+
+      if (!module)
+        {
+          g_set_error (error, 0, 0,
+                       "%s", g_module_error ());
+          return FALSE;
+        }
+
+      CL_LOAD_FUNCTION (clGetPlatformIDs)
+      CL_LOAD_FUNCTION (clGetPlatformInfo)
+
+      cl_is_accelerated = TRUE;
+    }
+
+  return TRUE;
+}
+
+#undef CL_LOAD_FUNCTION
diff --git a/gegl/opencl/gegl-cl-init.h b/gegl/opencl/gegl-cl-init.h
new file mode 100644
index 0000000..9dc0aba
--- /dev/null
+++ b/gegl/opencl/gegl-cl-init.h
@@ -0,0 +1,19 @@
+#ifndef __GEGL_CL_INIT_H__
+#define __GEGL_CL_INIT_H__
+
+#include "gegl-cl-types.h"
+#include <gmodule.h>
+
+gboolean gegl_cl_init (GError **error);
+
+gboolean gegl_cl_is_accelerated (void);
+
+#ifdef __GEGL_CL_INIT_MAIN__
+t_clGetPlatformIDs  gegl_clGetPlatformIDs  = NULL;
+t_clGetPlatformInfo gegl_clGetPlatformInfo = NULL;
+#else
+extern t_clGetPlatformIDs  gegl_clGetPlatformIDs;
+extern t_clGetPlatformInfo gegl_clGetPlatformInfo;
+#endif
+
+#endif  /* __GEGL_CL_INIT_H__ */
diff --git a/gegl/opencl/gegl-cl-types.h b/gegl/opencl/gegl-cl-types.h
new file mode 100644
index 0000000..1182c4c
--- /dev/null
+++ b/gegl/opencl/gegl-cl-types.h
@@ -0,0 +1,21 @@
+#ifndef __GEGL_CL_TYPES_H__
+#define __GEGL_CL_TYPES_H__
+
+#include <glib-object.h>
+
+#include <CL/cl.h>
+#include <CL/cl_gl.h>
+#include <CL/cl_gl_ext.h>
+#include <CL/cl_ext.h>
+
+typedef cl_int (*t_clGetPlatformIDs)(cl_uint,
+                                     cl_platform_id *,
+                                     cl_uint *);
+
+typedef cl_int (*t_clGetPlatformInfo)(cl_platform_id,
+                                      cl_platform_info,
+                                      size_t,
+                                      void *,
+                                      size_t *);
+
+#endif /* __GEGL_CL_TYPES_H__ */



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