Re: [Rcpp-devel] Rf_error() format string

2023-11-28 Thread Serguei Sokol

Thanks to all.
As CRAN said: package rmumps_5.2.1-26.tar.gz is on its way to CRAN.

Best,
Serguei.

Le 28/11/2023 à 17:49, Dirk Eddelbuettel a écrit :


Sorry for being slow to respond (very busy morning at work and play, got five
such emails myself) but as Inaki and Georgi helpfully pointed out already the
issue is _known_ and has a _fix_ at either the default branch of the Rcpp
repo, or, if you prefer, via the Rcpp drat repo as detailed in this comment
of issue #1287 at the Rcpp repo

https://github.com/RcppCore/Rcpp/issues/1287#issuecomment-1829886024

from which I quote (and, again, sorry for not posting here sooner)

   Yes, I actually got five such emails myself today for packages of mine
   using Rcpp.

   The fix is simple thanks to PR #1288 we made two days ago. Install Rcpp
   1.0.11.5 from the Rcpp drat repo via, e.g.,

  Rscript -e 'install.packages("Rcpp", \
  repos=c("https://RcppCore.github.io/drat;, getOption("repos")))'

   and then re-run compileAttributes(). That will fix the RcppExports.cpp file
   for you. You then need to upload your updated package to CRAN. It has no
   change in its run-time dependency on Rcpp so you do not need to change
   anything in DESCRIPTION. Just re-run compileAttributes().

Of course, you can also use whichever tool runs `compileAttributes()` for
you. I tend to use `littler` script `compAttr.r` on the command-line. Others
rely on RStudio which automagically does it if it notices you rebuild a
package using Rcpp.  I believe the devtools/usethis nexus has this in
`document_all()` (?), and I am sure someone somewhere cooked something up for
VSCode too.  The key is the provided function Rcpp::compileAttributes() which
remains at your service and is now updated.

I may also push an updated `rocker/drd` container which I recently switched
to being based on ubuntu and r2u (rather than debian).  Then installing your
build dependencies for you packages is trivial (I run `installDeps.r` inside
the container) and it will come with an r-devel build. You "only" need to
create `~/.R/Makevars` to tell the compiler to use extended format warnings.
That allows you to replicate the CRAN warnings, and ensure rebuild
RcppExports.cpp addresses it.  If I find time I'll post an example on my
blog.

Cheers, Dirk



___
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] Rf_error() format string

2023-11-28 Thread Serguei Sokol

Hi,

I've got a request from CRAN to correct my package rmumps using Rcpp:
https://cran.r-project.org/web/checks/check_results_rmumps.html

The problems come from RcppExports.cpp automatically generated lines like:

   Rf_error(CHAR(rcpp_msgSEXP_gen));

With a message saying:

Found the following significant warnings:
   RcppExports.cpp:58:18: warning: format string is not a string 
literal (potentially insecure) [-Wformat-security]

   ...

The fix may be as trivial as:

  Rf_error("%s", CHAR(rcpp_msgSEXP_gen));

However, if I do it manually, it will be overwritten at the next 
RcppExports.cppbuild.

Are there some plans to incorporate this fix in Rcpp?
Am I alone in this case?

Best,
Serguei.
___
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 shared libraries (?)

2022-06-23 Thread Serguei Sokol

Le 23/06/2022 à 17:12, THIOULOUSE JEAN a écrit :

...

I can make a PR if you wish.


Yes, please do. Thanks a lot !


Done: https://github.com/sdray/ade4/pull/31
Serguei.



Le 23/06/2022 à 16:10, Jean Thioulouse a écrit :

Le 23 juin 2022 à 16:04, Serguei Sokol  a écrit :

Le 23/06/2022 à 15:47, Jean Thioulouse a écrit :

Thank you Serguei, I tried this, but I still get the "undeclared identifier" 
error for these internal functions during package compilation:
clang++ -arch arm64 -std=gnu++14 
-I"/Library/Frameworks/R.framework/Resources/include" -DNDEBUG  
-I'/Library/Frameworks/R.framework/Versions/4.2-arm64/Resources/library/Rcpp/include' 
-I'/Library/Frameworks/R.framework/Versions/4.2-arm64/Resources/library/RcppArmadillo/include'
 -I/opt/R/arm64/include   -fPIC  -falign-functions=64 -Wall -g -O2  -c 
testertracenuCpp.cpp -o testertracenuCpp.o
testertracenuCpp.cpp:112:9: error: use of undeclared identifier 'matcentrageCpp'
 i = matcentrageCpp (ti1p, pl, typ1);

I cannot find 'matcentrageCpp' in 
https://github.com/sdray/ade4/tree/ade4-Rcpp/. Do you have a current state 
example somewhere?

Yes, I just pushed it in the ade4-Rcpp branch.


Serguei.


 ^
Jean

Le 23 juin 2022 à 15:17, Serguei Sokol  a écrit :

Le 23/06/2022 à 15:02, THIOULOUSE JEAN a écrit :

Hi
Sorry to bother you again with my C/C++ problems in the ade4 package (available on 
CRAN & GitHub).
Thanks to your help, I have succeeded in converting several of the C functions 
to C++ using Rcpp. Now the package compiles without problem and all my C++ 
functions run fine in the dedicated GitHub branch of the package (ade4-Rcpp).
The new problem I am facing now is that in ade4, I use a library of about 30 
utilities functions collected in one file named adesub.c, with adesub.h headers 
file. From what I understand, during package compilation these functions are 
added to the ade4 shared library and they can be called by my other C functions 
without being declared in the init.c or NAMESPACE files, as they do not need to 
be known by R.
I would like to know if it is possible to do the same thing in C++ with Rcpp.

If I understand correctly you goal, just don't put "// [[Rcpp::export]]" before your 
"internal" functions. They will be available to all C and C++ functions in src/ but not seen in R. 
You don't need extra "R CMD SHLIB" either.

Best,
Serguei.

I have found reading the documentation (Dirk's book and Rcpp vignettes) that I 
could use R CMD SHLIB to build a shared library and then dyn.load it in R. I 
have created a file named ade4lib.cpp containing the utilities functions in the 
src directory, and I can make the shared library (ade4lib.so) with RCMD SHLIB. 
But this does not solve my problem because, as I understand it, this makes the 
utilities library functions available to R (which is useless since they are not 
interfaced correctly to R but to C++), and not to the other C++ functions 
(which is what i need to do).

Can anybody see where I am mistaken, and point me to the right direction to 
accomplish this ?
Thank you,
Jean
—-
Jean THIOULOUSE - 
https://lbbe-web.univ-lyon1.fr/en/annuaires-des-membres/thioulouse-jean
https://orcid.org/-0001-7664-0598 (ORCID page)
https://www.springer.com/fr/book/9781493988488 (ade4 Book)
https://pbil.univ-lyon1.fr/JTHome/Biblio/ThioulouseEB2021.pdf (Evol. Biol. 
paper)
___
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

Re: [Rcpp-devel] Rcpp shared libraries (?)

2022-06-23 Thread Serguei Sokol

Rcpp or plain C++ or even C ;) , you still have to declare used functions.
In Rcpp package, the most natural way to do it, is to put such 
declarations in a file $pkg/inst/include/.h. In your case, it 
will be inst/include/ade4.h


In this file you put:

#include 
int matcentrageCpp (arma::mat& A, const arma::vec& poili, const int typ);

and all other functions of yours you plan to use in a cross-file way.
Then, in a file where you want to use such functions (e.g. in 
testertracenuCpp.cpp), you add


#include 

Finally, you add the following line in your src/Makevars:

PKG_CPPFLAGS = -I../inst/include

After all this, I've got a compilation without error.
I can make a PR if you wish.

Best,
Serguei.

Le 23/06/2022 à 16:10, Jean Thioulouse a écrit :




Le 23 juin 2022 à 16:04, Serguei Sokol  a écrit :

Le 23/06/2022 à 15:47, Jean Thioulouse a écrit :

Thank you Serguei, I tried this, but I still get the "undeclared identifier" 
error for these internal functions during package compilation:
clang++ -arch arm64 -std=gnu++14 
-I"/Library/Frameworks/R.framework/Resources/include" -DNDEBUG  
-I'/Library/Frameworks/R.framework/Versions/4.2-arm64/Resources/library/Rcpp/include' 
-I'/Library/Frameworks/R.framework/Versions/4.2-arm64/Resources/library/RcppArmadillo/include'
 -I/opt/R/arm64/include   -fPIC  -falign-functions=64 -Wall -g -O2  -c 
testertracenuCpp.cpp -o testertracenuCpp.o
testertracenuCpp.cpp:112:9: error: use of undeclared identifier 'matcentrageCpp'
 i = matcentrageCpp (ti1p, pl, typ1);

I cannot find 'matcentrageCpp' in 
https://github.com/sdray/ade4/tree/ade4-Rcpp/. Do you have a current state 
example somewhere?


Yes, I just pushed it in the ade4-Rcpp branch.



Serguei.


 ^
Jean

Le 23 juin 2022 à 15:17, Serguei Sokol  a écrit :

Le 23/06/2022 à 15:02, THIOULOUSE JEAN a écrit :

Hi
Sorry to bother you again with my C/C++ problems in the ade4 package (available on 
CRAN & GitHub).
Thanks to your help, I have succeeded in converting several of the C functions 
to C++ using Rcpp. Now the package compiles without problem and all my C++ 
functions run fine in the dedicated GitHub branch of the package (ade4-Rcpp).
The new problem I am facing now is that in ade4, I use a library of about 30 
utilities functions collected in one file named adesub.c, with adesub.h headers 
file. From what I understand, during package compilation these functions are 
added to the ade4 shared library and they can be called by my other C functions 
without being declared in the init.c or NAMESPACE files, as they do not need to 
be known by R.
I would like to know if it is possible to do the same thing in C++ with Rcpp.

If I understand correctly you goal, just don't put "// [[Rcpp::export]]" before your 
"internal" functions. They will be available to all C and C++ functions in src/ but not seen in R. 
You don't need extra "R CMD SHLIB" either.

Best,
Serguei.

I have found reading the documentation (Dirk's book and Rcpp vignettes) that I 
could use R CMD SHLIB to build a shared library and then dyn.load it in R. I 
have created a file named ade4lib.cpp containing the utilities functions in the 
src directory, and I can make the shared library (ade4lib.so) with RCMD SHLIB. 
But this does not solve my problem because, as I understand it, this makes the 
utilities library functions available to R (which is useless since they are not 
interfaced correctly to R but to C++), and not to the other C++ functions 
(which is what i need to do).

Can anybody see where I am mistaken, and point me to the right direction to 
accomplish this ?
Thank you,
Jean
—-
Jean THIOULOUSE - 
https://lbbe-web.univ-lyon1.fr/en/annuaires-des-membres/thioulouse-jean
https://orcid.org/-0001-7664-0598 (ORCID page)
https://www.springer.com/fr/book/9781493988488 (ade4 Book)
https://pbil.univ-lyon1.fr/JTHome/Biblio/ThioulouseEB2021.pdf (Evol. Biol. 
paper)
___
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

Re: [Rcpp-devel] Rcpp shared libraries (?)

2022-06-23 Thread Serguei Sokol

Le 23/06/2022 à 15:47, Jean Thioulouse a écrit :


Thank you Serguei, I tried this, but I still get the "undeclared identifier" 
error for these internal functions during package compilation:

clang++ -arch arm64 -std=gnu++14 
-I"/Library/Frameworks/R.framework/Resources/include" -DNDEBUG  
-I'/Library/Frameworks/R.framework/Versions/4.2-arm64/Resources/library/Rcpp/include' 
-I'/Library/Frameworks/R.framework/Versions/4.2-arm64/Resources/library/RcppArmadillo/include'
 -I/opt/R/arm64/include   -fPIC  -falign-functions=64 -Wall -g -O2  -c 
testertracenuCpp.cpp -o testertracenuCpp.o
testertracenuCpp.cpp:112:9: error: use of undeclared identifier 'matcentrageCpp'
 i = matcentrageCpp (ti1p, pl, typ1);
I cannot find 'matcentrageCpp' in 
https://github.com/sdray/ade4/tree/ade4-Rcpp/. Do you have a current 
state example somewhere?


Serguei.


 ^

Jean



Le 23 juin 2022 à 15:17, Serguei Sokol  a écrit :

Le 23/06/2022 à 15:02, THIOULOUSE JEAN a écrit :

Hi
Sorry to bother you again with my C/C++ problems in the ade4 package (available on 
CRAN & GitHub).
Thanks to your help, I have succeeded in converting several of the C functions 
to C++ using Rcpp. Now the package compiles without problem and all my C++ 
functions run fine in the dedicated GitHub branch of the package (ade4-Rcpp).
The new problem I am facing now is that in ade4, I use a library of about 30 
utilities functions collected in one file named adesub.c, with adesub.h headers 
file. From what I understand, during package compilation these functions are 
added to the ade4 shared library and they can be called by my other C functions 
without being declared in the init.c or NAMESPACE files, as they do not need to 
be known by R.
I would like to know if it is possible to do the same thing in C++ with Rcpp.

If I understand correctly you goal, just don't put "// [[Rcpp::export]]" before your 
"internal" functions. They will be available to all C and C++ functions in src/ but not seen in R. 
You don't need extra "R CMD SHLIB" either.

Best,
Serguei.

I have found reading the documentation (Dirk's book and Rcpp vignettes) that I 
could use R CMD SHLIB to build a shared library and then dyn.load it in R. I 
have created a file named ade4lib.cpp containing the utilities functions in the 
src directory, and I can make the shared library (ade4lib.so) with RCMD SHLIB. 
But this does not solve my problem because, as I understand it, this makes the 
utilities library functions available to R (which is useless since they are not 
interfaced correctly to R but to C++), and not to the other C++ functions 
(which is what i need to do).

Can anybody see where I am mistaken, and point me to the right direction to 
accomplish this ?
Thank you,
Jean
—-
Jean THIOULOUSE - 
https://lbbe-web.univ-lyon1.fr/en/annuaires-des-membres/thioulouse-jean
https://orcid.org/-0001-7664-0598 (ORCID page)
https://www.springer.com/fr/book/9781493988488 (ade4 Book)
https://pbil.univ-lyon1.fr/JTHome/Biblio/ThioulouseEB2021.pdf (Evol. Biol. 
paper)
___
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

Re: [Rcpp-devel] Rcpp shared libraries (?)

2022-06-23 Thread Serguei Sokol

Le 23/06/2022 à 15:02, THIOULOUSE JEAN a écrit :

Hi

Sorry to bother you again with my C/C++ problems in the ade4 package (available on 
CRAN & GitHub).

Thanks to your help, I have succeeded in converting several of the C functions 
to C++ using Rcpp. Now the package compiles without problem and all my C++ 
functions run fine in the dedicated GitHub branch of the package (ade4-Rcpp).

The new problem I am facing now is that in ade4, I use a library of about 30 
utilities functions collected in one file named adesub.c, with adesub.h headers 
file. From what I understand, during package compilation these functions are 
added to the ade4 shared library and they can be called by my other C functions 
without being declared in the init.c or NAMESPACE files, as they do not need to 
be known by R.

I would like to know if it is possible to do the same thing in C++ with Rcpp.
If I understand correctly you goal, just don't put "// [[Rcpp::export]]" 
before your "internal" functions. They will be available to all C and 
C++ functions in src/ but not seen in R. You don't need extra "R CMD 
SHLIB" either.


Best,
Serguei.

 I have found reading the documentation (Dirk's book and Rcpp 
vignettes) that I could use R CMD SHLIB to build a shared library and 
then dyn.load it in R. I have created a file named ade4lib.cpp 
containing the utilities functions in the src directory, and I can make 
the shared library (ade4lib.so) with RCMD SHLIB. But this does not solve 
my problem because, as I understand it, this makes the utilities library 
functions available to R (which is useless since they are not interfaced 
correctly to R but to C++), and not to the other C++ functions (which is 
what i need to do).


Can anybody see where I am mistaken, and point me to the right direction to 
accomplish this ?

Thank you,
Jean

—-
Jean THIOULOUSE - 
https://lbbe-web.univ-lyon1.fr/en/annuaires-des-membres/thioulouse-jean
https://orcid.org/-0001-7664-0598 (ORCID page)
https://www.springer.com/fr/book/9781493988488 (ade4 Book)
https://pbil.univ-lyon1.fr/JTHome/Biblio/ThioulouseEB2021.pdf (Evol. Biol. 
paper)

___
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

Re: [Rcpp-devel] Help reading libzma file

2022-06-23 Thread Serguei Sokol

Le 23/06/2022 à 04:07, Roberto Spadim a écrit :

Hi folks

I have a .xz file and need to read it and "convert" to a DataFrame using 
Rcpp if possible.


But... I'm with a problem (first time testing this) using liblzma with 
Rcpp, I already know how to do this with CMakeLists.txt, but I don't 
know how to do with RCpp


Anyone have an idea?

You have to use Makevars file, either in src/ or in $HOME/.R/ dir.
In this file you have to add something like:

  PKG_CPPFLAGS += -I
  PKG_LDFLAGS += -L -llzma

To make this solution portable, you can want to use configure script 
which usage is described in WRE 
https://cran.r-project.org/doc/manuals/r-release/R-exts.html#Configure-and-cleanup


Best,
Serguei.



Output from compile time:

 > sourceCpp("D:/Trabalho/FTDB-R/FTDB/src/ftdbR.cpp")
/mingw64/bin/g++ -std=gnu++11  -I"C:/PROGRA~1/R/R-41~1.2/include" 
-DNDEBUG   -I"C:/Users/rspadim/Documents/R/win-library/4.1/Rcpp/include" 
-I"D:/Trabalho/FTDB-R/FTDB/src" -I"D:/Trabalho/FTDB-R/FTDB/inst/include" 
        -O2 -Wall  -mfpmath=sse -msse2 -mstackrealign  -c 
D:/Trabalho/FTDB-R/FTDB/src/xz/xz/decode.cpp -o 
D:/Trabalho/FTDB-R/FTDB/src/xz/xz/decode.o
D:/Trabalho/FTDB-R/FTDB/src/xz/xz/decode.cpp:5:10: fatal error: lzma.h: 
No such file or directory

  #include 
           ^~~~
compilation terminated.
make: *** [C:/PROGRA~1/R/R-41~1.2/etc/x64/Makeconf:245: 
D:/Trabalho/FTDB-R/FTDB/src/xz/xz/decode.o] Error 1

Error in sourceCpp("D:/Trabalho/FTDB-R/FTDB/src/ftdbR.cpp") :
   Error 1 occurred building shared library.
In addition: Warning message:
In normalizePath(path.expand(path), winslash, mustWork) :
   path[1]="D:/Trabalho/FTDB-R/FTDB/src/../inst/include": O sistema não 
pode encontrar o caminho especificado



___
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

Re: [Rcpp-devel] Assertion error in ud_ep.c, when running with MPI

2022-06-14 Thread Serguei Sokol

Hi,

Probably, this issue would be better posted here 
https://github.com/openucx/ucx/issues


Best,
Serguei.

Le 14/06/2022 à 07:24, Maddegedara Lalith a écrit :

Hello,

I want to use RInside in my C++ based MPI application to do time series 
forecasting using the auto.arima library of R. The RInside instance in 
each MPI rank is expected to do an independent calculation (e.g. time 
series forecast).


With one MPI rank, it always completes without producing any error.  
However, with more than 1 mpi ranks, it produces the following error. 
Depending on the run, different numbers of mpi ranks produce the same 
error. On rare occasions, all the ranks successfully complete the 
execution. Further, I found that even your example 
"rinside_mpi_sample4.cpp" produces the same error.


I am using the Intel MPI library (version 2021.1). I tried 
compiling with icpc and g++. Both produced the same error.

Could you please help me to solve this problem.

With best regards
Lal

[ibis:14878:0:14992]       ud_ep.c:565  Assertion `ep->dest_ep_id == 
UCT_UD_EP_NULL_ID || ep->dest_ep_id == ctl->conn_rep.src_ep_id' failed


 backtrace (tid:  14994) 
  0 0x0004d455 ucs_debug_print_backtrace()  ???:0
  1 0x00042b5f uct_ud_ep_process_rx()  ???:0
  2 0x000471cd uct_ud_mlx5_ep_t_delete()  ???:0
  3 0x0003ebdf uct_ud_iface_release_desc()  ???:0
  4 0x00040436 ucs_cpu_get_memcpy_bw()  ???:0
  5 0x0004050b ucs_cpu_get_memcpy_bw()  ???:0
  6 0x00041343 ucs_async_dispatch_handlers()  ???:0
  7 0x00041488 ucs_async_dispatch_timerq()  ???:0
  8 0x00043c34 ucs_async_pipe_drain()  ???:0
  9 0x7ea5 start_thread()  pthread_create.c:0
10 0x000fe96d __clone()  ???:0
=

___
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

Re: [Rcpp-devel] [R-pkg-devel] moving from C to C++ with Rcpp in R package

2022-06-02 Thread Serguei Sokol

Le 02/06/2022 à 15:32, Dirk Eddelbuettel a écrit :


(Removed CC to r-package-devel)

On 2 June 2022 at 16:24, Ivan Krylov wrote:
| Unfortunately, this means editing RcppExports.R, which (I think)
| prevents you from enjoying the way Rcpp::compileAttributes() sets
| everything up for you.

Yes. I think that is the best I came up with once too in a time of need in a
package actively mixing C and C++.  It simply is easier to have just C++, and
I think you generally can just convert your C files.
... or simply call them from exported C++ and letting Rcpp to manage the 
whole R export and init.c


Best,
Serguei.

___
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 function results are different on different systems

2021-08-20 Thread Serguei Sokol

Le 19/08/2021 à 19:01, Sokol Serguei a écrit :

Le 19/08/2021 à 17:41, Sokol Serguei a écrit :

Le 19/08/2021 à 17:04, Naeem Khoshnevis a écrit :


Thank you so much, everyone, for responding to this email.


Dirk,

  * I didn't think about testing _equality_ of doubles because the
numbers are significantly different (e.g., instead of 0.5,
chooses 1.5). However, that is a valid point, and I should be
aware of that.
  * You are right about the serial runs. Whenever I deactivate
OpenMP, the results are correct.


Serguei,

  * Thanks for the comments. Yes, I agree. It seems outer is a better
option. We have started with outer. However, outer builds the
entire matrix of differences first, then finds the minimum index.
In our application, it requires 200 GB of memory to build that
matrix.

This problem was hardly foreseeable from examples in hand. But I still 
think that a pure R solution can be a runner:


   ac=a*sc
   bc=b*sc
   out=vapply(seq_along(bc), function(i) 
which.min(abs(ac-bc[i])+cd[i]), integer(1))


Further optimizing: "+cd[i]" does not change the result of which.min() 
so it can be dropped. The same reasoning can be applied to "*sc".
As the problem is reduced to searching the index of the closest value in 
'a' to 'b[i]', it can be solved in O(log(n)) time (instead of O(n)) by 
binary search if 'a' can be sorted before operations. If 'b' can also be 
sorted, then the whole timing can be further reduced. E.g. we can use 
findInterval() implementing such optimal searching. However, its result 
should be post-processed to find value indexes instead of interval ones 
and get back to original indexes instead of sorted ones. Something like:


    oa=order(a)
    ob=order(b)
    ao=a[oa]
    bo=b[ob]
    i=findInterval(bo, ao) # main work is done here
    ii=i+ifelse(i < n & ao[i+1]-bo < bo-ao[i], 1, 0)
oops... Here 'n' is length(b). It was defined in my environment, so the 
snipet worked as expected when I copy/pasted it. But it may be not your 
case. Sorry.


Best,
Serguei.


    out=oa[ii]
    out[ob]=out

Serguei.


Best,
Serguei.


  * Rcpp does the job with around 10 MB. That is why I switched to
Rcpp. Please let me know your thoughts.


  Iñaki,

  * Thanks for your suggestion. Yes, the problem is shared values,
and it resolved the issue. I really appreciate it.


Best regards,
Naeem

On Thu, Aug 19, 2021 at 4:56 AM Iñaki Ucar > wrote:


On Thu, 19 Aug 2021 at 04:53, Dirk Eddelbuettel mailto:e...@debian.org>> wrote:
>
>
> Naeem,
>
> I would simplify, simplify, simplify -- as 'Rcpp FAQ 7.31'
reminds us all,
> testing _equality_ of doubles is challenging anyway.
>
> Besides, it may make sense to would ascertain first you get
what you want in
> _purely serial modes_ and then move to OpenMP.

Exactly. Serial execution should be fine. I.e., if you set the number
of threads to 1, then all platforms will return the same result.
However, you have defined a number of variables outside the parallel
region, and then you modify them inside the parallel region. OpenMP
takes those variables as shared by default, which leads to the
unexpected results you are getting. You need to tell OpenMP that
those
variables are threadprivate. Or you could just define them inside the
parallel region, so that OpenMP knows that they are private without
additional hints.

-- 
Iñaki Úcar



___
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

Re: [Rcpp-devel] efficient ingestion of "sparse csv"

2021-05-26 Thread Serguei Sokol

Le 26/05/2021 à 16:36, Vincent Carey a écrit :

On this theme, the following proved sufficient to ingest and
convert sparse csv without column headers or row names:
Nice to share your final solution which could be further shorten to smth 
like:


#include "RcppArmadillo.h"
using namespace Rcpp;
// [[Rcpp::depends(RcppArmadillo)]]
// [[Rcpp::export]]
arma::sp_mat parse_sparse_csv_short(std::string fname) {
   arma::sp_mat D;
   D.load(fname, arma::csv_ascii);
   return D;
}

Best,
Serguei.



#include "RcppArmadillo.h"

using namespace Rcpp;

// [[Rcpp::depends(RcppArmadillo)]]
// [[Rcpp::export]]

List parse_sparse_csv_impl(SEXP fname) {
using namespace Rcpp;
std::string v = Rcpp::as(fname);
arma::sp_mat D;
D.load(v, arma::csv_ascii);
return Rcpp::List::create(Rcpp::Named("sp")=D);
}

On Mon, May 10, 2021 at 11:19 PM Dirk Eddelbuettel > wrote:



Vincent,

In the broad terms of the question the best answer may be a simple
"sure".
More seriously, there have been many approaches.  Consider for
example the
recent Rcpp Gallery post lead by Zach (with some edits by me):
https://gallery.rcpp.org/articles/sparse-matrix-class/


It's focus on not copying  again if we already have them as R
vectors,
which is a fair point. If the goal is to get to SuperLU via
(Rcpp)Armadillo
then I do not think you can avoid the (internal) copies.  As always, the
answer may be "it depends".

Hope this helps, happy to refine,  Dirk

-- 
https://dirk.eddelbuettel.com  |

@eddelbuettel | e...@debian.org 


The information in this e-mail is intended only for the person to whom it is
addressed. If you believe this e-mail was sent to you in error and the 
e-mail
contains patient information, please contact the Partners 
Compliance HelpLine at
http://www.partners.org/complianceline 
 . If the e-mail was sent to you 
in error
but does not contain patient information, please contact the sender and 
properly

dispose of the e-mail.

___
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

Re: [Rcpp-devel] rcpp compile problem

2020-01-28 Thread Serguei Sokol

Le 28/01/2020 à 10:04, Gerhard Boenisch a écrit :

Hi

I am new to rcpp and am unable to compile code I know works. I must be 
doing something stupid.


I am using Windows (10).

I have received the cpp source files for the BHPMF package. We want to 
make the package work on the latest version of R.


I am currently using R 3.4.4 (the last version BHPMF works).

I have installed rcpp (install.packages("Rcpp")).

I have installed RTools.

The source files are in J:\TempWork\BHPMF_CRAN_Archive\src

I am using the RTools C++ compiler in C:\Progs\Rtools\mingw_64\bin\c++.exe

When I try to compile, the compiler cannot find file R.h and ends with 
error message:


J:\TempWork\BHPMF_CRAN_Archive\src>C:\Progs\Rtools\mingw_64\bin\c++.exe 
HPMF_main.cpp


HPMF_main.cpp:11:35: fatal error: R.h: No such file or directory

#include   // R functions

    ^

compilation terminated.

Can someone please tell me how I can tell the compiler to find R.h and 
the other files from the rcpp package?


I  compile from the command prompt. Is this the right way or must I do 
this in R?


Have I missed installing something? Have I installed staff in the wrong 
order?

I see two options for compiling your packages.
1. You do

 R CMD build J:\TempWork\BHPMF_CRAN_Archive\

which should produce something like BHPMF_1.0.tar.gz (1.0 being an
example of package version). Then
 R CMD check J:\TempWork\BHPMF_1.0.tar.gz (put the true name of the 
produced tarball here). After that you can see all used flags and 
options in J:\TempWork\BHPMF.Rcheck\00install.out file.


2. In R session having J:\TempWork\BHPMF_CRAN_Archive\ as working dir you do

 devtools::load_all()

The first option is better suited for final stages of your dev work 
while the second one is more appropriate for your iterative dev cycles.


Best,
Serguei.



Thanks in advance.

Best wishes

gerhard_boeni...@bgc-jena.mpg.de 



Scientific Data Manager

Dr. Gerhard Boenisch Phone: +49 3641 576277

MPI für Biogeochemie   Fax:    +49 3641 577200

Hans-Knoell Str. 10, 07745 Jena,    Germany

http://www.bgc-jena.mpg.de/~boenisch


___
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

Re: [Rcpp-devel] Value type of DataFrame::operator[]

2019-12-09 Thread Serguei Sokol

Le 07/12/2019 à 00:29, Qiang Kou a écrit :
What about using "TYPEOF"? Please see R Internals 
(https://cran.r-project.org/doc/manuals/r-release/R-ints.html#SEXPTYPEs) 
for more details.
... and if you want to convert integer encoded SEXPTYPE to a human 
readable string you can use


const char * Rf_type2char(SEXPTYPE);

By the way, conversion in the opposite direction is made by

SEXPTYPE Rf_str2type(const char *);

Best,
Sergueï.



#include 

using namespace Rcpp;

// [[Rcpp::export]]
DataFrame rcpp_df(){
   NumericVector   numeric = {1,3};
   IntegerVector   integer = {1,3};
   CharacterVector character = {"B","D"};
   LogicalVector   logical = {false, true};

   DataFrame df = DataFrame::create(Named("numeric") = numeric,
                                 Named("integer") = integer,
                                 Named("character") = character,
                                 Named("logical") = logical);

   Rcpp::Rcout << "numeric:" << TYPEOF(df["numeric"]) << std::endl;
   Rcpp::Rcout << "integer:" << TYPEOF(df["integer"]) << std::endl;
   Rcpp::Rcout << "character:" << TYPEOF(df["character"]) << std::endl;
   Rcpp::Rcout << "logical:" << TYPEOF(df["logical"]) << std::endl;

   return df;
}

/*
 > Rcpp::sourceCpp("test.cc")
 > df <- rcpp_df()
numeric:14
integer:13
character:13
logical:10
*/

On Fri, Dec 6, 2019 at 2:59 PM Brook Milligan > wrote:


Is there a way to discover the value type for the result of calling
DataFrame::operator[]?  For example, is there a metafiction that
will deduce the type?

A similar question could be asked about the result of calling
Vector::operator[].

I understand that these functions actually return a proxy, but it
would be very helpful to find out the underlying value type.

Thanks for your help.

Cheers,
Brook

___
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 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] RInside: C/C++ function(s) passed to R?

2019-11-13 Thread Serguei Sokol

Le 13/11/2019 à 09:31, LEVRA-JUILLET William a écrit :

Thx Sergei,

Yes you're right, I want to use my c++ function as a R function.
Sorry about the Banana function that takes 2 scalars, you’re right again, it 
should take a vector as input.
Please ignore this for now (this is a dummy example)

My c++ "Banana" function is already compiled in my project by the time I start 
he R session (via RInside).
I can pass it to R via a XPtr.
Something like R["myfuncptr"] = Rcpp::XPtr(new func_type (Banana));
So, in R I got "myfuncptr" which is an "externalptr".

Now my only problem is to make that "externalptr" seen as a R function in R.
Maybe with something like a ".Call(externalptr, param...)"?
Here (https://stackoverflow.com/questions/13904786/r-pointer-to-c-function), 
Romains says something about wrapping the external pointer into an R function...
But I struggle to find the correct way to achieve this with the function 
parameters...
Well, I won't help you in this fight against XPtrs but I have made my 
own hack without them and it works on my Linux. May be it will inspire 
you to smth useful.


8<--file optim2.cpp
#include 
using namespace Rcpp;

double Banana(double x, double y) {return (1-x)*(1-x)+100*(y-x*x)*(y-x*x);};
RcppExport SEXP wrap_banana(SEXP xSEXP, SEXP ySEXP) {
BEGIN_RCPP
Rcpp::RObject rcpp_result_gen;
Rcpp::traits::input_parameter< double >::type x(xSEXP);
Rcpp::traits::input_parameter< double >::type y(ySEXP);
rcpp_result_gen = Rcpp::wrap(Banana(x, y));
return rcpp_result_gen;
END_RCPP
}

int main(int argc, char *argv[]) {
   RInside R(argc, argv);
   R.parseEval("dyn.load('optim2.so'); fn <- function(v) 
invisible(.Call('wrap_banana', v[1L], v[2L]))");

   Rcpp::RObject res = R.parseEval("optim(c(-2,-2), fn)");
   Rf_PrintValue(res);
   return 0;
}
8<--end of file optim2.cpp

In classical makefile I had to modify implicit rules to produce and use 
an intermidiate .so library (which is loaded in R session):

%.so:   %.cpp
$(CXX) $(CPPFLAGS) $(CXXFLAGS) $(RINSIDELIBS) -fPIC -shared $< -o $@
%:  %.so
$(CXX) $^ $(LDLIBS) -o $@

Best,
Serguei.



Thank you
William




-Message d'origine-
De : Serguei Sokol 
Envoyé : mardi 12 novembre 2019 17:49
À : LEVRA-JUILLET William ; 
rcpp-devel@lists.r-forge.r-project.org
Objet : Re: [Rcpp-devel] RInside: C/C++ function(s) passed to R?

Le 08/11/2019 à 11:06, LEVRA-JUILLET William a écrit :

Hi all,

I’m evaluating RInside/Rcpp to interact our C++ project with R.

I’ve seen the samples to get/set data (matrix, dataframes, etc)
to/from R and it builds/works fine.

(Using gcc/mingw compiler from RTools on Windows)

My questions is about functions…

My project is C++, and it uses RInside to call R from our code.

Let’s say I have a C++ function implemented in my project, and I want
to pass that c++ function to R so it can be “used” as a R function
(typically passed as argument to optim/optimize)

I couldn’t find a way to do that using RInside.

Ideally, I’d like to achieve something like:

#include 

double Banana(double x, double y)

{

return (1-x)*(1-x)+100*(y-x*x)*(y-x*x);

}

Int main()

{

RInside R(argc,, argv);

R[“fn”] = Banana;

R.parseEval(“optim(c(-2,-2), fn”);

}

I believe it’s similar to
https://stackoverflow.com/questions/53729493/rinside-wrapping-a-c-func
tion-for-use-in-an-r-function

But I can’t really understand the Rcpp::XPtr suggestion from Dirk.

Any help or pointers welcome J

Sorry for a late comment but for what it's worth, XPtr won't help here as 
optim() expects an R function, not C++ pointer to a function.
Moreover, this R function takes only one argument -- a vector, not a couple of 
scalars. So maybe you meant something similar to:
#include 
int main(int argc, char *argv[]) {
 RInside R(argc, argv);
 R.parseEval("Rcpp::cppFunction('double Banana(double x, double y) {return 
(1-x)*(1-x)+100*(y-x*x)*(y-x*x);};', cacheDir='lib')");
 R.parseEval("fn <- function(p, ...) Banana(p[1L], p[2L])");
 Rcpp::RObject res = R.parseEval("optim(c(-2,-2), fn)");
 Rf_PrintValue(res);
 return 0;
}

which prints:

$par
[1] 0.9990277 0.9977136

$value
[1] 1.269722e-05

$counts
function gradient
   157   NA

$convergence
[1] 0

$message
NULL

As already underlined by others this is a sub-optimal way to use C++ functions in R but 
here it is imposed by optim() interface. For better designed interfaces (at least from 
"optimality of R/C++ exchanges" point of view) refer to examples cited by Dirk 
and Inaki.

Best,
Serguei.



Thx a mil

mailbox:///home/sokol/.thunderbird/r8hre4b3.default/Mail/pop.insa-tlse
.fr/Inbox?number=44=quotebody=1.1.2=image005.jpg
<https://addactis.letsignit.com/r/32/6de77c22-c065-4906-899e-eec38189d
60a/undefined>

mailbox:///home/sokol/.thunderbird/r8hre4b3.default/Mail/pop.insa-tlse
.fr/

Re: [Rcpp-devel] RInside: C/C++ function(s) passed to R?

2019-11-12 Thread Serguei Sokol

Le 08/11/2019 à 11:06, LEVRA-JUILLET William a écrit :

Hi all,

I’m evaluating RInside/Rcpp to interact our C++ project with R.

I’ve seen the samples to get/set data (matrix, dataframes, etc) to/from 
R and it builds/works fine.


(Using gcc/mingw compiler from RTools on Windows)

My questions is about functions…

My project is C++, and it uses RInside to call R from our code.

Let’s say I have a C++ function implemented in my project, and I want to 
pass that c++ function to R so it can be “used” as a R function 
(typically passed as argument to optim/optimize)


I couldn’t find a way to do that using RInside.

Ideally, I’d like to achieve something like:

#include 

double Banana(double x, double y)

{

return (1-x)*(1-x)+100*(y-x*x)*(y-x*x);

}

Int main()

{

RInside R(argc,, argv);

R[“fn”] = Banana;

R.parseEval(“optim(c(-2,-2), fn”);

}

I believe it’s similar to 
https://stackoverflow.com/questions/53729493/rinside-wrapping-a-c-function-for-use-in-an-r-function


But I can’t really understand the Rcpp::XPtr suggestion from Dirk.

Any help or pointers welcome J
Sorry for a late comment but for what it's worth, XPtr won't help here 
as optim() expects an R function, not C++ pointer to a function. 
Moreover, this R function takes only one argument -- a vector, not a 
couple of scalars. So maybe you meant something similar to:

#include 
int main(int argc, char *argv[]) {
   RInside R(argc, argv);
   R.parseEval("Rcpp::cppFunction('double Banana(double x, double y) 
{return (1-x)*(1-x)+100*(y-x*x)*(y-x*x);};', cacheDir='lib')");

   R.parseEval("fn <- function(p, ...) Banana(p[1L], p[2L])");
   Rcpp::RObject res = R.parseEval("optim(c(-2,-2), fn)");
   Rf_PrintValue(res);
   return 0;
}

which prints:

$par
[1] 0.9990277 0.9977136

$value
[1] 1.269722e-05

$counts
function gradient
 157   NA

$convergence
[1] 0

$message
NULL

As already underlined by others this is a sub-optimal way to use C++ 
functions in R but here it is imposed by optim() interface. For better 
designed interfaces (at least from "optimality of R/C++ exchanges" point 
of view) refer to examples cited by Dirk and Inaki.


Best,
Serguei.



Thx a mil

mailbox:///home/sokol/.thunderbird/r8hre4b3.default/Mail/pop.insa-tlse.fr/Inbox?number=44=quotebody=1.1.2=image005.jpg 



mailbox:///home/sokol/.thunderbird/r8hre4b3.default/Mail/pop.insa-tlse.fr/Inbox?number=44=quotebody=1.1.3=image006.png 
 
mailbox:///home/sokol/.thunderbird/r8hre4b3.default/Mail/pop.insa-tlse.fr/Inbox?number=44=quotebody=1.1.4=image007.png 
 
mailbox:///home/sokol/.thunderbird/r8hre4b3.default/Mail/pop.insa-tlse.fr/Inbox?number=44=quotebody=1.1.5=image008.png 






*   William **LEVRA-JUILLET*
    Senior Software Architect
*   ADDACTIS Software*
    +33 (0)4 81 92 13 00


www.addactis.com 





 
 






*   William **LEVRA-JUILLET*
    Senior Software Architect
*   ADDACTIS Software*
    +33 (0)4 81 92 13 00


www.addactis.com 




___
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

Re: [Rcpp-devel] class methods and R_RegisterCCallable

2019-10-28 Thread Serguei Sokol

Le 24/10/2019 à 18:23, Leonardo Silvestri a écrit :

On 24 October 2019 at 16:01, Serguei Sokol wrote:
| I'am trying to find a way to access class methods of one Rcpp 
package | (say pkgM) from C++ code in another one (say pkgA).
| If it were simple C++ routines and not class methods, the way is 
known | and even automatized. E.g. if I generate pkgM with

| | Rcpp::Rcpp.package.skeleton("pkgM", module=TRUE)
| Rcpp::compileAttributes("pkgM")
| | then in pkgM/src/RcppExports.cpp we can see
| | | static const R_CallMethodDef CallEntries[] = {
|  {"_pkgM_rcpp_hello_world", (DL_FUNC) &_pkgM_rcpp_hello_world, 0},
| ...
| | As far as I understand, it registers _pkgM_rcpp_hello_world for 
use with | R_GetCCallable from other packages. And it is a consequence 
of preceding

| rcpp_hello_world() with "// [[Rcpp::export]]".
| If I add "// [[Rcpp::interfaces(r, cpp)]]" in 
pkgM/src/rcpp_module.cpp | it produces 
pkgM/inst/include/{pkgM.h,pkgM_RcppExports.h} and

| | static const R_CallMethodDef CallEntries[] = {
| ...
|  {"_pkgM_RcppExport_registerCCallable", (DL_FUNC) | 
&_pkgM_RcppExport_registerCCallable, 0},

| ...
| but still nothing for methods and functions from RCPP_MODULE.
| | So my question, what is a canonical way (if defined) to expose 
class | methods (via RCPP_MODULE or any other mechanism) in such a way 
that they | become callable from C++ code of another package? Am I 
supposed to | register them manually in some way using | 
_pkgM_RcppExport_registerCCallable()? And if so, how can I retrieve 
them | in C++ code of another package?


From the top of my head I would say "No" for the simple reason that 
all R
does (and this is the same for all other foreign-function interfaces) 
has to
be C based.  Hence no class methods.  C++ signatures are compiler 
dependent,

and have been avoided for that reason for as long as we had them.

So I fear you need plain C wrappers for every member function you want to
expose. I could of course be wrong -- if someone else has 
counter-examples I

would be all ears!

Dirk

--
http://dirk.eddelbuettel.com | @eddelbuettel | edd at debian.org


I believe Dirk is correct in saying the traditional mechanism won't work 
for non-static member functions, but if the C++ code is fully contained 
in header files in 'inst/include', then it's possible to access that 
code from another package since there's no library that one needs to 
link against. I believe that's the mechanism that Rcpp itself uses.


Things get ugly when one tries to link to another package's library. 
I've tried a few experiments with Dirk's help 
(https://github.com/lsilvest/linktest), but haven't found a reliable way 
to make this portable (things break when packages are installed 
pre-compiled).

Thanks for sharing, it saved me some iterations of trying.

I tested a direct exposing of member function with
R_RegisterCCallable("pkgM", "Num__setx", (DL_FUNC) ::setX);

and then passing a call with a first argument as pointer to an object.
It worked on my Linux with gcc 8.3 with a warning at
(DL_FUNC) ::setX. Then I read here 
https://isocpp.org/wiki/faq/pointers-to-members that such kind of cast 
are explicetly prohibeted as inducing undefined behavior. So my working 
example was just a chance.


As Dirk suggested, wrapping a method in a plain C-line function worked.
Here, for the record, main steps:
0. put Num class definition in pkgM/inst/include/pkgM.h
1. in pkgM/Num.cpp

void wrap_Num__setx(Num *pobj, double val) {
pobj->setX(val);
}

2. in pkgM/RcppExports.cpp add
R_RegisterCCallable("pkgM", "Num__setx", (DL_FUNC) _Num__setx);
to R_init_pkgM(DllInfo *dll) { ...

3. in pkgA/DESCRIPTION
LinkingTo: Rcpp, pkgM
Depends: pkgM

4. in pkgA/rcpp_hello_world.cpp
// [[Rcpp::export]]
void a_setx(XPtr xp, double val)
{
  XPtr p(xp);
  static void (*fun)(Num*, double) = NULL;
  if (fun == NULL) fun = (void (*)(Num*, double)) 
R_GetCCallable("pkgM","Num__setx");

  fun(p, val);
}

5. in R session
library(pkgA) # loads pkgM too
num=Num$new() # created with pkgM infrastructure
a_setx(num@.xData$.pointer, 100)
num$x
#[1] 100

Best,
Serguei.
___
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] class methods and R_RegisterCCallable

2019-10-24 Thread Serguei Sokol

Hi,

I'am trying to find a way to access class methods of one Rcpp package 
(say pkgM) from C++ code in another one (say pkgA).
If it were simple C++ routines and not class methods, the way is known 
and even automatized. E.g. if I generate pkgM with


Rcpp::Rcpp.package.skeleton("pkgM", module=TRUE)
Rcpp::compileAttributes("pkgM")

then in pkgM/src/RcppExports.cpp we can see


static const R_CallMethodDef CallEntries[] = {
    {"_pkgM_rcpp_hello_world", (DL_FUNC) &_pkgM_rcpp_hello_world, 0},
...

As far as I understand, it registers _pkgM_rcpp_hello_world for use with 
R_GetCCallable from other packages. And it is a consequence of preceding

rcpp_hello_world() with "// [[Rcpp::export]]".
If I add "// [[Rcpp::interfaces(r, cpp)]]" in pkgM/src/rcpp_module.cpp 
it produces pkgM/inst/include/{pkgM.h,pkgM_RcppExports.h} and


static const R_CallMethodDef CallEntries[] = {
...
    {"_pkgM_RcppExport_registerCCallable", (DL_FUNC) 
&_pkgM_RcppExport_registerCCallable, 0},

...
but still nothing for methods and functions from RCPP_MODULE.

So my question, what is a canonical way (if defined) to expose class 
methods (via RCPP_MODULE or any other mechanism) in such a way that they 
become callable from C++ code of another package? Am I supposed to 
register them manually in some way using 
_pkgM_RcppExport_registerCCallable()? And if so, how can I retrieve them 
in C++ code of another package?


Thanks,
Serguei.

___
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] stderr@@GLIBC_2.2.5' can not be used when making a shared object; recompile with -fPIC

2019-02-01 Thread Serguei Sokol

On 01/02/2019 17:22, Sariya, Sanjeev wrote:

Hi devs,

Platform – Unix

R version 3.4 and 3.5

Errors for –fPIC installation for two packages:

1)

I’m using r3.5 to install simplexreg on my local as:

/mnt/mfs/cluster/bin/R-3.5/bin/R  CMD INSTALL -l ./ simplexreg_1.3.tar.gz

When I do it, I get error as follows:

* installing *source* package ‘simplexreg’ ...

** package ‘simplexreg’ successfully unpacked and MD5 sums checked

** libs

g++  -I"/mnt/mfs/cluster/bin/R-3.5/include" -DNDEBUG 
-I/gsl/win64/include  -I/usr/local/include   -fpic  -g -O2  -c 
sim.gee.cpp -o sim.gee.o


g++ -shared -L/usr/local/lib -o simplexreg.so sim.gee.o -L/gsl/win64/lib 
-lgsl -lgslcblas -lm


/usr/bin/ld: /usr/local/lib/libgslcblas.a(xerbla.o): relocation 
R_X86_64_PC32 against symbol `stderr@@GLIBC_2.2.5' can not be used when 
making a shared object; recompile with -fPIC



...


May I know how to bypass this error?
Try to do what error message says: "recompile with -fPIC" (you have 
-fpic instead). It is safer to get the exact syntax from

$ R CMD config CXXPICFLAGS
-fPIC

Serguei.
___
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] Debugging Rcpp for Solaris

2019-01-09 Thread Serguei Sokol

On 09/01/2019 15:03, mike guggis wrote:

Hello,

I submitted a package to CRAN that passed all checks in RStudio but 
failed when it was tested on Solaris (after it was on CRAN). Now I need 
to correct the error and resubmit to CRAN. I would like to avoid using 
CRAN as a debugging tool. Is there a way to check packages on Solaris? I 
am using Windows 10.


The packages checks are here 
https://cran.r-project.org/web/checks/check_results_BiProbitPartial.html


An excerpt of the error is below

BiProbitPartialMCMC.cpp:424:49: error: call of overloaded ‘log(bool)’ is 
ambiguous
  double SupportRatio = log(abs(rhoStar)<1) - log(abs(rho)<1);
  ^

There is a similar error for the second log function. It seems I can 
simply fix it with an if/else statement, but I would like to check it 
before submitting back to CRAN.
It does not respond to your question about Solaris but I am just curios 
about the logic behind this line of code. Do you really mean to 
calculate a log() of Boolean variable with a result -Inf (for false) and 
0 (for true)? Or is it a typo and you meant (log(abs(rhoStar)) < 1)? 
Then what would be the meaning of a difference between two booleans? Can 
it really be a "SupportRatio" with only three possible values: -1, 0 and 1?


You can just ignore all these questions if you cannot afford spare time.

Best,
Serguei.
___
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] trying to insert a number as first element of already existing vector

2018-12-12 Thread Serguei Sokol

Le 12/12/2018 à 02:25, Mark Leeds a écrit :


Just to close this thread out, I did a more comprehensive benchmark 
using 8 different approaches

and it looks like

A) Jan's solution using memcopy and NumericVector.

B) A push_front solution using  NumericVector

C) Serguei's const trick solution using NumericVector

are the top 3 solutions in terms of speed with  B) push_front 
technically the winner !
Hm. I am wondering how much the jury was payed under the table by the 
promoters of push_front()? ;)
More seriously, in your results push_front() is in mybar7 which is 
second after mybar3 in every timing position (min, lq etc.):


> Unit: milliseconds
>expr  minlq  mean
> medianuq   max neval
>   mybar3(testvec, testelem) 27.93793  31.94207  36.76242  37.17255
> 41.52102  47.31534   100
>   mybar7(testvec, testelem) 30.80926  33.41609  38.45877  37.71916
> 43.70371  48.88513   100

To be honest, on my machine, mybar3 is far behind other champions (72 ms 
vs 60 ms) and there is no clear leader among the latter (mybar4 to 8) as 
the leadership is changing from one run to another.


Best,
Serguei.


Thanks to everyone for their help. I learned much.

#==

#include 
using namespace Rcpp;

// [[Rcpp::export]]
std::vector mybar(const std::vector& x, double firstelem) {
    std::vector tmp(x.size() + 1);
    tmp[0] = firstelem;
    for (int i = 1; i < (x.size()+1); i++)
  tmp[i] = x[i-1];
    return tmp;
}
// [[Rcpp::export]]
std::vector mybar2(const std::vector& x, double firstelem) {
    std::vector tmp(x.size() + 1);
    tmp[0] = firstelem;
    std::copy(x.begin(), x.end(), tmp.begin()+1);
    return tmp;
}

// [[Rcpp::export]]
NumericVector mybar3(NumericVector x, double firstelem) {
    NumericVector tmp(x.size() + 1);
    tmp[0] = firstelem;
    std::copy(x.begin(), x.end(), tmp.begin()+1);
    return tmp;
}

// [[Rcpp::export]]
NumericVector mybar4(NumericVector x, double firstelem) {
    NumericVector result(x.size() + 1);
    result[0] = firstelem;
    std::memcpy(result.begin()+1, x.begin(), x.size()*sizeof(double));
    return result;
}

// [[Rcpp::export]]
NumericVector mybar5(NumericVector x, NumericVector y) {
    NumericVector result(x.size() + y.size());
    std::memcpy(result.begin(), x.begin(), x.size()*sizeof(double));
    std::memcpy(result.begin()+x.size(), y.begin(), 
y.size()*sizeof(double));

    return result;
}

// [[Rcpp::export]]
NumericVector mybar6(NumericVector x, double firstelem) {
   x.insert(0, firstelem);
   return x;
}

// [[Rcpp::export]]
NumericVector mybar7(NumericVector x, double firstelem) {
   x.push_front(firstelem);
   return x;
}

// [[Rcpp::export]]
NumericVector mybar8(const NumericVector , const NumericVector ) {
     NumericVector result(x.size() + y.size());
     std::memcpy(result.begin(), x.begin(), x.size()*sizeof(double));
     std::memcpy(result.begin()+x.size(), y.begin(),
     y.size()*sizeof(double));
     return result;
}


/*** R

library(microbenchmark)

  n=1E7
  testvec = c(1,seq_len(n))
  testelem <- 7
  microbenchmark(c(testelem, testvec), mybar(testvec,testelem),
  mybar2(testvec,testelem),
  mybar3(testvec,testelem),
  mybar4(testvec,testelem),
  mybar5(testvec,testelem),
  mybar6(testvec,testelem),
  mybar7(testvec,testelem),
  mybar8(testvec,testelem)
  )


  */

microbenchmark(c(testelem, testvec), mybar(testvec,testelem),
+  mybar2(testvec,testelem),
+  mybar3(testvec,testelem),
+  mybar4(testvec,testelem)  [TRUNCATED]
Unit: milliseconds
   expr  min    lq  mean
median    uq   max neval
   c(testelem, testvec) 33.82390  37.41429  42.70048  42.48487  
47.72840  81.53239   100
   mybar(testvec, testelem) 93.35373 100.67106 105.30134 105.67559 
109.62234 125.15337   100
  mybar2(testvec, testelem) 88.00770  94.62231  98.84161  98.51031 
102.49516 114.58349   100
  mybar3(testvec, testelem) 27.93793  31.94207  36.76242  37.17255  
41.52102  47.31534   100
  mybar4(testvec, testelem) 31.37486  34.73718  39.72786  40.83917  
44.21151  49.48883   100
  mybar5(testvec, testelem) 30.90608  35.25496  40.24085  40.59592  
44.88581  50.33709   100
  mybar6(testvec, testelem) 33.24435  38.32075  43.11721  43.46578  
47.93726  52.72538   100
  mybar7(testvec, testelem) 30.80926  33.41609  38.45877  37.71916  
43.70371  48.88513   100
  mybar8(testvec, testelem) 30.88067  35.01826  40.38411  40.02501  
44.49641  73.84147   100

 >




On Mon, Dec 10, 2018 at 8:42 AM Serguei Sokol <mailto:serguei.so...@gmail.com>> wrote:


Le 10/12/2018 à 13:04, Jan van der Laan a écrit :
 > Small addendum: A large part of the performance gain in my
example comes
 > from using NumericVector instead of std::vector. Which
avoids a
 > conversion. An example using std::copy with Numeric vector runs
in the
 > sa

Re: [Rcpp-devel] trying to insert a number as first element of already existing vector

2018-12-10 Thread Serguei Sokol

Le 10/12/2018 à 16:48, Mark Leeds a écrit :
...
Oh, as I said, the documentation on Rcpp is incredible but is there 
anything discussing memory because

I'm pretty lost on that. Thanks again.

Are you talking about this list archives?
http://lists.r-forge.r-project.org/pipermail/rcpp-devel/

Serguei.
___
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] trying to insert a number as first element of already existing vector

2018-12-10 Thread Serguei Sokol

Le 10/12/2018 à 13:04, Jan van der Laan a écrit :
Small addendum: A large part of the performance gain in my example comes 
from using NumericVector instead of std::vector. Which avoids a 
conversion. An example using std::copy with Numeric vector runs in the 
same time as the version using memcpy.


Yep.
Few more percents of mean cpu time can be saved by using "const &" trick :

// [[Rcpp::export]]
NumericVector mybar5(const NumericVector , const NumericVector ) {
   NumericVector result(x.size() + y.size());
   std::memcpy(result.begin(), x.begin(), x.size()*sizeof(double));
   std::memcpy(result.begin()+x.size(), y.begin(), 
y.size()*sizeof(double));

   return result;
}

# output
Unit: microseconds
  expr min   lq mean   median   uq 
max
  c(testelem, testvec) 258.343 338.3110 418.0047 343.4450 378.7850 
3077.347
  mybar(testvec, testelem) 352.699 366.8770 498.3948 374.6635 450.4420 
3046.408
 mybar2(testvec, testelem) 334.820 348.3685 425.0098 354.7240 366.5270 
3024.128
 mybar3(testvec, testelem) 233.689 244.8640 315.7256 247.5180 255.0955 
2945.068
 mybar4(testvec, testelem) 232.083 241.9655 340.0751 245.0035 252.8260 
2934.312
 mybar5(testvec, testelem) 150.787 242.7685 285.4264 245.9465 254.1880 
2049.493


Serguei.



Jan



On 10-12-18 12:28, Jan van der Laan wrote:


For performance memcpy is probably fastest. This gives the same 
performance a c().


// [[Rcpp::export]]
NumericVector mybar3(NumericVector x, double firstelem) {
   NumericVector result(x.size() + 1);
   result[0] = firstelem;
   std::memcpy(result.begin()+1, x.begin(), x.size()*sizeof(double));
   return result;
}


Or a more general version concatenating vector of arbitrary lengths:


// [[Rcpp::export]]
NumericVector mybar4(NumericVector x, NumericVector y) {
   NumericVector result(x.size() + y.size());
   std::memcpy(result.begin(), x.begin(), x.size()*sizeof(double));
   std::memcpy(result.begin()+x.size(), y.begin(), 
y.size()*sizeof(double));

   return result;
}



 > n=1E7
 > testvec = c(1,seq_len(n))
 > testelem <- 7
 > microbenchmark(c(testelem, testvec), mybar(testvec,testelem),
+   mybar2(testvec,testelem),
+   mybar3(testvec,testelem),
+   mybar4(testvec,testelem)
+   )
Unit: milliseconds
   expr   min    lq  mean    median   
uq    max neval
   c(testelem, testvec)  36.48577  36.93754  41.10550  43.76742 
44.20709  46.09741   100
   mybar(testvec, testelem) 102.54042 103.21756 106.88749 104.32033 
110.31527 119.55512   100
  mybar2(testvec, testelem)  95.64696  96.19447 100.24691 102.61380 
103.58189 109.28290   100
  mybar3(testvec, testelem)  36.45794  36.87915  40.43486  37.18063 
43.49643  95.49049   100
  mybar4(testvec, testelem)  36.51334  37.05409  41.39680  43.20627 
43.57958  94.95482   100



Best,
Jan



On 10-12-18 12:10, Serguei Sokol wrote:

Le 09/12/2018 à 09:35, Mark Leeds a écrit :
Hi All: I wrote below and it works but I have a strong feeling 
there's a better way to do it.
If performance is an issue, you can save few percents of cpu time by 
using std::copy() instead of explicit for loop. Yet, for this 
operation R's c() remains the best bet. It is more then twice faster 
than both Rcpp versions below:


#include 
using namespace Rcpp;

// [[Rcpp::export]]
std::vector mybar(const std::vector& x, double 
firstelem) {

   std::vector tmp(x.size() + 1);
   tmp[0] = firstelem;
   for (int i = 1; i < (x.size()+1); i++)
 tmp[i] = x[i-1];
   return tmp;
}
// [[Rcpp::export]]
std::vector mybar2(const std::vector& x, double 
firstelem) {

   std::vector tmp(x.size() + 1);
   tmp[0] = firstelem;
   std::copy(x.begin(), x.end(), tmp.begin()+1);
   return tmp;
}

/*** R
library(microbenchmark)
n=10
testvec = c(1,seq_len(n))
testelem <- 7
microbenchmark(c(testelem, testvec), mybar(testvec,testelem), 
mybar2(testvec,testelem))

*/

# Ouput
Unit: microseconds
   expr min   lq  mean   
median    uq
   c(testelem, testvec) 247.098 248.5655  444.8657 257.3300  
630.7725
   mybar(testvec, testelem) 594.978 622.3560 1226.5683 637.0230 
1386.8385
  mybar2(testvec, testelem) 576.191 604.7565 1029.2124 616.1055 
1351.6740

    max neval
   7587.977   100
  22149.605   100
  11651.831   100


Best,
Serguei.

I looked on the net and found some material from back in ~2014 about 
concatenating
vectors but I didn't see anything final about it. Thanks for any 
insights.


Also, the documentation for Rcpp is beyond incredible (thanks to 
dirk, romain, kevin and all the other people I'm leaving out )  but 
is there a general methodology for finding equivalents of R 
functions. For example, if I want a cumsum function in Rcpp, how do 
I know whether to use the stl with accumulate or if there's already 
one built in so

that I just call cumsum.

Thanks.

#===

#include 
using namespace Rcpp;

// [

Re: [Rcpp-devel] trying to insert a number as first element of already existing vector

2018-12-10 Thread Serguei Sokol

Le 09/12/2018 à 09:35, Mark Leeds a écrit :
Hi All: I wrote below and it works but I have a strong feeling there's a 
better way to do it.
If performance is an issue, you can save few percents of cpu time by 
using std::copy() instead of explicit for loop. Yet, for this operation 
R's c() remains the best bet. It is more then twice faster than both 
Rcpp versions below:


#include 
using namespace Rcpp;

// [[Rcpp::export]]
std::vector mybar(const std::vector& x, double firstelem) {
  std::vector tmp(x.size() + 1);
  tmp[0] = firstelem;
  for (int i = 1; i < (x.size()+1); i++)
tmp[i] = x[i-1];
  return tmp;
}
// [[Rcpp::export]]
std::vector mybar2(const std::vector& x, double firstelem) {
  std::vector tmp(x.size() + 1);
  tmp[0] = firstelem;
  std::copy(x.begin(), x.end(), tmp.begin()+1);
  return tmp;
}

/*** R
library(microbenchmark)
n=10
testvec = c(1,seq_len(n))
testelem <- 7
microbenchmark(c(testelem, testvec), mybar(testvec,testelem), 
mybar2(testvec,testelem))

*/

# Ouput
Unit: microseconds
  expr min   lq  mean   medianuq
  c(testelem, testvec) 247.098 248.5655  444.8657 257.3300  630.7725
  mybar(testvec, testelem) 594.978 622.3560 1226.5683 637.0230 1386.8385
 mybar2(testvec, testelem) 576.191 604.7565 1029.2124 616.1055 1351.6740
   max neval
  7587.977   100
 22149.605   100
 11651.831   100


Best,
Serguei.

I looked on the net and found some material from 
back in ~2014 about concatenating

vectors but I didn't see anything final about it. Thanks for any insights.

Also, the documentation for Rcpp is beyond incredible (thanks to dirk, 
romain, kevin and all the other people I'm leaving out )  but is there a 
general methodology for finding equivalents of R functions. For example, 
if I want a cumsum function in Rcpp, how do I know whether to use the 
stl with accumulate or if there's already one built in so

that I just call cumsum.

Thanks.

#===

#include 
using namespace Rcpp;

// [[Rcpp::export]]
std::vector mybar(const std::vector& x, double firstelem) {
   std::vector tmp(x.size() + 1);
   tmp[0] = firstelem;
   for (int i = 1; i < (x.size()+1); i++)
     tmp[i] = x[i-1];
   return tmp;
}

/*** R

testvec = c(1,2,3)
testelem <- 7
mybar(testvec,testelem)

*/

#===
# OUTPUT FROM RUNNING ABOVE
#=
 > testvec <-  c(1,2,3)
 > testelem <- 7
 > mybar(testvec,testelem)
[1] 7 1 2 3
 >







___
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

Re: [Rcpp-devel] Debug Rcpp datatypes with lldb

2018-12-06 Thread Serguei Sokol

Le 06/12/2018 à 15:34, Dirk Eddelbuettel a écrit :


On 6 December 2018 at 13:29, Huelsmann, Till wrote:
| Hello,
|
| First of all, I really like Rccp and would like to thank you all for your 
work.

s/Rccp/Rcpp/

Rccp = Russian for USSR

Did you mean CCCP? ;)

Serguei.
___
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] Speed of RCppEigen Cholesky decomposition on sparse matrix (Serguei Sokol)

2018-11-28 Thread Serguei Sokol

Le 27/11/2018 à 17:57, Dmitriy Selivanov a écrit :
But adding 0 to a sparse matrix is expensive operation. It doesn't look 
fair to include it to benchmark.
It is true that adding 0 comes at some cost. But qualifying it expensive 
or not is kind of subjective opinion. Let see how much does it cost:


system.time(replicate(10, C+0.))
#utilisateur système  écoulé
#  0.017   0.030   0.047

So, in my opinion 0.017 s is negligible time laps compared to 5 or even 
3 s needed for 10 matrix decompositions. And someone else could say 
"it's important".


Anyhow, the goal was to show that Matrix::chol does only 1 decomposition 
not 10) while Eigen did all 10 decompositions, and not to thoroughly 
compare performances of two methods. The bias detected in the original 
test was much higher than the bias induced by adding 0.


Moreover, if adding 0 would be really a problem, one could easily 
exclude it from time accounting:

sum(sapply(1:10, function(i) {C=C+0.; system.time(chol(C))[1]}))
#[1] 4.488
sum(sapply(1:10, function(i) {C=C+0.; system.time(CholSparse(C))[1]}))
#[1] 3.341

and once more (to have an idea about time variability)

sum(sapply(1:10, function(i) {C=C+0.; system.time(CholSparse(C))[1]}))
#[1] 3.481

The conclusion remains the same. We see also that time variability is 
almost 10 fold higher than time needed to add 0. So I conclude that 
adding 0 did not perturbed so much the fairness of the test.


Best,
Serguei.



вт, 27 нояб. 2018 г., 20:07 
rcpp-devel-requ...@lists.r-forge.r-project.org 
<mailto:rcpp-devel-requ...@lists.r-forge.r-project.org>:


Send Rcpp-devel mailing list submissions to
rcpp-devel@lists.r-forge.r-project.org
<mailto: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
<mailto: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
<mailto: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: Speed of RCppEigen Cholesky decomposition on sparse
       matrix (Serguei Sokol)
    2. Problems with Rcout (Barth Riley)
    3. Re: Problems with Rcout (I?aki Ucar)
    4. Re: Problems with Rcout (Serguei Sokol)
    5. Re: Problems with Rcout (Barth Riley)


--

Message: 1
Date: Tue, 27 Nov 2018 15:33:55 +0100
From: Serguei Sokol mailto:serguei.so...@gmail.com>>
To: "Hoffman, Gabriel" mailto:gabriel.hoff...@mssm.edu>>,
         "rcpp-devel@lists.r-forge.r-project.org
<mailto:rcpp-devel@lists.r-forge.r-project.org>"
         mailto:rcpp-devel@lists.r-forge.r-project.org>>
Subject: Re: [Rcpp-devel] Speed of RCppEigen Cholesky decomposition on
         sparse matrix
Message-ID: <91e163f1-c169-217f-739b-2a379c63c...@gmail.com
<mailto:91e163f1-c169-217f-739b-2a379c63c...@gmail.com>>
Content-Type: text/plain; charset=utf-8; format=flowed

Le 26/11/2018 ? 18:23, Hoffman, Gabriel a ?crit?:
 > I am developing a statistical model and I have a prototype
working in R
 > code.??I make extensive use of sparse matrices, so the R code is
pretty
 > fast, but hoped that using RCppEigen to evaluate the log-likelihood
 > function could avoid a lot of memory copying and be substantially
 > faster.??However, in a simple??example I am seeing that RCppEigen is
 > 3-5x slower than standard R code for cholesky decomposition of a
sparse
 > matrix.??This is the case on R 3.5.1 using RcppEigen_0.3.3.4.0 on
both
 > OS X and CentOS 6.9.
 >
 > Since this simple operation is so much slower it doesn't seem like
 > using RCppEigen is worth it in this case.??Is this an issue with
BLAS,
 > some libraries or compiler options, or is R code really the fastest
 > option?
After few checks, it seems to be a test issue. Matrix package stores
the
decomposition somewhere in attributes of the submitted matrix. So the
the repetitive calls requiring chol() decomposition are not really
doing
the job. Instead, previously stored result is reused.

You can easily convince yourself by "modifying" the matrix C (and thus
invalidating previous decomposition) by doing something like "C + 0." :

system.time(replicate(10, chol( C )))
#utilisateur     syst?me      ?coul?
#      0.459       0.011       0.471
system.

Re: [Rcpp-devel] Problems with Rcout

2018-11-27 Thread Serguei Sokol

Le 27/11/2018 à 16:35, Barth Riley a écrit :

Dear Rcppers

I am encountering a problem with Rcout. Even with basic string output, 
when I run the function containing the call to Rcout, no output is 
generated. Here is a simple example of what I’m trying to do:


// [[Rcpp::export]]

void testFunc() {

    Rcpp::Rcout << "testFunc begins" << std:endl;

    . . .

}


This example works for me:

library(Rcpp)
sourceCpp(code="#include \n// [[Rcpp::export]]\nvoid testFunc() 
{\nRcpp::Rcout << \"testFunc begins\" << std::endl;\n}")

testFunc()
#testFunc begins

May be in your session you have redirected stdout elsewhere?

Serguei.

My code is part of a package I’m developing, using R 3.51 and Rcpp 
0.12.19. The Rcpp code compiles without a problem.


Thanks

Barth



___
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

Re: [Rcpp-devel] Speed of RCppEigen Cholesky decomposition on sparse matrix

2018-11-27 Thread Serguei Sokol

Le 26/11/2018 à 18:23, Hoffman, Gabriel a écrit :

I am developing a statistical model and I have a prototype working in R
code.  I make extensive use of sparse matrices, so the R code is pretty
fast, but hoped that using RCppEigen to evaluate the log-likelihood
function could avoid a lot of memory copying and be substantially
faster.  However, in a simple  example I am seeing that RCppEigen is
3-5x slower than standard R code for cholesky decomposition of a sparse
matrix.  This is the case on R 3.5.1 using RcppEigen_0.3.3.4.0 on both
OS X and CentOS 6.9.

Since this simple operation is so much slower it doesn't seem like
using RCppEigen is worth it in this case.  Is this an issue with BLAS,
some libraries or compiler options, or is R code really the fastest
option?
After few checks, it seems to be a test issue. Matrix package stores the 
decomposition somewhere in attributes of the submitted matrix. So the 
the repetitive calls requiring chol() decomposition are not really doing 
the job. Instead, previously stored result is reused.


You can easily convince yourself by "modifying" the matrix C (and thus 
invalidating previous decomposition) by doing something like "C + 0." :


system.time(replicate(10, chol( C )))
#utilisateur système  écoulé
#  0.459   0.011   0.471
system.time(replicate(10, chol( C+0. )))
#utilisateur système  écoulé
#  5.365   0.060   5.425
system.time(replicate(10, CholSparse( C+0. )))
#utilisateur système  écoulé
#  3.627   0.035   3.665

On my machine, I have almost identical timing for CholSparse() with or 
without C modification:


system.time(replicate(10, CholSparse( C )))
#utilisateur système  écoulé
#  3.283   0.004   3.289
which proves that Eigen doesn't store the decomposition for future reuse 
and redo the decomposition at each call on the same matrix.


Best,
Serguei.




library(Matrix)
library(inline)

# construct sparse matrix
#

# construct a matrix C that is N x N with S total entries
# set C = crossprod(X)
N = 10
S = 100
i = sample(1:1000, S, replace=TRUE)
j = sample(1:1000, S, replace=TRUE)
values = runif(S, 0, .3)
X = sparseMatrix(i=i, j=j, x = values, symmetric=FALSE )

C = as(crossprod(X), "dgCMatrix")

# check sparsity fraction
S / N^2

# define RCppEigen code
CholeskyCppSparse<-'
using Rcpp::as;
using Eigen::Map;
using Eigen::SparseMatrix;
using Eigen::MappedSparseMatrix;
using Eigen::SimplicialLLT;

// get data into RcppEigen
const MappedSparseMatrix Sigma(as 
 >(Sigma_in));


// compute Cholesky
typedef SimplicialLLT > SpChol;
const SpChol Ch(Sigma);
'

CholSparse <- cxxfunction(signature(Sigma_in = "dgCMatrix"), 
CholeskyCppSparse, plugin = "RcppEigen")


# compare times
system.time(replicate(10, chol( C )))
# output:
#   user  system elapsed
#  0.341   0.014   0.355

system.time(replicate(10, CholSparse( C )))
# output:
#   user  system elapsed
# 1.639   0.046   1.687

sessionInfo()

R version 3.5.1 (2018-07-02)
Platform: x86_64-apple-darwin15.6.0 (64-bit)
Running under: macOS  10.14

Matrix products: default
BLAS:
/Library/Frameworks/R.framework/Versions/3.5/Resources/lib/libRblas.0.dylib
LAPACK:
/Library/Frameworks/R.framework/Versions/3.5/Resources/lib/libRlapack.dylib

locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

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

other attached packages:
[1] inline_0.3.15 Matrix_1.2-15

loaded via a namespace (and not attached):
[1] compiler_3.5.1  RcppEigen_0.3.3.4.0 Rcpp_1.0.0
[4] grid_3.5.1  lattice_0.20-38

Changing the size of the matrix and the number of entries does not
change the relative times much

Thanks,
- Gabriel


___
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

Re: [Rcpp-devel] coercion NULL to vector

2018-04-18 Thread Serguei Sokol

Le 18/04/2018 à 17:31, Jeff Newmiller a écrit :

as.numeric( NULL ) is not silent, it is very explicit.
Well, written in this way yes. But I was referring more to the content 
than to the form. If you write as.numeric(x) and at some run-time point 
x become NULL, the code won't stop with an error. It will silently 
coerce NULL to numeric(0). Analogously in what I propose, if you write 
f(vec x) and at some call x is NULL, f would continue to work as if 
called with f(numeric(0)).




Re this not being a C++ NULL, I don't object to R NULL being used that way (in 
fact it may be more sensible to do this than not, but it is not clear that R 
core thinks this way).

No sure to get your point here but never mind.

Serguei.
___
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] coercion NULL to vector

2018-04-18 Thread Serguei Sokol

Le 18/04/2018 à 16:15, Jeff Newmiller a écrit :

Regarding:

"Actually, when NULL is provided to where a vector or a matrix is expected just an 
error is thrown. So we can hardly imagine that someone would have his program broken or 
will be misled if such coercion is introduced."

If all programs were perfect this would be true, but the ability to identify 
incorrect pointer initialization is an important feature for testing.

In the late '90s, some Internet Service Providers tried to "assist" users who typed bad 
site names into browsers by making failed DNS lookups redirect to a server prepared to supply an 
informative "were you looking for X?" web page. This caused significant disruption in 
non-user-interactive network protocols that relied on getting a failure notice for bad DNS lookups, 
and that idea had to go to the trash heap eventually.

Interesting example but not really "parallel" to our situation.
In R, we already have as.numeric(NULL) without such kind of catastrophe.
I just propose an equivalent functioning in Rcpp.



I don't have failed code tests to offer for this, but the idea of silently 
replacing a C++ NULL with a valid value is anathema.

It's not C++ NULL which is coerced (not "replaced") to a vector but R's one. 
And only where a vector of a given type is expected, not everywhere.

I think there is a possible compromise.
We can introduce an automatic coercion in #ifdef'ed blocks with e.g. 
RCPP_COERCE_NULL_TO_VECTOR and RCPP_COERCE_VECTOR_TO_MARIX optional macros.
In such a way, those who want this feature can have it by defining these macros before including Rcpp.h and those who consider it as anathema can go a 
traditional way of doing.


Serguei.
___
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] coercion NULL to vector

2018-04-18 Thread Serguei Sokol

Le 18/04/2018 à 15:59, Dirk Eddelbuettel a écrit :


Could you use XPtr ?

Probably yes, as I could use Nullable<> too. But at what price?
For me, the code would become far less readable.

Serguei.
___
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] coercion NULL to vector

2018-04-18 Thread Serguei Sokol

Le 18/04/2018 à 13:32, Dirk Eddelbuettel a écrit :


On 18 April 2018 at 10:26, Georgi Boshnakov wrote:
| I may be missing something here but
| it doesn't seem right to introduce silent coercion of NULL to vectors, etc.,
| especially if it would become imposed on everybody using Rcpp/Armadillo.
| Even the "convenience" of this  is questionable.

Agreed. I am also not yet convinced by Serguei's argument.  It seems a little
invasive for an unclear (to me) use case.

I can describe my own case which made me search for a such solution.
I have an Rcpp/Armadillo function accepting in an optional argument a vector of 
uvec type.
It can be called from R and I can call it from other C++ functions of the same 
package too.
Nullable mechanism was cumbersome but sufficient (note that there is 
no possible Nullable)
when only R calls were planned. But when it comes to calls from C++ I'll have 
to juggle with wrap() too.
Both of Nullable (as well as special code for dealing with NULL case) and 
wrap()
become unnecessary with a neat declaration f(..., uvec v, ...). So that
an R call with 'c()' can pass as well as C++ calls with an empty uvec without
any additional hassle for treating NULL case.

Serguei.
___
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] coercion NULL to vector

2018-04-18 Thread Serguei Sokol

Le 18/04/2018 à 12:26, Georgi Boshnakov a écrit :

I may be missing something here but
it doesn't seem right to introduce silent coercion of NULL to vectors, etc.,
especially if it would become imposed on everybody using Rcpp/Armadillo.

Nothing is imposed. Everyone will be free to use Nullable mechanism as 
before.
Actually, when NULL is provided to where a vector or a matrix is expected
just an error is thrown. So we can hardly imagine that someone would have
his program broken or will be misled if such coercion is introduced.


Even the "convenience" of this  is questionable.

Right. It's a matter of taste. But with a quick glance on SO
I have seen a discussion here
https://stackoverflow.com/questions/34718570/rcpp-pass-vector-of-length-0-null-to-cppfunction
and on this list here (and follow-ups)
http://lists.r-forge.r-project.org/pipermail/rcpp-devel/2014-February/007215.html
where some people are surprised not having such coercion and even
expressed a wish like "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."
It's also my thought but I am ready to hear the arguments of those who
think otherwise.

Serguei.



Georgi Boshnakov


-Original Message-
From: rcpp-devel-boun...@lists.r-forge.r-project.org 
[mailto:rcpp-devel-boun...@lists.r-forge.r-project.org] On Behalf Of Serguei 
Sokol
Sent: 18 April 2018 10:57
To: Dirk Eddelbuettel
Cc: Rcpp-devel
Subject: Re: [Rcpp-devel] coercion NULL to vector

Le 17/04/2018 à 17:53, Dirk Eddelbuettel a écrit :


On 17 April 2018 at 10:31, Dirk Eddelbuettel wrote:
|
| On 17 April 2018 at 15:09, Serguei Sokol wrote:

...

| | It will automatically coerce NULL to a declared vector type.
| | It's a more primitive solution than Nullable but in many cases
| | it can be largely sufficient and make code look shorter and clearer.
| |
| | Will it break something in established usage habits? Is it compatible with 
your plans for Rcpp?
| | I can prepare a PR if you decide to include it.
|
| We could test that.  It may not do harm -- so I just turned on a
| rev.dep check for it.
|
| Can you open an issue for it on GH though?  Better visibility and
| easier 'per concrete topic' discussion.

I was just about to do so when I saw your post here. So let decide if the 
semantic is useful or not and if yes, we'll switch to GH. OK?



I am not convinced that the semantics are useful.  Your example works
on the arma type (where we our very old design issue of always returning a 
matrix):

What is returned: a matrix or a vector is not important here. By the way, it is 
already resolved by an optional RCPP_ARMADILLO_RETURN_COLVEC_AS_VECTOR macro, 
remember?
The point here is how the /input/ NULL is interpreted. Is it coerced to smth or 
just an error is thrown?




sourceCpp("/tmp/serguei.cpp")
f(NULL)

   [,1]

f(integer(0))

   [,1]

f()

Error in f() : argument "x" is missing, with no default

Normal. If you want to make the parameter optional, you have to declare vec 
f(Rcpp::NumericVector x=R_NilValue) {return x+1;}  > f()
   [,1]
It does not work yet with 'vec' type but it could with an appropriate patch.
We are just discussing the usefulness of semantics not yet the patch.


class(f(NULL))

[1] "matrix"

(not a big deal what is returned as discussed above).





But if I do the same with Rcpp types, say a matrix via

// [[Rcpp::export]]
Rcpp::NumericMatrix g(Rcpp::NumericMatrix x) { return x+1; }

then I get more restrictive behaviour (as NumericMatrix tests for
matrix)

Right. I think that the next logical step would be to allow an automatic coercion 
of atomic vectors to matrices too. As in R  > as.matrix(integer(0))
   [,1]
we got a matrix of dims (0, 1) we could make the same available in Rcpp.
More generally, a vector x could be coerced into a matrix of dims (length(x), 
1) (once again just as in R as.matrix())




sourceCpp("/tmp/serguei.cpp")
g(NULL)

Error in g(NULL) : Not a matrix.

g(matrix())

   [,1]
[1,]   NA

g(vector())

Error in g(vector()) : Not a matrix.

g(integer())

Error in g(integer()) : Not a matrix.
   



Is this really useful, and can you not use Nullable<> instead?

Sure I can, but as I already said, my thought is to make the code looking simpler and 
clearer in this kind of situations where automatic coercion does sufficient job. 
Nullable is much more flexible than that but more expensive in coding on 
user's side.

Serguei.
___
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

Re: [Rcpp-devel] coercion NULL to vector

2018-04-18 Thread Serguei Sokol

Le 17/04/2018 à 17:53, Dirk Eddelbuettel a écrit :


On 17 April 2018 at 10:31, Dirk Eddelbuettel wrote:
|
| On 17 April 2018 at 15:09, Serguei Sokol wrote:

...

| | It will automatically coerce NULL to a declared vector type.
| | It's a more primitive solution than Nullable but in many cases
| | it can be largely sufficient and make code look shorter and clearer.
| |
| | Will it break something in established usage habits? Is it compatible with 
your plans for Rcpp?
| | I can prepare a PR if you decide to include it.
|
| We could test that.  It may not do harm -- so I just turned on a rev.dep
| check for it.
|
| Can you open an issue for it on GH though?  Better visibility and easier 'per
| concrete topic' discussion.

I was just about to do so when I saw your post here. So let decide if
the semantic is useful or not and if yes, we'll switch to GH. OK?



I am not convinced that the semantics are useful.  Your example works on the
arma type (where we our very old design issue of always returning a matrix):

What is returned: a matrix or a vector is not important here. By the way, it is
already resolved by an optional RCPP_ARMADILLO_RETURN_COLVEC_AS_VECTOR macro, 
remember?
The point here is how the /input/ NULL is interpreted. Is it coerced to smth or
just an error is thrown?




sourceCpp("/tmp/serguei.cpp")
f(NULL)

  [,1]

f(integer(0))

  [,1]

f()

Error in f() : argument "x" is missing, with no default

Normal. If you want to make the parameter optional, you have to declare
vec f(Rcpp::NumericVector x=R_NilValue) {return x+1;}
> f()
 [,1]
It does not work yet with 'vec' type but it could with an appropriate patch.
We are just discussing the usefulness of semantics not yet the patch.


class(f(NULL))

[1] "matrix"

(not a big deal what is returned as discussed above).





But if I do the same with Rcpp types, say a matrix via

// [[Rcpp::export]]
Rcpp::NumericMatrix g(Rcpp::NumericMatrix x) { return x+1; }

then I get more restrictive behaviour (as NumericMatrix tests for matrix)

Right. I think that the next logical step would be to allow an automatic
coercion of atomic vectors to matrices too. As in R
> as.matrix(integer(0))
 [,1]
we got a matrix of dims (0, 1) we could make the same available in Rcpp.
More generally, a vector x could be coerced into a matrix of dims (length(x), 1)
(once again just as in R as.matrix())




sourceCpp("/tmp/serguei.cpp")
g(NULL)

Error in g(NULL) : Not a matrix.

g(matrix())

  [,1]
[1,]   NA

g(vector())

Error in g(vector()) : Not a matrix.

g(integer())

Error in g(integer()) : Not a matrix.
  



Is this really useful, and can you not use Nullable<> instead?

Sure I can, but as I already said, my thought is to make the code
looking simpler and clearer in this kind of situations where automatic
coercion does sufficient job. Nullable is much more flexible than that
but more expensive in coding on user's side.

Serguei.
___
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] coercion NULL to vector

2018-04-17 Thread Serguei Sokol

Hi,

I would like to re-discuss the subject of automatic coercion of
NULL to some vector in function parameter list. This old topic was
already raised e.g. here
https://stackoverflow.com/questions/34718570/rcpp-pass-vector-of-length-0-null-to-cppfunction

To resume, actually a function defined as (ivec is from RcppArmadillo package)

sourceCpp(code="
    // [[Rcpp::depends(RcppArmadillo)]]
    #include 
    using namespace arma;
    // [[Rcpp::export]]
    ivec f(ivec x) {return x+1;}
")

and called as f(c()) will produce an error:
Error in f(c()) :
  Not compatible with requested type: [type=NULL; target=integer].

What I propose is (for this example) to mimic a call f(integer(0)) which gives:
> f(integer(0))
 [,1]

The following one-line patch would do the job:
diff --git a/inst/include/Rcpp/r_cast.h b/inst/include/Rcpp/r_cast.h
index e59fa799..c0ec0817 100644
--- a/inst/include/Rcpp/r_cast.h
+++ b/inst/include/Rcpp/r_cast.h
@@ -63,6 +63,7 @@ namespace Rcpp {
 case LGLSXP:
 case CPLXSXP:
 case INTSXP:
+    case NILSXP:
 return Rf_coerceVector(x, RTYPE);
 default:
 const char* fmt = "Not compatible with requested type: "

It will automatically coerce NULL to a declared vector type.
It's a more primitive solution than Nullable but in many cases
it can be largely sufficient and make code look shorter and clearer.

Will it break something in established usage habits? Is it compatible with your 
plans for Rcpp?
I can prepare a PR if you decide to include it.

Best,
Serguei.

___
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] how to reference a row of a matrix in C++ in Rcpp

2018-03-21 Thread Serguei Sokol

Dear Akshay,

In previous messages, Dirk and I told you that this list was not
the right place for beginner questions. Please stop asking that kind
of questions here.

Best,
Serguei.

Le 21/03/2018 à 09:40, akshay kulkarni a écrit :

dear serguei,

                          I have another question:

If M is a matrix, M.nrow() should return the number of rows...but I encountered 
the following inconsistency:


 > M
      x  y  z
[1,] 1  0  0
[2,] 4  3  0
[3,] 3  1  1
[4,] 2 90 87
[5,] 9 76 23
[6,] 5 23 13
 > cppFunction('IntegerVector tccp5(IntegerMatrix M) { int x = M.nrow(); return 
x;}')
 > tccp5(M)
[1] 0 0 0 0 0 0

can you please explain what is happening( I am very new to Rcpp and C++)?

very many thanks for your time and effort...
yours sincerely,
AKSHAY M KULKARNI




*From:* Serguei Sokol <serguei.so...@gmail.com>
*Sent:* Tuesday, March 20, 2018 7:26 PM
*To:* akshay kulkarni; Rcpp R
*Subject:* Re: [Rcpp-devel] how to reference a row of a matrix in C++ in Rcpp
Le 20/03/2018 à 12:10, akshay kulkarni a écrit :

dear members,

                               I came to know from stackoverflow that the 
following references a row in a matrix in C++:

M[2] references 2nd row of the Matrix.

SO is too big to check this assertion by ourself. Do you have a link?




I am using Rcpp to write C++ code in R.


However, I ended up with the following inconsistency:

  > M
       x y z
[1,] 1 1 1
[2,] 2 2 2
[3,] 3 3 3
[4,] 4 4 4
[5,] 5 5 5
[6,] 6 6 6
  > cppFunction('IntegerVector tccp3(IntegerMatrix M) { IntegerVector x = M[2]; 
return x;}')

Try

cppFunction('IntegerVector tccp3(IntegerMatrix M) { IntegerVector x = M(2,_); 
return x;}')

Best,
Serguei.



  > tccp3(M)
[1] 0 0 0
  > cppFunction('IntegerVector tccp4(IntegerMatrix M) { IntegerVector x = M[1]; 
return x;}')
  > tccp4(M)
[1] 0 0

tccp3 should return (3,3,3) and tccp4 should return  (2,2,2). Can you please 
shed light on what is going on?

very many thanks for your time and effort

Yours sincerely,
AKSHAY M KULKARNI



___
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 Info Page 
<https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel>
lists.r-forge.r-project.org
Discussion list for Rcpp, RInside and various packages using Rcpp or RInside. This list is subscriber-only, in other words in order to post to the list you must 
be ...










___
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] how to reference a row of a matrix in C++ in Rcpp

2018-03-20 Thread Serguei Sokol

Le 20/03/2018 à 17:22, Dirk Eddelbuettel a écrit :


Akshay,

On 20 March 2018 at 15:54, akshay kulkarni wrote:
| Can I also write this:
|
| M[1:i, n:m] as we write in R?

Please consider to stop posting elementary C++ beginners question to the
list.  We do not provide as a C++ tutorial or training service here.  Please
learn at least /some/ C++ after which a re-reading of the Rcpp documentation
will make more sense to you.

+1

However, to reach a point I can say that certainly RcppEigen will allow you to 
do this
with block operations you have already mentioned. But also RcppArmadillo is 
another
alternative well suited for this kind of basic operations.
You'll have some homework on your side ;)

Best,
Serguei.



Thanks, Dirk



___
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] how to reference a row of a matrix in C++ in Rcpp

2018-03-20 Thread Serguei Sokol

Le 20/03/2018 à 12:10, akshay kulkarni a écrit :

dear members,

                              I came to know from stackoverflow that the 
following references a row in a matrix in C++:

M[2] references 2nd row of the Matrix.

SO is too big to check this assertion by ourself. Do you have a link?




I am using Rcpp to write C++ code in R.


However, I ended up with the following inconsistency:

 > M
      x y z
[1,] 1 1 1
[2,] 2 2 2
[3,] 3 3 3
[4,] 4 4 4
[5,] 5 5 5
[6,] 6 6 6
 > cppFunction('IntegerVector tccp3(IntegerMatrix M) { IntegerVector x = M[2]; 
return x;}')

Try

cppFunction('IntegerVector tccp3(IntegerMatrix M) { IntegerVector x = M(2,_); 
return x;}')

Best,
Serguei.



 > tccp3(M)
[1] 0 0 0
 > cppFunction('IntegerVector tccp4(IntegerMatrix M) { IntegerVector x = M[1]; 
return x;}')
 > tccp4(M)
[1] 0 0

tccp3 should return (3,3,3) and tccp4 should return  (2,2,2). Can you please 
shed light on what is going on?

very many thanks for your time and effort

Yours sincerely,
AKSHAY M KULKARNI



___
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

Re: [Rcpp-devel] help needed on RcppEigen

2018-03-19 Thread Serguei Sokol

Le 19/03/2018 à 17:21, akshay kulkarni a écrit :

dear sir,

                I am using R to model stock prices but a slow "for" loop has 
stifled my research.


I am using Rcpp to speed up my "for" loop. I am also using RcppEigen to 
implement block operations on xts objects.


how do I use RcppEigen? If I just load it by:

 > require(RcppEigen)

it is not recognising the .block method in C++.

If I include it inside the cppFunction:

 > cppFunction(' include    .body')

I get the following error:

 > error: "include doesn't name a type"


My question is : how do I manipulate the installed RcppEigen package for the cppFunction to recognize block operations that are part of the Eigen library in 
C++? If I have to use the "include" key word (something like this: include  ), where exactly should i position it in order to work with cppFunction 
( I want to use Eigen block operation methods or functions inside the body of the cppFunction)?

It looks like you have missed some starter texts.
Take a look at
https://cran.r-project.org/web/packages/Rcpp/vignettes/Rcpp-introduction.pdf
and http://gallery.rcpp.org/articles/eigen-eigenvalues/
Note that in all examples there is "#" before "include", it's not optional.
May be some introduction texts to C++ would be useful too.

Best,
Serguei.




very many thanks for your time and effort.


yours sincerely,

AKSHAY M KULKARNI




___
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

Re: [Rcpp-devel] Modules + Attributes for extensions

2018-03-07 Thread Serguei Sokol

Le 07/03/2018 à 15:59, fernandohtol...@gmail.com a écrit :

Dear Ralf and others,

Thanks for your reply. I did looked for the references you pointed
before. Unfortunately, there is nothing new in the Extending vignette
than what can be found in Dirk's book chapter. Basically, all the
others implement custom as/wrap for third part classes to SEXP
fundamental types.

Perhaps, I didn't state the question clearly... : Given my own class
exposed through Modules, how to retrieve it from the C++ side?

(Supposing I understood well your question...)

For this part of problem, you have to declare your classes in your
original package (say mypack) in a separate file say 
mypack/inst/include/mypack.h
Then in a package which will extend it (say otherpack) you will
delcare in its DESCRIPTION
LinkingTo: mypack

It will allow you to put in your otherpack/src/*.cpp files
#include  So you'll be able to use/extend mypack classes.



How to
allow perspective users to extend a package taking exposed class and
defining new Rccp functions that uses those exposed class as argument
or return?

As to taking objects instantiated from your classes as arguments in R session,
it's indeed a matter of custom as()/wrap() custom definition.
Or you can use XPtr hook to passe just pointers to your objects
and de-referencing them appropriately in the Rcpp code.

Best,
Serguei.



My main difficulty is that! I will appreciate your points over that?

Cheers,
FH

-Original Message-
From: Ralf Stubner 
To: rcpp-devel@lists.r-forge.r-project.org
Subject: Re: [Rcpp-devel] Modules + Attributes for extensions
Date: Tue, 6 Mar 2018 14:41:32 +0100

On 06.03.2018 00:08, fernandohtol...@gmail.com wrote:

I
will also appreciate if someone point me an R package in which custom
as/wrap are defined!


The reference: http://dirk.eddelbuettel.com/code/rcpp/Rcpp-extending.pd
f
Also very useful:
http://gallery.rcpp.org/articles/custom-templated-wrap-and-as-for-seami
ngless-interfaces/

Simple example: RcppBDT
More complex examples: RcppArmadillo, RcppEigen, RcppGSL

Greetings
Ralf

___
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 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] Setting seed using Rcpp

2018-02-07 Thread Serguei Sokol

Le 07/02/2018 à 16:20, Hmamouche Youssef a écrit :

Hi,

I get a note from R CMD check for an Rcpp package:

Found ‘_srand’, possibly from ‘srand’

Found no calls to: ‘R_registerRoutines’, ‘R_useDynamicSymbols’



Is there some an  equivalent function for srand from Rcpp, which i can use it 
inside c++ code?

If you try "rcpp random number generation" in a search engine,
the first line will point to

http://gallery.rcpp.org/articles/random-number-generation/

which probably has your answer.

Best,
Serguei.
___
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] Rcpp is the most downloaded package from CRAN

2017-10-11 Thread Serguei Sokol

Hi,

Just to share this survey that claims something that your always knew
but probably never had statistics to prove it ;) -- Rcpp is the most downloaded 
package from CRAN!
http://redmonk.com/rstephens/2017/10/10/r-package-downloads/

Enjoy,
Serguei.
___
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] return Colvec as vector

2017-07-28 Thread Serguei Sokol

Le 28/07/2017 à 13:45, Dirk Eddelbuettel a écrit :


On 27 July 2017 at 18:37, Serguei Sokol wrote:
| I can hear from here "Oh no, not again this question!" ;)

;-)

| But this time it is not a question it is a feature suggestion.
| What if we replace in RcppArmadilloWrap.h:64:
|  template  SEXP wrap( const arma::Col& data ){
|  return RcppArmadillo::arma_wrap( data, Dimension( data.n_elem, 1) ) ;
|  }
| by something like:
|  template  SEXP wrap( const arma::Col& data ){
| #ifdef RETURN_COLVEC_AS_VECTOR
|  return RcppArmadillo::arma_wrap( data ) ;
| #else
|  return RcppArmadillo::arma_wrap( data, Dimension( data.n_elem, 1) ) ;
| #endif
|  }
|
| In this way, a legacy code relying on one column matrix is not impacted,
| while other users can define a macro RETURN_COLVEC_AS_VECTOR
| before "#include " and enjoy a more intuitive behavior.
|
| Was it already proposed and rejected?

Don't think so, but I have thought about it too, and I was planning to bring
this up.  I also find the 'always forced to be dimension one matrix' to be a
(very early) design decision I would not make again.  And I have been
thinking along similar lines---we need an option to restore it / keep it as
the defaults, and a newer alternative to let this collapse to a dimless
vector.  What you rprose could work (maybe by prefixing RCPP_ARMADILLO_ to
the #define though).

Cool. Then to be complete, we can think about a full set of options, smth. like:
RCPP_ARMADILLO_RETURN_COLVEC_AS_VECTOR
RCPP_ARMADILLO_RETURN_ROWVEC_AS_VECTOR
RCPP_ARMADILLO_RETURN_ANYVEC_AS_VECTOR

Serguei.
___
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] return Colvec as vector

2017-07-27 Thread Serguei Sokol

I can hear from here "Oh no, not again this question!" ;)
But this time it is not a question it is a feature suggestion.
What if we replace in RcppArmadilloWrap.h:64:
template  SEXP wrap( const arma::Col& data ){
return RcppArmadillo::arma_wrap( data, Dimension( data.n_elem, 1) ) ;
}
by something like:
template  SEXP wrap( const arma::Col& data ){
#ifdef RETURN_COLVEC_AS_VECTOR
return RcppArmadillo::arma_wrap( data ) ;
#else
return RcppArmadillo::arma_wrap( data, Dimension( data.n_elem, 1) ) ;
#endif
}

In this way, a legacy code relying on one column matrix is not impacted,
while other users can define a macro RETURN_COLVEC_AS_VECTOR
before "#include " and enjoy a more intuitive behavior.

Was it already proposed and rejected?

Best,
Serguei.

___
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] Fwd: Calling R function in Rcpp returns compatibility issues

2017-07-27 Thread Serguei Sokol

Le 27/07/2017 à 18:12, Sharat a écrit :

Also, after your comments, it just hit me why not write the entire code in pure 
C++? Would that be worthwhile?

It really depends on the size of your real data and on time it takes to process 
them in R.
If it is a matter of just a second or so, I would leave it in R.
Otherwise, C++ can be considered. But you should also to take into
account your skills in C++ programming. Programming time should be
(largely) covered by the CPU time economy made with faster program.

Serguei.
___
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] Fwd: Calling R function in Rcpp returns compatibility issues

2017-07-27 Thread Serguei Sokol

Hi Sharat,

First, few preliminary remarks before answering your question.
1. Just like every one asking for assistance, your should provide a minimal 
reproducible example.
2. Before asking about Rcpp issue, it would be reasonable if you test your 
comps() function in R,
to be sure that the problem is not in comps() itself (and yes, there is a 
problem in comps(), cf. hereafter)
3. It is counter-intelligent to use Rcpp just for calling an R function. 
Sometimes such calling is
unavoidable but doing _only_ this is a waste of time. Simply call your function 
directly from R.
Main interest of Rcpp is to easily interface functions written in C++ (which 
are usually
much faster then their counterparts in R) with R session.

Now, about your problem.
In fact, your version of comps() returns always NULL which causes the error 
message.
It is because of the line:
   append(res,paste(prop1,"<-->", prop2))
which should be
   res=append(res,paste(prop1,"<-->", prop2))

Furthermore, when corrected and in presence of matches, comps() returns a 
string vector, not a matrix.
So your have to invert your declarations of getGoing() making him return a 
StringVector and taking
as argument a StringMatrix. In shorter writing, it gives:

// [[Rcpp::export]]
StringVector getGoing(StringMatrix vec){
   Function f = Environment::global_env()["comps"];
   return f(vec);
}

Best,
Serguei.

Le 27/07/2017 à 15:30, Sharat a écrit :

Hi:

I'm trying to call a R function (that compares strings) in Rcpp and return the result to make the comparisons run faster. Am a noob with Rcpp; so please bear. 
The input to the R function is a matrix of 2 columns.


The error is: *Error in getGoing(product) : Not compatible with STRSXP: 
[type=NULL]. *

Please assist.

#include
using namespace Rcpp;

/*** R
comps= function(vec)
{
   streets <- vec
   cnt <- nrow(streets)
   res <- c()
   print(paste0("Comparing ", (cnt)," pairs..."))
   for (i in 1:cnt)
   {
 matched <- TRUE
 prop1 = streets[i,1]
 prop2 = streets[i,2]

 prop1_parts = strsplit(trimws(prop1), ' ')
 prop2_parts = strsplit(trimws(prop2), ' ')
 prop1_parts_count = length(prop1_parts[[1]])
 prop2_parts_count = length(prop2_parts[[1]])
 if (prop1_parts_count == prop2_parts_count)
 {
   for (x in 1:prop1_parts_count)
   {
 part1 = prop1_parts[[1]][x]
 part2 = prop2_parts[[1]][x]
 if (part1 == part2) {
 matched = matched & TRUE
 }
 else if (adist(part1, part2, partial = T)==0 | adist(part2,part1, 
partial = T)==0){
 matched = matched & TRUE
 }
 else {
   matched = matched & FALSE
   break
 }
   }#forloop ends
   if(matched){
 append(res,paste(prop1,"<-->", prop2))
   }
 }#if loops ends
   }#primary for loops ends
   res
}#function ends
*/

// [[Rcpp::export]]
Rcpp::StringMatrix getGoing(Rcpp::StringVector vec){
   Rcpp::Environment env = Rcpp::Environment::global_env();
   Rcpp::Function f = env["comps"];
   Rcpp::StringMatrix result = f(vec);
   return result;
}



___
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

Re: [Rcpp-devel] [RcppArmadillo] Result of Rcpp Wrap() for Sparse Matrix

2017-06-15 Thread Serguei Sokol

Le 14/06/2017 à 16:06, Serguei Sokol a écrit :

Le 14/06/2017 à 15:21, Douglas Bates a écrit :


...
That is the format of the dgTMatrix class from the Matrix package for R but not, as far as I can tell, in Armadillo.  A brief glance at the Armadillo 
documentation indicates that sparse matrices are always in the compressed sparse column (CSC) format.

Indeed, but nothing prevents Binxiang to develop a wrap() that will convert
armadillo format to one or many of R formats, right?

I realize that I was asking too much to wrap(). After having a glance on
http://gallery.rcpp.org/articles/custom-templated-wrap-and-as-for-seamingless-interfaces/
I understand that it cannot convert a given object to more than one R format.
There is no mechanism equivalent to T as in as().
Given this, I agree that the most logical solution would be to stay with
CSC format which is common to Armadillo and Matrix package.
___
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] [RcppArmadillo] Result of Rcpp Wrap() for Sparse Matrix

2017-06-14 Thread Serguei Sokol

Le 14/06/2017 à 17:33, Douglas Bates a écrit :



On Wed, Jun 14, 2017 at 9:06 AM Serguei Sokol <serguei.so...@gmail.com 
<mailto:serguei.so...@gmail.com>> wrote:

Le 14/06/2017 à 15:21, Douglas Bates a écrit :
 >
 >
 > On Wed, Jun 14, 2017 at 3:59 AM Serguei Sokol <serguei.so...@gmail.com 
<mailto:serguei.so...@gmail.com> <mailto:serguei.so...@gmail.com
<mailto:serguei.so...@gmail.com>>> wrote:
 >
 > Le 13/06/2017 à 18:24, Douglas Bates a écrit :
 >  > On Tue, Jun 13, 2017 at 10:56 AM Binxiang Ni <binxian...@gmail.com 
<mailto:binxian...@gmail.com> <mailto:binxian...@gmail.com
<mailto:binxian...@gmail.com>> <mailto:binxian...@gmail.com 
<mailto:binxian...@gmail.com>
 > <mailto:binxian...@gmail.com <mailto:binxian...@gmail.com>>>> wrote:
 >  >
 >  > Hi,
 >  >
 >  > I am working on fixing sparse matrix conversion for 
RcppArmadillo. Now a problem comes up to me: what kind of sparse matrix is expected to
pass from
 >  > Armadillo to R? That is, what should the result of wrap() be? 
dgCMatrix(if logical, lgCMatrix or ngCMatrix)  or their original type?
 >  >
 >  >
 >  > What do you mean by "their original type"?
 >  >
 >  > It seems that the correspondence is
 >  > Armadillo   Matrix package
 >  > sp_mat   <=> dgCMatrix
 >  > sp_cx_mat <=> zgCMatrix
 >  > sp_imat  <=> igCMatrix
 > I would also consider the format used in a package slam.
 > It simply stores the indexes and non-zero values in a triplet 
(i,j,v).
 >
 >
 > That is the format of the dgTMatrix class from the Matrix package for R 
but not, as far as I can tell, in Armadillo.  A brief glance at the Armadillo
 > documentation indicates that sparse matrices are always in the 
compressed sparse column (CSC) format.
Indeed, but nothing prevents Binxiang to develop a wrap() that will convert
armadillo format to one or many of R formats, right?


Why?

Sure, Matrix is very versatile and rich in features but the price for this is 
its heavy weight.
It can take several seconds to load it up. On my rather mighty PC (Intel Xeon 
E5-2609 v2 @ 2.50GHz with 16 GB of memory),
I have:
> system.time(library(Matrix))
utilisateur système  écoulé
  1.427   0.052   1.619

I don't have my laptop here but the load time can be longer.
While for slam it takes only a fraction of second:

> system.time(library(slam))
utilisateur système  écoulé
  0.012   0.000   0.011
When slam can suffice, why not to use it?

Is there a reason for doing type conversion from the dgCMatrix format to another format in an Rcpp wrap function instead of with the existing functions 
from the Matrix package?


Bear in mind that dgCMatrix is an efficient format both in terms of the amount of memory required  (that's the "compressed" part of the name) and in terms of 
performing operations with the matrix.  Most operations on sparse matrices stored in the triplet format start by creating a CSC or CSR (compressed sparse row) 
form of the matrix anyway.

In Matrix package, I presume?
Few basic operations that I have seen in slam, stay with triplet format.
So if a user did not load Matrix package and want to use e.g. slam format,
it would be great if wrap() could give him expected format.
___
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] [RcppArmadillo] Result of Rcpp Wrap() for Sparse Matrix

2017-06-14 Thread Serguei Sokol

Le 14/06/2017 à 15:21, Douglas Bates a écrit :



On Wed, Jun 14, 2017 at 3:59 AM Serguei Sokol <serguei.so...@gmail.com 
<mailto:serguei.so...@gmail.com>> wrote:

Le 13/06/2017 à 18:24, Douglas Bates a écrit :
 > On Tue, Jun 13, 2017 at 10:56 AM Binxiang Ni <binxian...@gmail.com 
<mailto:binxian...@gmail.com> <mailto:binxian...@gmail.com
<mailto:binxian...@gmail.com>>> wrote:
 >
 > Hi,
 >
 > I am working on fixing sparse matrix conversion for RcppArmadillo. 
Now a problem comes up to me: what kind of sparse matrix is expected to pass from
 > Armadillo to R? That is, what should the result of wrap() be? 
dgCMatrix(if logical, lgCMatrix or ngCMatrix)  or their original type?
 >
 >
 > What do you mean by "their original type"?
 >
 > It seems that the correspondence is
 > Armadillo   Matrix package
 > sp_mat   <=> dgCMatrix
 > sp_cx_mat <=> zgCMatrix
 > sp_imat  <=> igCMatrix
I would also consider the format used in a package slam.
It simply stores the indexes and non-zero values in a triplet (i,j,v).


That is the format of the dgTMatrix class from the Matrix package for R but not, as far as I can tell, in Armadillo.  A brief glance at the Armadillo 
documentation indicates that sparse matrices are always in the compressed sparse column (CSC) format.

Indeed, but nothing prevents Binxiang to develop a wrap() that will convert
armadillo format to one or many of R formats, right?
___
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] [RcppArmadillo] Result of Rcpp Wrap() for Sparse Matrix

2017-06-14 Thread Serguei Sokol

Le 13/06/2017 à 18:24, Douglas Bates a écrit :

On Tue, Jun 13, 2017 at 10:56 AM Binxiang Ni > wrote:

Hi,

I am working on fixing sparse matrix conversion for RcppArmadillo. Now a 
problem comes up to me: what kind of sparse matrix is expected to pass from
Armadillo to R? That is, what should the result of wrap() be? dgCMatrix(if 
logical, lgCMatrix or ngCMatrix)  or their original type?


What do you mean by "their original type"?

It seems that the correspondence is
Armadillo   Matrix package
sp_mat   <=> dgCMatrix
sp_cx_mat <=> zgCMatrix
sp_imat  <=> igCMatrix

I would also consider the format used in a package slam.
It simply stores the indexes and non-zero values in a triplet (i,j,v).

Serguei.
___
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] Symbol registration in packages with module classes

2017-03-09 Thread Serguei Sokol

Le 08/03/2017 à 17:16, Dirk Eddelbuettel a écrit :


On 6 March 2017 at 10:11, Serguei Sokol wrote:
| Le 04/03/2017 à 20:41, Nathan Russell a écrit :
| > To summarize an issue I recently encountered,
| >
| > - Beginning in R 3.4, R CMD check will (likely) expect users to register 
symbols for native routines (see e.g. this discussion:
| > https://github.com/RcppCore/Rcpp/issues/636)
| I came across the same issue with the package rmumps and applied a simplified 
workaround:
| - created an empty init.c (my package has only module export and no
| C-callable functions, so tools::package_native_routine_registration_skeleton()
| did not help)
| 8< src/init.c
| #include 
| #include 
| #include  // for NULL
| #include 
|
| void R_init_rmumps(DllInfo *dll)
| {
|  R_registerRoutines(dll, NULL, NULL, NULL, NULL);
|  R_useDynamicSymbols(dll, TRUE);
| }
|
| 8< end of init.c
| - in NAMESPACE I have put (note .registration=TRUE):
| useDynLib(rmumps, .registration=TRUE)
| exportPattern("^[[:alpha:]]+")
| import(methods, Rcpp)
| # plus some S3 methods I have defined
|
| For now, it keeps silent the check routine of R-devel.

Sure -- but that just lets you satisfy the "letter of the law" as you
suppress the messages from R CMD check.

It may be better to satisfy the "spiti of the law" by registering symbols.

Right. But in this case I just don't know how as I don't
have plain C-callable functions while module registering mechanisms in Rcpp is
a mystery for me. Should I register my constructors, methods, fields, ... ?
With what names, with what syntax?

I'll wait that the dust settles down with this new registering requirement
and happyly apply new instructions for Rcpp modules in this matter.

Best,
Serguei.



Dirk

| Serguei.
|
| >
| > - Generally speaking, the function 
tools::package_native_routine_registration_skeleton seems to generate the 
appropriate boilerplate code, which can then be
| > placed in src/init.c to appease R CMD check
| >
| > - However, packages using Rcpp modules will contain extra symbols of the 
form '_rcpp_module_boot_modulename' (resulting from a call to
| > Rcpp::loadModule("modulename") AFAICT), which do __not__ get picked up by 
package_native_routine_registration_skeleton
| >
| > - Such packages will build correctly, but throw an error when you attempt 
to load them:
| >
| > "Unable to load module "modulename": Failed to initialize module pointer: 
Error in FUN(X[[i]], ...):
| > no such symbol _rcpp_module_boot_modulename in package whatever"
| >
| > - To fix this, one can manually add corresponding entries to the code 
generated by package_native_routine_registration_skeleton, i.e. a declaration
| >
| > extern SEXP _rcpp_module_boot_modulename(void);
| >
| > and an entry in the CallEntries array
| >
| > {"_rcpp_module_boot_modulename", (DL_FUNC) &_rcpp_module_boot_modulename, 0}
| >
| > --
| >
| > If nothing else, I'm just putting this information out in the open for 
others who run into this issue. However, if anyone has thoughts on addressing this,
| > either from the Rcpp side, or by making changes to 
tools::package_native_routine_registration_skeleton, or something else, please 
chime in.
| >
| >
| > Nate
| >
| > --
| >
| > Steps to reproduce from a terminal, using the stock Rcpp modules:
| >
| >
| > cd /tmp
| > Rscript -e 'Rcpp::Rcpp.package.skeleton("mod", path = "/tmp", module = TRUE); 
Rcpp::compileAttributes("mod")'
| > R CMD build mod && R CMD check --as-cran mod_1.0.tar.gz
| > # as expected:
| > # * checking compiled code ... NOTE
| > # File ‘mod/libs/mod.so’:
| > #   Found no calls to: ‘R_registerRoutines’, ‘R_useDynamicSymbols’
| > #
| > # It is good practice to register native routines and to disable symbol
| > # search.
| >
| > Rscript -e 'tools::package_native_routine_registration_skeleton("mod")' | 
tee mod/src/init.c
| > # #include 
| > # #include 
| > # #include  // for NULL
| > # #include 
| > #
| > # /* FIXME:
| > #Check these declarations against the C/Fortran source code.
| > # */
| > #
| > # /* .Call calls */
| > # extern SEXP mod_rcpp_hello_world();
| > #
| > # static const R_CallMethodDef CallEntries[] = {
| > # {"mod_rcpp_hello_world", (DL_FUNC) _rcpp_hello_world, 0},
| > # {NULL, NULL, 0}
| > # };
| > #
| > # void R_init_mod(DllInfo *dll)
| > # {
| > # R_registerRoutines(dll, NULL, CallEntries, NULL, NULL);
| > # R_useDynamicSymbols(dll, FALSE);
| > # }
| >
| > R CMD build mod && R CMD check --as-cran mod_1.0.tar.gz
| > # ...
| > # * checking whether package ‘mod’ can be installed ... ERROR
| > # Installation failed.
| > # Se

Re: [Rcpp-devel] Symbol registration in packages with module classes

2017-03-06 Thread Serguei Sokol

Le 04/03/2017 à 20:41, Nathan Russell a écrit :

To summarize an issue I recently encountered,

- Beginning in R 3.4, R CMD check will (likely) expect users to register 
symbols for native routines (see e.g. this discussion:
https://github.com/RcppCore/Rcpp/issues/636)

I came across the same issue with the package rmumps and applied a simplified 
workaround:
- created an empty init.c (my package has only module export and no
C-callable functions, so tools::package_native_routine_registration_skeleton()
did not help)
8< src/init.c
#include 
#include 
#include  // for NULL
#include 

void R_init_rmumps(DllInfo *dll)
{
R_registerRoutines(dll, NULL, NULL, NULL, NULL);
R_useDynamicSymbols(dll, TRUE);
}

8< end of init.c
- in NAMESPACE I have put (note .registration=TRUE):
useDynLib(rmumps, .registration=TRUE)
exportPattern("^[[:alpha:]]+")
import(methods, Rcpp)
# plus some S3 methods I have defined

For now, it keeps silent the check routine of R-devel.

Serguei.



- Generally speaking, the function 
tools::package_native_routine_registration_skeleton seems to generate the 
appropriate boilerplate code, which can then be
placed in src/init.c to appease R CMD check

- However, packages using Rcpp modules will contain extra symbols of the form 
'_rcpp_module_boot_modulename' (resulting from a call to
Rcpp::loadModule("modulename") AFAICT), which do __not__ get picked up by 
package_native_routine_registration_skeleton

- Such packages will build correctly, but throw an error when you attempt to 
load them:

"Unable to load module "modulename": Failed to initialize module pointer: Error 
in FUN(X[[i]], ...):
no such symbol _rcpp_module_boot_modulename in package whatever"

- To fix this, one can manually add corresponding entries to the code generated 
by package_native_routine_registration_skeleton, i.e. a declaration

extern SEXP _rcpp_module_boot_modulename(void);

and an entry in the CallEntries array

{"_rcpp_module_boot_modulename", (DL_FUNC) &_rcpp_module_boot_modulename, 0}

--

If nothing else, I'm just putting this information out in the open for others 
who run into this issue. However, if anyone has thoughts on addressing this,
either from the Rcpp side, or by making changes to 
tools::package_native_routine_registration_skeleton, or something else, please 
chime in.


Nate

--

Steps to reproduce from a terminal, using the stock Rcpp modules:


cd /tmp
Rscript -e 'Rcpp::Rcpp.package.skeleton("mod", path = "/tmp", module = TRUE); 
Rcpp::compileAttributes("mod")'
R CMD build mod && R CMD check --as-cran mod_1.0.tar.gz
# as expected:
# * checking compiled code ... NOTE
# File ‘mod/libs/mod.so’:
#   Found no calls to: ‘R_registerRoutines’, ‘R_useDynamicSymbols’
#
# It is good practice to register native routines and to disable symbol
# search.

Rscript -e 'tools::package_native_routine_registration_skeleton("mod")' | tee 
mod/src/init.c
# #include 
# #include 
# #include  // for NULL
# #include 
#
# /* FIXME:
#Check these declarations against the C/Fortran source code.
# */
#
# /* .Call calls */
# extern SEXP mod_rcpp_hello_world();
#
# static const R_CallMethodDef CallEntries[] = {
# {"mod_rcpp_hello_world", (DL_FUNC) _rcpp_hello_world, 0},
# {NULL, NULL, 0}
# };
#
# void R_init_mod(DllInfo *dll)
# {
# R_registerRoutines(dll, NULL, CallEntries, NULL, NULL);
# R_useDynamicSymbols(dll, FALSE);
# }

R CMD build mod && R CMD check --as-cran mod_1.0.tar.gz
# ...
# * checking whether package ‘mod’ can be installed ... ERROR
# Installation failed.
# See ‘/tmp/mod.Rcheck/00install.out’ for details.
# ...

tail mod.Rcheck/00install.out
# *** installing help indices
# ** building package indices
# ** testing if installed package can be loaded
# Error: package or namespace load failed for ‘mod’ in .doLoadActions(where, 
attach):
#  error in load action .__A__.1 for package mod: loadModule(module = "NumEx", 
what = TRUE, env = ns, loadNow = TRUE):
#Unable to load module "NumEx": Failed to initialize module pointer: Error 
in FUN(X[[i]], ...):
#no such symbol _rcpp_module_boot_NumEx in package mod
#^^^
# Error: loading failed
# Execution halted
# ERROR: loading failed
# * removing ‘/tmp/mod.Rcheck/mod’

grep '^loadModule' mod/R/zzz.R
# loadModule("NumEx", TRUE)
# loadModule("yada", TRUE)
# loadModule("stdVector", TRUE)

# changes
diff -c init.c.old mod/src/init.c
# *** init.c.old2017-03-04 12:40:07.639280221 -0500
# --- mod/src/init.c2017-03-04 12:54:50.295290658 -0500
# ***
# *** 9,17 
# --- 9,23 
#
#   /* .Call calls */
#   extern SEXP mod_rcpp_hello_world();
# + extern SEXP _rcpp_module_boot_NumEx(void);
# + extern SEXP _rcpp_module_boot_yada(void);
# + extern SEXP _rcpp_module_boot_stdVector(void);
#
#   static const R_CallMethodDef CallEntries[] = {
#   {"mod_rcpp_hello_world", (DL_FUNC) _rcpp_hello_world, 0},
# + {"_rcpp_module_boot_NumEx", (DL_FUNC) 

Re: [Rcpp-devel] Rcpp Parallel is not working

2016-12-22 Thread Serguei Sokol

Hi To,

Le 22/12/2016 à 12:55, To Duc Khanh a écrit :

Dear all

I am trying to apply RcppParallel for my function, but my code seems to be
not working. Could you please help me to find any mistakes in my code or
explain me why it is not working?

As your result is of type "aggregation" (res[0] += ...) wouldn't it be more
appropriate to use parallelReduce() instead of parallelFor()?

Another point to pay attention for is that index domain to cover is
square in your case (i and j) while parallelFor (and Reduce) are
intended to split plain linear index domains. So some additional effort
is needed to treat it properly.

Hoping it helps,
Serguei.



I include my C/C++ code and R code at the end of this mail. Thank you for
the help.


// parallel version
// [[Rcpp::depends(RcppParallel)]]
#include
#include 

using namespace Rcpp;
using namespace RcppParallel;

inline double indvus(double a, double b, double c){
  if((a < b) && (b < c)){
return 1.0;
  } else if((a < b) && (b == c)){
return 0.5;
  } else if((a == b) && (b < c)){
return 0.5;
  } else if((a == b) && (b == c)){
return 1.0/6;
  } else{
return 0.0;
  }
}

struct VUS : public Worker {
  // input matrix
  const RVector test;
  const RMatrix dise;
  // output value
  RVector rvus;
// initialize from Rcpp input and output
  VUS(const NumericVector test, const NumericMatrix dise, NumericVector rvus)
: test(test), dise(dise), rvus(rvus) {}
  // function call operator that work for the specified range (begin/end)
  void operator()(std::size_t begin, std::size_t end) {
for(std::size_t i = begin; i < end; i++) {
  for(std::size_t j = begin; j < end; j++) {
if(j != i){
  for(std::size_t k = begin; k < end; k++){
if((k != j) && (k != i)){
  double temp = dise(i,0)*dise(j,1)*dise(k,2);
  rvus[0] += temp*indvus(test[i], test[j], test[k]);
  rvus[1] += temp;
}
  }
}
  }
}
  }
};

// [[Rcpp::export]]
NumericVector parallel_vus(NumericVector tt, NumericMatrix dd) {
  // allocate the matrix we will return
  NumericVector rvus(2);
  // create the worker
  VUS pvus(tt, dd, rvus);
  // call it with parallelFor
  parallelFor(0, tt.size(), pvus);
  return rvus;
}


//serial version
// [[Rcpp::export]]
NumericVector serial_vus(NumericVector tt, NumericMatrix dd) {
  NumericVector res(2);
  int n = tt.size();
  double temp = 0.0;
  for(int i = 0; i < n; i++){
for(int j = 0; j < n; j++){
  if(j != i){
for(int k = 0; k < n; k++){
  if((k != j) && (k != i)){
temp = dd(i, 0)*dd(j, 1)*dd(k, 2);
res[0] += temp*indvus(tt[i], tt[j], tt[k]);
res[1] += temp;
  }
}
  }
}
  }
  return res;
}


### R code
dd = t(rmultinom(1000, 1, c(0.4, 0.35, 0.25)))
tt <- numeric(1000)
tt[dd[,1] == 1] <- rnorm(sum(dd[,1] == 1), 3, sqrt(1.2))
tt[dd[,2] == 1] <- rnorm(sum(dd[,2] == 1), 6, sqrt(1.2))
tt[dd[,3] == 1] <- rnorm(sum(dd[,3] == 1), 9, sqrt(1.2))

serial_vus(tt,dd)

parallel_vus(tt, dd)




___
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] a variable for loop

2016-12-14 Thread Serguei Sokol

Hi Amina,

Looks like you want us to do your homework, right? ;)

Pay attention to subscript translation from R to C:

c[i-1, j, k-1] * b[i, j]
vs
c(i-1, j, k) *b(i, j)

Can you see the difference?

Best,
Serguei.

Le 14/12/2016 à 07:46, Amina Shahzadi a écrit :

Oh sorry. No. of columns in b and size of a must always be same.
I have made an r code to show the output.


a = c(1, 2, 3)
b = matrix(1:30, nrow=10, ncol=3)
c = array(as.double(0), dim=c(10, 3, 10))
for(i in 1:10){
  for(j in 1:3){
for(k in 1:i){
  if(k==1) c[i, j, k] = b[i, j]
  else c[i, j, k] = c[i-1, j, k-1] * b[i, j]
}
  }
}

Output:


c , , 1


  [,1] [,2] [,3]
 [1,]1   11   21
 [2,]2   12   22
 [3,]3   13   23
 [4,]4   14   24
 [5,]5   15   25
 [6,]6   16   26
 [7,]7   17   27
 [8,]8   18   28
 [9,]9   19   29
[10,]   10   20   30

, , 2

  [,1] [,2] [,3]
 [1,]000
 [2,]2  132  462
 [3,]6  156  506
 [4,]   12  182  552
 [5,]   20  210  600
 [6,]   30  240  650
 [7,]   42  272  702
 [8,]   56  306  756
 [9,]   72  342  812
[10,]   90  380  870

, , 3

  [,1] [,2]  [,3]
 [1,]00 0
 [2,]00 0
 [3,]6 1716 10626
 [4,]   24 2184 12144
 [5,]   60 2730 13800
 [6,]  120 3360 15600
 [7,]  210 4080 17550
 [8,]  336 4896 19656
 [9,]  504 5814 21924
[10,]  720 6840 24360

, , 4

  [,1]   [,2]   [,3]
 [1,]0  0  0
 [2,]0  0  0
 [3,]0  0  0
 [4,]   24  24024 255024
 [5,]  120  32760 303600
 [6,]  360  43680 358800
 [7,]  840  57120 421200
 [8,] 1680  73440 491400
 [9,] 3024  93024 570024
[10,] 5040 116280 657720

, , 5

   [,1][,2] [,3]
 [1,] 0   00
 [2,] 0   00
 [3,] 0   00
 [4,] 0   00
 [5,]   120  360360  6375600
 [6,]   720  524160  7893600
 [7,]  2520  742560  9687600
 [8,]  6720 1028160 11793600
 [9,] 15120 1395360 14250600
[10,] 30240 1860480 17100720

, , 6

[,1] [,2]  [,3]
 [1,]  00 0
 [2,]  00 0
 [3,]  00 0
 [4,]  00 0
 [5,]  00 0
 [6,]720  5765760 165765600
 [7,]   5040  8910720 213127200
 [8,]  20160 13366080 271252800
 [9,]  60480 19535040 342014400
[10,] 151200 27907200 427518000

, , 7

[,1]  [,2][,3]
 [1,]  0 0   0
 [2,]  0 0   0
 [3,]  0 0   0
 [4,]  0 0   0
 [5,]  0 0   0
 [6,]  0 0   0
 [7,]   5040  98017920  4475671200
 [8,]  40320 160392960  5967561600
 [9,] 181440 253955520  7866331200
[10,] 604800 390700800 10260432000

, , 8

 [,1]   [,2] [,3]
 [1,]   0  00
 [2,]   0  00
 [3,]   0  00
 [4,]   0  00
 [5,]   0  00
 [6,]   0  00
 [7,]   0  00
 [8,]   40320 1764322560 125318793600
 [9,]  362880 3047466240 173059286400
[10,] 1814400 5079110400 235989936000

, , 9

 [,1][,2] [,3]
 [1,]   0   0 0.00e+00
 [2,]   0   0 0.00e+00
 [3,]   0   0 0.00e+00
 [4,]   0   0 0.00e+00
 [5,]   0   0 0.00e+00
 [6,]   0   0 0.00e+00
 [7,]   0   0 0.00e+00
 [8,]   0   0 0.00e+00
 [9,]  362880 33522128640 3.634245e+12
[10,] 3628800 60949324800 5.191779e+12

, , 10

 [,1] [,2] [,3]
 [1,]   00 0.00e+00
 [2,]   00 0.00e+00
 [3,]   00 0.00e+00
 [4,]   00 0.00e+00
 [5,]   00 0.00e+00
 [6,]   00 0.00e+00
 [7,]   00 0.00e+00
 [8,]   00 0.00e+00
 [9,]   00 0.00e+00
[10,] 3628800 670442572800 1.090274e+14




On Wed, Dec 14, 2016 at 7:27 PM, Avraham Adler > wrote:



On Wed, Dec 14, 2016 at 1:24 AM Amina Shahzadi > wrote:

Hello Avraham --Happy to see you

My code is trying to produce a cube c which is going to be constructed 
by a vector a and matrix b.
And the number of rows in b and size of a must be same.

So we can assume that if a is a vector of size 3, Then b must be 2 x 3 
or 3 X 3 etc.

Thank you Avraham for quick response. I hope this will make my question 
more clear.

Best regards


On Wed, Dec 14, 2016 at 4:46 PM, Avraham Adler > wrote:

On Tue, Dec 13, 2016 at 9:51 PM, Amina Shahzadi > wrote:

Hello Friends and Prof. Dirk

 

Re: [Rcpp-devel] Rtools not finding *.o files during compilation

2016-07-07 Thread Serguei Sokol

Le 07/07/2016 14:30, Charles Determan a écrit :

I am trying to help a user debug the installation of one of my packages and 
have come across an error I don't understand.

The current process has been as follows:

1. User using Windows OS
2. User trying to install development version from my github
3. User has current Rtools installed

You mean Rtools34? While R v3.2 is used?
According to the correspondence table in 
https://cran.r-project.org/bin/windows/Rtools/
Rtools33 or Rtools32 must be used with this version of R.

My 0.02 €.
Serguei.


4. User behind firewall so downloaded local copy
5. User extracted tar version and re-archived as zip file.
6. User runs install.packages('package.zip", repos=NULL, type="source")

Compilation appears to run normally and all environmental variables are 
recognized but when reaching the linking stage it returns multiple statements 
such as:

C:\Rtools\mingw_64\bin\nm.exe: 'RcppExports.o': No such file

For every *.o file.  You could see the full user output here 
(http://pastebin.com/itNbaMJg).  What could have resulted in this problem?

Regards,
Charles


___
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

Re: [Rcpp-devel] Returning an arma vec

2014-12-05 Thread Serguei Sokol

Le 04/12/2014 17:57, Romain François a écrit :

Something like this:

template typename T
inline wrap( const arma::subview_coldouble x ){
 return wrap( arma::MatT( x ) ) ;
}

Interesting. If I do

inline SEXP wrap( const arma::vec x ) {
return wrap(Rcpp::NumericVector( x.begin(), x.end()));
}

then wrap(v) returns a vector to R, not a matrix n x 1
(v is a vec here) but wrap(v+1) still returns a matrix.
Is there a more universal way to say that any expression
resulting in vec should return a vector?

Serguei.
___
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] tracking volatile bug

2014-11-21 Thread Serguei Sokol

Well, I could narrow the issue but not yet resolve it.

Le 18/11/2014 20:46, William Dunlap a écrit :

I will try gctorture(TRUE) suggested by Martin.

I'll start with this easy part. Unfortunately valgrind
didn't detect any wrong access to memory.

Now, the difficult part.
The most reduced code in cpp producing an error under
gctorture(TRUE) is the following:
8file matrix_norm.cpp
//[[Rcpp::depends(RcppArmadillo)]]
#include RcppArmadillo.h
using namespace Rcpp;
using namespace arma;

// [[Rcpp::export]]
SEXP nmat(mat A) {
   Function Matrix_norm_r_=Environment(package:Matrix)[norm];
   SEXP s=wrap(1);
   Rf_PrintValue(s);
   SEXP res=Matrix_norm_r_(wrap(A), s);
   return res;
}
8

It is compiled with CXXFLAGS=-g -O0 -std=c++11 in ~/.R/Makevars
The R-3.1.2 itself is also compiled with -g -O0 -std=c++11 by
gcc version 4.8.2

How to reproduce ?
$ R -d gdb
(gdb) run
 library(Rcpp)
 library(Matrix)
 sourceCpp(matrix_norm.cpp)
 [Ctrl-C]
(gdb) b nmat
Breakpoint 1 at 0x7079c253: file matrix_norm.cpp, line 8.
(gdb) c
Continuing.
nmat(as.matrix(pi))

Breakpoint 1, nmat (A=...) at matrix_norm.cpp:8
8  Function Matrix_norm_r_=Environment(package:Matrix)[norm];
(gdb) c
Continuing.
[1] 1
[1] 3.141593
This is a correct result. Now, push it to an error with gctorture
 gctorture(TRUE)
 nmat(as.matrix(pi))

Breakpoint 1, nmat (A=...) at matrix_norm.cpp:8
8  Function Matrix_norm_r_=Environment(package:Matrix)[norm];
(gdb) c
Continuing.
CHARSXP: NA
Erreur : erreur d'évaluation de l'argument 'type' lors de la sélection d'une 
méthode pour la fonction 'norm' : Erreur : type 'char' indisponible dans 'eval'

The wrong behavior is manifested in printing 'CHARSXP: NA'
It means that the string 1 was not wrapped correctly. The error messages
(here in french but never mind) are just a consequence of the wrong string
parameter passed to a call Matrix::norm(). I must say that in a function
without evoking the Matrix environment but doing just a plain
wrap(1), the wrap() is working as expected.
At one moment I suspected that the problem was in Rf_mkString() where there
is a call to unprotected mkChar(), so in R-3.1.2/src/include/Rinlinedfuns.h:682
I replaced
SET_STRING_ELT(t, (R_xlen_t)0, mkChar(s));
by
SET_STRING_ELT(t, (R_xlen_t)0, PROTECT(mkChar(s)));
and consequent UNPROTECT(1) by UNPROTECT(2)
But ... adding this protection did not resolve the issue.

 nmat(as.matrix(pi))

Breakpoint 1, nmat (A=...) at matrix_norm.cpp:8
8  Function Matrix_norm_r_=Environment(package:Matrix)[norm];
(gdb) n
9  SEXP s=wrap(1);
(gdb) s
Rcpp::wrap (v=0x707a1a5a 1)
at /home/local/src/R-3.1.2/library/Rcpp/include/Rcpp/internal/wrap.h:930
930 if (v != NULL)
(gdb)
931 return Rf_mkString(v) ;
(gdb) s
Rf_mkString (s=0x707a1a5a 1) at ../../src/include/Rinlinedfuns.h:681
681 PROTECT(t = allocVector(STRSXP, (R_xlen_t)1));
(gdb) n
682 SET_STRING_ELT(t, (R_xlen_t)0, PROTECT(mkChar(s)));
(gdb)
683 UNPROTECT(2);
(gdb)
684 return t;
(gdb) call Rf_PrintValue(t)
CHARSXP: NA
instead of a correct 1.
My problem to locate the place where a pointer corruption occurs is that
if I dig deep enough to see what is going on on the stack of protected values
and during memory allocation, it ... produce a good result, i.e. a string
vector with a single element 1.
 nmat(as.matrix(pi))

Breakpoint 1, nmat (A=...) at matrix_norm.cpp:8
8  Function Matrix_norm_r_=Environment(package:Matrix)[norm];
(gdb) n
9  SEXP s=wrap(1);
(gdb) s
Rcpp::wrap (v=0x707a1a5a 1)
at /home/local/src/R-3.1.2/library/Rcpp/include/Rcpp/internal/wrap.h:930
930 if (v != NULL)
(gdb)
931 return Rf_mkString(v) ;
(gdb)
Rf_mkString (s=0x707a1a5a 1) at ../../src/include/Rinlinedfuns.h:681
681 PROTECT(t = allocVector(STRSXP, (R_xlen_t)1));
(gdb) n
682 SET_STRING_ELT(t, (R_xlen_t)0, PROTECT(mkChar(s)));
(gdb) s
Rf_mkChar (name=0x707a1a5a 1) at envir.c:3444
3444size_t len =  strlen(name);
(gdb) p name
$8 = 0x707a1a5a 1
(gdb) finish
Run till exit from #0  Rf_mkChar (name=0x707a1a5a 1) at envir.c:3444
0x778ef4f6 in Rf_mkString (s=0x707a1a5a 1) at 
../../src/include/Rinlinedfuns.h:682
682 SET_STRING_ELT(t, (R_xlen_t)0, PROTECT(mkChar(s)));
Value returned is $9 = (struct SEXPREC *) 0x48bf4c8
(gdb) call Rf_PrintValue(0x48bf4c8)
CHARSXP: 1
(gdb) n
683 UNPROTECT(2);
(gdb)
684 return t;
(gdb) call Rf_PrintValue(t)
[1] 1
(gdb) c
Continuing.
[1] 1
[1] 3.141593

But immediately after:
 nmat(as.matrix(pi))

Breakpoint 1, nmat (A=...) at matrix_norm.cpp:8
8  Function Matrix_norm_r_=Environment(package:Matrix)[norm];
(gdb) c
Continuing.
CHARSXP: NA
Erreur : erreur d'évaluation de l'argument 'type' lors de la sélection d'une 
méthode pour la fonction 'norm' : Erreur : type 'char' indisponible dans 'eval'

Any thoughts how to locate this problem with pointer 

[Rcpp-devel] tracking volatile bug

2014-11-18 Thread Serguei Sokol

Hi everybody,

I am facing a volatile bug which appears and disappears in
identical calls on identical data.

My RcppArmadilla code is expm_cpp() (it is obtained by rex2arma tool
which I have presented before, cf. attached file). It is compared and
benchmarked vs its R counterpart expm.higham() (also attached). Both
are computing matrix exponential by a same particular method.

The results are identical for R and Rcpp on small and medium sized
matrices nxn n=10:100 but on large matrices (n  800) various error messages
can interrupt (or not) a normal run of expm_cpp().

Sometimes message says (in French) type 'char' indisponible dans 'eval',
I suppose in English it must be unimplemented type 'char' in 'eval'
I have seen (thanks to Google) a similar error message in the following snippet:

/* call this R command: source(FileName) */
int errorOccurred;
SEXP e = lang2(install(source), mkString(FileName));
/* mkChar instead of mkString would lead to this runtime error:
 * Error in source(FileName) : unimplemented type 'char' in 'eval' */
R_tryEval(e, R_GlobalEnv, errorOccurred);

which suggests that somewhere in Rcpp or RcppArmadillo there is
a mkChar() call instead of mkString().

Other times, error message can say something like
argument type[1]='x' must be one of 'M','1','O','I','F' or 'E'
or argument type[1]='character' must be a one-letter character string
This latter message is somewhat volatile per se. The part of message
just after type[1]= can be 'character' (as above) or 'method' or 'ANY' etc.
I have found these messages in the Matrix package
https://r-forge.r-project.org/scm/viewvc.php/pkg/Matrix/src/Mutils.c?view=markuproot=matrixpathrev=2614
function char La_norm_type(const char *typstr)
Seemingly, the argument *typstr is getting corrupted somewhere
on the road.

It is useless to say that debugging is of no help here.
If run in a debugger, the program stops normally with
or without error messages cited above.

I have also tried to low the level of gcc optimization
both in compiling R and the Rcpp code but it didn't help.

Anybody has an experience in tracking down similar cases?

This example can be run as:
library(expm)
library(Rcpp)
source(expm_cpp.R)
source(expm.higham.R)
n=1000
As=matrix(rnorm(n*n), n)
stopifnot(diff(range(expm.higham(As, balancing=TRUE)-expm_cpp(As, 
balancing=TRUE)))  1.e-14)

The last command may be run several times before an error shows up.

Cheers,
Serguei.

cppFunction(depends='RcppArmadillo', rebuild=TRUE, includes='
template typename T
inline unsigned which_max(T v) {
   unsigned i;
   v.max(i);
   return i+1;
}

templatetypename T
inline unsigned which_min(T v) {
   unsigned i;
   v.min(i);
   return i+1;
}
', 


using namespace arma;
using namespace Rcpp;

SEXP expm_cpp(
NumericMatrix A_in_,
bool balancing) {

   // auxiliary functions
   Environment base_env_r_=Environment::base_env();
   Function rep_r_=base_env_r_[\rep\];
   Function c_r_=base_env_r_[\c\];
   // External R function declarations
   Function expm_balance_r_=Environment(\package:expm\)[\balance\];
   Function Matrix_norm_r_=Environment(\package:Matrix\)[\norm\];

   // Input variable declarations and conversion
   mat A(A_in_.begin(), A_in_.nrow(), A_in_.ncol(), false);

   // Output and intermediate variable declarations
   mat A2;
   mat B;
   mat B2;
   mat B4;
   mat B6;
   List baP;
   List baS;
   mat C;
   vec c_;
   ivec d;
   vec dd;
   int i;
   mat I;
   int k;
   int l;
   int n;
   double nA;
   mat P;
   ivec pp;
   double s;
   vec t;
   mat tt;
   mat U;
   mat V;
   mat X;

   // Translated code starts here
   d=ivec(IntegerVector::create(A.n_rows, A.n_cols));
   if (d.size() != 2 || d.at(0) != d.at(1)) stop(\'A' must be a square 
matrix\);
   n=d.at(0);
   if (n = 1) return wrap(exp(A));
   if (balancing) {
  baP=asList(expm_balance_r_(A, \P\));
  baS=asList(expm_balance_r_(asmat(baP[\z\]), \S\));
  A=asmat(baS[\z\]);
   }
   nA=asdouble(Matrix_norm_r_(A, \1\));
   I=eyemat(n, n);
   if (nA = 2.1) {
  t=vec({0.015, 0.25, 0.95, 2.1});
  l=which_max(nA = t);
  C=join_vert(join_vert(join_vert(vec({120, 60, 12, 1, 0, 0, 0, 0, 0, 
0}).st(), vec({30240, 15120, 3360, 420, 30, 1, 0, 0, 0, 0}).st()), 
vec({17297280, 8648640, 1995840, 277200, 25200, 1512, 56, 1, 0, 0}).st()), 
vec({17643225600, 8821612800, 2075673600, 302702400, 30270240, 2162160, 110880, 
3960, 90, 1}).st());
  A2=A*A;
  P=I;
  U=C.at((l)-1, 1) * I;
  V=C.at((l)-1, 0) * I;
  for (int incr_arma_=(1 = l ? 1 : -1), k=1; k != l+incr_arma_; 
k+=incr_arma_) {
 P=P*A2;
 U=U + C((l)-1, ((2 * k) + 2)-1) * P;
 V=V + C((l)-1, ((2 * k) + 1)-1) * P;
  }
  U=A*U;
  X=solve(V - U, V + U);
   } else {
  s=log2(nA / 5.4);
  B=A;
  if (s  0) {
 s=ceil(s);
 B=B / (pow(2, s));
  }
  c_=vec({6476475253248, 3238237626624, 7771770303897600, 
1187353796428800, 129060195264000, 10559470521600, 

Re: [Rcpp-devel] tracking volatile bug

2014-11-18 Thread Serguei Sokol

Le 18/11/2014 16:39, Romain François a écrit :



Le 18 nov. 2014 à 14:42, Serguei Sokol serguei.so...@gmail.com a écrit :

/* call this R command: source(FileName) */
int errorOccurred;
SEXP e = lang2(install(source), mkString(FileName));
/* mkChar instead of mkString would lead to this runtime error:
 * Error in source(FileName) : unimplemented type 'char' in 'eval' */
R_tryEval(e, R_GlobalEnv, errorOccurred);


e is not protected here. Does the problem go away if you protect it:

SEXP e = PROTECT( lang2(install(source), mkString(FileName))) ;

you mean
SEXP e = PROTECT(lang2(install(source), mkChar(expm.higham.R)));
(because mkString() had no problem)?
No, it does not, the problem is still there.




Or more R/C++ idiomatically, which is both nicer and safer.

Language e( source, FileName ) ;

This works as expected. We can suppose that behind the stage
FileName is wrap()-ed here with mkString() and not with mkChar().
That's why it's working.




which suggests that somewhere in Rcpp or RcppArmadillo there is
a mkChar() call instead of mkString().

Other times, error message can say something like
argument type[1]='x' must be one of 'M','1','O','I','F' or 'E'
or argument type[1]='character' must be a one-letter character string
This latter message is somewhat volatile per se. The part of message
just after type[1]= can be 'character' (as above) or 'method' or 'ANY' etc.


That kind of problem usually is related to premature GC.

I will try gctorture(TRUE) suggested by Martin.

Thanks,
Serguei.
___
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] rex2arma: from R expression to RcppArmadillo code

2014-11-03 Thread Serguei Sokol

Hi,

I am new on this list so let me say that in my opinion
RcppArmadillo have a bright future among R developers
especially among those doing scientific calculus.

That's why and to facilitate my life with it, I have developed
a small tool rex2arma() for automatic conversion of R code to
RcppArmadillo inlined code. For instance, it is written in pure R
but if the conversion time becomes an issue it can be easily ported
to C or Rcpp.

Now, let me take a small case to present how rex2arma can be
used and what it gives. The case fastLM from RcppDistrubution
is a perfect example for this.

The most straightforward way is to write an R function that you want
to convert, e.g.:

# pure R solution which can be automatically converted to RcppArmadillo code
fastLm_r=function(X, y) {
   df=nrow(X)-ncol(X) # more generally, it should do nb_of_rows(X)-rank(X) but 
I keep the original code
   coef=qr.solve(X, y)
   res=y-c(X%*%coef)
   s2=(res%*%res)/df
   std_err=sqrt(s2*diag(ginv(t(X)%*%X)))
   return(list(coefficients=coef, stderr=std_err, df=df))
}

Then you do
 # NB. The input params X and y must exist before rex2arma() call
 y - log(trees$Volume)
 X - cbind(1, log(trees$Girth))
 code=rex2arma(fastLm_r, fname=fastLm_rex, exec=1)

Here fastLm_rex is the function name that is created by
the conversion. To call it, do like with the original R function:

result - fastLm_rex(X, y)

That's it. Adding fastLM_rex to the whole benchmark gives:
 print(res[,1:4])
test replications relative elapsed
1   fLmOneCast(X, y) 50001.000   0.198
11   fastLm_rex_nocopy(X, y) 50001.010   0.200
3  fLmConstRef(X, y) 50001.025   0.203
2  fLmTwoCasts(X, y) 50001.030   0.204
10  fastLm_rex(X, y) 50001.035   0.205
5fastLmPureDotCall(X, y) 50001.212   0.240
4   fastLmPure(X, y) 50001.823   0.361
7   lm.fit(X, y) 50002.904   0.575
9 fastLm_r(X, y) 5000   10.025   1.985
6  fastLm(frm, data = trees) 5000   39.399   7.801
8  lm(frm, data = trees) 5000   50.919  10.082

Let glimps the cpp code now.
 cat(code)

cppFunction(depends='RcppArmadillo', rebuild=FALSE,
'SEXP fastLm_rex(
NumericMatrix X_in_,
NumericVector y_in_) {
   using namespace arma;
   using namespace Rcpp;
   // Variable declarations
   mat X(X_in_.begin(), X_in_.nrow(), X_in_.ncol(), true);
   vec y(y_in_.begin(), y_in_.size(), true);
   // Translated code starts here

   double df=(X).n_rows-(X).n_cols;
   vec coef=solve(X, y);
   vec res=y-vectorise(X*coef);
   double s2=(dot(res, res))/df;
   vec std_err=sqrt(s2*diagvec(pinv((X).t()*X)));
   return(List::create(Named(coefficients)=coef, Named(stderr)=std_err, 
Named(df)=df));

}'
)

Quite straightforward and faithful to R source, isn't it?

By default, the input parameters are copied in a new memory but if you know 
what you are doing
you can call rex2arma with an optional parameter copy=FALSE (it corresponds to 
a row
fastLm_rex_nocopy in the benchmark above) so the cpp code will do operations 
in-place.

Other options exist, like rebuild or not the compiled cpp function or
produce the code with or without its execution.

Another point is that different call modes are possible. Instead of passing an 
R function,
like above, one can pass directly an R code (which obviously won't be executed 
in R
but in the produced cpp function), e.g.:

 result=rex2arma(diag(ginv(t(X)%*%X)))

or you can just see the produced code without function creation
 cat(rex2arma(diag(ginv(t(X)%*%X)), exec=FALSE))
This can be helpful as kind of cheat-sheet.

You can also pass a text containing an R code
 x=1:5; src=inner=x%*%x; outer=x%o%x
 (result - rex2arma(src))

Generally speaking, some limitations apply to
the R code that can be converted:
- only numeric types are allowed (a list() can appear only
as the last returned expression)
- no indexing (for now)
- only few and basic R functions can be converted to their Armadillo homologues
- etc. (cf. comments in the source code)

So, how can I verse this code to RcppArmadillo (in hypothesis that you
are interested in and willing to test it on your side)? Do I just post
the code here? Do I commit it somewhere to a guest branch of your git
repository? Something else?

Looking forward to here from you,
Serguei.



___
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] rex2arma: from R expression to RcppArmadillo code

2014-11-03 Thread Serguei Sokol

Dirk,

Le 03/11/2014 17:33, Dirk Eddelbuettel a écrit :


Serguei,

Thanks for your enthusiasm about Rcpp and RcppArmadillo. It is mostly shared :)

You give a long sales job on rex2arma, which is fine.  Open Source mostly
works differently.  If you really wanted this to be part of RcppArmadillo,
could you provide a pull request?  Code rules, and we need to see the code.

That was precisely the sens of my question : where can I put the code ?
Have you a github account ?

Best,
Serguei.



But without having seen the code, why not ... just make rex2arma a package
which you would author and ship to CRAN?  Just a thought.

Cheers, Dirk



___
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