Skip to content

[Rcpp-devel] conflict between R_ext/Applic.h and RcppArmadillo.h?

5 messages · Dirk Eddelbuettel, Richard Chandler

#
Hi,

I am trying to call Rdqags (from R_ext/Applic.h) in a C++ program that
uses RcppArmadillo. Everything seems to work fine except the compiler
warns about conflicts. Is there a way to avoid these conflicts? Could
they cause any unforeseen problems? Below is a self-contained example
illustrating the problem. Note that RcppArmadillo is not actually
required in the example, but it is in my program. See below for
sessionInfo().

Thanks,
Richard


library(RcppArmadillo)
library(inline)

# Integrand
inc <- "
#include <R_ext/Applic.h>
void f(double *x, int n, void *ex) {
  double *v;
  v = (double*)ex;
  double sigma = v[0];
  for(int i=0; i<n; i++) {
    x[i] = exp(-(x[i]*x[i]) / (2*sigma*sigma));
  }
}
"

# Compute integral
f <- cxxfunction(signature(sigma_="numeric",
                           lower_="numeric", upper_="numeric"),
  "
    double sigma = as<double>(sigma_);
    double lower = as<double>(lower_);
    double upper = as<double>(upper_);
    void *ex;
    ex = &sigma;
    double epsabs = 0.001;
    double epsrel = 0.001;
    double result = 0.0;
    double abserr = 0.0;
    int neval = 0;
    int ier = 0;
    int limit = 50;
    int lenw = 200;
    int last = 0;
    int iwork = 50;
    double work = 200.0;
    Rdqags(f, ex, &lower, &upper, &epsabs, &epsrel, &result, &abserr,
           &neval, &ier, &limit, &lenw, &last, &iwork, &work);
    return Rcpp::List::create(result, abserr, last, ier);
  ", plugin="RcppArmadillo", includes=inc)

fr <- function(x, sigma=1) exp(-x^2 / (2*sigma*sigma))

sig <- 10
f(sig, 0, 2)
str(integrate(fr, 0, 2, sigma=sig))
R version 2.14.1 (2011-12-22)
Platform: i386-pc-mingw32/i386 (32-bit)

locale:
[1] LC_COLLATE=English_United States.1252
[2] LC_CTYPE=English_United States.1252
[3] LC_MONETARY=English_United States.1252
[4] LC_NUMERIC=C
[5] LC_TIME=English_United States.1252

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base

other attached packages:
[1] inline_0.3.8         unmarked_0.9-6       RcppArmadillo_0.2.34
[4] Rcpp_0.9.9           lattice_0.20-0       reshape_0.8.4
[7] plyr_1.7.1

loaded via a namespace (and not attached):
[1] grid_2.14.1  tools_2.14.1
#
Hi Richard,
On 1 March 2012 at 16:25, Richard Chandler wrote:
| Hi,
| 
| I am trying to call Rdqags (from R_ext/Applic.h) in a C++ program that
| uses RcppArmadillo. Everything seems to work fine except the compiler
| warns about conflicts. Is there a way to avoid these conflicts? Could
| they cause any unforeseen problems? Below is a self-contained example
| illustrating the problem. Note that RcppArmadillo is not actually
| required in the example, but it is in my program. See below for
| sessionInfo().

Hm, that's interesting.   I haven't used R_ext/Applic.h much; it says

    /* This header file contains routines which are in the R API and ones which
   are not.

   Those which are not can be used only at the user's risk and may change
   or disappear in a future release of R.
   */

so in a way that gives access from C to the R API.  Not surprising that it
bites with what we have.

Rcpp and RcppArmadillo have a moderately involved hierarchy of headers, we
generally do not recommend mixing or changing.

So the simplest suggestion I have ... is to 'split' things. Have your
RcppArmadillo code, have it call a C function ... defined over in another
source file and header which just includes R_ext/Applic.h and communicates
with the other side via standard C (or C++ / STL types).  Makes sense?

Let us know how that goes.  

Dirk


| Thanks,
| Richard
| 
| 
| library(RcppArmadillo)
| library(inline)
| 
| # Integrand
| inc <- "
| #include <R_ext/Applic.h>
| void f(double *x, int n, void *ex) {
|   double *v;
|   v = (double*)ex;
|   double sigma = v[0];
|   for(int i=0; i<n; i++) {
|     x[i] = exp(-(x[i]*x[i]) / (2*sigma*sigma));
|   }
| }
| "
| 
| # Compute integral
| f <- cxxfunction(signature(sigma_="numeric",
|                            lower_="numeric", upper_="numeric"),
|   "
|     double sigma = as<double>(sigma_);
|     double lower = as<double>(lower_);
|     double upper = as<double>(upper_);
|     void *ex;
|     ex = &sigma;
|     double epsabs = 0.001;
|     double epsrel = 0.001;
|     double result = 0.0;
|     double abserr = 0.0;
|     int neval = 0;
|     int ier = 0;
|     int limit = 50;
|     int lenw = 200;
|     int last = 0;
|     int iwork = 50;
|     double work = 200.0;
|     Rdqags(f, ex, &lower, &upper, &epsabs, &epsrel, &result, &abserr,
|            &neval, &ier, &limit, &lenw, &last, &iwork, &work);
|     return Rcpp::List::create(result, abserr, last, ier);
|   ", plugin="RcppArmadillo", includes=inc)
| 
| fr <- function(x, sigma=1) exp(-x^2 / (2*sigma*sigma))
| 
| sig <- 10
| f(sig, 0, 2)
| str(integrate(fr, 0, 2, sigma=sig))
| 
| 
| 
| 
| 
| > sessionInfo()
| R version 2.14.1 (2011-12-22)
| Platform: i386-pc-mingw32/i386 (32-bit)
| 
| locale:
| [1] LC_COLLATE=English_United States.1252
| [2] LC_CTYPE=English_United States.1252
| [3] LC_MONETARY=English_United States.1252
| [4] LC_NUMERIC=C
| [5] LC_TIME=English_United States.1252
| 
| attached base packages:
| [1] stats     graphics  grDevices utils     datasets  methods   base
| 
| other attached packages:
| [1] inline_0.3.8         unmarked_0.9-6       RcppArmadillo_0.2.34
| [4] Rcpp_0.9.9           lattice_0.20-0       reshape_0.8.4
| [7] plyr_1.7.1
| 
| loaded via a namespace (and not attached):
| [1] grid_2.14.1  tools_2.14.1
| _______________________________________________
| Rcpp-devel mailing list
| Rcpp-devel at lists.r-forge.r-project.org
| https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel
#
Richard,
On 1 March 2012 at 15:36, Dirk Eddelbuettel wrote:
| 
| Hi Richard,
|
| On 1 March 2012 at 16:25, Richard Chandler wrote:
| | Hi,
| | 
| | I am trying to call Rdqags (from R_ext/Applic.h) in a C++ program that
| | uses RcppArmadillo. Everything seems to work fine except the compiler
| | warns about conflicts. Is there a way to avoid these conflicts? Could
| | they cause any unforeseen problems? Below is a self-contained example
| | illustrating the problem. Note that RcppArmadillo is not actually
| | required in the example, but it is in my program. See below for
| | sessionInfo().

Another idea would be to dig up some old posts on the about how Rcpp lets you
get R API access points directly.  Davor used that to get to loess.  That way
you would get to use Rdqags but without needing the conflicting header.  Just
another thought.

Dirk
#
Hi Dirk,

Thanks for those suggestions. I am trying them both but without
success so far. I will keep at it, but first, I wonder how serious
these warnings are? I can still pass R CMD check, and my program seems
to work very well. Could something disastrous happen if I ignore the
warnings? Do you think CRAN would accept the package?

Richard
On Thu, Mar 1, 2012 at 5:35 PM, Dirk Eddelbuettel <edd at debian.org> wrote:
1 day later
#
Hi Richard.
On 2 March 2012 at 10:32, Richard Chandler wrote:
| Hi Dirk,
| 
| Thanks for those suggestions. I am trying them both but without
| success so far. I will keep at it, but first, I wonder how serious
| these warnings are? I can still pass R CMD check, and my program seems

You never showed them to us, so we can't tell.

| to work very well. Could something disastrous happen if I ignore the
| warnings? Do you think CRAN would accept the package?

For CRAN you probably want a package framework anyway rather than an inline
call.  And what R CMD check says matters -- which is a superset of compiler
warnings plus a boatload or two of other tests.  Which are sometimes a pain,
but beneficial to all of us in the long run.

Dirk
 
| Richard
|
| On Thu, Mar 1, 2012 at 5:35 PM, Dirk Eddelbuettel <edd at debian.org> wrote:
| >
| > Richard,
| >
| > On 1 March 2012 at 15:36, Dirk Eddelbuettel wrote:
| > |
| > | Hi Richard,
| > |
| > | On 1 March 2012 at 16:25, Richard Chandler wrote:
| > | | Hi,
| > | |
| > | | I am trying to call Rdqags (from R_ext/Applic.h) in a C++ program that
| > | | uses RcppArmadillo. Everything seems to work fine except the compiler
| > | | warns about conflicts. Is there a way to avoid these conflicts? Could
| > | | they cause any unforeseen problems? Below is a self-contained example
| > | | illustrating the problem. Note that RcppArmadillo is not actually
| > | | required in the example, but it is in my program. See below for
| > | | sessionInfo().
| >
| > Another idea would be to dig up some old posts on the about how Rcpp lets you
| > get R API access points directly. ?Davor used that to get to loess. ?That way
| > you would get to use Rdqags but without needing the conflicting header. ?Just
| > another thought.
| >
| > Dirk
| >
| > --
| > "Outside of a dog, a book is a man's best friend. Inside of a dog, it is too
| > dark to read." -- Groucho Marx