kaskr / adcomp

AD computation with Template Model Builder (TMB)
Other
176 stars 80 forks source link

Difficulties with isfinite() on Windows machines #347

Open cschwarz-stat-sfu-ca opened 3 years ago

cschwarz-stat-sfu-ca commented 3 years ago

Description:

Using the isfinite() function in TMB cases compiler error on Windows but not on Macintosh when using compile() in R

Reproducible Steps:

Here is my simple.cpp file>


include

template Type objective_function::operator() () {

DATA_VECTOR( left ); // left and right values PARAMETER( log_shape );

Type test; Type test2;

test = pow(left(1)-log_shape,2);

test2 = isfinite(test);

ADREPORT (test); REPORT(test); return test2; };

Here is my R code

library(TMB) compile( "simple.cpp" ) # compile() is a function within the TMB library

dyn.load(dynlib("simple"))

simple.model <- MakeADFun( data= list(left=10.1), parameters=list(log_shape=3.1 ), DLL="simple" )

simple.model$fn(simple.model$par)

Current Output:

When running the R code on Macintosh

library(TMB) compile( "simple.cpp" ) # compile() is a function within the TMB library Note: Using Makevars in /Users/cschwarz/.R/Makevars clang++ -I"/Library/Frameworks/R.framework/Resources/include" -DNDEBUG -I"/Users/cschwarz/Rlibs/TMB/include" -I"/Users/cschwarz/Rlibs/RcppEigen/include" -DTMB_SAFEBOUNDS -DLIB_UNLOAD=R_unload_simple -DTMB_LIB_INIT=R_init_simple -I/usr/local/include -fPIC -Wall -g -O2 -c simple.cpp -o simple.o .... clang++ -dynamiclib -Wl,-headerpad_max_install_names -undefined dynamic_lookup -single_module -multiply_defined suppress -L/Library/Frameworks/R.framework/Resources/lib -L/usr/local/lib -o simple.so simple.o -F/Library/Frameworks/R.framework/.. -framework R -Wl,-framework -Wl,CoreFoundation [1] 0

i.e. a successful compilation

When running the R code on Windows it terminates with C:/Rlibs/RCPPEI~1/include/Eigen/src/Core/CoreEvaluators.h:960:8: warning: ignoring attributes on template argument 'Eigen::internal::packet_traits::type' {aka '__vector(2) double'} [-Wignored-attributes] enum { ^ make: *** [C:/PROGRA~1/R/R-41~1.0/etc/x64/Makeconf:245: simple.o] Error 1 Error in compile("simple.cpp") : Compilation failed

If you comment out the isfinite() statement it compiles fine.

TMB Version:

Mac: > packageVersion("TMB") [1] ‘1.7.20’

Windows

packageVersion("TMB") [1] ‘1.7.20’

R Version:

Mac: "R version 4.0.5 (2021-03-31)"

Windows: "R version 4.1.0 (2021-05-18)"

Operating System:

Mac: OS 11.3.1

Windows: Windows 8.1 Pro

Thanks Carl Schwarz

cschwarz-stat-sfu-ca commented 3 years ago

I'm afraid that you've lost me... I'm a Mac user and have seldom used C++ (except to write TMB functions).

I don't have a Makevars file (that I'm aware of), nor do I know how to set C++ compiler options in this make file. Can you point me in the right direction?

Can I specify this using the flags= parameter of the TMB::compile() function?

For example, I ran

library(TMB) compile( "simple.cpp", flags="-std=c++11") # compile() is a function within the TMB library

and it looks like the std=c++11 in the output below appears to be set (i'm assuming that if it appears twice in the call, it overrides the gnu++11 call??

"C:/rtools40/mingw64/bin/"g++ -std=gnu++11 -I"C:/PROGRA~1/R/R-41~1.0/include" -DNDEBUG -I"C:/Rlibs/TMB/include" -I"C:/Rlibs/RCPPEI~1/include" -DTMB_SAFEBOUNDS -DLIB_UNLOAD=R_unload_simple -DTMB_LIB_INIT=R_init_simple -std=c++11 -c simple.cpp -o simple.o In file included from C:/Rlibs/RCPPEI~1/include/Eigen/Core:397, from C:/Rlibs/RCPPEI~1/include/Eigen/Dense:1, from C:/Rlibs/TMB/include/TMB.hpp:53, from simple.cpp:1:

but this leads to the same compilation error ....

double'} [-Wignored-attributes] enum { ^ make: *** [C:/PROGRA~1/R/R-41~1.0/etc/x64/Makeconf:245: simple.o] Error 1 Error in compile("simple.cpp", flags = "-std=c++11") : Compilation failed

Thanks Carl

On Sun, May 23, 2021 at 6:34 PM xiangjiexue @.***> wrote:

Did you include "std=c++11" in your Makevars? My guess is that isfinite() is a c++11 function (see https://eigen.tuxfamily.org/dox/group__CoeffwiseMathFunctions.html). I know that with clang++, c++11 is enabled by default but I am not sure about windows distribution of gcc 8.2.0.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/kaskr/adcomp/issues/347#issuecomment-846673532, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABXIXRXELAZI6QWO7CZT3UDTPGUJLANCNFSM45JPOMOQ .