1
- #cython: language_level=3
1
+ # cython: language_level=3
2
+ # cython: embedsignature=True, language_level=3
3
+ # cython: freethreading_compatible=True
2
4
cimport numpy as np
3
5
import 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
- )
11
6
12
7
import numpy as np
13
8
import os
@@ -79,7 +74,6 @@ class PardisoError(Exception):
79
74
class PardisoWarning (UserWarning ):
80
75
pass
81
76
82
-
83
77
# call pardiso (pt, maxfct, mnum, mtype, phase, n, a, ia, ja, perm, nrhs, iparm, msglvl, b, x, error)
84
78
cdef int mkl_progress(int * thread, int * step, char * stage, int stage_len) nogil:
85
79
# must be a nogil process to pass to mkl pardiso progress reporting
@@ -175,7 +169,7 @@ ctypedef fused real_or_complex:
175
169
{{for int_type in [" int_t" , " long_t" ]}}
176
170
cdef class _PardisoHandle_{{int_type}}:
177
171
cdef _MKL_DSS_HANDLE_t handle[64 ]
178
- cdef PyThread_type_lock lock
172
+ cdef cython.pymutex lock
179
173
180
174
cdef {{int_type}} n, maxfct, mnum, msglvl
181
175
cdef public {{int_type}} matrix_type
@@ -184,7 +178,6 @@ cdef class _PardisoHandle_{{int_type}}:
184
178
185
179
@ cython.boundscheck (False )
186
180
def __cinit__ (self , A_dat_dtype , n , matrix_type , maxfct , mnum , msglvl ):
187
- self.lock = PyThread_allocate_lock()
188
181
189
182
np_int_dtype = np.dtype(f" i{sizeof({{int_type}})}" )
190
183
@@ -197,11 +190,13 @@ cdef class _PardisoHandle_{{int_type}}:
197
190
self .mnum = mnum
198
191
self .msglvl = msglvl
199
192
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)
205
200
206
201
is_single_precision = np.issubdtype(A_dat_dtype, np.single) or np.issubdtype(A_dat_dtype, np.csingle)
207
202
@@ -264,14 +259,13 @@ cdef class _PardisoHandle_{{int_type}}:
264
259
cdef {{int_type}} error, nrhs
265
260
with nogil:
266
261
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
+ )
275
269
return error
276
270
277
271
@ cython.boundscheck (False )
@@ -280,20 +274,15 @@ cdef class _PardisoHandle_{{int_type}}:
280
274
cdef {{int_type}} phase = - 1 , nrhs = 0 , error = 0
281
275
282
276
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
293
286
if error != 0 :
294
287
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
299
288
{{endfor}}
0 commit comments