[Rcpp-devel] Eigen-like pointer mappings in Rcpp?
Hi Gang: I am working with some C++ code that uses pointers. I know I could change the code. But, I was wondering if there is an easy way to map the double pointer into the R data via Rcpp. I have been using RcppEigen to do that. For example... RcppExport SEXP myfunc(SEXP _n, SEXP _x) { const int n=Rcpp::as(_n); double *x=(double *)malloc(n*sizeof(double)); Eigen::Map map_x(x, n); map_x=Rcpp::as< Eigen::Map >(_x); double a=fit(x, n); ... It seems to me there should be an easier way like ... RcppExport SEXP myfunc(SEXP _n, SEXP _x) { const int n=Rcpp::as(_n); double *x=Rcpp::as(_x); // or a const-correct version double a=fit(x, n); ... Thanks, Rodney ___ Rcpp-devel mailing list Rcpp-devel@lists.r-forge.r-project.org https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel
Re: [Rcpp-devel] Eigen-like pointer mappings in Rcpp?
Hello Rodney, If all you need is to pass a pointer to the fit() function (assuming fit() does not modify x) and get the result a, you do not need to make a copy. Simply wrap _x by a NumericVector and call the data() method. Rcpp::NumericVector x(_x); double a = fit(x.data(), n); You can even directly use the R API without creating an Rcpp object. double a = fit(REAL(_x), n); Best, Yixuan 2015-02-17 14:53 GMT-05:00 Sparapani, Rodney : > Hi Gang: > > I am working with some C++ code that uses pointers. I know I could > change the code. But, I was wondering if there is an easy way to > map the double pointer into the R data via Rcpp. I have been using > RcppEigen to do that. For example... > > RcppExport SEXP myfunc(SEXP _n, SEXP _x) { >const int n=Rcpp::as(_n); > >double *x=(double *)malloc(n*sizeof(double)); > >Eigen::Map map_x(x, n); > >map_x=Rcpp::as< Eigen::Map >(_x); > >double a=fit(x, n); > > ... > > It seems to me there should be an easier way like ... > > RcppExport SEXP myfunc(SEXP _n, SEXP _x) { >const int n=Rcpp::as(_n); > >double *x=Rcpp::as(_x); // or a const-correct version > >double a=fit(x, n); > > ... > > Thanks, > > Rodney > > > ___ > Rcpp-devel mailing list > Rcpp-devel@lists.r-forge.r-project.org > https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel > -- Yixuan Qiu Department of Statistics, Purdue University ___ Rcpp-devel mailing list Rcpp-devel@lists.r-forge.r-project.org https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel
Re: [Rcpp-devel] Eigen-like pointer mappings in Rcpp?
On Tue, 2015-02-17 at 15:57 -0500, Yixuan Qiu wrote: > Hello Rodney, > > If all you need is to pass a pointer to the fit() function (assuming > fit() does not modify x) and get the result a, you do not need to make > a copy. Simply wrap _x by a NumericVector and call the data() method. > > Rcpp::NumericVector x(_x); > > double a = fit(x.data(), n); > > You can even directly use the R API without creating an Rcpp object. > > double a = fit(REAL(_x), n); > > > Best, > > Yixuan Hi Yixuan: That's exactly what I wanted! However, I can't seem to find that method at http://dirk.eddelbuettel.com/code/rcpp/html Can you please point me to it? >From this... Rcpp::NumericVector x(_x); double a = fit(x.data(), n); I get... error: class Rcpp::NumericVector has no member named data Am I doing something dumb? I'm using Rcpp 0.10.3 w/ 2.15.3 R and GCC 4.4.7 if it matters. Thanks, Rodney ___ Rcpp-devel mailing list Rcpp-devel@lists.r-forge.r-project.org https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel
Re: [Rcpp-devel] Eigen-like pointer mappings in Rcpp?
On 17 February 2015 at 23:12, Sparapani, Rodney wrote: | That's exactly what I wanted! However, I can't seem to find that method | at http://dirk.eddelbuettel.com/code/rcpp/html The documentation is for version 0.11.4. | I'm using Rcpp 0.10.3 w/ 2.15.3 R and GCC 4.4.7 if it matters. Ouch. You could "grow your own" and use the Doxyfile in the repository to locally re-create this documentation for the (much) older and (more than somewhat) outdated version if you (poor soul) are in fact constrained to use it. Dirk -- http://dirk.eddelbuettel.com | @eddelbuettel | e...@debian.org ___ Rcpp-devel mailing list Rcpp-devel@lists.r-forge.r-project.org https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel
Re: [Rcpp-devel] Eigen-like pointer mappings in Rcpp?
Oops sorry, should be begin(). data() is the Eigen method. Best, Yixuan On Feb 17, 2015 6:12 PM, "Sparapani, Rodney" wrote: > On Tue, 2015-02-17 at 15:57 -0500, Yixuan Qiu wrote: > > Hello Rodney, > > > > If all you need is to pass a pointer to the fit() function (assuming > > fit() does not modify x) and get the result a, you do not need to make > > a copy. Simply wrap _x by a NumericVector and call the data() method. > > > > Rcpp::NumericVector x(_x); > > > > double a = fit(x.data(), n); > > > > You can even directly use the R API without creating an Rcpp object. > > > > double a = fit(REAL(_x), n); > > > > > > Best, > > > > Yixuan > > Hi Yixuan: > > That's exactly what I wanted! However, I can't seem to find that method > at http://dirk.eddelbuettel.com/code/rcpp/html > > Can you please point me to it? > > From this... > > Rcpp::NumericVector x(_x); > > double a = fit(x.data(), n); > > I get... > > error: class Rcpp::NumericVector has no member named data > > Am I doing something dumb? > > I'm using Rcpp 0.10.3 w/ 2.15.3 R and GCC 4.4.7 if it matters. > > Thanks, > > Rodney > > ___ Rcpp-devel mailing list Rcpp-devel@lists.r-forge.r-project.org https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel
[Rcpp-devel] assert() for Rcpp?
Dirk Eddelbuettel and I were discussing how people do not seem to be putting assert() statements in their C code in Rcpp packages. I expect the reason is because assert() prints to cerr, which is a violation of the CRAN policies of proper packages. However, the assert() command is a simple macro, and we could tweak it so it is compliant with CRAN standards. Below, I have an example of what might be included. Dirk suggested that this idea be posted to this list to gather peoples insights and thoughts (and to catch any memory management issues, for example, that might exist with the code below). The traditional assert() is a macro that calls a function __assert() which in turn seems fairly simple, but the C program for Rcpp should not just abort but instead throw an error, I think. (The macro allows for detection of line numbers in the code, and also allows for a NDEBUG flag to omit all asserts for efficient code.) The proposed code: #include #ifdef NDEBUG # define assert(EX) #else # define assert(EX) (void)((EX) || (__assert (#EX, __FILE__, __LINE__),0)) #endif void __assert (const char *msg, const char *file, int line) { char buffer [100]; snprintf( buffer, 100, "Assert Failure: %s at %s line #%d", msg, file, line ); ::Rf_error( buffer ); } Anyway, I would love to hear people¹s thoughts on this. I found assert() useful in wrapping an existing spaghetti code base with an R package; it seems like a nice tool to provide enhanced ability to track down bugs. I am currently using the above in my package Œtextreg¹ and it appears to work great. Sincerely, Luke Miratrix Assistant Professor of Statistics Note: Due to my RSI (wrist trouble), e-mail often abrupt. -- Department of Statistics Science Center Harvard University 1 Oxford Street Cambridge MA 02138-2901 lmirat...@stat.harvard.edu 510-735-7635 ___ Rcpp-devel mailing list Rcpp-devel@lists.r-forge.r-project.org https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel
Re: [Rcpp-devel] assert() for Rcpp?
I agree that having an assert which complies with CRAN standards would be valuable. One piece of immediate feedback on your initial implementation: you can't call Rf_error from C++ code (as it will bypass C++ destructors on the stack). Rather, you should throw Rcpp::exception. Whether assert should throw is another issue entirely. The traditional semantics of assert don't give it control flow behavior (either do nothing in release mode or blow the process up in debug mode) so I'm not sure whether we'd want to introduce a variation of it that does have control flow. A couple of options: (1) Create another function e.g. "verify" with the semantics you suggest (with the different name not fooling people into thinking it has traditional assert semantics) (2) Call it assert but have it call Rcpp::warning rather than yielding an error. On Tue, Feb 17, 2015 at 5:41 PM, Miratrix, Luke wrote: > > Dirk Eddelbuettel and I were discussing how people do not seem to be > putting assert() statements in their C code in Rcpp packages. I expect > the reason is because assert() prints to cerr, which is a violation of the > CRAN policies of proper packages. However, the assert() command is a > simple macro, and we could tweak it so it is compliant with CRAN > standards. Below, I have an example of what might be included. Dirk > suggested that this idea be posted to this list to gather peoples insights > and thoughts (and to catch any memory management issues, for example, that > might exist with the code below). > > The traditional assert() is a macro that calls a function __assert() which > in turn seems fairly simple, but the C program for Rcpp should not just > abort but instead throw an error, I think. (The macro allows for > detection of line numbers in the code, and also allows for a NDEBUG flag > to omit all asserts for efficient code.) > > The proposed code: > > #include > > #ifdef NDEBUG > # define assert(EX) > #else > # define assert(EX) (void)((EX) || (__assert (#EX, __FILE__, __LINE__),0)) > #endif > > void __assert (const char *msg, const char *file, int line) { > char buffer [100]; > snprintf( buffer, 100, "Assert Failure: %s at %s line #%d", msg, file, > line ); > ::Rf_error( buffer ); > } > > > > Anyway, I would love to hear people¹s thoughts on this. I found assert() > useful in wrapping an existing spaghetti code base with an R package; it > seems like a nice tool to provide enhanced ability to track down bugs. I > am currently using the above in my package Œtextreg¹ and it appears to > work great. > > > > Sincerely, > > Luke Miratrix > Assistant Professor of Statistics > > Note: Due to my RSI (wrist trouble), e-mail often abrupt. > > > -- > > Department of Statistics > Science Center > > Harvard University > 1 Oxford Street > Cambridge MA 02138-2901 > > > lmirat...@stat.harvard.edu > 510-735-7635 > > > > > ___ > Rcpp-devel mailing list > Rcpp-devel@lists.r-forge.r-project.org > https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel ___ Rcpp-devel mailing list Rcpp-devel@lists.r-forge.r-project.org https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel
Re: [Rcpp-devel] Eigen-like pointer mappings in Rcpp?
On 17 February 2015 at 18:31, Yixuan Qiu wrote: | Oops sorry, should be begin(). data() is the Eigen method. Ah, indeed. And begin() has been there for a lng time. And we have shown the trick of initializing with it several dozen times too -- all the early RcppArmadillo docs show it. Dirk -- http://dirk.eddelbuettel.com | @eddelbuettel | e...@debian.org ___ Rcpp-devel mailing list Rcpp-devel@lists.r-forge.r-project.org https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel