Skip to content

Commit 4b9be8d

Browse files
committed
Adding optional mpirun command to pass to python interface.
Passing a string 'mpirun' to python interface functions 'simulat' and 'optimize'. Defaults to "mpirun -np ". Also distributing the number of cores over initial conditions and also over Petsc.
1 parent 6beb994 commit 4b9be8d

File tree

1 file changed

+20
-15
lines changed

1 file changed

+20
-15
lines changed

quandary.py

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,7 @@ def update(self):
280280
self.uT = uT_org.copy()
281281

282282

283-
def simulate(self, *, pcof0=[], pt0=[], qt0=[], maxcores=-1, datadir="./run_dir", quandary_exec="", cygwinbash="", batchargs=[]):
283+
def simulate(self, *, pcof0=[], pt0=[], qt0=[], maxcores=-1, datadir="./run_dir", quandary_exec="", cygwinbash="", mpirun="mpirun -np ", batchargs=[]):
284284
"""
285285
Simulate the quantm dynamics using the current settings.
286286
@@ -308,10 +308,10 @@ def simulate(self, *, pcof0=[], pt0=[], qt0=[], maxcores=-1, datadir="./run_dir"
308308
if len(pt0) > 0 and len(qt0) > 0:
309309
pcof0 = self.downsample_pulses(pt0=pt0, qt0=qt0)
310310

311-
return self.__run(pcof0=pcof0, runtype="simulation", overwrite_popt=False, maxcores=maxcores, datadir=datadir, quandary_exec=quandary_exec, cygwinbash=cygwinbash, batchargs=batchargs)
311+
return self.__run(pcof0=pcof0, runtype="simulation", overwrite_popt=False, maxcores=maxcores, datadir=datadir, quandary_exec=quandary_exec, cygwinbash=cygwinbash,mpirun=mpirun, batchargs=batchargs)
312312

313313

314-
def optimize(self, *, pcof0=[], pt0=[], qt0=[], maxcores=-1, datadir="./run_dir", quandary_exec="", cygwinbash="", batchargs=[]):
314+
def optimize(self, *, pcof0=[], pt0=[], qt0=[], maxcores=-1, datadir="./run_dir", quandary_exec="", cygwinbash="", mpirun="mpirun -np ", batchargs=[]):
315315
"""
316316
Optimize the quantm dynamics using the current settings.
317317
@@ -337,10 +337,10 @@ def optimize(self, *, pcof0=[], pt0=[], qt0=[], maxcores=-1, datadir="./run_dir"
337337
if len(pt0) > 0 and len(qt0) > 0:
338338
pcof0 = self.downsample_pulses(pt0=pt0, qt0=qt0)
339339

340-
return self.__run(pcof0=pcof0, runtype="optimization", overwrite_popt=True, maxcores=maxcores, datadir=datadir, quandary_exec=quandary_exec, cygwinbash=cygwinbash, batchargs=batchargs)
340+
return self.__run(pcof0=pcof0, runtype="optimization", overwrite_popt=True, maxcores=maxcores, datadir=datadir, quandary_exec=quandary_exec, cygwinbash=cygwinbash, mpirun=mpirun, batchargs=batchargs)
341341

342342

343-
def evalControls(self, *, pcof0=[], points_per_ns=1,datadir="./run_dir", quandary_exec="", cygwinbash=""):
343+
def evalControls(self, *, pcof0=[], points_per_ns=1,datadir="./run_dir", quandary_exec="", mpirun="mpirun -np ", cygwinbash=""):
344344
"""
345345
Evaluate control pulses on a specific sample rate.
346346
@@ -367,7 +367,7 @@ def evalControls(self, *, pcof0=[], points_per_ns=1,datadir="./run_dir", quandar
367367
os.makedirs(datadir_controls, exist_ok=True)
368368
runtype = 'evalcontrols'
369369
configfile_eval= self.__dump(pcof0=pcof0, runtype=runtype, datadir=datadir_controls)
370-
err = execute(runtype=runtype, ncores=1, config_filename=configfile_eval, datadir=datadir_controls, quandary_exec=quandary_exec, verbose=False, cygwinbash=cygwinbash)
370+
err = execute(runtype=runtype, ncores=1, config_filename=configfile_eval, datadir=datadir_controls, quandary_exec=quandary_exec, verbose=False, mpirun=mpirun, cygwinbash=cygwinbash)
371371
time, pt, qt, _, _, _, pcof, _, _ = self.get_results(datadir=datadir_controls, ignore_failure=True)
372372

373373
# Save pcof to config.popt
@@ -428,7 +428,7 @@ def downsample_pulses(self, *, pt0=[], qt0=[]):
428428
return pcof0
429429

430430

431-
def __run(self, *, pcof0=[], runtype="optimization", overwrite_popt=False, maxcores=-1, datadir="./run_dir", quandary_exec="", cygwinbash="", batchargs=[]):
431+
def __run(self, *, pcof0=[], runtype="optimization", overwrite_popt=False, maxcores=-1, datadir="./run_dir", quandary_exec="", cygwinbash="", mpirun="mpirun -np ", batchargs=[]):
432432
"""
433433
Internal helper function to launch processes to execute the C++ Quandary code:
434434
1. Writes quandary config files to file system
@@ -442,17 +442,22 @@ def __run(self, *, pcof0=[], runtype="optimization", overwrite_popt=False, maxco
442442
config_filename = self.__dump(pcof0=pcof0, runtype=runtype, datadir=datadir)
443443

444444
# Set default number of cores to the number of initial conditions, unless otherwise specified. Make sure ncores is an integer divisible of ninit.
445-
ncores = self._ninit
445+
ncores_init = self._ninit
446446
if maxcores > -1:
447-
ncores = min(self._ninit, maxcores)
447+
ncores_init = min(self._ninit, maxcores)
448448
for i in range(self._ninit, 0, -1):
449449
if self._ninit % i == 0: # i is a factor of ninit
450-
if i <= ncores:
451-
ncores = i
450+
if i <= ncores_init:
451+
ncores_init = i
452452
break
453+
# Set remaining number of cores for petsc
454+
ncores_petsc = 1
455+
if maxcores > ncores_init and maxcores % ncores_init == 0:
456+
ncores_petsc = int(maxcores / ncores_init)
457+
ncores = ncores_init * ncores_petsc
453458

454459
# Execute subprocess to run Quandary
455-
err = execute(runtype=runtype, ncores=ncores, config_filename=config_filename, datadir=datadir, quandary_exec=quandary_exec, verbose=self.verbose, cygwinbash=cygwinbash, batchargs=batchargs)
460+
err = execute(runtype=runtype, ncores=ncores, config_filename=config_filename, datadir=datadir, quandary_exec=quandary_exec, verbose=self.verbose, cygwinbash=cygwinbash, mpirun=mpirun, batchargs=batchargs)
456461
if self.verbose:
457462
print("Quandary data dir: ", datadir, "\n")
458463

@@ -1332,7 +1337,7 @@ def timestep_richardson_est(quandary, tol=1e-8, order=2, quandary_exec=""):
13321337
return errs_J, errs_u, dts
13331338

13341339

1335-
def execute(*, runtype="simulation", ncores=1, config_filename="config.cfg", datadir=".", quandary_exec="", verbose=False, cygwinbash="", batchargs=[]):
1340+
def execute(*, runtype="simulation", ncores=1, config_filename="config.cfg", datadir=".", quandary_exec="", verbose=False, cygwinbash="", mpirun="mpirun -np ", batchargs=[]):
13361341
"""
13371342
Helper function to evoke a subprocess that executes Quandary.
13381343
@@ -1367,8 +1372,8 @@ def execute(*, runtype="simulation", ncores=1, config_filename="config.cfg", dat
13671372
if len(batchargs)>0:
13681373
myrun = batch_run # currently set to "srun -n"
13691374
else:
1370-
myrun = "mpirun -np "
1371-
runcommand = f"{myrun} {ncores} " + runcommand
1375+
myrun = mpirun
1376+
runcommand = f"{myrun}{ncores} " + runcommand
13721377
if verbose:
13731378
print("Running Quandary ... ")
13741379

0 commit comments

Comments
 (0)