Moved serial mode to target mcelecron_serial.

Implemented parallelization with mpi.
This commit is contained in:
Christian
2019-07-18 14:02:19 +02:00
parent fd131afdd3
commit 578d673b65
35 changed files with 916 additions and 469 deletions
+160 -125
View File
@@ -10,147 +10,182 @@
using namespace std;
const long int Simulation::seed = load_parameter("seed");
const long int Simulation::numberTotalDecays = load_parameter("numberTotalDecays");
const long int Simulation::simulationStepWidth = load_parameter("simulationStepWidth");
void Simulation::init() {
set_RNGs();
timer = Timer(&numberDecays);
part_loggers = Particle_Logger_Builder::build(&h5f);
populations = Population_Builder::build(&timer, &rngUni, &rngNormal);
injector = Injector(&rngUni, &rngNormal);
movers = Mover_Builder::build(&part_loggers, &populations);
interactions = Interaction_Builder::build(&populations, &rngUni);
terminators = Terminator_Builder::build();
pop_loggers = Population_Logger_Builder::build(&h5f, &populations, &timer);
pop_load_saver = Population_Load_Saver(&populations, &timer);
set_statusUpdater();
Simulation::Simulation() {
MPI_Comm_size(MPI_COMM_WORLD, &mpi_size);
MPI_Comm_rank(MPI_COMM_WORLD, &mpi_rank);
looper.init(calc_seed(), calc_ID_offset(), calc_outfile_path());
init_pop_arrays();
}
Simulation::~Simulation() {
deconstruct_pop_arrays();
}
void Simulation::set_RNGs() {
rngUni.seed(seed);
rngNormal.seed(seed);
void Simulation::init_pop_arrays() {
vector<species> all_species = get_all_spieces_vector();
number_pop_species = all_species.size();
pop_species = new species[number_pop_species];
pop_array_lengths = new int[number_pop_species];
global_cum_time_s = new double *[number_pop_species];
global_offset = new double *[number_pop_species];
Population_Manager *populations = looper.get_populations();
Population_Base *population;
for (int i = 0; i<number_pop_species; i++) {
pop_species[i] = all_species[i];
population = populations->find(pop_species[i]);
pop_array_lengths[i] = population->get_array_length();
global_cum_time_s[i] = new double[pop_array_lengths[i]];
global_offset[i] = new double[pop_array_lengths[i]];
for (int j = 0; j<pop_array_lengths[i]; j++) {
global_cum_time_s[i][j] = 0;
global_offset[i][j] = 0;
}
}
}
void Simulation::set_statusUpdater() {
updater_fraction = numberTotalDecays/100;
updater_fraction = updater_fraction==0 ? 1 : updater_fraction;
void Simulation::deconstruct_pop_arrays() {
delete[] pop_species;
delete[] pop_array_lengths;
for (int i = 0; i<number_pop_species; i++) {
delete[] global_cum_time_s[i];
delete[] global_offset[i];
}
delete[] global_cum_time_s;
delete[] global_offset;
}
long int Simulation::calc_ID_offset() {
return numberTotalDecays * 1000 * mpi_rank;
}
long int Simulation::calc_seed() {
return seed + mpi_rank;
}
std::string Simulation::calc_outfile_path() {
int dir_number;
std::string dirname;
if (mpi_rank == 0) {
dir_number = calc_dir_number();
dirname = build_dirname(dir_number);
create_out_dir(dirname);
MPI_Bcast(&dir_number, 1, MPI_INT, 0, MPI_COMM_WORLD);
cout<<"Logging to folder "<<dirname<<"\n";
} else {
MPI_Bcast(&dir_number, 1, MPI_INT, 0, MPI_COMM_WORLD);
dirname = build_dirname(dir_number);
}
MPI_Barrier(MPI_COMM_WORLD);
std::stringstream filename;
filename<<dirname<<"mc_electron_out_"<<mpi_rank<<".h5";
return filename.str();
}
int Simulation::calc_dir_number() {
string parname = get_parameter_filenamepath();
stringstream dirname;
int i = 0;
do {
dirname.str(std::string());
dirname << parname << "_" << get_time_string() << "_" << ++i;
} while ( is_dir(dirname.str()) );
return i;
}
std::string Simulation::build_dirname(int dir_number) {
string parname = get_parameter_filenamepath();
stringstream dirname;
dirname.str(std::string());
dirname << parname << "_" << get_time_string() << "_" << dir_number<<"/";
return dirname.str();
}
void Simulation::create_out_dir(std::string dirname) {
if (mkdir(dirname.c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH) != 0) {
stringstream ss;
ss<<"Simulation::create_out_dir: Could not create output directiory.\n outdirName = "<<dirname<<endl;
throw MyException(ss.str());
}
}
void Simulation::doit() {
Mover_Base *moverPtr;
Interaction_Base *interactionPtr;
Terminator_Base *terminatorPtr;
InteractionOut IntOut;
double macroSigma_m2;
std::deque<Particle> partQue;
Particle curPart;
vec3 oldLoc_m;
while ( (numberDecays < numberTotalDecays) || (!partQue.empty()) ) {
if (partQue.empty()) {
pop_loggers.log();
curPart = injector.injectElectron(numberTotalParticles);
numberDecays++;
numberTotalParticles++;
do_statusUpdate();
} else {
curPart = partQue.front();
partQue.pop_front();
}
moverPtr = movers.find(curPart.get_species());
interactionPtr = interactions.find(curPart.get_species());
terminatorPtr = terminators.find(curPart.get_species());
while (!terminatorPtr->checkTermination(curPart)) {
oldLoc_m = curPart.get_x_m();
macroSigma_m2 = interactionPtr->get_macroSigma_m2(curPart);
moverPtr->move(&curPart, macroSigma_m2);
if (!terminatorPtr->checkTermination(curPart)) {
IntOut = interactionPtr->do_Interaction(&curPart);
for (int i=0; i<IntOut.get_number_new_particles(); i++) {
partQue.push_back(IntOut.get_new_particles()[i]);
partQue.back().set_Id(numberTotalParticles++);
}
if (IntOut.get_delete_current()) {
break;
}
}
}
long int decays_this_cpu = numberTotalDecays/mpi_size;
int overflow = numberTotalDecays/mpi_size;
if (overflow < mpi_rank) { // Split overflows equaly to left over CPUs.
decays_this_cpu += 1;
}
pop_loggers.log();
int num_cycles = decays_this_cpu/simulationStepWidth;
int num_remaining = decays_this_cpu%simulationStepWidth;
for (int i = 0; i<(num_cycles-1); i++) {
do_decays(simulationStepWidth);
}
do_decays(simulationStepWidth + num_remaining);
};
void Simulation::do_decays(int number_decays) {
looper.do_loops(number_decays);
number_local_decays += number_decays;
sync_cpus();
do_statusUpdate();
}
void Simulation::sync_cpus() {
MPI_Allreduce(&number_local_decays, &number_global_decays, 1, MPI_INT, MPI_SUM, MPI_COMM_WORLD);
looper.set_global_decays(number_global_decays - number_local_decays);
sync_populations();
}
void Simulation::sync_populations() {
Population_Manager *populations = looper.get_populations();
Population_Base *population;
double *global_delta_cumulated_time_s;
double *global_delta_offset;
double *local_delta_cumulated_time_s;
double *local_delta_offset;
int array_length;
for (int i = 0; i<number_pop_species; i++) {
population = populations->find(pop_species[i]);
array_length = pop_array_lengths[i];
if (array_length == 0) {
continue;
}
global_delta_cumulated_time_s = new double[array_length];
global_delta_offset = new double[array_length];
local_delta_cumulated_time_s = new double[array_length];
local_delta_offset = new double[array_length];
for (int j = 0; j<array_length; j++) {
local_delta_cumulated_time_s[j] = population->get_cumulated_time_s_array()[j] - global_cum_time_s[i][j];
local_delta_offset[j] = population->get_offset_array()[j] - global_offset[i][j];
}
MPI_Allreduce(local_delta_cumulated_time_s, global_delta_cumulated_time_s, array_length, MPI_DOUBLE, MPI_SUM, MPI_COMM_WORLD);
MPI_Allreduce(local_delta_offset, global_delta_offset, array_length, MPI_DOUBLE, MPI_SUM, MPI_COMM_WORLD);
for (int j = 0; j<pop_array_lengths[i]; j++) {
global_cum_time_s[i][j] += global_delta_cumulated_time_s[j];
global_offset[i][j] += global_delta_offset[j];
population->get_cumulated_time_s_array()[j] = global_cum_time_s[i][j];
population->get_offset_array()[j] = global_offset[i][j];
}
delete[] global_delta_cumulated_time_s;
delete[] global_delta_offset;
delete[] local_delta_cumulated_time_s;
delete[] local_delta_offset;
}
}
void Simulation::do_statusUpdate() {
if (numberDecays%updater_fraction == 0){
cout<<numberDecays<<"/"<<numberTotalDecays<<endl;
if (mpi_rank == 0) {
cout<<"Status: "<<number_global_decays<<"/"<<numberTotalDecays<<endl;
}
}
/*
void Simulation::doTC6() {
cout<<endl<<"Simulation::doTC6: Doing Testcase 6."<<endl<<endl;
parameterCheck_TC6();
while ( (numberTotalElectrons < numberTotalDecays) || (electronQue.size() > 0) ) {
if (!electronQue.empty()) {
throw MyException("Simulation::doTC6: electronQue should be empty");
}
electronQue.push_back(Kat.injectElectron(numberTotalElectrons++, &rngUni, &rngNormal));
do_interaction(&electronQue.front()); //parameter should only allow for ionizations
electronQue.pop_front(); // delete initial electron
Log.log_Electron(&electronQue.front(), vec3(0,0,0)); //should be daugther electron
electronQue.pop_front(); //delete daugther electron
}
};
void Simulation::do_interaction(Electron *interactElectron) {
vec3 newLoc_m = electronQue.front().get_x_m();
if (Kat.check_Termination(*interactElectron)) {
stringstream ss;
ss<<"Simulation::do_interaction: Particle already terminated.";
throw MyException(ss.str());
}
int process = interactElectron->roll_Interaction(Kat.get_T2_flowSpeed_ms(newLoc_m),&rngUni);
switch (process) {
case 1: // Elastic Scattering
interactElectron->do_elaScattering(Kat.roll_T2_v_ms(newLoc_m, &rngUni, &rngNormal) , &rngUni);
break;
case 2: // Ionization
electronQue.push_back( interactElectron->do_Ionization(Kat.roll_T2_v_ms(newLoc_m, &rngUni, &rngNormal), numberTotalElectrons, &rngUni) );
numberTotalElectrons++;
// Update Ions...
break;
case 3: // Recombination
break;
default:
stringstream ss;
ss<<"Simulation::do_interaction: process="<<process<< "not defined.";
throw MyException(ss.str());
break;
}
}
void Simulation::parameterCheck_TC6() {
if (
load_parameter("injectionMethod") == 1 &&
load_parameter("elaOn") == 0 &&
load_parameter("ionOn") == 1 &&
load_parameter("numberZBins") == -1
) {
return;
}else {
throw MyException("Simulation::parameterCheck_TC6: Some Parameter is wrong");
}
}
*/