Skip to content

Commit ff84b9c

Browse files
authored
Merge pull request #97 from pyiron/zmq
Communicated via zmq
2 parents a270840 + 3bcd4e0 commit ff84b9c

File tree

11 files changed

+96
-19
lines changed

11 files changed

+96
-19
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"

.github/workflows/coverage.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ jobs:
2222
channels: conda-forge
2323
channel-priority: strict
2424
use-mamba: true
25-
environment-file: .ci_support/environment.yml
25+
environment-file: .ci_support/environment-openmpi.yml
2626
- name: Setup
2727
shell: bash -l {0}
2828
run: |

.github/workflows/pypicheck.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ jobs:
1818
channels: conda-forge
1919
channel-priority: strict
2020
use-mamba: true
21-
environment-file: .ci_support/environment.yml
21+
environment-file: .ci_support/environment-openmpi.yml
2222
- name: Setup
2323
shell: bash -l {0}
2424
run: |

.github/workflows/unittests.yml renamed to .github/workflows/unittests-mpich.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# This workflow will install Python dependencies, run tests and lint with a variety of Python versions
22
# For more information see: https://help.github.com/actions/language-and-framework-guides/using-python-with-github-actions
33

4-
name: Python package
4+
name: mpich
55

66
on:
77
push:
@@ -26,7 +26,7 @@ jobs:
2626
channels: conda-forge
2727
channel-priority: strict
2828
use-mamba: true
29-
environment-file: .ci_support/environment.yml
29+
environment-file: .ci_support/environment-mpich.yml
3030
- name: Setup
3131
shell: bash -l {0}
3232
run: |
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# This workflow will install Python dependencies, run tests and lint with a variety of Python versions
2+
# For more information see: https://help.github.com/actions/language-and-framework-guides/using-python-with-github-actions
3+
4+
name: OpenMPI
5+
6+
on:
7+
push:
8+
branches: [ main ]
9+
pull_request:
10+
branches: [ main ]
11+
12+
jobs:
13+
build:
14+
15+
runs-on: ubuntu-latest
16+
strategy:
17+
matrix:
18+
python-version: ['3.8', '3.9', '3.10', '3.11']
19+
20+
steps:
21+
- uses: actions/checkout@v2
22+
- uses: conda-incubator/setup-miniconda@v2
23+
with:
24+
python-version: ${{ matrix.python-version }}
25+
miniforge-variant: Mambaforge
26+
channels: conda-forge
27+
channel-priority: strict
28+
use-mamba: true
29+
environment-file: .ci_support/environment-openmpi.yml
30+
- name: Setup
31+
shell: bash -l {0}
32+
run: |
33+
pip install --no-deps .
34+
- name: Test
35+
shell: bash -l {0}
36+
run: coverage run --omit pylammpsmpi/_version.py -m unittest discover tests

pylammpsmpi/lammps_wrapper.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,17 +23,25 @@ class LammpsLibrary:
2323
"""
2424

2525
def __init__(
26-
self, cores=1, working_directory=".", client=None, mode="local", cmdargs=None
26+
self,
27+
cores=1,
28+
oversubscribe=False,
29+
working_directory=".",
30+
client=None,
31+
mode="local",
32+
cmdargs=None,
2733
):
2834
self.cores = cores
2935
self.working_directory = working_directory
36+
self.oversubscribe = oversubscribe
3037
self.client = client
3138
self.mode = mode
3239

3340
if self.mode == "dask":
3441
fut = self.client.submit(
3542
LammpsBase,
3643
cores=self.cores,
44+
oversubscribe=self.oversubscribe,
3745
working_directory=self.working_directory,
3846
cmdargs=cmdargs,
3947
actor=True,
@@ -46,6 +54,7 @@ def __init__(
4654
elif self.mode == "local":
4755
self.lmp = LammpsBase(
4856
cores=self.cores,
57+
oversubscribe=self.oversubscribe,
4958
working_directory=self.working_directory,
5059
cmdargs=cmdargs,
5160
)

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

0 commit comments

Comments
 (0)