1 #ifndef LOAD_BALANCE_HPP
2 #define LOAD_BALANCE_HPP
14 extern "C" void set_weights(
int* gvid0_pid,
double* ptl_count,
double* f0_node_cost);
37 MPI_Comm inter_period_comm;
48 void update_model_no_history(
const View<int*,CLayout,HostType>& current_partition,
const View<double*, HostType>& all_periods_timings){
50 for(
int i=0; i<all_periods_timings.size(); i++){
51 int node_offset = current_partition(i) - 1;
52 int next_node_offset = current_partition(i+1) - 1;
53 int nnodes = next_node_offset - node_offset;
55 double time_per_node = all_periods_timings(i)/nnodes;
56 for(
int j=0; j<nnodes; j++){
62 void update_model_exp_history(
const View<int*,CLayout,HostType>& current_partition,
const View<double*, HostType>& all_periods_timings){
64 for(
int i=0; i<all_periods_timings.size(); i++){
66 int node_offset = current_partition(i) - 1;
67 int next_node_offset = current_partition(i+1) - 1;
68 int nnodes = next_node_offset - node_offset;
71 double avg_time_per_node = all_periods_timings(i)/nnodes;
74 double expected_time = 0.0;
75 for(
int j=0; j<nnodes; j++){
78 double expected_time_per_node = expected_time/nnodes;
81 double extra_time_per_node = avg_time_per_node - expected_time_per_node;
83 for(
int j=0; j<nnodes; j++){
85 constexpr
double adjustment_rate = 0.5;
94 double new_accumulated_time = 0.0;
98 double accumulated_single_timer = 0;
99 int ierr = GPTLget_wallclock(
timer_names[i].c_str(), -1, &accumulated_single_timer);
102 if(ierr != 0) accumulated_single_timer = 0.0;
104 new_accumulated_time += accumulated_single_timer;
130 inter_period_comm(inter_period_comm),
131 period_comm(period_comm),
148 MPI_Comm_size(inter_period_comm, &
n_periods);
183 double largest_time = 0.0;
185 for(
int i=0; i<(proposed_partition.size()-1); i++){
186 double proc_time = 0.0;
187 for(
int i_node=proposed_partition(i)-1; i_node<proposed_partition(i+1)-1; i_node++){
190 if(proc_time>largest_time){
192 largest_time = proc_time;
205 void update_model(
const View<int*,CLayout,HostType>& current_partition,
double manual_time=-1.0){
221 MPI_Reduce(&time_spent_this_rank, &time_spent, 1, MPI_DOUBLE, MPI_MAX, 0, inter_period_comm);
223 time_spent = time_spent_this_rank;
227 MPI_Gather(&time_spent, 1, MPI_DOUBLE, all_periods_timings.data(), 1, MPI_DOUBLE, 0, period_comm);
237 double observed_sum_region_time = 0.0;
238 for(
int i=0; i<all_periods_timings.size(); i++){
240 observed_sum_region_time += all_periods_timings(i);
265 exit_XGC(
"Error: Update method not available\n");
309 Kokkos::parallel_reduce(
"sum", Kokkos::RangePolicy<HostExSpace>(0,input.size()), [=](
const int i,
double& l_total){
317 bool greedily_fill_partition(
const View<double*,HostType>& weight,
const View<double*,HostType>& constraint1,
double target_weight_per_rank){
318 int nnode = weight.size();
322 double constraint1_in_this_rank = 0.0;
323 double weight_in_this_rank = 0.0;
326 bool assign_one_node_per_proc =
false;
327 for(
int i_node = 0; i_node<nnode; i_node++){
330 if(nnode-i_node==nproc-pid) assign_one_node_per_proc =
true;
333 bool rank_is_loaded =
false;
336 if(assign_one_node_per_proc) rank_is_loaded =
true;
342 if(weight_in_this_rank>=target_weight_per_rank) rank_is_loaded =
true;
348 constraint1_in_this_rank = 0.0;
349 weight_in_this_rank = 0.0;
351 if(pid==nproc-1)
break;
353 constraint1_in_this_rank += constraint1(i_node);
354 weight_in_this_rank += weight(i_node);
361 constraint1_in_this_rank = 0.0;
363 constraint1_in_this_rank += constraint1(i_node);
375 double largest_time = 0.0;
377 for(
int i=0; i<(partition.size()-1); i++){
378 double proc_time = 0.0;
379 for(
int i_node=partition(i)-1; i_node<partition(i+1)-1; i_node++){
380 proc_time += weight(i_node);
382 if(proc_time>largest_time){
384 largest_time = proc_time;
390 void one_weight_balance(
const View<double*,HostType>& weight,
const View<double*,CLayout,HostType> constraint1){
399 if(!meets_constraints){
403 if(!meets_constraints)
exit_XGC(
"\nUnexpected issue in load balance: constraint-based partition doesn't satisfy constraints\n");
407 if(
verbose) printf(
"The ideal distribution (%1.3e) does not satisfy constraints. This distribution (%1.3e) does.\n", ideal_weight_per_rank, upper_limit_weight_per_rank);
411 const double desired_precision = 0.01;
412 double desired_step_size = ideal_weight_per_rank*desired_precision;
413 double step_size = upper_limit_weight_per_rank - ideal_weight_per_rank;
414 double compromise_weight_per_rank = upper_limit_weight_per_rank;
415 if(
verbose) printf(
"\nEmploying a binary search to narrow down on the best option.");
416 while(step_size>desired_step_size){
420 if(meets_constraints){
422 compromise_weight_per_rank -= step_size;
425 compromise_weight_per_rank += step_size;
430 if(
verbose) printf(
"\n Stepped by %1.3e to %1.3e. The new partition does%s meet constraints.", step_size, compromise_weight_per_rank, meets_constraints?
"" :
"nt");
433 while(!meets_constraints){
434 compromise_weight_per_rank += step_size;
436 if(
verbose) printf(
"\n Stepped by %1.3e UP to %1.3e. The new partition does%s meet constraints.", step_size, compromise_weight_per_rank, meets_constraints?
"" :
"nt");
453 double observed_max_all_rgns_time = 0.0;
454 for(
int i=0; i<
regions.size(); i++){
455 observed_max_all_rgns_time +=
regions[i].get_observed_max_region_time();
459 double predicted_max_all_rgns_time = 0.0;
460 for(
int i=0; i<
regions.size(); i++){
464 double fractional_improvement = 1.0 - predicted_max_all_rgns_time/observed_max_all_rgns_time;
467 printf(
"\nDetermining whether to adopt the proposed partition:");
468 printf(
"\n Observed time with current partition was: %1.3e", observed_max_all_rgns_time);
469 printf(
"\n Predicted time with proposed partition, adjusting for historical undershoot (of Rgn 0: %1.3e): %1.3e",
regions[0].get_prediction_undershoot(), predicted_max_all_rgns_time);
470 printf(
"\n Adopted if Fractional improvement of adjusted prediction (%1.3e) exceeds specified threshold (%1.3e)\n", fractional_improvement,
threshold_to_rebalance);
489 void update_model(
const View<int*,CLayout,HostType>& current_partition){
490 for(
int i=0; i<
regions.size(); i++){
491 regions[i].update_model(current_partition);
496 void update_model(
const View<int*,CLayout,HostType>& current_partition,
const std::vector<double>& manual_times){
497 for(
int i=0; i<
regions.size(); i++){
498 regions[i].update_model(current_partition, manual_times[i]);
503 for(
int i=0; i<
regions.size(); i++){
509 bool is_initialized=
true;
510 for(
int i=0; i<
regions.size(); i++){
511 is_initialized = (is_initialized &&
regions[i].get_model_is_initialized());
513 return is_initialized;
517 auto constraint1 = ptl_count;
519 if(
regions.size()!=1)
exit_XGC(
"Error: Load balancing is currently single-region only\n");
526 printf(
"\nNewly PROPOSED partition:\n");
543 #ifndef NO_FORTRAN_MODULES
558 printf(
"\nNEW PARTITION:\n");
572 const View<int*,CLayout,HostType>& old_partition){
575 TIMER(
"F0_REDISTRIBUTE",
590 #ifndef NO_FORTRAN_MODULES
600 return (should_rebalance_int==1);
606 bool should_rebalance;
616 int should_rebalance_int = (should_rebalance ? 1 : 0);
618 MPI_Bcast(&should_rebalance_int, 1, MPI_INT, 0, comm);
620 return (should_rebalance_int==1);
633 , comm(sync_planes ? pol_decomp.mpi.comm : pol_decomp.mpi.plane_comm)
638 double max_mem_redist_gb = nlr.
get<
double>(
"max_mem_redist_gb", 10.0);
639 std::string weighting_algorithm_str = nlr.
get<std::string>(
"weighting_algorithm",
"Fortran");
642 std::string update_method_str = nlr.
get<std::string>(
"update_method",
"NoHistory");
647 double max_n_ptl_on_rank = max_mem_redist_gb*1024*1024*1024/80.0;
649 if(weighting_algorithm_str==
"Fortran"){
651 }
else if(weighting_algorithm_str==
"ParticleBalance"){
653 }
else if(weighting_algorithm_str==
"SingleRegionBalance"){
656 exit_XGC(
"\nError: weighting_algorithm input not valid.");
661 MPI_Comm inter_period_comm = sync_planes ? pol_decomp.mpi.intpl_comm : MPI_COMM_SELF;
666 if(update_method_str==
"NoHistory"){
668 }
else if(update_method_str==
"ExpHistory"){
671 exit_XGC(
"\nError: update_method input not valid.");
688 MPI_Comm_size(inter_period_comm, &n_periods);
691 exit_XGC(
"\nError: ParticleConstraint is only available ConstraintOption.\n");
729 double avg_n_ptl = (double)(total_n_ptl)/pol_decomp.mpi.nranks;
730 double max_ratio = max_n_ptl/avg_n_ptl;
731 if(
is_rank_zero()) printf(
" Species %d: (max/avg ptl per rank = %1.2f); total n_ptl = %lld\n", species.
idx, max_ratio, total_n_ptl);
742 TIMER(
"LOAD_BAL_UPDATE",
745 if(
is_rank_zero() &&
verbose) printf(
"Initializing timing model, no partition proposal yet.");
760 if(
will_rebalance(reweight_option, weighting_algorithm, istep, gstep, f0_cost)){
762 View<int*,CLayout,HostType> old_partition(
NoInit(
"old_partition"), pol_decomp.
gvid0_pid_h.layout());
763 Kokkos::deep_copy(old_partition, pol_decomp.
gvid0_pid_h);
766 TIMER(
"LOAD_BAL_SET_NEW",
770 TIMER(
"LOAD_BAL_REDIST",
784 load_imbalance =
regions[0].get_observed_load_imbalance();
785 model_belief =
regions[0].get_estimated_time_per_vertex();
790 double f0_cost = 0.0;
int nnodes_on_plane
Number of nodes on local plane.
Definition: domain_decomposition.hpp:89
bool pol_decomp
Use poloidal decomposition.
Definition: domain_decomposition.hpp:82
void update_pol_decomp()
Definition: domain_decomposition.cpp:235
Kokkos::View< int *, Kokkos::LayoutRight, HostType > gvid0_pid_h
Which processors get which vertices (host)
Definition: domain_decomposition.hpp:99
void update_flux_surf(const HostArray< VertexList > &surfaces)
Definition: domain_decomposition.cpp:124
HostArray< VertexList > flux_surfaces_h
Definition: grid.hpp:204
Definition: load_balance.hpp:270
WeightingAlgorithm default_weighting_algorithm
Definition: load_balance.hpp:292
ConstraintOption
Definition: load_balance.hpp:279
double threshold_to_rebalance
Definition: load_balance.hpp:302
bool will_rebalance(ReweightOption reweight_option, WeightingAlgorithm weighting_algorithm, int istep, int gstep, double f0_cost)
Definition: load_balance.hpp:584
bool greedily_fill_partition(const View< double *, HostType > &weight, const View< double *, HostType > &constraint1, double target_weight_per_rank)
Definition: load_balance.hpp:317
double get_even_division(const View< double *, HostType > &input, int n) const
Definition: load_balance.hpp:307
double get_largest_predicted_time(const View< int *, CLayout, HostType > &partition, const View< double *, HostType > &weight) const
Definition: load_balance.hpp:374
void print_new_partition()
Definition: load_balance.hpp:481
double constraint1_max
Definition: load_balance.hpp:305
void initialize_model()
Definition: load_balance.hpp:502
void set_new_partition(const Simulation< DeviceType > &sml, const Grid< DeviceType > &grid, const MagneticField< DeviceType > &magnetic_field, const VelocityGrid &vgrid, Plasma &plasma, DomainDecomposition< DeviceType > &pol_decomp, WeightingAlgorithm weighting_algorithm)
Definition: load_balance.hpp:531
void update_model(const View< int *, CLayout, HostType > ¤t_partition, const std::vector< double > &manual_times)
Definition: load_balance.hpp:496
std::vector< LoadRegion > regions
Definition: load_balance.hpp:294
void update_model(const View< int *, CLayout, HostType > ¤t_partition)
Definition: load_balance.hpp:489
LoadBalance()
Definition: load_balance.hpp:628
void propose_new_partition(const Kokkos::View< double *, Kokkos::LayoutRight, HostType > &ptl_count, WeightingAlgorithm weighting_algorithm)
Definition: load_balance.hpp:516
bool model_is_initialized()
Definition: load_balance.hpp:508
WeightingAlgorithm
Definition: load_balance.hpp:272
bool shift_before_push
Definition: load_balance.hpp:288
void rebalance(DomainDecomposition< DeviceType > &pol_decomp, const View< double *, CLayout, HostType > &constraint, const std::vector< double > &timings, double &load_imbalance, View< double *, HostType > &model_belief)
Definition: load_balance.hpp:780
bool verbose
Definition: load_balance.hpp:303
LoadBalance(NLReader::NamelistReader &nlr, const DomainDecomposition< DeviceType > &pol_decomp, bool sync_planes=true)
Definition: load_balance.hpp:630
ReweightOption
Definition: load_balance.hpp:283
void rebalance(const Simulation< DeviceType > &sml, const Grid< DeviceType > &grid, const MagneticField< DeviceType > &magnetic_field, const VelocityGrid &vgrid, Plasma &plasma, DomainDecomposition< DeviceType > &pol_decomp, int istep, int gstep, ReweightOption reweight_option, WeightingAlgorithm weighting_algorithm=WeightingAlgorithm::Default)
Definition: load_balance.hpp:696
void redistribute_load(const Simulation< DeviceType > &sml, const Grid< DeviceType > &grid, const MagneticField< DeviceType > &magnetic_field, const VelocityGrid &vgrid, Plasma &plasma, DomainDecomposition< DeviceType > &pol_decomp, const View< int *, CLayout, HostType > &old_partition)
Definition: load_balance.hpp:570
void one_weight_balance(const View< double *, HostType > &weight, const View< double *, CLayout, HostType > constraint1)
Definition: load_balance.hpp:390
bool recommend_proposed_partition()
Definition: load_balance.hpp:451
View< int *, CLayout, HostType > proposed_partition
Which processors get which vertices.
Definition: load_balance.hpp:296
Definition: load_balance.hpp:16
View< double *, HostType > get_estimated_time_per_vertex() const
Definition: load_balance.hpp:161
double get_estimated_time_per_vertex(int i) const
Definition: load_balance.hpp:157
double get_largest_predicted_time(const View< int *, CLayout, HostType > &proposed_partition) const
Definition: load_balance.hpp:182
View< double *, HostType > estimated_time_per_vertex
Definition: load_balance.hpp:25
double time_accumulated
Definition: load_balance.hpp:44
double get_time_since_previous_call()
Definition: load_balance.hpp:92
double get_prediction_undershoot() const
Definition: load_balance.hpp:165
void initialize_model()
Definition: load_balance.hpp:198
void update_model_exp_history(const View< int *, CLayout, HostType > ¤t_partition, const View< double *, HostType > &all_periods_timings)
Definition: load_balance.hpp:62
bool model_has_history
Definition: load_balance.hpp:30
UpdateMethod update_method
Definition: load_balance.hpp:46
bool get_model_is_initialized() const
Definition: load_balance.hpp:177
std::string region_name
Definition: load_balance.hpp:26
std::vector< std::string > timer_names
Definition: load_balance.hpp:27
int n_unique_ranks
How many ranks are in a 'period' (in tokamaks, in a plane)
Definition: load_balance.hpp:41
double prediction_undershoot
Definition: load_balance.hpp:34
bool verbose
Definition: load_balance.hpp:28
bool model_is_initialized
Definition: load_balance.hpp:29
double observed_max_region_time
Definition: load_balance.hpp:32
double observed_load_imbalance
Definition: load_balance.hpp:33
double predicted_max_region_time
Definition: load_balance.hpp:31
void update_model_no_history(const View< int *, CLayout, HostType > ¤t_partition, const View< double *, HostType > &all_periods_timings)
Definition: load_balance.hpp:48
double get_observed_max_region_time() const
Definition: load_balance.hpp:169
void update_model(const View< int *, CLayout, HostType > ¤t_partition, double manual_time=-1.0)
Definition: load_balance.hpp:205
int my_period_rank
Definition: load_balance.hpp:42
int n_periods
How many repeating periods there are; in tokamaks this is planes.
Definition: load_balance.hpp:40
void touch_timers()
Definition: load_balance.hpp:116
void reset_timer()
Definition: load_balance.hpp:152
UpdateMethod
Definition: load_balance.hpp:18
double get_observed_load_imbalance() const
Definition: load_balance.hpp:173
Definition: magnetic_field.hpp:12
Definition: NamelistReader.hpp:199
T get(const string ¶m, const T default_val, int val_ind=0)
Definition: NamelistReader.hpp:392
void use_namelist(const string &namelist, Options required=Required)
Definition: NamelistReader.hpp:366
@ Optional
Definition: NamelistReader.hpp:293
Definition: plasma.hpp:13
@ NoDevicePtl
Definition: plasma.hpp:101
bool electron_on
Use kinetic electrons.
Definition: sml.hpp:57
Definition: species.hpp:75
int get_max_n_ptl()
Definition: species.hpp:743
int idx
Index in all_species.
Definition: species.hpp:78
long long int get_total_n_ptl()
Definition: species.hpp:732
Kokkos::View< double **, Kokkos::LayoutRight, HostType > count_ptl_per_node_elec_main_ion(const Grid< DeviceType > &grid, const MagneticField< DeviceType > &magnetic_field, Plasma &plasma, bool kinetic_electrons)
Definition: count_ptl_per_node.cpp:29
View< double *, CLayout, HostType > count_all_ptl_per_node(const Grid< DeviceType > &grid, const MagneticField< DeviceType > &magnetic_field, Plasma &plasma)
Definition: count_ptl_per_node.cpp:71
void f0_redistribute(Plasma &plasma, const DomainDecomposition< DeviceType > &pol_decomp, const Grid< DeviceType > &grid, const MagneticField< DeviceType > &magnetic_field, const VelocityGrid &vgrid, const View< int *, CLayout, HostType > &old_partition)
Definition: f0_redistribute.cpp:81
void exit_XGC(std::string msg)
Definition: globals.hpp:38
bool is_rank_zero()
Definition: globals.hpp:28
void reset_cost_trackers()
int assess_whether_to_rebalance_load(int istep)
void calculate_load_imbalance(int istep, int gstep, double f0_cost)
void set_weights(int *gvid0_pid, double *ptl_count, double *f0_node_cost)
Definition: magnetic_field.F90:1
logical false
Definition: module.F90:103
subroutine plasma(grid, itr, p, dene_out, deni_out, Te_out, Ti_out, Vparai_out, ignore_vacuum)
Calculate the plasma density, temperature, and parallel velocity for a point in triangle itr using pl...
Definition: neutral_totalf.F90:1541
void shift_all_species(Plasma &plasma, const Grid< DeviceType > &grid, const MagneticField< DeviceType > &magnetic_field, const DomainDecomposition< DeviceType > &pol_decomp, bool evenly_distribute_ptl)
Definition: shift.cpp:440
Kokkos::ViewAllocateWithoutInitializing NoInit
Definition: space_settings.hpp:69
Definition: velocity_grid.hpp:8
#define TIMER(N, F)
Definition: timer_macro.hpp:24
static int GPTLstart(const char *name)
Definition: timer_macro.hpp:9
static int GPTLstop(const char *name)
Definition: timer_macro.hpp:10
T::value_type sum_view(const T &view)
Definition: view_arithmetic.hpp:135