Problems reloading C++ code with devtools::load_all when using RcppArmadillo

241 views
Skip to first unread message

Mike P

unread,
Sep 27, 2016, 9:30:51 AM9/27/16
to devtools
Hi,

It seems that devtools::load_all does not reload shared library after re-compiling new changes in C++ code. I have to restart R for my changes to take effect. This only happens when I use armadillo library. Below example is reproducible every time.

To reproduce, start a new R session and do:
> library(devtools)
> library(RcppArmadillo)
> RcppArmadillo.package.skeleton("armatest")


Then edit the generated armatest/src/rcpparma_hello_world.cpp file and add the following function at the end:
// [[Rcpp::export]]
Rcpp::NumericVector see_change() {
  Rcpp::NumericVector v(1);
  v[0] = 1;
  return v;
}

Then compile and load the package, and call the function in the R session:
> load_all("armatest")
> see_change()
[1] 1

Now make a small change to the above C++ function by changing "v[0]=1" to "v[0]=2". In the same R session reload the package and run the function again:
> load_all("armatest")
> see_change()
[1] 1


Notice that the output hasn't changed, even though the C++ code was compiled and the shared library was supposedly reloaded. However if I restart R and load the package again, the output does change to 2. Also notice that doing load_all on this package in the new R session doesn't recompile anything, which means that compilation was done properly in the previous R session, and the problem is only that the new shared library was not reloaded.

I'm running Ubuntu 14.04. Here are some details about my system:
> R.version
               _                           
platform       x86_64-pc-linux-gnu         
arch           x86_64                      
os             linux-gnu                   
system         x86_64, linux-gnu           
status                                     
major          3                           
minor          2.2                         
year           2015                        
month          08                          
day            14                          
svn rev        69053                       
language       R                           
version.string R version 3.2.2 (2015-08-14)
nickname       Fire Safety                 
> packageVersion("devtools")
[1] ‘1.9.1’
> packageVersion("Rcpp")
[1] ‘0.12.1’
> packageVersion("RcppArmadillo")
[1] ‘0.5.500.2.0’

The armadillo library has version 4.200.0 and is from the official Ubuntu installation.

Thanks for any help on this.

-Mike


Mike P

unread,
Sep 27, 2016, 12:25:33 PM9/27/16
to devtools
I've a solution and going to post it here for reference for other people.

I've begun experimenting with the produced shared library armatest.so by loading/unloading it using R function dyn.load/dyn.unload. The functions seemed to work correctly, but when using lsof on my R process, I've noticed that the library remained loaded. So dyn.unload was not actually unloading the library, and it remained mapped in the process memory.

Following suggestions in this thread:

https://github.com/kaskr/adcomp/issues/27

I've ran readelf on the shared library, and sure enough it contained symbols labeled UNIQUE:
$ readelf -s armatest.so | grep UNIQUE
   
118: 000000000020d248     8 OBJECT  UNIQUE DEFAULT   24 _ZZN4arma16arma_stream_er
   
127: 000000000020d7c8     8 OBJECT  UNIQUE DEFAULT   25 _ZN4arma5DatumIdE3nanE
   
133: 000000000020d240     8 OBJECT  UNIQUE DEFAULT   24 _ZZN4arma16arma_stream_er
   
182: 000000000020d7c0     8 OBJECT  UNIQUE DEFAULT   25 _ZGVN4arma5DatumIdE3nanE
   
179: 000000000020d240     8 OBJECT  UNIQUE DEFAULT   24 _ZZN4arma16arma_stream_er
   
211: 000000000020d7c0     8 OBJECT  UNIQUE DEFAULT   25 _ZGVN4arma5DatumIdE3nanE
   
214: 000000000020d248     8 OBJECT  UNIQUE DEFAULT   24 _ZZN4arma16arma_stream_er
   
262: 000000000020d7c8     8 OBJECT  UNIQUE DEFAULT   25 _ZN4arma5DatumIdE3nanE


Again from the above thread, I've added this to my Makevars, and the problem disappeared!:
PKG_CXXFLAGS=-fno-gnu-unique

With this flag I'm now able to reload the library using devtools::load_all and all my changes take effect immediately. No more restarting R after changing C++ code.

-Mike
Reply all
Reply to author
Forward
0 new messages