From 5d00517fa262a2ccbc65ce1c631c2bca833c9a7d Mon Sep 17 00:00:00 2001
From: Jonathan Mackenzie <jonmac1@gmail.com>
Date: Thu, 8 Dec 2016 14:20:29 +1030
Subject: [PATCH] Resolves #159 Added module for scalar type mapping from
 OpenCL type names to numpy types. Refactores cl.arrays to use the new file to
 avoid redundancy

---
 pyopencl/array.py  | 15 ++-------------
 pyopencl/dtypes.py | 43 +++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 45 insertions(+), 13 deletions(-)
 create mode 100644 pyopencl/dtypes.py

diff --git a/pyopencl/array.py b/pyopencl/array.py
index a03227f7..479f5375 100644
--- a/pyopencl/array.py
+++ b/pyopencl/array.py
@@ -42,7 +42,7 @@ from pyopencl.compyte.array import (
         ArrayFlags as _ArrayFlags,
         get_common_dtype as _get_common_dtype_base)
 from pyopencl.characterize import has_double_support
-
+from pyopencl import dtypes
 
 def _get_common_dtype(obj1, obj2, queue):
     return _get_common_dtype_base(obj1, obj2,
@@ -78,18 +78,7 @@ def _create_vector_types():
 
     counts = [2, 3, 4, 8, 16]
 
-    for base_name, base_type in [
-            ('char', np.int8),
-            ('uchar', np.uint8),
-            ('short', np.int16),
-            ('ushort', np.uint16),
-            ('int', np.int32),
-            ('uint', np.uint32),
-            ('long', np.int64),
-            ('ulong', np.uint64),
-            ('float', np.float32),
-            ('double', np.float64),
-            ]:
+    for base_name, base_type in cl.dtypes.cl2np_mapping:
         for count in counts:
             name = "%s%d" % (base_name, count)
 
diff --git a/pyopencl/dtypes.py b/pyopencl/dtypes.py
new file mode 100644
index 00000000..b7f32a55
--- /dev/null
+++ b/pyopencl/dtypes.py
@@ -0,0 +1,43 @@
+# encoding: utf8
+import numpy as __np
+
+__copyright__ = "Copyright (C) 2016 Jonathan Mackenzie"
+
+__license__ = """
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+The above copyright notice and this permission notice shall be included in
+all copies or substantial portions of the Software.
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+THE SOFTWARE.
+"""
+
+"""
+This file provides a type mapping from OpenCl type names to their numpy equivalents
+"""
+
+char = __np.int8
+uchar = __np.uint8
+short = __np.int16
+ushort = __np.uint16
+int = __np.int32
+uint = __np.uint32
+long = __np.int64
+ulong = __np.uint64
+float = __np.float32
+double = __np.float64
+
+# {{{ cl -> np type mapping
+cl2np_mapping = [
+    (k, v) for k, v in globals().items() if not k.startswith('__')
+    ]
+# }}}
\ No newline at end of file
-- 
GitLab