Skip to content

Commit 214125b

Browse files
committed
Communicated via zmq
1 parent a270840 commit 214125b

File tree

6 files changed

+46
-14
lines changed

6 files changed

+46
-14
lines changed

.ci_support/environment-mpich.yml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
channels:
2+
- conda-forge
3+
dependencies:
4+
- coveralls
5+
- coverage
6+
- codacy-coverage
7+
- lammps >=2022.06.23
8+
- mpi4py =3.1.4
9+
- mpich
10+
- numpy
11+
- distributed
12+
- dask-jobqueue
13+
- pyzmq =25.0.0

.ci_support/environment.yml renamed to .ci_support/environment-openmpi.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,4 @@ dependencies:
1010
- numpy
1111
- distributed
1212
- dask-jobqueue
13+
- pyzmq =25.0.0

.github/workflows/UpdateDependabotPR.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ jobs:
2020
package=$(echo "${{ github.event.pull_request.title }}" | awk '{print $2}')
2121
from=$(echo "${{ github.event.pull_request.title }}" | awk '{print $4}')
2222
to=$(echo "${{ github.event.pull_request.title }}" | awk '{print $6}')
23-
sed -i "/${package}/s/${from}/${to}/g" .ci_support/environment.yml
23+
sed -i "/${package}/s/${from}/${to}/g" .ci_support/environment-openmpi.yml
24+
sed -i "/${package}/s/${from}/${to}/g" .ci_support/environment-mpich.yml
2425
- name: UpdateDependabotPR commit
2526
run: |
2627
git config --local user.email "41898282+github-actions[bot]@users.noreply.github.com"

pylammpsmpi/mpi/lmpmpi.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import numpy as np
88
import pickle
99
import sys
10+
import zmq
1011
from lammps import lammps
1112

1213
__author__ = "Sarath Menon, Jan Janssen"
@@ -43,8 +44,8 @@
4344

4445
# Lammps executable
4546
args = ["-screen", "none"]
46-
if len(sys.argv) > 1:
47-
args.extend(sys.argv[1:])
47+
if len(sys.argv) > 3:
48+
args.extend(sys.argv[3:])
4849
job = lammps(cmdargs=args)
4950

5051

@@ -483,9 +484,15 @@ def _gather_data_from_all_processors(data):
483484

484485

485486
if __name__ == "__main__":
487+
if MPI.COMM_WORLD.rank == 0:
488+
context = zmq.Context()
489+
socket = context.socket(zmq.PAIR)
490+
argument_lst = sys.argv
491+
port_selected = argument_lst[argument_lst.index("--zmqport") + 1]
492+
socket.connect("tcp://localhost:" + port_selected)
486493
while True:
487494
if MPI.COMM_WORLD.rank == 0:
488-
input_dict = pickle.load(sys.stdin.buffer)
495+
input_dict = pickle.loads(socket.recv())
489496
# with open('process.txt', 'a') as file:
490497
# print('Input:', input_dict, file=file)
491498
else:
@@ -498,5 +505,4 @@ def _gather_data_from_all_processors(data):
498505
if MPI.COMM_WORLD.rank == 0 and output is not None:
499506
# with open('process.txt', 'a') as file:
500507
# print('Output:', output, file=file)
501-
pickle.dump(output, sys.stdout.buffer)
502-
sys.stdout.flush()
508+
socket.send(pickle.dumps(output))

pylammpsmpi/utils/lammps.py

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import os
66
import pickle
77
import subprocess
8+
import zmq
89

910

1011
__author__ = "Sarath Menon, Jan Janssen"
@@ -20,23 +21,33 @@
2021

2122

2223
class 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):

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
keywords='lammps, mpi4py',
2727
packages=find_packages(exclude=["*tests*"]),
2828
install_requires=[
29-
'mpi4py==3.1.4'
29+
'mpi4py==3.1.4', "pyzmq==25.0.0"
3030
],
3131
cmdclass=versioneer.get_cmdclass(),
3232
)

0 commit comments

Comments
 (0)