Skip to content

Commit a88c990

Browse files
authored
Add scan_split option to mcrun
Adds scan_split option to mcrun, to optimise scans made up of many short runs. Uses mpi to multipool each process, instead of the big process, such that IO bound processes can be parallelized instead.
1 parent 5be8fda commit a88c990

1 file changed

Lines changed: 25 additions & 9 deletions

File tree

tools/Python/mcrun/mcrun.py

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@
1111
from optparse import OptionParser, OptionGroup, OptionValueError
1212
from decimal import Decimal, InvalidOperation
1313
from datetime import datetime
14-
14+
import multiprocessing
1515
from mccode import McStas, Process
16-
from optimisation import Scanner, LinearInterval, MultiInterval, Optimizer
16+
from optimisation import Scanner, Scanner_split, LinearInterval, MultiInterval, Optimizer
1717

1818
# import config
1919
import sys
@@ -224,6 +224,11 @@ def add_mcrun_options(parser):
224224
default="",
225225
)
226226

227+
add("--scan_split",
228+
type=int,
229+
metavar="scan_split",
230+
help='Scanning over a variable is split onto individual cpu threads')
231+
227232
# --optimize-maxiter maxiter max iter of optimization
228233
# --tol tol tolerance criteria to end the optimization
229234
# --method method Method to maximize the intensity in ['nelder-mead', 'powell', 'cg', 'bfgs', 'newton-cg', 'l-bfgs-b', 'tnc', 'cobyla', 'slsqp', 'trust-constr', 'dogleg', 'trust-ncg', 'trust-exact', 'trust-krylov']
@@ -259,15 +264,11 @@ def add_mcstas_options(parser):
259264

260265
add('-n', '--ncount',
261266
metavar='COUNT', type=float, default=1000000,
262-
help='Set number of %ss to simulate' % (mccode_config.configuration["PARTICLE"]))
267+
help='Set number of %s to simulate' % (mccode_config.configuration["PARTICLE"]))
263268

264269
add('-t', '--trace',
265270
metavar='trace', type=int, default=0,
266-
help='Enable trace of %ss through instrument' % (mccode_config.configuration["PARTICLE"]))
267-
268-
add('--no-trace',
269-
action='store_true', metavar='notrace', default=None,
270-
help='Disable trace of %ss in instrument (combine with -c)' % (mccode_config.configuration["PARTICLE"]))
271+
help='Enable trace of %s through instrument' % (mccode_config.configuration["PARTICLE"]))
271272

272273
add('-y', '--yes',
273274
action='store_true', default=False,
@@ -574,13 +575,28 @@ def main():
574575
elif options.numpoints is not None:
575576
interval_points = LinearInterval.from_range(options.numpoints, intervals)
576577

578+
579+
# Check that mpi and scan split are not both used. Default to mpi if they are
580+
if options.scan_split is not None and options.mpi is not None:
581+
options.scan_split = None
582+
577583
# Parameters for linear scanning present
578-
if interval_points:
584+
if interval_points and (options.scan_split is None):
579585
scanner = Scanner(mcstas, intervals)
580586
scanner.set_points(interval_points)
581587
if (not options.dir == ''):
582588
mkdir(options.dir)
583589
scanner.run() # in optimisation.py
590+
591+
elif options.scan_split is not None:
592+
if options.scan_split == 0:
593+
options.scan_split = multiprocessing.cpu_count()-1
594+
split_scanner = Scanner_split(mcstas, intervals, options.scan_split)
595+
split_scanner.set_points(interval_points)
596+
if (not options.dir == ''):
597+
mkdir(options.dir)
598+
split_scanner.run() # in optimisation.py
599+
584600
elif options.optimize:
585601
optimizer = Optimizer(mcstas, intervals)
586602
if (not options.dir == ''):

0 commit comments

Comments
 (0)