Python
Python interface supports Python 3.8 or newer.
Pip
Install osqp
with the default algebra backend using pip
:
pip install osqp
The builtin
algebra backend is always available for use. Alternative osqp
algebra backends - mkl
or cu12
as of the time of this writing, can also be installed:
To install osqp
with the mkl backend:
pip install osqp[mkl]
To install osqp
with the cu12 (Cuda 12.x) backend:
pip install osqp[cu12]
To install osqp
with the mkl and cu12 backends:
pip install osqp[mkl,cu12]
Note
These commands install osqp with the mkl or cu12 “extras”, which provide the osqp-mkl
or osqp-cuda
packages respectively.
These extension modules are directly importable using import osqp_mkl
or import osqp_cuda
, though you will never directly need to do this.
Using Alternative Algebra Backends
Checking which backends are installed
Use the algebras_available
function to check what algebra backends you have available locally:
$ python -c "from osqp import algebras_available; print(algebras_available())"
['cuda', 'mkl', 'builtin']
Note
Installing alternative algebras like mkl or cu12 does not automatically install the runtime MKL or Cuda libraries for your system. If you have installed osqp with the mkl or cu12 extras but do not see the expected algebra(s) available, you may need to install these libraries separately, and ensure that they are available to the Python interpreter at runtime.
For example, to use mkl, you might need to run:
$ pip install mkl-devel && LD_LIBRARY_PATH=$LD_LIBRARY_PATH:.venv/lib
$ conda install conda-forge::mkl
To use cuda, you might need to install the Cuda toolkit for your system, or (more easily) for your python environment:
pip install nvidia-cublas-cu12 nvidia-cusparse-cu12 nvidia-cuda-runtime-cu12
LD_LIBRARY_PATH=$LD_LIBRARY_PATH:.venv/lib/nvidia/cusparse/lib
conda install conda-forge::cudatoolkit
The exact path to the libraries will vary depending on your system. If unsure, you can try to directly import the extension modules to see what additional runtime libraries are needed:
$ python -c "import osqp_mkl" Traceback (most recent call last): File "<string>", line 1, in <module> ImportError: libmkl_rt.so.2: cannot open shared object file: No such file or directory $ python -c "import osqp_cuda"
If you’re using osqp in a cluster environment, it might be sufficient to just activate a module that provides the necessary libraries. For example, on our clusters, we do a module load intel-mkl/2024.0
or module load cudatoolkit/12.6
to get the necessary libraries.
Switching algebra backends
Use the default_algebra
function to check what algebra backend is currently set as the default:
$ python -c "from osqp import default_algebra; print(default_algebra())"
cuda
By default, osqp
uses the best algebra that is available (cuda
being preferred over mkl
, which is preferred over builtin
).
The default algebra can be overridden by setting the OSQP_ALGEBRA_BACKEND
environment variable to one of builtin
, mkl
, or cuda
.
$ OSQP_ALGEBRA_BACKEND=builtin python -c "from osqp import default_algebra; print(default_algebra())"
builtin
You can set this environment variable in your shell, or in your python script, before importing osqp
.
Alternatively, you can specify the algebra
argument to osqp.OSQP
to use a specific algebra backend for a particular problem.
$ python -c "from osqp import OSQP; problem = OSQP(algebra='mkl'); print(problem.algebra)"
mkl
$ python -c "from osqp import OSQP; problem = OSQP(algebra='builtin'); print(problem.algebra)"
builtin