Trying to run autodifferentiation example

I’m trying to run the simple example of Reverse-Model Auto-differentiation given here: https://arxiv.org/pdf/1509.07164.pdf.

#include <cmath>
#include <stan/math.hpp>
int main() {
        using std::pow;
        double y = 1.3;
        stan::math::var mu = 0.5, sigma = 1.2;
        stan::math::var lp = 0;
        lp -= 0.5 * log(2 * stan::math::pi());
        lp -= log(sigma);
        lp -= 0.5 * pow((y - mu) / sigma, 2);
        std::cout << "f(mu, sigma) = " << lp.val() << std::endl;
        lp.grad();
        std::cout << " d.f / d.mu = " << mu.adj()
        << " d.f / d.sigma = " << sigma.adj() << std::endl;
        return 0;
}

The article says that this is C++ code, so I’ve been trying the command line:

g++ -o normal normal.cpp

where normal.cpp is the file name of the above code. I keep getting the error:

fatal error: 'Eigen/Dense' file not found

which I find puzzling because I’m fairly sure that file is there. I noticed something about the most recent commit of that file not working with gcc…how should I go about running this example? I’ve also tried some cmdstan stuff, but I’ve had similar problems there, and ultimately I thought that wasn’t right since this isn’t really a stan model.

Thanks for writing in! There’s a little more to using the Math library; you also have to tell the compiler where the library files are. That includes Eigen.

Please take a look here: https://github.com/stan-dev/math/blob/develop/README.md#installation

It has some more details on the build process. I’ll have to warn you that using v2.20 will be much easier to use for what you’re attempting to do vs v3.0.0. We changed from being header-only to requiring linking with the latest release.

Thanks for the info! I used

clang++ -std=c++1y -I ~/stan-dev/math -I ~/stan-dev/math/lib/eigen_3.3.3/ -I ~/stan-dev/math/lib/boost_1.69.0/ -I ~/stan-dev/math/lib/sundials_4.1.0/include  -I ~/stan-dev/math/lib/tbb_2019_U8/include -L ~/stan-dev/math/lib/tbb_2019_U8/include -ltbb -Wl,-rpath,"~/stan-dev/math/lib/tbb" -D_REENTRANT normal.cpp

and I get

ld: library not found for -ltbb
clang: error: linker command failed with exit code 1 (use -v to see invocation)

I’m fairly sure all these file paths are correct, and all the relevant libraries were downloaded from the stan-dev/math github page. Do you know what that -ltbb call is for?

Here is the verbose output with the original command line:

(base) Alexanders-MBP:cmdstan murph$ clang++ -v -std=c++1y -I ~/stan-dev/math -I ~/stan-dev/math/lib/eigen_3.3.3/ -I ~/stan-dev/math/lib/boost_1.69.0/ -I ~/stan-dev/math/lib/sundials_4.1.0/include  -I ~/stan-dev/math/lib/tbb_2019_U8/include -L ~/stan-dev/math/lib/tbb -ltbb -Wl,-rpath,"~/stan-dev/math/lib/tbb" -D_REENTRANT normal.cpp
Apple LLVM version 10.0.1 (clang-1001.0.46.4)
Target: x86_64-apple-darwin18.7.0
Thread model: posix
InstalledDir: /Library/Developer/CommandLineTools/usr/bin
 "/Library/Developer/CommandLineTools/usr/bin/clang" -cc1 -triple x86_64-apple-macosx10.14.0 -Wdeprecated-objc-isa-usage -Werror=deprecated-objc-isa-usage -emit-obj -mrelax-all -disable-free -disable-llvm-verifier -discard-value-names -main-file-name normal.cpp -mrelocation-model pic -pic-level 2 -mthread-model posix -mdisable-fp-elim -fno-strict-return -masm-verbose -munwind-tables -target-sdk-version=10.14 -target-cpu penryn -dwarf-column-info -debugger-tuning=lldb -target-linker-version 450.3 -v -resource-dir /Library/Developer/CommandLineTools/usr/lib/clang/10.0.1 -isysroot /Library/Developer/CommandLineTools/SDKs/MacOSX10.14.sdk -I /Users/murph/stan-dev/math -I /Users/murph/stan-dev/math/lib/eigen_3.3.3/ -I /Users/murph/stan-dev/math/lib/boost_1.69.0/ -I /Users/murph/stan-dev/math/lib/sundials_4.1.0/include -I /Users/murph/stan-dev/math/lib/tbb_2019_U8/include -D _REENTRANT -I/usr/local/include -stdlib=libc++ -Wno-atomic-implicit-seq-cst -Wno-framework-include-private-from-public -Wno-atimport-in-framework-header -Wno-quoted-include-in-framework-header -std=c++1y -fdeprecated-macro -fdebug-compilation-dir /Users/murph/Documents/Research/EAS_GFD/cmdstan -ferror-limit 19 -fmessage-length 169 -stack-protector 1 -fblocks -fencode-extended-block-signature -fregister-global-dtors-with-atexit -fobjc-runtime=macosx-10.14.0 -fcxx-exceptions -fexceptions -fmax-type-align=16 -fdiagnostics-show-option -fcolor-diagnostics -o /var/folders/l5/b68wvnms6ddb00_8r7l0zk3r0000gn/T/normal-99e6a5.o -x c++ normal.cpp
clang -cc1 version 10.0.1 (clang-1001.0.46.4) default target x86_64-apple-darwin18.7.0
ignoring nonexistent directory "/Library/Developer/CommandLineTools/SDKs/MacOSX10.14.sdk/usr/include/c++/v1"
ignoring nonexistent directory "/Library/Developer/CommandLineTools/SDKs/MacOSX10.14.sdk/usr/local/include"
ignoring nonexistent directory "/Library/Developer/CommandLineTools/SDKs/MacOSX10.14.sdk/Library/Frameworks"
#include "..." search starts here:
#include <...> search starts here:
 /Users/murph/stan-dev/math
 /Users/murph/stan-dev/math/lib/eigen_3.3.3
 /Users/murph/stan-dev/math/lib/boost_1.69.0
 /Users/murph/stan-dev/math/lib/sundials_4.1.0/include
 /Users/murph/stan-dev/math/lib/tbb_2019_U8/include
 /usr/local/include
 /Library/Developer/CommandLineTools/usr/include/c++/v1
 /Library/Developer/CommandLineTools/usr/lib/clang/10.0.1/include
 /Library/Developer/CommandLineTools/usr/include
 /Library/Developer/CommandLineTools/SDKs/MacOSX10.14.sdk/usr/include
 /Library/Developer/CommandLineTools/SDKs/MacOSX10.14.sdk/System/Library/Frameworks (framework directory)
End of search list.
 "/Library/Developer/CommandLineTools/usr/bin/ld" -demangle -lto_library /Library/Developer/CommandLineTools/usr/lib/libLTO.dylib -no_deduplicate -dynamic -arch x86_64 -macosx_version_min 10.14.0 -syslibroot /Library/Developer/CommandLineTools/SDKs/MacOSX10.14.sdk -o a.out -L/Users/murph/stan-dev/math/lib/tbb -ltbb -rpath ~/stan-dev/math/lib/tbb /var/folders/l5/b68wvnms6ddb00_8r7l0zk3r0000gn/T/normal-99e6a5.o -L/usr/local/lib -lc++ -lSystem /Library/Developer/CommandLineTools/usr/lib/clang/10.0.1/lib/darwin/libclang_rt.osx.a
ld: warning: directory not found for option '-L/Users/murph/stan-dev/math/lib/tbb'
ld: library not found for -ltbb
clang: error: linker command failed with exit code 1 (use -v to see invocation)

Here is the verbose output when I copied the tbb direction into my math-stan path. The documentation says that this directly is normally created by the makefile, but I added it in to see if it fixed my problem (it didn’t):

(base) Alexanders-MBP:cmdstan murph$ clang++ -v -std=c++1y -I ~/stan-dev/math -I ~/stan-dev/math/lib/eigen_3.3.3/ -I ~/stan-dev/math/lib/boost_1.69.0/ -I ~/stan-dev/math/lib/sundials_4.1.0/include  -I ~/stan-dev/math/lib/tbb_2019_U8/include -L ~/stan-dev/math/lib/tbb -ltbb -Wl,-rpath,"~/stan-dev/math/lib/tbb" -D_REENTRANT normal.cpp
Apple LLVM version 10.0.1 (clang-1001.0.46.4)
Target: x86_64-apple-darwin18.7.0
Thread model: posix
InstalledDir: /Library/Developer/CommandLineTools/usr/bin
 "/Library/Developer/CommandLineTools/usr/bin/clang" -cc1 -triple x86_64-apple-macosx10.14.0 -Wdeprecated-objc-isa-usage -Werror=deprecated-objc-isa-usage -emit-obj -mrelax-all -disable-free -disable-llvm-verifier -discard-value-names -main-file-name normal.cpp -mrelocation-model pic -pic-level 2 -mthread-model posix -mdisable-fp-elim -fno-strict-return -masm-verbose -munwind-tables -target-sdk-version=10.14 -target-cpu penryn -dwarf-column-info -debugger-tuning=lldb -target-linker-version 450.3 -v -resource-dir /Library/Developer/CommandLineTools/usr/lib/clang/10.0.1 -isysroot /Library/Developer/CommandLineTools/SDKs/MacOSX10.14.sdk -I /Users/murph/stan-dev/math -I /Users/murph/stan-dev/math/lib/eigen_3.3.3/ -I /Users/murph/stan-dev/math/lib/boost_1.69.0/ -I /Users/murph/stan-dev/math/lib/sundials_4.1.0/include -I /Users/murph/stan-dev/math/lib/tbb_2019_U8/include -D _REENTRANT -I/usr/local/include -stdlib=libc++ -Wno-atomic-implicit-seq-cst -Wno-framework-include-private-from-public -Wno-atimport-in-framework-header -Wno-quoted-include-in-framework-header -std=c++1y -fdeprecated-macro -fdebug-compilation-dir /Users/murph/Documents/Research/EAS_GFD/cmdstan -ferror-limit 19 -fmessage-length 169 -stack-protector 1 -fblocks -fencode-extended-block-signature -fregister-global-dtors-with-atexit -fobjc-runtime=macosx-10.14.0 -fcxx-exceptions -fexceptions -fmax-type-align=16 -fdiagnostics-show-option -fcolor-diagnostics -o /var/folders/l5/b68wvnms6ddb00_8r7l0zk3r0000gn/T/normal-36beb2.o -x c++ normal.cpp
clang -cc1 version 10.0.1 (clang-1001.0.46.4) default target x86_64-apple-darwin18.7.0
ignoring nonexistent directory "/Library/Developer/CommandLineTools/SDKs/MacOSX10.14.sdk/usr/include/c++/v1"
ignoring nonexistent directory "/Library/Developer/CommandLineTools/SDKs/MacOSX10.14.sdk/usr/local/include"
ignoring nonexistent directory "/Library/Developer/CommandLineTools/SDKs/MacOSX10.14.sdk/Library/Frameworks"
#include "..." search starts here:
#include <...> search starts here:
 /Users/murph/stan-dev/math
 /Users/murph/stan-dev/math/lib/eigen_3.3.3
 /Users/murph/stan-dev/math/lib/boost_1.69.0
 /Users/murph/stan-dev/math/lib/sundials_4.1.0/include
 /Users/murph/stan-dev/math/lib/tbb_2019_U8/include
 /usr/local/include
 /Library/Developer/CommandLineTools/usr/include/c++/v1
 /Library/Developer/CommandLineTools/usr/lib/clang/10.0.1/include
 /Library/Developer/CommandLineTools/usr/include
 /Library/Developer/CommandLineTools/SDKs/MacOSX10.14.sdk/usr/include
 /Library/Developer/CommandLineTools/SDKs/MacOSX10.14.sdk/System/Library/Frameworks (framework directory)
End of search list.
 "/Library/Developer/CommandLineTools/usr/bin/ld" -demangle -lto_library /Library/Developer/CommandLineTools/usr/lib/libLTO.dylib -no_deduplicate -dynamic -arch x86_64 -macosx_version_min 10.14.0 -syslibroot /Library/Developer/CommandLineTools/SDKs/MacOSX10.14.sdk -o a.out -L/Users/murph/stan-dev/math/lib/tbb -ltbb -rpath ~/stan-dev/math/lib/tbb /var/folders/l5/b68wvnms6ddb00_8r7l0zk3r0000gn/T/normal-36beb2.o -L/usr/local/lib -lc++ -lSystem /Library/Developer/CommandLineTools/usr/lib/clang/10.0.1/lib/darwin/libclang_rt.osx.a
ld: library not found for -ltbb
clang: error: linker command failed with exit code 1 (use -v to see invocation)

If anyone is still listening, this is what I was missing:

brew install tbb
1 Like

Hi,

just to provide a bit more background on why you had to do this. Sorry I failed to see this before.

If you are using Stan Math directly without the Stan Math makefiles you need to build the TBB library manually. One option of doing that is your solution, others are listed on the TBB website. https://software.intel.com/en-us/articles/get-started-with-tbb