/
_cuda_types.py
226 lines (166 loc) · 5.98 KB
/
_cuda_types.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
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
219
220
221
222
223
224
225
226
from typing import Mapping, Optional, Sequence, Union, TYPE_CHECKING
import numpy
import numpy.typing as npt
import cupy
from cupy._core._scalar import get_typename
if TYPE_CHECKING:
from cupyx.jit._internal_types import Data
# Base class for cuda types.
class TypeBase:
def __str__(self) -> str:
raise NotImplementedError
def declvar(self, x: str, init: Optional['Data']) -> str:
if init is None:
return f'{self} {x}'
return f'{self} {x} = {init.code}'
def assign(self, var: 'Data', value: 'Data') -> str:
return f'{var.code} = {value.code}'
class Void(TypeBase):
def __init__(self) -> None:
pass
def __str__(self) -> str:
return 'void'
class Scalar(TypeBase):
def __init__(self, dtype: npt.DTypeLike) -> None:
self.dtype = numpy.dtype(dtype)
def __str__(self) -> str:
dtype = self.dtype
if dtype == numpy.float16:
# For the performance
dtype = numpy.dtype('float32')
return get_typename(dtype)
def __eq__(self, other: object) -> bool:
assert isinstance(other, TypeBase)
return isinstance(other, Scalar) and self.dtype == other.dtype
def __hash__(self) -> int:
return hash(self.dtype)
class PtrDiff(Scalar):
def __init__(self) -> None:
super().__init__('q')
def __str__(self) -> str:
return 'ptrdiff_t'
class ArrayBase(TypeBase):
def __init__(self, child_type: TypeBase, ndim: int) -> None:
assert isinstance(child_type, TypeBase)
self.child_type = child_type
self.ndim = ndim
class CArray(ArrayBase):
def __init__(
self,
dtype: npt.DTypeLike,
ndim: int,
is_c_contiguous: bool,
index_32_bits: bool,
) -> None:
self.dtype = numpy.dtype(dtype)
self._c_contiguous = is_c_contiguous
self._index_32_bits = index_32_bits
super().__init__(Scalar(dtype), ndim)
@classmethod
def from_ndarray(cls, x: cupy.ndarray) -> 'CArray':
return CArray(x.dtype, x.ndim, x._c_contiguous, x._index_32_bits)
def __str__(self) -> str:
ctype = get_typename(self.dtype)
c_contiguous = get_cuda_code_from_constant(self._c_contiguous, bool_)
index_32_bits = get_cuda_code_from_constant(self._index_32_bits, bool_)
return f'CArray<{ctype}, {self.ndim}, {c_contiguous}, {index_32_bits}>'
def __eq__(self, other: object) -> bool:
assert isinstance(other, TypeBase)
return (
isinstance(other, CArray) and
self.dtype == other.dtype and
self.ndim == other.ndim and
self._c_contiguous == other._c_contiguous and
self._index_32_bits == other._index_32_bits
)
def __hash__(self) -> int:
return hash(
(self.dtype, self.ndim, self._c_contiguous, self._index_32_bits))
class SharedMem(ArrayBase):
def __init__(
self,
child_type: TypeBase,
size: Optional[int],
alignment: Optional[int] = None,
) -> None:
if not (isinstance(size, int) or size is None):
raise 'size of shared_memory must be integer or `None`'
if not (isinstance(alignment, int) or alignment is None):
raise 'alignment must be integer or `None`'
self._size = size
self._alignment = alignment
super().__init__(child_type, 1)
def declvar(self, x: str, init: Optional['Data']) -> str:
assert init is None
if self._alignment is not None:
code = f'__align__({self._alignment})'
else:
code = ''
if self._size is None:
code = f'extern {code} __shared__ {self.child_type} {x}[]'
else:
code = f'{code} __shared__ {self.child_type} {x}[{self._size}]'
return code
class Ptr(ArrayBase):
def __init__(self, child_type: TypeBase) -> None:
super().__init__(child_type, 1)
def __str__(self) -> str:
return f'{self.child_type}*'
class Tuple(TypeBase):
def __init__(self, types: Sequence[TypeBase]) -> None:
self.types = types
def __str__(self) -> str:
types = ', '.join([str(t) for t in self.types])
return f'thrust::tuple<{types}>'
def __eq__(self, other: object) -> bool:
assert isinstance(other, TypeBase)
return isinstance(other, Tuple) and self.types == other.types
void: Void = Void()
bool_: Scalar = Scalar(numpy.bool_)
int32: Scalar = Scalar(numpy.int32)
uint32: Scalar = Scalar(numpy.uint32)
uint64: Scalar = Scalar(numpy.uint64)
class Dim3(TypeBase):
"""
An integer vector type based on uint3 that is used to specify dimensions.
Attributes:
x (uint32)
y (uint32)
z (uint32)
"""
def x(self, code: str) -> 'Data':
from cupyx.jit import _internal_types # avoid circular import
return _internal_types.Data(f'{code}.x', uint32)
def y(self, code: str) -> 'Data':
from cupyx.jit import _internal_types # avoid circular import
return _internal_types.Data(f'{code}.y', uint32)
def z(self, code: str) -> 'Data':
from cupyx.jit import _internal_types # avoid circular import
return _internal_types.Data(f'{code}.z', uint32)
def __str__(self) -> str:
return 'dim3'
dim3: Dim3 = Dim3()
_suffix_literals_dict: Mapping[str, str] = {
'float64': '',
'float32': 'f',
'int64': 'll',
'int32': '',
'uint64': 'ull',
'uint32': 'u',
'bool': '',
}
def get_cuda_code_from_constant(
x: Union[bool, int, float, complex],
ctype: Scalar,
) -> str:
dtype = ctype.dtype
suffix_literal = _suffix_literals_dict.get(dtype.name)
if suffix_literal is not None:
s = str(x).lower()
return f'{s}{suffix_literal}'
ctype_str = str(ctype)
if dtype.kind == 'c':
return f'{ctype_str}({x.real}, {x.imag})'
if ' ' in ctype_str:
return f'({ctype_str}){x}'
return f'{ctype_str}({x})'