|
| 1 | +import numpy as np |
| 2 | + |
| 3 | +from suitesparse_graphblas import check_status, ffi, lib |
| 4 | +from suitesparse_graphblas.utils import claim_buffer |
| 5 | + |
| 6 | + |
| 7 | +def free_desc(desc): |
| 8 | + """Free a descriptor.""" |
| 9 | + check_status(desc, lib.GrB_Descriptor_free(desc)) |
| 10 | + |
| 11 | + |
| 12 | +def get_serialize_desc(compression=lib.GxB_COMPRESSION_DEFAULT, level=None, nthreads=None): |
| 13 | + """Create a descriptor for serializing or deserializing. |
| 14 | +
|
| 15 | + This returns None (for NULL descriptor) or a pointer to a GrB_Descriptor. |
| 16 | + """ |
| 17 | + if nthreads is None and (compression is None or compression == lib.GxB_COMPRESSION_DEFAULT): |
| 18 | + return None |
| 19 | + desc = ffi.new("GrB_Descriptor*") |
| 20 | + check_status(desc, lib.GrB_Descriptor_new(desc)) |
| 21 | + desc = ffi.gc(desc, free_desc) |
| 22 | + if nthreads is not None: |
| 23 | + check_status(desc, lib.GxB_Desc_set(desc[0], lib.GxB_NTHREADS, ffi.cast("int", nthreads))) |
| 24 | + if compression is not None: |
| 25 | + if level is not None and compression == lib.GxB_COMPRESSION_LZ4HC: |
| 26 | + compression += level |
| 27 | + check_status( |
| 28 | + desc, lib.GxB_Desc_set(desc[0], lib.GxB_COMPRESSION, ffi.cast("int", compression)) |
| 29 | + ) |
| 30 | + return desc |
| 31 | + |
| 32 | + |
| 33 | +def serialize_matrix(A, compression=lib.GxB_COMPRESSION_DEFAULT, level=None, *, nthreads=None): |
| 34 | + """Serialize a Matrix into an array of bytes. |
| 35 | +
|
| 36 | + Parameters |
| 37 | + ---------- |
| 38 | + compression : int, optional |
| 39 | + One of None, GxB_COMPRESSION_NONE, GxB_COMPRESSION_DEFAULT, |
| 40 | + GxB_COMPRESSION_LZ4, or GxB_COMPRESSION_LZ4HC |
| 41 | + level : int, optional |
| 42 | + Used by GxB_COMPRESSION_LZ4HC. Should be between 1 and 9, where 9 is most compressed. |
| 43 | + nthreads : int, optional |
| 44 | + The maximum number of OpenMP threads to use. |
| 45 | + """ |
| 46 | + desc = get_serialize_desc(compression, level, nthreads) |
| 47 | + data_ptr = ffi.new("void**") |
| 48 | + size_ptr = ffi.new("GrB_Index*") |
| 49 | + check_status( |
| 50 | + A, lib.GxB_Matrix_serialize(data_ptr, size_ptr, A[0], ffi.NULL if desc is None else desc[0]) |
| 51 | + ) |
| 52 | + return claim_buffer(ffi, data_ptr[0], size_ptr[0], np.dtype(np.uint8)) |
| 53 | + |
| 54 | + |
| 55 | +def serialize_vector(v, compression=lib.GxB_COMPRESSION_DEFAULT, level=None, *, nthreads=None): |
| 56 | + """Serialize a Vector into an array of bytes. |
| 57 | +
|
| 58 | + Parameters |
| 59 | + ---------- |
| 60 | + compression : int, optional |
| 61 | + One of None, GxB_COMPRESSION_NONE, GxB_COMPRESSION_DEFAULT, |
| 62 | + GxB_COMPRESSION_LZ4, or GxB_COMPRESSION_LZ4HC |
| 63 | + level : int, optional |
| 64 | + Used by GxB_COMPRESSION_LZ4HC. Should be between 1 and 9, where 9 is most compressed. |
| 65 | + nthreads : int, optional |
| 66 | + The maximum number of OpenMP threads to use. |
| 67 | + """ |
| 68 | + desc = get_serialize_desc(compression, level, nthreads) |
| 69 | + data_ptr = ffi.new("void**") |
| 70 | + size_ptr = ffi.new("GrB_Index*") |
| 71 | + check_status( |
| 72 | + v, lib.GxB_Vector_serialize(data_ptr, size_ptr, v[0], ffi.NULL if desc is None else desc[0]) |
| 73 | + ) |
| 74 | + return claim_buffer(ffi, data_ptr[0], size_ptr[0], np.dtype(np.uint8)) |
| 75 | + |
| 76 | + |
| 77 | +def deserialize_matrix(data, *, free=True, nthreads=None): |
| 78 | + """Deserialize a Matrix from bytes. |
| 79 | +
|
| 80 | + The `free` argument is called when the object is garbage |
| 81 | + collected, the default is `matrix.free()`. If `free` is None then |
| 82 | + there is no automatic garbage collection and it is up to the user |
| 83 | + to free the matrix. |
| 84 | + """ |
| 85 | + data = np.frombuffer(data, np.uint8) |
| 86 | + desc = get_serialize_desc(None, nthreads) |
| 87 | + A = ffi.new("GrB_Matrix*") |
| 88 | + check_status( |
| 89 | + A, |
| 90 | + lib.GxB_Matrix_deserialize( |
| 91 | + A, |
| 92 | + ffi.NULL, # dtype; we don't check for now |
| 93 | + ffi.from_buffer("void*", data), |
| 94 | + data.nbytes, |
| 95 | + ffi.NULL if desc is None else desc[0], |
| 96 | + ), |
| 97 | + ) |
| 98 | + if free: |
| 99 | + if callable(free): |
| 100 | + return ffi.gc(A, free) |
| 101 | + return ffi.gc(A, matrix.free) |
| 102 | + return A |
| 103 | + |
| 104 | + |
| 105 | +def deserialize_vector(data, *, free=True, nthreads=None): |
| 106 | + """Deserialize a Vector from bytes. |
| 107 | +
|
| 108 | + The `free` argument is called when the object is garbage |
| 109 | + collected, the default is `vector.free()`. If `free` is None then |
| 110 | + there is no automatic garbage collection and it is up to the user |
| 111 | + to free the vector. |
| 112 | + """ |
| 113 | + data = np.frombuffer(data, np.uint8) |
| 114 | + desc = get_serialize_desc(None, nthreads) |
| 115 | + v = ffi.new("GrB_Vector*") |
| 116 | + check_status( |
| 117 | + v, |
| 118 | + lib.GxB_Vector_deserialize( |
| 119 | + v, |
| 120 | + ffi.NULL, # dtype; we don't check for now |
| 121 | + ffi.from_buffer("void*", data), |
| 122 | + data.nbytes, |
| 123 | + ffi.NULL if desc is None else desc[0], |
| 124 | + ), |
| 125 | + ) |
| 126 | + if free: |
| 127 | + if callable(free): |
| 128 | + return ffi.gc(v, free) |
| 129 | + return ffi.gc(v, vector.free) |
| 130 | + return v |
| 131 | + |
| 132 | + |
| 133 | +from suitesparse_graphblas import matrix, vector # noqa isort:skip |
0 commit comments