Skip to content

Instantly share code, notes, and snippets.

@lebedov
Created March 4, 2015 15:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lebedov/7ad4cfa6fa4b3fc62137 to your computer and use it in GitHub Desktop.
Save lebedov/7ad4cfa6fa4b3fc62137 to your computer and use it in GitHub Desktop.
How to use twiggy for logging in both parent and MPI-spawned processes.
#!/usr/bin/bin python
"""
How to use twiggy for logging in both parent and MPI-spawned processes.
"""
import sys
# Need to use dill to serialize the twiggy emitters because pickle can't handle
# them:
import dill
from mpi4py import MPI
MPI.pickle.dumps = dill.dumps
MPI.pickle.loads = dill.loads
import twiggy
rank = MPI.COMM_WORLD.Get_rank()
intercomm = MPI.Comm.Get_parent()
if intercomm == MPI.COMM_NULL:
output = twiggy.outputs.StreamOutput(twiggy.formats.line_format,
stream=sys.stdout)
emitters = twiggy.filters.Emitter(twiggy.levels.DEBUG,
True, output)
twiggy.emitters['*'] = emitters
logger = twiggy.log.name('parent')
logger.info('in parent')
maxprocs = 2
comm = MPI.COMM_SELF.Spawn(sys.executable,
args=[__file__],
maxprocs=maxprocs)
for i in xrange(maxprocs):
comm.send(twiggy.emitters, i)
else:
emitters = intercomm.recv()
# The transmitted emitters must be explicitly associated with their keys in
# the child twiggy.emitters dict:
twiggy.emitters.update(emitters)
logger = twiggy.log.name('child %s' % rank)
logger.info('in child')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment