From 08a2baba118cd4da547eba484b6a8533dc3e5199 Mon Sep 17 00:00:00 2001 From: Max Luebke Date: Tue, 2 Apr 2024 09:16:58 +0000 Subject: [PATCH] Remove unnecessary includes and update function signature --- R_lib/kin_r_library.R | 37 +++++++++---------- src/Chemistry/WorkerFunctions.cpp | 59 ++++++++++++++++++++---------- src/Transport/DiffusionModule.cpp | 2 - src/poet.cpp | 61 +++++++++++++++---------------- 4 files changed, 86 insertions(+), 73 deletions(-) diff --git a/R_lib/kin_r_library.R b/R_lib/kin_r_library.R index d4d04f9e2..a314eb702 100644 --- a/R_lib/kin_r_library.R +++ b/R_lib/kin_r_library.R @@ -67,33 +67,32 @@ master_init <- function(setup) { ## This function, called only by master, stores on disk the last ## calculated time step if store_result is TRUE and increments the ## iteration counter -master_iteration_end <- function(setup) { +master_iteration_end <- function(setup,iter) { # iter <- setup$iter # print(iter) ## max digits for iterations - iter <- 1 - dgts <- as.integer(ceiling(log10(1))) + dgts <- as.integer(ceiling(log10(iter))) ## string format to use in sprintf fmt <- paste0("%0", dgts, "d") ## Write on disk state_T and state_C after every iteration ## comprised in setup$out_save - # if (setup$store_result) { - # if (iter %in% setup$out_save) { - nameout <- paste0(fileout, "/iter_", sprintf(fmt = fmt, iter), ".rds") - info <- list( - tr_req_dt = as.integer(1) - ## tr_allow_dt = setup$allowed_dt, - ## tr_inniter = as.integer(setup$inniter) - ) - saveRDS(list( - T = setup$state_T, C = setup$state_C, - simtime = as.integer(0), - tr_info = info - ), file = nameout) - msgm("results stored in <", nameout, ">") - # } - # } + if (setup$store_result) { + if (iter %in% setup$out_save) { + nameout <- paste0(fileout, "/iter_", sprintf(fmt = fmt, iter), ".rds") + info <- list( + tr_req_dt = as.integer(1) + ## tr_allow_dt = setup$allowed_dt, + ## tr_inniter = as.integer(setup$inniter) + ) + saveRDS(list( + T = setup$state_T, C = setup$state_C, + simtime = as.integer(0), + tr_info = info + ), file = nameout) + msgm("results stored in <", nameout, ">") + } + } msgm("done iteration", iter, "/", 1) setup$iter <- setup$iter + 1 return(setup) diff --git a/src/Chemistry/WorkerFunctions.cpp b/src/Chemistry/WorkerFunctions.cpp index 6f4f81c6d..d3d4edf23 100644 --- a/src/Chemistry/WorkerFunctions.cpp +++ b/src/Chemistry/WorkerFunctions.cpp @@ -281,7 +281,7 @@ void poet::ChemistryModule::WorkerRunWorkPackage(WorkPackage &work_package, double dSimTime, double dTimestep) { // check if we actually need to start phreeqc - bool queued_cell = false; + std::map> zone_mapping; for (std::size_t wp_id = 0; wp_id < work_package.size; wp_id++) { if (work_package.mapping[wp_id] != CHEM_PQC) { @@ -289,9 +289,9 @@ void poet::ChemistryModule::WorkerRunWorkPackage(WorkPackage &work_package, } const auto &input = work_package.input[wp_id]; - const auto &sol_id = input[0]; + const auto pqc_id = input[0]; - auto &phreeqc_instance = this->phreeqc_instances[sol_id]; + auto &phreeqc_instance = this->phreeqc_instances[pqc_id]; work_package.output[wp_id] = work_package.input[wp_id]; work_package.input[wp_id].erase(work_package.input[wp_id].begin()); @@ -304,38 +304,57 @@ void poet::ChemistryModule::WorkerRunWorkPackage(WorkPackage &work_package, work_package.input[wp_id].end()); phreeqc_instance->queueCell(work_package.input[wp_id]); - queued_cell = true; + zone_mapping[pqc_id].push_back(wp_id); } - if (!queued_cell) { + if (zone_mapping.empty()) { return; } - std::map> zone_mapping; - - // run the phreeqc instances - for (const auto &[pqc_id, phreeqc_instance] : this->phreeqc_instances) { - if (zone_mapping.find(pqc_id) == zone_mapping.end()) { + for (const auto &[pqc_id, eval_cells] : zone_mapping) { + if (eval_cells.empty()) { continue; } - phreeqc_instance->runQueuedCells(dTimestep); - // remap the output to the work_package + this->phreeqc_instances[pqc_id]->runQueuedCells(dTimestep); + std::vector> pqc_out; - phreeqc_instance->dequeueCells(pqc_out); + this->phreeqc_instances[pqc_id]->dequeueCells(pqc_out); - std::size_t output_id = 0; - - for (const auto &wp_id : zone_mapping[pqc_id]) { + for (std::size_t i = 0; i < eval_cells.size(); i++) { + std::size_t wp_id = eval_cells[i]; std::size_t output_index = 0; - for (std::size_t i = 1; i < work_package.output[wp_id].size(); i++) { - if (!(std::isnan(work_package.output[wp_id][i]))) { - work_package.output[wp_id][i] = pqc_out[output_id][output_index++]; + for (std::size_t j = 1; j < work_package.output[wp_id].size(); j++) { + if (!(std::isnan(work_package.output[wp_id][j]))) { + work_package.output[wp_id][j] = pqc_out[i][output_index++]; } } - output_id++; } } + + // run the phreeqc instances + // for (const auto &[pqc_id, phreeqc_instance] : this->phreeqc_instances) { + // // if (zone_mapping.find(pqc_id) == zone_mapping.end()) { + // // continue; + // // } + // phreeqc_instance->runQueuedCells(dTimestep); + + // // remap the output to the work_package + // std::vector> pqc_out; + // phreeqc_instance->dequeueCells(pqc_out); + + // std::size_t output_id = 0; + + // for (const auto &wp_id : zone_mapping[pqc_id]) { + // std::size_t output_index = 0; + // for (std::size_t i = 1; i < work_package.output[wp_id].size(); i++) { + // if (!(std::isnan(work_package.output[wp_id][i]))) { + // work_package.output[wp_id][i] = pqc_out[output_id][output_index++]; + // } + // } + // output_id++; + // } + // } } void poet::ChemistryModule::WorkerPerfToMaster(int type, diff --git a/src/Transport/DiffusionModule.cpp b/src/Transport/DiffusionModule.cpp index 17f761b57..d81f0d885 100644 --- a/src/Transport/DiffusionModule.cpp +++ b/src/Transport/DiffusionModule.cpp @@ -35,8 +35,6 @@ #include "tug/Grid.hpp" #include "tug/Simulation.hpp" -#include -#include #include #include diff --git a/src/poet.cpp b/src/poet.cpp index f162b7820..1f6be971f 100644 --- a/src/poet.cpp +++ b/src/poet.cpp @@ -276,8 +276,9 @@ ParseRet parseInitValues(char **argv, RInsidePOET &R, // chem.LoadDatabase(database_path); // } -static double RunMasterLoop(RInside &R, const RuntimeParameters ¶ms, - DiffusionModule &diffusion, ChemistryModule &chem) { +static Rcpp::List RunMasterLoop(RInside &R, const RuntimeParameters ¶ms, + DiffusionModule &diffusion, + ChemistryModule &chem) { /* Iteration Count is dynamic, retrieving value from R (is only needed by * master for the following loop) */ @@ -333,7 +334,9 @@ static double RunMasterLoop(RInside &R, const RuntimeParameters ¶ms, chem.simulate(dt); writeFieldsToR(R, diffusion.getField(), chem.GetField()); - // diffusion.getField().update(chem.GetField()); + // R["store_result"] = true; + // R.parseEval("mysetup$store_result <- TRUE"); + diffusion.getField().update(chem.GetField()); R["req_dt"] = dt; R["simtime"] = (sim_time += dt); @@ -341,11 +344,13 @@ static double RunMasterLoop(RInside &R, const RuntimeParameters ¶ms, R.parseEval("mysetup$req_dt <- req_dt"); R.parseEval("mysetup$simtime <- simtime"); + R["iter"] = iter; + // MDL master_iteration_end just writes on disk state_T and // state_C after every iteration if the cmdline option // --ignore-results is not given (and thus the R variable // store_result is TRUE) - R.parseEval("mysetup <- master_iteration_end(setup=mysetup)"); + R.parseEval("mysetup <- master_iteration_end(setup=mysetup, iter)"); MSG("End of *coupling* iteration " + std::to_string(iter) + "/" + std::to_string(maxiter)); @@ -356,31 +361,21 @@ static double RunMasterLoop(RInside &R, const RuntimeParameters ¶ms, dSimTime += end_t - start_t; } // END SIMULATION LOOP - // R.parseEvalQ("profiling <- list()"); + Rcpp::List chem_profiling; + chem_profiling["simtime"] = chem.GetChemistryTime(); + chem_profiling["loop"] = chem.GetMasterLoopTime(); + chem_profiling["sequential"] = chem.GetMasterSequentialTime(); + chem_profiling["idle_master"] = chem.GetMasterIdleTime(); + chem_profiling["idle_worker"] = Rcpp::wrap(chem.GetWorkerIdleTimings()); + chem_profiling["phreeqc_time"] = Rcpp::wrap(chem.GetWorkerPhreeqcTimings()); - // R["simtime_chemistry"] = chem.GetChemistryTime(); - // R.parseEvalQ("profiling$simtime_chemistry <- simtime_chemistry"); + Rcpp::List diffusion_profiling; + diffusion_profiling["simtime"] = diffusion.getTransportTime(); - // R["chemistry_loop"] = chem.GetMasterLoopTime(); - // R.parseEvalQ("profiling$chemistry_loop <- chemistry_loop"); - - // R["chemistry_sequential"] = chem.GetMasterSequentialTime(); - // R.parseEvalQ("profiling$simtime_sequential <- chemistry_sequential"); - - // R["idle_master"] = chem.GetMasterIdleTime(); - // R.parseEvalQ("profiling$idle_master <- idle_master"); - - // R["idle_worker"] = Rcpp::wrap(chem.GetWorkerIdleTimings()); - // R.parseEvalQ("profiling$idle_worker <- idle_worker"); - - // R["phreeqc_time"] = Rcpp::wrap(chem.GetWorkerPhreeqcTimings()); - // R.parseEvalQ("profiling$phreeqc <- phreeqc_time"); - - // R["simtime_transport"] = diffusion.getTransportTime(); - // R.parseEvalQ("profiling$simtime_transport <- simtime_transport"); - - // R["phreeqc_count"] = phreeqc_counts; - // R.parseEvalQ("profiling$phreeqc_count <- phreeqc_count"); + Rcpp::List profiling; + profiling["simtime"] = dSimTime; + profiling["chemistry"] = chem_profiling; + profiling["diffusion"] = diffusion_profiling; // if (params.getChemParams().use_dht) { // R["dht_hits"] = Rcpp::wrap(chem.GetWorkerDHTHits()); @@ -411,7 +406,7 @@ static double RunMasterLoop(RInside &R, const RuntimeParameters ¶ms, chem.MasterLoopBreak(); - return dSimTime; + return profiling; } int main(int argc, char *argv[]) { @@ -492,7 +487,7 @@ int main(int argc, char *argv[]) { chemistry.masterSetField(init_list.getInitialGrid()); - dSimTime = RunMasterLoop(R, run_params, diffusion, chemistry); + Rcpp::List profiling = RunMasterLoop(R, run_params, diffusion, chemistry); MSG("finished simulation loop"); @@ -501,9 +496,11 @@ int main(int argc, char *argv[]) { // R["simtime"] = dSimTime; // R.parseEvalQ("profiling$simtime <- simtime"); - // string r_vis_code; - // r_vis_code = "saveRDS(profiling, file=paste0(fileout,'/timings.rds'));"; - // R.parseEval(r_vis_code); + R["profiling"] = profiling; + + string r_vis_code; + r_vis_code = "saveRDS(profiling, file=paste0(fileout,'/timings.rds'));"; + R.parseEval(r_vis_code); // MSG("Done! Results are stored as R objects into <" + run_params.getOutDir() // +