PyRSB: a Cython-based Python interface to librsb
Project description
PyRSB
librsb is a high performance sparse matrix library implementing the Recursive Sparse Blocks format, which is especially well suited for multiplications in iterative methods on very large sparse matrices.
PyRSB is a Cython-based Python interface to librsb.
On multicore machines, PyRSB can be several times faster than e.g. scipy.sparse.csr_matrix()
.
For an example how to invoke it with minimal overhead, see the advanced example.
So far, PyRSB is a prototype tested on Linux only. The librsb library instead is mature and well tested. Prospective PyRSB users and collaborators are welcome to contact me.
Features
The following functionality is implemented:
- Initialization with
rsb.rsb_matrix()
styled asscipy.sparse.csr_matrix()
. - Conversion from
scipy.sparse.csr_matrix()
. - Multiplication by vector/multivector.
- Rows/columns through
nr=a.shape()[0]
/nr=a.shape()[1]
, ornr()
/nc()
. find()
,find_block()
,tril()
,triu()
,shape()
,nnz
.print
'able.- PyRSB-Specific:
autotune()
,do_print()
. - load from a Matrix Market file, e.g.
rsb_matrix(bytes(filename,encoding='utf-8'))
Build and Use
- If you want the
Makefile
to build librsb (in this directory):make all-local
will attempt downloading librsb-1.3.0.0 from the web and building it here before building pyrsb. If the file is in place, it won't download it a second time. After that,make local-librsb-pyrsb
(ormake lp
) will build pyrsb using local librsb, then run it. This method shall use the best compilation flags. - If you have librsb already installed:
make
shall build and test. - Make sure you have
cython
,scipy
,numpy
. installed. - If you don't have librsb installed you may want to try via pip
pip install pyrsb
- If you want to install librsb on Ubuntu or Debian:
sudo apt-get install librsb-dev
shall suffice. Other operating systems have librsb, too. Please check yours. Or check librsb's web site. make test
will test benchmark code usingtest.py
(to compare speed to SciPy)make b
will also produce graphs (requiresgnuplot
)
Example Usage
# Example: demo1.py
"""
pyrsb demo
"""
import numpy
import scipy
from scipy.sparse import csr_matrix
from pyrsb import *
V = [11.0, 12.0, 22.0]
I = [0, 0, 1]
J = [0, 1, 1]
c = csr_matrix((V, (I, J)))
print(c)
# several constructor forms, as with csr_matrix:
a = rsb_matrix((V, (I, J)))
a = rsb_matrix((V, (I, J)), [3, 3])
a = rsb_matrix((V, (I, J)), sym="S") # symmetric example
print(a)
a = rsb_matrix((4, 4))
a = rsb_matrix(c)
nrhs = 1 # set to nrhs>1 to multiply by multiple vectors at once
nr = a.shape[0]
nc = a.shape[1]
order = "F"
x = numpy.empty([nc, nrhs], dtype=rsb_dtype, order=order)
y = numpy.empty([nr, nrhs], dtype=rsb_dtype, order=order)
x[:, :] = 1.0
y[:, :] = 0.0
print(a)
print(x)
print(y)
# import rsb # import operators
# a.autotune() # makes only sense for large matrices
y = y + a * x
# equivalent to y=y+c*x
print(y)
del a
Example Advanced Usage
# Example: demo2.py
"""
pyrsb demo
"""
import numpy
import scipy
from scipy.sparse import csr_matrix
from pyrsb import *
V = [11.0, 12.0, 22.0]
I = [0, 0, 1]
J = [0, 1, 1]
a = rsb_matrix((V, (I, J)))
nrhs = 4 # set to nrhs>1 to multiply by multiple vectors at once
nr = a.shape[0]
nc = a.shape[1]
# Choose Fortran or "by columns" order here.
order = "F"
x = numpy.empty([nc, nrhs], dtype=rsb_dtype, order=order)
y = numpy.empty([nr, nrhs], dtype=rsb_dtype, order=order)
x[:, :] = 1.0
y[:, :] = 0.0
print(a)
print(x)
print(y)
# Autotuning example: use it if you need many multiplication iterations on huge matrices (>>1e6 nonzeroes).
# Here general (nrhs=1) case:
a.autotune()
# Here with all the autotuning parameters specified:
a.autotune(1.0,1,2.0,'N',1.0,nrhs,'F',1.0,False)
# Inefficient: reallocate y
y = y + a * x
# Inefficient: reallocate y
y += a * x
# Equivalent but more efficient: don't reallocate y
a._spmm(x,y)
print(y)
del a
License
GPLv3+
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distributions
Hashes for pyrsb-0.2.20220404.1-cp39-cp39-manylinux_2_12_x86_64.manylinux2010_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 48f2add9a4b81fa50d19d306209b80605fc822752b566e61cee316920893a1b7 |
|
MD5 | e771b0e6e0fa2aee575e35ae3ecd7693 |
|
BLAKE2b-256 | b3d6dc441f7d257cdafa49352d3903cccd2d256418fd6ba47d44d69a9a749dbb |
Hashes for pyrsb-0.2.20220404.1-cp38-cp38-manylinux_2_12_x86_64.manylinux2010_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | d5b091ed31be5e43ee30d0df308e24548229f70d2f17a47383d4f4c3f6f2901a |
|
MD5 | c9848adfd8372d85250f7d7f856ef8b7 |
|
BLAKE2b-256 | 1f03974d3f125a87a88f35e288fc7c8e5c4453aa301a9534c59f173cb9548318 |
Hashes for pyrsb-0.2.20220404.1-cp37-cp37m-manylinux_2_12_x86_64.manylinux2010_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | fed04d711b4ada5be2eed17d65540e05b1f3b68ec19f6f71838e1d94e41133cb |
|
MD5 | f77ca6140259798c9bf781c971f6147b |
|
BLAKE2b-256 | aa4baa4215dedef729dcc8ede4c34f97fa512bf5604ef6c323f6e8568f768a73 |
Hashes for pyrsb-0.2.20220404.1-cp36-cp36m-manylinux_2_12_x86_64.manylinux2010_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 72f1540237e10351035f4a7cc8ebf55c6858a7acc6838b6a51c15e40d62a5c02 |
|
MD5 | d77f358254af90574f39e567b11574fd |
|
BLAKE2b-256 | d2b1cb9ef9f253c1fe9892bcd0b28ac66bc469e9ffa229b7744a61771963d079 |