diff --git a/micro_manager/adaptivity/global_adaptivity.py b/micro_manager/adaptivity/global_adaptivity.py index 6164efdf..e0bcf70b 100644 --- a/micro_manager/adaptivity/global_adaptivity.py +++ b/micro_manager/adaptivity/global_adaptivity.py @@ -18,8 +18,7 @@ def __init__( self, configurator, logger, - is_sim_on_this_rank: list, - rank_of_sim: np.ndarray, + global_number_of_sims: float, global_ids: list, rank: int, comm) -> None: @@ -32,10 +31,8 @@ def __init__( Object which has getter functions to get parameters defined in the configuration file. logger : object of logging Logger defined from the standard package logging - is_sim_on_this_rank : list + global_number_of_sims : float List of booleans. True if simulation is on this rank, False otherwise. - rank_of_sim : numpy array - 1D array consisting of rank on which the simulation lives. global_ids : list List of global IDs of simulations living on this rank. rank : int @@ -44,12 +41,25 @@ def __init__( Global communicator of MPI. """ super().__init__(configurator, logger) - self._is_sim_on_this_rank = is_sim_on_this_rank - self._rank_of_sim = rank_of_sim self._global_ids = global_ids self._comm = comm self._rank = rank + local_number_of_sims = len(global_ids) + + # Create a map of micro simulation global IDs and the ranks on which they are + micro_sims_on_this_rank = np.zeros(local_number_of_sims, dtype=np.intc) + for i in range(local_number_of_sims): + micro_sims_on_this_rank[i] = self._rank + + self._rank_of_sim = np.zeros(global_number_of_sims, dtype=np.intc) # DECLARATION + self._comm.Allgather(micro_sims_on_this_rank, self._rank_of_sim) + + self._is_sim_on_this_rank = [False] * global_number_of_sims # DECLARATION + for i in range(global_number_of_sims): + if self._rank_of_sim[i] == self._rank: + self._is_sim_on_this_rank[i] = True + def compute_adaptivity( self, dt: float, diff --git a/micro_manager/micro_manager.py b/micro_manager/micro_manager.py index b2e6c0b6..fa71e579 100644 --- a/micro_manager/micro_manager.py +++ b/micro_manager/micro_manager.py @@ -314,25 +314,15 @@ def _initialize(self) -> None: fromlist=["MicroSimulation"]), "MicroSimulation") - if self._is_adaptivity_on: - # Create micro simulation objects - for i in range(self._local_number_of_sims): - self._micro_sims[i] = create_simulation_class( - micro_problem)(self._global_ids_of_local_sims[i]) - - # Create a map of micro simulation global IDs and the ranks on which they are - micro_sims_on_this_rank = np.zeros(self._local_number_of_sims, dtype=np.intc) - for i in range(self._local_number_of_sims): - micro_sims_on_this_rank[i] = self._rank - - self._rank_of_sim = np.zeros(self._global_number_of_sims, dtype=np.intc) # DECLARATION - self._comm.Allgather(micro_sims_on_this_rank, self._rank_of_sim) + # Create micro simulation objects + for i in range(self._local_number_of_sims): + self._micro_sims[i] = create_simulation_class( + micro_problem)(self._global_ids_of_local_sims[i]) - self._is_sim_on_this_rank = [False] * self._global_number_of_sims # DECLARATION - for i in range(self._global_number_of_sims): - if self._rank_of_sim[i] == self._rank: - self._is_sim_on_this_rank[i] = True + self._logger.info("Micro simulations with global IDs {} - {} created.".format( + self._global_ids_of_local_sims[0], self._global_ids_of_local_sims[-1])) + if self._is_adaptivity_on: if self._adaptivity_type == "local": self._adaptivity_controller = LocalAdaptivityCalculator( self._config, self._logger) @@ -341,21 +331,13 @@ def _initialize(self) -> None: self._adaptivity_controller = GlobalAdaptivityCalculator( self._config, self._logger, - self._is_sim_on_this_rank, - self._rank_of_sim, + self._global_number_of_sims, self._global_ids_of_local_sims, self._rank, self._comm) self._number_of_sims_for_adaptivity = self._global_number_of_sims self._micro_sims_active_steps = np.zeros(self._local_number_of_sims) - else: - for i in range(self._local_number_of_sims): - self._micro_sims[i] = ( - create_simulation_class(micro_problem)(self._global_ids_of_local_sims[i])) - - self._logger.info("Micro simulations with global IDs {} - {} created.".format( - self._global_ids_of_local_sims[0], self._global_ids_of_local_sims[-1])) self._micro_sims_have_output = False if hasattr(micro_problem, 'output') and callable(getattr(micro_problem, 'output')): diff --git a/tests/unit/test_adaptivity_parallel.py b/tests/unit/test_adaptivity_parallel.py index f237feb8..0d2ec629 100644 --- a/tests/unit/test_adaptivity_parallel.py +++ b/tests/unit/test_adaptivity_parallel.py @@ -98,7 +98,6 @@ def test_communicate_micro_output(self): expected_sim_output = [output_1, output_0] is_sim_active = np.array([False, False, True, True, False]) - rank_of_sim = [0, 0, 0, 1, 1] sim_is_associated_to = [3, 3, -2, -2, 2] configurator = MagicMock() @@ -106,12 +105,14 @@ def test_communicate_micro_output(self): adaptivity_controller = GlobalAdaptivityCalculator( configurator, MagicMock(), - is_sim_on_this_rank, - rank_of_sim, + 5, global_ids, rank=self._rank, comm=self._comm) + adaptivity_controller._rank_of_sim = [0, 0, 0, 1, 1] + adaptivity_controller._is_sim_on_this_rank = is_sim_on_this_rank + adaptivity_controller.communicate_micro_output(is_sim_active, sim_is_associated_to, sim_output) self.assertTrue(np.array_equal(expected_sim_output, sim_output))