-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathtest_ss_utils.py
More file actions
296 lines (259 loc) · 10.8 KB
/
Copy pathtest_ss_utils.py
File metadata and controls
296 lines (259 loc) · 10.8 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
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
295
296
import numpy as np
import pytest
from numpy.testing import assert_array_equal
import graphblas as gb
from graphblas import Matrix, Vector, backend
from graphblas.exceptions import InvalidValue
if backend != "suitesparse":
pytest.skip("gb.ss and A.ss only available with suitesparse backend", allow_module_level=True)
@pytest.mark.parametrize("do_iso", [False, True])
def test_vector_head(do_iso):
v0 = Vector(int, 5)
if do_iso:
values1 = values2 = values3 = [1, 1, 1]
else:
values1 = [10, 20, 30]
values2 = [2, 4, 6]
values3 = [1, 2, 3]
v1 = Vector.from_coo([0, 1, 2], values1) # full
v2 = Vector.from_coo([1, 3, 5], values2) # bitmap
v3 = Vector.from_coo([100, 200, 300], values3) # sparse
assert v1.ss.export()["format"] == "full"
assert v2.ss.export()["format"] == "bitmap"
assert v3.ss.export()["format"] == "sparse"
assert v1.ss.is_iso is do_iso
assert v2.ss.is_iso is do_iso
assert v3.ss.is_iso is do_iso
for dtype in [None, np.float64]:
expected_dtype = np.int64 if dtype is None else dtype
for _ in range(2):
indices, vals = v0.ss.head(2, sort=True, dtype=dtype)
assert_array_equal(indices, [])
assert_array_equal(vals, [])
assert indices.dtype == np.uint64
assert vals.dtype == expected_dtype
indices, vals = v1.ss.head(2, sort=True, dtype=dtype)
assert_array_equal(indices, [0, 1])
assert_array_equal(vals, values1[:2])
assert indices.dtype == np.uint64
assert vals.dtype == expected_dtype
indices, vals = v2.ss.head(2, sort=True, dtype=dtype)
assert_array_equal(indices, [1, 3])
assert_array_equal(vals, values2[:2])
assert indices.dtype == np.uint64
assert vals.dtype == expected_dtype
indices, vals = v3.ss.head(2, sort=False, dtype=dtype)
assert indices.size == vals.size == 2
assert indices.dtype == np.uint64
assert vals.dtype == expected_dtype
indices, vals = v3.ss.head(2, sort=True, dtype=dtype)
assert_array_equal(indices, [100, 200])
assert_array_equal(vals, values3[:2])
assert indices.dtype == np.uint64
assert vals.dtype == expected_dtype
@pytest.mark.parametrize("do_iso", [False, True])
def test_matrix_head(do_iso):
A0 = Matrix(int, 5, 5)
if do_iso:
values1 = [1, 1, 1, 1]
values2 = values3 = values4 = [1, 1, 1]
else:
values1 = [1, 2, 3, 4]
values2 = [1, 2, 4]
values3 = values4 = [1, 2, 3]
A1 = Matrix.from_coo([0, 0, 1, 1], [0, 1, 0, 1], values1) # fullr
A2 = Matrix.from_coo([0, 0, 1], [0, 1, 1], values2) # Bitmap
A3 = Matrix.from_coo([5, 5, 10], [4, 5, 10], values3) # CSR
A4 = Matrix.from_coo([500, 500, 1000], [400, 500, 1000], values4) # HyperCSR
d = A1.ss.export(raw=True)
assert d["format"] == "fullr"
d["format"] = "fullc"
A5 = Matrix.ss.import_any(**d) # fullc
d = A2.ss.export(raw=True)
assert d["format"] == "bitmapr"
d["format"] = "bitmapc"
A6 = Matrix.ss.import_any(**d) # bitmapc
d = A3.ss.export(raw=True)
assert d["format"] == "csr"
d["format"] = "csc"
d["row_indices"] = d["col_indices"]
del d["col_indices"]
A7 = Matrix.ss.import_any(**d) # csc
d = A4.ss.export(raw=True)
assert d["format"] == "hypercsr"
d["format"] = "hypercsc"
d["row_indices"] = d["col_indices"]
del d["col_indices"]
d["cols"] = d["rows"]
del d["rows"]
A8 = Matrix.ss.import_any(**d) # hypercsc
assert A1.ss.is_iso is do_iso
assert A2.ss.is_iso is do_iso
assert A3.ss.is_iso is do_iso
assert A4.ss.is_iso is do_iso
assert A5.ss.is_iso is do_iso
assert A6.ss.is_iso is do_iso
assert A7.ss.is_iso is do_iso
assert A8.ss.is_iso is do_iso
for dtype in [None, np.float64]:
expected_dtype = np.int64 if dtype is None else dtype
for _ in range(2):
rows, cols, vals = A0.ss.head(2, sort=True, dtype=dtype)
assert_array_equal(rows, [])
assert_array_equal(cols, [])
assert_array_equal(vals, [])
assert rows.dtype == cols.dtype == np.uint64
assert vals.dtype == expected_dtype
rows, cols, vals = A1.ss.head(2, sort=True, dtype=dtype)
assert_array_equal(rows, [0, 0])
assert_array_equal(cols, [0, 1])
assert_array_equal(vals, values1[:2])
assert rows.dtype == cols.dtype == np.uint64
assert vals.dtype == expected_dtype
rows, cols, vals = A2.ss.head(2, sort=True, dtype=dtype)
assert_array_equal(rows, [0, 0])
assert_array_equal(cols, [0, 1])
assert_array_equal(vals, values2[:2])
assert rows.dtype == cols.dtype == np.uint64
assert vals.dtype == expected_dtype
rows, cols, vals = A3.ss.head(2, sort=True, dtype=dtype)
assert_array_equal(rows, [5, 5])
assert_array_equal(cols, [4, 5])
assert_array_equal(vals, values3[:2])
assert rows.dtype == cols.dtype == np.uint64
assert vals.dtype == expected_dtype
rows, cols, vals = A4.ss.head(2, sort=True, dtype=dtype)
assert_array_equal(rows, [500, 500])
assert_array_equal(cols, [400, 500])
assert_array_equal(vals, values4[:2])
assert rows.dtype == cols.dtype == np.uint64
assert vals.dtype == expected_dtype
rows, cols, vals = A5.ss.head(2, sort=True, dtype=dtype)
assert_array_equal(rows, [0, 1])
assert_array_equal(cols, [0, 0])
assert_array_equal(vals, values1[:2])
assert rows.dtype == cols.dtype == np.uint64
assert vals.dtype == expected_dtype
rows, cols, vals = A6.ss.head(2, sort=True, dtype=dtype)
assert_array_equal(rows, [0, 1])
assert_array_equal(cols, [0, 0])
assert_array_equal(vals, values2[:2])
assert rows.dtype == cols.dtype == np.uint64
assert vals.dtype == expected_dtype
rows, cols, vals = A7.ss.head(2, sort=True, dtype=dtype)
assert_array_equal(rows, [4, 5])
assert_array_equal(cols, [5, 5])
assert_array_equal(vals, values3[:2])
assert rows.dtype == cols.dtype == np.uint64
assert vals.dtype == expected_dtype
rows, cols, vals = A8.ss.head(2, sort=False, dtype=dtype)
assert rows.size == cols.size == vals.size == 2
assert rows.dtype == cols.dtype == np.uint64
assert vals.dtype == expected_dtype
rows, cols, vals = A8.ss.head(2, sort=True, dtype=dtype)
assert_array_equal(rows, [400, 500])
assert_array_equal(cols, [500, 500])
assert_array_equal(vals, values4[:2])
assert rows.dtype == cols.dtype == np.uint64
assert vals.dtype == expected_dtype
def test_about():
d = {}
about = gb.ss.about
for k in about:
d[k] = about[k]
assert "openmp" in about
assert d == about
assert len(d) == len(about)
with pytest.raises(KeyError):
about["badkey"]
assert "SuiteSparse" in about["library_name"]
with pytest.raises(TypeError):
del about["library_name"] # pylint: disable=unsupported-delete-operation
assert "library_name" in repr(about)
def test_openmp_enabled():
# SuiteSparse:GraphBLAS without OpenMP enabled is very undesirable
assert gb.ss.about["openmp"]
def test_global_config():
d = {}
config = gb.ss.config
for k in config:
d[k] = config[k]
assert d == config
assert len(d) == len(config)
for k, v in d.items():
config[k] = v
assert d == config
with pytest.raises(KeyError):
config["badkey"]
with pytest.raises(KeyError):
config["badkey"] = None
config["format"] = "by_col"
assert config["format"] == "by_col"
config["format"] = "by_row"
assert config["format"] == "by_row"
with pytest.raises(TypeError):
del config["format"]
with pytest.raises(KeyError):
config["format"] = "bad_format"
for k in config:
if k in config._defaults:
config[k] = None
else:
with pytest.raises(ValueError, match="Unable to set default value for"):
config[k] = None
# with pytest.raises(ValueError, match="Wrong number"):
# config["memory_pool"] = [1, 2] # No longer used
assert "format" in repr(config)
@pytest.mark.skipif("gb.core.ss._IS_SSGB7")
def test_context():
context = gb.ss.Context()
prev = dict(context)
context["chunk"] += 1
context["nthreads"] += 1
assert context["chunk"] == prev["chunk"] + 1
assert context["nthreads"] == prev["nthreads"] + 1
context2 = gb.ss.Context(stack=True)
assert context2 == context
context3 = gb.ss.Context(stack=False)
assert context3 == prev
context4 = gb.ss.Context(
chunk=context["chunk"] + 1, nthreads=context["nthreads"] + 1, stack=False
)
assert context4["chunk"] == context["chunk"] + 1
assert context4["nthreads"] == context["nthreads"] + 1
assert context == context.dup()
assert context4 == context.dup(chunk=context["chunk"] + 1, nthreads=context["nthreads"] + 1)
if "gpu_id" in gb.ss.Context._options:
assert context.dup(gpu_id=-1)["gpu_id"] == -1
context.engage()
assert gb.core.ss.context.threadlocal.context is context
with gb.ss.Context(nthreads=1) as ctx:
assert gb.core.ss.context.threadlocal.context is ctx
v = Vector(int, 5)
v(nthreads=2) << v + v
assert gb.core.ss.context.threadlocal.context is ctx
assert gb.core.ss.context.threadlocal.context is context
with pytest.raises(InvalidValue):
# Wait, why does this raise?!
ctx.disengage()
assert gb.core.ss.context.threadlocal.context is context
context.disengage()
assert gb.core.ss.context.threadlocal.context is gb.core.ss.context.global_context
assert context._prev_context is None
# hackery
gb.core.ss.context.threadlocal.context = context
context.disengage()
context.disengage()
context.disengage()
assert gb.core.ss.context.threadlocal.context is gb.core.ss.context.global_context
# Actually engaged, but not set in threadlocal
context._engage()
assert gb.core.ss.context.threadlocal.context is gb.core.ss.context.global_context
context.disengage()
context.engage()
context._engage()
assert gb.core.ss.context.threadlocal.context is context
context.disengage()
context._context = context # This is allowed to work with config
with pytest.raises(AttributeError, match="_context"):
context._context = ctx # This is not