Skip to main content

PyRSB: a Cython-based Python interface to librsb

Project description

PyRSB

GPL enforced badge

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 as scipy.sparse.csr_matrix().
  • Conversion from scipy.sparse.csr_matrix().
  • Multiplication by vector/multivector.
  • Rows/columns through nr=a.shape()[0]/nr=a.shape()[1], or nr()/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 (or make 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 using test.py (to compare speed to SciPy)
  • make b will also produce graphs (requires gnuplot)

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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pyrsb-0.2.20220404.2.tar.gz (29.2 kB view details)

Uploaded Source

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

pyrsb-0.2.20220404.2-cp39-cp39-manylinux_2_12_x86_64.manylinux2010_x86_64.whl (3.5 MB view details)

Uploaded CPython 3.9manylinux: glibc 2.12+ x86-64

pyrsb-0.2.20220404.2-cp38-cp38-manylinux_2_12_x86_64.manylinux2010_x86_64.whl (3.6 MB view details)

Uploaded CPython 3.8manylinux: glibc 2.12+ x86-64

pyrsb-0.2.20220404.2-cp37-cp37m-manylinux_2_12_x86_64.manylinux2010_x86_64.whl (3.4 MB view details)

Uploaded CPython 3.7mmanylinux: glibc 2.12+ x86-64

pyrsb-0.2.20220404.2-cp36-cp36m-manylinux_2_12_x86_64.manylinux2010_x86_64.whl (3.4 MB view details)

Uploaded CPython 3.6mmanylinux: glibc 2.12+ x86-64

File details

Details for the file pyrsb-0.2.20220404.2.tar.gz.

File metadata

  • Download URL: pyrsb-0.2.20220404.2.tar.gz
  • Upload date:
  • Size: 29.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.0 CPython/3.9.12

File hashes

Hashes for pyrsb-0.2.20220404.2.tar.gz
Algorithm Hash digest
SHA256 76362d176be1751c70e57245f07a58976de6ce2ff255a2164998d6e033044b25
MD5 345eecb07fdac1a5637bb2f555cdd52c
BLAKE2b-256 4cd50d5687fb576d4e51acfa761a241867b8d72d2ddb1d36b5f3a847f488a0fd

See more details on using hashes here.

File details

Details for the file pyrsb-0.2.20220404.2-cp39-cp39-manylinux_2_12_x86_64.manylinux2010_x86_64.whl.

File metadata

File hashes

Hashes for pyrsb-0.2.20220404.2-cp39-cp39-manylinux_2_12_x86_64.manylinux2010_x86_64.whl
Algorithm Hash digest
SHA256 f0be3861dc8c56408e32f12f77bef006c08196dc94882ca46a70538943ab0cb1
MD5 02be8b15ba6fcb791058910dd6de9e23
BLAKE2b-256 43b28aada6171aafdb4abd5291e1f457bf2ef9ddc3d5fad14f1c68ed730da227

See more details on using hashes here.

File details

Details for the file pyrsb-0.2.20220404.2-cp38-cp38-manylinux_2_12_x86_64.manylinux2010_x86_64.whl.

File metadata

File hashes

Hashes for pyrsb-0.2.20220404.2-cp38-cp38-manylinux_2_12_x86_64.manylinux2010_x86_64.whl
Algorithm Hash digest
SHA256 f7422b0c9da94e383e612bee8cb2a883c27fde9469acb35a99fcbdcd93748532
MD5 64467c110ce2206c39a2663d4a0f9c84
BLAKE2b-256 734a4e88d7e1b0adddc5cc9abacb176e8c02ae52259fc9c6ff6fb7165a87d0ba

See more details on using hashes here.

File details

Details for the file pyrsb-0.2.20220404.2-cp37-cp37m-manylinux_2_12_x86_64.manylinux2010_x86_64.whl.

File metadata

File hashes

Hashes for pyrsb-0.2.20220404.2-cp37-cp37m-manylinux_2_12_x86_64.manylinux2010_x86_64.whl
Algorithm Hash digest
SHA256 28da9a27b3cd583718fc2aa9de2931c2270fee15095cece651c6dafacb77d31d
MD5 0436ee55fb5c57cbf37b7ebb3f90e77b
BLAKE2b-256 68f000a3633c8180e823a4c4f4c0639ee10a4a0dd61d3a4042917e60243aad79

See more details on using hashes here.

File details

Details for the file pyrsb-0.2.20220404.2-cp36-cp36m-manylinux_2_12_x86_64.manylinux2010_x86_64.whl.

File metadata

File hashes

Hashes for pyrsb-0.2.20220404.2-cp36-cp36m-manylinux_2_12_x86_64.manylinux2010_x86_64.whl
Algorithm Hash digest
SHA256 5b99d2526e24b63ac98a65719b66e83d27cbae23569b315af559b388bd7d3f0e
MD5 85afe831279993dc1762f969922eff83
BLAKE2b-256 5f73e8bbe4a46690936c5f6c46e72c82ef62acbe427d7ae957a309cb594b964c

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page