[Rcpp-devel] Memory not mapped
Kevin Ushey
kevinushey at gmail.com
Thu Feb 20 19:30:44 CET 2014
Hi Hideyoshi,
At this line:
if(x(i,exit_col)==1){
I see that exit_col is equal to the number of columns in the matrix,
thereby indexing outside the matrix if 'i' is large enough. Do you
want 'exit_col - 1' instead? Be careful when going from R's 1-based
indexing to C++'s 0-based indexing.
Cheers,
Kevin
On Thu, Feb 20, 2014 at 10:16 AM, Hideyoshi Maeda
<hideyoshi.maeda at gmail.com> wrote:
> Thanks for the suggestion to keep adding one line at a time, but annoyingly
> the error has not become apparent...when i add everything but the last section
> of code it works...but when adding in simple statements at the end, it no
> longer works
>
> here is the full test.cpp that WORKS
>
>
> #include <Rcpp.h>
> using namespace Rcpp;
>
> // [[Rcpp::export]]
> List entryexitclock( SEXP xts_obj_,int ent_col, int exit_col, double
> clock_limit, int init_pos) {
> // function takes in xts_obj_ (an xts object from R) and converts to a
> NumericMatrix
> Rcpp::NumericMatrix x(xts_obj_);
> // Get the index and call v
> DatetimeVector v(NumericVector(RObject(xts_obj_).attr("index")));
> // The dimensions of rtn_xts are extracted
> int nr = x.nrow();
>
> int pos = init_pos;
> Rcpp::NumericVector pos_vec(nr);
> Rcpp::NumericVector clock_vec(nr);
> Rcpp::NumericVector real_entry(nr);
> Rcpp::NumericVector signal_exit(nr);
> Rcpp::NumericVector time_exit(nr);
>
> for(int i=0; i<nr; i++){
> if(pos==0){
> // No position before period i
> if(x(i,ent_col)==0){
> // No position and no entry
> } else {
> //No position and entry signal
> pos = 1;
> pos_vec[i] = 1;
> real_entry[i] = 1;
> }
> } else{
> // Exisiting position i.e. pos==1
> // Add the gap difference to the timer
> // time is in seconds...
> double gap_diff = (v[i]-v[i-1]);
> double time_in_trade = clock_vec[i-1]+gap_diff;
> clock_vec[i] = time_in_trade;
> pos_vec[i] = 1;
> if(time_in_trade>=clock_limit){
> //When holding an existing position, exit due to time given
> pos = 0;
> time_exit[i] = 1;
> } else {
> //If time based exit hasnt triggered...check for signal
> if(x(i,exit_col)==1){
> // Exit signal given with existing position
> } else{
> // No exit signal, so keep position
> }
> }
> }
> }
>
> List l;
> return (l);
>
> }
>
> but when changing the last section (starting from the penultimate else) to
> something like this...
>
> } else {
> //If time based exit hasnt triggered...check for signal
> if(x(i,exit_col)==1){
> // Exit signal given with existing position
> pos = 0;
> signal_exit[i] = 1;
> } else{
> // No exit signal, so keep position
> }
> }
> }
> }
> return List::create(_["pos_vec"] = pos_vec,
> _["clock_vec"] = clock_vec,
> _["real_entry"] = real_entry,
> _["signal_exit"] = signal_exit,
> _["time_exit"] = time_exit);
> }
>
> It no longer works...and I can't tell why. To me at least it doesn't look like
> I'm doing anything crazy...
>
> And to test the code I am running the following....
>
> require(xts)
> require(Rcpp)
> # 500 days
> n <- 60*24*500
>
> # minutely standard dev
> sd_m <- 0.003
>
> #minutely mean
> mu_m <- 5e-6
>
> # Rate at which entry/exits triggered
> entry_rate <- 0.002
> exit_rate <- 0.002
>
> # set seed
> set.seed(1)
>
> x <- xts(100*cumprod(1+rnorm(n,mu_m,sd_m)),
> as.POSIXct(Sys.Date())+(60*c(1:n)))
> x <- round(to.minutes5(x,name="ABC"),2)
> x$rand_entry <- rpois(nrow(x),entry_rate)
> x$rand_exit <- rpois(nrow(x),exit_rate)
>
> Rcpp::sourceCpp('test.cpp')
>
> init_pos <- 0
>
> clock_time_mins <- 120
>
> res <- lapply(1:100, function(z) {
> entryexitclock(x,match("rand_entry",colnames(x)),
> match("rand_exit",colnames(x)),
> clock_time_mins*60,init_pos)
> })
>
> The error obtained when adding in the last part of code is:
>
> *** caught segfault ***
> address 0x10d0cfc40, cause 'memory not mapped'
>
> Traceback:
> 1: .Primitive(".Call")(<pointer: 0x10155d2e0>, xts_obj_, ent_col,
> exit_col, clock_limit, init_pos)
> 2: entryexitclock(x, match("rand_entry", colnames(x)), match("rand_exit",
> colnames(x)), clock_time_mins * 60, init_pos)
> 3: FUN(1:100[[3L]], ...)
> 4: lapply(1:100, function(z) { entryexitclock(x, match("rand_entry",
> colnames(x)), match("rand_exit", colnames(x)), clock_time_mins * 60,
> init_pos)})
>
> Possible actions:
> 1: abort (with core dump, if enabled)
> 2: normal R exit
> 3: exit R without saving workspace
> 4: exit R saving workspace
>
> Interestingly, from the traceback it suggests it fails on the third attempt
> (z=3) rather than the first or second...
>
> Any suggestions to help fix this issue would be greatly be appreciated...
>
> hlm
>
>
>
>
>
> On 20 Feb 2014, at 16:48, Sameer D'Costa <sameerdcosta at gmail.com> wrote:
>
> One thing you can do is start with a little test case and run multiple times
> it to see if the segfault is really coming from where you think it is.
>
> /* test.cpp */
> #include <Rcpp.h>
> using namespace Rcpp;
>
> // [[Rcpp::export]]
> List entryexitclock( SEXP xts_obj_,int ent_col, int exit_col, double
> clock_limit, int init_pos) {
> // function takes in xts_obj_ (an xts object from R) and converts to a
> NumericMatrix
> Rcpp::NumericMatrix x(xts_obj_);
> // Get the index and call v
> DatetimeVector v(NumericVector(RObject(xts_obj_).attr("index")));
> // The dimensions of rtn_xts are extracted
> int nr = x.nrow();
> List l;
> return (l);
> }
>
> Then on the R side you can test it out
> library(Rcpp)
> library(xts)
> sourceCpp("test.cpp")
> n <- 10000
> k <- xts(as.numeric(1:n), Sys.time()+1:n)
> res <- lapply(1:10000, function(z) entryexitclock(k, 12, 12, 60*60,0))
>
> This code does not seem to segfault for me using R 2.15.3 on Ubuntu with
> Rcpp 0.10.4. Does it segfault for you?
>
> If I were you I would keep adding code to the test example till it segfaults
> reliably. If you do this one line at a time you will have a good idea of
> which line is causing the segfault. Usually the "user error" becomes
> apparent :). If you are unsure you could winnow down the test case to a
> small reproducible test case that crashes reliably and then post it to the
> list.
>
> Regards
> Sameer
>
>
> On Thu, Feb 20, 2014 at 9:50 AM, Hideyoshi Maeda <hideyoshi.maeda at gmail.com>
> wrote:
>>
>> Hi I am trying to run a function I created in Rcpp, which is then
>> sourced/compiled and executed in R.
>>
>> The function is called "entryexitclock".
>>
>> and starts off as follows:
>>
>> #include <Rcpp.h>
>> using namespace Rcpp;
>>
>> // [[Rcpp::export]]
>> List entryexitclock( SEXP xts_obj_,int ent_col, int exit_col, double
>> clock_limit, int init_pos) {
>> // function takes in xts_obj_ (an xts object from R) and converts to a
>> NumericMatrix
>> Rcpp::NumericMatrix x(xts_obj_);
>> // Get the index and call v
>> DatetimeVector v(NumericVector(RObject(xts_obj_).attr("index")));
>> // The dimensions of rtn_xts are extracted
>> int nr = x.nrow();
>>
>> There is more code but I'm starting to doubt if its related to the rest of
>> the code. I have a feeling it might be my use of SEXP but I'm not sure...
>>
>> The compilation works fine, however "sometimes" the code works and
>> sometimes the code doesn't...
>>
>> The error when it doesn't is as follows:
>>
>>
>> *** caught segfault ***
>> address 0x11a9783c8, cause 'memory not mapped'
>>
>> Traceback:
>> 1: .Primitive(".Call")(<pointer: 0x1044dde70>, xts_obj_, ent_col,
>> exit_col, clock_limit, init_pos)
>> 2: entryexitclock(k, 11, 12, 60 * 60, 0)
>>
>> Possible actions:
>> 1: abort (with core dump, if enabled)
>> 2: normal R exit
>> 3: exit R without saving workspace
>> 4: exit R saving workspace
>>
>> Any suggestions as to what I can do to make sure that this function works
>> every time rather than just sometimes?
>>
>> HLM
>>
>>
>>
>> _______________________________________________
>> 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
More information about the Rcpp-devel
mailing list