diff --git a/suitesparse_graphblas/api/io/serialize.py b/suitesparse_graphblas/api/io/serialize.py index c6aefde..1d9e114 100644 --- a/suitesparse_graphblas/api/io/serialize.py +++ b/suitesparse_graphblas/api/io/serialize.py @@ -1,7 +1,11 @@ +import operator +import warnings + import numpy as np from suitesparse_graphblas import check_status, ffi, lib from suitesparse_graphblas.api.global_options import global_get_int32 +from suitesparse_graphblas.exceptions import InvalidValue from suitesparse_graphblas.utils import claim_buffer @@ -11,11 +15,85 @@ def _free_desc(desc): lib.GrB_Descriptor_free(desc_ptr) +def _call_with_nthreads(nthreads, func, *args): + """Return ``func(*args)``, called under a new GxB_Context with `nthreads` threads. + + GraphBLAS 10 takes the number of threads from the Context engaged on the calling thread, + not from a descriptor. If `nthreads` is None, 0 or negative, `func` is just called. + `nthreads` must be an integer, and not a bool. + + Apart from the number of threads, the new Context is a copy of GxB_CONTEXT_WORLD: + GxB_Context_new copies its chunk and GPUs, and the arenas are copied here, since they + would otherwise be arena 0. WORLD is the Context the call would use without `nthreads`, + because a Context engaged on this thread makes this raise. + + It all happens in this one frame, in try and finally blocks, rather than in a generator + context manager. A KeyboardInterrupt can come as the context manager's ``__exit__`` + begins, before the generator resumes, and that leaves disengaging and freeing the Context + to garbage collection: later, and on PyPy perhaps in another thread. + """ + if nthreads is not None: + # NumPy 2.2 still lets `operator.index` take its bool, with a DeprecationWarning + if isinstance(nthreads, (bool, np.bool_)): + raise TypeError("nthreads must be an integer, not a bool") + nthreads = operator.index(nthreads) # TypeError if it isn't an integer + if nthreads is None or nthreads <= 0: + return func(*args) + ctx = ffi.new("GxB_Context*") + # A KeyboardInterrupt between here and `try` leaks the Context, which isn't engaged yet + check_status(ctx, lib.GxB_Context_new(ctx)) + try: + # A Context engaged on this thread would be lost: GraphBLAS can't say which one it + # is, so it can't be engaged again afterwards. But it can say whether there is one: + # disengaging a Context that isn't engaged fails with GrB_INVALID_VALUE if another + # one is, and changes nothing. + info = lib.GxB_Context_disengage(ctx[0]) + if info == lib.GrB_INVALID_VALUE: + raise InvalidValue( + "nthreads can't be given while a GxB_Context is engaged on this thread; " + "set GxB_CONTEXT_NTHREADS of that Context instead" + ) + check_status(ctx, info) + # OverflowError if `nthreads` doesn't fit in int32_t + check_status(ctx, lib.GxB_Context_set_INT(ctx[0], nthreads, lib.GxB_CONTEXT_NTHREADS)) + header_arena = global_get_int32(lib.GxB_ARENA_HEADER) + check_status(ctx, lib.GxB_Context_set_INT(ctx[0], header_arena, lib.GxB_ARENA_HEADER)) + data_arena = global_get_int32(lib.GxB_ARENA_DATA) + check_status(ctx, lib.GxB_Context_set_INT(ctx[0], data_arena, lib.GxB_ARENA_DATA)) + try: + check_status(ctx, lib.GxB_Context_engage(ctx[0])) + return func(*args) + finally: + lib.GxB_Context_disengage(ctx[0]) + finally: + # Disengaging a Context that isn't engaged changes nothing, so disengage it again, in + # case a KeyboardInterrupt stopped the `finally` above before it could (PyPy may stop + # it at its first bytecode). Freeing it while engaged would leave the thread with a + # dangling Context. + lib.GxB_Context_disengage(ctx[0]) + lib.GxB_Context_free(ctx) + + def get_serialize_desc(compression=lib.GxB_COMPRESSION_DEFAULT, level=None, nthreads=None): """Create a descriptor for serializing or deserializing. This returns None (for NULL descriptor) or a GrB_Descriptor. + + .. deprecated:: 10.5.3.0 + The `nthreads` argument. The number of threads isn't a descriptor setting in + GraphBLAS 10, which raises InvalidValue if `nthreads` is given. Use the `nthreads` + argument of serialize_matrix, serialize_vector, deserialize_matrix or + deserialize_vector instead, which sets it in a GxB_Context. """ + if nthreads is not None: + warnings.warn( + "The `nthreads` argument of get_serialize_desc is deprecated: GraphBLAS 10 " + "rejects the number of threads in a descriptor. Use the `nthreads` argument " + "of serialize_matrix, serialize_vector, deserialize_matrix or " + "deserialize_vector instead.", + DeprecationWarning, + stacklevel=2, + ) if nthreads is None and (compression is None or compression == lib.GxB_COMPRESSION_DEFAULT): return None desc_ptr = ffi.new("GrB_Descriptor*") @@ -50,18 +128,26 @@ def serialize_matrix(A, compression=lib.GxB_COMPRESSION_DEFAULT, level=None, *, level : int, optional For GxB_COMPRESSION_LZ4HC, should be between 1 and 9, where 9 is most compressed. For GxB_COMPRESSION_ZSTD, should be between 1 and 19, where 19 is most compressed. - nthreads : int, optional - The maximum number of OpenMP threads to use. + The maximum number of OpenMP threads to use. None (the default), 0 or any negative + integer, however large, means the number of threads of the GxB_Context engaged on + this thread, if any, or else of GxB_CONTEXT_WORLD. A positive integer makes the + call run under a new Context with that many threads. It raises OverflowError if + that is more than int32_t holds, and InvalidValue if a Context is engaged on this + thread (which stays engaged: set its number of threads instead). Raises TypeError + if `nthreads` isn't an integer, or is a bool. """ - desc = get_serialize_desc(compression, level, nthreads) + desc = get_serialize_desc(compression, level) data_ptr = ffi.new("void**") size_ptr = ffi.new("GrB_Index*") # Name the arena for the result, which is otherwise that of the engaged Context (if # any), so that `claim_buffer` knows which allocator must free it arena = global_get_int32(lib.GxB_ARENA_DATA) desc = ffi.NULL if desc is None else desc - check_status(A, lib.GxB_Matrix_serialize_arena(data_ptr, size_ptr, A[0], arena, desc)) + info = _call_with_nthreads( + nthreads, lib.GxB_Matrix_serialize_arena, data_ptr, size_ptr, A[0], arena, desc + ) + check_status(A, info) return claim_buffer(ffi, data_ptr[0], size_ptr[0], np.dtype(np.uint8), arena) @@ -77,38 +163,59 @@ def serialize_vector(v, compression=lib.GxB_COMPRESSION_DEFAULT, level=None, *, For GxB_COMPRESSION_LZ4HC, should be between 1 and 9, where 9 is most compressed. For GxB_COMPRESSION_ZSTD, should be between 1 and 19, where 19 is most compressed. nthreads : int, optional - The maximum number of OpenMP threads to use. + The maximum number of OpenMP threads to use. None (the default), 0 or any negative + integer, however large, means the number of threads of the GxB_Context engaged on + this thread, if any, or else of GxB_CONTEXT_WORLD. A positive integer makes the + call run under a new Context with that many threads. It raises OverflowError if + that is more than int32_t holds, and InvalidValue if a Context is engaged on this + thread (which stays engaged: set its number of threads instead). Raises TypeError + if `nthreads` isn't an integer, or is a bool. """ - desc = get_serialize_desc(compression, level, nthreads) + desc = get_serialize_desc(compression, level) data_ptr = ffi.new("void**") size_ptr = ffi.new("GrB_Index*") arena = global_get_int32(lib.GxB_ARENA_DATA) # see `serialize_matrix` desc = ffi.NULL if desc is None else desc - check_status(v, lib.GxB_Vector_serialize_arena(data_ptr, size_ptr, v[0], arena, desc)) + info = _call_with_nthreads( + nthreads, lib.GxB_Vector_serialize_arena, data_ptr, size_ptr, v[0], arena, desc + ) + check_status(v, info) return claim_buffer(ffi, data_ptr[0], size_ptr[0], np.dtype(np.uint8), arena) def deserialize_matrix(data, *, free=True, nthreads=None): """Deserialize a Matrix from bytes. - The `free` argument is called when the object is garbage - collected, the default is `matrix.matrix_free()`. If `free` is None then - there is no automatic garbage collection and it is up to the user - to free the matrix. + Parameters + ---------- + data : bytes-like + A serialized Matrix, such as the result of `serialize_matrix`: bytes, a NumPy array, or + anything else with the buffer protocol. + free : callable or bool, optional + Called with the result when it is garbage collected. True (the default) means + `matrix.matrix_free`. If `free` is None or False, there is no automatic garbage + collection, and it is up to the caller to free the Matrix. + nthreads : int, optional + The maximum number of OpenMP threads to use. None (the default), 0 or any negative + integer, however large, means the number of threads of the GxB_Context engaged on + this thread, if any, or else of GxB_CONTEXT_WORLD. A positive integer makes the + call run under a new Context with that many threads. It raises OverflowError if + that is more than int32_t holds, and InvalidValue if a Context is engaged on this + thread (which stays engaged: set its number of threads instead). Raises TypeError + if `nthreads` isn't an integer, or is a bool. """ data = np.frombuffer(data, np.uint8) - desc = get_serialize_desc(None, nthreads) A = ffi.new("GrB_Matrix*") - check_status( + info = _call_with_nthreads( + nthreads, + lib.GxB_Matrix_deserialize, A, - lib.GxB_Matrix_deserialize( - A, - ffi.NULL, # dtype; we don't check for now - ffi.from_buffer("void*", data), - data.nbytes, - ffi.NULL if desc is None else desc, - ), + ffi.NULL, # dtype; we don't check for now + ffi.from_buffer("void*", data), + data.nbytes, + ffi.NULL, ) + check_status(A, info) if free: if callable(free): return ffi.gc(A, free) @@ -119,24 +226,36 @@ def deserialize_matrix(data, *, free=True, nthreads=None): def deserialize_vector(data, *, free=True, nthreads=None): """Deserialize a Vector from bytes. - The `free` argument is called when the object is garbage - collected, the default is `vector.vector_free()`. If `free` is None then - there is no automatic garbage collection and it is up to the user - to free the vector. + Parameters + ---------- + data : bytes-like + A serialized Vector, such as the result of `serialize_vector`: bytes, a NumPy array, or + anything else with the buffer protocol. + free : callable or bool, optional + Called with the result when it is garbage collected. True (the default) means + `vector.vector_free`. If `free` is None or False, there is no automatic garbage + collection, and it is up to the caller to free the Vector. + nthreads : int, optional + The maximum number of OpenMP threads to use. None (the default), 0 or any negative + integer, however large, means the number of threads of the GxB_Context engaged on + this thread, if any, or else of GxB_CONTEXT_WORLD. A positive integer makes the + call run under a new Context with that many threads. It raises OverflowError if + that is more than int32_t holds, and InvalidValue if a Context is engaged on this + thread (which stays engaged: set its number of threads instead). Raises TypeError + if `nthreads` isn't an integer, or is a bool. """ data = np.frombuffer(data, np.uint8) - desc = get_serialize_desc(None, nthreads) v = ffi.new("GrB_Vector*") - check_status( + info = _call_with_nthreads( + nthreads, + lib.GxB_Vector_deserialize, v, - lib.GxB_Vector_deserialize( - v, - ffi.NULL, # dtype; we don't check for now - ffi.from_buffer("void*", data), - data.nbytes, - ffi.NULL if desc is None else desc, - ), + ffi.NULL, # dtype; we don't check for now + ffi.from_buffer("void*", data), + data.nbytes, + ffi.NULL, ) + check_status(v, info) if free: if callable(free): return ffi.gc(v, free) diff --git a/suitesparse_graphblas/conftest.py b/suitesparse_graphblas/conftest.py index 17e8144..e5d6f3d 100644 --- a/suitesparse_graphblas/conftest.py +++ b/suitesparse_graphblas/conftest.py @@ -1,6 +1,8 @@ +import threading + import pytest -from suitesparse_graphblas import initialize +from suitesparse_graphblas import ffi, initialize, lib try: from numpy._core._multiarray_umath import _set_numpy_warn_if_no_mem_policy @@ -37,3 +39,43 @@ def warn_if_no_memory_handler(): old = _set_numpy_warn_if_no_mem_policy(True) yield _set_numpy_warn_if_no_mem_policy(old) + + +class _FreedInThisThread(threading.local): + # Every thread starts with an empty list, so a free in any thread is recorded: an + # AttributeError in the cffi callback would only be printed, and the free not seen. + def __init__(self): + self.addresses = [] + + +# GraphBLAS may call the recording arena's functions until the process ends +_recording_arena_callbacks = [] + + +@pytest.fixture(scope="session") +def recording_arena(): + """An arena that records the addresses it frees: ``(k, freed)``. + + `k` is its index. Tests may run in several threads at once, so each thread records only + its own frees, in ``freed.addresses``. An arena can't be changed, so it's made once. It + needs cffi callbacks, which Pyodide doesn't have. + """ + blocks = {} + freed = _FreedInThisThread() + + @ffi.callback("void *(size_t)") + def malloc(size): + block = ffi.new("char[]", size or 1) + blocks[int(ffi.cast("uintptr_t", block))] = block + return block + + @ffi.callback("void (void *)") + def free(ptr): + address = int(ffi.cast("uintptr_t", ptr)) + blocks.pop(address, None) + freed.addresses.append(address) + + k = lib.GxB_NARENAS - 2 # not 5: test_can_unclaim_buffer needs it uninitialized + assert lib.GxB_arena_init(k, malloc, ffi.NULL, ffi.NULL, free) == lib.GrB_SUCCESS + _recording_arena_callbacks.extend([malloc, free]) + return k, freed diff --git a/suitesparse_graphblas/tests/test_io.py b/suitesparse_graphblas/tests/test_io.py index 00507ce..c5b3de9 100644 --- a/suitesparse_graphblas/tests/test_io.py +++ b/suitesparse_graphblas/tests/test_io.py @@ -1,7 +1,10 @@ import bz2 +import functools import gzip import platform import struct +import sys +from contextlib import contextmanager from pathlib import Path import numpy as np @@ -30,8 +33,11 @@ if platform.system() == "Windows": pytest.skip("skipping windows-only tests", allow_module_level=True) +from suitesparse_graphblas.api import context # isort:skip +from suitesparse_graphblas.api.global_options import global_get_int32 # isort:skip from suitesparse_graphblas.exceptions import InvalidObject, InvalidValue # isort:skip from suitesparse_graphblas.io import binary, serialize # isort:skip +from suitesparse_graphblas.utils import claim_buffer # isort:skip NULL = ffi.NULL @@ -465,6 +471,18 @@ def test_get_serialize_desc(): assert _compression(get(lib.GxB_COMPRESSION_NONE, 9)) == lib.GxB_COMPRESSION_NONE +@pytest.mark.parametrize( + "compression", [lib.GxB_COMPRESSION_DEFAULT, None], ids=["default", "None"] +) +def test_get_serialize_desc_nthreads_deprecated(compression): + # The number of threads is a Context setting in GraphBLAS 10, not a descriptor setting. + # The warning names the replacement and the caller, and GraphBLAS still rejects it. + with pytest.warns(DeprecationWarning, match="serialize_matrix") as record: + with pytest.raises(InvalidValue): + serialize.get_serialize_desc(compression, nthreads=1) + assert [w.filename for w in record] == [__file__] + + _serialize_options = { "none": (lib.GxB_COMPRESSION_NONE, None), "default": (lib.GxB_COMPRESSION_DEFAULT, None), @@ -585,20 +603,286 @@ def test_deserialize_invalid(): deserialize(bytes(64)) -@pytest.mark.xfail( - strict=True, - raises=InvalidValue, - reason="serialize sets nthreads on a descriptor, which GraphBLAS 10 rejects with " - "GrB_INVALID_VALUE: the number of threads is a GxB_Context setting now", -) +def _context_engaged(probe): + """Whether a GxB_Context is engaged on this thread. + + GraphBLAS can't say which one, but disengaging a Context that isn't engaged, `probe`, + fails with GrB_INVALID_VALUE if another one is, and changes nothing. Make `probe` before + the Contexts it looks for: one freed while engaged (a bug) stays the engaged Context, and + a probe made later could be given its address. + """ + info = lib.GxB_Context_disengage(probe[0]) + assert info in {lib.GrB_SUCCESS, lib.GrB_INVALID_VALUE} + return info == lib.GrB_INVALID_VALUE + + +@contextmanager +def _engaged(nthreads): + """Engage a new Context that uses `nthreads` threads on this thread.""" + ctx = context.context_new() + context.context_set_nthreads(ctx, nthreads) + context.context_engage(ctx) + try: + yield + finally: + context.context_disengage(ctx) + + +def _assert_vectors_equal(v, w): + assert vector.vector_type(w) == vector.vector_type(v) + assert vector.vector_size(w) == vector.vector_size(v) + for x, y in zip( + vector.vector_extract_tuples_int64(v), vector.vector_extract_tuples_int64(w), strict=True + ): + np.testing.assert_array_equal(x, y) + + +def _big(kind): + """A Matrix or Vector whose blob shows how many threads serialized it. + + Returns the object, the functions that serialize and deserialize it, and one that asserts + that two of them are equal. GraphBLAS compresses an array as a single block if it uses + one thread, and as blocks of at least 64 KiB if it uses more (see GB_serialize_array.c). + It uses one thread for each GxB_CHUNK bytes (64 KiB by default), up to the number of + threads of the Context, and the 512 KiB of values here are enough for several. + """ + if kind == "matrix": + A = _numbered_matrix(256) + return A, serialize.serialize_matrix, serialize.deserialize_matrix, _assert_matrices_equal + n = 256 * 256 + v = vector.vector_new(lib.GrB_INT64, n) + indices = np.arange(n, dtype=np.uint64) + vector.vector_build_int64(v, indices, np.arange(n, dtype=np.int64), n, lib.GrB_PLUS_INT64) + return v, serialize.serialize_vector, serialize.deserialize_vector, _assert_vectors_equal + + @pytest.mark.parametrize("kind", ["matrix", "vector"]) def test_serialize_nthreads(kind): - if kind == "matrix": - A = _make_matrix(lib.GrB_INT64, "sparse") - B = serialize.deserialize_matrix(serialize.serialize_matrix(A, nthreads=1), nthreads=1) - _assert_matrices_equal(A, B) - else: - v = vector.vector_new(lib.GrB_INT64, 3) - vector.set_int64(v, 7, 2) - w = serialize.deserialize_vector(serialize.serialize_vector(v, nthreads=1), nthreads=1) - assert vector.vector_nvals(w) == 1 + # GraphBLAS 10 rejects the number of threads in a descriptor: it's a Context setting. + # Serializing with `nthreads` gives what serializing under a Context with that many + # threads gives, and one thread gives a different result from two. + probe = context.context_new() + A, ser, deser, assert_equal = _big(kind) + blobs = {} + for nthreads in (1, 2): + with _engaged(nthreads): + expected = ser(A).tobytes() + blobs[nthreads] = ser(A, nthreads=nthreads).tobytes() + assert blobs[nthreads] == expected + assert blobs[1] != blobs[2] + assert ser(A, nthreads=np.int64(2)).tobytes() == blobs[2] # any kind of integer will do + # How many threads deserialize uses doesn't show in its result + for blob in blobs.values(): + for nthreads in (1, 2): + assert_equal(A, deser(blob, nthreads=nthreads)) + assert not _context_engaged(probe) + + +@pytest.mark.parametrize("nthreads", [None, 0, -1, -(2**40)]) # -2**40: beyond int32_t +@pytest.mark.parametrize("kind", ["matrix", "vector"]) +def test_serialize_nthreads_default(kind, nthreads): + # These use the Context engaged on this thread, if any, or else GxB_CONTEXT_WORLD + probe = context.context_new() + A, ser, deser, assert_equal = _big(kind) + blob = ser(A).tobytes() + assert ser(A, nthreads=nthreads).tobytes() == blob + assert_equal(A, deser(blob, nthreads=nthreads)) + expected = {engaged: ser(A, nthreads=engaged).tobytes() for engaged in (1, 2)} + for engaged in (1, 2): + with _engaged(engaged): + assert ser(A, nthreads=nthreads).tobytes() == expected[engaged] + assert_equal(A, deser(blob, nthreads=nthreads)) + assert _context_engaged(probe) + assert not _context_engaged(probe) + + +@pytest.mark.skipif(sys.platform == "emscripten", reason="needs cffi callbacks") +@pytest.mark.thread_unsafe(reason="changes the global header arena") +@pytest.mark.parametrize("nthreads", [None, 0, -1]) +@pytest.mark.parametrize("kind", ["matrix", "vector"]) +def test_serialize_nthreads_default_makes_no_context(kind, nthreads, recording_arena): + # The default does no Context work at all. A new Context's header comes from the global + # header arena, so making and freeing one frees a block there that the GraphBLAS call + # alone doesn't free. The recording arena records the blocks freed in this thread. + A, ser, deser, assert_equal = _big(kind) + blob = ser(A) + data = np.frombuffer(blob, np.uint8) + T = "Matrix" if kind == "matrix" else "Vector" + free = matrix.matrix_free if kind == "matrix" else vector.vector_free + + def serialize_raw(): + data_ptr, size_ptr = ffi.new("void**"), ffi.new("GrB_Index*") + info = getattr(lib, f"GxB_{T}_serialize_arena")(data_ptr, size_ptr, A[0], 0, NULL) + check_status(A, info) + return claim_buffer(ffi, data_ptr[0], size_ptr[0], np.dtype(np.uint8), 0) + + def deserialize_raw(): + B = ffi.new(f"GrB_{T}*") + blob_ptr = ffi.from_buffer("void*", data) + check_status(B, getattr(lib, f"GxB_{T}_deserialize")(B, NULL, blob_ptr, data.nbytes, NULL)) + return B + + calls = { + "serialize_raw": serialize_raw, + "serialize": lambda: ser(A, nthreads=nthreads), + "serialize_2": lambda: ser(A, nthreads=2), # this one does make a Context + "deserialize_raw": deserialize_raw, + "deserialize": lambda: deser(blob, free=None, nthreads=nthreads), + } + k, freed = recording_arena + old = global_get_int32(lib.GxB_ARENA_HEADER) + results, frees = {}, {} + assert lib.GrB_Global_set_INT32(lib.GrB_GLOBAL, k, lib.GxB_ARENA_HEADER) == lib.GrB_SUCCESS + try: + for name, call in calls.items(): + freed.addresses = [] + results[name] = call() + frees[name] = len(freed.addresses) + finally: + assert lib.GrB_Global_set_INT32(lib.GrB_GLOBAL, old, lib.GxB_ARENA_HEADER) == 0 + assert frees["serialize"] == frees["serialize_raw"] + assert frees["serialize_2"] == frees["serialize_raw"] + 1 + assert frees["deserialize"] == frees["deserialize_raw"] + assert results["serialize"].tobytes() == results["serialize_raw"].tobytes() + for name in ("deserialize_raw", "deserialize"): + assert_equal(A, results[name]) + free(results[name]) + + +@pytest.mark.parametrize("kind", ["matrix", "vector"]) +def test_serialize_nthreads_with_engaged_context(kind): + # GraphBLAS can't say which Context is engaged on this thread, so a call that engaged + # its own for `nthreads` couldn't engage that one again afterwards. It refuses instead, + # and the caller's Context stays engaged. + probe = context.context_new() + A, ser, deser, assert_equal = _big(kind) + blob = ser(A) + ctx = context.context_new() + context.context_engage(ctx) + try: + for call in (lambda: ser(A, nthreads=2), lambda: deser(blob, nthreads=2)): + with pytest.raises(InvalidValue, match="engaged on this thread"): + call() + assert _context_engaged(probe) + # ...and it's `ctx`: disengaging it fails if another Context is engaged + context.context_disengage(ctx) + assert not _context_engaged(probe) + finally: + lib.GxB_Context_disengage(NULL) # whichever is engaged, if the test failed + + +@pytest.mark.parametrize("kind", ["matrix", "vector"]) +def test_serialize_nthreads_invalid(kind): + probe = context.context_new() + A, ser, deser, assert_equal = _big(kind) + blob = ser(A) + for call in (lambda n: ser(A, nthreads=n), lambda n: deser(blob, nthreads=n)): + for nthreads in (2.0, 0.0, "2"): # not even 0.0, which would mean the default + with pytest.raises(TypeError): + call(nthreads) + for nthreads in (True, False, np.True_, np.False_): # nor a bool, which would mean 1 or 0 + with pytest.raises(TypeError, match="bool"): + call(nthreads) + with pytest.raises(OverflowError): + call(2**31) # more than int32_t holds + assert not _context_engaged(probe) + # Nor do failures. This one is while the Context is engaged. + other = vector.vector_new(lib.GrB_INT64) if kind == "matrix" else _numbered_matrix(2) + with pytest.raises(TypeError): + ser(other, nthreads=2) # the wrong kind of object + assert not _context_engaged(probe) + with pytest.raises(TypeError): + ser(other) # nor does the default path swallow the error + with pytest.raises(InvalidObject): + deser(bytes(64), nthreads=2) + assert not _context_engaged(probe) + + +def test_call_with_nthreads_interrupted(): + # The call runs under the Context, which is disengaged whatever it raises + probe = context.context_new() + + def interrupted(): + assert _context_engaged(probe) + raise KeyboardInterrupt + + with pytest.raises(KeyboardInterrupt): + serialize._call_with_nthreads(2, interrupted) + assert not _context_engaged(probe) + + +class _InterruptedLib: + """`lib`, except that the `n`th lookup of GxB_Context_disengage raises KeyboardInterrupt.""" + + def __init__(self, n): + self.lookups = 0 + self.n = n + + def __getattr__(self, name): + if name == "GxB_Context_disengage": + self.lookups += 1 + if self.lookups == self.n: + raise KeyboardInterrupt + return getattr(lib, name) + + +@pytest.mark.thread_unsafe(reason="replaces `lib` in the serialize module") +@pytest.mark.parametrize("n", [2, 3], ids=["inner", "outer"]) +def test_call_with_nthreads_interrupted_in_finally(monkeypatch, n): + # A KeyboardInterrupt can come at the first bytecode of a `finally` block (PyPy can stop + # it there), before the block disengages the Context. Each of the two `finally` blocks + # disengages it, so the other one still does. The lookups of GxB_Context_disengage are + # for the probe, then the inner `finally` block, then the outer one. + probe = context.context_new() + interrupted = _InterruptedLib(n) + monkeypatch.setattr(serialize, "lib", interrupted) + with pytest.raises(KeyboardInterrupt): + serialize._call_with_nthreads(2, lambda: None) + monkeypatch.undo() + assert not _context_engaged(probe) + assert interrupted.lookups == 3 + + +@functools.cache +def _arena_like_0(): + """Arena 1, which allocates as arena 0 does. It's made once: arenas can't be changed. + + Other tests that run in this process keep arena 5 uninitialized (test_can_unclaim_buffer) + and use arena 6 (the `recording_arena` fixture in conftest.py). + """ + k = 1 + malloc, free = ffi.new("void**"), ffi.new("void**") + info = lib.GrB_Global_get_VOID(lib.GrB_GLOBAL, malloc, lib.GxB_ARENA_MALLOC) + assert info == lib.GrB_SUCCESS + info = lib.GrB_Global_get_VOID(lib.GrB_GLOBAL, free, lib.GxB_ARENA_FREE) + assert info == lib.GrB_SUCCESS + malloc = ffi.cast("void *(*)(size_t)", malloc[0]) + free = ffi.cast("void (*)(void *)", free[0]) + assert lib.GxB_arena_init(k, malloc, NULL, NULL, free) == lib.GrB_SUCCESS + return k + + +@pytest.mark.thread_unsafe(reason="changes the global header and data arenas") +@pytest.mark.parametrize("kind", ["matrix", "vector"]) +def test_deserialize_nthreads_arenas(kind): + # The result is in the global arenas with or without `nthreads`. A new Context has arena + # 0 for both, so the one that `nthreads` engages is given the global arenas. + A, ser, deser, assert_equal = _big(kind) + blob = ser(A) + get = lib.GrB_Matrix_get_INT32 if kind == "matrix" else lib.GrB_Vector_get_INT32 + fields = (lib.GxB_ARENA_HEADER, lib.GxB_ARENA_DATA) + k = _arena_like_0() + old = [global_get_int32(field) for field in fields] + try: + for field in fields: + assert lib.GrB_Global_set_INT32(lib.GrB_GLOBAL, k, field) == lib.GrB_SUCCESS + for nthreads in (None, 2): + B = deser(blob, nthreads=nthreads) + arena = ffi.new("int32_t*") + for field in fields: + check_status(B, get(B[0], arena, field)) + assert arena[0] == k + assert_equal(A, B) + finally: + for field, value in zip(fields, old, strict=True): + assert lib.GrB_Global_set_INT32(lib.GrB_GLOBAL, value, field) == lib.GrB_SUCCESS diff --git a/suitesparse_graphblas/tests/test_memory.py b/suitesparse_graphblas/tests/test_memory.py index 1021267..651bd14 100644 --- a/suitesparse_graphblas/tests/test_memory.py +++ b/suitesparse_graphblas/tests/test_memory.py @@ -2,7 +2,6 @@ import subprocess import sys import textwrap -import threading import numpy as np import pytest @@ -234,43 +233,6 @@ def test_claim_buffer(): claim_buffer_2d(ffi, ffi.NULL, 6, 2, 3, int64, True) -# An arena that records the addresses it frees, made once (arenas can't be changed). Tests may -# run in several threads at once, so each thread records only its own frees. -_recording_arena = {} -_recording_arena_lock = threading.Lock() - - -class _FreedInThisThread(threading.local): - # Every thread starts with an empty list, so a free in any thread is recorded: an - # AttributeError in the cffi callback would only be printed, and the free not seen. - def __init__(self): - self.addresses = [] - - -def recording_arena(): - with _recording_arena_lock: - if not _recording_arena: - blocks = {} - freed = _FreedInThisThread() - - @ffi.callback("void *(size_t)") - def malloc(size): - block = ffi.new("char[]", size or 1) - blocks[int(ffi.cast("uintptr_t", block))] = block - return block - - @ffi.callback("void (void *)") - def free(ptr): - address = int(ffi.cast("uintptr_t", ptr)) - blocks.pop(address, None) - freed.addresses.append(address) - - k = lib.GxB_NARENAS - 2 # not 5: test_can_unclaim_buffer needs it uninitialized - assert lib.GxB_arena_init(k, malloc, ffi.NULL, ffi.NULL, free) == lib.GrB_SUCCESS - _recording_arena.update(k=k, freed=freed, callbacks=(malloc, free)) - return _recording_arena["k"], _recording_arena["freed"] - - @pytest.mark.skipif(sys.platform == "emscripten", reason="needs cffi callbacks") # PyPy runs C extension code (utils.pyx here) holding the GIL, and a cffi callback takes the # GIL on entry without checking whether its own thread holds it, so the first call into this @@ -279,9 +241,9 @@ def free(ptr): sys.implementation.name == "pypy", reason="on PyPy, a cffi callback called from a C extension module deadlocks", ) -def test_claim_buffer_frees_on_error(): +def test_claim_buffer_frees_on_error(recording_arena): # GraphBLAS has handed the buffer over, so if claiming it fails, nothing else frees it - k, freed = recording_arena() + k, freed = recording_arena int64 = np.dtype(np.int64) huge = sys.maxsize // 4 # too many int64s for NumPy for claim, match in [