Re: [Rcpp-devel] Specify Rcpp header location in Makevars

2015-07-29 Thread Romain Francois
You need 

LinkingTo: Rcpp

As per the current documentation. 

Romain

Envoyé de mon iPhone

 Le 29 juil. 2015 à 17:19, jsmith5...@yahoo.com jsmith5...@yahoo.com a 
 écrit :
 
 Hi,
 
 Is there a way to automatically specify the location of the Rcpp include
 directory (for Rcpp.h) in Makevars?
 
 In my Makevars, I use the following compile command:
 
 %.o: %.cpp $(cpp_sources)
$(CXX) $(PKG_CXXFLAGS) $
 
 
 I also specified Imports: Rcpp in DESCRIPTION, but it does not appear
 to automatically add the right flags to the compile command.
 
 I am looking for something that could work similarly to Rscript -e
 Rcpp:::LdFlags(), which used to export the Rcpp library flags.
 
 Any ideas are greatly appreciated!
 
 Thanks!
 Lutz
 ___
 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

[Rcpp-devel] Specify Rcpp header location in Makevars

2015-07-29 Thread jsmith5...@yahoo.com
Hi,

Is there a way to automatically specify the location of the Rcpp include
directory (for Rcpp.h) in Makevars?

In my Makevars, I use the following compile command:

%.o: %.cpp $(cpp_sources)
$(CXX) $(PKG_CXXFLAGS) $


I also specified Imports: Rcpp in DESCRIPTION, but it does not appear
to automatically add the right flags to the compile command.

I am looking for something that could work similarly to Rscript -e
Rcpp:::LdFlags(), which used to export the Rcpp library flags.

Any ideas are greatly appreciated!

Thanks!
Lutz
___
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] Call by reference

2015-07-29 Thread Rguy
The reason I am interested in using call by reference is that I am
accessing data frames with over a million rows and hundreds of columns. It
is more efficient to operate on such a data frame directly, as opposed to
copying it into and out of a function. In other words, I want to be *not*
like R, which is why I am interested in utilizing C++, which supports call
by reference.

On Wed, Jul 29, 2015 at 12:37 PM, Dirk Eddelbuettel e...@debian.org wrote:


 On 29 July 2015 at 07:29, Rguy wrote:
 | Thanks for the insight. For other readers possibly struggling with call
 by
 | reference, the following code works as expected, i.e., x is changed to
 c(1, 2).

 Nobody is struggling, but your example is still unusual (to avoid the
 loaded term wrong) as we generally prefer functions which compute
 something
 to return something.

 But if you insist on side-effects, this is simpler:

 R cppFunction(void myabs(NumericVector  x) { x = abs(x); })
 R x - c(-2.0, 4.0)
 R myabs(x)
 R x
 [1] 2 4
 R

 But you should really do

 R cppFunction(NumericVector myabs(NumericVector  x) { return abs(x); })
 R x - c(-2.0, 4.0)
 R myabs(x)
 [1] 2 4
 R

 which more like R.

 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] Call by reference

2015-07-29 Thread Nathan Kurz
On Wed, Jul 29, 2015 at 10:37 AM, Dirk Eddelbuettel e...@debian.org wrote:
 You misunderstand.  We communicate by SEXP.  Where the P stands for pointer.
 IE even with

   R M - as.data.frame(matrix(rnorm(1e6, 1e3)))
   R object.size(M)
   8000672 bytes
   R

 we'd still only pass the same 56 bytes a SEXP takes. See eg
 https://cran.r-project.org/doc/manuals/R-ints.html#SEXPs and play with memory
 profiling in R.

I tried memory profiling both of your versions with a long vector, and
found that the recommended Rcpp approach made an additional 8GB copy.
Is there a way to avoid this extra copy without resorting to in place
modification?

--nate

nate@ubuntu:~/R/byreference$ less abs.R
  library(Rcpp)

  cppFunction(NumericVector absCopy(NumericVector  x) { return abs(x); })
  cppFunction(void absInPlace(NumericVector  x) { x = abs(x); })

  xOrig = rnorm(1000*1000*1000)
  xRef = xOrig   # shallow copy shares memory
  xCopy = xOrig * 1  # deep copy is independent

  Rprofmem(absInPlace.txt)
  absInPlace(xOrig)
  Rprofmem(NULL)

  identical(xOrig, xRef)
  identical(xOrig, xCopy)

  Rprofmem(absCopy.txt)
  xCopy = absCopy(xCopy)
  Rprofmem(NULL)

  identical(xOrig, xRef)
  identical(xOrig, xCopy)

nate@ubuntu:~/R/byreference$ Rscript abs.R
  [1] TRUE
  [1] FALSE
  [1] TRUE
  [1] TRUE

nate@ubuntu:~/R/byreference$ cat absInPlace.txt
  2544 :Anonymous absInPlace

nate@ubuntu:~/R/byreference$ cat absCopy.txt
  800040 :Anonymous absCopy
  2544 :Anonymous absCopy
___
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] Call by reference

2015-07-29 Thread Nathan Kurz
 On 29 July 2015 at 15:12, Dirk Eddelbuettel wrote:
 All this is becoming a wee bit obscure.  The focus of the list is to help
 people use Rcpp, not to navigate constraints built into the R system.

genuine
Yes, but for many users (well, at least RGuy and me) the impetus for
using Rcpp is to to improve performance on larger datasets.  Since R
is relatively weak in memory management, avoiding large unnecessary
copies by using Rcpp is frequently the best way to do this.
/genuine

joking
Really, your misfortune was creating such a generally useful tool that
solves other people's problems.  If you'd thought ahead and designed
it to be less flexible, you wouldn't have people asking all these
uncomfortable questions about how to modify in place.
/joking

--nate

ps.  I messed up slightly in my example.   With a 1000*1000 vector,
it's only an 8MB copy.  You need to change that to 1000*1000*1000 to
suffer an 8GB copy.
___
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] Call by reference

2015-07-29 Thread Dirk Eddelbuettel

On 29 July 2015 at 15:12, Dirk Eddelbuettel wrote:
| All this is becoming a wee bit obscure.  The focus of the list is to help
| people use R, not to navigate constraints built into the R system.

Sorry:  ... is to help people use Rcpp, not to ...

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] Call by reference

2015-07-29 Thread Dirk Eddelbuettel

On 29 July 2015 at 17:28, Rguy wrote:
| The reason I am interested in using call by reference is that I am accessing
| data frames with over a million rows and hundreds of columns. It is more
| efficient to operate on such a data frame directly, as opposed to copying it
| into and out of a function. In other words, I want to be *not* like R, which 
is
| why I am interested in utilizing C++, which supports call by reference.

You misunderstand.  We communicate by SEXP.  Where the P stands for pointer.
IE even with 

  R M - as.data.frame(matrix(rnorm(1e6, 1e3)))
  R object.size(M)
  8000672 bytes
  R

we'd still only pass the same 56 bytes a SEXP takes. See eg
https://cran.r-project.org/doc/manuals/R-ints.html#SEXPs and play with memory
profiling in R.

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] Specify Rcpp header location in Makevars

2015-07-29 Thread jsmith5...@yahoo.com
Thanks Dirk and Romain for your comments! CLINK_CPPFLAGS did the trick.

Dirk, I understand that it might seem sub-optimal to define my own
compile commands: part of my project is in cuda for GPU-based
parallelization, and I manually set the compile commands to compile my
cuda source files with nvcc and the c++ source files with the default
compiler.

If there is a more elegant or a more robust way to integrate c++ and
cuda sources in one project, I'd be delighted to hear about it; for now
my Makevars-based approach gets the job done.

Thanks again!
Lutz

On 07/29/2015 09:16 PM, Dirk Eddelbuettel wrote:
 
 On 29 July 2015 at 17:19, jsmith5...@yahoo.com wrote:
 | Is there a way to automatically specify the location of the Rcpp include
 | directory (for Rcpp.h) in Makevars?
 
 As Romain said, add LinkingTo: Rcpp to your DESCRIPTION.
  
 | In my Makevars, I use the following compile command:
 | 
 | %.o: %.cpp $(cpp_sources)
 | $(CXX) $(PKG_CXXFLAGS) $
 
 This is generally not a good idea. The advise in Writing R Extensions is
 not as clear-cut on this as it might be but there were e.g. some comments by
 Simon on this list (or maybe r-devel).  In general, you want your Makevars to
 be as minimal as possibly as these implicit rules __already__ come in via the
 core Makefile parts provided by R.  You are more likely to do harm than good
 here (and yes, I got burned too in the past).
  
 | I also specified Imports: Rcpp in DESCRIPTION, but it does not appear
 | to automatically add the right flags to the compile command.
 | 
 | I am looking for something that could work similarly to Rscript -e
 | Rcpp:::LdFlags(), which used to export the Rcpp library flags.
 | 
 | Any ideas are greatly appreciated!
 
 The best best may be to start with Rcpp.package.skeleton() and see what it
 creates.
 
 Cheers, Dirk
 
___
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] Call by reference

2015-07-29 Thread Dirk Eddelbuettel

On 29 July 2015 at 13:05, Nathan Kurz wrote:
| I tried memory profiling both of your versions with a long vector, and
| found that the recommended Rcpp approach made an additional 8GB copy.
| Is there a way to avoid this extra copy without resorting to in place
| modification?

Look at Rcpp::XPtr for the most fine-grained control.  We have used that to
pass bigmemory object handles around.

| nate@ubuntu:~/R/byreference$ less abs.R
|   library(Rcpp)
| 
|   cppFunction(NumericVector absCopy(NumericVector  x) { return abs(x); })

Copy-on-write semantics. *If* you alter you create copies.


All this is becoming a wee bit obscure.  The focus of the list is to help
people use R, not to navigate constraints built into the R system.

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] Specify Rcpp header location in Makevars

2015-07-29 Thread Dirk Eddelbuettel

On 29 July 2015 at 17:19, jsmith5...@yahoo.com wrote:
| Is there a way to automatically specify the location of the Rcpp include
| directory (for Rcpp.h) in Makevars?

As Romain said, add LinkingTo: Rcpp to your DESCRIPTION.
 
| In my Makevars, I use the following compile command:
| 
| %.o: %.cpp $(cpp_sources)
|   $(CXX) $(PKG_CXXFLAGS) $

This is generally not a good idea. The advise in Writing R Extensions is
not as clear-cut on this as it might be but there were e.g. some comments by
Simon on this list (or maybe r-devel).  In general, you want your Makevars to
be as minimal as possibly as these implicit rules __already__ come in via the
core Makefile parts provided by R.  You are more likely to do harm than good
here (and yes, I got burned too in the past).
 
| I also specified Imports: Rcpp in DESCRIPTION, but it does not appear
| to automatically add the right flags to the compile command.
| 
| I am looking for something that could work similarly to Rscript -e
| Rcpp:::LdFlags(), which used to export the Rcpp library flags.
| 
| Any ideas are greatly appreciated!

The best best may be to start with Rcpp.package.skeleton() and see what it
creates.

Cheers, 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] Call by reference

2015-07-29 Thread Hao Ye
The data.table package might also be worth looking into.

Best,
--
Hao Ye
h...@ucsd.edu

 On Jul 29, 2015, at 10:37 AM, Dirk Eddelbuettel e...@debian.org wrote:
 
 
 On 29 July 2015 at 17:28, Rguy wrote:
 | The reason I am interested in using call by reference is that I am accessing
 | data frames with over a million rows and hundreds of columns. It is more
 | efficient to operate on such a data frame directly, as opposed to copying it
 | into and out of a function. In other words, I want to be *not* like R, 
 which is
 | why I am interested in utilizing C++, which supports call by reference.
 
 You misunderstand.  We communicate by SEXP.  Where the P stands for pointer.
 IE even with 
 
  R M - as.data.frame(matrix(rnorm(1e6, 1e3)))
  R object.size(M)
  8000672 bytes
  R
 
 we'd still only pass the same 56 bytes a SEXP takes. See eg
 https://cran.r-project.org/doc/manuals/R-ints.html#SEXPs and play with memory
 profiling in R.
 
 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

___
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] Call by reference

2015-07-29 Thread Rguy
Soren,

Thanks for the insight. For other readers possibly struggling with call by
reference, the following code works as expected, i.e., x is changed to c(1,
2).

#include Rcpp.h
using namespace Rcpp;

// [[Rcpp::export]]
void absC(NumericVector  x) {
int n = x.size();
for (int i = 0; i  n; ++i) {
if (x[i]  0) x[i] = -x[i];
}
}

/*** R
x = c(-1, -2)
absC(x)
x
*/


On Wed, Jul 29, 2015 at 5:36 AM, Søren Højsgaard sor...@math.aau.dk wrote:

 Just a follow up on copying: If you copying: If you do

 #include Rcpp.h
 using namespace Rcpp;

 // [[Rcpp::export]]
 void absC(NumericVector  x) {
 if (x[0]  0)
   x = -x;
 }

 /*** R
 y - -1L
 absC(y)
 y
 */

 you'll get

  y - -1L
  absC(y)
  y
 [1] -1

 and that is because an IntegerVector is provided but a NumericVector is
 expected; hence a copying takes place.

 Regards
 Søren




 |-Original Message-
 |From: rcpp-devel-boun...@lists.r-forge.r-project.org [mailto:rcpp-devel-
 |boun...@lists.r-forge.r-project.org] On Behalf Of Dirk Eddelbuettel
 |Sent: 28. juli 2015 20:46
 |To: Rguy
 |Cc: rcpp-devel@lists.r-forge.r-project.org
 |Subject: Re: [Rcpp-devel] Call by reference
 |
 |
 |On 28 July 2015 at 17:53, Rguy wrote:
 || I attempted to implement the call by reference example on page 200 of
 || Seamless R and C++, as follows:
 ||
 || #include Rcpp.h
 || using namespace Rcpp;
 ||
 || // [[Rcpp::export]]
 || void absC(double  x) {
 || if (x  0) x = -x;
 || }
 ||
 || /*** R
 || x = -1
 || absC(x)
 || x
 || */
 ||
 || Unfortunately, x remains equal to -1.
 || Platforms: Windows 7,  R version 3.1.3 Patched (2015-03-16 r68103).
 || Please advise.
 |
 |An (atomic) double does not exist in R, so you are _always_ forcing a
 |copy, which works against your intent here.
 |
 |Try replacing double with Rcpp::NumericVector.
 |
 |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

___
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] Call by reference

2015-07-29 Thread Dirk Eddelbuettel

On 29 July 2015 at 07:29, Rguy wrote:
| Thanks for the insight. For other readers possibly struggling with call by
| reference, the following code works as expected, i.e., x is changed to c(1, 
2).

Nobody is struggling, but your example is still unusual (to avoid the
loaded term wrong) as we generally prefer functions which compute something
to return something.

But if you insist on side-effects, this is simpler:

R cppFunction(void myabs(NumericVector  x) { x = abs(x); })
R x - c(-2.0, 4.0)
R myabs(x)
R x
[1] 2 4
R

But you should really do

R cppFunction(NumericVector myabs(NumericVector  x) { return abs(x); })
R x - c(-2.0, 4.0)
R myabs(x)
[1] 2 4
R

which more like R.

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