diff --git a/app/poet.cpp b/app/poet.cpp index 69d1b470f..67cf3c6fd 100644 --- a/app/poet.cpp +++ b/app/poet.cpp @@ -18,14 +18,15 @@ ** Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ -#include "poet/ChemistryModule.hpp" #include #include #include +#include #include #include #include #include +#include #include #include @@ -146,12 +147,13 @@ inline double RunMasterLoop(SimParams ¶ms, RInside &R, double dt = Rcpp::as( R.parseEval("mysetup$timesteps[" + std::to_string(iter) + "]")); - cout << "CPP: Next time step is " << dt << "[s]" << endl; + + // cout << "CPP: Next time step is " << dt << "[s]" << endl; + MSG("Next time step is " + std::to_string(dt) + " [s]"); /* displaying iteration number, with C++ and R iterator */ - cout << "CPP: Going through iteration " << iter << endl; - cout << "CPP: R's $iter: " << ((uint32_t)(R.parseEval("mysetup$iter"))) - << ". Iteration" << endl; + MSG("Going through iteration " + std::to_string(iter)); + MSG("R's $iter: " + std::to_string((uint32_t)(R.parseEval("mysetup$iter"))) + ". Iteration"); /* run transport */ // TODO: transport to diffusion @@ -159,9 +161,7 @@ inline double RunMasterLoop(SimParams ¶ms, RInside &R, chem.getField().UpdateFromField(diffusion.getField()); - cout << "CPP: Chemistry" << endl; - - chem.SetTimeStep(dt); + MSG("Chemistry step"); chem.SetTimeStep(dt); chem.RunCells(); @@ -181,10 +181,8 @@ inline double RunMasterLoop(SimParams ¶ms, RInside &R, // store_result is TRUE) R.parseEvalQ("mysetup <- master_iteration_end(setup=mysetup)"); - cout << endl - << "CPP: End of *coupling* iteration " << iter << "/" << maxiter - << endl - << endl; + MSG("End of *coupling* iteration " + std::to_string(iter) + "/" + std::to_string(maxiter)); + MSG(); // MPI_Barrier(MPI_COMM_WORLD); double end_t = MPI_Wtime(); @@ -261,7 +259,7 @@ int main(int argc, char *argv[]) { MPI_Comm_rank(MPI_COMM_WORLD, &world_rank); if (world_rank == 0) { - cout << "Running POET in version " << poet_version << endl << endl; + MSG("Running POET version " + std::string(poet_version)); } if (world_rank > 0) { @@ -288,7 +286,9 @@ int main(int argc, char *argv[]) { } MPI_Barrier(MPI_COMM_WORLD); - cout << "CPP: finished, cleanup of process " << world_rank << endl; + + MSG("finished, cleanup of process " + std::to_string(world_rank)); + MPI_Finalize(); return EXIT_SUCCESS; @@ -312,7 +312,7 @@ int main(int argc, char *argv[]) { return EXIT_SUCCESS; } - cout << "CPP: R Init (RInside) on process " << world_rank << endl; + MSG("RInside initialized on process " + std::to_string(world_rank)); R.parseEvalQ("mysetup <- setup"); // if (world_rank == 0) { // get timestep vector from @@ -326,12 +326,12 @@ int main(int argc, char *argv[]) { // MDL: store all parameters if (world_rank == 0) { - cout << "CPP: Calling R Function to store calling parameters" << endl; - R.parseEvalQ("StoreSetup(setup=mysetup)"); + MSG("Calling R Function to store calling parameters"); + R.parseEvalQ("StoreSetup(setup=mysetup)"); } if (world_rank == 0) { - cout << "CPP: Init done on process with rank " << world_rank << endl; + MSG("Init done on process with rank " + std::to_string(world_rank)); } // MPI_Barrier(MPI_COMM_WORLD); @@ -340,9 +340,9 @@ int main(int argc, char *argv[]) { dSimTime = RunMasterLoop(params, R, g_params, nxyz_master); - cout << "CPP: finished simulation loop" << endl; + MSG("finished simulation loop"); - cout << "CPP: start timing profiling" << endl; + MSG("start timing profiling"); R["simtime"] = dSimTime; R.parseEvalQ("profiling$simtime <- simtime"); @@ -351,16 +351,15 @@ int main(int argc, char *argv[]) { r_vis_code = "saveRDS(profiling, file=paste0(fileout,'/timings.rds'));"; R.parseEval(r_vis_code); - cout << "CPP: Done! Results are stored as R objects into <" - << params.getOutDir() << "/timings.rds>" << endl; + MSG("Done! Results are stored as R objects into <" + params.getOutDir() + "/timings.rds>"); MPI_Barrier(MPI_COMM_WORLD); - cout << "CPP: finished, cleanup of process " << world_rank << endl; + MSG("finished, cleanup of process " + std::to_string(world_rank)); MPI_Finalize(); if (world_rank == 0) { - cout << "CPP: done, bye!" << endl; + MSG("done, bye!"); } exit(0); diff --git a/app/poet.h.in b/app/poet.h.in index d98fe41e9..96a25a2c1 100644 --- a/app/poet.h.in +++ b/app/poet.h.in @@ -7,4 +7,5 @@ const char *poet_version = "@POET_VERSION@"; const char *CHEMISTRY_MODULE_NAME = "state_C"; + #endif // POET_H diff --git a/include/poet/SimParams.hpp b/include/poet/SimParams.hpp index 5651f98d6..a73669865 100644 --- a/include/poet/SimParams.hpp +++ b/include/poet/SimParams.hpp @@ -27,6 +27,7 @@ #include #include +#include "Macros.hpp" #include "DataStructures.hpp" #include "RInsidePOET.hpp" #include "argh.hpp" // Argument handler https://github.com/adishavit/argh diff --git a/src/DiffusionModule.cpp b/src/DiffusionModule.cpp index 609d9cf91..576785461 100644 --- a/src/DiffusionModule.cpp +++ b/src/DiffusionModule.cpp @@ -18,9 +18,10 @@ ** Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ -#include "poet/SimParams.hpp" -#include "tug/BoundaryCondition.hpp" -#include "tug/Diffusion.hpp" +#include +#include +#include +#include #include #include #include @@ -153,32 +154,32 @@ void DiffusionModule::simulate(double dt) { sim_b_transport = MPI_Wtime(); - std::cout << "DiffusionModule::simulate(): Starting diffusion ..." - << std::flush; - + MSG_NOENDL("DiffusionModule::simulate(): Starting diffusion ..."); + std::cout << std::flush; + std::vector> field_2d = t_field.As2DVector(); this->diff_input.setTimestep(dt); for (int i = 0; i < field_2d.size(); i++) { - std::vector in_alpha(this->n_cells_per_prop, this->alpha[i]); - this->diff_input.setBoundaryCondition(this->bc_vec[i]); - - if (this->dim == this->DIM_1D) { - tug::diffusion::BTCS_1D(this->diff_input, field_2d[i].data(), - in_alpha.data()); - } else { - tug::diffusion::ADI_2D(this->diff_input, field_2d[i].data(), - in_alpha.data()); - } + std::vector in_alpha(this->n_cells_per_prop, this->alpha[i]); + this->diff_input.setBoundaryCondition(this->bc_vec[i]); + + if (this->dim == this->DIM_1D) { + tug::diffusion::BTCS_1D(this->diff_input, field_2d[i].data(), + in_alpha.data()); + } else { + tug::diffusion::ADI_2D(this->diff_input, field_2d[i].data(), + in_alpha.data()); + } } - + t_field.SetFromVector(field_2d); - - std::cout << " done!\n"; - + + std::cout << " done!" << std::endl; + sim_a_transport = MPI_Wtime(); - + transport_t += sim_a_transport - sim_b_transport; } diff --git a/src/SimParams.cpp b/src/SimParams.cpp index 62a231a08..8088609cf 100644 --- a/src/SimParams.cpp +++ b/src/SimParams.cpp @@ -18,10 +18,10 @@ ** Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ -#include "poet/DHT_Types.hpp" #include #include #include +#include #include #include @@ -37,204 +37,202 @@ using namespace std; using namespace Rcpp; poet::GridParams::s_GridParams(RInside &R) { - auto tmp_n_cells = - Rcpp::as>(R.parseEval("mysetup$grid$n_cells")); - assert(tmp_n_cells.size() < 3); + auto tmp_n_cells = + Rcpp::as>(R.parseEval("mysetup$grid$n_cells")); + assert(tmp_n_cells.size() < 3); - this->dim = tmp_n_cells.size(); + this->dim = tmp_n_cells.size(); - std::copy(tmp_n_cells.begin(), tmp_n_cells.end(), this->n_cells.begin()); + std::copy(tmp_n_cells.begin(), tmp_n_cells.end(), this->n_cells.begin()); - auto tmp_s_cells = - Rcpp::as>(R.parseEval("mysetup$grid$s_cells")); + auto tmp_s_cells = + Rcpp::as>(R.parseEval("mysetup$grid$s_cells")); - assert(tmp_s_cells.size() == this->dim); + assert(tmp_s_cells.size() == this->dim); - std::copy(tmp_s_cells.begin(), tmp_s_cells.end(), this->s_cells.begin()); + std::copy(tmp_s_cells.begin(), tmp_s_cells.end(), this->s_cells.begin()); - this->total_n = - (dim == 1 ? this->n_cells[0] : this->n_cells[0] * this->n_cells[1]); + this->total_n = + (dim == 1 ? this->n_cells[0] : this->n_cells[0] * this->n_cells[1]); - this->type = Rcpp::as(R.parseEval("mysetup$grid$type")); + this->type = Rcpp::as(R.parseEval("mysetup$grid$type")); } poet::DiffusionParams::s_DiffusionParams(RInside &R) { - this->initial_t = - Rcpp::as(R.parseEval("mysetup$diffusion$init")); - this->alpha = - Rcpp::as(R.parseEval("mysetup$diffusion$alpha")); - if (Rcpp::as( - R.parseEval("'vecinj_inner' %in% names(mysetup$diffusion)"))) { - this->vecinj_inner = - Rcpp::as(R.parseEval("mysetup$diffusion$vecinj_inner")); - } - this->vecinj = - Rcpp::as(R.parseEval("mysetup$diffusion$vecinj")); - this->vecinj_index = - Rcpp::as(R.parseEval("mysetup$diffusion$vecinj_index")); + this->initial_t = + Rcpp::as(R.parseEval("mysetup$diffusion$init")); + this->alpha = + Rcpp::as(R.parseEval("mysetup$diffusion$alpha")); + if (Rcpp::as( + R.parseEval("'vecinj_inner' %in% names(mysetup$diffusion)"))) { + this->vecinj_inner = + Rcpp::as(R.parseEval("mysetup$diffusion$vecinj_inner")); + } + this->vecinj = + Rcpp::as(R.parseEval("mysetup$diffusion$vecinj")); + this->vecinj_index = + Rcpp::as(R.parseEval("mysetup$diffusion$vecinj_index")); } void poet::ChemistryParams::initFromR(RInsidePOET &R) { - this->database_path = - Rcpp::as(R.parseEval("mysetup$chemistry$database")); - this->input_script = - Rcpp::as(R.parseEval("mysetup$chemistry$input_script")); + this->database_path = + Rcpp::as(R.parseEval("mysetup$chemistry$database")); + this->input_script = + Rcpp::as(R.parseEval("mysetup$chemistry$input_script")); - if (R.checkIfExists("dht_species", "mysetup$chemistry")) { - this->dht_signifs = - R.wrapNamedVector("mysetup$chemistry$dht_species"); - } + if (R.checkIfExists("dht_species", "mysetup$chemistry")) { + this->dht_signifs = + R.wrapNamedVector("mysetup$chemistry$dht_species"); + } - if (R.checkIfExists("pht_species", "mysetup$chemistry")) { - this->pht_signifs = - R.wrapNamedVector("mysetup$chemistry$pht_species"); - } + if (R.checkIfExists("pht_species", "mysetup$chemistry")) { + this->pht_signifs = + R.wrapNamedVector("mysetup$chemistry$pht_species"); + } } SimParams::SimParams(int world_rank_, int world_size_) { - this->simparams.world_rank = world_rank_; - this->simparams.world_size = world_size_; + this->simparams.world_rank = world_rank_; + this->simparams.world_size = world_size_; } int SimParams::parseFromCmdl(char *argv[], RInsidePOET &R) { - // initialize argh object - argh::parser cmdl(argv); + // initialize argh object + argh::parser cmdl(argv); - // if user asked for help - if (cmdl[{"help", "h"}]) { - if (simparams.world_rank == 0) { - cout << "Todo" << endl - << "See README.md for further information." << endl; + // if user asked for help + if (cmdl[{"help", "h"}]) { + if (simparams.world_rank == 0) { + MSG("Todo"); + MSG("See README.md for further information."); + } + return poet::PARSER_HELP; } - return poet::PARSER_HELP; - } - // if positional arguments are missing - else if (!cmdl(2)) { - if (simparams.world_rank == 0) { - cerr << "ERROR. Kin needs 2 positional arguments: " << endl - << "1) the R script defining your simulation and" << endl - << "2) the directory prefix where to save results and profiling" - << endl; + // if positional arguments are missing + else if (!cmdl(2)) { + if (simparams.world_rank == 0) { + ERRMSG("Kin needs 2 positional arguments: "); + ERRMSG("1) the R script defining your simulation and"); + ERRMSG("2) the directory prefix where to save results and profiling"); + } + return poet::PARSER_ERROR; } - return poet::PARSER_ERROR; - } - // collect all parameters which are not known, print them to stderr and return - // with PARSER_ERROR - std::list optionsError = validateOptions(cmdl); - if (!optionsError.empty()) { - if (simparams.world_rank == 0) { - cerr << "Unrecognized option(s):\n" << endl; - for (auto option : optionsError) { - cerr << option << endl; - } - cerr << "\nMake sure to use available options. Exiting!" << endl; + // collect all parameters which are not known, print them to stderr and return + // with PARSER_ERROR + std::list optionsError = validateOptions(cmdl); + if (!optionsError.empty()) { + if (simparams.world_rank == 0) { + ERRMSG("Unrecognized option(s):\n"); + for (auto option : optionsError) { + ERRMSG(std::string(option)); + } + ERRMSG("Make sure to use available options. Exiting!"); + } + return poet::PARSER_ERROR; } - return poet::PARSER_ERROR; - } + + simparams.print_progressbar = cmdl[{"P", "progress"}]; - simparams.print_progressbar = cmdl[{"P", "progress"}]; - - /*Parse DHT arguments*/ - chem_params.use_dht = cmdl["dht"]; - // cout << "CPP: DHT is " << ( dht_enabled ? "ON" : "OFF" ) << '\n'; - - if (chem_params.use_dht) { - cmdl("dht-size", DHT_SIZE_PER_PROCESS_MB) >> chem_params.dht_size; - // cout << "CPP: DHT size per process (Byte) = " << dht_size_per_process << - // endl; - - cmdl("dht-snaps", 0) >> chem_params.dht_snaps; - - cmdl("dht-file") >> chem_params.dht_file; - } - /*Parse work package size*/ - cmdl("work-package-size", WORK_PACKAGE_SIZE_DEFAULT) >> simparams.wp_size; - - chem_params.use_interp = cmdl["interp"]; - cmdl("interp-size", 100) >> chem_params.pht_size; - cmdl("interp-min", 5) >> chem_params.interp_min_entries; - cmdl("interp-bucket-entries", 20) >> chem_params.pht_max_entries; - - /*Parse output options*/ - simparams.store_result = !cmdl["ignore-result"]; - - if (simparams.world_rank == 0) { - cout << "CPP: Complete results storage is " - << (simparams.store_result ? "ON" : "OFF") << endl; - cout << "CPP: Work Package Size: " << simparams.wp_size << endl; - cout << "CPP: DHT is " << (chem_params.use_dht ? "ON" : "OFF") << '\n'; + /*Parse DHT arguments*/ + chem_params.use_dht = cmdl["dht"]; + // cout << "CPP: DHT is " << ( dht_enabled ? "ON" : "OFF" ) << '\n'; if (chem_params.use_dht) { - cout << "CPP: DHT strategy is " << simparams.dht_strategy << endl; - cout << "CPP: DHT key default digits (ignored if 'signif_vector' is " - "defined) = " - << simparams.dht_significant_digits << endl; - cout << "CPP: DHT logarithm before rounding: " - << (simparams.dht_log ? "ON" : "OFF") << endl; - cout << "CPP: DHT size per process (Byte) = " << chem_params.dht_size - << endl; - cout << "CPP: DHT save snapshots is " << chem_params.dht_snaps << endl; - cout << "CPP: DHT load file is " << chem_params.dht_file << endl; + cmdl("dht-size", DHT_SIZE_PER_PROCESS_MB) >> chem_params.dht_size; + // cout << "CPP: DHT size per process (Byte) = " << dht_size_per_process << + // endl; + + cmdl("dht-snaps", 0) >> chem_params.dht_snaps; + + cmdl("dht-file") >> chem_params.dht_file; + } + /*Parse work package size*/ + cmdl("work-package-size", WORK_PACKAGE_SIZE_DEFAULT) >> simparams.wp_size; + + chem_params.use_interp = cmdl["interp"]; + cmdl("interp-size", 100) >> chem_params.pht_size; + cmdl("interp-min", 5) >> chem_params.interp_min_entries; + cmdl("interp-bucket-entries", 20) >> chem_params.pht_max_entries; + + /*Parse output options*/ + simparams.store_result = !cmdl["ignore-result"]; + + if (simparams.world_rank == 0) { + MSG("Complete results storage is " + BOOL_PRINT(simparams.store_result)); + MSG("Work Package Size: " + std::to_string(simparams.wp_size)); + MSG("DHT is " + BOOL_PRINT(chem_params.use_dht)); + + if (chem_params.use_dht) { + MSG("DHT strategy is " + std::to_string(simparams.dht_strategy)); + // MDL: these should be outdated (?) + // MSG("DHT key default digits (ignored if 'signif_vector' is " + // "defined) = " + // << simparams.dht_significant_digits); + // MSG("DHT logarithm before rounding: " + // << (simparams.dht_log ? "ON" : "OFF")); + MSG("DHT size per process (Byte) = " + std::to_string(chem_params.dht_size)); + MSG("DHT save snapshots is " + BOOL_PRINT(chem_params.dht_snaps)); + MSG("DHT load file is " + chem_params.dht_file); + } + + if (chem_params.use_interp) { + MSG("PHT interpolation enabled: " + BOOL_PRINT(chem_params.use_interp)); + MSG("PHT interp-size = " + std::to_string(chem_params.pht_size)); + MSG("PHT interp-min = " + std::to_string(chem_params.interp_min_entries)); + MSG("PHT interp-bucket-entries = " + std::to_string(chem_params.pht_max_entries)); + } } - if (chem_params.use_interp) { - cout << "CPP: PHT interpolation enabled: " << chem_params.use_interp << endl; - cout << "CPP: PHT interp-size = " << chem_params.pht_size << endl; - cout << "CPP: PHT interp-min = " << chem_params.interp_min_entries << endl; - cout << "CPP: PHT interp-bucket-entries = " << chem_params.pht_max_entries << endl; - } - } + cmdl(1) >> filesim; + cmdl(2) >> out_dir; - cmdl(1) >> filesim; - cmdl(2) >> out_dir; + chem_params.dht_outdir = out_dir; - chem_params.dht_outdir = out_dir; + /* distribute information to R runtime */ + // if local_rank == 0 then master else worker + R["local_rank"] = simparams.world_rank; + // assign a char* (string) to 'filesim' + R["filesim"] = wrap(filesim); + // assign a char* (string) to 'fileout' + R["fileout"] = wrap(out_dir); + // pass the boolean "store_result" to the R process + R["store_result"] = simparams.store_result; + // worker count + R["n_procs"] = simparams.world_size - 1; + // work package size + R["work_package_size"] = simparams.wp_size; + // dht enabled? + R["dht_enabled"] = chem_params.use_dht; + // log before rounding? + R["dht_log"] = simparams.dht_log; - /* distribute information to R runtime */ - // if local_rank == 0 then master else worker - R["local_rank"] = simparams.world_rank; - // assign a char* (string) to 'filesim' - R["filesim"] = wrap(filesim); - // assign a char* (string) to 'fileout' - R["fileout"] = wrap(out_dir); - // pass the boolean "store_result" to the R process - R["store_result"] = simparams.store_result; - // worker count - R["n_procs"] = simparams.world_size - 1; - // work package size - R["work_package_size"] = simparams.wp_size; - // dht enabled? - R["dht_enabled"] = chem_params.use_dht; - // log before rounding? - R["dht_log"] = simparams.dht_log; + // eval the init string, ignoring any returns + R.parseEvalQ("source(filesim)"); + R.parseEvalQ("mysetup <- setup"); - // eval the init string, ignoring any returns - R.parseEvalQ("source(filesim)"); - R.parseEvalQ("mysetup <- setup"); + this->chem_params.initFromR(R); - this->chem_params.initFromR(R); - - return poet::PARSER_OK; + return poet::PARSER_OK; } void SimParams::initVectorParams(RInside &R) {} std::list SimParams::validateOptions(argh::parser cmdl) { - /* store all unknown parameters here */ - std::list retList; + /* store all unknown parameters here */ + std::list retList; - /* loop over all flags and compare to given flaglist*/ - for (auto &flag : cmdl.flags()) { - if (!(flaglist.find(flag) != flaglist.end())) - retList.push_back(flag); - } + /* loop over all flags and compare to given flaglist*/ + for (auto &flag : cmdl.flags()) { + if (!(flaglist.find(flag) != flaglist.end())) + retList.push_back(flag); + } - /* and loop also over params and compare to given paramlist */ - for (auto ¶m : cmdl.params()) { - if (!(paramlist.find(param.first) != paramlist.end())) - retList.push_back(param.first); - } + /* and loop also over params and compare to given paramlist */ + for (auto ¶m : cmdl.params()) { + if (!(paramlist.find(param.first) != paramlist.end())) + retList.push_back(param.first); + } - return retList; + return retList; }