Skip to content

Commit

Permalink
Moving functionality relevant to global adaptivity into its own const…
Browse files Browse the repository at this point in the history
…ructor rather than initialize()
  • Loading branch information
IshaanDesai committed Jan 31, 2024
1 parent d25fddc commit cfca0ae
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 36 deletions.
24 changes: 17 additions & 7 deletions micro_manager/adaptivity/global_adaptivity.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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
Expand All @@ -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,
Expand Down
34 changes: 8 additions & 26 deletions micro_manager/micro_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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')):
Expand Down
7 changes: 4 additions & 3 deletions tests/unit/test_adaptivity_parallel.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,20 +98,21 @@ 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()
configurator.get_adaptivity_similarity_measure = MagicMock(return_value="L1")
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))

0 comments on commit cfca0ae

Please sign in to comment.