This repository was archived by the owner on Jan 26, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtest_setget.py
More file actions
221 lines (175 loc) · 6.39 KB
/
test_setget.py
File metadata and controls
221 lines (175 loc) · 6.39 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
219
220
221
import numpy
import pytest
from utils import device, runAndCompare
import sharpy as sp
class TestSetGet:
@pytest.mark.skip(reason="FIXME")
def test_get_item(self):
for i in range(1, 5):
N = (10**i) * 16
print(N, N // 4, (N // 4) * 3)
def doit(aapi, **kwargs):
a = aapi.ones((N, N), aapi.float32, **kwargs)
return a[0:N:2, N // 4 : (N // 4) * 3 : 2]
assert runAndCompare(doit)
def test_setitem1(self):
def doit(aapi, **kwargs):
a = aapi.ones((16, 16), aapi.float32, **kwargs)
a[0:16:2, 0:16:2] = aapi.zeros([8, 8], aapi.float32, **kwargs)
return a
assert runAndCompare(doit)
def test_setitem2(self):
def doit(aapi, **kwargs):
a = aapi.ones((16, 16), aapi.float32, **kwargs)
b = aapi.fromfunction(
lambda i, j: 10 * i + j, (16, 16), dtype=aapi.float32, **kwargs
)
a[1:8, 0:6] = b[0:7, 0:6]
return a
assert runAndCompare(doit)
def test_setitem3(self):
def doit(aapi, **kwargs):
a = aapi.ones((16, 16), aapi.float32, **kwargs)
b = aapi.fromfunction(
lambda i, j: 10 * i + j, (16, 16), dtype=aapi.float32, **kwargs
)
a[7:16:3, 4:10:2] = b[4:7, 10:16:2]
return a
assert runAndCompare(doit)
def test_setitem4(self):
# Note: test halo update without send buffer
def doit(aapi, **kwargs):
a = aapi.ones((16, 16), aapi.float32, **kwargs)
b = aapi.fromfunction(
lambda i, j: 10 * i + j, (16, 16), dtype=aapi.float32, **kwargs
)
a[7:16:3, 0:16] = b[4:7, 0:16]
return a
assert runAndCompare(doit)
def test_setitem5(self):
# Note: test assignment to one full local part
def doit(aapi, **kwargs):
a = aapi.fromfunction(
lambda i, j: 10 * i + j, (16, 16), dtype=aapi.int32, **kwargs
)
a[0:10, 4:11] = a[0:10, 4:11]
return a
assert runAndCompare(doit)
def test_setitem6(self):
def doit(aapi, **kwargs):
n = 16
a = aapi.fromfunction(
lambda i, j: i, (n, n), dtype=aapi.float32, **kwargs
)
b = aapi.zeros((n + 1, n + 1), aapi.float32, **kwargs)
b[1:n, 1:n] = a[1:n, 1:n]
return b
assert runAndCompare(doit)
def test_setitem7(self):
# Note: assert halo does not segfault
def doit(aapi, **kwargs):
n = 1024
a = aapi.fromfunction(
lambda i, j: i, (n, n), dtype=aapi.float32, **kwargs
)
b = aapi.zeros((n, n), aapi.float32, **kwargs)
b[1:n, 1:n] = a[1:n, 1:n]
b[0, 1:n] = a[0, 1:n]
return b
assert runAndCompare(doit)
def test_setitem8(self):
# Note: assert halo does not segfault
def doit(aapi, **kwargs):
n = 1024
a = aapi.fromfunction(
lambda i, j: i, (n, n), dtype=aapi.float32, **kwargs
)
b = aapi.zeros((n, n), aapi.float32, **kwargs)
b[0, 1:n] = 1.0 * a[0, 1:n]
b[1 : n - 1, 1:n] = 1.0 * a[1 : n - 1, 1:n]
b[n - 1, 1:n] = 1.0 * a[n - 1, 1:n]
return b
assert runAndCompare(doit)
def test_setitem9(self):
N = 3
r1 = sp.random.rand(N)
a = sp.zeros((N, 2))
r11 = sp.reshape(r1, (N, 1))
# strided array
a[:, 0] = r11
np_r1 = sp.to_numpy(r1)
b = numpy.zeros((N, 2))
b[:, 0] = np_r1
assert numpy.allclose(sp.to_numpy(a), b)
def test_colon(self):
a = sp.ones((16, 16), sp.float32, device=device)
b = sp.zeros((16, 16), sp.float32, device=device)
a[:, :] = b[:, :]
r1 = sp.sum(a)
assert float(r1) == 0
def test_neg_last(self):
a = sp.arange(0, 16, 1, sp.int32, device=device)
b = a[-1]
assert int(b) == 15
def test_neg_end(self):
def doit(aapi, **kwargs):
a = aapi.arange(0, 16, 1, aapi.int32, **kwargs)
b = aapi.arange(2, 18, 1, aapi.int32, **kwargs)
a[0:10] = b[:-6]
return a
assert runAndCompare(doit)
def test_neg_start(self):
def doit(aapi, **kwargs):
a = aapi.arange(0, 16, 1, aapi.int32, **kwargs)
b = aapi.arange(2, 18, 1, aapi.int32, **kwargs)
a[0:4] = b[-4:]
return a
assert runAndCompare(doit)
def test_neg_slice(self):
def doit(aapi, **kwargs):
a = aapi.ones((16, 16), aapi.float32, **kwargs)
b = aapi.zeros((16, 16), aapi.float32, **kwargs)
a[:-1, 0:3] = b[:-1, -4:-1]
return a
assert runAndCompare(doit)
@pytest.mark.skip(reason="FIXME multi-proc")
def test_neg_stride(self):
def doit(aapi, **kwargs):
a = aapi.arange(0, 8, 1, aapi.int32, **kwargs)
b = a[8:0:-1]
return b
assert runAndCompare(doit)
@pytest.mark.skip(reason="FIXME")
def test_reverse(self):
def doit(aapi, **kwargs):
a = aapi.arange(0, 8, 1, aapi.int32, **kwargs)
b = a[::-1]
return b
assert runAndCompare(doit)
def test_assign_bcast_scalar(self):
a = sp.zeros((16, 16), sp.int32, device=device)
b = 2
a[:, :] = b
a2 = sp.to_numpy(a)
assert numpy.all(a2 == 2)
@pytest.fixture(
params=[
((6,), (slice(6, None))),
((6,), (slice(7, 10))),
((6, 5), (slice(7, None), slice(None, None))),
((6, 5), (slice(None, None), slice(6, None))),
((6, 5, 4), (slice(None, None), slice(None, None), slice(6, None))),
],
)
def shape_and_slices(self, request):
return request.param[0], request.param[1]
def test_get_invalid_bounds(self, shape_and_slices):
shape, slices = shape_and_slices
with pytest.raises(IndexError):
a = sp.ones(shape, dtype=sp.float64)
a[slices]
def test_set_invalid_bounds(self, shape_and_slices):
shape, slices = shape_and_slices
with pytest.raises(IndexError):
a = sp.ones(shape, dtype=sp.float64)
a[slices] = 1.0