Skip to content

ENH: Add mp_context plugin arg for MultiProc #2778

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 15, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 14 additions & 6 deletions nipype/pipeline/plugins/multiproc.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

# Import packages
import os
from multiprocessing import cpu_count
import multiprocessing as mp
from concurrent.futures import ProcessPoolExecutor
from traceback import format_exception
import sys
Expand Down Expand Up @@ -95,7 +95,7 @@ class MultiProcPlugin(DistributedPluginBase):

Currently supported options are:

- non_daemon : boolean flag to execute as non-daemon processes
- non_daemon: boolean flag to execute as non-daemon processes
- n_procs: maximum number of threads to be executed in parallel
- memory_gb: maximum memory (in GB) that can be used at once.
- raise_insufficient: raise error if the requested resources for
Expand All @@ -104,8 +104,7 @@ class MultiProcPlugin(DistributedPluginBase):
- scheduler: sort jobs topologically (``'tsort'``, default value)
or prioritize jobs by, first, memory consumption and, second,
number of threads (``'mem_thread'`` option).
- maxtasksperchild: number of nodes to run on each process before
refreshing the worker (default: 10).
- mp_context: name of multiprocessing context to use

"""

Expand All @@ -121,7 +120,7 @@ def __init__(self, plugin_args=None):
self._cwd = os.getcwd()

# Read in options or set defaults.
self.processors = self.plugin_args.get('n_procs', cpu_count())
self.processors = self.plugin_args.get('n_procs', mp.cpu_count())
self.memory_gb = self.plugin_args.get(
'memory_gb', # Allocate 90% of system memory
get_system_total_memory_gb() * 0.9)
Expand All @@ -133,7 +132,16 @@ def __init__(self, plugin_args=None):
'mem_gb=%0.2f, cwd=%s)',
self.processors, self.memory_gb, self._cwd)

self.pool = ProcessPoolExecutor(max_workers=self.processors)
try:
mp_context = mp.context.get_context(
self.plugin_args.get('mp_context'))
self.pool = ProcessPoolExecutor(max_workers=self.processors,
initializer=os.chdir,
initargs=(self._cwd,),
mp_context=mp_context)
except (AttributeError, TypeError):
# Python < 3.7 does not support initialization or contexts
self.pool = ProcessPoolExecutor(max_workers=self.processors)

self._stats = None

Expand Down