diff --git a/app/poet.cpp b/app/poet.cpp index e63c8dbba..c6c455143 100644 --- a/app/poet.cpp +++ b/app/poet.cpp @@ -18,11 +18,11 @@ ** Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ +#include #include #include #include #include -#include #include #include @@ -62,7 +62,7 @@ int main(int argc, char *argv[]) { } /* initialize R runtime */ - RRuntime R(argc, argv); + RInside R(argc, argv); /*Loading Dependencies*/ // TODO: kann raus @@ -114,7 +114,8 @@ int main(int argc, char *argv[]) { // TODO: Grid anpassen - Grid grid(R, poet::GridParams(R)); + GridParams grid_params = GridParams(R); + Grid grid(grid_params); // grid.init_from_R(); params.initVectorParams(R, grid.getSpeciesCount()); diff --git a/include/poet/ChemSim.hpp b/include/poet/ChemSim.hpp index 1d2cc1227..9e456f811 100644 --- a/include/poet/ChemSim.hpp +++ b/include/poet/ChemSim.hpp @@ -23,7 +23,7 @@ #include "DHT_Wrapper.hpp" #include "Grid.hpp" -#include "RRuntime.hpp" +#include "RInside.h" #include "SimParams.hpp" #include #include @@ -69,7 +69,7 @@ public: * @param R_ R runtime * @param grid_ Initialized grid */ - ChemSim(SimParams ¶ms, RRuntime &R_, Grid &grid_); + ChemSim(SimParams ¶ms, RInside &R_, Grid &grid_); /** * @brief Run iteration of simulation in sequential mode @@ -139,7 +139,7 @@ protected: * @brief Instance of RRuntime object * */ - RRuntime &R; + RInside &R; /** * @brief Initialized grid object @@ -224,7 +224,7 @@ public: * @param R_ R runtime * @param grid_ Grid object */ - ChemMaster(SimParams ¶ms, RRuntime &R_, Grid &grid_); + ChemMaster(SimParams ¶ms, RInside &R_, Grid &grid_); /** * @brief Destroy the ChemMaster object @@ -445,7 +445,7 @@ public: * @param grid_ Grid object * @param dht_comm Communicator addressing all processes marked as worker */ - ChemWorker(SimParams ¶ms, RRuntime &R_, Grid &grid_, MPI_Comm dht_comm); + ChemWorker(SimParams ¶ms, RInside &R_, Grid &grid_, MPI_Comm dht_comm); /** * @brief Destroy the ChemWorker object diff --git a/include/poet/Grid.hpp b/include/poet/Grid.hpp index 76680ba29..46a133829 100644 --- a/include/poet/Grid.hpp +++ b/include/poet/Grid.hpp @@ -21,7 +21,6 @@ #ifndef GRID_H #define GRID_H -#include "RRuntime.hpp" #include "poet/SimParams.hpp" #include #include @@ -67,7 +66,7 @@ public: * * @param R */ - Grid(RRuntime &R, poet::GridParams grid_args); + Grid(poet::GridParams grid_args); ~Grid(); /** @@ -97,78 +96,17 @@ public: std::string module_name = poet::GRID_MODULE_NAME) -> std::vector; - /** - * @brief Shuffle the grid and export it to C memory area - * - * This will call shuffle_field inside R runtime, set the resulting grid as - * buffered data frame in RRuntime object and write R grid to continous C - * memory area. - * - * @param[in,out] buffer Pointer to C memory area - */ - void shuffleAndExport(double *buffer); - - /** - * @brief Unshuffle the grid and import it from C memory area into R runtime - * - * Write C memory area into temporary R grid variable and unshuffle it. - * - * @param buffer Pointer to C memory area - */ - void importAndUnshuffle(double *buffer); - - /** - * @brief Import a C memory area as a work package into R runtime - * - * Get a skeleton from getSkeletonDataFrame inside R runtime and set this as - * buffer data frame of RRuntime object. Now convert C memory area to R data - * structure. - * - * @param buffer Pointer to C memory area - * @param wp_size Count of grid cells per work package - */ - void importWP(double *buffer, unsigned int wp_size); - - /** - * @brief Export a work package from R runtime into C memory area - * - * Set buffer data frame in RRuntime object to data frame holding the results - * and convert this to C memory area. - * - * @param buffer Pointer to C memory area - */ - void exportWP(double *buffer); - private: - /** - * @brief Instance of RRuntime - * - */ - RRuntime R; - - // uint32_t species_count; std::uint8_t dim; std::array d_spatial; std::array n_cells; std::map state_register; - // std::map> prop_register; prop_vec prop_names; std::map> grid_init; - - /** - * @brief Get a SkeletonDataFrame - * - * Return a skeleton with \e n rows of current grid - * - * @param rows number of rows to return skeleton - * @return Rcpp::DataFrame Can be seen as a skeleton. The content of the data - * frame might be irrelevant. - */ - Rcpp::DataFrame getSkeletonDataFrame(unsigned int rows); }; } // namespace poet #endif // GRID_H diff --git a/include/poet/RRuntime.hpp b/include/poet/RRuntime.hpp deleted file mode 100644 index f09933a72..000000000 --- a/include/poet/RRuntime.hpp +++ /dev/null @@ -1,137 +0,0 @@ -/* -** Copyright (C) 2018-2021 Alexander Lindemann, Max Luebke (University of -** Potsdam) -** -** Copyright (C) 2018-2021 Marco De Lucia (GFZ Potsdam) -** -** POET is free software; you can redistribute it and/or modify it under the -** terms of the GNU General Public License as published by the Free Software -** Foundation; either version 2 of the License, or (at your option) any later -** version. -** -** POET is distributed in the hope that it will be useful, but WITHOUT ANY -** WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR -** A PARTICULAR PURPOSE. See the GNU General Public License for more details. -** -** You should have received a copy of the GNU General Public License along with -** this program; if not, write to the Free Software Foundation, Inc., 51 -** Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. -*/ - -#ifndef RRUNTIME_H -#define RRUNTIME_H - -#include -#include - -#include - -namespace poet { - -/** - * @brief Provides an interface to a R runtime. - * - * RRuntime is a wrapper class around a RInside (R) runtime and provides several - * simplified methods to use R commands inside POET. - * - */ -class RRuntime : public RInside { - public: - /** - * @brief Construct a new RRuntime object - * - * Since this is an inherited class of RInside the constructor of the super - * class is just called. - * - * @param argc Argument counter of the program - * @param argv Argument values of the program - */ - RRuntime(const int argc, const char *const argv[]) : RInside(argc, argv){}; - - /** - * Convert a R dataframe into a C continious memory area. - * - * @param buffer Name of the R internal variable name. - */ - - /** - * @brief Convert a R dataframe into a C continious memory area. - * - * A buffer data frame must be set beforehand with setBufferDataFrame. Then - * each value will be set into the continious memory area. This is done row - * wise. - * - * @todo: Might be more performant if all columns would be loaded at once and - * not for each column seperatly - * - * @param buffer Pointer to pre-allocated memory - */ - void to_C_domain(double *buffer); - - /** - * @brief Convert continious C memory area into R dataframe and puts it into R - * runtime. - * - * A buffer data frame must be set beforehand with setBufferDataFrame. Then - * each value will be set into buffered data frame of this object. This is - * done row wise. - * - * @todo: Might be more performant if all columns would be loaded at once and - * not for each column seperatly - * - * @param buffer Pointer to memory area which should be converted into R - * dataframe. - */ - void from_C_domain(double *buffer); - - /** - * @brief Set the Buffer Data Frame object - * - * Set the buffered data frame (will be mostly the grid) of this object. - * - * @param dfname Name of the data frame inside R runtime - */ - void setBufferDataFrame(std::string dfname); - - /** - * @brief Get the Buffer Data Frame object - * - * Returning the current buffered data frame as a Rcpp data frame. - * - * @return Rcpp::DataFrame Current buffered data frame - */ - Rcpp::DataFrame getBufferDataFrame(); - - /** - * @brief Get the Buffer N Col object - * - * Get the numbers of columns of the buffered data frame. - * - * @return size_t Count of columns of buffered data frame - */ - size_t getBufferNCol(); - - /** - * @brief Get the Buffer N Row object - * - * Get the numbers of rows of the buffered data frame. - * - * @return size_t Count of rows of buffered data frame - */ - size_t getBufferNRow(); - - private: - /** - * @brief Buffered data frame - * - * This is used to convert a R data frame into continious memory used by C/C++ - * runtime and vice versa. Must be set with setBufferDataFrame and can be - * manipulated with from_C_domain. - * - * @todo: Find a cleaner solution. Maybe abstraction via another class. - * - */ - Rcpp::DataFrame dfbuff; -}; -} // namespace poet -#endif // RRUNTIME_H diff --git a/include/poet/SimParams.hpp b/include/poet/SimParams.hpp index 5ba447679..289da5a5c 100644 --- a/include/poet/SimParams.hpp +++ b/include/poet/SimParams.hpp @@ -25,9 +25,8 @@ #include #include -#include "RRuntime.hpp" -#include "Rcpp/DataFrame.h" #include "argh.hpp" // Argument handler https://github.com/adishavit/argh +#include #include // BSD-licenced @@ -85,7 +84,7 @@ using GridParams = struct s_GridParams { std::vector props; - s_GridParams(poet::RRuntime &R); + s_GridParams(RInside &R); }; using DiffusionParams = struct s_DiffusionParams { @@ -97,7 +96,7 @@ using DiffusionParams = struct s_DiffusionParams { Rcpp::DataFrame vecinj; Rcpp::DataFrame vecinj_index; - s_DiffusionParams(poet::RRuntime &R); + s_DiffusionParams(RInside &R); }; /** @@ -149,7 +148,7 @@ public: * @return int Returns with 0 if no error occured, otherwise value less than 0 * is returned. */ - int parseFromCmdl(char *argv[], RRuntime &R); + int parseFromCmdl(char *argv[], RInside &R); /** * @brief Init std::vector values @@ -162,7 +161,7 @@ public: * @param col_count Count of variables per grid cell (typically the count of * columns of each grid cell) */ - void initVectorParams(RRuntime &R, int col_count); + void initVectorParams(RInside &R, int col_count); /** * @brief Set if dt differs diff --git a/src/ChemMaster.cpp b/src/ChemMaster.cpp index bef46381e..727faa7e8 100644 --- a/src/ChemMaster.cpp +++ b/src/ChemMaster.cpp @@ -33,7 +33,7 @@ using namespace poet; using namespace std; using namespace Rcpp; -ChemMaster::ChemMaster(SimParams ¶ms, RRuntime &R_, Grid &grid_) +ChemMaster::ChemMaster(SimParams ¶ms, RInside &R_, Grid &grid_) : ChemSim(params, R_, grid_) { t_simparams tmp = params.getNumParams(); diff --git a/src/ChemSim.cpp b/src/ChemSim.cpp index beea599e7..bd1b54a98 100644 --- a/src/ChemSim.cpp +++ b/src/ChemSim.cpp @@ -33,7 +33,7 @@ using namespace Rcpp; using namespace poet; -ChemSim::ChemSim(SimParams ¶ms, RRuntime &R_, Grid &grid_) +ChemSim::ChemSim(SimParams ¶ms, RInside &R_, Grid &grid_) : R(R_), grid(grid_) { t_simparams tmp = params.getNumParams(); this->world_rank = tmp.world_rank; diff --git a/src/ChemWorker.cpp b/src/ChemWorker.cpp index 9f2e1919f..f2257c9e7 100644 --- a/src/ChemWorker.cpp +++ b/src/ChemWorker.cpp @@ -32,7 +32,7 @@ using namespace poet; using namespace std; using namespace Rcpp; -ChemWorker::ChemWorker(SimParams ¶ms, RRuntime &R_, Grid &grid_, +ChemWorker::ChemWorker(SimParams ¶ms, RInside &R_, Grid &grid_, MPI_Comm dht_comm) : ChemSim(params, R_, grid_) { t_simparams tmp = params.getNumParams(); diff --git a/src/Grid.cpp b/src/Grid.cpp index 1f4130c31..47b224893 100644 --- a/src/Grid.cpp +++ b/src/Grid.cpp @@ -49,9 +49,8 @@ static inline int8_t get_type_id(std::string type) { return -1; } -Grid::Grid(RRuntime &R, poet::GridParams grid_args) : R(R) { +Grid::Grid(poet::GridParams grid_args) { // get dimension of grid - // this->dim = R.parseEval("length(mysetup$grid$n_cells)"); this->dim = grid_args.n_cells.size(); // assert that dim is 1 or 2 @@ -205,34 +204,3 @@ auto poet::Grid::getSpeciesByName(std::string name, std::string module_name) return std::vector(module_memory->mem.begin() + begin_vec, module_memory->mem.begin() + end_vec); } - -void Grid::shuffleAndExport(double *buffer) { - R.parseEval("tmp <- shuffle_field(mysetup$state_T, ordered_ids)"); - R.setBufferDataFrame("tmp"); - R.to_C_domain(buffer); -} - -void Grid::importAndUnshuffle(double *buffer) { - R.setBufferDataFrame("GRID_TMP"); - R.from_C_domain(buffer); - R["GRID_CHEM_DATA"] = R.getBufferDataFrame(); - R.parseEval("result <- unshuffle_field(GRID_CHEM_DATA, ordered_ids)"); -} - -void Grid::importWP(double *buffer, unsigned int wp_size) { - R["GRID_WP_SKELETON"] = getSkeletonDataFrame(wp_size); - R.setBufferDataFrame("GRID_WP_SKELETON"); - R.from_C_domain(buffer); - R["work_package_full"] = R.getBufferDataFrame(); -} -void Grid::exportWP(double *buffer) { - R.setBufferDataFrame("result_full"); - R.to_C_domain(buffer); -} - -Rcpp::DataFrame Grid::getSkeletonDataFrame(unsigned int rows) { - R["GRID_ROWS"] = rows; - - Rcpp::DataFrame tmp = R.parseEval("head(GRID_TMP,GRID_ROWS)"); - return tmp; -} diff --git a/src/RRuntime.cpp b/src/RRuntime.cpp deleted file mode 100644 index bb46d1ce2..000000000 --- a/src/RRuntime.cpp +++ /dev/null @@ -1,64 +0,0 @@ -/* -** Copyright (C) 2018-2021 Alexander Lindemann, Max Luebke (University of -** Potsdam) -** -** Copyright (C) 2018-2021 Marco De Lucia (GFZ Potsdam) -** -** POET is free software; you can redistribute it and/or modify it under the -** terms of the GNU General Public License as published by the Free Software -** Foundation; either version 2 of the License, or (at your option) any later -** version. -** -** POET is distributed in the hope that it will be useful, but WITHOUT ANY -** WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR -** A PARTICULAR PURPOSE. See the GNU General Public License for more details. -** -** You should have received a copy of the GNU General Public License along with -** this program; if not, write to the Free Software Foundation, Inc., 51 -** Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. -*/ - -#include - -#include -#include - -#include - -using namespace poet; - -void RRuntime::to_C_domain(double *buffer) { - size_t rowCount = dfbuff.nrow(); - size_t colCount = dfbuff.ncol(); - - for (size_t i = 0; i < rowCount; i++) { - for (size_t j = 0; j < colCount; j++) { - /* Access column vector j and extract value of line i */ - Rcpp::DoubleVector col = dfbuff[j]; - buffer[i * colCount + j] = col[i]; - } - } -} - -void RRuntime::from_C_domain(double *buffer) { - size_t rowCount = dfbuff.nrow(); - size_t colCount = dfbuff.ncol(); - - for (size_t i = 0; i < rowCount; i++) { - for (size_t j = 0; j < colCount; j++) { - /* Access column vector j and extract value of line i */ - Rcpp::DoubleVector col = dfbuff[j]; - col[i] = buffer[i * colCount + j]; - } - } -} - -void RRuntime::setBufferDataFrame(std::string dfname) { - this->dfbuff = parseEval(dfname); -} - -Rcpp::DataFrame RRuntime::getBufferDataFrame() { return this->dfbuff; } - -size_t RRuntime::getBufferNCol() { return (this->dfbuff).ncol(); } - -size_t RRuntime::getBufferNRow() { return (this->dfbuff).nrow(); } diff --git a/src/SimParams.cpp b/src/SimParams.cpp index 5b3eabfab..aaf3d357a 100644 --- a/src/SimParams.cpp +++ b/src/SimParams.cpp @@ -18,10 +18,10 @@ ** Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ -#include "poet/RRuntime.hpp" #include #include +#include #include #include @@ -32,7 +32,7 @@ using namespace poet; using namespace std; using namespace Rcpp; -poet::GridParams::s_GridParams(poet::RRuntime &R) { +poet::GridParams::s_GridParams(RInside &R) { this->n_cells = Rcpp::as>(R.parseEval("mysetup$grid$n_cells")); this->s_cells = @@ -46,7 +46,7 @@ poet::GridParams::s_GridParams(poet::RRuntime &R) { // Rcpp::as(R.parseEval("mysetup$grid$init_sim")); } -poet::DiffusionParams::s_DiffusionParams(poet::RRuntime &R) { +poet::DiffusionParams::s_DiffusionParams(RInside &R) { this->prop_names = Rcpp::as>( R.parseEval("names(mysetup$diffusion$init)")); this->alpha = @@ -66,7 +66,7 @@ SimParams::SimParams(int world_rank_, int world_size_) { this->simparams.world_size = world_size_; } -int SimParams::parseFromCmdl(char *argv[], RRuntime &R) { +int SimParams::parseFromCmdl(char *argv[], RInside &R) { // initialize argh object argh::parser cmdl(argv); @@ -180,7 +180,7 @@ int SimParams::parseFromCmdl(char *argv[], RRuntime &R) { return PARSER_OK; } -void SimParams::initVectorParams(RRuntime &R, int col_count) { +void SimParams::initVectorParams(RInside &R, int col_count) { if (simparams.dht_enabled) { /*Load significance vector from R setup file (or set default)*/ bool signif_vector_exists = R.parseEval("exists('signif_vector')");