python: unlock python GIL for long running tasks

Signed-off-by: Thorsten Liebig <liebig@imst.de>
This commit is contained in:
Thorsten Liebig 2020-01-04 15:57:21 +01:00
parent e7475a3bd1
commit 9e5dcecd31
4 changed files with 26 additions and 14 deletions

View File

@ -24,9 +24,9 @@ cimport cython.numeric
cdef extern from "openEMS/nf2ff.h": cdef extern from "openEMS/nf2ff.h":
cdef cppclass cpp_nf2ff "nf2ff": cdef cppclass cpp_nf2ff "nf2ff":
cpp_nf2ff(vector[float] freq, vector[float] theta, vector[float] phi, vector[float] center, unsigned int numThreads) except + cpp_nf2ff(vector[float] freq, vector[float] theta, vector[float] phi, vector[float] center, unsigned int numThreads) nogil except +
bool AnalyseFile(string E_Field_file, string H_Field_file) bool AnalyseFile(string E_Field_file, string H_Field_file) nogil
void SetRadius(float radius) void SetRadius(float radius)
void SetPermittivity(vector[float] permittivity); void SetPermittivity(vector[float] permittivity);
@ -41,7 +41,7 @@ cdef extern from "openEMS/nf2ff.h":
complex[double]** GetEPhi(size_t f_idx) complex[double]** GetEPhi(size_t f_idx)
double** GetRadPower(size_t f_idx) double** GetRadPower(size_t f_idx)
bool Write2HDF5(string filename) bool Write2HDF5(string filename) nogil
void SetVerboseLevel(int level) void SetVerboseLevel(int level)

View File

@ -40,7 +40,11 @@ cdef class _nf2ff:
def AnalyseFile(self, e_file, h_file): def AnalyseFile(self, e_file, h_file):
assert os.path.exists(e_file) assert os.path.exists(e_file)
assert os.path.exists(h_file) assert os.path.exists(h_file)
return self.thisptr.AnalyseFile(e_file.encode('UTF-8'), h_file.encode('UTF-8')) cdef string e_fn = e_file.encode('UTF-8')
cdef string h_fn = h_file.encode('UTF-8')
with nogil:
ok = self.thisptr.AnalyseFile(e_fn, h_fn)
return ok
def SetMirror(self, mirr_type, ny, pos): def SetMirror(self, mirr_type, ny, pos):
if mirr_type<=0: if mirr_type<=0:

View File

@ -23,7 +23,7 @@ from CSXCAD.CSXCAD cimport _ContinuousStructure, ContinuousStructure
cdef extern from "openEMS/openems.h": cdef extern from "openEMS/openems.h":
cdef cppclass _openEMS "openEMS": cdef cppclass _openEMS "openEMS":
_openEMS() except + _openEMS() nogil except +
void SetNumberOfTimeSteps(unsigned int val) void SetNumberOfTimeSteps(unsigned int val)
void SetCSX(_ContinuousStructure* csx) void SetCSX(_ContinuousStructure* csx)
@ -49,13 +49,15 @@ cdef extern from "openEMS/openems.h":
void SetGaussExcite(double f0, double fc) void SetGaussExcite(double f0, double fc)
void SetVerboseLevel(int level) void SetAbort(bool val)
void DebugPEC()
void DebugMaterial()
void DebugCSX()
int SetupFDTD() void SetVerboseLevel(int level)
void RunFDTD() void DebugPEC() nogil
void DebugMaterial() nogil
void DebugCSX() nogil
int SetupFDTD() nogil
void RunFDTD() nogil
@staticmethod @staticmethod
void WelcomeScreen() void WelcomeScreen()

View File

@ -432,15 +432,21 @@ cdef class openEMS:
if verbose is not None: if verbose is not None:
self.thisptr.SetVerboseLevel(verbose) self.thisptr.SetVerboseLevel(verbose)
if debug_pec: if debug_pec:
with nogil:
self.thisptr.DebugPEC() self.thisptr.DebugPEC()
if 'numThreads' in kw: if 'numThreads' in kw:
self.thisptr.SetNumberOfThreads(int(kw['numThreads'])) self.thisptr.SetNumberOfThreads(int(kw['numThreads']))
assert os.getcwd() == sim_path assert os.getcwd() == sim_path
_openEMS.WelcomeScreen() _openEMS.WelcomeScreen()
cdef int EC cdef int EC
with nogil:
EC = self.thisptr.SetupFDTD() EC = self.thisptr.SetupFDTD()
if EC!=0: if EC!=0:
print('Run: Setup failed, error code: {}'.format(EC)) print('Run: Setup failed, error code: {}'.format(EC))
if setup_only or EC!=0: if setup_only or EC!=0:
return EC return EC
with nogil:
self.thisptr.RunFDTD() self.thisptr.RunFDTD()
def SetAbort(self, val):
self.thisptr.SetAbort(val)