[SciPy-user] scipy.sparse.linalg.cg not thread safe?

Sturla Molden sturla@molden...
Tue Jan 27 14:02:16 CST 2009

> All local variables are declared static.

Fortran code written like this is not uncommon. It is often used because
Fortran 66 and 77 did not support dynamic memory management or derived
data types.

I'd also like to add that this may still be safe for "parallel processing"
in a Fortran context. Fortran programmers rarely work with threads
directly as C and Python programmers often do. Instead it is common to use
multiple processes (forking or MPI), compiler directives (OpenMP), or
autovectorizing compilers. The code cited could be "safe" for concurrency
in either of these contexts. The issue of what is thread safe and what is
not is actually produced from a bad concurrency abstraction used in C
(posix threads or Win32 threads). Thread safety is a problem Fortran
programmers usually don't have to care about. Parallel processing is not
done with threads. Making this subroutine thread-safe is easy: Just put a
lock in it. Or better yet: put the lock in the C wrapper that f2py
produces. Then, if parallel processing is required, use Fortran the
correct way: e.g. insert OpenMP directives into the Fortran code. Don't
try to do parallel processing by calling this function from multiple
threads concurrently. That is what's causing the havoc. This is Fortran,
not C, so don't use it like C.

Sturla Molden

More information about the SciPy-user mailing list