55import os
66import pickle
77import subprocess
8+ import zmq
89
910
1011__author__ = "Sarath Menon, Jan Janssen"
2021
2122
2223class LammpsBase :
23- def __init__ (self , cores = 8 , working_directory = "." , cmdargs = None ):
24+ def __init__ (
25+ self , cores = 8 , oversubscribe = False , working_directory = "." , cmdargs = None
26+ ):
2427 self .cores = cores
2528 self .working_directory = working_directory
2629 self ._process = None
30+ self ._oversubscribe = oversubscribe
2731 self ._cmdargs = cmdargs
32+ self ._socket = None
2833
2934 def start_process (self ):
3035 executable = os .path .join (
3136 os .path .dirname (os .path .abspath (__file__ )), "../mpi" , "lmpmpi.py"
3237 )
33- cmds = [
34- "mpiexec" ,
35- "--oversubscribe" ,
38+ context = zmq .Context ()
39+ self ._socket = context .socket (zmq .PAIR )
40+ port_selected = self ._socket .bind_to_random_port ("tcp://*" )
41+ cmds = ["mpiexec" ]
42+ if self ._oversubscribe :
43+ cmds += ["--oversubscribe" ]
44+ cmds += [
3645 "-n" ,
3746 str (self .cores ),
3847 "python" ,
3948 executable ,
49+ "--zmqport" ,
50+ str (port_selected ),
4051 ]
4152 if self ._cmdargs is not None :
4253 cmds .extend (self ._cmdargs )
@@ -64,8 +75,7 @@ def _send(self, command, data=None):
6475 -------
6576 None
6677 """
67- pickle .dump ({"c" : command , "d" : data }, self ._process .stdin )
68- self ._process .stdin .flush ()
78+ self ._socket .send (pickle .dumps ({"c" : command , "d" : data }))
6979
7080 def _receive (self ):
7181 """
@@ -80,7 +90,7 @@ def _receive(self):
8090 data : string
8191 data from the command
8292 """
83- output = pickle .load (self ._process . stdout )
93+ output = pickle .loads (self ._socket . recv () )
8494 return output
8595
8696 @property
@@ -670,6 +680,7 @@ def close(self):
670680 except AttributeError :
671681 pass
672682 self ._process = None
683+ self ._socket = None
673684
674685 # TODO
675686 def __del__ (self ):
0 commit comments