forked from lijiext/lammps
Simplify MPI abort code path, make C++ exceptions optional
This commit is contained in:
parent
b4e2876776
commit
f4263e3849
|
@ -28,6 +28,15 @@ import os
|
||||||
import select
|
import select
|
||||||
import re
|
import re
|
||||||
|
|
||||||
|
|
||||||
|
class MPIAbortException(Exception):
|
||||||
|
def __init__(self, message):
|
||||||
|
self.message = message
|
||||||
|
|
||||||
|
def __str__(self):
|
||||||
|
return repr(self.message)
|
||||||
|
|
||||||
|
|
||||||
class lammps(object):
|
class lammps(object):
|
||||||
# detect if Python is using version of mpi4py that can pass a communicator
|
# detect if Python is using version of mpi4py that can pass a communicator
|
||||||
|
|
||||||
|
@ -151,16 +160,13 @@ class lammps(object):
|
||||||
if cmd: cmd = cmd.encode()
|
if cmd: cmd = cmd.encode()
|
||||||
self.lib.lammps_command(self.lmp,cmd)
|
self.lib.lammps_command(self.lmp,cmd)
|
||||||
|
|
||||||
if self.lib.lammps_has_error(self.lmp):
|
if self.uses_exceptions and self.lib.lammps_has_error(self.lmp):
|
||||||
sb = create_string_buffer(100)
|
sb = create_string_buffer(100)
|
||||||
error_type = self.lib.lammps_get_last_error_message(self.lmp, sb, 100)
|
error_type = self.lib.lammps_get_last_error_message(self.lmp, sb, 100)
|
||||||
error_msg = sb.value.decode().strip()
|
error_msg = sb.value.decode().strip()
|
||||||
|
|
||||||
if error_type == 2 and lammps.has_mpi4py_v2 and self.comm != None and self.comm.Get_size() > 1:
|
if error_type == 2:
|
||||||
print(error_msg, file=sys.stderr)
|
raise MPIAbortException(error_msg)
|
||||||
print("Aborting...", file=sys.stderr)
|
|
||||||
sys.stderr.flush()
|
|
||||||
self.comm.Abort()
|
|
||||||
raise Exception(error_msg)
|
raise Exception(error_msg)
|
||||||
|
|
||||||
def extract_global(self,name,type):
|
def extract_global(self,name,type):
|
||||||
|
@ -294,6 +300,14 @@ class lammps(object):
|
||||||
if name: name = name.encode()
|
if name: name = name.encode()
|
||||||
self.lib.lammps_scatter_atoms(self.lmp,name,type,count,data)
|
self.lib.lammps_scatter_atoms(self.lmp,name,type,count,data)
|
||||||
|
|
||||||
|
@property
|
||||||
|
def uses_exceptions(self):
|
||||||
|
try:
|
||||||
|
if self.lib.lammps_has_error:
|
||||||
|
return True
|
||||||
|
except(AttributeError):
|
||||||
|
return False
|
||||||
|
|
||||||
# -------------------------------------------------------------------------
|
# -------------------------------------------------------------------------
|
||||||
# -------------------------------------------------------------------------
|
# -------------------------------------------------------------------------
|
||||||
# -------------------------------------------------------------------------
|
# -------------------------------------------------------------------------
|
||||||
|
|
Loading…
Reference in New Issue