<html><head><meta http-equiv="Content-Type" content="text/html charset=windows-1252"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;">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<div><br></div><div>here is the full test.cpp that WORKS</div><div><br></div><div><br></div><div><div>#include <Rcpp.h></div><div>using namespace Rcpp;</div><div><br></div><div>// [[Rcpp::export]]</div><div>List entryexitclock( SEXP xts_obj_,int ent_col, int exit_col, double clock_limit, int init_pos) {</div><div>  // function takes in xts_obj_ (an xts object from R) and converts to a NumericMatrix</div><div>  Rcpp::NumericMatrix x(xts_obj_);</div><div>  // Get the index and call v</div><div>  DatetimeVector v(NumericVector(RObject(xts_obj_).attr("index")));</div><div>  // The dimensions of rtn_xts are extracted</div><div>  int nr = x.nrow();</div><div>  </div><div>  int pos = init_pos;</div><div>  Rcpp::NumericVector pos_vec(nr);</div><div>  Rcpp::NumericVector clock_vec(nr);</div><div>  Rcpp::NumericVector real_entry(nr);</div><div>  Rcpp::NumericVector signal_exit(nr);</div><div>  Rcpp::NumericVector time_exit(nr);</div><div>  </div><div>  for(int i=0; i<nr; i++){</div><div>    if(pos==0){</div><div>      // No position before period i</div><div>      if(x(i,ent_col)==0){</div><div>        // No position and no entry</div><div>      } else {</div><div>        //No position and entry signal</div><div>        pos = 1;</div><div>        pos_vec[i] = 1;</div><div>        real_entry[i] = 1;</div><div>      }</div><div>    } else{</div><div>      // Exisiting position i.e. pos==1</div><div>      // Add the gap difference to the timer</div><div>      // time is in seconds...</div><div>      double gap_diff = (v[i]-v[i-1]);</div><div>      double time_in_trade = clock_vec[i-1]+gap_diff;</div><div>      clock_vec[i] = time_in_trade;</div><div>      pos_vec[i] = 1;</div><div>      if(time_in_trade>=clock_limit){</div><div>        //When holding an existing position, exit due to time given </div><div>        pos = 0;</div><div>        time_exit[i] = 1;</div><div>      } else {</div><div>        //If time based exit hasnt triggered...check for signal</div><div>        if(x(i,exit_col)==1){</div><div>          // Exit signal given with existing position</div><div>        } else{</div><div>          // No exit signal, so keep position</div><div>        }</div><div>      }</div><div>    }</div><div>  }</div><div>  </div><div>  List l;</div><div>  return (l);</div><div><br></div><div>}</div></div><div><br></div><div>but when changing the last section (starting from the penultimate else) to something like this…</div><div><br></div><div><div>      } else {</div><div>        //If time based exit hasnt triggered...check for signal</div><div>        if(x(i,exit_col)==1){</div><div>          // Exit signal given with existing position</div><div>          pos = 0;</div><div>          signal_exit[i] = 1;</div><div>        } else{</div><div>          // No exit signal, so keep position</div><div>        }</div><div>      }</div><div>    }</div><div>  }</div><div> return List::create(_["pos_vec"] = pos_vec, </div><div>                     _["clock_vec"] = clock_vec,</div><div>                     _["real_entry"] = real_entry,</div><div>                     _["signal_exit"] = signal_exit,</div><div>                     _["time_exit"] = time_exit);</div><div>}</div></div><div><br></div><div>It no longer works…and I can’t tell why. To me at least it doesn’t look like I’m doing anything crazy...</div><div><br></div><div>And to test the code I am running the following….</div><div><br></div><div><div>require(xts)</div><div>require(Rcpp)</div><div># 500 days</div><div>n <- 60*24*500</div><div><br></div><div># minutely standard dev</div><div>sd_m <- 0.003</div><div><br></div><div>#minutely mean</div><div>mu_m <- 5e-6</div><div><br></div><div># Rate at which entry/exits triggered</div><div>entry_rate <- 0.002</div><div>exit_rate <- 0.002</div><div><br></div><div># set seed</div><div>set.seed(1)</div><div><br></div><div>x <- xts(100*cumprod(1+rnorm(n,mu_m,sd_m)),</div><div>         as.POSIXct(Sys.Date())+(60*c(1:n)))</div><div>x <- round(to.minutes5(x,name="ABC"),2)</div><div>x$rand_entry <- rpois(nrow(x),entry_rate)</div><div>x$rand_exit <- rpois(nrow(x),exit_rate)</div><div><br></div><div>Rcpp::sourceCpp('test.cpp')</div><div><br></div><div>init_pos <- 0</div><div><br></div><div>clock_time_mins <- 120</div><div><br></div><div>res <- lapply(1:100, function(z) {</div><div>  entryexitclock(x,match("rand_entry",colnames(x)),</div><div>                 match("rand_exit",colnames(x)),</div><div>                 clock_time_mins*60,init_pos)</div><div>})</div><div><br></div><div>The error obtained when adding in the last part of code is:</div><div><br></div><div><div> *** caught segfault ***</div><div>address 0x10d0cfc40, cause 'memory not mapped'</div><div><br></div><div>Traceback:</div><div> 1: .Primitive(".Call")(<pointer: 0x10155d2e0>, xts_obj_, ent_col,     exit_col, clock_limit, init_pos)</div><div> 2: entryexitclock(x, match("rand_entry", colnames(x)), match("rand_exit",     colnames(x)), clock_time_mins * 60, init_pos)</div><div> 3: FUN(1:100[[3L]], ...)</div><div> 4: lapply(1:100, function(z) {    entryexitclock(x, match("rand_entry", colnames(x)), match("rand_exit",         colnames(x)), clock_time_mins * 60, init_pos)})</div><div><br></div><div>Possible actions:</div><div>1: abort (with core dump, if enabled)</div><div>2: normal R exit</div><div>3: exit R without saving workspace</div><div>4: exit R saving workspace</div></div><div><br></div><div>Interestingly, from the traceback it suggests it fails on the third attempt (z=3) rather than the first or second...</div><div><br></div><div>Any suggestions to help fix this issue would be greatly be appreciated…</div><div><br></div><div>hlm</div><div><br></div><div><br></div></div><div><br></div><div><br><div><br><div><div>On 20 Feb 2014, at 16:48, Sameer D'Costa <<a href="mailto:sameerdcosta@gmail.com">sameerdcosta@gmail.com</a>> wrote:</div><br class="Apple-interchange-newline"><blockquote type="cite"><div dir="ltr">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. <div><br></div><div>/*  test.cpp */</div><div><div>#include <Rcpp.h></div>
<div>using namespace Rcpp;</div><div><br></div><div>// [[Rcpp::export]]</div><div>List entryexitclock( SEXP xts_obj_,int ent_col, int exit_col, double clock_limit, int init_pos) {</div><div>  // function takes in xts_obj_ (an xts object from R) and converts to a NumericMatrix</div>
<div>  Rcpp::NumericMatrix x(xts_obj_);</div><div>  // Get the index and call v</div><div>  DatetimeVector v(NumericVector(RObject(xts_obj_).attr("index")));</div><div>  // The dimensions of rtn_xts are extracted</div>
<div>  int nr = x.nrow();</div><div>  List l;</div><div>  return (l);</div><div>}</div></div><div><br></div><div>Then on the R side you can test it out</div><div><div>library(Rcpp)</div><div>library(xts)</div><div>
sourceCpp("test.cpp")</div></div><div><div>n <- 10000</div><div>k <- xts(as.numeric(1:n), Sys.time()+1:n)<br></div></div><div><div>res <- lapply(1:10000, function(z) entryexitclock(k, 12, 12, 60*60,0))</div>
</div><div><br></div><div>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?</div><div><br></div><div>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.</div>
<div><br></div><div>Regards</div><div>Sameer</div></div><div class="gmail_extra"><br><br><div class="gmail_quote">On Thu, Feb 20, 2014 at 9:50 AM, Hideyoshi Maeda <span dir="ltr"><<a href="mailto:hideyoshi.maeda@gmail.com" target="_blank">hideyoshi.maeda@gmail.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Hi I am trying to run a function I created in Rcpp, which is then sourced/compiled and executed in R.<br>
<br>
The function is called “entryexitclock”.<br>
<br>
and starts off as follows:<br>
<br>
#include <Rcpp.h><br>
using namespace Rcpp;<br>
<br>
// [[Rcpp::export]]<br>
List entryexitclock( SEXP xts_obj_,int ent_col, int exit_col, double clock_limit, int init_pos) {<br>
  // function takes in xts_obj_ (an xts object from R) and converts to a NumericMatrix<br>
  Rcpp::NumericMatrix x(xts_obj_);<br>
  // Get the index and call v<br>
  DatetimeVector v(NumericVector(RObject(xts_obj_).attr("index")));<br>
  // The dimensions of rtn_xts are extracted<br>
  int nr = x.nrow();<br>
<br>
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...<br>
<br>
The compilation works fine, however “sometimes” the code works and sometimes the code doesn’t…<br>
<br>
The error when it doesn't is as follows:<br>
<br>
<br>
 *** caught segfault ***<br>
address 0x11a9783c8, cause 'memory not mapped'<br>
<br>
Traceback:<br>
 1: .Primitive(".Call")(<pointer: 0x1044dde70>, xts_obj_, ent_col,     exit_col, clock_limit, init_pos)<br>
 2: entryexitclock(k, 11, 12, 60 * 60, 0)<br>
<br>
Possible actions:<br>
1: abort (with core dump, if enabled)<br>
2: normal R exit<br>
3: exit R without saving workspace<br>
4: exit R saving workspace<br>
<br>
Any suggestions as to what I can do to make sure that this function works every time rather than just sometimes?<br>
<br>
HLM<br>
<br>
<br>
<br>
_______________________________________________<br>
Rcpp-devel mailing list<br>
<a href="mailto:Rcpp-devel@lists.r-forge.r-project.org">Rcpp-devel@lists.r-forge.r-project.org</a><br>
<a href="https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel" target="_blank">https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel</a><br>
</blockquote></div><br></div>
</blockquote></div><br></div></div></body></html>