stan icon indicating copy to clipboard operation
stan copied to clipboard

Compilation errors related with stan::math::arena_matrix

Open crespofabian8012 opened this issue 3 years ago • 5 comments

Hello

I translated a stan model using stanc3(CMDSTAN 2.28.1). I included the .hpp file in Xcode(MacOSX Mojave). I have Eigen 3.4 installed, Sundials, Boost , TBB 2020.3

I am getting compilation errors:

No member named 'adj' in 'stan::math::arena_matrix<Eigen::Matrix<stan::math::var_value<double, void>, -1, 1, 0, -1, 1> >'

No member named 'val' in 'stan::math::arena_matrix<Eigen::Matrix<stan::math::var_value<double, void>, -1, 1, 0, -1, 1> >'

crespofabian8012 avatar Nov 18 '21 23:11 crespofabian8012

@crespofabian8012: Thanks for reporting. That's definitely a bug in Stan---you shouldn't ever see C++ compilation errors. Can you share the Stan program that led to this error (the shorter, the better)?

bob-carpenter avatar Nov 18 '21 23:11 bob-carpenter

Hey Fabian,

I am guessing this a continuation of this discussion.

I have Eigen 3.4 installed, Sundials, Boost , TBB 2020.3

Based on the fact you are using Eigen 3.4, I am guessing you are using your own system-installed libraries? Is there a specific need to use Eigen 3.4? Stan currently uses 3.3.9 and that is also the version we test with in our CI.

You can see which libraries are bundled with Stan here: https://github.com/stan-dev/math/tree/develop/lib If you need to use some other version of one of the dependencies, we cant guarantee that it will work.

No member named 'adj'

Errors like this typically mean that Eigen was included ahead of Stan headers. Stan Math is tightly coupled with Eigen in that we add code to the Eigen:: namespace. The adj() member function is one of those additions. The header file that must be included first is #include <stan/math/prim/fun/Eigen.hpp>. Which is also why the master header file is organized as it is: https://github.com/stan-dev/math/blob/develop/stan/math/rev.hpp

I would recommend you check out one of the top-level tests in Stan, for example https://github.com/stan-dev/stan/blob/develop/src/test/unit/services/sample/hmc_nuts_unit_e_adapt_test.cpp and try to run it and observe how the test is compiled. Then use that as a shim to compile your own C++ program (you can obviously then remove the GTest includes that are used for the test only).

I am moving this to the Stan repository, as that is where this issue should live, as its about using plain C++ Stan.

rok-cesnovar avatar Nov 19 '21 08:11 rok-cesnovar

Thanks @bob-carpenter and @rok-cesnovar. Yes, I am having the same issue with /examples/bernoulli.stan when translated with stanc. I am using Eigen 3.4, because I use some new features in Eigen 3.4 like Eigen::SeqN( for even and odd positions).

crespofabian8012 avatar Nov 19 '21 09:11 crespofabian8012

Probably the easiest workaround is to just include path_to_stan_math/stan/math/prim/fun/Eigen.hpp in the g++ compile call as the first include. Something like: g++ -I path_to_stan_math/stan/math/prim/fun/Eigen.hpp ...

There is also a branch in Stan Math update/eigen-3.4 that has Eigen 3.4, but its failing in CI.

rok-cesnovar avatar Nov 19 '21 10:11 rok-cesnovar

Until we bump to 3.4 I would not use it. There's a lot that has changed internally in eigen and we have to update a good bit for that

SteveBronder avatar Nov 19 '21 17:11 SteveBronder

fwiw, I fixed this in my project by making sure #include <stan/math.hpp> cam before any #include <Eigen/ lines

alecjacobson avatar Apr 17 '24 14:04 alecjacobson