Skip to content

Commit

Permalink
Move adaptivity metrics logging into the respective adaptivity classes
Browse files Browse the repository at this point in the history
  • Loading branch information
IshaanDesai committed Dec 16, 2024
1 parent e44c7e2 commit 4e95270
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 30 deletions.
16 changes: 16 additions & 0 deletions micro_manager/adaptivity/global_adaptivity.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,22 @@ def get_full_field_micro_output(

return micro_sims_output

def log_metrics(self, logger, adaptivity_data: list, n: int) -> None:
""" """
is_sim_active = adaptivity_data[1]
global_active_sims = np.count_nonzero(is_sim_active)
global_inactive_sims = np.count_nonzero(is_sim_active == False)

logger.log_info_one_rank(
"{},{},{},{},{}".format(
n,
np.mean(global_active_sims),
np.mean(global_inactive_sims),
np.max(global_active_sims),
np.max(global_inactive_sims),
)
)

def _communicate_micro_output(
self,
adaptivity_data: list,
Expand Down
28 changes: 25 additions & 3 deletions micro_manager/adaptivity/local_adaptivity.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,19 @@


class LocalAdaptivityCalculator(AdaptivityCalculator):
def __init__(self, configurator) -> None:
def __init__(self, configurator, comm) -> None:
"""
Class constructor.
Parameters
----------
configurator : object of class Config
Object which has getter functions to get parameters defined in the configuration file.
logger : object of logging
Logger defined from the standard package logging
comm : MPI.COMM_WORLD
Global communicator of MPI.
"""
super().__init__(configurator)
self._comm = comm

def compute_adaptivity(
self,
Expand Down Expand Up @@ -143,6 +144,27 @@ def get_full_field_micro_output(

return micro_sims_output

def log_metrics(self, logger, adaptivity_list: list, n: int) -> None:
""" """
is_sim_active = adaptivity_list[1]

# MPI Gather is necessary as local adaptivity only stores local data
local_active_sims = np.count_nonzero(is_sim_active)
global_active_sims = self._comm.gather(local_active_sims)

local_inactive_sims = np.count_nonzero(is_sim_active == False)
global_inactive_sims = self._comm.gather(local_inactive_sims)

logger.log_info_one_rank(
"{},{},{},{},{}".format(
n,
np.mean(global_active_sims),
np.mean(global_inactive_sims),
np.max(global_active_sims),
np.max(global_inactive_sims),
)
)

def _update_inactive_sims(
self,
similarity_dists: np.ndarray,
Expand Down
2 changes: 1 addition & 1 deletion micro_manager/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def __init__(self, config_file_name):
self._adaptivity_refining_constant = 0.5
self._adaptivity_every_implicit_iteration = False
self._adaptivity_similarity_measure = "L1"
self._adaptivity_output_n = 1
self._adaptivity_output_n = -2
self._adaptivity_output_cpu_time = False
self._adaptivity_output_mem_usage = False

Expand Down
39 changes: 13 additions & 26 deletions micro_manager/micro_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,6 @@ def __init__(self, config_file: str) -> None:

self._adaptivity_data_names = self._config.get_data_for_adaptivity()

self._adaptivity_output_n = self._config.get_adaptivity_output_n()

# Names of macro data to be used for adaptivity computation
self._adaptivity_macro_data_names = dict()

Expand All @@ -138,7 +136,9 @@ def __init__(self, config_file: str) -> None:
self._config.is_adaptivity_required_in_every_implicit_iteration()
)
self._micro_sims_active_steps = None
self._output_adaptivity_cpu_time = self._config.output_adaptivity_cpu_time()

self._adaptivity_output_n = self._config.get_adaptivity_output_n()
self._output_adaptivity_cpu_time = self._config.output_adaptivity_cpu_time()

# Define the preCICE Participant
self._participant = precice.Participant(
Expand Down Expand Up @@ -306,28 +306,15 @@ def solve(self) -> None:
for sim in self._micro_sims:
sim.output()

if self._is_adaptivity_on:
if self._adaptivity_type == "local":
# MPI Gather is necessary as local adaptivity only stores local data
local_active_sims = np.count_nonzero(is_sim_active)
global_active_sims = self._comm.gather(local_active_sims)

local_inactive_sims = np.count_nonzero(is_sim_active == False)
global_inactive_sims = self._comm.gather(local_inactive_sims)
elif self._adaptivity_type == "global":
global_active_sims = np.count_nonzero(is_sim_active)
global_inactive_sims = np.count_nonzero(is_sim_active == False)

if n % self._adaptivity_output_n == 0 and self._rank == 0:
self._adaptivity_logger.log_info_one_rank(
"{},{},{},{},{}".format(
n,
np.mean(global_active_sims),
np.mean(global_inactive_sims),
np.max(global_active_sims),
np.max(global_inactive_sims),
)
)
if (
self._is_adaptivity_on
and n % self._adaptivity_output_n == 0
and self._rank == 0
):
self._adaptivity_controller.log_metrics(
self._adaptivity_logger, adaptivity_data, n
)

self._logger.log_info_one_rank("Time window {} converged.".format(n))

self._participant.finalize()
Expand Down Expand Up @@ -466,7 +453,7 @@ def initialize(self) -> None:
if self._is_adaptivity_on:
if self._adaptivity_type == "local":
self._adaptivity_controller: LocalAdaptivityCalculator = (
LocalAdaptivityCalculator(self._config)
LocalAdaptivityCalculator(self._config, self._comm)
)
self._number_of_sims_for_adaptivity = self._local_number_of_sims
elif self._adaptivity_type == "global":
Expand Down

0 comments on commit 4e95270

Please sign in to comment.