Hi list - although not strictly (or not really at all..) an Rcpp problem - I thought that since there are so many 'better-than-I' c++ coders here - whom are familiar with R... it might be a good place to ask.
I have a "naive" piece of code - that in essence replicates what the R function "sample" does.
Here is a full/toy example using inline, below.
My problem is this is used in part of a larger piece of code.. and it is a major bottleneck... Where I have put 50,000 in the function below - what I am really using is
10,000,000 so sscpp2(ttin,10000000,1.0,126) is the real function call.
I have toyed with the idea of running all the "runifs" at once (hence the make_matrix function) but R complains about allocating large vectors..sometimes.
I have also tried using std::random_shuffle (to get rid of the runifs) - although performance is then dependent upon the size of the incoming vector 'RetIn' (why shuffle all the elements when I only need some, etc).
I have thought (but not tried) importing R's sample function...but I suspect this may be a totally rude idea.
If anyone has time, or inclination, if they spot a dumb idea in my code - or know a better Rcpp way to do this - Id much appreciate it!
Thanks, chris
________________________________
library(inline); library(Rcpp);
inc<-'
using namespace Rcpp;
double Fn(double a, double b)
{
return std::max(0.0,exp(a)-b);
}
SEXP make_matrix(int nr, int nc, int d){
NumericVector y=floor(runif(nr*nc)*(d-1));
y.attr("dim")=Dimension(nr,nc);
return y;
}
'
src<-'
NumericVector RetIn(RetVec);
int SS_C=as<int>(SamSize);
double St_C=as<double>(StM);
int ES_C=as<int>(ExpSize);
int i,j;
NumericVector OutVec=rep(0.0,SS_C),SamIndx(ES_C);
RNGScope scope;
//NumericMatrix SamIndx=make_matrix(SS_C,ES_C,RetIn.size()-1);
for(j=0;j<SS_C;j++)
{
SamIndx=floor(runif(ES_C)*(RetIn.size()-1));
for(i=0; i<ES_C; i++)
{
OutVec[j]+=RetIn[SamIndx[i]];
}
}
NumericVector strVEC=rep(St_C,SS_C), ans(SS_C);
std::transform(OutVec.begin(),OutVec.end(),strVEC.begin(),ans.begin(),Fn);
double MeanOut=mean(ans);
return(wrap(MeanOut));
'
sscpp2<-cfunction( signature(RetVec="numericVector",SamSize="numeric",StM="numeric",ExpSize="numeric"), src,inc, Rcpp=TRUE,cppargs="",convention=".Call")
ttin=runif(2000)/10000
sscpp2(ttin,50000,1.0,126)
#####
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.r-forge.r-project.org/pipermail/rcpp-devel/attachments/20110816/369ebbe4/attachment.htm>
[Rcpp-devel] Speed gain assistance
6 messages · Wray, Christopher, Dirk Eddelbuettel, Darren Cook
1 day later
I'd like to shamelessly hijack Christopher Wray's question to ask if there is a convention for parameter naming with Rcpp and inline. Well, actually I'll propose one, to then see what others prefer. First I would propose to use long variable names, instead of abbreviations, in both the CPP and R sides [1]. I already know enough R to know this will be immediately rejected ;-) [2] Anyway, while trying to understand the posted code I found myself jumping back and forth between the below two blocks of code, muttering to myself, "so the bottleneck is proportional to 2nd parameter which is SamSize, which is then called SS_C, ..."
src<-' NumericVector RetIn(RetVec); int SS_C=as<int>(SamSize); double St_C=as<double>(StM); int ES_C=as<int>(ExpSize); ... sscpp2<-cfunction( signature(RetVec="numericVector",SamSize="numeric",StM="numeric",ExpSize="numeric"), src,inc, Rcpp=TRUE,cppargs="",convention=".Call")
So, my proposal would be to use the same names in the signature that you intend to use in the C++ code, but with underline appended. I.e. signature(RetIn_="numericVector",SS_C_="numeric",St_C_="numeric",ES_C_="numeric") The C++ code then starts: NumericVector RetIn(RetIn_); int SS_C=as<int>(SS_C_); double St_C=as<double>(St_C_); int ES_C=as<int>(ES_C); What conventions are other people using? Darren [1]: I am not alone in this. E.g. see "Rename Method" in Martin Fowler's Refactoring; "It's All Writing" in The Pragmatic Programmer (ch.8); etc. [2]: I realize many R functions are direct translations of mathematical functions, so calling a parameter "p" is not as meaningless as it appears at first glance. I still think it should be "probabilities" not "p" (yes, "qunif", I'm looking at you), but I realize I'm in a minority when surrounded byq mathematicians.
Darren Cook, Software Researcher/Developer http://dcook.org/work/ (About me and my work) http://dcook.org/blogs.html (My blogs and articles)
On 18 August 2011 at 16:24, Darren Cook wrote:
| I'd like to shamelessly hijack Christopher Wray's question to ask if | there is a convention for parameter naming with Rcpp and inline. Well, | actually I'll propose one, to then see what others prefer. | | First I would propose to use long variable names, instead of | abbreviations, in both the CPP and R sides [1]. I already know enough R | to know this will be immediately rejected ;-) [2] | | Anyway, while trying to understand the posted code I found myself | jumping back and forth between the below two blocks of code, muttering | to myself, "so the bottleneck is proportional to 2nd parameter which is | SamSize, which is then called SS_C, ..." | | > src<-' | > NumericVector RetIn(RetVec); | > int SS_C=as<int>(SamSize); | > double St_C=as<double>(StM); | > int ES_C=as<int>(ExpSize); | > ... | > sscpp2<-cfunction( | > signature(RetVec="numericVector",SamSize="numeric",StM="numeric",ExpSize="numeric"), | > src,inc, Rcpp=TRUE,cppargs="",convention=".Call") | | So, my proposal would be to use the same names in the signature that you | intend to use in the C++ code, but with underline appended. I.e. You must do that anyway or you get linking errors. The rest is stylistic, and we do not impose a style (besides maybe the default indentation I get from Emacs :). If you need a style, I suggest to follow R conventions as layed out in the R Internals manual. Personally, I also like this one: http://quantlib.org/style.shtml Descriptive names are good, comments are good, using separate names for members vars is good and I still mostly prepend m_. Dirk | signature(RetIn_="numericVector",SS_C_="numeric",St_C_="numeric",ES_C_="numeric") | | The C++ code then starts: | NumericVector RetIn(RetIn_); | int SS_C=as<int>(SS_C_); | double St_C=as<double>(St_C_); | int ES_C=as<int>(ES_C); | | What conventions are other people using? | | Darren | | [1]: I am not alone in this. E.g. see "Rename Method" in Martin Fowler's | Refactoring; "It's All Writing" in The Pragmatic Programmer (ch.8); etc. | | [2]: I realize many R functions are direct translations of mathematical | functions, so calling a parameter "p" is not as meaningless as it | appears at first glance. I still think it should be "probabilities" not | "p" (yes, "qunif", I'm looking at you), but I realize I'm in a minority | when surrounded byq mathematicians. | | | | | -- | Darren Cook, Software Researcher/Developer | | http://dcook.org/work/ (About me and my work) | http://dcook.org/blogs.html (My blogs and articles) | _______________________________________________ | 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
Two new Rcpp master classes for R and C++ integration scheduled for New York (Sep 24) and San Francisco (Oct 8), more details are at http://dirk.eddelbuettel.com/blog/2011/08/04#rcpp_classes_2011-09_and_2011-10 http://www.revolutionanalytics.com/products/training/public/rcpp-master-class.php
On 18 August 2011 at 08:44, Dirk Eddelbuettel wrote:
|
| On 18 August 2011 at 16:24, Darren Cook wrote:
| | I'd like to shamelessly hijack Christopher Wray's question to ask if
| | there is a convention for parameter naming with Rcpp and inline. Well,
| | actually I'll propose one, to then see what others prefer.
| |
| | First I would propose to use long variable names, instead of
| | abbreviations, in both the CPP and R sides [1]. I already know enough R
| | to know this will be immediately rejected ;-) [2]
| |
| | Anyway, while trying to understand the posted code I found myself
| | jumping back and forth between the below two blocks of code, muttering
| | to myself, "so the bottleneck is proportional to 2nd parameter which is
| | SamSize, which is then called SS_C, ..."
| |
| | > src<-'
| | > NumericVector RetIn(RetVec);
| | > int SS_C=as<int>(SamSize);
| | > double St_C=as<double>(StM);
| | > int ES_C=as<int>(ExpSize);
| | > ...
| | > sscpp2<-cfunction(
| | > signature(RetVec="numericVector",SamSize="numeric",StM="numeric",ExpSize="numeric"),
| | > src,inc, Rcpp=TRUE,cppargs="",convention=".Call")
| |
| | So, my proposal would be to use the same names in the signature that you
| | intend to use in the C++ code, but with underline appended. I.e.
|
| You must do that anyway or you get linking errors.
I was sloppy: compile-time errors. I.e. this:
errorDemo <- cxxfunction(signature(badName="integer"), plugin='Rcpp', body='
Rcpp::IntegerVector x(otherName);
return 2*x;
')
gives you
Error in compileCode(f, code, language = language, verbose = verbose) :
Compilation ERROR, function(s)/method(s) not created! file181a5f5c.cpp: In function ?SEXPREC* file181a5f5c(SEXPREC*)?:
file181a5f5c.cpp:32:26: error: ?otherName? was not declared in this scope
file181a5f5c.cpp:33:13: error: cannot convert ?Rcpp::sugar::Times_Vector_Primitive<13, true, Rcpp::Vector<13> >? to ?SEXPREC*? in return
make: *** [file181a5f5c.o] Error 1
In addition: Warning message:
running command '/usr/lib64/R/bin/R CMD SHLIB file181a5f5c.cpp 2> file181a5f5c.cpp.err.txt' had status 1
R>
So as I indicated, you have no choice but to make those match.
Dirk
| The rest is stylistic, and we do not impose a style (besides maybe the
| default indentation I get from Emacs :). If you need a style, I suggest to
| follow R conventions as layed out in the R Internals manual. Personally, I
| also like this one:
|
| http://quantlib.org/style.shtml
|
| Descriptive names are good, comments are good, using separate names for
| members vars is good and I still mostly prepend m_.
|
| Dirk
|
|
| | signature(RetIn_="numericVector",SS_C_="numeric",St_C_="numeric",ES_C_="numeric")
| |
| | The C++ code then starts:
| | NumericVector RetIn(RetIn_);
| | int SS_C=as<int>(SS_C_);
| | double St_C=as<double>(St_C_);
| | int ES_C=as<int>(ES_C);
| |
| | What conventions are other people using?
| |
| | Darren
| |
| | [1]: I am not alone in this. E.g. see "Rename Method" in Martin Fowler's
| | Refactoring; "It's All Writing" in The Pragmatic Programmer (ch.8); etc.
| |
| | [2]: I realize many R functions are direct translations of mathematical
| | functions, so calling a parameter "p" is not as meaningless as it
| | appears at first glance. I still think it should be "probabilities" not
| | "p" (yes, "qunif", I'm looking at you), but I realize I'm in a minority
| | when surrounded byq mathematicians.
| |
| |
| |
| |
| | --
| | Darren Cook, Software Researcher/Developer
| |
| | http://dcook.org/work/ (About me and my work)
| | http://dcook.org/blogs.html (My blogs and articles)
| | _______________________________________________
| | 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
|
| --
| Two new Rcpp master classes for R and C++ integration scheduled for
| New York (Sep 24) and San Francisco (Oct 8), more details are at
| http://dirk.eddelbuettel.com/blog/2011/08/04#rcpp_classes_2011-09_and_2011-10
| http://www.revolutionanalytics.com/products/training/public/rcpp-master-class.php
| _______________________________________________
| 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
Two new Rcpp master classes for R and C++ integration scheduled for New York (Sep 24) and San Francisco (Oct 8), more details are at http://dirk.eddelbuettel.com/blog/2011/08/04#rcpp_classes_2011-09_and_2011-10 http://www.revolutionanalytics.com/products/training/public/rcpp-master-class.php
4 days later
| | So, my proposal would be to use the same names in the signature that you
| | intend to use in the C++ code, but with underline appended. I.e.
I was sloppy: compile-time errors. I.e. this:
errorDemo <- cxxfunction(signature(badName="integer"), plugin='Rcpp', body='
Rcpp::IntegerVector x(otherName);
return 2*x;
')
gives you
[compile error]
Hello Dirk,
Thanks for the reply. What I meant was "badName" and "x" should be the
same, because using two names for the same thing makes the code harder
to understand and maintain (*). So either this:
goodDemo <- cxxfunction(signature(goodName_="integer"), plugin='Rcpp',
body='
Rcpp::IntegerVector goodName(goodName_);
return 2*goodName;
')
or this:
nearlyAsGoodDemo <- cxxfunction(signature(x_="integer"), plugin='Rcpp',
body='
Rcpp::IntegerVector x(x_);
return 2*goodName;
')
Darren
*: I'm still amazed at how much "throw-away" code, that I was told was
only needed to run for a month, or even for a one-off data import, I end
up still maintaining 3 or 4 years later; I sometimes wonder if my
clients are deliberately lying to me about their intentions ;-)
Darren Cook, Software Researcher/Developer http://dcook.org/work/ (About me and my work) http://dcook.org/blogs.html (My blogs and articles)
P.S. I think it was obvious from the context that this version contained
a typo but, just in case anyone was confused, it should've looked like:
nearlyAsGoodDemo <- cxxfunction(signature(x_="integer"), plugin='Rcpp',
body='
Rcpp::IntegerVector x(x_);
return 2*x;
')
Darren