Skip to content

Commit 009d3d6

Browse files
committed
Basic initialize(...) and is_initialized
1 parent 66eaae9 commit 009d3d6

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

suitesparse_graphblas/__init__.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,45 @@
11
from ._graphblas import ffi, lib # noqa
2+
from . import utils
23
from ._version import get_versions
34

5+
is_initialized = False
6+
7+
8+
def initialize(*, blocking=False, memory_manager="numpy"):
9+
"""Initialize GraphBLAS via GrB_init or GxB_init.
10+
11+
This must be called before any other GraphBLAS functions are called.
12+
A RuntimeError will be raised if called more than once.
13+
14+
Parameters
15+
----------
16+
blocking : bool, optional
17+
Whether to call init with GrB_BLOCKING or GrB_NONBLOCKING.
18+
Default is False.
19+
memory_manager : {'numpy', 'c'}, optional
20+
Choose which malloc/free functions to use. 'numpy' uses numpy's
21+
allocators, which makes it safe to perform zero-copy to and from numpy,
22+
and allows Python to track memory usage via tracemalloc (if enabled).
23+
'c' uses the default allocators. Default is 'numpy'.
24+
25+
The global variable `suitesparse_graphblas.is_initialized` indicates whether
26+
GraphBLAS has been initialized.
27+
"""
28+
global is_initialized
29+
if is_initialized:
30+
raise RuntimeError(
31+
"suitesparse-python is already initialized! Unable to initialize again."
32+
)
33+
blocking = lib.GrB_BLOCKING if blocking else lib.GrB_NONBLOCKING
34+
memory_manager = memory_manager.lower()
35+
if memory_manager == "numpy":
36+
utils.call_gxb_init(ffi, lib, blocking)
37+
elif memory_manager == "c":
38+
lib.GrB_init(blocking)
39+
else:
40+
raise ValueError(f'memory_manager argument must be "numpy" or "c"; got: {memory_manager!r}')
41+
is_initialized = True
42+
43+
444
__version__ = get_versions()["version"]
545
del get_versions

0 commit comments

Comments
 (0)