FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
libgpuarray/pygpu/_array.py at lowerC99 · NeutralCode/libgpuarray · GitHub
NeutralCode
/
libgpuarray
Public
forked from
Theano/libgpuarray
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Wiki
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
libgpuarray
/
pygpu
/
_array.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
294 lines (241 loc) · 10.1 KB
Breadcrumbs
libgpuarray
/
pygpu
/
_array.py
Copy path
File metadata and controls
294 lines (241 loc) · 10.1 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
from
__future__
import
division
import
numpy
as
np
from
.
elemwise
import
elemwise1
,
elemwise2
,
ielemwise2
,
compare
,
arg
,
GpuElemwise
,
as_argument
from
.
reduction
import
reduce1
from
.
dtypes
import
dtype_to_ctype
,
get_np_obj
,
get_common_dtype
from
.
import
gpuarray
class
ndgpuarray
(
gpuarray
.
GpuArray
):
"""
Extension class for gpuarray.GpuArray to add numpy mathematical
operations between arrays. These operations are all performed on
the GPU but this is not the most efficient way since it will
involve the creation of temporaries (just like numpy) for all
intermediate results.
This class may help transition code from numpy to pygpu by acting
more like a drop-in replacement for numpy.ndarray than the raw
GpuArray class.
"""
# add
def
__add__
(
self
,
other
):
return
elemwise2
(
self
,
'+'
,
other
,
self
,
broadcast
=
True
)
def
__radd__
(
self
,
other
):
return
elemwise2
(
other
,
'+'
,
self
,
self
,
broadcast
=
True
)
def
__iadd__
(
self
,
other
):
return
ielemwise2
(
self
,
'+'
,
other
,
broadcast
=
True
)
# sub
def
__sub__
(
self
,
other
):
return
elemwise2
(
self
,
'-'
,
other
,
self
,
broadcast
=
True
)
def
__rsub__
(
self
,
other
):
return
elemwise2
(
other
,
'-'
,
self
,
self
,
broadcast
=
True
)
def
__isub__
(
self
,
other
):
return
ielemwise2
(
self
,
'-'
,
other
,
broadcast
=
True
)
# mul
def
__mul__
(
self
,
other
):
return
elemwise2
(
self
,
'*'
,
other
,
self
,
broadcast
=
True
)
def
__rmul__
(
self
,
other
):
return
elemwise2
(
other
,
'*'
,
self
,
self
,
broadcast
=
True
)
def
__imul__
(
self
,
other
):
return
ielemwise2
(
self
,
'*'
,
other
,
broadcast
=
True
)
# div
def
__div__
(
self
,
other
):
return
elemwise2
(
self
,
'/'
,
other
,
self
,
broadcast
=
True
)
def
__rdiv__
(
self
,
other
):
return
elemwise2
(
other
,
'/'
,
self
,
self
,
broadcast
=
True
)
def
__idiv__
(
self
,
other
):
return
ielemwise2
(
self
,
'/'
,
other
,
broadcast
=
True
)
# truediv
def
__truediv__
(
self
,
other
):
np1
=
get_np_obj
(
self
)
np2
=
get_np_obj
(
other
)
res
=
(
np1
.
__truediv__
(
np2
)).
dtype
return
elemwise2
(
self
,
'/'
,
other
,
self
,
odtype
=
res
,
broadcast
=
True
)
def
__rtruediv__
(
self
,
other
):
np1
=
get_np_obj
(
self
)
np2
=
get_np_obj
(
other
)
res
=
(
np2
.
__truediv__
(
np1
)).
dtype
return
elemwise2
(
other
,
'/'
,
self
,
self
,
odtype
=
res
,
broadcast
=
True
)
def
__itruediv__
(
self
,
other
):
np2
=
get_np_obj
(
other
)
kw
=
{
'broadcast'
:
True
}
if
self
.
dtype
==
np
.
float32
or
np2
.
dtype
==
np
.
float32
:
kw
[
'op_tmpl'
]
=
"a = (float)a / (float)b"
if
self
.
dtype
==
np
.
float64
or
np2
.
dtype
==
np
.
float64
:
kw
[
'op_tmpl'
]
=
"a = (double)a / (double)b"
return
ielemwise2
(
self
,
'/'
,
other
,
**
kw
)
# floordiv
def
__floordiv__
(
self
,
other
):
out_dtype
=
get_common_dtype
(
self
,
other
,
True
)
kw
=
{
'broadcast'
:
True
}
if
out_dtype
.
kind
==
'f'
:
kw
[
'op_tmpl'
]
=
"res = floor((%(out_t)s)a / (%(out_t)s)b)"
return
elemwise2
(
self
,
'/'
,
other
,
self
,
odtype
=
out_dtype
,
**
kw
)
def
__rfloordiv__
(
self
,
other
):
out_dtype
=
get_common_dtype
(
other
,
self
,
True
)
kw
=
{
'broadcast'
:
True
}
if
out_dtype
.
kind
==
'f'
:
kw
[
'op_tmpl'
]
=
"res = floor((%(out_t)s)a / (%(out_t)s)b)"
return
elemwise2
(
other
,
'/'
,
self
,
self
,
odtype
=
out_dtype
,
**
kw
)
def
__ifloordiv__
(
self
,
other
):
out_dtype
=
self
.
dtype
kw
=
{
'broadcast'
:
True
}
if
out_dtype
==
np
.
float32
:
kw
[
'op_tmpl'
]
=
"a = floor((float)a / (float)b)"
if
out_dtype
==
np
.
float64
:
kw
[
'op_tmpl'
]
=
"a = floor((double)a / (double)b)"
return
ielemwise2
(
self
,
'/'
,
other
,
**
kw
)
# mod
def
__mod__
(
self
,
other
):
out_dtype
=
get_common_dtype
(
self
,
other
,
True
)
kw
=
{
'broadcast'
:
True
}
if
out_dtype
.
kind
==
'f'
:
kw
[
'op_tmpl'
]
=
"res = fmod((%(out_t)s)a, (%(out_t)s)b)"
return
elemwise2
(
self
,
'%'
,
other
,
self
,
odtype
=
out_dtype
,
**
kw
)
def
__rmod__
(
self
,
other
):
out_dtype
=
get_common_dtype
(
other
,
self
,
True
)
kw
=
{
'broadcast'
:
True
}
if
out_dtype
.
kind
==
'f'
:
kw
[
'op_tmpl'
]
=
"res = fmod((%(out_t)s)a, (%(out_t)s)b)"
return
elemwise2
(
other
,
'%'
,
self
,
self
,
odtype
=
out_dtype
,
**
kw
)
def
__imod__
(
self
,
other
):
out_dtype
=
get_common_dtype
(
self
,
other
,
self
.
dtype
==
np
.
float64
)
kw
=
{
'broadcast'
:
True
}
if
out_dtype
==
np
.
float32
:
kw
[
'op_tmpl'
]
=
"a = fmod((float)a, (float)b)"
if
out_dtype
==
np
.
float64
:
kw
[
'op_tmpl'
]
=
"a = fmod((double)a, (double)b)"
return
ielemwise2
(
self
,
'%'
,
other
,
**
kw
)
# divmod
def
__divmod__
(
self
,
other
):
if
not
isinstance
(
other
,
gpuarray
.
GpuArray
):
other
=
np
.
asarray
(
other
)
odtype
=
get_common_dtype
(
self
,
other
,
True
)
a_arg
=
as_argument
(
self
,
'a'
,
read
=
True
)
b_arg
=
as_argument
(
other
,
'b'
,
read
=
True
)
args
=
[
arg
(
'div'
,
odtype
,
write
=
True
),
arg
(
'mod'
,
odtype
,
write
=
True
),
a_arg
,
b_arg
]
div
=
self
.
_empty_like_me
(
dtype
=
odtype
)
mod
=
self
.
_empty_like_me
(
dtype
=
odtype
)
if
odtype
.
kind
==
'f'
:
tmpl
=
"div = floor((%(out_t)s)a / (%(out_t)s)b),"
\
"mod = fmod((%(out_t)s)a, (%(out_t)s)b)"
else
:
tmpl
=
"div = (%(out_t)s)a / (%(out_t)s)b,"
\
"mod = a %% b"
ksrc
=
tmpl
%
{
'out_t'
:
dtype_to_ctype
(
odtype
)}
k
=
GpuElemwise
(
self
.
context
,
ksrc
,
args
)
k
(
div
,
mod
,
self
,
other
,
broadcast
=
True
)
return
(
div
,
mod
)
def
__rdivmod__
(
self
,
other
):
if
not
isinstance
(
other
,
gpuarray
.
GpuArray
):
other
=
np
.
asarray
(
other
)
odtype
=
get_common_dtype
(
other
,
self
,
True
)
a_arg
=
as_argument
(
other
,
'a'
,
read
=
True
)
b_arg
=
as_argument
(
self
,
'b'
,
read
=
True
)
args
=
[
arg
(
'div'
,
odtype
,
write
=
True
),
arg
(
'mod'
,
odtype
,
write
=
True
),
a_arg
,
b_arg
]
div
=
self
.
_empty_like_me
(
dtype
=
odtype
)
mod
=
self
.
_empty_like_me
(
dtype
=
odtype
)
if
odtype
.
kind
==
'f'
:
tmpl
=
"div = floor((%(out_t)s)a / (%(out_t)s)b),"
\
"mod = fmod((%(out_t)s)a, (%(out_t)s)b)"
else
:
tmpl
=
"div = (%(out_t)s)a / (%(out_t)s)b,"
\
"mod = a %% b"
ksrc
=
tmpl
%
{
'out_t'
:
dtype_to_ctype
(
odtype
)}
k
=
GpuElemwise
(
self
.
context
,
ksrc
,
args
)
k
(
div
,
mod
,
other
,
self
,
broadcast
=
True
)
return
(
div
,
mod
)
def
__neg__
(
self
):
return
elemwise1
(
self
,
'-'
)
def
__pos__
(
self
):
return
elemwise1
(
self
,
'+'
)
def
__abs__
(
self
):
if
self
.
dtype
.
kind
==
'u'
:
return
self
.
copy
()
if
self
.
dtype
.
kind
==
'f'
:
oper
=
"res = fabs(a)"
elif
self
.
dtype
.
itemsize
<
4
:
# cuda 5.5 finds the c++ stdlib definition if we don't cast here.
oper
=
"res = abs((int)a)"
else
:
oper
=
"res = abs(a)"
return
elemwise1
(
self
,
None
,
oper
=
oper
)
# richcmp
def
__lt__
(
self
,
other
):
return
compare
(
self
,
'<'
,
other
,
broadcast
=
True
)
def
__le__
(
self
,
other
):
return
compare
(
self
,
'<='
,
other
,
broadcast
=
True
)
def
__eq__
(
self
,
other
):
return
compare
(
self
,
'=='
,
other
,
broadcast
=
True
)
def
__ne__
(
self
,
other
):
return
compare
(
self
,
'!='
,
other
,
broadcast
=
True
)
def
__ge__
(
self
,
other
):
return
compare
(
self
,
'>='
,
other
,
broadcast
=
True
)
def
__gt__
(
self
,
other
):
return
compare
(
self
,
'>'
,
other
,
broadcast
=
True
)
# misc other things
@
property
def
T
(
self
):
if
self
.
ndim
<
2
:
return
self
return
self
.
transpose
()
"""
Since these functions are untested (thus probably wrong), we disable them.
def clip(self, a_min, a_max, out=None):
oper=('res = a > %(max)s ? %(max)s : '
'(a < %(min)s ? %(min)s : a)' % dict(min=a_min, max=a_max))
return elemwise1(self, '', oper=oper, out=out)
def fill(self, value):
self[...] = value
"""
# reductions
def
all
(
self
,
axis
=
None
,
out
=
None
):
if
self
.
ndim
==
0
:
return
self
.
copy
()
return
reduce1
(
self
,
'&&'
,
'1'
,
np
.
dtype
(
'bool'
),
axis
=
axis
,
out
=
out
)
def
any
(
self
,
axis
=
None
,
out
=
None
):
if
self
.
ndim
==
0
:
return
self
.
copy
()
return
reduce1
(
self
,
'||'
,
'0'
,
np
.
dtype
(
'bool'
),
axis
=
axis
,
out
=
out
)
def
prod
(
self
,
axis
=
None
,
dtype
=
None
,
out
=
None
):
if
dtype
is
None
:
dtype
=
self
.
dtype
# we only upcast integers that are smaller than the plaform default
if
dtype
.
kind
==
'i'
:
di
=
np
.
dtype
(
'int'
)
if
di
.
itemsize
>
dtype
.
itemsize
:
dtype
=
di
if
dtype
.
kind
==
'u'
:
di
=
np
.
dtype
(
'uint'
)
if
di
.
itemsize
>
dtype
.
itemsize
:
dtype
=
di
return
reduce1
(
self
,
'*'
,
'1'
,
dtype
,
axis
=
axis
,
out
=
out
)
# def max(self, axis=None, out=None);
# nd = self.ndim
# if nd == 0:
# return self.copy()
# idx = (0,) * nd
# n = str(self.__getitem__(idx).__array__())
# return reduce1(self, '', n, self.dtype, axis=axis, out=out,
# oper='max(a, b)')
# def min(self, axis=None, out=None):
# nd = self.ndim
# if nd == 0:
# return self.copy()
# idx = (0,) * nd
# n = str(self.__getitem__(idx).__array__())
# return reduce1(self, '', n, self.dtype, axis=axis, out=out,
# oper='min(a, b)')
def
sum
(
self
,
axis
=
None
,
dtype
=
None
,
out
=
None
):
if
dtype
is
None
:
dtype
=
self
.
dtype
# we only upcast integers that are smaller than the plaform default
if
dtype
.
kind
==
'i'
:
di
=
np
.
dtype
(
'int'
)
if
di
.
itemsize
>
dtype
.
itemsize
:
dtype
=
di
if
dtype
.
kind
==
'u'
:
di
=
np
.
dtype
(
'uint'
)
if
di
.
itemsize
>
dtype
.
itemsize
:
dtype
=
di
return
reduce1
(
self
,
'+'
,
'0'
,
dtype
,
axis
=
axis
,
out
=
out
)
Back
|
FazBrowse Home
|
New Git URL