[Rcpp-devel] Issue with Matrices and Iterators

2013-10-14 Thread Alessandro Mammana
Hi all,
I have very little experience with Rcpp and also with c++, so I am
sorry if my question might be too stupid.
I am trying to use iterators in my code, because I understand them
better than vectors, because they should be efficient, and because I
should be able to combine different implementations of vector and
matrix classes. However the following code does not compile:

#include 
using namespace Rcpp;

typedef NumericVector::iterator DoubleIter;

double sum(DoubleIter b, DoubleIter e){
double sum = 0;
while (b!=e){
sum += *(b++);
}
return sum;
}

// [[Rcpp::export]]
double sumFirstRow(NumericMatrix mat){
return sum(mat(0,_).begin(), mat(0,_).end());
}

The error message is not very useful (or at least I cannot interpret it):

In function 'double sumFirstRow(Rcpp::NumericMatrix)':
error: no matching function for call to
'sum(Rcpp::MatrixRow<14>::iterator, Rcpp::MatrixRow<14>::iterator)'
note: candidates are:
note: double sum(DoubleIter, DoubleIter)
note: no known conversion for argument 1 from
'Rcpp::MatrixRow<14>::iterator' to 'DoubleIter {aka double*}'

Am I missing something very basic?
Thanks a lot!
Ale
___
Rcpp-devel mailing list
Rcpp-devel@lists.r-forge.r-project.org
https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel


Re: [Rcpp-devel] Issue with Matrices and Iterators

2013-10-14 Thread Alessandro Mammana
You are right, now the code compiles! But why?
I understand that data is stored in column major. However I hoped that
using an iterator I would abstract away from how the data is stored
and that the function that increases the iterator takes care of
increasing the pointer of nrow positions...

Thanks again!
Ale

On Mon, Oct 14, 2013 at 9:39 PM, Matteo Fasiolo
 wrote:
> Hi Ale,
>
>  maybe I'm wrong but mat(0,_) is not a NumericVector, but a Rcpp::MatrixRow.
> So your function sum() is being called with arguments of the wront type (the
> error
> message is saying that).
>
> If instead you select a column mat( _, 0) you have a conversion to a
> NumericVector.
> I've encountered the same issue a couple of time.
>
> Matteo
>
>
> On Mon, Oct 14, 2013 at 8:21 PM, Alessandro Mammana 
> wrote:
>>
>> Hi all,
>> I have very little experience with Rcpp and also with c++, so I am
>> sorry if my question might be too stupid.
>> I am trying to use iterators in my code, because I understand them
>> better than vectors, because they should be efficient, and because I
>> should be able to combine different implementations of vector and
>> matrix classes. However the following code does not compile:
>>
>> #include 
>> using namespace Rcpp;
>>
>> typedef NumericVector::iterator DoubleIter;
>>
>> double sum(DoubleIter b, DoubleIter e){
>> double sum = 0;
>> while (b!=e){
>> sum += *(b++);
>> }
>> return sum;
>> }
>>
>> // [[Rcpp::export]]
>> double sumFirstRow(NumericMatrix mat){
>> return sum(mat(0,_).begin(), mat(0,_).end());
>> }
>>
>> The error message is not very useful (or at least I cannot interpret it):
>>
>> In function 'double sumFirstRow(Rcpp::NumericMatrix)':
>> error: no matching function for call to
>> 'sum(Rcpp::MatrixRow<14>::iterator, Rcpp::MatrixRow<14>::iterator)'
>> note: candidates are:
>> note: double sum(DoubleIter, DoubleIter)
>> note: no known conversion for argument 1 from
>> 'Rcpp::MatrixRow<14>::iterator' to 'DoubleIter {aka double*}'
>>
>> Am I missing something very basic?
>> Thanks a lot!
>> Ale
>> ___
>> Rcpp-devel mailing list
>> Rcpp-devel@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@lists.r-forge.r-project.org
https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel


[Rcpp-devel] Suggestions for possible optimizations

2013-10-23 Thread Alessandro Mammana
Dear Rcpp developers,

I am optimizing some R code by replacing some loops with Rcpp code.
After some profiling, it looks like the cpp function below is still
the limiting step. Does anybody see some possible source of
inefficiency or something easy to improve? Or is it already as fast as
it can get? Thanks a lot.

#include 

using namespace Rcpp;

// [[Rcpp::export]]
List llik2posteriors(NumericMatrix lliks){
NumericMatrix posteriors(lliks.nrow(), lliks.ncol());
double llik = 0;
double cmax;
double psum;
for (int i = 0; i < lliks.ncol(); ++i){
MatrixColumn llikrow = lliks.column(i);
MatrixColumn postrow = posteriors.column(i);
cmax = max(llikrow);
llik += cmax;
postrow = exp(llikrow - cmax);
psum = sum(postrow);
llik += log(psum);
postrow = postrow/psum;
}

return List::create(Named("posteriors")=posteriors, Named("tot_llik")=llik);
}

Alessandro
___
Rcpp-devel mailing list
Rcpp-devel@lists.r-forge.r-project.org
https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel


[Rcpp-devel] Interfacing Rcpp with a C library: memory allocation

2013-11-20 Thread Alessandro Mammana
Dear all,
I'm trying to write some efficient code for analyzing sequencing data
in R. To do this I would like to use the C library samtools. I've
created a package where the src directory looks like this:

src
|-- Makevars
|-- RcppExports.cpp
|-- mysourcecode.cpp
`-- samtools
|-- all *.c and *.h files as well as an independent Makefile

My Makevars file looks like this:

PKG_CPPFLAGS = -Isamtools
PKG_LIBS = `$(R_HOME)/bin/Rscript -e "Rcpp:::LdFlags()"`
PKG_LIBS += -Lsamtools  -lbam -lz -lpthread

$(SHLIB): samtools/libbam.a

samtools/libbam.a:
  @(cd samtools-0.1.19 && $(MAKE) libbam.a \
  CC="$(CC)" CFLAGS="$(CFLAGS) $(CPICFLAGS)" AR="$(AR)"
RANLIB="$(RANLIB)")

Everything compiles and I get my cpp functions in R, however I am
getting some weird segfaults, I think they are due to memory
allocation, but it's hard for me to track them. Especially now, these
errors are showing up not immediately, but at the second time that I
call a Rcpp function.

I wanted to ask the following:
1. Is it the right way of using external C libraries? I couldn't find
much documentation around
2. The C library uses malloc and free, and so do I (as little as
possible, just to interface with the library), is this mechanism
clashing against Rcpp/R memory management? Could it happen, for
instance, that R tries to free allocated memory that I already
manually freed myself?

In general I didn't understand much about memory allocation in Rcpp
and I couldn't find many resources talking about it. Is there anything
R- or Rcpp-specific that I have to keep in mind or should I program as
if I were programming in C/C++?

Thanks a lot!


-- 
Alessandro Mammana, PhD Student
Max Planck Institute for Molecular Genetics
Ihnestraße 63-73
D-14195 Berlin, Germany
___
Rcpp-devel mailing list
Rcpp-devel@lists.r-forge.r-project.org
https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel


Re: [Rcpp-devel] Interfacing Rcpp with a C library: memory allocation

2013-11-20 Thread Alessandro Mammana
Thanks a lot for your help, I'll look at the references. I saw Section
6.1.2 already. Does that mean that I should replace  all the calls to
malloc() and to free() with those to Calloc() and Free() in the
external C code (maybe with a macro?)? Or does it just mean that I
would get the same memory errors, but this way they are handled by R?

Thanks a lot!
Ale

On Wed, Nov 20, 2013 at 6:35 PM, Dirk Eddelbuettel  wrote:
>
> On 20 November 2013 at 18:27, Alessandro Mammana wrote:
> | Dear all,
> | I'm trying to write some efficient code for analyzing sequencing data
> | in R. To do this I would like to use the C library samtools. I've
> | created a package where the src directory looks like this:
> |
> | src
> | |-- Makevars
> | |-- RcppExports.cpp
> | |-- mysourcecode.cpp
> | `-- samtools
> | |-- all *.c and *.h files as well as an independent Makefile
> |
> | My Makevars file looks like this:
> |
> | PKG_CPPFLAGS = -Isamtools
> | PKG_LIBS = `$(R_HOME)/bin/Rscript -e "Rcpp:::LdFlags()"`
> | PKG_LIBS += -Lsamtools  -lbam -lz -lpthread
> |
> | $(SHLIB): samtools/libbam.a
> |
> | samtools/libbam.a:
> |   @(cd samtools-0.1.19 && $(MAKE) libbam.a \
> |   CC="$(CC)" CFLAGS="$(CFLAGS) $(CPICFLAGS)" AR="$(AR)"
> | RANLIB="$(RANLIB)")
> |
> | Everything compiles and I get my cpp functions in R, however I am
> | getting some weird segfaults, I think they are due to memory
> | allocation, but it's hard for me to track them. Especially now, these
> | errors are showing up not immediately, but at the second time that I
> | call a Rcpp function.
> |
> | I wanted to ask the following:
> | 1. Is it the right way of using external C libraries? I couldn't find
> | much documentation around
>
> Sure.
>
> | 2. The C library uses malloc and free, and so do I (as little as
> | possible, just to interface with the library), is this mechanism
> | clashing against Rcpp/R memory management? Could it happen, for
> | instance, that R tries to free allocated memory that I already
> | manually freed myself?
>
> Yes.
>
> Please read the "Writing R Extensions" manual, section 6.1.2, on
> User-controlled memory.
>
> And/or see the Rcpp documentation: if you actually use our data containers,
> things "just work", ie
>
>   R> cppFunction("NumericMatrix foo(int n) { return NumericMatrix(n); }")
>   R> foo(2)
>[,1] [,2]
>   [1,]00
>   [2,]00
>   R> foo(4)
>[,1] [,2] [,3] [,4]
>   [1,]0000
>   [2,]0000
>   [3,]0000
>   [4,]0000
>   R>
>
> will never create a segfault. Ditto for using proper C++ containers (eg
> std::vector).
>
> But you cannot randomly match the C approach (see Section 6.1.2, as mentioned
> above) and the R/Rcpp approach.
>
> | In general I didn't understand much about memory allocation in Rcpp
> | and I couldn't find many resources talking about it. Is there anything
>
> Well I could recommend a book to you...  There are also eight vignettes in
> the package, and the more on other sites (the list archive, our blogs,
> StackOverlow, Hadley's draft book).
>
> | R- or Rcpp-specific that I have to keep in mind or should I program as
> | if I were programming in C/C++?
>
> You can provided you do it right.
>
> Hope this helps, if it was unclear please come back with more questions.
> Also see the unit tests and examples.
>
> Cheers, Dirk
>
> --
> Dirk Eddelbuettel | e...@debian.org | http://dirk.eddelbuettel.com



-- 
Alessandro Mammana, PhD Student
Max Planck Institute for Molecular Genetics
Ihnestraße 63-73
D-14195 Berlin, Germany
___
Rcpp-devel mailing list
Rcpp-devel@lists.r-forge.r-project.org
https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel


Re: [Rcpp-devel] Interfacing Rcpp with a C library: memory allocation

2013-11-21 Thread Alessandro Mammana
I found out what the problem was, luckily it was just a stupid bug in
my code (an array index out of bounds).
I still didn't quite get how memory allocation works. As far as I
understood there are these two ways of allocating memory:

1. Using malloc/free, new/delete, normal C++ constructors and
destructors: this memory area is completely separated from R memory
area and it is erased at the end of the Rcpp call. Pointers to these
addresses will not be valid in a second Rcpp call.
2. Using Rcpp constructors/destructors, Calloc() and Free(): this is
the same memory used by R and it is suitable for passing objects in
and out.

Is that right?
Thanks again,
Ale


On Wed, Nov 20, 2013 at 7:25 PM, Dirk Eddelbuettel  wrote:
>
> On 20 November 2013 at 18:54, Alessandro Mammana wrote:
> | Thanks a lot for your help, I'll look at the references. I saw Section
> | 6.1.2 already. Does that mean that I should replace  all the calls to
> | malloc() and to free() with those to Calloc() and Free() in the
> | external C code (maybe with a macro?)? Or does it just mean that I
> | would get the same memory errors, but this way they are handled by R?
>
> The idea is not to get errors.
>
> Let's assume that the library you use is in itself correct.  You could then
> use it and write wrapper functions. In the wrapper functions, you create Rcpp
> type and assign to those. That would avoid having to alter the library. A
> good defensive approach.
>
> Depending on how well you know what you are doing, you can do something like
> too where we assume we have  someFunction(double* p):
>
> int n;
>
> Rcpp::NumericVector x(n);  // vector of n element
>
> someFunction(x.begin());   // someFunction sees a double*
>
> which is how I would interface a C library.  The devil is in the detail, and
> you still have not provide any so we can't help you much more than via
> generalities.
>
> Pick one function from the library. Interface it. Get it work without a
> segfault.  Apply what you learned on another function etc pp.
>
> There are other approaches (Rcpp modules is one) but you may to make sure
> you clear a few conceptual hurdles first.
>
> Dirk
>
>
>
> |
> | Thanks a lot!
> | Ale
> |
> | On Wed, Nov 20, 2013 at 6:35 PM, Dirk Eddelbuettel  wrote:
> | >
> | > On 20 November 2013 at 18:27, Alessandro Mammana wrote:
> | > | Dear all,
> | > | I'm trying to write some efficient code for analyzing sequencing data
> | > | in R. To do this I would like to use the C library samtools. I've
> | > | created a package where the src directory looks like this:
> | > |
> | > | src
> | > | |-- Makevars
> | > | |-- RcppExports.cpp
> | > | |-- mysourcecode.cpp
> | > | `-- samtools
> | > | |-- all *.c and *.h files as well as an independent Makefile
> | > |
> | > | My Makevars file looks like this:
> | > |
> | > | PKG_CPPFLAGS = -Isamtools
> | > | PKG_LIBS = `$(R_HOME)/bin/Rscript -e "Rcpp:::LdFlags()"`
> | > | PKG_LIBS += -Lsamtools  -lbam -lz -lpthread
> | > |
> | > | $(SHLIB): samtools/libbam.a
> | > |
> | > | samtools/libbam.a:
> | > |   @(cd samtools-0.1.19 && $(MAKE) libbam.a \
> | > |   CC="$(CC)" CFLAGS="$(CFLAGS) $(CPICFLAGS)" AR="$(AR)"
> | > | RANLIB="$(RANLIB)")
> | > |
> | > | Everything compiles and I get my cpp functions in R, however I am
> | > | getting some weird segfaults, I think they are due to memory
> | > | allocation, but it's hard for me to track them. Especially now, these
> | > | errors are showing up not immediately, but at the second time that I
> | > | call a Rcpp function.
> | > |
> | > | I wanted to ask the following:
> | > | 1. Is it the right way of using external C libraries? I couldn't find
> | > | much documentation around
> | >
> | > Sure.
> | >
> | > | 2. The C library uses malloc and free, and so do I (as little as
> | > | possible, just to interface with the library), is this mechanism
> | > | clashing against Rcpp/R memory management? Could it happen, for
> | > | instance, that R tries to free allocated memory that I already
> | > | manually freed myself?
> | >
> | > Yes.
> | >
> | > Please read the "Writing R Extensions" manual, section 6.1.2, on
> | > User-controlled memory.
> | >
> | > And/or see the Rcpp documentation: if you actually use our data 
> containers,
> | > things "just work", ie
> | >
> | >   R> cppFunction("NumericMatrix foo(int n) { return NumericMatrix(n); }")
> | >   R> foo(

[Rcpp-devel] Some beginner questions

2013-11-24 Thread Alessandro Mammana
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@values ~ c(1, 2, 3)
attributes(rle@values)$levels ~ c("a","b","c")
rle@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@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 names;
public:
rleIter(RObject& rle):
rlens(as(rle.slot("lengths"))), // is here
the vector copied?
values(as(rle.slot("values"))),
names(as >(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(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(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(gr.slot("seqnames"));
so that I am sure that the vector is not copied, the compiler
complains saying that
as(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@lists.r-forge.r-project.org
https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel


[Rcpp-devel] Problems wrapping stl::vector

2014-01-03 Thread Alessandro Mammana
Dear all,
thank you again for this fantastic library.
Sorry for the very stupid question, I looked before posting, but I
couldn't find anything.
I am encountering a problem when I try to do something very basic: I
am trying to wrap a std::vector to an IntegerVector:

In the file scratchpad.cpp:

// [[Rcpp::export]]
IntegerVector test(){
std::vector a;
a.push_back(1);a.push_back(2);a.push_back(3);
return wrap(a);
}

when doing:
>sourceCpp("scratchpad.cpp")

I get the error:

scratchpad.cpp: In function 'Rcpp::IntegerVector test()':
scratchpad.cpp:181:30: error: no matching function for call to
'wrap(std::vector&)'
scratchpad.cpp:181:30: note: candidates are:
In file included from
/package/mariux64/R/R-3.0.2/lib/R/library/Rcpp/include/RcppCommon.h:120:0,
 from
/package/mariux64/R/R-3.0.2/lib/R/library/Rcpp/include/Rcpp.h:27,
 from scratchpad.cpp:2:
/package/mariux64/R/R-3.0.2/lib/R/library/Rcpp/include/Rcpp/internal/wrap.h:924:13:
note: template SEXPREC* Rcpp::wrap(const T&)
/package/mariux64/R/R-3.0.2/lib/R/library/Rcpp/include/Rcpp/internal/wrap.h:924:13:
note:   template argument deduction/substitution failed:
scratchpad.cpp:181:30: note:   cannot convert 'a' (type
'std::vector') to type 'const Rcpp::Vector<13>&'
In file included from
/package/mariux64/R/R-3.0.2/lib/R/library/Rcpp/include/RcppCommon.h:120:0,
 from
/package/mariux64/R/R-3.0.2/lib/R/library/Rcpp/include/Rcpp.h:27,
 from scratchpad.cpp:2:
/package/mariux64/R/R-3.0.2/lib/R/library/Rcpp/include/Rcpp/internal/wrap.h:965:13:
note: template SEXPREC* Rcpp::wrap(InputIterator,
InputIterator)
/package/mariux64/R/R-3.0.2/lib/R/library/Rcpp/include/Rcpp/internal/wrap.h:965:13:
note:   template argument deduction/substitution failed:
scratchpad.cpp:181:30: note:   cannot convert 'a' (type
'std::vector') to type 'Rcpp::Vector<13>'
scratchpad.cpp:183:1: warning: control reaches end of non-void
function [-Wreturn-type]
make: *** [scratchpad.o] Error 1
g++ -I/package/mariux64/R/R-3.0.2/lib/R/include -DNDEBUG
-I/usr/local/include
-I"/package/mariux64/R/R-3.0.2/lib/R/library/Rcpp/include"-fpic
-g -O3 -Wall -pedantic -c scratchpad.cpp -o scratchpad.o
Error in sourceCpp("scratchpad.cpp") :
Error 1 occurred building shared library.

I must definitely be doing something very stupid. But what?

thanks in advance!
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@lists.r-forge.r-project.org
https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel


Re: [Rcpp-devel] Problems wrapping stl::vector

2014-01-03 Thread Alessandro Mammana
Thanks a lot, it works!
I thought I tried to return the variable a directly without template
specialization but apparently I didn't T.T However I tried this:
IntegerVector x = wrap(a);
and it still didn't work...

I'll remember this, thanks again!
Ale

On Fri, Jan 3, 2014 at 5:23 PM, Romain Francois
 wrote:
>
> Hello,
>
> wrap is for converting an object of arbitrary class to an R object. It 
> deduces what to do based on the class of what you pass it. So you usually 
> don’t specify the template parameter. So you’d usually just write :
>
> return wrap(a);
>
> If you wanted to be explicit, then you would use:
>
> return wrap< std::vector >( a ) ;
>
> wrap returns a « SEXP » (any R object) which can be used by an IntegerVector 
> constructor. So you can do:
>
> IntegerVector x( wrap(a) ) ;
>
> or even:
>
> IntegerVector  = as( wrap(a) ) ;
>
> Romain
>
> Le 3 janv. 2014 à 17:18, Alessandro Mammana  a écrit :
>
>> Dear all,
>> thank you again for this fantastic library.
>> Sorry for the very stupid question, I looked before posting, but I
>> couldn't find anything.
>> I am encountering a problem when I try to do something very basic: I
>> am trying to wrap a std::vector to an IntegerVector:
>>
>> In the file scratchpad.cpp:
>>
>> // [[Rcpp::export]]
>> IntegerVector test(){
>>std::vector a;
>>a.push_back(1);a.push_back(2);a.push_back(3);
>>return wrap(a);
>> }
>>
>> when doing:
>>> sourceCpp("scratchpad.cpp")
>>
>> I get the error:
>>
>> scratchpad.cpp: In function 'Rcpp::IntegerVector test()':
>> scratchpad.cpp:181:30: error: no matching function for call to
>> 'wrap(std::vector&)'
>> scratchpad.cpp:181:30: note: candidates are:
>> In file included from
>> /package/mariux64/R/R-3.0.2/lib/R/library/Rcpp/include/RcppCommon.h:120:0,
>> from
>> /package/mariux64/R/R-3.0.2/lib/R/library/Rcpp/include/Rcpp.h:27,
>> from scratchpad.cpp:2:
>> /package/mariux64/R/R-3.0.2/lib/R/library/Rcpp/include/Rcpp/internal/wrap.h:924:13:
>> note: template SEXPREC* Rcpp::wrap(const T&)
>> /package/mariux64/R/R-3.0.2/lib/R/library/Rcpp/include/Rcpp/internal/wrap.h:924:13:
>> note:   template argument deduction/substitution failed:
>> scratchpad.cpp:181:30: note:   cannot convert 'a' (type
>> 'std::vector') to type 'const Rcpp::Vector<13>&'
>> In file included from
>> /package/mariux64/R/R-3.0.2/lib/R/library/Rcpp/include/RcppCommon.h:120:0,
>> from
>> /package/mariux64/R/R-3.0.2/lib/R/library/Rcpp/include/Rcpp.h:27,
>> from scratchpad.cpp:2:
>> /package/mariux64/R/R-3.0.2/lib/R/library/Rcpp/include/Rcpp/internal/wrap.h:965:13:
>> note: template SEXPREC* Rcpp::wrap(InputIterator,
>> InputIterator)
>> /package/mariux64/R/R-3.0.2/lib/R/library/Rcpp/include/Rcpp/internal/wrap.h:965:13:
>> note:   template argument deduction/substitution failed:
>> scratchpad.cpp:181:30: note:   cannot convert 'a' (type
>> 'std::vector') to type 'Rcpp::Vector<13>'
>> scratchpad.cpp:183:1: warning: control reaches end of non-void
>> function [-Wreturn-type]
>> make: *** [scratchpad.o] Error 1
>> g++ -I/package/mariux64/R/R-3.0.2/lib/R/include -DNDEBUG
>> -I/usr/local/include
>> -I"/package/mariux64/R/R-3.0.2/lib/R/library/Rcpp/include"-fpic
>> -g -O3 -Wall -pedantic -c scratchpad.cpp -o scratchpad.o
>> Error in sourceCpp("scratchpad.cpp") :
>> Error 1 occurred building shared library.
>>
>> I must definitely be doing something very stupid. But what?
>>
>> thanks in advance!
>> 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@lists.r-forge.r-project.org
>> https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel
>



-- 
Alessandro Mammana, PhD Student
Max Planck Institute for Molecular Genetics
Ihnestraße 63-73
D-14195 Berlin, Germany
___
Rcpp-devel mailing list
Rcpp-devel@lists.r-forge.r-project.org
https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel


[Rcpp-devel] Wierd compilation error

2014-01-29 Thread Alessandro Mammana
Hi all,
I was experimenting with templates and Rcpp and trying to compile this code:


// [[Rcpp::export]]
static void readVector(Vector v){
traits::storage_type::type i = v[0];
Rcout << i << std::endl;
}

I got this error:

scratchpad.cpp: In function 'SEXPREC* sourceCpp_3062_readVector(SEXP)':
scratchpad.cpp:337:21: error: variable or field '__result' declared void
scratchpad.cpp:338:9: error: '__result' was not declared in this scope
make: *** [scratchpad.o] Error 1

Also, the line numbers don't make any sense, because the file
scratchpad.cpp is only 250 lines long...

Do you know what could be wrong?

Thanks in advance!
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@lists.r-forge.r-project.org
https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel


[Rcpp-devel] Error when handling default values

2014-02-03 Thread Alessandro Mammana
Hi all,
once again I have a bug and no idea what I am doing wrong.
I started to handle optional arguments directly at the C++ level, so
that I don't have to write a wrapper function in R. The pattern below
has always worked so far, but today it crashed. It was very hard to
reproduce, It took ages to nail it down to the minimal example below.


in a file:

// [[Rcpp::export]]
void reproduce_error(int n,
SEXP ucs = R_NilValue,
SEXP mConst = R_NilValue){

if (Rf_isNull(ucs)){
IntegerVector v1(n);
IntegerVector v2(n);
ucs = (SEXP)  List::create(Named("map")=v1, Named("values")=v2);
}
if (Rf_isNull(mConst)){
NumericVector v1(n);
mConst = (SEXP) v1;
}

List ucs_list(ucs);
IntegerVector map = ucs_list["map"];
Rcout << "before" << std::endl;
IntegerVector uniqueCS = ucs_list["values"];
Rcout << "after" << std::endl;
NumericVector multinomConst(mConst);
}

from R's terminal:

>library(microbenchmark)
>sourceCpp("scratchpad.cpp")
>microbenchmark(reproduce_error(100), times=100)
before
after
before
after
before
after
before
after
before
after
before
after
before
after
before
after
before
after
before
after
before
Error: not compatible with INTSXP

What am I doing wrong?
Do you have other ideas about how I could handle NULL values?
Maybe I should avoid to rewrap the list in a SEXP, but this method
allows me to write very little code and no need for an "else" after
"if (Rf_isNull(ucs))"

I think the problem has to do with the way R manages its memory.
Correct me if I am wrong. If ucs was a normal stl container (with a
particular NULL state), then when I do:

ucs = (SEXP)  List::create(Named("map")=v1, Named("values")=v2);

ucs becomes the owner of the list, and the list is the owner of
vectors v1 and v2 (no idea if they are getting copied there by the
way..., with stl vectors I guess they would get copied), so when ucs
goes out of scope it tries to free the memory allocated for the list,
and the list tries to free the memory allocated for v1 and v2.

But because ucs is not a normal stl container, I guess I need some
other rules of thumb to understand how I should manage memory.

Thanks a lot in advance, and, don't get me wrong, I still love Rcpp :D


-- 
Alessandro Mammana, PhD Student
Max Planck Institute for Molecular Genetics
Ihnestraße 63-73
D-14195 Berlin, Germany
___
Rcpp-devel mailing list
Rcpp-devel@lists.r-forge.r-project.org
https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel


[Rcpp-devel] Segfault, is it because of iterators/pointers?

2014-02-11 Thread Alessandro Mammana
Hi all,
I got another segfault using Rcpp. It is very difficult to understand
where it happens and to reduce it to a minimal example, so for now I
am not posting very precise code here, but I have a suspicion, maybe
you could help me saying if my suspect is right.

I am doing something similar:

in a .cpp file:
@@@
struct GapMat {
int* ptr;
int* colset;
int nrow;
int ncol;


inline int* colptr(int col){
return ptr + colset[col];
}

GapMat(){}

GapMat(int* _ptr, int* _colset, int _nrow, int _ncol):
ptr(_ptr), colset(_colset), nrow(_nrow), ncol(_ncol){}
};


// [[Rcpp::export]]
IntegerVector colSumsGapMat(Rcpp::IntegerVector vec,
Rcpp::IntegerVector pos, int nrow){
   GapMat mat(vec.begin(), pos.begin(), nrow, pos.length());
   IntegerVector res(pos.length());

for (int i = 0; i < pos.length(); ++i){
for (int j = 0; j < nrow; ++j){
res[i] += mat.colptr(i)[j];
}
}

return res;
}
@

from R:

vec <- a very big integer vector
nrow <- 80
pos <- a very big subset of positions, such that max(pos) + nrow < length(vec)
colsums <- colSumsGapMat(vec, pos, nrow)


from time to time I get a segfault.
Note: this is not exactly the code that produces the segfault (because
that one is very complicated), so it might be that this code is
totally fine.

My suspicion:

I am using the pointer "vec.begin()", but then I am allocating new
memory in the R area of memory with "IntegerVector res(pos.length())"
and R decides to move the original values of "vec" to some other
place, making the pointer invalid.

Is that possible

Sorry for being very vague and thx in advance!!!
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@lists.r-forge.r-project.org
https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel


Re: [Rcpp-devel] Segfault, is it because of iterators/pointers?

2014-02-12 Thread Alessandro Mammana
Ok I was able to find the code causing the bug. So it looks like the
pointers you get from an Rcpp::Vector using .begin() become invalid
after that the Rcpp::Vector goes out of scope (and this makes sense),
what I do not understand is that this Rcpp::Vector was allocated in R
and should still be "living" during the execution of the Rcpp call
(that's why I wasn't expecting the pointer to be invalid).

This is the exact code (the one above is probably fine):
@@ in CPP @@i

struct GapMat {
int* ptr;
int* colset;
int nrow;
int ncol;


inline int* colptr(int col){
return ptr + colset[col];
}

GapMat(){}

GapMat(int* _ptr, int* _colset, int _nrow, int _ncol):
ptr(_ptr), colset(_colset), nrow(_nrow), ncol(_ncol){}
};


GapMat getGapMat(Rcpp::List gapmat){
IntegerVector vec = gapmat["vec"];
IntegerVector pos = gapmat["colset"];
int nrow = gapmat["nrow"];

return GapMat(vec.begin(), pos.begin(), nrow, pos.length());
}

// [[Rcpp::export]]
IntegerVector colSumsGapMat(Rcpp::List gapmat){

GapMat mat = getGapMat(gapmat);
IntegerVector res(mat.ncol);

for (int i = 0; i < mat.ncol; ++i){
for (int j = 0; j < mat.nrow; ++j){
res[i] += mat.colptr(i)[j];
}
}

return res;
}

@@ in R (with gdb debugger as suggested by Dirk) @@i
library(Rcpp)
sourceCpp("scratchpad.cpp")

vec <- rnbinom(3e7, mu=0.1, size=1); storage.mode(vec) <- "integer"
nr <- 80

colset <- sample(3e7-nr, 1e7)
foo <- vec[colset] #this is only to trigger some obscure garbage
collection mechanisms...

for (i in 1:10){
colset <- sample(3e7-nr, 1e7)
gapmat <- list(vec=vec, nrow=nr, colset=colset-1)
cs <- colSumsGapMat(gapmat)
print(sum(cs))
}

[1] 8000
[1] 8000
[1] 80016890
[1] 80008144
[1] 80016022
[1] 80021609

Program received signal SIGSEGV, Segmentation fault.
0x718a5455 in GapMat::colptr (this=0x7fffc120, col=0) at
scratchpad.cpp:295
295return ptr + colset[col];

@@@

Why did it happen? What should I do to make sure that my pointers
remain valid? My goal is to convert safely some vectors or matrices
that "exist" in R to some pointers, how can I do that?

Thanks a lot for your help

Ale

On Tue, Feb 11, 2014 at 3:44 PM, Dirk Eddelbuettel  wrote:
>
> In essence: "Yes"
>
> On 11 February 2014 at 15:18, Alessandro Mammana wrote:
> | Hi all,
> | I got another segfault using Rcpp. It is very difficult to understand
> | where it happens and to reduce it to a minimal example, so for now I
> | am not posting very precise code here, but I have a suspicion, maybe
> | you could help me saying if my suspect is right.
>
> Use a debugger:
>
> R -d gdb
>
> and then proceed as normal.
>
> Make sure your compiler flags include -g as well.
>
> Dirk
>
>
> | I am doing something similar:
> |
> | in a .cpp file:
> | @@@
> | struct GapMat {
> | int* ptr;
> | int* colset;
> | int nrow;
> | int ncol;
> |
> |
> | inline int* colptr(int col){
> | return ptr + colset[col];
> | }
> |
> | GapMat(){}
> |
> | GapMat(int* _ptr, int* _colset, int _nrow, int _ncol):
> | ptr(_ptr), colset(_colset), nrow(_nrow), ncol(_ncol){}
> | };
> |
> |
> | // [[Rcpp::export]]
> | IntegerVector colSumsGapMat(Rcpp::IntegerVector vec,
> | Rcpp::IntegerVector pos, int nrow){
> |GapMat mat(vec.begin(), pos.begin(), nrow, pos.length());
> |IntegerVector res(pos.length());
> |
> | for (int i = 0; i < pos.length(); ++i){
> | for (int j = 0; j < nrow; ++j){
> | res[i] += mat.colptr(i)[j];
> | }
> | }
> |
> | return res;
> | }
> | @
> |
> | from R:
> |
> | vec <- a very big integer vector
> | nrow <- 80
> | pos <- a very big subset of positions, such that max(pos) + nrow < 
> length(vec)
> | colsums <- colSumsGapMat(vec, pos, nrow)
> |
> |
> | from time to time I get a segfault.
> | Note: this is not exactly the code that produces the segfault (because
> | that one is very complicated), so it might be that this code is
> | totally fine.
> |
> | My suspicion:
> |
> | I am using the pointer "vec.begin()", but then I am allocating new
> | memory in the R area of memory with "IntegerVector res(pos.length())"
> | and R decides to move the original values of "vec" to some other
> | place, making the pointer invalid.
> |
> | Is that possible
> |
> | Sorry for being very vague and thx in advance!!!
> | Ale
> |
> |

Re: [Rcpp-devel] Segfault, is it because of iterators/pointers?

2014-02-12 Thread Alessandro Mammana
Ah wait, my bad (as always T.T), I found a much simpler explanation:

colset <- sample(3e7-nr, 1e7)
storage.mode(colset)
[1] "integer"
storage.mode(colset-1)
[1] "double"

So when I was unwrapping colset I allocated new memory in Rcpp to
convert from double to integer, which was no longer valid when I went
out of scope.
I think it is a bit dangerous that you never know if you are
allocating memory or just wrapping R objects when parsing arguments in
Rcpp.
Is there a way of ensuring that NOTHING gets copied when parsing
arguments? Can you throw an exception if the type you try to cast to
is not the one you expect?
 You might imagine that with large datasets this is important.

Sorry for bothering and thanks again,
Ale


On Wed, Feb 12, 2014 at 1:10 PM, Dirk Eddelbuettel  wrote:
>
> On 12 February 2014 at 11:47, Alessandro Mammana wrote:
> | Ok I was able to find the code causing the bug. So it looks like the
>
> Thanks for the added detail.
>
> | pointers you get from an Rcpp::Vector using .begin() become invalid
> | after that the Rcpp::Vector goes out of scope (and this makes sense),
> | what I do not understand is that this Rcpp::Vector was allocated in R
> | and should still be "living" during the execution of the Rcpp call
> | (that's why I wasn't expecting the pointer to be invalid).
> |
> | This is the exact code (the one above is probably fine):
> | @@ in CPP @@i
> |
> | struct GapMat {
> | int* ptr;
> | int* colset;
> | int nrow;
> | int ncol;
> |
> |
> | inline int* colptr(int col){
> | return ptr + colset[col];
> | }
> |
> | GapMat(){}
> |
> | GapMat(int* _ptr, int* _colset, int _nrow, int _ncol):
> | ptr(_ptr), colset(_colset), nrow(_nrow), ncol(_ncol){}
> | };
> |
> |
> | GapMat getGapMat(Rcpp::List gapmat){
> | IntegerVector vec = gapmat["vec"];
> | IntegerVector pos = gapmat["colset"];
> | int nrow = gapmat["nrow"];
> |
> | return GapMat(vec.begin(), pos.begin(), nrow, pos.length());
> | }
> |
> | // [[Rcpp::export]]
> | IntegerVector colSumsGapMat(Rcpp::List gapmat){
> |
> | GapMat mat = getGapMat(gapmat);
> | IntegerVector res(mat.ncol);
> |
> | for (int i = 0; i < mat.ncol; ++i){
> | for (int j = 0; j < mat.nrow; ++j){
> | res[i] += mat.colptr(i)[j];
> | }
> | }
> |
> | return res;
> | }
> |
> | @@ in R (with gdb debugger as suggested by Dirk) @@i
> | library(Rcpp)
> | sourceCpp("scratchpad.cpp")
> |
> | vec <- rnbinom(3e7, mu=0.1, size=1); storage.mode(vec) <- "integer"
> | nr <- 80
> |
> | colset <- sample(3e7-nr, 1e7)
> | foo <- vec[colset] #this is only to trigger some obscure garbage
> | collection mechanisms...
> |
> | for (i in 1:10){
> | colset <- sample(3e7-nr, 1e7)
> | gapmat <- list(vec=vec, nrow=nr, colset=colset-1)
> | cs <- colSumsGapMat(gapmat)
> | print(sum(cs))
> | }
> |
> | [1] 8000
> | [1] 8000
> | [1] 80016890
> | [1] 80008144
> | [1] 80016022
> | [1] 80021609
> |
> | Program received signal SIGSEGV, Segmentation fault.
> | 0x718a5455 in GapMat::colptr (this=0x7fffc120, col=0) at
> | scratchpad.cpp:295
> | 295return ptr + colset[col];
> |
> | @@@
> |
> | Why did it happen? What should I do to make sure that my pointers
> | remain valid? My goal is to convert safely some vectors or matrices
> | that "exist" in R to some pointers, how can I do that?
>
> Not sure. It looks fine at first instance. But then it's early in the morning
> and I had very little coffee yet...
>
> Maybe the fact that you tickle the gc() via vec[colset] has something to do
> with it, maybe it has not.  Maybe I would try the decomposition of the List
> object inside the colSumsGapMat() function to keep it simpler.  Or if you
> _really_ want an external object to iterate over, memcpy it out.
>
> With really large object, you may be stressing parts of the code that have
> not been stressed the same way.  If it breaks, you do get to keep both pieces.
>
> Dirk
>
> --
> Dirk Eddelbuettel | e...@debian.org | http://dirk.eddelbuettel.com



-- 
Alessandro Mammana, PhD Student
Max Planck Institute for Molecular Genetics
Ihnestraße 63-73
D-14195 Berlin, Germany
___
Rcpp-devel mailing list
Rcpp-devel@lists.r-forge.r-project.org
https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel


Re: [Rcpp-devel] Segfault, is it because of iterators/pointers?

2014-02-12 Thread Alessandro Mammana
I like the "Strict  mode" idea, I will use it, thanks!

On Wed, Feb 12, 2014 at 2:34 PM, Romain Francois
 wrote:
>
> Le 12 févr. 2014 à 13:36, Alessandro Mammana  a écrit :
>
>> Ah wait, my bad (as always T.T), I found a much simpler explanation:
>>
>> colset <- sample(3e7-nr, 1e7)
>> storage.mode(colset)
>> [1] "integer"
>> storage.mode(colset-1)
>> [1] "double"
>>
>> So when I was unwrapping colset I allocated new memory in Rcpp to
>> convert from double to integer, which was no longer valid when I went
>> out of scope.
>> I think it is a bit dangerous that you never know if you are
>> allocating memory or just wrapping R objects when parsing arguments in
>> Rcpp.
>> Is there a way of ensuring that NOTHING gets copied when parsing
>> arguments? Can you throw an exception if the type you try to cast to
>> is not the one you expect?
>> You might imagine that with large datasets this is important.
>
> Silent coercion was added by design. Rcpp does not give you a << strict >> 
> mode.
>
> One thing you can do is something like this:
>
> #include 
> using namespace Rcpp ;
>
> template 
> class Strict : public T {
> public:
>   Strict( SEXP x ) {
> if( TYPEOF(x) != T::r_type::value )
>   stop( "not compatible" ) ;
> T::Storage::set__(x) ;
>   }
>
> } ;
>
> // [[Rcpp::export]]
> int foo( Strict v ){
>   return v.size() ;
> }
>
> You'd get e.g.
>
>> foo(rnorm(10))
> [1] 10
>
>> foo(1:10)
> Error in eval(expr, envir, enclos) : not compatible
> Calls: sourceCpp ... source -> withVisible -> eval -> eval -> foo -> 
> 
> Execution halted
>
>
>
>> Sorry for bothering and thanks again,
>> Ale
>>
>>
>> On Wed, Feb 12, 2014 at 1:10 PM, Dirk Eddelbuettel  wrote:
>>>
>>> On 12 February 2014 at 11:47, Alessandro Mammana wrote:
>>> | Ok I was able to find the code causing the bug. So it looks like the
>>>
>>> Thanks for the added detail.
>>>
>>> | pointers you get from an Rcpp::Vector using .begin() become invalid
>>> | after that the Rcpp::Vector goes out of scope (and this makes sense),
>>> | what I do not understand is that this Rcpp::Vector was allocated in R
>>> | and should still be "living" during the execution of the Rcpp call
>>> | (that's why I wasn't expecting the pointer to be invalid).
>>> |
>>> | This is the exact code (the one above is probably fine):
>>> | @@ in CPP @@i
>>> |
>>> | struct GapMat {
>>> | int* ptr;
>>> | int* colset;
>>> | int nrow;
>>> | int ncol;
>>> |
>>> |
>>> | inline int* colptr(int col){
>>> | return ptr + colset[col];
>>> | }
>>> |
>>> | GapMat(){}
>>> |
>>> | GapMat(int* _ptr, int* _colset, int _nrow, int _ncol):
>>> | ptr(_ptr), colset(_colset), nrow(_nrow), ncol(_ncol){}
>>> | };
>>> |
>>> |
>>> | GapMat getGapMat(Rcpp::List gapmat){
>>> | IntegerVector vec = gapmat["vec"];
>>> | IntegerVector pos = gapmat["colset"];
>>> | int nrow = gapmat["nrow"];
>>> |
>>> | return GapMat(vec.begin(), pos.begin(), nrow, pos.length());
>>> | }
>>> |
>>> | // [[Rcpp::export]]
>>> | IntegerVector colSumsGapMat(Rcpp::List gapmat){
>>> |
>>> | GapMat mat = getGapMat(gapmat);
>>> | IntegerVector res(mat.ncol);
>>> |
>>> | for (int i = 0; i < mat.ncol; ++i){
>>> | for (int j = 0; j < mat.nrow; ++j){
>>> | res[i] += mat.colptr(i)[j];
>>> | }
>>> | }
>>> |
>>> | return res;
>>> | }
>>> |
>>> | @@ in R (with gdb debugger as suggested by Dirk) 
>>> @@i
>>> | library(Rcpp)
>>> | sourceCpp("scratchpad.cpp")
>>> |
>>> | vec <- rnbinom(3e7, mu=0.1, size=1); storage.mode(vec) <- "integer"
>>> | nr <- 80
>>> |
>>> | colset <- sample(3e7-nr, 1e7)
>>> | foo <- vec[colset] #this is only to trigger some obscure garbage
>>> | collection mechanisms...
>>> |
>>> | for (i in 1:10){
>>> | colset <- sample(3e7-nr, 1e7)
>>> | gapmat <- list(vec=vec, nrow=nr, colset=colset-1)
>>&g

[Rcpp-devel] Extending Rcpp with a dummy class

2014-02-13 Thread Alessandro Mammana
Dear all,
I have another one of my stupid questions.
Suppose that for any reason I want to define my own vector class like this:

@
template
struct Vec {
T* ptr;
int len;
};
@

so, without owning memory but just pointing to some (hopefully valid)
area of memory.
How can I extend Rcpp with this class so that I can have R vectors
automatically converted to these vectors?
The obvious answer is "read "extending Rcpp" instead of bothering us",
and I did it, and I tried the following:

@
#include 

template
struct Vec {
T* ptr;
int len;

Vec(SEXP x){
if (TYPEOF(x) != Rcpp::traits::r_sexptype_traits::rtype)
Rcpp::stop("incompatible types");

Rcpp::Vector< Rcpp::traits::r_sexptype_traits::rtype > rcpp_vec(x);
ptr = rcpp_vec.begin();
len = rcpp_vec.length();
}
};

#include 

blablabla
@


but the compiler complains and says:
error: 'Vector' is not a member of 'Rcpp'

this is probably because I still did not import Rcpp.h, but in
"extending Rcpp" they say to include Rcpp.h only after defining my
classes, so how should I do?

Thanks in advance!
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@lists.r-forge.r-project.org
https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel


Re: [Rcpp-devel] Extending Rcpp with a dummy class

2014-02-14 Thread Alessandro Mammana
Thanks a lot, I misunderstood the document
http://cran.r-project.org/web/packages/Rcpp/vignettes/Rcpp-extending.pdf
.
But there is still a problem when I try to use these classes
definitions in a package.
If these classes are defined in the file classdef.cpp, I somehow need
to add the line #include "classdef.cpp" in the file RcppExports.h.
Because this file is automatically generated, I looked for some Rcpp
attribute that adds this line. Rcpp::depends could be the one, but
I don't see how to use it, since classdef.cpp is not an Rcpp package.
I also tried just to add -I../inst/include in the Makevars and a
header classdef.h in
that directory, but this does not seem to add the desired line.
One again I am lost...
What should I do?


On Thu, Feb 13, 2014 at 4:13 PM, Dirk Eddelbuettel  wrote:
>
> On 13 February 2014 at 15:55, Alessandro Mammana wrote:
> | Suppose that for any reason I want to define my own vector class like this:
> |
> | template
> | struct Vec {
> | T* ptr;
> | int len;
> | };
>
> Conceptually that is about the same as our vectors.
>
> | so, without owning memory but just pointing to some (hopefully valid)
> | area of memory.
> | How can I extend Rcpp with this class so that I can have R vectors
> | automatically converted to these vectors?
> | The obvious answer is "read "extending Rcpp" instead of bothering us",
>
> Precisely. :)
>
> And look at the working examples, eg on the Rcpp Gallery I have posts doing
> this for Boost DateTime, and for sparse Armadillo matrices.  Look at other
> packages. The most full-featured are probably RcppArmadillo and RcppEigen.
>
> | but the compiler complains and says:
> | error: 'Vector' is not a member of 'Rcpp'
> |
> | this is probably because I still did not import Rcpp.h, but in
>
> Correct.
>
> | "extending Rcpp" they say to include Rcpp.h only after defining my
> | classes, so how should I do?
>
> Re-read it. You misunderstood that statement. RcppCommon is used for some
> forward declarations. You use it here to replace Rcpp.h.  Won't work.
>
> Dirk
>
> --
> Dirk Eddelbuettel | e...@debian.org | http://dirk.eddelbuettel.com



-- 
Alessandro Mammana, PhD Student
Max Planck Institute for Molecular Genetics
Ihnestraße 63-73
D-14195 Berlin, Germany
___
Rcpp-devel mailing list
Rcpp-devel@lists.r-forge.r-project.org
https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel


Re: [Rcpp-devel] Extending Rcpp with a dummy class

2014-02-14 Thread Alessandro Mammana
What is exactly "the old school way"?
1. Hacking the automatically generated RcppExports.cpp by adding that line?
2. Keeping the SEXP in the function definitions and converting them
manually one-by-one?
That's surprising... that this feature works well with sourceCpp and
not when deploying packages...


On Fri, Feb 14, 2014 at 1:31 PM, Dirk Eddelbuettel  wrote:
>
> On 14 February 2014 at 11:02, Alessandro Mammana wrote:
> | Thanks a lot, I misunderstood the document
> | http://cran.r-project.org/web/packages/Rcpp/vignettes/Rcpp-extending.pdf
> | .
> | But there is still a problem when I try to use these classes
> | definitions in a package.
> | If these classes are defined in the file classdef.cpp, I somehow need
> | to add the line #include "classdef.cpp" in the file RcppExports.h.
> | Because this file is automatically generated, I looked for some Rcpp
> | attribute that adds this line. Rcpp::depends could be the one, but
> | I don't see how to use it, since classdef.cpp is not an Rcpp package.
> | I also tried just to add -I../inst/include in the Makevars and a
> | header classdef.h in
> | that directory, but this does not seem to add the desired line.
> | One again I am lost...
> | What should I do?
>
> That is a different issue. Rcpp Attributes is wonderful to automate getting
> functions to R.
>
> It has nothing for as<> and wrap. You have to do that the old school way.
>
> Dirk
>
> --
> Dirk Eddelbuettel | e...@debian.org | http://dirk.eddelbuettel.com



-- 
Alessandro Mammana, PhD Student
Max Planck Institute for Molecular Genetics
Ihnestraße 63-73
D-14195 Berlin, Germany
___
Rcpp-devel mailing list
Rcpp-devel@lists.r-forge.r-project.org
https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel


[Rcpp-devel] Default empty values for vector and matrices

2014-02-17 Thread Alessandro Mammana
Dear all,
I am trying to write an Rcpp function that accepts optionally a
vector, but I cannot figure out what default value I should give to
it.

First I tried the following:

// [[Rcpp::export]]
int nilVec(NumericVector v = R_NilValue){
int sum = 0;
for (int i = 0; i < v.length(); ++i){
sum += v[i];
}
return sum;
}

but when trying:
nilVec()
I get the error "Not compatible with REALSXP", which is maybe not very
consistent with the fact that in R you can do as.numeric(NULL) and it
gives you a vector of length 0.

Then I tried:

// [[Rcpp::export]]
int nilVec(NumericVector v = NumericVector()){
int sum = 0;
for (int i = 0; i < v.length(); ++i){
sum += v[i];
}
return sum;
}

but it doesn't compile.

I found the following discussion:
http://comments.gmane.org/gmane.comp.lang.r.rcpp/5922 where I
discovered that for whatever reason the following compiles and works:

// [[Rcpp::export]]
int nilVec(NumericVector v = NumericVector::create(0)){
int sum = 0;
for (int i = 0; i < v.length(); ++i){
sum += v[i];
}
return sum;
}

But the funny thing is that this does not work:

// [[Rcpp::export]]
int nilMat(NumericMatrix v = NumericMatrix::create(0)){
int sum = 0;
for (int i = 0; i < v.length(); ++i){
sum += v[i];
}
return sum;
}

So how should I do for matrices? I would also be happy with the
solution of defining somewhere an empty vector  or an empty matrix and
assigning it as default argument, but the following does not work:

NumericVector empty(0);

// [[Rcpp::export]]
int nilVec(NumericVector v = empty){
int sum = 0;
for (int i = 0; i < v.length(); ++i){
sum += v[i];
}
return sum;
}


Suggestions? I would be super happy if in the future the special value
R_NilValue could be converted to an empty vector, it would make things
very easy and coherent with R's behaviour.

Thx in advance,
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@lists.r-forge.r-project.org
https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel


[Rcpp-devel] Sharing Cpp code between packages

2014-05-20 Thread Alessandro Mammana
Dear All,
first of all thanks again for Rcpp, I am becoming more and more
dependent on it and I am filling my directories with .cpp scripts...

I have a Rcpp package P using some header file F.hpp in its src/
directory. This file F.hpp contains templated functions and structs.
Is there a way for a new Rcpp package Q to include file F.hpp?

I read already Writing R Extensions "Linking to native routines in
other packages" as well as Rcpp-attriibutes, "Providing a C++
Interface". It looks like there are two mechanisms for sharing c++
code.
1. The "interface" mechanism: the real code is in the src/ directory
but it is referred to from some headers in the inst/include directory.
Additionally, these headers need to be wrapped with some (boilerplate
and obscure to me) C code. This works only with functions. The
boilerplate code can be nicely generated automatically with Rcpp
attributes.
2. The "direct" mechanism: just put the code in inst/include and tweak
the Makevars so that the compiler knows where to look for those
headers. This should work also with structs and templated cpp code.

I think I need the "direct" mechanism.
In package P, I moved my hpp files in inst/include and tweaked the
Makevars, the package compiled fine.
Now in package Q I am trying to use some of the stuff, and I am
including the files I need. I also did the following:
1. put import(P) in Q's namespace file
2. put Import P and LinkingTo P in Q's description file

however during compilation I got an error like:
undefined symbol: _Z16rcpp_hello_worldv

do you know what could be wrong?
Thanks a lot and sorry for bothering.

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@lists.r-forge.r-project.org
https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel

Re: [Rcpp-devel] Sharing Cpp code between packages

2014-05-20 Thread Alessandro Mammana
My bad, I forgot to call the compileAttributes function... T.T
Everything works fine!!! Yes, I don't need object code.
 "tweak the Makevars", I meant in package P, because now the files are
not in src/ anymore, but I moved them to /inst/include.
In package Q no need to tweak the Makevars :D

On Tue, May 20, 2014 at 5:41 PM, Dirk Eddelbuettel  wrote:
>
> On 20 May 2014 at 17:26, Alessandro Mammana wrote:
> | Dear All,
> | first of all thanks again for Rcpp, I am becoming more and more
> | dependent on it and I am filling my directories with .cpp scripts...
> |
> | I have a Rcpp package P using some header file F.hpp in its src/
> | directory. This file F.hpp contains templated functions and structs.
> | Is there a way for a new Rcpp package Q to include file F.hpp?
>
> Yes.
>
> But you need to move it from src/F.hpp to inst/include/F.hpp which, once
> installed, will become include/F.hpp.
>
> The big trick then is that by just saying "LinkingTo: P" you get R to do the
> right thing via the appropriate -I flags.
>
> This is essence all that the package BH does to give everybody Boost headers.
>
> | I read already Writing R Extensions "Linking to native routines in
> | other packages" as well as Rcpp-attriibutes, "Providing a C++
> | Interface". It looks like there are two mechanisms for sharing c++
> | code.
> | 1. The "interface" mechanism: the real code is in the src/ directory
> | but it is referred to from some headers in the inst/include directory.
> | Additionally, these headers need to be wrapped with some (boilerplate
> | and obscure to me) C code. This works only with functions. The
> | boilerplate code can be nicely generated automatically with Rcpp
> | attributes.
>
> Yes. That is probably the best route __when you need object code__.  I am
> using that in package RcppXts from (non-Rcpp) package xts, and in the new
> RcppRedis from RApiSerialize.
>
> The exporting package also needs to do some work.  See for example
> RApiSerialize -- all it does is register two functions.  The mechanism is
> pure R mechanics and not Rcpp specific but can be used by Rcpp if you want
> to.
>
> | 2. The "direct" mechanism: just put the code in inst/include and tweak
> | the Makevars so that the compiler knows where to look for those
> | headers. This should work also with structs and templated cpp code.
>
> See above. No tweaks to Makevars needed (!!) but works only for template 
> headers.
>
> | I think I need the "direct" mechanism.
> | In package P, I moved my hpp files in inst/include and tweaked the
> | Makevars, the package compiled fine.
> | Now in package Q I am trying to use some of the stuff, and I am
> | including the files I need. I also did the following:
> | 1. put import(P) in Q's namespace file
> | 2. put Import P and LinkingTo P in Q's description file
> |
> | however during compilation I got an error like:
> | undefined symbol: _Z16rcpp_hello_worldv
>
> It is a linking error.
>
> | do you know what could be wrong?
>
> Are you expecting object code? Then you need to do more.
>
> You are doing the right thing by working it out with small examples.
>
> Dirk
>
> | Thanks a lot and sorry for bothering.
> |
> | 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@lists.r-forge.r-project.org
> | https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel
> --
> Dirk Eddelbuettel | e...@debian.org | http://dirk.eddelbuettel.com



-- 
Alessandro Mammana, PhD Student
Max Planck Institute for Molecular Genetics
Ihnestraße 63-73
D-14195 Berlin, Germany
___
Rcpp-devel mailing list
Rcpp-devel@lists.r-forge.r-project.org
https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel

Re: [Rcpp-devel] Rcpp-devel Digest, Vol 57, Issue 18

2014-07-17 Thread Alessandro Mammana
Sorry,  I read this post just by chance,
did I understand correctly, the class Rcpp::Matrix are not thread safe?
Is there any problem in writing code such as:

mat(i,j) = something

in a multithreaded environment (such as inside a "#pragma omp parallel
for" loop)?
My scripts are full of such loops. I have assumed that it was the same as doing:

double* M = mat.begin();
M[i + ncol*j] = something;

This at least should be thread safe, right?

Thx a lot!
Ale



On Tue, Jul 15, 2014 at 12:00 PM,
 wrote:
> Send Rcpp-devel mailing list submissions to
> rcpp-devel@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-requ...@lists.r-forge.r-project.org
>
> You can reach the person managing the list at
> rcpp-devel-ow...@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: parallel distance matrix calculation (JJ Allaire)
>2. Re: parallel distance matrix calculation (JJ Allaire)
>
>
> --
>
> Message: 1
> Date: Mon, 14 Jul 2014 10:45:06 -0400
> From: JJ Allaire 
> To: Dirk Eddelbuettel 
> Cc: "rcpp-devel@lists.r-forge.r-project.org"
> 
> Subject: Re: [Rcpp-devel] parallel distance matrix calculation
> Message-ID:
> 
> Content-Type: text/plain; charset="utf-8"
>
> Here's a parallel version:
>
> https://github.com/jjallaire/RcppParallel/blob/master/inst/examples/parallel-distance-matrix.cpp
>
> To make the code reasonable I introduced a new RMatrix class in
> RcppParallel that makes offsetting into rows and columns safe and
> straightforward. This class has no connection on the R or Rcpp APIs so is
> guaranteed to be thread-safe.
>
> On a 4 core machine (8 with hyperthreading) I'm observing a 10x speedup.
> The parallel related speedup is 4x. There is an additional 2.5x speedup
> which appears to be related to the lower level access to the Matrix memory
> done by RMatrix (and perhaps some elimination of copying).
>
>
>
>
> On Sun, Jul 13, 2014 at 7:27 AM, Dirk Eddelbuettel  wrote:
>
>>
>> On 12 July 2014 at 12:37, JJ Allaire wrote:
>> | If you could send the full source code to your example (including
>> js_distance
>> | and whatever R code you are using to test/exercise the functions) I'll
>> see if I
>> | can come up with the code you'd use to parallelize the outer loop.
>> Depending on
>> | how it turns out perhaps we can even convert this into another gallery
>> article!
>>
>> That's the spirit!  [ I had another quick look at the inner-product example
>> which is short and nice, but there is just too little of a pickup there...
>> ]
>>
>> Dirk
>>
>> --
>> http://dirk.eddelbuettel.com | @eddelbuettel | e...@debian.org
>>
> -- next part --
> An HTML attachment was scrubbed...
> URL: 
> <http://lists.r-forge.r-project.org/pipermail/rcpp-devel/attachments/20140714/a4fcf62a/attachment-0001.html>
>
> --
>
> Message: 2
> Date: Mon, 14 Jul 2014 12:48:25 -0400
> From: JJ Allaire 
> To: Dirk Eddelbuettel 
> Cc: "rcpp-devel@lists.r-forge.r-project.org"
> 
> Subject: Re: [Rcpp-devel] parallel distance matrix calculation
> Message-ID:
> 
> Content-Type: text/plain; charset="utf-8"
>
>> On a 4 core machine (8 with hyperthreading) I'm observing a 10x speedup.
>> The parallel related speedup is 4x. There is an additional 2.5x speedup
>> which appears to be related to the lower level access to the Matrix memory
>> done by RMatrix (and perhaps some elimination of copying).
>>
>
> It turns out that the additional slowdown in the serial version was due to
> repeatedly calling Vector::length as a loop termination condition. I
> re-wrote the serial version using iterators and now the speedup from
> parallel is about 5x (more in line with expectations).
> -- next part ------
> An HTML attachment was scrubbed...
> URL: 
> <http://lists.r-forge.r-project.org/pipermail/rcpp-devel/attachments/20140714/39077e47/attachment-0001.html>
>
> --
>
> ___
> Rcpp-devel mailing list
> Rcpp-devel@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 57, Issue 18
> **



-- 
Alessandro Mammana, PhD Student
Max Planck Institute for Molecular Genetics
Ihnestraße 63-73
D-14195 Berlin, Germany
___
Rcpp-devel mailing list
Rcpp-devel@lists.r-forge.r-project.org
https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel

[Rcpp-devel] The proper way of wrapping an R object

2015-02-10 Thread Alessandro Mammana
Dear all,
the following is not about "how to make it work", because it already
works, it is only about "what's the best, cleanest way of doing it".

I have an s4 object with three slots, and many Rcpp functions dealing with it.
My goal is to have a C struct (or class) equivalent to the s4 object,
i.e. where each field is an s4 slot.

Right now I defined the struct in this way:

struct CountSignals {
Rcpp::IntegerVector counts;
Rcpp::IntegerVector breaks;
bool ss;

CountSignals(Rcpp::RObject csig) {
if (not csig.inherits("CountSignals")) Rcpp::stop("expecting a
CountSignals object");
counts = Rcpp::as(csig.slot("counts"));
breaks = Rcpp::as(csig.slot("breaks"));
ss = Rcpp::as(csig.slot("ss"));
}
};


And then each method starts like this, for example:

// [[Rcpp::export]]
Rcpp::List asList(Rcpp::RObject csig){
CountSignals x(csig);


As said before, this is working fine, but I am skeptical about the
following things:
1. in the assignment operators in the CountSignals constructor, am I
copying the whole vector or am I copying a pointer to it? I need to
make sure that nothing is copied (as these vectors can be very long).
2. Even cleaner would be to start a method in this way:

// [[Rcpp::export]]
Rcpp::List asList(CountSignals x){


do you know if/how I can achieve that? (All of this code is inside an
R package).

Thanks a lot!

-- 
Alessandro Mammana, PhD Student
Max Planck Institute for Molecular Genetics
Ihnestraße 63-73
D-14195 Berlin, Germany
___
Rcpp-devel mailing list
Rcpp-devel@lists.r-forge.r-project.org
https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel

[Rcpp-devel] Overflow error when creating a large matrix

2015-10-31 Thread Alessandro Mammana
Dear All,
When creating a large matrix with the follwing code:

#include 
// [[Rcpp::export]]
Rcpp::IntegerMatrix makeMat(int nrow, int ncol){
Rcpp::IntegerMatrix mat(nrow, ncol);
return mat;
}

I get the error:

Error in .Primitive(".Call")(, nrow, ncol) :
  negative length vectors are not allowed

Where nrow*ncol is a very large number (about 3*10^9). I understand
that such a number cannot be represented by an int type, but this does
not seem to be a problem when in R I do:

> mat <- matrix(0, nrow=nrow, ncol=ncol)

Questions:
1. why is the behaviour different between R and Rcpp in the allocation
of a matrix?
2. In a 64 bits machine, what is actually the maximum allowed length
of a vector/matrix? does the length of a vector/matrix need to be
represented by an int?

Thanks a lot and best regards,
Alessandro

-- 
Alessandro Mammana, PhD Student
Max Planck Institute for Molecular Genetics
Ihnestraße 63-73
D-14195 Berlin, Germany
___
Rcpp-devel mailing list
Rcpp-devel@lists.r-forge.r-project.org
https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel

Re: [Rcpp-devel] Overflow error when creating a large matrix

2015-11-01 Thread Alessandro Mammana
It's great that R and Rcpp have support for long vectors! that makes
my life so much easier!

So I installed Rcpp version 0.12.1 and the error is still there (see
end of the email). If I interpret the commit history correctly, the
commit that fixes the bug was done shortly after the 0.12.1 release
right? Is there an easy way of getting the latest version of the
master branch?
I tried:

devtools::install_github("https://github.com/RcppCore/Rcpp";)

But I got:

Error in username %||% getOption("github.user") %||% stop("Unknown username.") :
  Unknown username.

> sourceCpp("makeMat.cpp")
> makeMat(18, 136633572)
Error in .Primitive(".Call")(, nrow, ncol) :
  negative length vectors are not allowed
> sesssionInfo()
Error: could not find function "sesssionInfo"
> sessionInfo()
R version 3.2.0 (2015-04-16)
Platform: x86_64-unknown-linux-gnu (64-bit)
Running under: MarIuX64 2.0 GNU/Linux 2010-2012

locale:
 [1] LC_CTYPE=en_US.UTF-8 LC_NUMERIC=C LC_TIME=C
 [4] LC_COLLATE=C LC_MONETARY=CLC_MESSAGES=C
 [7] LC_PAPER=C   LC_NAME=CLC_ADDRESS=C
[10] LC_TELEPHONE=C   LC_MEASUREMENT=C LC_IDENTIFICATION=C

attached base packages:
[1] stats graphics  grDevices utils datasets  methods   base

other attached packages:
[1] Rcpp_0.12.1

On Sun, Nov 1, 2015 at 3:37 PM, Florian Plaza Oñate
 wrote:
> The bug is probably fixed by this commit:
> https://github.com/RcppCore/Rcpp/commit/eb069bf5c20aaa4d38e0ca2897bf04e956cb8c4c
>
>
> Le 31/10/2015 20:08, Alessandro Mammana a écrit :
>>
>> Dear All,
>> When creating a large matrix with the follwing code:
>>
>> #include 
>> // [[Rcpp::export]]
>> Rcpp::IntegerMatrix makeMat(int nrow, int ncol){
>>  Rcpp::IntegerMatrix mat(nrow, ncol);
>>  return mat;
>> }
>>
>> I get the error:
>>
>> Error in .Primitive(".Call")(, nrow, ncol) :
>>negative length vectors are not allowed
>>
>> Where nrow*ncol is a very large number (about 3*10^9). I understand
>> that such a number cannot be represented by an int type, but this does
>> not seem to be a problem when in R I do:
>>
>>> mat <- matrix(0, nrow=nrow, ncol=ncol)
>>
>>
>> Questions:
>> 1. why is the behaviour different between R and Rcpp in the allocation
>> of a matrix?
>> 2. In a 64 bits machine, what is actually the maximum allowed length
>> of a vector/matrix? does the length of a vector/matrix need to be
>> represented by an int?
>>
>> Thanks a lot and best regards,
>> Alessandro
>>
>



-- 
Alessandro Mammana, PhD Student
Max Planck Institute for Molecular Genetics
Ihnestraße 63-73
D-14195 Berlin, Germany
___
Rcpp-devel mailing list
Rcpp-devel@lists.r-forge.r-project.org
https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel

Re: [Rcpp-devel] Overflow error when creating a large matrix

2015-11-01 Thread Alessandro Mammana
As Sean O'Riordain correctly pointed out, I used the install_github
command improperly. Doing

devtools::install_github("RcppCore/Rcpp")

works and the big matrix is allocated correctly.

Thanks for the help and for the great work you are doing!
Ale

On Sun, Nov 1, 2015 at 6:30 PM, Alessandro Mammana
 wrote:
> It's great that R and Rcpp have support for long vectors! that makes
> my life so much easier!
>
> So I installed Rcpp version 0.12.1 and the error is still there (see
> end of the email). If I interpret the commit history correctly, the
> commit that fixes the bug was done shortly after the 0.12.1 release
> right? Is there an easy way of getting the latest version of the
> master branch?
> I tried:
>
> devtools::install_github("https://github.com/RcppCore/Rcpp";)
>
> But I got:
>
> Error in username %||% getOption("github.user") %||% stop("Unknown 
> username.") :
>   Unknown username.
>
>> sourceCpp("makeMat.cpp")
>> makeMat(18, 136633572)
> Error in .Primitive(".Call")(, nrow, ncol) :
>   negative length vectors are not allowed
>> sesssionInfo()
> Error: could not find function "sesssionInfo"
>> sessionInfo()
> R version 3.2.0 (2015-04-16)
> Platform: x86_64-unknown-linux-gnu (64-bit)
> Running under: MarIuX64 2.0 GNU/Linux 2010-2012
>
> locale:
>  [1] LC_CTYPE=en_US.UTF-8 LC_NUMERIC=C LC_TIME=C
>  [4] LC_COLLATE=C LC_MONETARY=CLC_MESSAGES=C
>  [7] LC_PAPER=C   LC_NAME=CLC_ADDRESS=C
> [10] LC_TELEPHONE=C   LC_MEASUREMENT=C LC_IDENTIFICATION=C
>
> attached base packages:
> [1] stats graphics  grDevices utils datasets  methods   base
>
> other attached packages:
> [1] Rcpp_0.12.1
>
> On Sun, Nov 1, 2015 at 3:37 PM, Florian Plaza Oñate
>  wrote:
>> The bug is probably fixed by this commit:
>> https://github.com/RcppCore/Rcpp/commit/eb069bf5c20aaa4d38e0ca2897bf04e956cb8c4c
>>
>>
>> Le 31/10/2015 20:08, Alessandro Mammana a écrit :
>>>
>>> Dear All,
>>> When creating a large matrix with the follwing code:
>>>
>>> #include 
>>> // [[Rcpp::export]]
>>> Rcpp::IntegerMatrix makeMat(int nrow, int ncol){
>>>  Rcpp::IntegerMatrix mat(nrow, ncol);
>>>  return mat;
>>> }
>>>
>>> I get the error:
>>>
>>> Error in .Primitive(".Call")(, nrow, ncol) :
>>>negative length vectors are not allowed
>>>
>>> Where nrow*ncol is a very large number (about 3*10^9). I understand
>>> that such a number cannot be represented by an int type, but this does
>>> not seem to be a problem when in R I do:
>>>
>>>> mat <- matrix(0, nrow=nrow, ncol=ncol)
>>>
>>>
>>> Questions:
>>> 1. why is the behaviour different between R and Rcpp in the allocation
>>> of a matrix?
>>> 2. In a 64 bits machine, what is actually the maximum allowed length
>>> of a vector/matrix? does the length of a vector/matrix need to be
>>> represented by an int?
>>>
>>> Thanks a lot and best regards,
>>> Alessandro
>>>
>>
>
>
>
> --
> Alessandro Mammana, PhD Student
> Max Planck Institute for Molecular Genetics
> Ihnestraße 63-73
> D-14195 Berlin, Germany



-- 
Alessandro Mammana, PhD Student
Max Planck Institute for Molecular Genetics
Ihnestraße 63-73
D-14195 Berlin, Germany
___
Rcpp-devel mailing list
Rcpp-devel@lists.r-forge.r-project.org
https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel

Re: [Rcpp-devel] Overflow error when creating a large matrix

2015-11-01 Thread Alessandro Mammana
I got another error when using the Matrix.column() function, as in the
code below:

#include 
#include "array.cpp"
// [[Rcpp::export]]
Rcpp::IntegerMatrix makeMat(int nrow=18, int ncol=136633572){
Rcpp::IntegerMatrix mat(nrow, ncol);
for (int i = 0; i < ncol; ++i) {
Rcpp::MatrixColumn col = mat.column(i);
for (int j = 0; j < nrow; ++j){
col[j] = 1;
}
}
return mat;
}

Then in R:

> library(Rcpp)
> sourceCpp("makeMat.cpp")
> mat <- makeMat()

 *** caught segfault ***
address 0x7f4c1e825088, cause 'memory not mapped'

Traceback:
 1: .Primitive(".Call")(, nrow, ncol)
 2: makeMat()


If I had to guess, the column function is doing the multiplication
i*nrow and storing it as an int, which overflows when i is big. I am
sorry that I cannot point to the Rcpp code where this happens and
propose the fix, I am sitting on a chair with my laptop on my legs,
not quite convenient for programming :D.

Best,
Alessandro

On Sun, Nov 1, 2015 at 7:35 PM, Alessandro Mammana
 wrote:
> As Sean O'Riordain correctly pointed out, I used the install_github
> command improperly. Doing
>
> devtools::install_github("RcppCore/Rcpp")
>
> works and the big matrix is allocated correctly.
>
> Thanks for the help and for the great work you are doing!
> Ale
>
> On Sun, Nov 1, 2015 at 6:30 PM, Alessandro Mammana
>  wrote:
>> It's great that R and Rcpp have support for long vectors! that makes
>> my life so much easier!
>>
>> So I installed Rcpp version 0.12.1 and the error is still there (see
>> end of the email). If I interpret the commit history correctly, the
>> commit that fixes the bug was done shortly after the 0.12.1 release
>> right? Is there an easy way of getting the latest version of the
>> master branch?
>> I tried:
>>
>> devtools::install_github("https://github.com/RcppCore/Rcpp";)
>>
>> But I got:
>>
>> Error in username %||% getOption("github.user") %||% stop("Unknown 
>> username.") :
>>   Unknown username.
>>
>>> sourceCpp("makeMat.cpp")
>>> makeMat(18, 136633572)
>> Error in .Primitive(".Call")(, nrow, ncol) :
>>   negative length vectors are not allowed
>>> sesssionInfo()
>> Error: could not find function "sesssionInfo"
>>> sessionInfo()
>> R version 3.2.0 (2015-04-16)
>> Platform: x86_64-unknown-linux-gnu (64-bit)
>> Running under: MarIuX64 2.0 GNU/Linux 2010-2012
>>
>> locale:
>>  [1] LC_CTYPE=en_US.UTF-8 LC_NUMERIC=C LC_TIME=C
>>  [4] LC_COLLATE=C LC_MONETARY=CLC_MESSAGES=C
>>  [7] LC_PAPER=C   LC_NAME=CLC_ADDRESS=C
>> [10] LC_TELEPHONE=C   LC_MEASUREMENT=C LC_IDENTIFICATION=C
>>
>> attached base packages:
>> [1] stats graphics  grDevices utils datasets  methods   base
>>
>> other attached packages:
>> [1] Rcpp_0.12.1
>>
>> On Sun, Nov 1, 2015 at 3:37 PM, Florian Plaza Oñate
>>  wrote:
>>> The bug is probably fixed by this commit:
>>> https://github.com/RcppCore/Rcpp/commit/eb069bf5c20aaa4d38e0ca2897bf04e956cb8c4c
>>>
>>>
>>> Le 31/10/2015 20:08, Alessandro Mammana a écrit :
>>>>
>>>> Dear All,
>>>> When creating a large matrix with the follwing code:
>>>>
>>>> #include 
>>>> // [[Rcpp::export]]
>>>> Rcpp::IntegerMatrix makeMat(int nrow, int ncol){
>>>>  Rcpp::IntegerMatrix mat(nrow, ncol);
>>>>  return mat;
>>>> }
>>>>
>>>> I get the error:
>>>>
>>>> Error in .Primitive(".Call")(, nrow, ncol) :
>>>>    negative length vectors are not allowed
>>>>
>>>> Where nrow*ncol is a very large number (about 3*10^9). I understand
>>>> that such a number cannot be represented by an int type, but this does
>>>> not seem to be a problem when in R I do:
>>>>
>>>>> mat <- matrix(0, nrow=nrow, ncol=ncol)
>>>>
>>>>
>>>> Questions:
>>>> 1. why is the behaviour different between R and Rcpp in the allocation
>>>> of a matrix?
>>>> 2. In a 64 bits machine, what is actually the maximum allowed length
>>>> of a vector/matrix? does the length of a vector/matrix need to be
>>>> represented by an int?
>>>>
>>>> Thanks a lot and best regards,
>>>> Alessandro
>>>>
>>>
>>
>>
>>
>> --
>> Alessandro Mammana, PhD Student
>> Max Planck Institute for Molecular Genetics
>> Ihnestraße 63-73
>> D-14195 Berlin, Germany
>
>
>
> --
> Alessandro Mammana, PhD Student
> Max Planck Institute for Molecular Genetics
> Ihnestraße 63-73
> D-14195 Berlin, Germany



-- 
Alessandro Mammana, PhD Student
Max Planck Institute for Molecular Genetics
Ihnestraße 63-73
D-14195 Berlin, Germany
___
Rcpp-devel mailing list
Rcpp-devel@lists.r-forge.r-project.org
https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel