mirror of
https://git.gfz-potsdam.de/naaice/poet.git
synced 2025-12-16 04:48:23 +01:00
perf: remove RRuntime as there is no need anymore
This commit is contained in:
parent
aa05eaad76
commit
f78842c6d6
@ -18,11 +18,11 @@
|
||||
** Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||||
*/
|
||||
|
||||
#include <RInside.h>
|
||||
#include <Rcpp.h>
|
||||
#include <poet/ChemSim.hpp>
|
||||
#include <poet/DiffusionModule.hpp>
|
||||
#include <poet/Grid.hpp>
|
||||
#include <poet/RRuntime.hpp>
|
||||
#include <poet/SimParams.hpp>
|
||||
|
||||
#include <cstring>
|
||||
@ -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());
|
||||
|
||||
@ -23,7 +23,7 @@
|
||||
|
||||
#include "DHT_Wrapper.hpp"
|
||||
#include "Grid.hpp"
|
||||
#include "RRuntime.hpp"
|
||||
#include "RInside.h"
|
||||
#include "SimParams.hpp"
|
||||
#include <bits/stdint-uintn.h>
|
||||
#include <cstdint>
|
||||
@ -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
|
||||
|
||||
@ -21,7 +21,6 @@
|
||||
#ifndef GRID_H
|
||||
#define GRID_H
|
||||
|
||||
#include "RRuntime.hpp"
|
||||
#include "poet/SimParams.hpp"
|
||||
#include <Rcpp.h>
|
||||
#include <array>
|
||||
@ -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<double>;
|
||||
|
||||
/**
|
||||
* @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<std::uint32_t, MAX_DIM> d_spatial;
|
||||
std::array<std::uint32_t, MAX_DIM> n_cells;
|
||||
|
||||
std::map<std::string, StateMemory *> state_register;
|
||||
// std::map<std::string, std::vector<std::string>> prop_register;
|
||||
|
||||
prop_vec prop_names;
|
||||
|
||||
std::map<std::string, std::vector<double>> 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
|
||||
|
||||
@ -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 <RInside.h>
|
||||
#include <Rcpp.h>
|
||||
|
||||
#include <string>
|
||||
|
||||
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
|
||||
@ -25,9 +25,8 @@
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "RRuntime.hpp"
|
||||
#include "Rcpp/DataFrame.h"
|
||||
#include "argh.hpp" // Argument handler https://github.com/adishavit/argh
|
||||
#include <RInside.h>
|
||||
#include <Rcpp.h>
|
||||
// BSD-licenced
|
||||
|
||||
@ -85,7 +84,7 @@ using GridParams = struct s_GridParams {
|
||||
|
||||
std::vector<std::string> 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
|
||||
|
||||
@ -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();
|
||||
|
||||
|
||||
@ -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;
|
||||
|
||||
@ -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();
|
||||
|
||||
34
src/Grid.cpp
34
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<double>(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;
|
||||
}
|
||||
|
||||
@ -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 <poet/RRuntime.hpp>
|
||||
|
||||
#include <RInside.h>
|
||||
#include <Rcpp.h>
|
||||
|
||||
#include <string>
|
||||
|
||||
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(); }
|
||||
@ -18,10 +18,10 @@
|
||||
** Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||||
*/
|
||||
|
||||
#include "poet/RRuntime.hpp"
|
||||
#include <bits/stdint-uintn.h>
|
||||
#include <poet/SimParams.hpp>
|
||||
|
||||
#include <RInside.h>
|
||||
#include <Rcpp.h>
|
||||
|
||||
#include <iostream>
|
||||
@ -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<std::vector<uint32_t>>(R.parseEval("mysetup$grid$n_cells"));
|
||||
this->s_cells =
|
||||
@ -46,7 +46,7 @@ poet::GridParams::s_GridParams(poet::RRuntime &R) {
|
||||
// Rcpp::as<std::string>(R.parseEval("mysetup$grid$init_sim"));
|
||||
}
|
||||
|
||||
poet::DiffusionParams::s_DiffusionParams(poet::RRuntime &R) {
|
||||
poet::DiffusionParams::s_DiffusionParams(RInside &R) {
|
||||
this->prop_names = Rcpp::as<std::vector<std::string>>(
|
||||
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')");
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user