Got rid of remaining manual allocations, moved some variable declarations to where they are needed

This commit is contained in:
Yohai Meiron 2020-10-28 19:18:37 -04:00
parent aef0f26878
commit d9ed8e5131
8 changed files with 67 additions and 90 deletions

View file

@ -58,21 +58,20 @@ public:
jrk_loc.resize(N);
}
void operator()(const double t, const int n_act, std::vector<int> &ind_act, std::vector<double3> &x_act, std::vector<double3> &v_act,
std::vector<double>& pot, double3 acc[], double3 jrk[])
std::vector<double>& pot, std::vector<double3> &acc, std::vector<double3> &jrk)
{
g6_set_ti(clusterid, t);
for (int i=0; i<n_act; i+=npipe) {
int nn = npipe;
if (n_act-i < npipe) nn = n_act - i;
//TODO any way we can clean up this ugly casting?
g6calc_firsthalf(clusterid, n_loc, nn, ind_act.data()+i, (double(*)[3])&x_act[i], (double(*)[3])&v_act[i], (double(*)[3])&acc_loc[i], (double(*)[3])&jrk_loc[i], &pot_loc[i], eps2, h2.data());
g6calc_lasthalf( clusterid, n_loc, nn, ind_act.data()+i, (double(*)[3])&x_act[i], (double(*)[3])&v_act[i], eps2, h2.data(), (double(*)[3])&acc_loc[i], (double(*)[3])&jrk_loc[i], &pot_loc[i]);
g6_calls++;
} /* i */
/* Reduce the "global" vectors from "local" on all the nodes */
MPI_Allreduce(pot_loc.data(), pot.data(), n_act, MPI_DOUBLE, MPI_SUM, MPI_COMM_WORLD);
MPI_Allreduce(acc_loc.data(), acc, 3*n_act, MPI_DOUBLE, MPI_SUM, MPI_COMM_WORLD);
MPI_Allreduce(jrk_loc.data(), jrk, 3*n_act, MPI_DOUBLE, MPI_SUM, MPI_COMM_WORLD);
MPI_Allreduce(acc_loc.data(), acc.data(), 3*n_act, MPI_DOUBLE, MPI_SUM, MPI_COMM_WORLD);
MPI_Allreduce(jrk_loc.data(), jrk.data(), 3*n_act, MPI_DOUBLE, MPI_SUM, MPI_COMM_WORLD);
}
double g6_calls;
private:
@ -95,7 +94,7 @@ public:
if (component->is_active) return true;
return false;
}
void operator()(int n, const std::vector<double3> &x, const std::vector<double3> &v, double *pot, double3 *acc, double3* jrk)
void operator()(int n, const std::vector<double3> &x, const std::vector<double3> &v, std::vector<double> &pot, std::vector<double3> &acc, std::vector<double3> &jrk)
{
for (auto component : components) {
if (component->is_active)
@ -106,7 +105,7 @@ private:
std::vector<External_gravity*> components;
};
void energy_contr(const double time_cur, const double timesteps, const double n_act_sum, const double g6_calls, int N, const std::vector<double> &m, const std::vector<double3> &x, const std::vector<double3> &v, const std::vector<double> &pot, double pot_ext[])
void energy_contr(const double time_cur, const double timesteps, const double n_act_sum, const double g6_calls, int N, const std::vector<double> &m, const std::vector<double3> &x, const std::vector<double3> &v, const std::vector<double> &pot, const std::vector<double> &pot_ext)
{
double E_pot = 0;
for (int i=0; i<N; i++) E_pot += m[i]*pot[i];
@ -175,10 +174,9 @@ public:
Active_search(const int myRank, const int n_proc, const int n_loc, const int N, bool grapite_active_search_flag)
: myRank(myRank), n_proc(n_proc), n_loc(n_loc), N(N), grapite_active_search_flag(grapite_active_search_flag)
{
ind_act_loc = new int[n_loc];
ind_act_loc.resize(n_loc);
}
~Active_search() { delete[] ind_act_loc; };
double get_minimum_time(const double t[], const double dt[])
double get_minimum_time(const std::vector<double> &t, std::vector<double> &dt)
{
double min_t_loc, min_t;
#ifdef ETICS
@ -197,7 +195,7 @@ public:
MPI_Allreduce(&min_t_loc, &min_t, 1, MPI_DOUBLE, MPI_MIN, MPI_COMM_WORLD);
return min_t;
}
void get_active_indices(const double min_t, const double t[], const double dt[], int ind_act[], int& n_act)
void get_active_indices(const double min_t, const std::vector<double> &t, const std::vector<double> &dt, std::vector<int> &ind_act, int &n_act)
{
#ifdef ETICS
if (grapite_active_search_flag) {
@ -226,11 +224,11 @@ public:
}
private:
int myRank, n_proc, n_loc, N;
int *ind_act_loc;
std::vector<int> ind_act_loc;
bool grapite_active_search_flag;
};
inline void calc_high_derivatives(const double dt_tmp, const double3 a_old, const double3 a_new, const double3 a1_old, const double3 a1_new, double3& a2, double3& a3)
inline void calc_high_derivatives(const double dt_tmp, const double3 &a_old, const double3 &a_new, const double3 &a1_old, const double3 &a1_new, double3 &a2, double3 &a3)
{
double dtinv = 1/dt_tmp;
double dt2inv = dtinv*dtinv;
@ -244,7 +242,7 @@ inline void calc_high_derivatives(const double dt_tmp, const double3 a_old, cons
a3 = 12*a0mia1*dt3inv + 6*ad0plad1*dt2inv;
}
inline void corrector(const double dt_tmp, const double3 a2, const double3 a3, double3& x, double3& v)
inline void corrector(const double dt_tmp, const double3 &a2, const double3 &a3, double3 &x, double3 &v)
{
double dt3over6 = dt_tmp*dt_tmp*dt_tmp/6.0;
double dt4over24 = dt3over6*dt_tmp/4.0;
@ -254,7 +252,7 @@ inline void corrector(const double dt_tmp, const double3 a2, const double3 a3, d
v += dt3over6*a2 + dt4over24*a3;
}
inline double aarseth_step(const double eta, const double dt, const double3 a, const double3 a1, const double3 a2, const double3 a3)
inline double aarseth_step(const double eta, const double dt, const double3 &a, const double3 &a1, const double3 &a2, const double3 &a3)
{
double a1abs = a.norm();
double adot1abs = a1.norm();
@ -274,8 +272,6 @@ int main(int argc, char *argv[])
double timesteps=0.0, n_act_sum=0.0;
double3 zeros = {0, 0, 0}; // Dummy; can't really be const because of the GRAPE interface.
/* INIT the rand() !!! */
srand(19640916); /* it is just my birthday :-) */
@ -298,8 +294,6 @@ int main(int argc, char *argv[])
int diskstep, N;
double time_cur;
// The memory for m, x, and v is allocated inside h5_read or ascii_read
//double *m;
std::vector<double> m;
std::vector<double3> x, v;
if (is_hdf5(config.input_file_name)) {
@ -312,20 +306,6 @@ int main(int argc, char *argv[])
else
ascii_read(config.input_file_name, diskstep, N, time_cur, m, x, v);
std::vector<int> ind(N);
std::iota(begin(ind), end(ind), 0);
double3 *a = new double3[N], *adot = new double3[N];
std::vector<double> pot(N);
double *pot_ext = new double[N], *t = new double[N], *dt = new double[N];
/* data for active particles */
int n_act;
std::vector<int> ind_act(N);
std::vector<double> pot_act_new(N);
std::vector<double3> x_act_new(N), v_act_new(N);
double *pot_act_ext = new double[N];
double3 *a_act_new = new double3[N], *adot_act_new = new double3[N];
double eps = config.eps;
double eta = config.eta;
double t_end = config.t_end;
@ -382,8 +362,7 @@ int main(int argc, char *argv[])
if (ext_disk.is_active) printf("m_disk = %.4E a_disk = %.4E b_disk = %.4E\n", config.ext_m_disk*normalization_mass, config.ext_a_disk*normalization_length, config.ext_b_disk*normalization_length);
if (ext_halo_plummer.is_active) printf("m_halo = %.4E b_halo = %.4E\n", config.ext_m_halo_plummer*normalization_mass, config.ext_b_halo_plummer*normalization_length);
if (ext_log_halo.is_active) printf("v_halo = %.6E r_halo = %.6E \n", config.ext_log_halo_v*normalization_velocity, config.ext_log_halo_r*normalization_length);
if (ext_dehnen.is_active) printf("m_ext = %.6E r_ext = %.6E \t g_ext = %.3E \n", config.ext_dehnen_m*normalization_mass, config.ext_dehnen_r*normalization_length, config.ext_dehnen_gamma);
printf("\n");
if (ext_dehnen.is_active) printf("m_ext = %.6E r_ext = %.6E \t g_ext = %.3E \n\n", config.ext_dehnen_m*normalization_mass, config.ext_dehnen_r*normalization_length, config.ext_dehnen_gamma);
fflush(stdout);
double eta_bh = eta/ETA_BH_CORR;
@ -401,9 +380,6 @@ int main(int argc, char *argv[])
fflush(stdout);
} /* if (myRank == rootRank) */
std::fill(t, t+N, time_cur);
std::fill(dt, dt+N, dt_min);
/* some local settings for G6a boards */
int clusterid, numGPU;
if (config.devices_per_node==0) {
@ -431,33 +407,19 @@ int main(int argc, char *argv[])
#endif
int n_loc = N/n_proc;
Calc_self_grav calc_self_grav(N, n_loc, clusterid, npipe, eps);
Active_search active_search(myRank, n_proc, n_loc, N, grapite_active_search_flag);
Black_hole_physics black_hole_physics;
if (config.live_smbh_count == 1)
black_hole_physics = Black_hole_physics(m[0], 0, myRank, rootRank);
else if (config.live_smbh_count == 2)
black_hole_physics = Black_hole_physics(m[0], m[1], myRank, rootRank);
if (config.binary_smbh_pn) {
black_hole_physics.set_post_newtonian(config.pn_c, config.pn_usage.data());
if (config.pn_usage[6]) black_hole_physics.set_spins(config.smbh1_spin.data(), config.smbh2_spin.data());
}
black_hole_physics.set_softening(eps, config.live_smbh_custom_eps);
Binary_smbh_influence_sphere_output binary_smbh_influence_sphere_output(config.binary_smbh_influence_radius_factor, N, m, x, v, pot, dt);
Write_bh_nb_data write_bh_nb_data(config.live_smbh_neighbor_number, config.live_smbh_count, N, m, x, v);
#ifdef ETICS
grapite_read_particle_tags(N, config.grapite_mask_file_name.c_str(), myRank, n_loc);
grapite_set_dt_exp(config.dt_scf);
grapite_set_t_exp(time_cur);
#endif
std::vector<int> ind(N);
std::iota(begin(ind), end(ind), 0);
/* load the nj particles to the G6 */
double3 zeros = {0, 0, 0}; // Dummy; can't really be const because of the GRAPE interface.
for (int k=0; k<n_loc; k++) {
int j = k + myRank*n_loc;
g6_set_j_particle(clusterid, k, ind[j], t[j], dt[j], m[j], zeros, zeros, zeros, v[j], x[j]);
g6_set_j_particle(clusterid, k, ind[j], time_cur, dt_min, m[j], zeros, zeros, zeros, v[j], x[j]);
} /* k */
#ifdef ETICS
@ -476,16 +438,29 @@ int main(int argc, char *argv[])
}
#endif
std::vector<double3> a(N), adot(N);
std::vector<double> pot(N);
/* define the all particles as a active on all the processors for the first time grav calc. */
Calc_self_grav calc_self_grav(N, n_loc, clusterid, npipe, eps);
calc_self_grav(time_cur, N, ind, x, v, pot, a, adot);
if (config.live_smbh_count == 2) {
Black_hole_physics black_hole_physics;
if (config.live_smbh_count == 1)
black_hole_physics = Black_hole_physics(m[0], 0, myRank, rootRank);
else if (config.live_smbh_count == 2) {
black_hole_physics = Black_hole_physics(m[0], m[1], myRank, rootRank);
black_hole_physics.set_xv(x[0], x[1], v[0], v[1]);
if (config.live_smbh_custom_eps >= 0) black_hole_physics.adjust_softening(pot[0], pot[1], a[0], a[1], adot[0], adot[1]);
if (config.binary_smbh_pn) black_hole_physics.adjust_post_newtonian(dt[0], a[0], a[1], adot[0], adot[1]);
if (config.binary_smbh_pn) black_hole_physics.adjust_post_newtonian(dt_min, a[0], a[1], adot[0], adot[1]);
}
if (config.binary_smbh_pn) {
black_hole_physics.set_post_newtonian(config.pn_c, config.pn_usage.data());
if (config.pn_usage[6]) black_hole_physics.set_spins(config.smbh1_spin.data(), config.smbh2_spin.data());
}
black_hole_physics.set_softening(eps, config.live_smbh_custom_eps);
std::fill(pot_ext, pot_ext+N, 0.);
std::vector<double> pot_ext(N, 0.);
calc_ext_grav(N, x, v, pot_ext, a, adot);
/* Energy control... */
@ -507,6 +482,7 @@ int main(int argc, char *argv[])
}
#endif
std::vector<double> dt(N);
/* Define initial timestep for all particles on all nodes */
for (int j=0; j<N; j++) {
double a2_mod = a[j].norm2();
@ -534,28 +510,33 @@ int main(int argc, char *argv[])
} /* j */
if (config.live_smbh_count > 0) {
double min_dt = *std::min_element(dt, dt+N);
double min_dt = *std::min_element(begin(dt), end(dt));
for (int i=0; i<config.live_smbh_count; i++) dt[i] = min_dt;
}
/* load the new values for particles to the local GRAPEs */
for (int k=0; k<n_loc; k++) {
int j = k + myRank*n_loc;
g6_set_j_particle(clusterid, k, ind[j], t[j], dt[j], m[j], zeros, adot[j]*(1./6.), a[j]*0.5, v[j], x[j]);
g6_set_j_particle(clusterid, k, ind[j], time_cur, dt[j], m[j], zeros, adot[j]*(1./6.), a[j]*0.5, v[j], x[j]);
} /* k */
if (myRank == rootRank) {
/* Write BH data... */
if (config.live_smbh_output) black_hole_physics.write_bh_data(time_cur, m, x, v, pot, a, adot, dt);
/* Write BH NB data... */
if (config.live_smbh_neighbor_output) write_bh_nb_data(time_cur);
} /* if (myRank == rootRank) */
timesteps = 0.0; // Why won't those two be long long instead of double + should include the zeroth step
n_act_sum = 0.0;
std::vector<int> ind_act(N);
std::vector<double3> x_act_new(N), v_act_new(N), a_act_new(N), adot_act_new(N);
std::vector<double> t(N, time_cur), pot_act_new(N);
// Functors for the main integration loop
Active_search active_search(myRank, n_proc, n_loc, N, grapite_active_search_flag);
Binary_smbh_influence_sphere_output binary_smbh_influence_sphere_output(config.binary_smbh_influence_radius_factor, N, m, x, v, pot, dt);
Write_bh_nb_data write_bh_nb_data(config.live_smbh_neighbor_number, config.live_smbh_count, N, m, x, v);
if (myRank == rootRank) {
if (config.live_smbh_output) black_hole_physics.write_bh_data(time_cur, m, x, v, pot, a, adot, dt);
if (config.live_smbh_neighbor_output) write_bh_nb_data(time_cur);
} /* if (myRank == rootRank) */
/* The main integration loop */
while (time_cur <= t_end) {
@ -563,7 +544,8 @@ int main(int argc, char *argv[])
double min_t = active_search.get_minimum_time(t, dt);
/* Get indices of all particles that will be active in this bunch */
active_search.get_active_indices(min_t, t, dt, ind_act.data(), n_act);
int n_act;
active_search.get_active_indices(min_t, t, dt, ind_act, n_act);
/* Find the BH(s) indices in the active list */
int i_bh1=0, i_bh2=1;
@ -603,7 +585,7 @@ int main(int argc, char *argv[])
}
/* Calculate gravity on active particles due to external forces */
std::fill(pot_act_ext, pot_act_ext+n_act, 0.);
std::vector<double> pot_act_ext(N, 0.);
calc_ext_grav(n_act, x_act_new, v_act_new, pot_act_ext, a_act_new, adot_act_new);
/* correct the active particles positions etc... on all the nodes */
@ -757,10 +739,6 @@ int main(int argc, char *argv[])
fflush(stdout);
} /* if (myRank == rootRank) */
delete[] a; delete[] adot; delete[] pot_ext; delete[] t; delete[] dt; delete[] a_act_new; delete[] adot_act_new; delete[] pot_act_ext;
/* Finalize the MPI work */
MPI_Finalize();
return 0;
}