Skip to content

[Rcpp-devel] Rcpp function variable which is given as argument

3 messages · Mario Deng, Dirk Eddelbuettel, Krzysztof Sakrejda

#
Hello everyone,

I am observing a, to me, strange behaviour. Rcpp seems to modify my variable, which is given as argument. Here is my code:

In R am am running:

Rcpp::sourceCpp("~/.../VectorList/VectorList.cpp")
foo=matrix(runif(52), ncol=4)
colnames(foo)=paste("X",1:ncol(foo),sep="")
rownames(foo)=paste("Y",1:nrow(foo),sep="")

Until here foo looks like:
X1        X2        X3         X4
Y1  0.2786725 0.4024807 0.4968645 0.80620023
Y2  0.6441421 0.3540515 0.7312126 0.03784105
Y3  0.9090161 0.4328849 0.6004133 0.69923867
Y4  0.2553254 0.5678732 0.7917617 0.25911206
Y5  0.2667057 0.3320151 0.7481575 0.90253673
Y6  0.2635481 0.4849927 0.9537019 0.73662740
Y7  0.4501414 0.9451928 0.8241006 0.54619146
Y8  0.9177439 0.9556434 0.4024955 0.05961032
Y9  0.3763357 0.7825898 0.1443202 0.27215527
Y10 0.5892217 0.6663081 0.3577415 0.69333984
Y11 0.9915392 0.4184379 0.6896782 0.22941638
Y12 0.5609363 0.6079867 0.7183952 0.24935215
Y13 0.8792352 0.8951102 0.4679093 0.19242862

After running createVectorList(foo) (which is a Rcpp function) foo looks like (it's sorted):
X1        X2        X3         X4
Y1  0.2553254 0.3320151 0.1443202 0.03784105
Y2  0.2635481 0.3540515 0.3577415 0.05961032
Y3  0.2667057 0.4024807 0.4024955 0.19242862
Y4  0.2786725 0.4184379 0.4679093 0.22941638
Y5  0.3763357 0.4328849 0.4968645 0.24935215
Y6  0.4501414 0.4849927 0.6004133 0.25911206
Y7  0.5609363 0.5678732 0.6896782 0.27215527
Y8  0.5892217 0.6079867 0.7183952 0.54619146
Y9  0.6441421 0.6663081 0.7312126 0.69333984
Y10 0.8792352 0.7825898 0.7481575 0.69923867
Y11 0.9090161 0.8951102 0.7917617 0.73662740
Y12 0.9177439 0.9451928 0.8241006 0.80620023
Y13 0.9915392 0.9556434 0.9537019 0.90253673

I don't perform any allocation for foo. Why is this Rcpp function (shown below) modifying foo?
Here is the code I am behind createVectorList()

#include <Rcpp.h>
using namespace Rcpp;

// [[Rcpp::export]]
List createVectorList(NumericMatrix df) {
  CharacterVector rownames = VECTOR_ELT(df.attr("dimnames"), 0);
  CharacterVector colnames = VECTOR_ELT(df.attr("dimnames"), 1);
  // Check Dimnames
  if (df.nrow() != rownames.size() ) { 
    throw Rcpp::exception("Dimensions and corrosponding names differ in length");
  }
  List vectorList(df.ncol());
  for(int i = 0; i < df.ncol(); i++){
    NumericVector sorted_vec = df(_,i);
    sorted_vec=sorted_vec.sort();
    NumericVector one_col = df(_,i);
    df(_,i)=sorted_vec;
    IntegerVector matched;
    matched = match(sorted_vec, one_col);
    sorted_vec.attr("names")=rownames[matched-1];
    vectorList[i]=sorted_vec;
  }
  vectorList.attr("names") = colnames;
  CharacterVector classIDs = CharacterVector::create("VectorList", "list");
  vectorList.attr("class") = classIDs;
  return vectorList;
}


Is there anything I am missing? I mean, how can Rcpp even access my variables?!

With all the best,

Mario
__
MD
mariodeng at googlemail.com
#
On 9 July 2014 at 12:21, Mario Deng wrote:
| Hello everyone,
| 
| I am observing a, to me, strange behaviour. Rcpp seems to modify my variable, which is given as argument. Here is my code:
| 
| In R am am running:
| 
| Rcpp::sourceCpp("~/.../VectorList/VectorList.cpp")
| foo=matrix(runif(52), ncol=4)
| colnames(foo)=paste("X",1:ncol(foo),sep="")
| rownames(foo)=paste("Y",1:nrow(foo),sep="")

Creates foo.
| 
| Until here foo looks like:
| > foo
|            X1        X2        X3         X4
| Y1  0.2786725 0.4024807 0.4968645 0.80620023
| Y2  0.6441421 0.3540515 0.7312126 0.03784105
| Y3  0.9090161 0.4328849 0.6004133 0.69923867
| Y4  0.2553254 0.5678732 0.7917617 0.25911206
| Y5  0.2667057 0.3320151 0.7481575 0.90253673
| Y6  0.2635481 0.4849927 0.9537019 0.73662740
| Y7  0.4501414 0.9451928 0.8241006 0.54619146
| Y8  0.9177439 0.9556434 0.4024955 0.05961032
| Y9  0.3763357 0.7825898 0.1443202 0.27215527
| Y10 0.5892217 0.6663081 0.3577415 0.69333984
| Y11 0.9915392 0.4184379 0.6896782 0.22941638
| Y12 0.5609363 0.6079867 0.7183952 0.24935215
| Y13 0.8792352 0.8951102 0.4679093 0.19242862
| 
| After running createVectorList(foo) (which is a Rcpp function) foo looks like (it's sorted):

Calls function with foo, so now function has access to foo.
 
| > foo
|            X1        X2        X3         X4
| Y1  0.2553254 0.3320151 0.1443202 0.03784105
| Y2  0.2635481 0.3540515 0.3577415 0.05961032
| Y3  0.2667057 0.4024807 0.4024955 0.19242862
| Y4  0.2786725 0.4184379 0.4679093 0.22941638
| Y5  0.3763357 0.4328849 0.4968645 0.24935215
| Y6  0.4501414 0.4849927 0.6004133 0.25911206
| Y7  0.5609363 0.5678732 0.6896782 0.27215527
| Y8  0.5892217 0.6079867 0.7183952 0.54619146
| Y9  0.6441421 0.6663081 0.7312126 0.69333984
| Y10 0.8792352 0.7825898 0.7481575 0.69923867
| Y11 0.9090161 0.8951102 0.7917617 0.73662740
| Y12 0.9177439 0.9451928 0.8241006 0.80620023
| Y13 0.9915392 0.9556434 0.9537019 0.90253673
| 
| I don't perform any allocation for foo. Why is this Rcpp function (shown below) modifying foo?
| Here is the code I am behind createVectorList()
| 
| #include <Rcpp.h>
| using namespace Rcpp;
| 
| // [[Rcpp::export]]
| List createVectorList(NumericMatrix df) {
|   CharacterVector rownames = VECTOR_ELT(df.attr("dimnames"), 0);
|   CharacterVector colnames = VECTOR_ELT(df.attr("dimnames"), 1);
|   // Check Dimnames
|   if (df.nrow() != rownames.size() ) { 
|     throw Rcpp::exception("Dimensions and corrosponding names differ in length");
|   }
|   List vectorList(df.ncol());
|   for(int i = 0; i < df.ncol(); i++){
|     NumericVector sorted_vec = df(_,i);
|     sorted_vec=sorted_vec.sort();

Sorts the variables in foo.

|     NumericVector one_col = df(_,i);
|     df(_,i)=sorted_vec;
|     IntegerVector matched;
|     matched = match(sorted_vec, one_col);
|     sorted_vec.attr("names")=rownames[matched-1];
|     vectorList[i]=sorted_vec;
|   }
|   vectorList.attr("names") = colnames;
|   CharacterVector classIDs = CharacterVector::create("VectorList", "list");
|   vectorList.attr("class") = classIDs;
|   return vectorList;
| }
| 
| 
| Is there anything I am missing? I mean, how can Rcpp even access my variables?!

Because you pass them in.

A variable passed via .Call is a SEXP where P stands for pointer.

If you want to work on __distinct copies__ you need to call clone().  Search
the list archives, or read my book on Rcpp.  This has all been discussed before.

Cheers, Dirk
 
| With all the best,
| 
| Mario
| __
| MD
| mariodeng at googlemail.com
| 
| 
| 
| 
| _______________________________________________
| 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
4 days later
#
Hi,

I'm not sure if this is an Rcpp problem or an rstan problem but reading
through the rstan code I don't see a reason why the rstan code should be
triggering it... so I'm looking for advice about how to debug it
further.  

I think the right etiquette is to just give a link to the issue, but
please let me know if it is better to post everything here.  This is
currently an open issue on the rstan issue tracker:

https://github.com/stan-dev/rstan/issues/76

The upshot is that when running a Stan model through Rstan for a small
amount of data the code runs fine.  With a larger amount of data it
segfaults the R session with a stack overflow after getting into
infinite recursion immediately after a call to R_ReleaseObject...

I left the rest of the details at the link (including code/data which
the rstan developers have been able to replicate the issue with).  The
backtrace (pre infinite recursion) is below and I would appreciate any
help in figuring out the next step in debugging this.

Sincerely,

Krzysztof

#521237 0x00007ffff79bc5c8 in ?? () from /usr/lib64/R/lib/libR.so
#521238 0x00007ffff79bc5c8 in ?? () from /usr/lib64/R/lib/libR.so
#521239 0x00007ffff79bc5c8 in ?? () from /usr/lib64/R/lib/libR.so
---Type <return> to continue, or q <return> to quit---
#521240 0x00007ffff79bd030 in R_ReleaseObject ()
from /usr/lib64/R/lib/libR.so #521241 0x00007ffff0d0c67e in void
std::_Destroy_aux<false>::__destroy<Rcpp::Vector<14,
Rcpp::PreserveStorage>*>(Rcpp::Vector<14, Rcpp::PreserveStorage>*,
Rcpp::Vector<14, Rcpp::PreserveStorage>*) ()
from /tmp/RtmplSIXA8/file3cc170510487.so #521242 0x00007ffff0d11391 in
std::vector<Rcpp::Vector<14, Rcpp::PreserveStorage>,
std::allocator<Rcpp::Vector<14, Rcpp::PreserveStorage> >
from /tmp/RtmplSIXA8/file3cc170510487.so
#521245 0x00007ffff0d45d8b in
rstan::stan_fit<model3cc116ebc25d_growth_model_namespace::model3cc116ebc25d_growth_model,
boost::random::additive_combine_engine<boost::random::linear_congruential_engine<unsigned
int, 40014u, 0u, 2147483563u>,
boost::random::linear_congruential_engine<unsigned int, 40692u, 0u,
2147483399u> > >::call_sampler(SEXPREC*) ()
2147483399u> > >from /tmp/RtmplSIXA8/file3cc170510487.so #521246
2147483399u> > >0x00007ffff0d47c06 in
2147483399u> > >Rcpp::class_<rstan::stan_fit<model3cc116ebc25d_growth_model_namespace::model3cc116ebc25d_growth_model,
2147483399u> > >boost::random::additive_combine_engine<boost::random::linear_congruential_engine<unsigned
2147483399u> > >int, 40014u, 0u, 2147483563u>,
2147483399u> > >boost::random::linear_congruential_engine<unsigned int,
2147483399u> > >40692u, 0u, 2147483399u> > >
2147483399u> > >>::invoke_notvoid(SEXPREC*, SEXPREC*, SEXPREC**, int)
2147483399u> > >>() from /tmp/RtmplSIXA8/file3cc170510487.so #521247
2147483399u> > >>0x00007ffff2f40fd3 in
2147483399u> > >>CppMethod__invoke_notvoid(SEXPREC*) ()
2147483399u> > >>from /home/krzysiek/R/x86_64-pc-linux-gnu-library/3.1/Rcpp/libs/Rcpp.so
2147483399u> > >>#521248 0x00007ffff7951019 in ?? ()
2147483399u> > >>from /usr/lib64/R/lib/libR.so
#521249 0x00007ffff79918f5 in ?? () from /usr/lib64/R/lib/libR.so
---Type <return> to continue, or q <return> to quit---
#521250 0x00007ffff7994e03 in ?? () from /usr/lib64/R/lib/libR.so
#521251 0x00007ffff7991717 in ?? () from /usr/lib64/R/lib/libR.so
#521252 0x00007ffff7996475 in Rf_applyClosure ()
from /usr/lib64/R/lib/libR.so #521253 0x00007ffff799146e in ?? ()
from /usr/lib64/R/lib/libR.so