-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathdatatypes.py
79 lines (64 loc) · 2.21 KB
/
datatypes.py
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
import ctypes
import loopy as lp
import numpy
from petsc4py.PETSc import IntType, RealType, ScalarType
IntType = numpy.dtype(IntType)
RealType = numpy.dtype(RealType)
ScalarType = numpy.dtype(ScalarType)
def as_cstr(dtype):
"""Convert a numpy dtype like object to a C type as a string."""
return {"bool": "unsigned char",
"int": "int",
"int8": "int8_t",
"int16": "int16_t",
"int32": "int32_t",
"int64": "int64_t",
"uint8": "uint8_t",
"uint16": "uint16_t",
"uint32": "uint32_t",
"uint64": "uint64_t",
"float32": "float",
"float64": "double",
"complex128": "double complex"}[numpy.dtype(dtype).name]
def as_ctypes(dtype):
"""Convert a numpy dtype like object to a ctypes type."""
return {"bool": ctypes.c_bool,
"int": ctypes.c_int,
"int8": ctypes.c_char,
"int16": ctypes.c_int16,
"int32": ctypes.c_int32,
"int64": ctypes.c_int64,
"uint8": ctypes.c_ubyte,
"uint16": ctypes.c_uint16,
"uint32": ctypes.c_uint32,
"uint64": ctypes.c_uint64,
"float32": ctypes.c_float,
"float64": ctypes.c_double}[numpy.dtype(dtype).name]
def as_numpy_dtype(dtype):
"""Convert a dtype-like object into a numpy dtype."""
if isinstance(dtype, numpy.dtype):
return dtype
elif isinstance(dtype, lp.types.NumpyType):
return dtype.numpy_dtype
else:
raise ValueError
def dtype_limits(dtype):
"""Attempt to determine the min and max values of a datatype.
:arg dtype: A numpy datatype.
:returns: a 2-tuple of min, max
:raises ValueError: If numeric limits could not be determined.
"""
try:
info = numpy.finfo(dtype)
except ValueError:
# maybe an int?
try:
info = numpy.iinfo(dtype)
except ValueError as e:
raise ValueError("Unable to determine numeric limits from %s" % dtype) from e
return info.min, info.max
class OpaqueType(lp.types.OpaqueType):
def __init__(self, name):
super().__init__(name=name)
def __repr__(self):
return self.name