diff --git a/pyoptsparse/pyOpt_optimization.py b/pyoptsparse/pyOpt_optimization.py index e0bd752f..c285b194 100644 --- a/pyoptsparse/pyOpt_optimization.py +++ b/pyoptsparse/pyOpt_optimization.py @@ -542,16 +542,25 @@ def setDVsFromHistory(self, histFile: str, key: str | None = None) -> None: default is None which will use the last x-value stored in the dictionary. """ - - if os.path.exists(histFile): - hist = SqliteDict(histFile) - if key is None: - key = hist["last"] - - self.setDVs(hist[key]["xuser"]) - hist.close() - else: - raise FileNotFoundError(f"History file '{histFile}' not found!.") + xuser = None + exc: Exception | None = None + if self.comm.rank == 0: + try: + if not os.path.exists(histFile): + raise FileNotFoundError(f"History file '{histFile}' not found!.") + hist = SqliteDict(histFile) + try: + if key is None: + key = hist["last"] + xuser = hist[key]["xuser"] + finally: + hist.close() + except Exception as e: # re-raised on all processors after the bcast + exc = e + exc, xuser = self.comm.bcast((exc, xuser), root=0) + if exc is not None: + raise exc + self.setDVs(xuser) def printSparsity(self, verticalPrint: bool = False) -> None: """