From d2100dbbc4bd94b08165cc22e574f2d5aa87b40b Mon Sep 17 00:00:00 2001
From: Marko Bencun <mbencun@gmail.com>
Date: Sun, 1 Sep 2013 16:43:18 +0200
Subject: [PATCH] collapsed get_info wrapper to one function

---
 pyopencl/cffi_cl.py          |  53 ++++--------
 src/c_wrapper/wrap_cl.cpp    | 156 ++++++++++++++---------------------
 src/c_wrapper/wrap_cl_core.h |   6 +-
 3 files changed, 79 insertions(+), 136 deletions(-)

diff --git a/pyopencl/cffi_cl.py b/pyopencl/cffi_cl.py
index 5a3e0b64..a9a432a7 100644
--- a/pyopencl/cffi_cl.py
+++ b/pyopencl/cffi_cl.py
@@ -168,15 +168,24 @@ def _handle_error(error):
 # }}}
 
 class _Common(object):
+    @classmethod
+    def _c_class_type(cls):
+        return getattr(_lib, 'CLASS_%s' % cls._id.upper())
+        
     def __eq__(self, other):
         return hash(self) == hash(other)
 
     def __hash__(self):
-        return _lib._hash(self.ptr, getattr(_lib, 'CLASS_%s' % self._id.upper()))
+        return _lib._hash(self.ptr, self._c_class_type())
+
+    def get_info(self, param):
+        info = _ffi.new('generic_info *')
+        _handle_error(_lib._get_info(self.ptr, self._c_class_type(), param, info))
+        return _generic_info_to_python(info)
 
     @property
     def int_ptr(self):
-        return _lib._int_ptr(self.ptr, getattr(_lib, 'CLASS_%s' % self._id.upper()))
+        return _lib._int_ptr(self.ptr, self._c_class_type())
 
     @classmethod
     def from_int_ptr(cls, int_ptr_value):
@@ -193,9 +202,7 @@ class Device(_Common):
     def get_info(self, param):
         if param == 4145:
             return self.__dict__["platform"] # TODO HACK
-        info = _ffi.new('generic_info *')
-        _handle_error(_lib.device__get_info(self.ptr, param, info))
-        return _generic_info_to_python(info)
+        return super(Device, self).get_info(param)
 
 def _parse_context_properties(properties):
     props = []
@@ -235,9 +242,6 @@ class Context(_Common):
             raise NotImplementedError()
 
         self.ptr = ptr_ctx[0]
-        
-    def get_info(self, param):
-        return 'TODO'
 
 class CommandQueue(_Common):
     _id = 'command_queue'
@@ -248,15 +252,8 @@ class CommandQueue(_Common):
         _handle_error(_lib._create_command_queue(ptr_command_queue, context.ptr, _ffi.NULL if device is None else device.ptr, properties))
         self.ptr = ptr_command_queue[0]
 
-    def get_info(self, param):
-        print param
-        raise NotImplementedError()
-
 class MemoryObjectHolder(_Common):
-    def get_info(self, param):
-        info = _ffi.new('generic_info *')
-        _handle_error(_lib.memory_object_holder__get_info(self.ptr, param, info))
-        return _generic_info_to_python(info)
+    pass
 
 class MemoryObject(MemoryObjectHolder):
     pass
@@ -347,22 +344,12 @@ class _Program(_Common):
             ptr_binaries = _CArrays(_ffi.new('char***'))
             _handle_error(_lib.program__get_info__binaries(self.ptr, ptr_binaries.ptr, ptr_binaries.size))
             return map(_ffi.string, ptr_binaries)
-
-        info = _ffi.new('generic_info *')
-        _handle_error(_lib.program__get_info(self.ptr, param, info))
-        
-        return _generic_info_to_python(info)
-
+        return super(_Program, self).get_info(param)
         
 class Platform(_Common):
     _id = 'platform'
     # todo: __del__
 
-    def get_info(self, param):
-        info = _ffi.new('generic_info *')
-        _handle_error(_lib.platform__get_info(self.ptr, param, info))
-        return _generic_info_to_python(info)
-    
     def get_devices(self, device_type=device_type.ALL):
         devices = _CArray(_ffi.new('void**'))
         _handle_error(_lib.platform__get_devices(self.ptr, devices.ptr, devices.size, device_type))
@@ -387,7 +374,7 @@ def _generic_info_to_python(info):
     _lib._free(info.value)
     return ret
 
-class Kernel(object):
+class Kernel(_Common):
     _id = 'kernel'
     
     def __init__(self, program, name):
@@ -395,12 +382,6 @@ class Kernel(object):
         _handle_error(_lib._create_kernel(ptr_kernel, program.ptr, name))
         self.ptr = ptr_kernel[0]
         
-    def get_info(self, param):
-        info = _ffi.new('generic_info *')
-        _handle_error(_lib.kernel__get_info(self.ptr, param, info))
-        return _generic_info_to_python(info)
-        #raise NotImplementedError()
-
     def set_arg(self, arg_index, arg):
         if isinstance(arg, Buffer):
             _handle_error(_lib.kernel__set_arg_mem_buffer(self.ptr, arg_index, arg.ptr))
@@ -422,10 +403,6 @@ class Event(_Common):
     _id = 'event'
     def __init__(self):
         pass
-        
-    def get_info(self, param):
-        print param
-        raise NotImplementedError()
 
 
 def enqueue_nd_range_kernel(queue, kernel, global_work_size, local_work_size, global_work_offset=None, wait_for=None, g_times_l=False):
diff --git a/src/c_wrapper/wrap_cl.cpp b/src/c_wrapper/wrap_cl.cpp
index 75d1781b..f5e6eec4 100644
--- a/src/c_wrapper/wrap_cl.cpp
+++ b/src/c_wrapper/wrap_cl.cpp
@@ -314,32 +314,32 @@ namespace pyopencl
 
     PYOPENCL_EQUALITY_TESTS(event);
 
-    // py::object get_info(cl_event_info param_name) const
-    //       {
-    //         switch (param_name)
-    //         {
-    //           case CL_EVENT_COMMAND_QUEUE:
-    //             PYOPENCL_GET_OPAQUE_INFO(Event, m_event, param_name,
-    //                 cl_command_queue, command_queue);
-    //           case CL_EVENT_COMMAND_TYPE:
-    //             PYOPENCL_GET_INTEGRAL_INFO(Event, m_event, param_name,
-    //                 cl_command_type);
-    //           case CL_EVENT_COMMAND_EXECUTION_STATUS:
-    //             PYOPENCL_GET_INTEGRAL_INFO(Event, m_event, param_name,
-    //                 cl_int);
-    //           case CL_EVENT_REFERENCE_COUNT:
-    //             PYOPENCL_GET_INTEGRAL_INFO(Event, m_event, param_name,
-    //                 cl_uint);
-    // #if PYOPENCL_CL_VERSION >= 0x1010
-    //           case CL_EVENT_CONTEXT:
-    //             PYOPENCL_GET_OPAQUE_INFO(Event, m_event, param_name,
-    //                 cl_context, context);
-    // #endif
+    generic_info get_info(cl_event_info param_name) const
+    {
+      switch (param_name)
+	{
+	  //           case CL_EVENT_COMMAND_QUEUE:
+	  //             PYOPENCL_GET_OPAQUE_INFO(Event, m_event, param_name,
+	  //                 cl_command_queue, command_queue);
+	  //           case CL_EVENT_COMMAND_TYPE:
+	  //             PYOPENCL_GET_INTEGRAL_INFO(Event, m_event, param_name,
+	  //                 cl_command_type);
+	  //           case CL_EVENT_COMMAND_EXECUTION_STATUS:
+	  //             PYOPENCL_GET_INTEGRAL_INFO(Event, m_event, param_name,
+	  //                 cl_int);
+	  //           case CL_EVENT_REFERENCE_COUNT:
+	  //             PYOPENCL_GET_INTEGRAL_INFO(Event, m_event, param_name,
+	  //                 cl_uint);
+	  // #if PYOPENCL_CL_VERSION >= 0x1010
+	  //           case CL_EVENT_CONTEXT:
+	  //             PYOPENCL_GET_OPAQUE_INFO(Event, m_event, param_name,
+	  //                 cl_context, context);
+	  // #endif
 
-    //           default:
-    //             throw error("Event.get_info", CL_INVALID_VALUE);
-    //         }
-    //       }
+	default:
+	  throw error("Event.get_info", CL_INVALID_VALUE);
+	}
+    }
 
     // py::object get_profiling_info(cl_profiling_info param_name) const
     // {
@@ -823,10 +823,10 @@ namespace pyopencl
 
     PYOPENCL_EQUALITY_TESTS(context);
 
-    //       py::object get_info(cl_context_info param_name) const
-    //       {
-    //         switch (param_name)
-    // 	  {
+    generic_info get_info(cl_context_info param_name) const
+    {
+      switch (param_name)
+	{
     //           case CL_CONTEXT_REFERENCE_COUNT:
     //             PYOPENCL_GET_INTEGRAL_INFO(
     // 				       Context, m_context, param_name, cl_uint);
@@ -896,13 +896,10 @@ namespace pyopencl
     // 				       Context, m_context, param_name, cl_uint);
     // #endif
 
-    //           default:
-    //             throw error("Context.get_info", CL_INVALID_VALUE);
-    // 	  }
-    //       }
-    //     };
-
-
+	default:
+	  throw error("Context.get_info", CL_INVALID_VALUE);
+	}
+    }
 
     // }}}
 
@@ -969,27 +966,27 @@ namespace pyopencl
 
     PYOPENCL_EQUALITY_TESTS(command_queue);
 
-    // py::object get_info(cl_command_queue_info param_name) const
-    // {
-    //   switch (param_name)
-    //   {
-    //     case CL_QUEUE_CONTEXT:
-    //       PYOPENCL_GET_OPAQUE_INFO(CommandQueue, m_queue, param_name,
-    //           cl_context, context);
-    //     case CL_QUEUE_DEVICE:
-    //       PYOPENCL_GET_OPAQUE_INFO(CommandQueue, m_queue, param_name,
-    //           cl_device_id, device);
-    //     case CL_QUEUE_REFERENCE_COUNT:
-    //       PYOPENCL_GET_INTEGRAL_INFO(CommandQueue, m_queue, param_name,
-    //           cl_uint);
-    //     case CL_QUEUE_PROPERTIES:
-    //       PYOPENCL_GET_INTEGRAL_INFO(CommandQueue, m_queue, param_name,
-    //           cl_command_queue_properties);
-
-    //     default:
-    //       throw error("CommandQueue.get_info", CL_INVALID_VALUE);
-    //   }
-    // }
+    generic_info get_info(cl_command_queue_info param_name) const
+    {
+      switch (param_name)
+      {
+        // case CL_QUEUE_CONTEXT:
+        //   PYOPENCL_GET_OPAQUE_INFO(CommandQueue, m_queue, param_name,
+        //       cl_context, context);
+        // case CL_QUEUE_DEVICE:
+        //   PYOPENCL_GET_OPAQUE_INFO(CommandQueue, m_queue, param_name,
+        //       cl_device_id, device);
+        // case CL_QUEUE_REFERENCE_COUNT:
+        //   PYOPENCL_GET_INTEGRAL_INFO(CommandQueue, m_queue, param_name,
+        //       cl_uint);
+        // case CL_QUEUE_PROPERTIES:
+        //   PYOPENCL_GET_INTEGRAL_INFO(CommandQueue, m_queue, param_name,
+        //       cl_command_queue_properties);
+
+        default:
+          throw error("CommandQueue.get_info", CL_INVALID_VALUE);
+      }
+    }
 
     std::auto_ptr<context> get_context() const
     {
@@ -2041,13 +2038,7 @@ namespace pyopencl
     }
   }
 
-  ::error *platform__get_info(void *ptr_platform, cl_platform_info param, generic_info *out) {
-    C_HANDLE_ERROR(
-		   *out = static_cast<platform*>(ptr_platform)->get_info(param);
-		   )
-      return 0;
-  }
-
+ 
   ::error *platform__get_devices(void *ptr_platform, void **ptr_devices, uint32_t *num_devices, cl_device_type devtype) {
     typedef std::vector<cl_device_id> vec;
     C_HANDLE_ERROR(
@@ -2064,13 +2055,6 @@ namespace pyopencl
   }
 
 
-  ::error *device__get_info(void *ptr_device, cl_device_info param, generic_info *out) {
-    C_HANDLE_ERROR(
-		   *out = static_cast<device*>(ptr_device)->get_info(param);
-		   )
-      return 0;
-  }
-
   ::error *_create_context(void **ptr_ctx, cl_context_properties *properties, cl_uint num_devices, void **ptr_devices) {
     C_HANDLE_ERROR(
 		   cl_int status_code;
@@ -2173,14 +2157,6 @@ namespace pyopencl
   }
 
   
-  ::error *program__get_info(void *ptr_program, cl_program_info param, generic_info *out) {
-    C_HANDLE_ERROR(
-		   *out = static_cast<program*>(ptr_program)->get_info(param);
-		   )
-      return 0;
-  }
-
-  
   ::error *_create_kernel(void **ptr_kernel, void *ptr_program, char *name) {
     program *prg = static_cast<program*>(ptr_program);
     C_HANDLE_ERROR(
@@ -2189,13 +2165,6 @@ namespace pyopencl
       return 0;
   }
 
-  ::error *kernel__get_info(void *ptr_kernel, cl_kernel_info param, generic_info *out) {
-    C_HANDLE_ERROR(
-		   *out = static_cast<kernel*>(ptr_kernel)->get_info(param);
-		   )
-      return 0;
-  }
-
   ::error *kernel__set_arg_mem_buffer(void *ptr_kernel, cl_uint arg_index, void *ptr_buffer) {
     buffer *buf = static_cast<buffer*>(ptr_buffer);
     C_HANDLE_ERROR(
@@ -2226,13 +2195,6 @@ namespace pyopencl
       return 0;
   }
   
-  ::error *memory_object_holder__get_info(void *ptr_memory_object_holder, cl_mem_info param, generic_info *out) {
-    C_HANDLE_ERROR(
-		   *out = static_cast<memory_object_holder*>(ptr_memory_object_holder)->get_info(param);
-		   )
-      return 0;
-  }
-  
   intptr_t _int_ptr(void* ptr, class_t class_) {
 #define INT_PTR(CLSU, CLS) return (intptr_t)(static_cast<CLS*>(ptr)->data());
     SWITCHCLASS(INT_PTR);
@@ -2248,6 +2210,14 @@ namespace pyopencl
 #define HASH(CLSU, CLS)	return static_cast<CLS*>(ptr)->hash();
     SWITCHCLASS(HASH);
   }
+
+  
+  ::error *_get_info(void *ptr, class_t class_, cl_uint param, generic_info *out) {
+#define GET_INFO(CLSU, CLS) C_HANDLE_ERROR(*out = static_cast<CLS*>(ptr)->get_info(param);)
+    SWITCHCLASS(GET_INFO)
+      return 0;
+  }
+ 
   
 
 }
diff --git a/src/c_wrapper/wrap_cl_core.h b/src/c_wrapper/wrap_cl_core.h
index adf90029..d04c8657 100644
--- a/src/c_wrapper/wrap_cl_core.h
+++ b/src/c_wrapper/wrap_cl_core.h
@@ -24,9 +24,7 @@ typedef enum {
 
 int get_cl_version(void);
 error *get_platforms(void **ptr_platforms, uint32_t *num_platforms);
-error *platform__get_info(void *ptr_platform, cl_platform_info param_name, generic_info *out);
 error *platform__get_devices(void *ptr_platform, void **ptr_devices, uint32_t *num_devices, cl_device_type devtype);
-error *device__get_info(void *ptr_device, cl_device_info param_name, generic_info *out);
 error *_create_context(void **ptr_ctx, cl_context_properties *properties, cl_uint num_devices, void **ptr_devices);
 error *_create_command_queue(void **ptr_command_queue, void *ptr_context, void *ptr_device, cl_command_queue_properties properties);
 error *_create_buffer(void **ptr_buffer, void *ptr_context, cl_mem_flags flags, size_t size, void *hostbuf);
@@ -35,22 +33,20 @@ error *_create_program_with_binary(void **ptr_program, void *ptr_context, cl_uin
 error *program__build(void *ptr_program, char *options, cl_uint num_devices, void **ptr_devices);
 error *program__kind(void *ptr_program, int *kind);
 error *program__get_build_info(void *ptr_program, void *ptr_device, cl_program_build_info param, generic_info *out);
-error *program__get_info(void *ptr_program, cl_program_info param, generic_info *out);
 error *program__get_info__devices(void *ptr_program, void **ptr_devices, uint32_t *num_devices);
 error *program__get_info__binaries(void *ptr_program, char ***ptr_binaries, uint32_t *num_binaries);
 
 error *_create_kernel(void **ptr_kernel, void *ptr_program, char *name);
-error *kernel__get_info(void *ptr_kernel, cl_kernel_info param, generic_info *out);
 error *kernel__set_arg_mem_buffer(void *ptr_kernel, cl_uint arg_index, void *ptr_buffer);
 long _hash(void *ptr_platform, class_t);
 
 error *_enqueue_nd_range_kernel(void **ptr_event, void *ptr_command_queue, void *ptr_kernel, cl_uint work_dim, const size_t *global_work_offset, const size_t *global_work_size, const size_t *local_work_size);
 error *_enqueue_read_buffer(void **ptr_event, void *ptr_command_queue, void *ptr_memory_object_holder, void *buffer, size_t size, size_t device_offset, int is_blocking);
-error *memory_object_holder__get_info(void *ptr_memory_object_holder, cl_mem_info param, generic_info *out);
 void populate_constants(void(*add)(const char*, const char*, long value));
 
 intptr_t _int_ptr(void*, class_t);
 void* _from_int_ptr(void **ptr_out, intptr_t int_ptr_value, class_t);
+error *_get_info(void *ptr, class_t class_, cl_uint param, generic_info *out);
 
 void _free(void*);
 void _free2(void**, uint32_t size);
-- 
GitLab