From 1dec0e0380824c9c650359c0f7081ef65b9480e0 Mon Sep 17 00:00:00 2001 From: Guillaume Fraux Date: Thu, 10 Oct 2024 15:41:08 +0200 Subject: [PATCH] Do not hold Python's GIL when running commands --- python/cplumed.pxd | 20 ++++++++++---------- python/plumed.pyx | 19 +++++++++---------- 2 files changed, 19 insertions(+), 20 deletions(-) diff --git a/python/cplumed.pxd b/python/cplumed.pxd index 440b03981a..f2aa5af6a6 100644 --- a/python/cplumed.pxd +++ b/python/cplumed.pxd @@ -31,7 +31,7 @@ cdef extern from "Plumed.h": size_t nelem size_t* shape size_t flags - # ignore other + # ignore other ctypedef struct plumed_nothrow_handler: void* ptr void (*handler)(void*,int,const char*,const void*) @@ -46,12 +46,12 @@ cdef extern from "Plumed.h": plumed_error_filesystem_path path1 plumed_error_filesystem_path path2 # ignore other members - void plumed_cmd_safe_nothrow(plumed p,const char*key,plumed_safeptr safe,plumed_nothrow_handler nothrow) - void plumed_error_set(void*ptr,int code,const char*what,const void* opt) - void plumed_error_init(plumed_error* error) - void plumed_error_finalize(plumed_error error) - plumed plumed_create() - plumed plumed_create_dlopen(const char*path) - plumed plumed_create_invalid() - void plumed_finalize(plumed p) - int plumed_valid(plumed p) + void plumed_cmd_safe_nothrow(plumed p,const char*key,plumed_safeptr safe,plumed_nothrow_handler nothrow) nogil + void plumed_error_set(void*ptr,int code,const char*what,const void* opt) nogil + void plumed_error_init(plumed_error* error) nogil + void plumed_error_finalize(plumed_error error) nogil + plumed plumed_create() nogil + plumed plumed_create_dlopen(const char*path) nogil + plumed plumed_create_invalid() nogil + void plumed_finalize(plumed p) nogil + int plumed_valid(plumed p) nogil diff --git a/python/plumed.pyx b/python/plumed.pyx index 20b6232152..37cecd981a 100644 --- a/python/plumed.pyx +++ b/python/plumed.pyx @@ -36,6 +36,7 @@ import sys import warnings import types +import cython from cython.operator import dereference if sys.version_info < (3,): @@ -185,7 +186,8 @@ cdef class Plumed: cplumed.plumed_error_init(&error) nothrow.ptr=&error nothrow.handler=cplumed.plumed_error_set - cplumed.plumed_cmd_safe_nothrow(self.c_plumed,ckey,safe,nothrow) + with cython.nogil(): + cplumed.plumed_cmd_safe_nothrow(self.c_plumed,ckey,safe,nothrow) if(error.code): try: self.raise_exception(error) @@ -507,7 +509,7 @@ def read_as_pandas(file_or_path,enable_constants=True,enable_conversion=True,ker convert=_build_convert_function(kernel) # if necessary, set convert_all if enable_conversion=='all': convert_all=convert - + # handle file file_or_path=_fix_file(file_or_path,'rt') @@ -588,7 +590,7 @@ def write_pandas(df,file_or_path=None): colvar=plumed.read_as_colvar("COLVAR") colvar["distance"]=colvar["distance"]*2 plumed.write_pandas(colvar) - + """ # importing pandas is pretty slow, so we only do it when needed import pandas as pd @@ -787,7 +789,7 @@ def _readvimdict(plumedroot=None,kernel=None): # read dictionary for opt in plumedDictionary[action]: # skip label (it is added automatically) - if opt["menu"] != "(label)": + if opt["menu"] != "(label)": ret[action][re.sub("=$","",opt["word"])]=opt["menu"] return ret,doc @@ -916,7 +918,7 @@ def _format_at_one_residue(builder,name,residue,chain): return "@" + name + "-" + chain + str(residue) else: assert False - + def _format_at_one_chain(builder,name,residue,chain): res="" if hasattr(residue,'__iter__') and not isinstance(residue,str): @@ -924,7 +926,7 @@ def _format_at_one_chain(builder,name,residue,chain): res+=builder._separator + _format_at_one_residue(builder,name,x,chain) else: res+=builder._separator + _format_at_one_residue(builder,name,residue,chain) - + return res def _format_at(builder,name,residue,chain=""): @@ -1074,7 +1076,7 @@ def _format_anything(builder,name,arg): ret="" if name == "verbatim": ret+=_format_verbatim(builder,arg) - elif isinstance(arg,bool) : + elif isinstance(arg,bool) : ret+=_format_flag(builder,name,arg) elif isinstance(arg,_numbered): ret+=_format_numbered(builder,name,arg) @@ -1236,6 +1238,3 @@ class InputBuilder: Accepts a list/tuple. """ return _replicas(arg) - - -