Skip to content

lbfgsb from C/C++

4 messages · axionator, Dirk Eddelbuettel

#
Hi,

I would like to call R's lbfgsb function from my C/C++ code by including
R_ext/Applic.h and linking against libR.
Currently, I am allocating memory for x (and the other input arrays for
lbfgsb) in my C/C++ code via malloc/new. However, this gives a segmentation
fault when executing the program.
I tried to allocate x via PROTECT(x = NEW_NUMERIC(n)); x_p =
NUMERIC_POINTER(x);.
This compiles but also gives a segmentation fault.
Is there a way to use lbfgsb from C/C++ directly (without an intermediate
call of R)? Did I miss any compile flags?

Thanks
#
On 7 September 2014 at 12:30, axionator wrote:
| I would like to call R's lbfgsb function from my C/C++ code by including
| R_ext/Applic.h and linking against libR.
| Currently, I am allocating memory for x (and the other input arrays for
| lbfgsb) in my C/C++ code via malloc/new. However, this gives a segmentation
| fault when executing the program.
| I tried to allocate x via PROTECT(x = NEW_NUMERIC(n)); x_p =
| NUMERIC_POINTER(x);.
| This compiles but also gives a segmentation fault.
| Is there a way to use lbfgsb from C/C++ directly (without an intermediate
| call of R)? Did I miss any compile flags?

R is built to provide a 'language and environment', not a callable library.

There is a however an optional callable library with a (much smaller) subset
of functionality, see the section '6.16 Using these functions in your own C
code' in the 'Writing R Extension' manual.  However, the library does /not/
contain the bounded BFGS implementation you are seeking.

So you may want to look at another (open source) optmization library. NLopt
by Steven Johnson (http://ab-initio.mit.edu/wiki/index.php/NLopt) is decent
and easy to use; if you need LBFGSB there is also Jorge Nocedal's site
(http://www.ece.northwestern.edu/~nocedal/lbfgsb.html) as well as much more.

Dirk
#
Ok. Another question: I wanted to use RInternal and compile the following
small test example:

#include <RInside.h>

const char* hello( std::string who ) {
        std::string result( "hello ") ;
        result += who ;
        return result.c_str() ;
}

int main(int argc, char *argv[]) {

    RInside R(argc, argv);

    R["hello"] = Rcpp::InternalFunction( &hello );
    std::string result = R.parseEval("hello('world')") ;
    std::cout << "hello( 'world') =  " << result << std::endl ;

    return 0;
}

However, I get:

In file included from
/usr/local/lib/R/site-library/Rcpp/include/Rcpp/storage/storage.h:4:0,
                 from
/usr/local/lib/R/site-library/Rcpp/include/RcppCommon.h:128,
                 from /usr/local/lib/R/site-library/Rcpp/include/Rcpp.h:27,
                 from
/usr/local/lib/R/site-library/RInside/include/RInsideCommon.h:38,
                 from
/usr/local/lib/R/site-library/RInside/include/RInside.h:26,
                 from testRInside.cpp:1:
/usr/local/lib/R/site-library/Rcpp/include/Rcpp/storage/PreserveStorage.h:
In instantiation of ?void Rcpp::PreserveStorage<CLASS>::set__(SEXP) [with
CLASS = Rcpp::InternalFunction_Impl<Rcpp::PreserveStorage>; SEXP =
SEXPREC*]?:
/usr/local/lib/R/site-library/Rcpp/include/Rcpp/InternalFunction.h:43:13:
required from ?void Rcpp::InternalFunction_Impl<StoragePolicy>::set(SEXP)
[with StoragePolicy = Rcpp::PreserveStorage; SEXP = SEXPREC*]?
/usr/local/lib/R/site-library/Rcpp/include/Rcpp/generated/InternalFunction__ctors.h:37:3:
  required from
?Rcpp::InternalFunction_Impl<StoragePolicy>::InternalFunction_Impl(OUT
(*)(U0)) [with OUT = const char*; U0 = std::basic_string<char>;
StoragePolicy = Rcpp::PreserveStorage]?
testRInside.cpp:16:49:   required from here
/usr/local/lib/R/site-library/Rcpp/include/Rcpp/storage/PreserveStorage.h:22:13:
error: ?class Rcpp::InternalFunction_Impl<Rcpp::PreserveStorage>? has no
member named ?update?

Any ideas what's going wrong?
On Sun, Sep 7, 2014 at 4:46 PM, Dirk Eddelbuettel <edd at debian.org> wrote:

            

  
  
#
1)  Your question is no longer related to lbfgsb. It is common to start a
new thread with a new Subject: for new questions.

2)  You do not show your name or affiliation. That is generally frowned upon
around here.

3)  Your question was about RInside / Rcpp.  We prefer those questions over
on the rcpp-devel list.  Subscribe first in order to post.

4)  Your example is essentially identical to rinside_sample9.cpp for which a
patch / pull request was recently integrated.  This works with the current
sources (from Github) and the upcoming release.

Dirk