From c4d7a786ad974ff0dcbe31ce22c06192031dd64d Mon Sep 17 00:00:00 2001 From: Teytaud Date: Wed, 19 Jan 2022 17:22:04 +0100 Subject: [PATCH 1/5] The user can modify NG_CPUS_PER_TASK The environment variable NG_CPUS_PER_TASK modifies the number of CPUs to be used per task. --- nevergrad/benchmark/xpbase.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/nevergrad/benchmark/xpbase.py b/nevergrad/benchmark/xpbase.py index 668d75fbd..694abefb2 100644 --- a/nevergrad/benchmark/xpbase.py +++ b/nevergrad/benchmark/xpbase.py @@ -7,10 +7,12 @@ import time import random import numbers +import os import warnings import traceback import typing as tp import numpy as np +from concurrent import futures from nevergrad.parametrization import parameter as p from nevergrad.common import decorators from nevergrad.common import errors @@ -57,7 +59,11 @@ def __init__( self.optimizer = optimizer self.budget = budget self.num_workers = num_workers - self.executor = execution.MockedTimedExecutor(batch_mode) + ng_cpus_per_task = int(os.getenv("NG_CPUS_PER_TASK", "1")) + if ng_cpus_per_task > 0: + self.executor = futures.ThreadPoolExecutor(max_workers=ng_cpus_per_task) + else: + self.executor = execution.MockedTimedExecutor(batch_mode) @property def name(self) -> str: From 276842cfd3a638fc3873173953d3d228c2e10daa Mon Sep 17 00:00:00 2001 From: Teytaud Date: Wed, 19 Jan 2022 17:28:10 +0100 Subject: [PATCH 2/5] Update xpbase.py --- nevergrad/benchmark/xpbase.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/nevergrad/benchmark/xpbase.py b/nevergrad/benchmark/xpbase.py index 694abefb2..b3ee2ff28 100644 --- a/nevergrad/benchmark/xpbase.py +++ b/nevergrad/benchmark/xpbase.py @@ -62,6 +62,8 @@ def __init__( ng_cpus_per_task = int(os.getenv("NG_CPUS_PER_TASK", "1")) if ng_cpus_per_task > 0: self.executor = futures.ThreadPoolExecutor(max_workers=ng_cpus_per_task) + assert not batch_mode + self.executor.batch_mode = False else: self.executor = execution.MockedTimedExecutor(batch_mode) From b54085a1f1c6f4d6ea82d5a7d2e1a26bc75fbb35 Mon Sep 17 00:00:00 2001 From: Teytaud Date: Wed, 19 Jan 2022 17:53:35 +0100 Subject: [PATCH 3/5] Update xpbase.py --- nevergrad/benchmark/xpbase.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/nevergrad/benchmark/xpbase.py b/nevergrad/benchmark/xpbase.py index b3ee2ff28..51d1019c8 100644 --- a/nevergrad/benchmark/xpbase.py +++ b/nevergrad/benchmark/xpbase.py @@ -60,7 +60,7 @@ def __init__( self.budget = budget self.num_workers = num_workers ng_cpus_per_task = int(os.getenv("NG_CPUS_PER_TASK", "1")) - if ng_cpus_per_task > 0: + if ng_cpus_per_task > 1: self.executor = futures.ThreadPoolExecutor(max_workers=ng_cpus_per_task) assert not batch_mode self.executor.batch_mode = False From 2ca61e5f2b08594592064b007bc728f63f0f1a0b Mon Sep 17 00:00:00 2001 From: Olivier Teytaud Date: Thu, 20 Jan 2022 07:17:36 +0100 Subject: [PATCH 4/5] fix --- nevergrad/benchmark/xpbase.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/nevergrad/benchmark/xpbase.py b/nevergrad/benchmark/xpbase.py index 51d1019c8..d88cb4e86 100644 --- a/nevergrad/benchmark/xpbase.py +++ b/nevergrad/benchmark/xpbase.py @@ -61,9 +61,8 @@ def __init__( self.num_workers = num_workers ng_cpus_per_task = int(os.getenv("NG_CPUS_PER_TASK", "1")) if ng_cpus_per_task > 1: - self.executor = futures.ThreadPoolExecutor(max_workers=ng_cpus_per_task) + self.executor = futures.ThreadPoolExecutor(max_workers=ng_cpus_per_task) # type: ignore assert not batch_mode - self.executor.batch_mode = False else: self.executor = execution.MockedTimedExecutor(batch_mode) @@ -73,7 +72,7 @@ def name(self) -> str: @property def batch_mode(self) -> bool: - return self.executor.batch_mode + return hasattr(self.executor, "batch_mode") and self.executor.batch_mode def __repr__(self) -> str: return f"Experiment: {self.name}" @@ -215,7 +214,9 @@ def run(self) -> tp.Dict[str, tp.Any]: def _log_results(self, pfunc: fbase.ExperimentFunction, t0: float, num_calls: int) -> None: """Internal method for logging results before handling the error""" self.result["elapsed_time"] = time.time() - t0 - self.result["pseudotime"] = self.optimsettings.executor.time + self.result["pseudotime"] = ( + self.optimsettings.executor.time if hasattr(self.optimsettings.executor, "time") else float("nan") + ) # make a final evaluation with oracle (no noise, but function may still be stochastic) opt = self._optimizer assert opt is not None From d03868b1f2d8cfc25d1813603cbb15be7ce4376d Mon Sep 17 00:00:00 2001 From: Olivier Teytaud Date: Thu, 20 Jan 2022 08:05:43 +0100 Subject: [PATCH 5/5] fix --- nevergrad/benchmark/xpbase.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/nevergrad/benchmark/xpbase.py b/nevergrad/benchmark/xpbase.py index d88cb4e86..ef8d1c4f0 100644 --- a/nevergrad/benchmark/xpbase.py +++ b/nevergrad/benchmark/xpbase.py @@ -61,7 +61,7 @@ def __init__( self.num_workers = num_workers ng_cpus_per_task = int(os.getenv("NG_CPUS_PER_TASK", "1")) if ng_cpus_per_task > 1: - self.executor = futures.ThreadPoolExecutor(max_workers=ng_cpus_per_task) # type: ignore + self.executor: tp.Any = futures.ThreadPoolExecutor(max_workers=ng_cpus_per_task) assert not batch_mode else: self.executor = execution.MockedTimedExecutor(batch_mode)