|
1 | 1 | from ._graphblas import ffi, lib # noqa
|
| 2 | +from . import utils |
2 | 3 | from ._version import get_versions
|
3 | 4 |
|
| 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 | + |
4 | 44 | __version__ = get_versions()["version"]
|
5 | 45 | del get_versions
|
0 commit comments