Last active
August 29, 2015 14:14
-
-
Save donkirkby/16a89d276e46abb0a106 to your computer and use it in GitHub Desktop.
Example that shows how to wait for an MPI message without pegging the CPU to 100%.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
from mpi4py import MPI | |
import logging | |
import time | |
import sys | |
def polling_receive(comm, source): | |
# Set this to 0 for maximum responsiveness, but that will peg CPU to 100% | |
sleep_seconds = 0.1 | |
if sleep_seconds > 0: | |
while not comm.Iprobe(source=MPI.ANY_SOURCE): | |
time.sleep(sleep_seconds) | |
status = MPI.Status() | |
result = comm.recv(source=MPI.ANY_SOURCE, status=status) | |
return result | |
def main(): | |
logging.basicConfig(level=logging.INFO, | |
format='%(asctime)s[%(levelname)s]%(message)s') | |
comm = MPI.COMM_WORLD | |
process_count = comm.Get_size() | |
if process_count < 2: | |
if len(sys.argv) == 1: | |
comm = MPI.COMM_SELF.Spawn(sys.executable, | |
args=sys.argv + ['worker'], | |
maxprocs=1).Merge() | |
else: | |
comm = MPI.Comm.Get_parent().Merge() | |
process_rank = comm.Get_rank() | |
if process_rank == 0: | |
logging.info('Master start.') | |
worker_rank = 1 | |
while True: | |
duration = int(raw_input( | |
'How long should the worker work? (0 to quit.)\n')) | |
comm.send(duration, dest=worker_rank) | |
if duration == 0: | |
break | |
result = polling_receive(comm, source=worker_rank) | |
logging.info('Master received %d', result) | |
logging.info('Master done.') | |
else: | |
logging.info('Worker start.') | |
master_rank = 0 | |
while True: | |
duration = polling_receive(comm, source=master_rank) | |
if duration == 0: | |
break | |
logging.info('Worker working for %ds.', duration) | |
time.sleep(duration) | |
comm.send(duration * duration, dest=master_rank) | |
logging.info('Worker done.') | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment