Skip to main content

grblas

Conda Version Conda Platforms License Build Status Coverage Status Code style Binder

Python wrapper around GraphBLAS

To install, conda install -c conda-forge grblas. This will also install the SuiteSparse graphblas compiled C library.

Currently works with SuiteSparse:GraphBLAS, but the goal is to make it work with all implementations of the GraphBLAS spec.

The approach taken with this library is to follow the C-API specification as closely as possible while making improvements allowed with the Python syntax. Because the spec always passes in the output object to be written to, we follow the same, which is very different from the way Python normally operates. In fact, many who are familiar with other Python data libraries (numpy, pandas, etc) will find it strange to not create new objects for every call.

At the highest level, the goal is to separate output, mask, and accumulator on the left side of the assignment operator = and put the computation on the right side. Unfortunately, that approach doesn't always work very well with how Python handles assignment, so instead we (ab)use the left-shift << notation to give the same flavor of assignment. This opens up all kinds of nice possibilities.

This is an example of how the mapping works:

// C call
GrB_Matrix_mxm(M, mask, GrB_PLUS_INT64, GrB_MIN_PLUS_INT64, A, B, NULL)
# Python call
M(mask.V, accum=binary.plus) << A.mxm(B, semiring.min_plus)

The expression on the right A.mxm(B) creates a delayed object which does no computation. Once it is used in the << expression with M, the whole thing is translated into the equivalent GraphBLAS call.

Delayed objects also have a .new() method which can be used to force computation and return a new object. This is convenient and often appropriate, but will create many unnecessary objects if used in a loop. It also loses the ability to perform accumulation with existing results. For best performance, following the standard GraphBLAS approach of (1) creating the object outside the loop and (2) using the object repeatedly within each loop is a much better approach, even if it doesn't feel very Pythonic.

Descriptor flags are set on the appropriate elements to keep logic close to what it affects. Here is the same call with descriptor bits set. ttcsr indicates transpose the first and second matrices, complement the structure of the mask, and do a replacement on the output.

// C call
GrB_Matrix_mxm(M, mask, GrB_PLUS_INT64, GrB_MIN_PLUS_INT64, A, B, desc.ttcsr)
# Python call
M(~mask.S, accum=binary.plus, replace=True) << A.T.mxm(B.T, semiring.min_plus)

The objects receiving the flag operations (A.T, ~mask, etc) are also delayed objects. They hold on to the state but do no computation, allowing the correct descriptor bits to be set in a single GraphBLAS call.

If no mask or accumulator is used, the call looks like this:

M << A.mxm(B, semiring.min_plus)

The use of << to indicate updating is actually just syntactic sugar for a real .update() method. The above expression could be written as:

M.update(A.mxm(B, semiring.min_plus))

Operations

M(mask, accum) << A.mxm(B, semiring)        # mxm
w(mask, accum) << A.mxv(v, semiring)        # mxv
w(mask, accum) << v.vxm(B, semiring)        # vxm
M(mask, accum) << A.ewise_add(B, binaryop)  # eWiseAdd
M(mask, accum) << A.ewise_mult(B, binaryop) # eWiseMult
M(mask, accum) << A.kronecker(B, binaryop)  # kronecker
M(mask, accum) << A.T                       # transpose

Extract

M(mask, accum) << A[rows, cols]             # rows and cols are a list or a slice
w(mask, accum) << A[rows, col_index]        # extract column
w(mask, accum) << A[row_index, cols]        # extract row
s = A[row_index, col_index].value           # extract single element

Assign

M(mask, accum)[rows, cols] << A             # rows and cols are a list or a slice
M(mask, accum)[rows, col_index] << v        # assign column
M(mask, accum)[row_index, cols] << v        # assign row
M(mask, accum)[rows, cols] << s             # assign scalar to many elements
M[row_index, col_index] << s                # assign scalar to single element
                                            # (mask and accum not allowed)
del M[row_index, col_index]                 # remove single element

Apply

M(mask, accum) << A.apply(unaryop)
M(mask, accum) << A.apply(binaryop, left=s)   # bind-first
M(mask, accum) << A.apply(binaryop, right=s)  # bind-second

Reduce

v(mask, accum) << A.reduce_rows(op)         # reduce row-wise
v(mask, accum) << A.reduce_columns(op)      # reduce column-wise
s(accum) << A.reduce_scalar(op)
s(accum) << v.reduce(op)

Creating new Vectors / Matrices

A = Matrix.new(dtype, num_rows, num_cols)   # new_type
B = A.dup()                                 # dup
A = Matrix.from_values([row_indices], [col_indices], [values])  # build

New from delayed

Delayed objects can be used to create a new object using .new() method

C = A.mxm(B, semiring).new()

Properties

size = v.size                               # size
nrows = M.nrows                             # nrows
ncols = M.ncols                             # ncols
nvals = M.nvals                             # nvals
rindices, cindices, vals = M.to_values()    # extractTuples

Initialization

There is a mechanism to initialize grblas with a context prior to use. This allows for setting the backend to use as well as the blocking/non-blocking mode. If the context is not initialized, a default initialization will be performed automatically.

import grblas as gb
# Context initialization must happen before any other imports
gb.init('suitesparse', blocking=True)

# Now we can import other items from grblas
from grblas import binary, semiring
from grblas import Matrix, Vector, Scalar

Performant User Defined Functions

grblas requires numba which enables compiling user-defined Python functions to native C for use in GraphBLAS.

Example customized UnaryOp:

from grblas import unary
from grblas.ops import UnaryOp

def force_odd_func(x):
    if x % 2 == 0:
        return x + 1
    return x

UnaryOp.register_new('force_odd', force_odd_func)

v = Vector.from_values([0, 1, 3], [1, 2, 3])
w = v.apply(unary.force_odd).new()
w  # indexes=[0, 1, 3], values=[1, 3, 3]

Similar methods exist for BinaryOp, Monoid, and Semiring.

Import/Export connectors to the Python ecosystem

grblas.io contains functions for converting to and from:

import grblas as gb

# numpy arrays
# 1-D array becomes Vector, 2-D array becomes Matrix
A = gb.io.from_numpy(m)
m = gb.io.to_numpy(A)

# scipy.sparse matrices
A = gb.io.from_scipy_sparse_matrix(m)
m = gb.io.to_scipy_sparse_matrix(m, format='csr')

# networkx graphs
A = gb.io.from_networkx(g)
g = gb.io.to_networkx(A)

Attribution

This library borrows some great ideas from pygraphblas, especially around parsing operator names from SuiteSparse and the concept of a Scalar which the backend implementation doesn't need to know about.

Release files for grblas 1.3.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for grblas 1.3.3
File Size Uploaded
grblas-1.3.3.tar.gz 264.0 kB Details

Built distributions (wheels)

Table of built distributions (wheels) for grblas 1.3.3
File
grblas-1.3.3-cp38-cp38-win_amd64.whl CPython 3.8 CPython 3.8 Windows x86-64 Details
grblas-1.3.3-cp38-cp38-manylinux2014_x86_64.whl CPython 3.8 CPython 3.8 Linux glibc 2.17+ x86-64 Details
grblas-1.3.3-cp38-cp38-manylinux2010_x86_64.whl CPython 3.8 CPython 3.8 Linux glibc 2.12+ x86-64 Details
grblas-1.3.3-cp38-cp38-macosx_10_9_x86_64.whl CPython 3.8 CPython 3.8 macOS 10.9+ x86-64 Details
grblas-1.3.3-cp37-cp37m-win_amd64.whl CPython 3.7 CPython 3.7 pymalloc Windows x86-64 Details
grblas-1.3.3-cp37-cp37m-manylinux2014_x86_64.whl CPython 3.7 CPython 3.7 pymalloc Linux glibc 2.17+ x86-64 Details
grblas-1.3.3-cp37-cp37m-manylinux2010_x86_64.whl CPython 3.7 CPython 3.7 pymalloc Linux glibc 2.12+ x86-64 Details
grblas-1.3.3-cp37-cp37m-macosx_10_9_x86_64.whl CPython 3.7 CPython 3.7 pymalloc macOS 10.9+ x86-64 Details

Total release size: 364.9 MB

Release files / grblas-1.3.3.tar.gz

Download URL grblas-1.3.3.tar.gz
Size 264.0 kB
Tags Source
SHA-256 checksum
How to use checksums
029c9b8fdc805b2d9ee6279cc6551d8bbe0f044a21c977a43a655033cd28a45c
BLAKE2b-256 checksum
How to use checksums
f1d458c60f5154d8b6ec337581e0fb08c6420524507f13a174fba229751b64d0
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.3.0 pkginfo/1.7.0 requests/2.24.0 setuptools/50.3.1.post20201107 requests-toolbelt/0.9.1 tqdm/4.48.2 CPython/3.7.7

Release files / grblas-1.3.3-cp38-cp38-win_amd64.whl

Download URL grblas-1.3.3-cp38-cp38-win_amd64.whl
Size 30.2 MB
Tags CPython 3.8 Windows x86-64
SHA-256 checksum
How to use checksums
b8f8f26c8d579e9147ddc8b9bf706ef2cc7f452afc6126074e750a789aa627a5
BLAKE2b-256 checksum
How to use checksums
d58791265fda8b4e1a78dd94092c671f2459a236d61d30f59e33ac50ae246b53
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.3.0 pkginfo/1.6.1 requests/2.24.0 setuptools/50.3.1.post20201107 requests-toolbelt/0.9.1 tqdm/4.50.2 CPython/3.8.5

Release files / grblas-1.3.3-cp38-cp38-manylinux2014_x86_64.whl

Download URL grblas-1.3.3-cp38-cp38-manylinux2014_x86_64.whl
Size 48.9 MB
Tags CPython 3.8 Linux glibc 2.17+ x86-64
SHA-256 checksum
How to use checksums
c9f26f61a4523929837b53dcce1516fdd718b0bb6aec237128467abd567e9aa7
BLAKE2b-256 checksum
How to use checksums
0ef129c29cc77ceac9ec3ce281f9a0ec2791a20e50c5c08879dd48e77fe6f55f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.3.0 pkginfo/1.7.0 requests/2.24.0 setuptools/50.3.1.post20201107 requests-toolbelt/0.9.1 tqdm/4.51.0 CPython/3.8.5

Release files / grblas-1.3.3-cp38-cp38-manylinux2010_x86_64.whl

Download URL grblas-1.3.3-cp38-cp38-manylinux2010_x86_64.whl
Size 53.1 MB
Tags CPython 3.8 Linux glibc 2.12+ x86-64
SHA-256 checksum
How to use checksums
38c19fca45bf3794fd82399ac1dacffdf1a95f2e811cecc8814a78945b185860
BLAKE2b-256 checksum
How to use checksums
7b6c18ff8b199bd6c855ea2935bd57e5b9447d0bb0bf73e7b9e5e935067d5cb3
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.3.0 pkginfo/1.7.0 requests/2.24.0 setuptools/50.3.1.post20201107 requests-toolbelt/0.9.1 tqdm/4.51.0 CPython/3.8.5

Release files / grblas-1.3.3-cp38-cp38-macosx_10_9_x86_64.whl

Download URL grblas-1.3.3-cp38-cp38-macosx_10_9_x86_64.whl
Size 50.1 MB
Tags CPython 3.8 macOS 10.9+ x86-64
SHA-256 checksum
How to use checksums
430125542f2d5257aafa3e78f1772b559c77c9d40ddb59d4a362fea1b2a437ef
BLAKE2b-256 checksum
How to use checksums
a8c6956c978fc7861e3c1b7a1b94012687a08aedae3d0c4e310a854f1b2a7556
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.3.0 pkginfo/1.7.0 requests/2.24.0 setuptools/50.3.1.post20201107 requests-toolbelt/0.9.1 tqdm/4.48.2 CPython/3.7.7

Release files / grblas-1.3.3-cp37-cp37m-win_amd64.whl

Download URL grblas-1.3.3-cp37-cp37m-win_amd64.whl
Size 30.2 MB
Tags CPython 3.7 CPython 3.7 pymalloc Windows x86-64
SHA-256 checksum
How to use checksums
da7b2ef44f4ed61d0decd8bb5fec93e5a6e7cb7d89de268117a34470f9bab849
BLAKE2b-256 checksum
How to use checksums
3e88d3dd1194050bd696771552c19466384abb5d35dc4898f3e76a18a2761d15
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.3.0 pkginfo/1.6.1 requests/2.24.0 setuptools/50.3.1.post20201107 requests-toolbelt/0.9.1 tqdm/4.50.2 CPython/3.8.5

Release files / grblas-1.3.3-cp37-cp37m-manylinux2014_x86_64.whl

Download URL grblas-1.3.3-cp37-cp37m-manylinux2014_x86_64.whl
Size 48.9 MB
Tags CPython 3.7 CPython 3.7 pymalloc Linux glibc 2.17+ x86-64
SHA-256 checksum
How to use checksums
f1d917780dbf9b6c2144ad74b58c24c60df0dd7545c75b683cec89afdb00d77f
BLAKE2b-256 checksum
How to use checksums
37c24b92926ea0a3c5263208f2faaef9154e3816c3a9e03237d3add66f42792a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.3.0 pkginfo/1.7.0 requests/2.24.0 setuptools/50.3.1.post20201107 requests-toolbelt/0.9.1 tqdm/4.51.0 CPython/3.8.5

Release files / grblas-1.3.3-cp37-cp37m-manylinux2010_x86_64.whl

Download URL grblas-1.3.3-cp37-cp37m-manylinux2010_x86_64.whl
Size 53.1 MB
Tags CPython 3.7 CPython 3.7 pymalloc Linux glibc 2.12+ x86-64
SHA-256 checksum
How to use checksums
d261d216134bc7e1ab0407ecdeac0acfb8630d3c5abfa33976e2af520d8ba557
BLAKE2b-256 checksum
How to use checksums
2f87da23ee9d04b49c19f560c295ac5a05c0e11c5683bacb25ab93c9d4514b0d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.3.0 pkginfo/1.7.0 requests/2.24.0 setuptools/50.3.1.post20201107 requests-toolbelt/0.9.1 tqdm/4.51.0 CPython/3.8.5

Release files / grblas-1.3.3-cp37-cp37m-macosx_10_9_x86_64.whl

Download URL grblas-1.3.3-cp37-cp37m-macosx_10_9_x86_64.whl
Size 50.1 MB
Tags CPython 3.7 CPython 3.7 pymalloc macOS 10.9+ x86-64
SHA-256 checksum
How to use checksums
fa8b75bf215b4f8f628862a780e57eab341b743ac6c4a90e8afe9c7a40f375eb
BLAKE2b-256 checksum
How to use checksums
0e00a2e8c0359c3311b46ef10a2f20bb3384d1c6b67f6555c28f9f67154fe744
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.3.0 pkginfo/1.7.0 requests/2.24.0 setuptools/50.3.1.post20201107 requests-toolbelt/0.9.1 tqdm/4.48.2 CPython/3.7.7
Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page