Rcpp, dyn.load and C++ problems

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
10 messages Options
Reply | Threaded
Open this post in threaded view
|

Rcpp, dyn.load and C++ problems

Martin Møller Skarbiniks Pedersen
Hi,

  I have written a small C++ function and compile it.
  However in R I can't see the function I have defined in C++.
  I have read some web-pages about Rcpp and C++ but it is a bit confusion
for me.

Anyway,
  This is the C++-code:

#include <Rcpp.h>
using namespace Rcpp;

// [[Rcpp::export]]
List compute_values_cpp(int totalPoints = 1e5, double angle_increment =
0.01, int radius = 400, double grow = 3.64) {
  double xn = 0.5;
  double angle = 0.1;
  double xn_plus_one, yn_plus_one;
  NumericVector x(totalPoints);
  NumericVector y(totalPoints);

  for (int i=0; i<totalPoints; i++) {
    xn_plus_one = xn*cos(angle)*radius;
    yn_plus_one = xn*sin(angle)*radius;
    angle += angle_increment;
    xn = grow*xn*(1-xn);
    x[i] = xn_plus_one;
    y[i] = yn_plus_one;
  }
  return List::create(Rcpp::Named("x") = x, Rcpp::Named("y") = y);
}

And I compile it like this:
PKG_CXXFLAGS=$(Rscript -e 'Rcpp:::CxxFlags()') \
PKG_LIBS=$(Rscript -e 'Rcpp:::LdFlags()')  \
R CMD SHLIB logistic_map.cpp
without problems and I get a logistic_map.so file as expected.

However in R:
R> dyn.load("logistic_map.so")
R> compute_values_cpp()
Error in compute_values_cpp() :
  could not find function "compute_values_cpp"

Please advise,
  What piece of the puzzle is missing?

Regards
Martin M. S. Pedersen

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: Rcpp, dyn.load and C++ problems

Eric Berger
.Call("compute_values_cpp")
Also, if you were passing arguments to the C++ function you would need to
declare the function differently.
Do a search on "Rcpp calling C++ functions from R"

HTH,
Eric


On Sun, Dec 3, 2017 at 3:06 AM, Martin Møller Skarbiniks Pedersen <
[hidden email]> wrote:

> Hi,
>
>   I have written a small C++ function and compile it.
>   However in R I can't see the function I have defined in C++.
>   I have read some web-pages about Rcpp and C++ but it is a bit confusion
> for me.
>
> Anyway,
>   This is the C++-code:
>
> #include <Rcpp.h>
> using namespace Rcpp;
>
> // [[Rcpp::export]]
> List compute_values_cpp(int totalPoints = 1e5, double angle_increment =
> 0.01, int radius = 400, double grow = 3.64) {
>   double xn = 0.5;
>   double angle = 0.1;
>   double xn_plus_one, yn_plus_one;
>   NumericVector x(totalPoints);
>   NumericVector y(totalPoints);
>
>   for (int i=0; i<totalPoints; i++) {
>     xn_plus_one = xn*cos(angle)*radius;
>     yn_plus_one = xn*sin(angle)*radius;
>     angle += angle_increment;
>     xn = grow*xn*(1-xn);
>     x[i] = xn_plus_one;
>     y[i] = yn_plus_one;
>   }
>   return List::create(Rcpp::Named("x") = x, Rcpp::Named("y") = y);
> }
>
> And I compile it like this:
> PKG_CXXFLAGS=$(Rscript -e 'Rcpp:::CxxFlags()') \
> PKG_LIBS=$(Rscript -e 'Rcpp:::LdFlags()')  \
> R CMD SHLIB logistic_map.cpp
> without problems and I get a logistic_map.so file as expected.
>
> However in R:
> R> dyn.load("logistic_map.so")
> R> compute_values_cpp()
> Error in compute_values_cpp() :
>   could not find function "compute_values_cpp"
>
> Please advise,
>   What piece of the puzzle is missing?
>
> Regards
> Martin M. S. Pedersen
>
>         [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/
> posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: Rcpp, dyn.load and C++ problems

Martin Møller Skarbiniks Pedersen
On 3 December 2017 at 05:23, Eric Berger <[hidden email]> wrote:

> .Call("compute_values_cpp")
> Also, if you were passing arguments to the C++ function you would need to
> declare the function differently.
> Do a search on "Rcpp calling C++ functions from R"
>
>
Hi,
  It is still not working.
  $ ./compile.sh
g++  -I/usr/include/R/ -DNDEBUG   -D_FORTIFY_SOURCE=2
-I/home/tusk/R/x86_64-pc-linux-gnu-library/3.4/Rcpp/include -fpic
-march=x86-64 -mtune=generic -O2 -pipe -fstack-protector-strong -fno-plt
-c logistic_map.cpp -o logistic_map.o
g++ -shared -L/usr/lib64/R/lib
-Wl,-O1,--sort-common,--as-needed,-z,relro,-z,now -o logistic_map.so
logistic_map.o -L/usr/lib64/R/lib -lR

$ readelf -Ws logistic_map.so  | grep -i comp
    89: 0000000000002a50  2356 FUNC    GLOBAL DEFAULT    9
_Z18compute_values_cppidid
   141: 0000000000002a50  2356 FUNC    GLOBAL DEFAULT    9
_Z18compute_values_cppidid

$ R
R> dyn.load("logistic_map.so")
R> .Call("compute_values_cpp")
Error in .Call("compute_values_cpp") :
  C symbol name "compute_values_cpp" not in load table

Hmm.



> HTH,
> Eric
>
>
> On Sun, Dec 3, 2017 at 3:06 AM, Martin Møller Skarbiniks Pedersen <
> [hidden email]> wrote:
>
>> Hi,
>>
>>   I have written a small C++ function and compile it.
>>   However in R I can't see the function I have defined in C++.
>>   I have read some web-pages about Rcpp and C++ but it is a bit confusion
>> for me.
>>
>> Anyway,
>>   This is the C++-code:
>>
>> #include <Rcpp.h>
>> using namespace Rcpp;
>>
>> // [[Rcpp::export]]
>> List compute_values_cpp(int totalPoints = 1e5, double angle_increment =
>> 0.01, int radius = 400, double grow = 3.64) {
>>   double xn = 0.5;
>>   double angle = 0.1;
>>   double xn_plus_one, yn_plus_one;
>>   NumericVector x(totalPoints);
>>   NumericVector y(totalPoints);
>>
>>   for (int i=0; i<totalPoints; i++) {
>>     xn_plus_one = xn*cos(angle)*radius;
>>     yn_plus_one = xn*sin(angle)*radius;
>>     angle += angle_increment;
>>     xn = grow*xn*(1-xn);
>>     x[i] = xn_plus_one;
>>     y[i] = yn_plus_one;
>>   }
>>   return List::create(Rcpp::Named("x") = x, Rcpp::Named("y") = y);
>> }
>>
>> And I compile it like this:
>> PKG_CXXFLAGS=$(Rscript -e 'Rcpp:::CxxFlags()') \
>> PKG_LIBS=$(Rscript -e 'Rcpp:::LdFlags()')  \
>> R CMD SHLIB logistic_map.cpp
>> without problems and I get a logistic_map.so file as expected.
>>
>> However in R:
>> R> dyn.load("logistic_map.so")
>> R> compute_values_cpp()
>> Error in compute_values_cpp() :
>>   could not find function "compute_values_cpp"
>>
>> Please advise,
>>   What piece of the puzzle is missing?
>>
>> Regards
>> Martin M. S. Pedersen
>>
>>         [[alternative HTML version deleted]]
>>
>> ______________________________________________
>> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide http://www.R-project.org/posti
>> ng-guide.html
>> and provide commented, minimal, self-contained, reproducible code.
>>
>
>


--
Til uvedkommende, der læser med: Der er ingen grund til at læse min
mail. Jeg har intet at gøre med FARC, al-Jihad, al-Qaida, Hamas, Hizb
al-Mujahidin eller ETA. Jeg har aldrig gjort Zakat, går ikke ind for
Istishad, har ikke lavet en bilbombe eller kernevåben og jeg ved
dårligt nok, hvad Al Manar og бомба betyder.  Men tak for den udviste
interesse.

Leve Ligemageriet!
Styrk pøbelvældet!
Bevar misundelsesafgifterne og cafepengene!
Hurra for ældrebyrden!

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: Rcpp, dyn.load and C++ problems

Martin Møller Skarbiniks Pedersen
In reply to this post by Eric Berger
On 3 December 2017 at 05:23, Eric Berger <[hidden email]> wrote:

>
> Do a search on "Rcpp calling C++ functions from R"
>

Thanks. However search for "Rcpp calling C++ functions from R" gives a lot
of result but I think
some of them are outdated and others don't agree with each other.

Can you point to a specific good on-line guide for me?

Regards
Martin

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: Rcpp, dyn.load and C++ problems

plangfelder
I would go to the source, in this case Dirk Eddelbuettel's (I hope I
spelled it correctly) documentation for Rcpp:

http://dirk.eddelbuettel.com/code/rcpp/Rcpp-attributes.pdf

Note that you need to do

sourceCpp("logistic_map.cpp")

in R instead of building and dyn.load()-ing the object.

HTH,

Peter

On Sun, Dec 3, 2017 at 11:04 AM, Martin Møller Skarbiniks Pedersen
<[hidden email]> wrote:

> On 3 December 2017 at 05:23, Eric Berger <[hidden email]> wrote:
>
>>
>> Do a search on "Rcpp calling C++ functions from R"
>>
>
> Thanks. However search for "Rcpp calling C++ functions from R" gives a lot
> of result but I think
> some of them are outdated and others don't agree with each other.
>
> Can you point to a specific good on-line guide for me?
>
> Regards
> Martin
>
>         [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: Rcpp, dyn.load and C++ problems

Dirk Eddelbuettel
In reply to this post by Martin Møller Skarbiniks Pedersen

Martin,

You are making your life way too complicated.

There are a number of things I would do differently:

0) Wrong list. Rcpp has its down, rcpp-devel, and I basically do not read
this and would have missed this were it not for luck.

On 3 December 2017 at 02:06, Martin Møller Skarbiniks Pedersen wrote:
|   I have read some web-pages about Rcpp and C++ but it is a bit confusion
| for me.

1) Keep reading.

| And I compile it like this:
| PKG_CXXFLAGS=$(Rscript -e 'Rcpp:::CxxFlags()') \
| PKG_LIBS=$(Rscript -e 'Rcpp:::LdFlags()')  \
| R CMD SHLIB logistic_map.cpp
| without problems and I get a logistic_map.so file as expected.

2) Possible but too complicated. Read on.
 
| However in R:
| R> dyn.load("logistic_map.so")

3) You never ever need that with Rcpp and its tool, unless you insist on
redoing thing by hand in which case you _must_ use SEXP .Call(SEXP a, ...)

| Please advise,
|   What piece of the puzzle is missing?

4) Keep reading. I will pivot to you other mails. Solution below.

On 3 December 2017 at 20:00, Martin Møller Skarbiniks Pedersen wrote:
| Hi,
|   It is still not working.
|   $ ./compile.sh

5) Still wrong.

On 3 December 2017 at 20:04, Martin Møller Skarbiniks Pedersen wrote:
| Thanks. However search for "Rcpp calling C++ functions from R" gives a lot
| of result but I think
| some of them are outdated and others don't agree with each other.
|
| Can you point to a specific good on-line guide for me?

6) Call me crazy but maybe the nine vignettes included with the package?

In essence you _complitely_ missed what Rcpp Attributes does and shows, as
does the (newer) Rcpp Introduction vignette.

More crazy, your file was actually 100% correct. I just added three lines to
_also_ execute R code (and I indented just for clarity)

    /*** R
    res <- compute_values_cpp()
    str(res)
    */

Then in R:

    R> library(Rcpp)
    R> sourceCpp("/tmp/mmsp.cpp")
   
    R> res <- compute_values_cpp()
   
    R> str(res)
    List of 2
     $ x: num [1:100000] 199 362 118 302 262 ...
     $ y: num [1:100000] 20 40 14.3 39.5 36.9 ...
   
    R>

_One call_ of sourceCpp() compiles AND links AND loads AND runs the example R
code (which is optional).

For reference, the current mmsp.cpp follows.

Dirk


#include <Rcpp.h>
using namespace Rcpp;

// [[Rcpp::export]]
List compute_values_cpp(int totalPoints = 1e5, double angle_increment =
0.01, int radius = 400, double grow = 3.64) {
  double xn = 0.5;
  double angle = 0.1;
  double xn_plus_one, yn_plus_one;
  NumericVector x(totalPoints);
  NumericVector y(totalPoints);

  for (int i=0; i<totalPoints; i++) {
    xn_plus_one = xn*cos(angle)*radius;
    yn_plus_one = xn*sin(angle)*radius;
    angle += angle_increment;
    xn = grow*xn*(1-xn);
    x[i] = xn_plus_one;
    y[i] = yn_plus_one;
  }
  return List::create(Rcpp::Named("x") = x, Rcpp::Named("y") = y);
}

/*** R
res <- compute_values_cpp()
str(res)
*/

--
http://dirk.eddelbuettel.com | @eddelbuettel | [hidden email]

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: Rcpp, dyn.load and C++ problems

Martin Møller Skarbiniks Pedersen
On 3 December 2017 at 20:19, Dirk Eddelbuettel <[hidden email]> wrote:

Hi Dirk,
  Thanks for your answers. I got a few more questions.

>
> 0) Wrong list. Rcpp has its down, rcpp-devel, and I basically do not read
> this and would have missed this were it not for luck.

OK. I did found the rcpp-devel mailing-list.
But I though it was a developers of the rcpp-package.
So it is ok to post beginners questions to rcpp-devel?

>
> 6) Call me crazy but maybe the nine vignettes included with the package?
>

OK. I will print them and read them.

[...]

>
> Then in R:
>
>     R> library(Rcpp)
>     R> sourceCpp("/tmp/mmsp.cpp")
>

> [...]


>
> _One call_ of sourceCpp() compiles AND links AND loads AND runs the
example R
> code (which is optional).


Basically I was searching for ways to compile to C++ code a single time and
not everything the R code runs.

Like I don't recompile my pure C++ programs everything I run them, only
if I change something.

But that is different with C++ in R? That it is normal to compile the C++
code
for each run?

Regards
Martin

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: Rcpp, dyn.load and C++ problems

Dirk Eddelbuettel
In reply to this post by plangfelder

On 3 December 2017 at 11:08, Peter Langfelder wrote:
| I would go to the source, in this case Dirk Eddelbuettel's (I hope I
| spelled it correctly)

You did. Take a point :)

| documentation for Rcpp:
|
| http://dirk.eddelbuettel.com/code/rcpp/Rcpp-attributes.pdf

Yup.  And

    RShowDoc("Rcpp-attributes", package="Rcpp")

in R is even easier.

On 3 December 2017 at 13:19, Dirk Eddelbuettel wrote:
| 0) Wrong list. Rcpp has its down, rcpp-devel, and I basically do not read
| this and would have missed this were it not for luck.

"R has its own list, rcpp-devel" is what I meant to write.  For completeness,
we can also test on the command-line as Martin and what I do a lot myself:

edd@bud:~$ r -lRcpp -e'sourceCpp("/tmp/mmsp.cpp")'

R> res <- compute_values_cpp()

R> str(res)

Attaching package: ‘utils’

The following objects are masked from ‘package:Rcpp’:

    .DollarNames, prompt

List of 2
 $ x: num [1:100000] 199 362 118 302 262 ...
 $ y: num [1:100000] 20 40 14.3 39.5 36.9 ...
You have mail in /var/mail/edd
edd@bud:~$


That uses littler which I like, Rscript is almost the same (but has no -l
switch for libraries and fails to load the methods package which we sometimes
need).

Dirk


--
http://dirk.eddelbuettel.com | @eddelbuettel | [hidden email]

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: Rcpp, dyn.load and C++ problems

Martin Møller Skarbiniks Pedersen
>
> > edd@bud:~$ r -lRcpp -e'sourceCpp("/tmp/mmsp.cpp")'


???

What is r in this case ? A alias for something ?

Regards
Martin

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: Rcpp, dyn.load and C++ problems

Dirk Eddelbuettel
In reply to this post by Martin Møller Skarbiniks Pedersen

On 3 December 2017 at 20:30, Martin Møller Skarbiniks Pedersen wrote:
| On 3 December 2017 at 20:19, Dirk Eddelbuettel <[hidden email]> wrote:
|   Thanks for your answers. I got a few more questions.
|
| >
| > 0) Wrong list. Rcpp has its down, rcpp-devel, and I basically do not read
| > this and would have missed this were it not for luck.
|
| OK. I did found the rcpp-devel mailing-list.
| But I though it was a developers of the rcpp-package.
| So it is ok to post beginners questions to rcpp-devel?

Very much so. Just peruse the archive. We have a non-public and very quiet
list Rcpp-core for development of Rcpp -- as opposed 'developing with Rcpp'.

| Basically I was searching for ways to compile to C++ code a single time and
| not everything the R code runs.

A package does that for you. Just how Rcpp itself contains compiled code. Or
any of the ~ 1250 packages on CRAN. Each compiled once, running as often as
you want thereafter.
 
On 3 December 2017 at 20:42, Martin Møller Skarbiniks Pedersen wrote:
| >
| > > edd@bud:~$ r -lRcpp -e'sourceCpp("/tmp/mmsp.cpp")'
|
|
| ???
|
| What is r in this case ? A alias for something ?

Littler, another (CRAN) package of mine. I explained that a few lines
lower. If it still confuses you imagine you saw

$ Rscript -e 'Rcpp::sourceCpp("/tmp/mmsp.cpp")'

Dirk

--
http://dirk.eddelbuettel.com | @eddelbuettel | [hidden email]

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.