forked from Theano/libgpuarray
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdtypes.py
More file actions
218 lines (164 loc) · 6.58 KB
/
dtypes.py
File metadata and controls
218 lines (164 loc) · 6.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
"""Type mapping helpers."""
from __future__ import division
import numpy as np
from . import gpuarray
__copyright__ = "Copyright (C) 2011 Andreas Kloeckner"
__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.
"""
# {{{ registry
NAME_TO_DTYPE = {}
def register_dtype(dtype, c_names):
"""
Associate a numpy dtype with its C equivalents.
:param dtype: type to associate
:type dtype: numpy.dtype or string
:param c_names: list of C type names
:type c_names: str or list
Will register `dtype` for use with the gpuarray module. If the
c_names argument is a list then the first element of that list is
taken as the primary association and will be used for generated C
code. The other types will be mapped to the provided dtype when
going in the other direction.
"""
if isinstance(c_names, str):
c_names = [c_names]
dtype = np.dtype(dtype)
# register if not already there
try:
gpuarray.dtype_to_ctype(dtype)
except ValueError:
gpuarray.register_dtype(dtype, c_names[0])
for nm in c_names:
if nm in NAME_TO_DTYPE and NAME_TO_DTYPE[nm] != dtype:
raise RuntimeError("name '%s' already registered" % nm)
NAME_TO_DTYPE[nm] = dtype
def _fill_dtype_registry(respect_windows):
from sys import platform
register_dtype(np.bool, ["ga_bool", "bool"])
register_dtype(np.int8, ["ga_byte", "char", "signed char"])
register_dtype(np.uint8, ["ga_ubyte", "unsigned char"])
register_dtype(np.int16, ["ga_short", "short", "signed short", "signed short int", "short signed int"])
register_dtype(np.uint16, ["ga_ushort", "unsigned short", "unsigned short int", "short unsigned int"])
register_dtype(np.int32, ["ga_int", "int", "signed int"])
register_dtype(np.uint32, ["ga_uint", "unsigned", "unsigned int"])
register_dtype(np.int64, ["ga_long"])
register_dtype(np.uint64, ["ga_ulong"])
is_64_bit = tuple.__itemsize__ * 8 == 64
if is_64_bit:
if 'win32' in platform and respect_windows:
i64_name = "long long"
else:
i64_name = "long"
register_dtype(np.int64, [i64_name, "%s int" % i64_name,
"signed %s int" % i64_name,
"%s signed int" % i64_name])
register_dtype(np.uint64, ["unsigned %s" % i64_name,
"unsigned %s int" % i64_name,
"%s unsigned int" % i64_name])
# According to this uintp may not have the same hash as uint32:
# http://projects.scipy.org/numpy/ticket/2017
# Failing tests tell me this is the case for intp too.
if is_64_bit:
register_dtype(np.intp, ["ga_long"])
register_dtype(np.uintp, ["ga_ulong"])
else:
register_dtype(np.intp, ["ga_int"])
register_dtype(np.uintp, ["ga_uint"])
register_dtype(np.float32, ["ga_float", "float"])
register_dtype(np.float64, ["ga_double", "double"])
# }}}
# {{{ dtype -> ctype
def dtype_to_ctype(dtype, with_fp_tex_hack=False):
"""
Return the C type that corresponds to `dtype`.
:param dtype: a numpy dtype
"""
if dtype is None:
raise ValueError("dtype may not be None")
dtype = np.dtype(dtype)
if with_fp_tex_hack:
if dtype == np.float32:
return "fp_tex_float"
elif dtype == np.float64:
return "fp_tex_double"
return gpuarray.dtype_to_ctype(dtype)
# }}}
# {{{ c declarator parsing
def parse_c_arg_backend(c_arg, scalar_arg_class, vec_arg_class):
c_arg = c_arg.replace("const", "").replace("volatile", "")
# process and remove declarator
import re
decl_re = re.compile(r"(\**)\s*([_a-zA-Z0-9]+)(\s*\[[ 0-9]*\])*\s*$")
decl_match = decl_re.search(c_arg)
if decl_match is None:
raise ValueError("couldn't parse C declarator '%s'" % c_arg)
name = decl_match.group(2)
if decl_match.group(1) or decl_match.group(3) is not None:
arg_class = vec_arg_class
else:
arg_class = scalar_arg_class
tp = c_arg[:decl_match.start()]
tp = " ".join(tp.split())
try:
dtype = NAME_TO_DTYPE[tp]
except KeyError:
raise ValueError("unknown type '%s'" % tp)
return arg_class(dtype, name)
# }}}
def get_np_obj(obj):
"""
Returns a numpy object of the same dtype and comportement as the
source suitable for output dtype determination.
This is used since the casting rules of numpy are rather obscure
and the best way to imitate them is to try an operation ans see
what it does.
"""
if isinstance(obj, np.ndarray) and obj.shape == ():
return obj
try:
return np.ones(1, dtype=obj.dtype)
except AttributeError:
return np.asarray(obj)
def get_common_dtype(obj1, obj2, allow_double):
"""
Returns the proper output type for a numpy operation involving the
two provided objects. This may not be suitable for certain
obscure numpy operations.
If `allow_double` is False, a return type of float64 will be
forced to float32 and complex128 will be forced to complex64.
"""
# Yes, numpy behaves differently depending on whether
# we're dealing with arrays or scalars.
np1 = get_np_obj(obj1)
np2 = get_np_obj(obj2)
result = (np1 + np2).dtype
if not allow_double:
if result == np.float64:
result = np.dtype(np.float32)
elif result == np.complex128:
result = np.dtype(np.complex64)
return result
def upcast(*args):
a = np.array([0], dtype=args[0])
for t in args[1:]:
a = a + np.array([0], dtype=t)
return a.dtype
# vim: foldmethod=marker