Skip to content

free_memory() raises error code -1 if nothing has been factorized yet #90

Description

@cmutel

Calling free_memory() on a solver that has not solved anything yet raises PyPardisoError with error code -1.

from pypardiso.scipy_aliases import pypardiso_solver

pypardiso_solver.free_memory()
# PyPardisoError: The Pardiso solver failed with error code -1.

If any solve happens first, the same call works:

import numpy as np, scipy.sparse as sp, pypardiso
from pypardiso.scipy_aliases import pypardiso_solver

pypardiso.spsolve(sp.csr_matrix(np.array([[2.0, 1.0], [1.0, 3.0]])), np.array([1.0, 2.0]))
pypardiso_solver.free_memory()   # fine

Why this happens

free_memory() sets phase 0, which tells Pardiso to release the memory holding the L and U factors:

https://github.com/haasad/PyPardiso/blob/master/pypardiso/pardiso_wrapper.py#L335-L343

On a fresh solver there are no factors to release, so Pardiso gets a request it cannot fulfil and returns -1 ("input inconsistent"). Older MKL versions seem to have let this pass; this shows up with MKL 2026.1.0 from conda-forge.

Suggested fix

free_memory() reads as cleanup, and cleanup when there is nothing to clean up should do nothing rather than raise. Skipping the Pardiso call when no factorization exists would do it, for example:

def free_memory(self, everything=False):
    self.remove_stored_factorization()
    if self.factorized_A.shape[0] == 0 and not everything:
        return
    ...

(everything=True uses phase -1, which releases all internal memory and may still be worth calling on a fresh solver — I have not tested that case.)

Environment

pypardiso 0.4.7
mkl 2026.1.0 (conda-forge)
numpy / scipy 1.26.4 / 1.13.1
Python 3.11.15
OS Windows 11 (10.0.26100), x64

Context

This came up in bw2calc, which calls free_memory() to clear solver state between Monte Carlo iterations. Some of our solver classes never call spsolve, so they hit this on the first iteration. Reported by @KarinTreyer in brightway-lca/brightway2-calc#157. We are also handling it on our side, so this is not urgent for us.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions