Sub sequential C calls to R doesn't work - c++

I'm writing a c++ function that takes in a R-call and evaluates it. The function returns as expected whenever I compile with a single call to it.
But when there are sub sequential calls to my function it gets stuck when I run the object file.
How would I re-instantiate the R instance after a single function call?
The method below gets stuck when there are two calls to my function but works fine when I do one call.
int main(int argc, char* argv[]){
double result1 = calculateScalar(input2);
std::cout << "Result 1 " << result1;
double result2 = calculateScalar(input2);
std::cout << "Result 2 " <<result2;
return 0;
}
double calculateScalar(const char* RCALL){
SEXP formula, result;
ParseStatus status;
R_xlen_t len;
int errorStatus;
try {
Rf_initEmbeddedR(0, NULL);
Rf_protect(formula = mkString(RCALL));
Rf_protect(formula = R_ParseVector(formula, 1, &status, R_NilValue));
result = R_tryEval(VECTOR_ELT(formula,0), R_GlobalEnv, &errorStatus);
PROTECT(result);
len = xlength(result);
UNPROTECT(3);
Rf_endEmbeddedR(0);
return (double) getNumericScalar(result);
} catch(std::exception& e){
std::cout << "Standard exception: " << e.what();
}
//Clean up R
Rf_endEmbeddedR(0);
exit(0);
}

Instead of reinventing the wheel, you could just RInside:
edd#brad:~/git/rinside/inst/examples/standard(master)$ make rinside_sample3
ccache g++ -I/usr/share/R/include -I/usr/local/lib/R/site-library/Rcpp/include \
-I/usr/local/lib/R/site-library/RInside/include -g -O3 -Wall -pipe -Wno-unused \
-pedantic -Wall rinside_sample3.cpp -Wl,--export-dynamic -fopenmp\
-L/usr/lib/R/lib -lR -lpcre -llzma -lbz2 -lz -lrt -ldl -lm \
-lblas -llapack -L/usr/local/lib/R/site-library/RInside/lib -lRInside \
-Wl,-rpath,/usr/local/lib/R/site-library/RInside/lib -o rinside_sample3
edd#brad:~/git/rinside/inst/examples/standard(master)$
builds it (automagically using an automated `GNUmakefile) from this simple source file.
Running it yields:
edd#brad:~/git/rinside/inst/examples/standard(master)$ ./rinside_sample3
Call:
lm(formula = Fertility ~ ., data = swiss)
Residuals:
Min 1Q Median 3Q Max
-15.2743 -5.2617 0.5032 4.1198 15.3213
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 66.91518 10.70604 6.250 1.91e-07 ***
Agriculture -0.17211 0.07030 -2.448 0.01873 *
Examination -0.25801 0.25388 -1.016 0.31546
Education -0.87094 0.18303 -4.758 2.43e-05 ***
Catholic 0.10412 0.03526 2.953 0.00519 **
Infant.Mortality 1.07705 0.38172 2.822 0.00734 **
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Residual standard error: 7.165 on 41 degrees of freedom
Multiple R-squared: 0.7067, Adjusted R-squared: 0.671
F-statistic: 19.76 on 5 and 41 DF, p-value: 5.594e-10
And now from C++
Estimate Std. Error t value Pr(>|t|)
(Intercept) 66.9152 10.706 6.25023 1.90605e-07
Agriculture -0.172114 0.0703039 -2.44814 0.0187272
Examination -0.258008 0.253878 -1.01627 0.315462
Education -0.87094 0.183029 -4.75849 2.4306e-05
Catholic 0.104115 0.0352579 2.95297 0.00519008
Infant.Mortality 1.07705 0.38172 2.82157 0.00733572
edd#brad:~/git/rinside/inst/examples/standard(master)$
A few things to note:
I picked this example (among literally dozens of other in the package) as it contains multiple eval...() calls in the C++ code.
It is also cute as it redoes lm() for you.
The actual compilation following make rinside_sample3 will vary from system to system; on mine it reflect some parameters I set in ~/.R/Makevars
I manually narrow the display by four spaces to make it fit.

Related

can't update R package documentation: `System command 'R' failed` (#using statements don't work)

I have a RcppEigen project that I'm trying to update documentation for. This project only exports one c++ function into R.
Unfortunately, I get a problem when I run either devtools::document(pkg = "~/pfexamplesinr/") or roxygen2::roxygenize(roclets="rd", package.dir = "pfexamplesinr/") The error message is below.
It says something about System command 'R' failed. The entire build output is too large to paste (I go over the character limit). If I search for the first error, it's RcppExports.cpp:15:21: error: ‘Map’ was not declared in this scope. Is there some funny business about the order of #include directives and // [[Rcpp::depends(RcppEigen)]] statements?
Note: the code builds fine when I use Rcpp::sourceCpp('pfexamplesinr/src/likelihoods.cpp')
Edit: this problem goes away when I remove the #using statements, and prepend Eigen:: whenever necessary.
#include <RcppEigen.h>
#include "svol_sisr_hilb.h"
#include "resamplers.h"
// [[Rcpp::depends(RcppEigen)]]
// choose number of particles, and number of bits for inverse Hilbert curve map
#define NP 500
#define NB 5
#define debug_mode false
using Eigen::Map;
using Eigen::MatrixXd;
using Eigen::VectorXd;
using hilb_sys_resamp_T = pf::resamplers::sys_hilb_resampler<NP,1,NB,double>;
using svol_pfilter = svol_sisr_hilb<NP,NB, hilb_sys_resamp_T, double, debug_mode>;
// helpful notes:
// 1.
// parameters passed to svol_pfilter() ctor are in the following order: phi, beta, sigma
// 2.
// uProposal will be dimension (time X (particles + 1))
// first NP columns will be used for state sampling
// last column will be used for resampling at each time point
// 3.
// choosing NP or NB too large will result in stackoverflow
// number of particles is set in two places: in the #define directive and also used in your R script
// [[Rcpp::export]]
double svolApproxLL(const Map<VectorXd> y, const Map<VectorXd> thetaProposal, const Map<MatrixXd> uProposal) {
// construct particle filter object
svol_pfilter pf(thetaProposal(0), thetaProposal(1), thetaProposal(2)); // order: phi, beta, sigma
// iterate over the data
double log_like(0.0);
Eigen::Matrix<double,1,1> yt;
std::array<Eigen::Matrix<double,1,1>, NP> uStateTransition;
Eigen::Matrix<double,1,1> uResample;
for(int time = 0; time < y.rows(); ++time){
// change types of inputs
yt(0) = y(time);
for(unsigned particle = 0; particle < NP; ++particle) {
uStateTransition[particle] = uProposal.block(time,particle,1,1);
}
uResample(0) = uProposal(time,NP);
// std::cout << yt.transpose() << "\n";
// for(unsigned int i = 0; i < NP; ++i)
// std::cout << uStateTransition[i] << ", ";
// std::cout << "\n----------\n";
// update particle filter and log-likelihood
pf.filter(yt, uStateTransition, uResample);
log_like += pf.getLogCondLike();
}
//return es.eigenvalues();
return log_like;
}
// You can include R code blocks in C++ files processed with sourceCpp
// (useful for testing and development). The R code will be automatically
// run after the compilation.
/*** R
numTime <- 3
numParts <- 500 # make sure this agrees with NP
u <- matrix(rnorm(numTime*(numParts+1)), ncol = numParts+1)
params <- c(.9, 1, .1) # -1 < phi < 1, beta, sigma > 0
hist(replicate(100, svolApproxLL(rnorm(numTime), params, u)))
*/
Error in (function (command = NULL, args = character(), error_on_status = TRUE, :
System command 'R' failed, exit status: 1, stdout + stderr (last 10 lines):
E> /home/taylor/R/x86_64-pc-linux-gnu-library/4.1/RcppEigen/include/Eigen/src/Core/MatrixBase.h:48:34: required from ‘class Eigen::MatrixBase<Eigen::Matrix<double, -1, 1> >’
E> /home/taylor/R/x86_64-pc-linux-gnu-library/4.1/RcppEigen/include/Eigen/src/Core/PlainObjectBase.h:98:7: required from ‘class Eigen::PlainObjectBase<Eigen::Matrix<double, -1, 1> >’
E> /home/taylor/R/x86_64-pc-linux-gnu-library/4.1/RcppEigen/include/Eigen/src/Core/Matrix.h:178:7: required from ‘class Eigen::Matrix<double, -1, 1>’
E> /home/taylor/R/x86_64-pc-linux-gnu-library/4.1/Rcpp/include/Rcpp/InputParameter.h:77:11: required from ‘class Rcpp::ConstReferenceInputParameter<Eigen::Matrix<double, -1, 1> >’
E> RcppExports.cpp:43:74: required from here
E> /home/taylor/R/x86_64-pc-linux-gnu-library/4.1/RcppEigen/include/Eigen/src/Core/DenseCoeffsBase.h:5
Thanks for providing the URL to the repro. This error, as it happens not infrequently, appears to be one deeper down 'hidden' by devtools::document().
When I clone your repo onto a machine with 'current everything' from CRAN running Ubuntu 21.04 and doing my usual two-step of
compAttr.r # shell wrapper for Rcpp::compileAttributes
roxy.r -f # shell wrapper for roxygenize()
(using these helper scripts from my littler package),
I end in tears because your code uses C++17 statements without specifying C++17 as a compilation standard.
In other words, a vanilla bug at your end, independent of Rcpp or other tools.
edd#rob:~/git/stackoverflow/68985867/pfexamplesinr(master)$ compAttr.r
edd#rob:~/git/stackoverflow/68985867/pfexamplesinr(master)$ roxy.r -f
ℹ Loading pfexamplesinr
Exports from /home/edd/git/stackoverflow/68985867/pfexamplesinr/src/likelihoods.cpp:
double svolApproxLL(Eigen::Map<Eigen::VectorXd> y, Eigen::Map<Eigen::VectorXd> thetaProposal, Eigen::Map<Eigen::MatrixXd> uProposal)
Exports from /home/edd/git/stackoverflow/68985867/pfexamplesinr/src/rcppeigen_hello_world.cpp:
Eigen::MatrixXd rcppeigen_hello_world()
Eigen::MatrixXd rcppeigen_outerproduct(const Eigen::VectorXd& x)
double rcppeigen_innerproduct(const Eigen::VectorXd& x)
Rcpp::List rcppeigen_bothproducts(const Eigen::VectorXd& x)
Exports from /home/edd/git/stackoverflow/68985867/pfexamplesinr/src/resamplers.h:
Exports from /home/edd/git/stackoverflow/68985867/pfexamplesinr/src/rv_eval.h:
Exports from /home/edd/git/stackoverflow/68985867/pfexamplesinr/src/rv_samp.h:
/home/edd/git/stackoverflow/68985867/pfexamplesinr/src/RcppExports.cpp updated.
/home/edd/git/stackoverflow/68985867/pfexamplesinr/R/RcppExports.R updated.
Re-compiling pfexamplesinr
─ installing *source* package ‘pfexamplesinr’ ...
** using staged installation
** using staged installation
** libs
ccache g++ -std=gnu++14 -I"/usr/share/R/include" -DNDEBUG -I'/usr/local/lib/R/site-library/Rcpp/include' -I'/usr/local/lib/R/site-library/RcppEigen/include' -fpic -g -O3 -Wall -pipe -pedantic -Wno-misleading-indentation -Wno-unused -Wno-ignored-attributes -Wno-class-memaccess -c RcppExports.cpp -o RcppExports.o
ccache g++ -std=gnu++14 -I"/usr/share/R/include" -DNDEBUG -I'/usr/local/lib/R/site-library/Rcpp/include' -I'/usr/local/lib/R/site-library/RcppEigen/include' -fpic -g -O3 -Wall -pipe -pedantic -Wno-misleading-indentation -Wno-unused -Wno-ignored-attributes -Wno-class-memaccess -c likelihoods.cpp -o likelihoods.o
In file included from svol_sisr_hilb.h:8,
from likelihoods.cpp:1:
sisr_filter.h: In member function ‘void pf::filters::SISRFilter<nparts, dimx, dimy, resamp_t, float_t, debug>::filter(const osv&, const std::vector<std::function<const Eigen::Matrix<float_t, -1, -1>(const Eigen::Matrix<float_t, dimx, 1>&)> >&)’:
sisr_filter.h:229:16: warning: ‘if constexpr’ only available with ‘-std=c++17’ or ‘-std=gnu++17’
229 | if constexpr(debug)
| ^~~~~~~~~
sisr_filter.h:262:16: warning: ‘if constexpr’ only available with ‘-std=c++17’ or ‘-std=gnu++17’
262 | if constexpr(debug)
| ^~~~~~~~~
sisr_filter.h:288:16: warning: ‘if constexpr’ only available with ‘-std=c++17’ or ‘-std=gnu++17’
288 | if constexpr(debug)
| ^~~~~~~~~
sisr_filter.h:320:16: warning: ‘if constexpr’ only available with ‘-std=c++17’ or ‘-std=gnu++17’
320 | if constexpr(debug)
| ^~~~~~~~~
sisr_filter.h: In member function ‘void pf::filters::SISRFilterCRN<nparts, dimx, dimy, dimu, dimur, resamp_t, float_t, debug>::filter(const osv&, const arrayUs&, const usvr&, const std::vector<std::function<const Eigen::Matrix<float_t, -1, -1>(const Eigen::Matrix<float_t, dimx, 1>&)> >&)’:
sisr_filter.h:559:16: warning: ‘if constexpr’ only available with ‘-std=c++17’ or ‘-std=gnu++17’
559 | if constexpr(debug)
| ^~~~~~~~~
sisr_filter.h:592:16: warning: ‘if constexpr’ only available with ‘-std=c++17’ or ‘-std=gnu++17’
592 | if constexpr(debug)
| ^~~~~~~~~
sisr_filter.h:618:16: warning: ‘if constexpr’ only available with ‘-std=c++17’ or ‘-std=gnu++17’
618 | if constexpr(debug)
| ^~~~~~~~~
sisr_filter.h:650:16: warning: ‘if constexpr’ only available with ‘-std=c++17’ or ‘-std=gnu++17’
650 | if constexpr(debug)
| ^~~~~~~~~
In file included from likelihoods.cpp:2:
resamplers.h: In member function ‘void pf::resamplers::sys_hilb_resampler<nparts, dimx, num_hilb_bits, float_t>::resampLogWts(pf::resamplers::sys_hilb_resampler<nparts, dimx, num_hilb_bits, float_t>::arrayVec&, pf::resamplers::sys_hilb_resampler<nparts, dimx, num_hilb_bits, float_t>::arrayFloat&, const usvr&) [with long unsigned int nparts = 500; long unsigned int dimx = 1; long unsigned int num_hilb_bits = 5; float_t = double]’:
resamplers.h:1089:36: warning: ‘idx’ may be used uninitialized in this function [-Wmaybe-uninitialized]
1089 | tmpPartics[i] = sortedParts[idx];
| ~~~~~~~~~~~^
ccache g++ -std=gnu++14 -I"/usr/share/R/include" -DNDEBUG -I'/usr/local/lib/R/site-library/Rcpp/include' -I'/usr/local/lib/R/site-library/RcppEigen/include' -fpic -g -O3 -Wall -pipe -pedantic -Wno-misleading-indentation -Wno-unused -Wno-ignored-attributes -Wno-class-memaccess -c rcppeigen_hello_world.cpp -o rcppeigen_hello_world.o
ccache g++ -std=gnu++14 -Wl,-S -shared -L/usr/lib/R/lib -Wl,-Bsymbolic-functions -flto=auto -Wl,-z,relro -o pfexamplesinr.so RcppExports.o likelihoods.o rcppeigen_hello_world.o -L/usr/lib/R/lib -lR
installing to /tmp/devtools_install_2dc74d5a52ad61/00LOCK-pfexamplesinr/00new/pfexamplesinr/libs
** checking absolute paths in shared objects and dynamic libraries
─ DONE (pfexamplesinr)
edd#rob:~/git/stackoverflow/68985867/pfexamplesinr(master)$

collect2.exe: error: ld returned 1 exit status no DLL was created

Hello I am trying to make a package in R using Rcpp.
When I have just a single file, every thing is ok and my package(foad2) is compiled completely.
But, when I have more than one .cpp files (they do not depend on each other and each one contains only one function), I will be exposed to the following error
C:/RBuildTools/3.5/mingw_64/bin/g++ -std=gnu++11 -I"C:/PROGRA~1/R/R-36~1.3/include" -DNDEBUG -I"C:/Users/Foad/Documents/R/win-library/3.6/Rcpp/include" -O2 -Wall -mtune=core2 -c myFunc.cpp -o myFunc.o
C:/RBuildTools/3.5/mingw_64/bin/g++ -std=gnu++11 -I"C:/PROGRA~1/R/R-36~1.3/include" -DNDEBUG -I"C:/Users/Foad/Documents/R/win-library/3.6/Rcpp/include" -O2 -Wall -mtune=core2 -c rcpp_hello_world.cpp -o rcpp_hello_world.o
C:/RBuildTools/3.5/mingw_64/bin/g++ -std=gnu++11 -shared -s -static-libgcc -o foad2.dll tmp.def RcppExports.o myFunc.o rcpp_hello_world.o -LC:/PROGRA~1/R/R-36~1.3/bin/x64 -lR
RcppExports.o:RcppExports.cpp:(.rdata+0x6f8): undefined reference to `_foad2_rcpp_myFunc'
collect2.exe: error: ld returned 1 exit status
no DLL was created
ERROR: compilation failed for package 'foad2'
* removing 'C:/Users/Foad/Documents/R/win-library/3.6/foad2'
Exited with status 1.
my Cpp Codes are:
my 1st cpp file:
#include <Rcpp.h>
using namespace Rcpp;
// [[Rcpp::export]]
List rcpp_hello_world() {
CharacterVector x = CharacterVector::create( "foo", "bar" ) ;
NumericVector y = NumericVector::create( 0.0, 1.0 ) ;
List z = List::create( x, y ) ;
return z ;
}
my 2nd cpp file:
#include <Rcpp.h>
using namespace Rcpp;
// [[Rcpp::export]]
NumericVector myFunc(NumericVector x) {
return x * 2;
}
I would be greatful if anybody tell me how I can fix this problem?

C++/Fortran mixed programming: undefined reference to `_gfortran_reshape_r8'

I am compiling a C++ code together with Fortran subroutine. The C++ cpp code is like:
#include "Calculate.h"
extern "C" double SolveEq_(double *Gvalue, double *GvalueU, double *GvalueV, double *Gnodex, double *Gnodey, double *GtimeInc, double *Glfs);
template <class T1, class T2>
void Calculate(vector<Element<T1, T2> > &elm, GParameter &GeqPm, GmeshInfo &Gmesh)
{
// Solving Equation using Fortran code
SolveEq_(&Gmesh.Gvalue[0], &Gmesh.GvalueU[0], &Gmesh.GvalueV[0], &Gmesh.Gnodex[0], &Gmesh.Gnodey[0], &GeqPm.GtimeInc, &GeqPm.Glfs);
return;
}
And the Fortran code is like:
!==========================================================================
Module Inputpar
Implicit None
Integer,parameter :: Numx = 200, Numy = 200
End Module
!======================================== PROGRAM =============================================
Subroutine SolveEq(Gvalue, GvalueU, GvalueV, Gnodex, Gnodey, Deltat, Lfs);
Use Inputpar
Implicit None
Real*8 Deltat, Lfs, Dt, Su
Real*8 Gvalue(1, (Numx+1)*(Numy+1)), GvalueU(1, (Numx+1)*(Numy+1)), GvalueV(1, (Numx+1)*(Numy+1))
Real*8 Gnodex(0:Numx), Gnodey(0:Numy)
Real*8 DX, DY
Real*8 X(-3:Numx+3), Y(-3:Numy+3)
Real*8 VelX(-3:Numx+3,-3:Numy+3), VelY(-3:Numx+3,-3:Numy+3)
Real*8 G(-3:Numx+3,-3:Numy+3)
Common /CommonData/ X, Y, DX, DY, VelX, VelY, G, Dt, Su
!============================= Data Transfer ============================
Dt = Deltat
Su = Lfs
X (0:Numx) = Gnodex(0:Numx)
Y (0:Numy) = Gnodey(0:Numy)
VelX(0:Numx,0:Numy) = transpose(reshape(GvalueU,(/Numy+1,Numx+1/)))
VelY(0:Numx,0:Numy) = transpose(reshape(GvalueV,(/Numy+1,Numx+1/)))
G (0:Numx,0:Numy) = transpose(reshape(Gvalue ,(/Numy+1,Numx+1/)))
!==========Some other lines neglected here=================
End
!======================================== END PROGRAM =========================================
Firstly compile the Fortran code using command:
gfortran SolveEq.f90 -c -o SolveEq.o
And then compile the C++/Fortran codes together using makefile:
# Compiler
CC = g++
# Debug option
DEBUG = false
# Source directory of codes
SRC1 = /home
SRC2 = $(SRC1)/Resources
SRC3 = $(SRC1)/Resources/Classes
OPT=-fopenmp -O2
ifdef $(DEBUG)
PROG=test.out
else
PROG=i.out
endif
# Linker
#LNK=-I$(MPI)/include -L$(MPI)/lib -lmpich -lopa -lmpl -lpthread
OBJS = libtseutil.a Calculate.o SolveEq.o
OBJS_F=$(OBJS)
SUF_OPTS1=$(OBJS_F)
SUF_OPTS2=-I$(SRC2)/
SUF_OPTS3=-I$(SRC3)/
SUF_OPTS4=
# Details of compiling
$(PROG): $(OBJS_F)
$(CC) $(OPT) -o $# $(SUF_OPTS1)
%.o: $(SRC1)/%.cpp
$(CC) $(OPT) -c $< $(SUF_OPTS2)
%.o: $(SRC2)/%.cpp
$(CC) $(OPT) -c $< $(SUF_OPTS3)
%.o: $(SRC3)/%.cpp
$(CC) $(OPT) -c $< $(SUF_OPTS4)
# Clean
.PHONY: clean
clean:
#rm -rf *.o *.oo *.log
However, the error shows that:
g++ -fopenmp -O2 -o libtseutil.a Calculate.o SolveEq.o
Calculate.o: In function `void Calculate<CE_Tri, SolElm2d>(std::vector<Element<CE_Tri, SolElm2d>, std::allocator<Element<CE_Tri, SolElm2d> > >&, GParameter&, GmeshInfo&)':
Calculate.cpp:(.text._Z10CalculateGI6CE_Tri8SolElm2dEvRSt6vectorI7ElementIT_T0_ESaIS6_EER10GParameterR9GmeshInfo[_Z10CalculateGI6CE_Tri8SolElm2dEvRSt6vectorI7ElementIT_T0_ESaIS6_EER10GParameterR9GmeshInfo]+0x3c): undefined reference to `SolveEq_'
SolveEq.o: In function `solveeq_':
SolveEq.f90:(.text+0x2b8e): undefined reference to `_gfortran_reshape_r8'
SolveEq.f90:(.text+0x2d2a): undefined reference to `_gfortran_reshape_r8'
SolveEq.f90:(.text+0x2ec6): undefined reference to `_gfortran_reshape_r8'
SolveEq.f90:(.text+0x31fa): undefined reference to `_gfortran_reshape_r8'
collect2: error: ld returned 1 exit status
How does this happen?
I used a simple case to test the mixed compiling. The C++ code was:
#include <stdlib.h>
#include <stdio.h>
#include <iostream>
#include <vector>
using namespace std;
extern "C" double fortran_sum_(double *sum, double *su2m, double *vec, double* vec2, int *size);
int main(int argc, char ** argv)
{
int size;
double sum, sum2;
double aa,bb,cc,dd;
vector<double> vec;
vector<double> vec2;
size=2;
aa=1.0;
bb=2.0;
sum=0.0;
sum2=0.0;
vec.push_back(aa);
vec.push_back(bb);
vec2.push_back(aa*2.0);
vec2.push_back(bb*2.0);
fortran_sum_(&sum, &sum2, &vec[0], &vec2[0], &size);
cout << "Calling a Fortran function" << endl;
cout << "============================" << endl;
cout << "size = " << size << endl;
cout << "sum = " << sum << endl;
cout << "sum2 = " << sum2 << endl << endl;
}
And the Fortran code was:
Subroutine fortran_sum(gsum, gsum2, gvec, gvec2, gsize2)
integer gsize,gsize2
real*8 gvec(0:gsize2-1), gvec2(0:1)
real*8 gsum, gsum2
gsum = gvec(0)+gvec(1);
gsum2 = gvec2(0)+gvec2(1);
gsize = gsize*2;
end
Then used commands to compile :
gfortran fortran_sum.f90 -c -o fortran_sum.o
g++ fortran_sum.o call_fortran.cpp -o a.out
./a.out
It worked well:
Calling a Fortran function
============================
size = 2
sum = 3
sum2 = 6
The function _gfortran_reshape_r8 is part of the library that is used by code compiled by gfortran. When you compile in a single language such libraries are automatically linked because whichever program you use to do the linking knows about them. When you link mixed code, you need to find and explicitly put on the command line the libraries for the language that doesn't correspond to the linker you've chosen. Usually you link with the C++ syntax, as you've done here, and add the fortran compiler's libraries explicitly.
I am little bit weak on Fortran language. When I compiled your fortran code and put it through nm, it gave me the following. There is no symbol "SolveEq_". There is just "solveeq_".
0000000000000020 r A.15.3480
0000000000000000 r A.3.3436
0000000000000010 r A.9.3463
U _gfortran_reshape_r8
00000000000fbe28 C commondata_
U free
U malloc
0000000000000000 T solveeq_
Edit:
It compiled for me when I used "solveeq_". Here is simplified code for demo (main.cpp):
extern "C" double solveeq_(
double *Gvalue, double *GvalueU,
double *GvalueV, double *Gnodex,
double *Gnodey, double *GtimeInc, double *Glfs
);
template <typename T>
void Calculate(T *one, T *two, T *three,
T *four, T *five, T *six, T *seven) {
solveeq_(one,two,three,four,five,six,seven);
}
int main(int argc, char ** argv) {
double one,two,three,four,five,six,seven;
Calculate<double>(&one,&two,&three,&four,&five,&six,&seven);
}
Compiled it as (f.f90 has the fortran code):
gfortran -c f.f90
g++ f.o main.cpp -lgfortran
It seems, since 2003, if you want to call a fortran function from C/C++, you could use BIND (It may not with fortran/fortran though without some more additional effort).
Subroutine SolveEq(F) BIND(C,NAME="SolveMyEquation")
Real Gvalue, GvalueU, GvalueV, Gnodex, Gnodey, Deltat, Lfs
End
Thanks to every one, especially #Brick and #blackpen.
The problem has been fixed.
1), Add -lgfortran into command line so that the function,otherwise it will show: undefined reference to `_gfortran_reshape_r8'.
2), Change the name of the .f90 function into small letters "solveeq" other wise it will show: undefined reference to `SolveGeq_'
So finally my .cpp is changed into :
#include "Calculate.h"
extern "C" void solveeq_(double *Gvalue, double *GvalueU, double *GvalueV, double *Gnodex, double *Gnodey, double *GtimeInc, double *Glfs);
template <class T1, class T2>
void Calculate(vector<Element<T1, T2> > &elm, GParameter &GeqPm, GmeshInfo &Gmesh)
{
// Solving Equation using Fortran code
solveeq_(&Gmesh.Gvalue[0], &Gmesh.GvalueU[0], &Gmesh.GvalueV[0], &Gmesh.Gnodex[0], &Gmesh.Gnodey[0], &GeqPm.GtimeInc, &GeqPm.Glfs);
return;
}
The fortran code .f90 is like:
!==========================================================================
Module Inputpar
Implicit None
Integer,parameter :: Numx = 200, Numy = 200
End Module
!======================================== PROGRAM =============================================
Subroutine SolveEq(Gvalue, GvalueU, GvalueV, Gnodex, Gnodey, Deltat, Lfs);
Use Inputpar
Implicit None
Real*8 Deltat, Lfs, Dt, Su
Real*8 Gvalue(1, (Numx+1)*(Numy+1)), GvalueU(1, (Numx+1)*(Numy+1)), GvalueV(1, (Numx+1)*(Numy+1))
Real*8 Gnodex(0:Numx), Gnodey(0:Numy)
Real*8 DX, DY
Real*8 X(-3:Numx+3), Y(-3:Numy+3)
Real*8 VelX(-3:Numx+3,-3:Numy+3), VelY(-3:Numx+3,-3:Numy+3)
Real*8 G(-3:Numx+3,-3:Numy+3)
Common /CommonData/ X, Y, DX, DY, VelX, VelY, G, Dt, Su
!============================= Data Transfer ============================
Dt = Deltat
Su = Lfs
X (0:Numx) = Gnodex(0:Numx)
Y (0:Numy) = Gnodey(0:Numy)
VelX(0:Numx,0:Numy) = transpose(reshape(GvalueU,(/Numy+1,Numx+1/)))
VelY(0:Numx,0:Numy) = transpose(reshape(GvalueV,(/Numy+1,Numx+1/)))
G (0:Numx,0:Numy) = transpose(reshape(Gvalue ,(/Numy+1,Numx+1/)))
!==========Some other lines neglected here=================
End
!======================================== END PROGRAM =========================================
And the makefile is like:
# Compiler
CC = g++
# Debug option
DEBUG = false
# Source directory of codes
SRC1 = /home
SRC2 = $(SRC1)/Resources
SRC3 = $(SRC1)/Resources/Classes
OPT=-fopenmp -O2
ifdef $(DEBUG)
PROG=test.out
else
PROG=i.out
endif
# Linker
#LNK=-I$(MPI)/include -L$(MPI)/lib -lmpich -lopa -lmpl -lpthread
OBJS = libtseutil.a Calculate.o solveeq.o
OBJS_F=$(OBJS)
SUF_OPTS1=$(OBJS_F)
SUF_OPTS2=-I$(SRC2)/
SUF_OPTS3=-I$(SRC3)/
SUF_OPTS4=
# Details of compiling
$(PROG): $(OBJS_F)
$(CC) $(OPT) -o $# $(SUF_OPTS1)
%.o: $(SRC1)/%.cpp
$(CC) $(OPT) -c $< $(SUF_OPTS2)
%.o: $(SRC2)/%.cpp
$(CC) $(OPT) -c $< $(SUF_OPTS3)
%.o: $(SRC3)/%.cpp
$(CC) $(OPT) -c $< $(SUF_OPTS4)
solveeq.o: $(SRC1)/solveeq.f90
gfortran -c $<
# Clean
.PHONY: clean
clean:
#rm -rf *.o *.oo *.log

C++ and R: Create a .so or .dll

I have doubt. I know that it is possible to call from a cppfile an Rfunction.
But it is possible to compile that cppfile (that has an R function inside -from a package, for example caret-) into a .soor dll?
If it is possible. How does having a chuck of Rcode inside works. Does the compiled code calls the R interpreter before it compiles or it does not need to?
Thanks in advance
Yes, you can embed R inside of C or C++ application. The API is old, stable, somewhat documented and a little unwieldy. But it can be done.
Or you just use RInside which does everything for you -- and comes with eight (8) different example subdirectories containing dozens of worked and working examples. Here is (the core) of one (which is a little old, we may write it tighter now):
#include <RInside.h> // for the embedded R via RInside
#include <iomanip>
int main(int argc, char *argv[]) {
RInside R(argc, argv); // create an embedded R instance
std::string txt = // load library, run regression, create summary
"suppressMessages(require(stats));"
"swisssum <- summary(lm(Fertility ~ . , data = swiss));"
"print(swisssum)";
R.parseEvalQ(txt); // eval command, no return
// evaluate R expressions, and assign directly into Rcpp types
Rcpp::NumericMatrix M( (SEXP) R.parseEval("swcoef <- coef(swisssum)"));
Rcpp::StringVector cnames( (SEXP) R.parseEval("colnames(swcoef)"));
Rcpp::StringVector rnames( (SEXP) R.parseEval("rownames(swcoef)"));
std::cout << "\n\nAnd now from C++\n\n\t\t\t";
for (int i=0; i<cnames.size(); i++) {
std::cout << std::setw(11) << cnames[i] << "\t";
}
std::cout << std::endl;
for (int i=0; i<rnames.size(); i++) {
std::cout << std::setw(16) << rnames[i] << "\t";
for (int j=0; j<cnames.size(); j++) {
std::cout << std::setw(11) << M(i,j) << "\t";
}
std::cout << std::endl;
}
std::cout << std::endl;
exit(0);
}
Then:
edd#max:~/git/rinside/inst/examples/standard(master)$ make rinside_sample3
ccache g++ -I/usr/share/R/include -I/usr/local/lib/R/site-library/Rcpp/include -I/usr/local/lib/R/site-library/RInside/include -g -O2 -fstack-protector-strong -Wformat -Werror=format-security -D_FORTIFY_SOURCE=2 -g -O3 -Wall -pipe -Wno-unused -pedantic -Wall rinside_sample3.cpp -Wl,--export-dynamic -fopenmp -L/usr/lib/R/lib -lR -lpcre -llzma -lbz2 -lz -lrt -ldl -lm -lblas -llapack -L/usr/local/lib/R/site-library/RInside/lib -lRInside -Wl,-rpath,/usr/local/lib/R/site-library/RInside/lib -o rinside_sample3
edd#max:~/git/rinside/inst/examples/standard(master)$ ./rinside_sample3
Call:
lm(formula = Fertility ~ ., data = swiss)
Residuals:
Min 1Q Median 3Q Max
-15.2743 -5.2617 0.5032 4.1198 15.3213
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 66.91518 10.70604 6.250 1.91e-07 ***
Agriculture -0.17211 0.07030 -2.448 0.01873 *
Examination -0.25801 0.25388 -1.016 0.31546
Education -0.87094 0.18303 -4.758 2.43e-05 ***
Catholic 0.10412 0.03526 2.953 0.00519 **
Infant.Mortality 1.07705 0.38172 2.822 0.00734 **
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Residual standard error: 7.165 on 41 degrees of freedom
Multiple R-squared: 0.7067, Adjusted R-squared: 0.671
F-statistic: 19.76 on 5 and 41 DF, p-value: 5.594e-10
And now from C++
Estimate Std. Error t value Pr(>|t|)
(Intercept) 66.9152 10.706 6.25023 1.90605e-07
Agriculture -0.172114 0.0703039 -2.44814 0.0187272
Examination -0.258008 0.253878 -1.01627 0.315462
Education -0.87094 0.183029 -4.75849 2.4306e-05
Catholic 0.104115 0.0352579 2.95297 0.00519008
Infant.Mortality 1.07705 0.38172 2.82157 0.00733572
edd#max:~/git/rinside/inst/examples/standard(master)$
which shows that
yes we can use R from C++
yes we can have R report the result
yes we can get them back to C++ too
As I said, there are dozens more examples. And no, it doesn't just magically compile R into your executable -- you need R installed and its shared libraries are called.

"Bad" GCC optimization performance

I am trying to understand why using -O2 -march=native with GCC gives a slower code than without using them.
Note that I am using MinGW (GCC 4.7.1) under Windows 7.
Here is my code :
struct.hpp :
#ifndef STRUCT_HPP
#define STRUCT_HPP
#include <iostream>
class Figure
{
public:
Figure(char *pName);
virtual ~Figure();
char *GetName();
double GetArea_mm2(int factor);
private:
char name[64];
virtual double GetAreaEx_mm2() = 0;
};
class Disk : public Figure
{
public:
Disk(char *pName, double radius_mm);
~Disk();
private:
double radius_mm;
virtual double GetAreaEx_mm2();
};
class Square : public Figure
{
public:
Square(char *pName, double side_mm);
~Square();
private:
double side_mm;
virtual double GetAreaEx_mm2();
};
#endif
struct.cpp :
#include <cstdio>
#include "struct.hpp"
Figure::Figure(char *pName)
{
sprintf(name, pName);
}
Figure::~Figure()
{
}
char *Figure::GetName()
{
return name;
}
double Figure::GetArea_mm2(int factor)
{
return (double)factor*GetAreaEx_mm2();
}
Disk::Disk(char *pName, double radius_mm_) :
Figure(pName), radius_mm(radius_mm_)
{
}
Disk::~Disk()
{
}
double Disk::GetAreaEx_mm2()
{
return 3.1415926*radius_mm*radius_mm;
}
Square::Square(char *pName, double side_mm_) :
Figure(pName), side_mm(side_mm_)
{
}
Square::~Square()
{
}
double Square::GetAreaEx_mm2()
{
return side_mm*side_mm;
}
main.cpp
#include <iostream>
#include <cstdio>
#include "struct.hpp"
double Do(int n)
{
double sum_mm2 = 0.0;
const int figuresCount = 10000;
Figure **pFigures = new Figure*[figuresCount];
for (int i = 0; i < figuresCount; ++i)
{
if (i % 2)
pFigures[i] = new Disk((char *)"-Disque", i);
else
pFigures[i] = new Square((char *)"-Carré", i);
}
for (int a = 0; a < n; ++a)
{
for (int i = 0; i < figuresCount; ++i)
{
sum_mm2 += pFigures[i]->GetArea_mm2(i);
sum_mm2 += (double)(pFigures[i]->GetName()[0] - '-');
}
}
for (int i = 0; i < figuresCount; ++i)
delete pFigures[i];
delete[] pFigures;
return sum_mm2;
}
int main()
{
double a = 0;
StartChrono(); // home made lib, working fine
a = Do(10000);
double elapsedTime_ms = StopChrono();
std::cout << "Elapsed time : " << elapsedTime_ms << " ms" << std::endl;
return (int)a % 2; // To force the optimizer to keep the Do() call
}
I compile this code twice :
1 : Without optimization
mingw32-g++.exe -Wall -fexceptions -std=c++11 -c main.cpp -o main.o
mingw32-g++.exe -Wall -fexceptions -std=c++11 -c struct.cpp -o struct.o
mingw32-g++.exe -o program.exe main.o struct.o -s
2 : With -O2 optimization
mingw32-g++.exe -Wall -fexceptions -O2 -march=native -std=c++11 -c main.cpp -o main.o
mingw32-g++.exe -Wall -fexceptions -O2 -march=native -std=c++11 -c struct.cpp -o struct.o
mingw32-g++.exe -o program.exe main.o struct.o -s
1 : Execution time :
1196 ms (1269 ms with Visual Studio 2013)
2 : Execution time :
1569 ms (403 ms with Visual Studio 2013) !!!!!!!!!!!!!
Using -O3 instead of -O2 does not improve the results.
I was, and I still am, pretty convinced that GCC and Visual Studio are equivalents, so I don't understand this huge difference.
Plus, I don't understand why the optimized version is slower than the non-optimized version with GCC.
Do I miss something here ?
(Note that I had the same problem with genuine GCC 4.8.2 on Ubuntu)
Thanks for your help
Considering that I don't see the assembly code, I'm going to speculate the following :
The allocation loop can be optimized (by the compiler) by removing the if clause and causing the following :
for (int i=0;i <10000 ; i+=2)
{
pFigures[i] = new Square(...);
}
for (int i=1;i <10000 ; i +=2)
{
pFigures[i] = new Disk(...);
}
Considering that the end condition is a multiple of 4 , it can be even more "efficient"
for (int i=0;i < 10000 ;i+=2*4)
{
pFigures[i] = ...
pFigures[i+2] = ...
pFigures[i+4] = ...
pFigures[i+6] = ...
}
Memory wise this will make Disks to be allocated 4 by 4 an Squares 4 by 4 .
Now, this means they will be found in the memory next to each other.
Next, you are going to iterate the vector 10000 times in a normal order (by normal i mean index after index).
Think about the places where these shapes are allocated in memory.You will end up having 4 times more cache misses (think about the border example, when 4 disks and 4 squares are found in different pages, you will switch between the pages 8 times... in a normal case scenario you would switch between the pages only once).
This sort of optimization (if done by the compiler, and in your particular code) optimizes the time for Allocation , but not the time of access (which in your example is the biggest load).
Test this by removing the i%2 and see what results you get.
Again this is pure speculation, and it assumes that the reason for lower performance was a loop optimization.
I suspect that you've got an issue unique to the combination of mingw/gcc/glibc on Windows because your code performs faster with optimizations on Linux where gcc is altogether more 'at home'.
On a fairly pedestrian Linux VM using gcc 4.8.2:
$ g++ main.cpp struct.cpp
$ time a.out
real 0m2.981s
user 0m2.876s
sys 0m0.079s
$ g++ -O2 main.cpp struct.cpp
$ time a.out
real 0m1.629s
user 0m1.523s
sys 0m0.041s
...and if you really take the blinkers off the optimizer by deleting struct.cpp and moving the implementation all inline:
$ time a.out
real 0m0.550s
user 0m0.543s
sys 0m0.000s