1- #cython: language_level=3
1+ # cython: language_level=3
2+ # cython: embedsignature=True, language_level=3
3+ # cython: freethreading_compatible=True
24cimport numpy as np
35import cython
4- from cpython.pythread cimport (
5- PyThread_type_lock,
6- PyThread_allocate_lock,
7- PyThread_acquire_lock,
8- PyThread_release_lock,
9- PyThread_free_lock
10- )
116
127import numpy as np
138import os
@@ -79,7 +74,6 @@ class PardisoError(Exception):
7974class PardisoWarning (UserWarning ):
8075 pass
8176
82-
8377# call pardiso (pt, maxfct, mnum, mtype, phase, n, a, ia, ja, perm, nrhs, iparm, msglvl, b, x, error)
8478cdef int mkl_progress(int * thread, int * step, char * stage, int stage_len) nogil:
8579 # must be a nogil process to pass to mkl pardiso progress reporting
@@ -175,7 +169,7 @@ ctypedef fused real_or_complex:
175169{{for int_type in [" int_t" , " long_t" ]}}
176170cdef class _PardisoHandle_{{int_type}}:
177171 cdef _MKL_DSS_HANDLE_t handle[64 ]
178- cdef PyThread_type_lock lock
172+ cdef cython.pymutex lock
179173
180174 cdef {{int_type}} n, maxfct, mnum, msglvl
181175 cdef public {{int_type}} matrix_type
@@ -184,7 +178,6 @@ cdef class _PardisoHandle_{{int_type}}:
184178
185179 @ cython.boundscheck (False )
186180 def __cinit__ (self , A_dat_dtype , n , matrix_type , maxfct , mnum , msglvl ):
187- self.lock = PyThread_allocate_lock()
188181
189182 np_int_dtype = np.dtype(f" i{sizeof({{int_type}})}" )
190183
@@ -197,11 +190,13 @@ cdef class _PardisoHandle_{{int_type}}:
197190 self .mnum = mnum
198191 self .msglvl = msglvl
199192
200- if self.msglvl:
201- #for reporting factorization progress via python's `print`
202- mkl_set_progress(mkl_progress)
203- else:
204- mkl_set_progress(mkl_no_progress)
193+
194+ with self .lock:
195+ if self .msglvl:
196+ # for reporting factorization progress via python's `print`
197+ mkl_set_progress(mkl_progress)
198+ else :
199+ mkl_set_progress(mkl_no_progress)
205200
206201 is_single_precision = np.issubdtype(A_dat_dtype, np.single) or np.issubdtype(A_dat_dtype, np.csingle)
207202
@@ -264,14 +259,13 @@ cdef class _PardisoHandle_{{int_type}}:
264259 cdef {{int_type}} error, nrhs
265260 with nogil:
266261 nrhs = rhs.shape[1 ]
267- PyThread_acquire_lock(self.lock, mode=1)
268- pardiso{{if int_type == "long_t"}}_64{{endif}}(
269- self.handle, &self.maxfct, &self.mnum, &self.matrix_type, &phase, &self.n,
270- &a_data[0], &a_indptr[0], &a_indices[0], &self.perm[0],
271- &nrhs, self.iparm, &self.msglvl,
272- &rhs[0, 0], &out[0, 0], &error
273- )
274- PyThread_release_lock(self.lock)
262+ with self .lock:
263+ pardiso{{if int_type == " long_t" }}_64{{endif}}(
264+ self .handle, & self .maxfct, & self .mnum, & self .matrix_type, & phase, & self .n,
265+ & a_data[0 ], & a_indptr[0 ], & a_indices[0 ], & self .perm[0 ],
266+ & nrhs, self .iparm, & self .msglvl,
267+ & rhs[0 , 0 ], & out[0 , 0 ], & error
268+ )
275269 return error
276270
277271 @ cython.boundscheck (False )
@@ -280,20 +274,15 @@ cdef class _PardisoHandle_{{int_type}}:
280274 cdef {{int_type}} phase = - 1 , nrhs = 0 , error = 0
281275
282276 with nogil:
283- PyThread_acquire_lock(self.lock, mode=1)
284- if self._initialized():
285- pardiso{{if int_type == "long_t"}}_64{{endif}}(
286- self.handle, &self.maxfct, &self.mnum, &self.matrix_type,
287- &phase, &self.n, NULL, NULL, NULL, NULL, &nrhs, self.iparm,
288- &self.msglvl, NULL, NULL, &error)
289- if error == 0:
290- for i in range(64):
291- self.handle[i] = NULL
292- PyThread_release_lock(self.lock)
277+ with self .lock:
278+ if self ._initialized():
279+ pardiso{{if int_type == " long_t" }}_64{{endif}}(
280+ self .handle, & self .maxfct, & self .mnum, & self .matrix_type,
281+ & phase, & self .n, NULL , NULL , NULL , NULL , & nrhs, self .iparm,
282+ & self .msglvl, NULL , NULL , & error)
283+ if error == 0 :
284+ for i in range (64 ):
285+ self .handle[i] = NULL
293286 if error != 0 :
294287 raise MemoryError (" Pardiso Memory release error: " + _err_messages[error])
295- if self.lock:
296- #deallocate the lock
297- PyThread_free_lock(self.lock)
298- self.lock = NULL
299288{{endfor}}
0 commit comments