[Rcpp-devel] Rcpp-devel Digest, Vol 49, Issue 27

Brian Templeton mrme at inbox.com
Tue Nov 26 04:02:13 CET 2013


I wanted to thank Paul Johnson, Dirk Eddelbuettel, Gabor Grothendieck, and Hadley Wickham for feedback on my post.

-Paul: I am excited that you find this helpful; I was hoping it might be when I wrote it up.
-Dirk: Excellent suggestion. I will update the page to incorporate this.
-Gabor and Hadley: It sounds like what you are posting could be helpful to some, particularly if C++11 is not an issue. However, since C++11 capabilities are still reasonably new (and "experimental"), more recent versions of GCC can make a difference. See http://gcc.gnu.org/projects/cxx0x.html for some details. Rtools currently only comes with GCC version 4.6.3. I do believe that if you really want to use GCC 4.6.3, you can use the R command Sys.setenv("PKG_CXXFLAGS"="-std=c++0x") instead of Sys.setenv("PKG_CXXFLAGS"="-std=c++11") to use the different compiler flags. For what I am doing, I personally would be somewhat hesitant to use 4.6.3 for C++11 purposes and I'd also be somewhat hesitant to recommend it in general. However, if there is someone who knows more on the finer details with GCC that would like to comment further, that would be great!

Many thanks again for all of the suggestions!
-Brian



> -----Original Message-----
> From: rcpp-devel-request at lists.r-forge.r-project.org
> Sent: Mon, 25 Nov 2013 12:00:03 +0100
> To: rcpp-devel at lists.r-forge.r-project.org
> Subject: Rcpp-devel Digest, Vol 49, Issue 27
> 
> Send Rcpp-devel mailing list submissions to
> 	rcpp-devel at lists.r-forge.r-project.org
> 
> To subscribe or unsubscribe via the World Wide Web, visit
> 	https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel
> 
> or, via email, send a message with subject or body 'help' to
> 	rcpp-devel-request at lists.r-forge.r-project.org
> 
> You can reach the person managing the list at
> 	rcpp-devel-owner at lists.r-forge.r-project.org
> 
> When replying, please edit your Subject line so it is more specific
> than "Re: Contents of Rcpp-devel digest..."
> 
> 
> Today's Topics:
> 
>    1. Re: Installation tutorial for Rcpp, C++11,	and 64-bit Windows
>       (Paul Johnson)
>    2. Re: Installation tutorial for Rcpp, C++11,	and 64-bit Windows
>       (Dirk Eddelbuettel)
>    3. Re: Installation tutorial for Rcpp, C++11,	and 64-bit Windows
>       (Gabor Grothendieck)
>    4. Some beginner questions (Alessandro Mammana)
>    5. Re: Some beginner questions (Kevin Ushey)
> 
> 
> ----------------------------------------------------------------------
> 
> Message: 1
> Date: Sun, 24 Nov 2013 07:51:01 -0600
> From: Paul Johnson <pauljohn32 at gmail.com>
> To: Brian Templeton <mrme at inbox.com>
> Cc: "Rcpp-devel at lists.R-forge.R-project.org"
> 	<rcpp-devel at lists.r-forge.r-project.org>
> Subject: Re: [Rcpp-devel] Installation tutorial for Rcpp, C++11,	and
> 	64-bit Windows
> Message-ID:
> 	<CAErODj83DfZfRJk_K1C3UGBuzLKbTnn_mfR_=WFRMUK6fs+taw at mail.gmail.com>
> Content-Type: text/plain; charset="iso-8859-1"
> 
> On Nov 23, 2013 11:39 PM, "Brian Templeton" <mrme at inbox.com> wrote:
>> 
> I figured I'd write up what worked and post it on Rcpp list in case
> someone
> else has similar problems:
>> 
>> 
> http://batempleton.wordpress.com/2013/11/22/installing-rcpp-using-c11-on-windows-x64/
>> 
>> -Brian
>> 
> Thanks very much for the effort. I have coached Windows users through
> these
> steps. This writeup will really help. The detail on adding to Win PATH is
> especially good.
> 
> It never ceases to amaze me how "Program Files" is a curse on would be
> programmers. Every Windoze user treats me like an idiot when I say "don't
> install with spaces in the path." It is a tough lesson. Most 'user'
> things
> work better with the default: When you do that, Emacs and Notepad++ can't
> find R if you drop it in off the path.
> 
> pj
> Paul Johnson
> Lost my sig: http://pj.freefaculty.org
> 
>> 
> -------------- next part --------------
> An HTML attachment was scrubbed...
> URL:
> <http://lists.r-forge.r-project.org/pipermail/rcpp-devel/attachments/20131124/ff3037a0/attachment-0001.html>
> 
> ------------------------------
> 
> Message: 2
> Date: Sun, 24 Nov 2013 08:07:34 -0600
> From: Dirk Eddelbuettel <edd at debian.org>
> To: Brian Templeton <mrme at inbox.com>
> Cc: rcpp-devel at lists.r-forge.r-project.org
> Subject: Re: [Rcpp-devel] Installation tutorial for Rcpp, C++11,	and
> 	64-bit Windows
> Message-ID: <21138.2086.21233.68827 at max.nulle.part>
> Content-Type: text/plain; charset=us-ascii
> 
> 
> Hi Brian,
> 
> On 23 November 2013 at 21:38, Brian Templeton wrote:
> | I ended up spending a bit of time trying to get Rcpp to work for C++11
> standard code in Windows. (I already had code working on Linux with Rcpp,
> but needed to get it to work with Windows as well.) I figured I'd write
> up what worked and post it on Rcpp list in case someone else has similar
> problems:
> |
> |
> http://batempleton.wordpress.com/2013/11/22/installing-rcpp-using-c11-on-windows-x64/
> 
> Awesome -- thank you very much!
> 
> I am hearing through the gravevine that CRAN is also working on getting
> C++11
> sorted out; updating the Windows toolchain is apparently one of the
> issues.
> 
> One minor comment: as a 'does it work yet?' test, we often suggest using
> the
> Rcpp Attributes function evalCpp().  So after setting everything up, and
> having loaded the Rcpp package, try something like
> 
>        evalCpp("2 * M_PI")
> 
>        evalCpp("std::numeric_limits<double>::max()")
> 
> or even just
> 
>        evalCpp("sqrt(9)")
> 
> Cheers, Dirk
> 
> --
> Dirk Eddelbuettel | edd at debian.org | http://dirk.eddelbuettel.com
> 
> 
> ------------------------------
> 
> Message: 3
> Date: Sun, 24 Nov 2013 10:44:56 -0500
> From: Gabor Grothendieck <ggrothendieck at gmail.com>
> To: Brian Templeton <mrme at inbox.com>
> Cc: "rcpp-devel at lists.r-forge.r-project.org"
> 	<rcpp-devel at lists.r-forge.r-project.org>
> Subject: Re: [Rcpp-devel] Installation tutorial for Rcpp, C++11,	and
> 	64-bit Windows
> Message-ID:
> 	<CAP01uRmELTpomwjvObg9PAFnwzQ5W=HUW9f=g_Xopbshxh9cyA at mail.gmail.com>
> Content-Type: text/plain; charset=ISO-8859-1
> 
> On Sun, Nov 24, 2013 at 12:38 AM, Brian Templeton <mrme at inbox.com> wrote:
>> I ended up spending a bit of time trying to get Rcpp to work for C++11
>> standard code in Windows. (I already had code working on Linux with
>> Rcpp, but needed to get it to work with Windows as well.) I figured I'd
>> write up what worked and post it on Rcpp list in case someone else has
>> similar problems:
>> 
>> http://batempleton.wordpress.com/2013/11/22/installing-rcpp-using-c11-on-windows-x64/
>> 
> 
> I also have some Windows batch files that can help with this, the main
> benefit being that no enviornment variables or registry keys need to
> be set for R or for Rtools (although it will use them if set).  A
> secondary benefit is that they do not require Admin privileges since
> they make no permanent changes to your system.
> 
> R.bat is a single self contained Windows batch file that can start
> R.exe or Rgui.exe .  It looks up the location of R and Rtools using
> the registry or if not found looks in standard locations.  It also
> scans Rtools to discover the appropriate path to its directories. It
> works by temporarily setting the required environment variables
> eliminating the need to set any such variables yourself.
> 
> Using it installation of R is as simple as installing R and Rtools
> using the defaults (they both have automated installers so this is
> simple) and then placing R.bat anywhere on your path.  Running is just
> a matter of using (1) 'R.bat' in place of 'R.exe' and (2) 'R.bat gui'
> in place of 'Rgui.exe'.
> 
> They are available by downloading these two files (the second is the
> documentation):
> 
>    https://batchfiles.googlecode.com/svn/trunk/R.bat
>    https://batchfiles.googlecode.com/svn/trunk/batchfiles.md
> 
> On some systems its heuristic won't work and in that case use:
> 
>    https://batchfiles.googlecode.com/svn/trunk/Rpathset.bat
> 
> which is a simple batch file that can be edited manually (so its not
> as automatic as R.bat but due to its simplicity you may have fewer
> problems).  It also eliminates the need to set any environment
> variables or to make other changes in the registry and is also
> documented in the above md file.
> 
> Some of these installation simplifications are alternately provided in
> some form in Rcpp itself as well as in the devtools and installr
> packages.  These packages allow one to do this from within R whereas
> these batch files work from the Windows cmd line.
> 
> In addition to the cited documentation there is some info on these and
> other batch files on the home page:
> 
>    http://batchfiles.googlecode.com
> 
> 
> ------------------------------
> 
> Message: 4
> Date: Sun, 24 Nov 2013 19:08:59 +0100
> From: Alessandro Mammana <mammana at molgen.mpg.de>
> To: "rcpp-devel at lists.r-forge.r-project.org"
> 	<rcpp-devel at lists.r-forge.r-project.org>
> Subject: [Rcpp-devel] Some beginner questions
> Message-ID:
> 	<CA+RJ7GNozdNXGyNxaAurpcNUDm1v9hURBu+MRrnEZZ6auFa_LA at mail.gmail.com>
> Content-Type: text/plain; charset=ISO-8859-1
> 
> Dear all,
> I had some problems figuring out how to write some code for iterating
> through the values of a run-length-encoded factor (Rle). Now I kind of
> made it work, but I am not sure that the codes does exactly what I
> expect. My questions are both about Rcpp and about C++ , tell me if
> this is not the right place to ask them.
> 
> The function I am writing should iterate through an object of formal
> class 'Rle' (from the "IRanges" packages), which it's like this:
> 1. It has two slots: 'values' and 'lengths'. They have the same
> length, values is a factor and lengths is a integer vector.
> 2. values is a factor: an integer vector with an associated character
> vector (attribute "levels"), and the integer vector points to elements
> in the character vector.
> 
> For instance, the factor f= factor(c('a','a','a','a','b','c','c'))
> when it is run-lenght-encoded rle=Rle(f), it looks like this:
> rle at values ~ c(1, 2, 3)
> attributes(rle at values)$levels ~ c("a","b","c")
> rle at lengths ~ c(3,1,2)
> 
> To make things a bit more complicated, in my situation this Rle object
> is contained in a GRanges object 'gr': rle = gr at seqnames
> 
> I wanted to write the code for a class that encapsulates the iteration
> through such an object (maybe that's a bit java-style). And that was
> my first version that compiled:
> 
> class rleIter {
>     int run;
>     int rlen;
>     int rpos;
> //should I declare them references if I don't want any unnecessary
> copying?
>     IntegerVector rlens;
>     IntegerVector values;
>     std::vector<std::string> names;
>     public:
>         rleIter(RObject& rle):
>             rlens(as<IntegerVector>(rle.slot("lengths"))), // is here
> the vector copied?
>             values(as<IntegerVector>(rle.slot("values"))),
>             names(as<std::vector<std::string> >(values.attr("levels"))),
>             rlen(rlens[0]), // <--- THIS CAUSES SEGFAULT!!!!
>             run(0), rpos(0)
>         {}
> 
>         bool next(){
>             ++rpos;
>             if (rpos == rlens[run]){ //end of the run, go to the next
>                 ++run; rpos = 0;
>                 if (run == rlens.length())
>                     return false;
>             }
>             return true;
>         }
> 
>         const std::string& getValue(){
>             return names[values[run]-1];
>         }
> 
> };
> 
> 
> void readRle(RObject gr){ //passed in by value (it was a mistake)
>     RObject rle = as<RObject>(gr.slot("seqnames")); //<- is this
> vector copied here?
>     rleIter iter(rle);
>     bool finished = false;
>     for (; !finished; finished = !iter.next()){
>         Rcout << iter.getValue() << std::endl;
>     }
> }
> 
> // [[Rcpp::export]]
> void test(RObject gr){
>     readRle(gr);
> }
> 
> in R:
> 
> library(GenomicRanges)
> gr <- GRanges(seqnames=c("chr1", "chr1","chr2"),
> ranges=IRanges(start=c(1,10,7),end=c(10,101,74)))
> library(my_package_under_development_with_the_rcpp_code_shown_above)
> test(gr)
> 
> SEGFAULT
> 
> Questions:
> 
> 1. This code gives segfault at the point that I indicated. Why? Maybe
> I am pointing within the initializer list to areas of memory that are
> allocated and filled in in the initializer list and maybe this is
> forbidden?
> 2. If I change the signature of the function readRle and I pass the gr
> object by reference, the segfault dissappears, why? If I copy the gr
> object the copy should be identical, why do they have different
> behaviours?
> 3. I don't understand if doing:
> RObject rle = as<RObject>(gr.slot("seqnames"));
> causes the vector rle to be copied, and, what is worse, I have no idea
> about what resources to look up to find it out, or what
> reasoning/principles to think about, other than posting in this
> mailing list or attempting to look at the source code for hours...
> 4. If I replace the line above with:
> RObject& rle = as<RObject>(gr.slot("seqnames"));
> so that I am sure that the vector is not copied, the compiler
> complains saying that
> as<RObject>(gr.slot("seqnames")) is an rvalue, and if I want to
> reference it, the reference should be constant. How do I create a
> non-constant reference to a slot of a s4 object then?
> 
> If you made it through the end of this very long and boring email and
> if you could give me some help I would be extremely grateful.
> 
> Ale
> 
> --
> Alessandro Mammana, PhD Student
> Max Planck Institute for Molecular Genetics
> Ihnestra?e 63-73
> D-14195 Berlin, Germany
> 
> 
> ------------------------------
> 
> Message: 5
> Date: Sun, 24 Nov 2013 11:32:26 -0800
> From: Kevin Ushey <kevinushey at gmail.com>
> To: Alessandro Mammana <mammana at molgen.mpg.de>
> Cc: "rcpp-devel at lists.r-forge.r-project.org"
> 	<rcpp-devel at lists.r-forge.r-project.org>
> Subject: Re: [Rcpp-devel] Some beginner questions
> Message-ID:
> 	<CAJXgQP3pwyKpAz+f=Vv8USOUF4j=QTpKOEr1nuZFRquj3=V-Jg at mail.gmail.com>
> Content-Type: text/plain; charset=ISO-8859-1
> 
> Hi Ale,
> 
> My guess: the elements are not being initialized in the order you expect.
> 
> In fact, class members in C++ are initialized _in the order they are
> declared in the class_, not the order you place them in the
> initializer list. So, based on that, your code tries to first
> initialize run, then rlen, but rlen depends on rlens[0] which has not
> yet been initialized, and so things go wrong.
> 
> If you turn on compiler warnings (-Wall) you get informative errors.
> In fact, clang points right at the error for me ;)
> 
> test.cpp:16:3: warning: field 'names' will be initialized after field
> 'rlen' [-Wreorder]
>   names(as<std::vector<std::string> >(values.attr("levels"))),
>   ^
> test.cpp:17:3: warning: field 'rlen' will be initialized after field
> 'run' [-Wreorder]
>   rlen(rlens[0]), // <--- THIS CAUSES SEGFAULT!!!!
>   ^
> 
> (sidenote: I highly recommend creating a file '~/.R/Makevars', and
> inserting the line:
> 
>     CFLAGS="-g -O2 -Wall -pedantic"
>     CXXFLAGS="-g -O2 -Wall -pedantic"
> 
> so that your compiler picks out these code smells for you whenever
> compiling C/C++ code with R)
> 
> As for your other questions re: copying: RObjects are merely thin
> wrappers over pointers, so copying an RObject does not involve copying
> all the memory encompassing an R object, just the pointer to that
> object. Rcpp containers will always wrap to the R object if the R type
> matches the container type -- e.g., IntegerVectors wrap around R's
> integer vectors, but force a copy / coercion when you have a numeric R
> vector. Make sure the type of object you think you're passing from R
> matches the container you're using in Rcpp -- check what
> mode(rle at lengths) gives you.
> 
> All of Rcpp's containers are very light, so I doubt you gain much e.g.
> passing an Rcpp::IntegerVector by reference rather than by value.
> 
> -Kevin
> 
> On Sun, Nov 24, 2013 at 10:08 AM, Alessandro Mammana
> <mammana at molgen.mpg.de> wrote:
>> Dear all,
>> I had some problems figuring out how to write some code for iterating
>> through the values of a run-length-encoded factor (Rle). Now I kind of
>> made it work, but I am not sure that the codes does exactly what I
>> expect. My questions are both about Rcpp and about C++ , tell me if
>> this is not the right place to ask them.
>> 
>> The function I am writing should iterate through an object of formal
>> class 'Rle' (from the "IRanges" packages), which it's like this:
>> 1. It has two slots: 'values' and 'lengths'. They have the same
>> length, values is a factor and lengths is a integer vector.
>> 2. values is a factor: an integer vector with an associated character
>> vector (attribute "levels"), and the integer vector points to elements
>> in the character vector.
>> 
>> For instance, the factor f= factor(c('a','a','a','a','b','c','c'))
>> when it is run-lenght-encoded rle=Rle(f), it looks like this:
>> rle at values ~ c(1, 2, 3)
>> attributes(rle at values)$levels ~ c("a","b","c")
>> rle at lengths ~ c(3,1,2)
>> 
>> To make things a bit more complicated, in my situation this Rle object
>> is contained in a GRanges object 'gr': rle = gr at seqnames
>> 
>> I wanted to write the code for a class that encapsulates the iteration
>> through such an object (maybe that's a bit java-style). And that was
>> my first version that compiled:
>> 
>> class rleIter {
>>     int run;
>>     int rlen;
>>     int rpos;
>> //should I declare them references if I don't want any unnecessary
>> copying?
>>     IntegerVector rlens;
>>     IntegerVector values;
>>     std::vector<std::string> names;
>>     public:
>>         rleIter(RObject& rle):
>>             rlens(as<IntegerVector>(rle.slot("lengths"))), // is here
>> the vector copied?
>>             values(as<IntegerVector>(rle.slot("values"))),
>>             names(as<std::vector<std::string> >(values.attr("levels"))),
>>             rlen(rlens[0]), // <--- THIS CAUSES SEGFAULT!!!!
>>             run(0), rpos(0)
>>         {}
>> 
>>         bool next(){
>>             ++rpos;
>>             if (rpos == rlens[run]){ //end of the run, go to the next
>>                 ++run; rpos = 0;
>>                 if (run == rlens.length())
>>                     return false;
>>             }
>>             return true;
>>         }
>> 
>>         const std::string& getValue(){
>>             return names[values[run]-1];
>>         }
>> 
>> };
>> 
>> 
>> void readRle(RObject gr){ //passed in by value (it was a mistake)
>>     RObject rle = as<RObject>(gr.slot("seqnames")); //<- is this
>> vector copied here?
>>     rleIter iter(rle);
>>     bool finished = false;
>>     for (; !finished; finished = !iter.next()){
>>         Rcout << iter.getValue() << std::endl;
>>     }
>> }
>> 
>> // [[Rcpp::export]]
>> void test(RObject gr){
>>     readRle(gr);
>> }
>> 
>> in R:
>> 
>> library(GenomicRanges)
>> gr <- GRanges(seqnames=c("chr1", "chr1","chr2"),
>> ranges=IRanges(start=c(1,10,7),end=c(10,101,74)))
>> library(my_package_under_development_with_the_rcpp_code_shown_above)
>> test(gr)
>> 
>> SEGFAULT
>> 
>> Questions:
>> 
>> 1. This code gives segfault at the point that I indicated. Why? Maybe
>> I am pointing within the initializer list to areas of memory that are
>> allocated and filled in in the initializer list and maybe this is
>> forbidden?
>> 2. If I change the signature of the function readRle and I pass the gr
>> object by reference, the segfault dissappears, why? If I copy the gr
>> object the copy should be identical, why do they have different
>> behaviours?
>> 3. I don't understand if doing:
>> RObject rle = as<RObject>(gr.slot("seqnames"));
>> causes the vector rle to be copied, and, what is worse, I have no idea
>> about what resources to look up to find it out, or what
>> reasoning/principles to think about, other than posting in this
>> mailing list or attempting to look at the source code for hours...
>> 4. If I replace the line above with:
>> RObject& rle = as<RObject>(gr.slot("seqnames"));
>> so that I am sure that the vector is not copied, the compiler
>> complains saying that
>> as<RObject>(gr.slot("seqnames")) is an rvalue, and if I want to
>> reference it, the reference should be constant. How do I create a
>> non-constant reference to a slot of a s4 object then?
>> 
>> If you made it through the end of this very long and boring email and
>> if you could give me some help I would be extremely grateful.
>> 
>> Ale
>> 
>> --
>> Alessandro Mammana, PhD Student
>> Max Planck Institute for Molecular Genetics
>> Ihnestra?e 63-73
>> D-14195 Berlin, Germany
>> _______________________________________________
>> 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
> 
> 
> ------------------------------
> 
> _______________________________________________
> 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
> 
> End of Rcpp-devel Digest, Vol 49, Issue 27
> ******************************************

____________________________________________________________
FREE 3D MARINE AQUARIUM SCREENSAVER - Watch dolphins, sharks & orcas on your desktop!
Check it out at http://www.inbox.com/marineaquarium




More information about the Rcpp-devel mailing list