dmuck/redding-stan

Build issue with include order: no template named 'two_div_root_pi' in namespace (boost)

Closed this issue · 12 comments

On my Mac, both with C++ and clang++ I get:

rob@Rob-M1 redding-stan % make check
* bin/stanc ok
* Stan submodule ok
* Stan Math submodule ok

Check: All checks pass
rob@Rob-M1 redding-stan % bin/stanc --version
stanc3 v2.29.1 (Unix)
rob@Rob-M1 redding-stan % make examples/bernoulli/bernoulli
c++ -I src -isystem stan/src -isystem stan/lib/stan_math -isystem stan/lib/stan_math/lib/eigen_3.3.9 -isystem stan/lib/stan_math/lib/boost_1.75.0 -isystem stan/lib/stan_math/lib/sundials_6.0.0/include -isystem stan/lib/stan_math/lib/tbb_2020.3/include -std=c++14 -D_REENTRANT -O3   -c -o src/main.o src/main.cpp
In file included from src/main.cpp:7:
In file included from stan/src/stan/model/model_header.hpp:4:
In file included from stan/lib/stan_math/stan/math.hpp:19:
In file included from stan/lib/stan_math/stan/math/rev.hpp:8:
In file included from stan/lib/stan_math/stan/math/rev/core.hpp:28:
In file included from stan/lib/stan_math/stan/math/rev/core/operator_addition.hpp:10:
stan/lib/stan_math/stan/math/prim/fun/constants.hpp:169:31: error: no template named 'two_div_root_pi' in namespace 'boost::math::constants'; did you mean 'one_div_root_pi'?
    = boost::math::constants::two_div_root_pi<double>();
      ~~~~~~~~~~~~~~~~~~~~~~~~^
/usr/local/include/boost/math/constants/constants.hpp:300:30: note: 'one_div_root_pi' declared here
  BOOST_DEFINE_MATH_CONSTANT(one_div_root_pi, 5.641895835477562869480794515607725858e-01, "5.64189583547756286948079451560772585844050629328998856844085721710642468441493414486743660202107363443028347906e-01")
                             ^
1 error generated.
make: *** [src/main.o] Error 1

Thanks for reporting the issue! I'll try to piece together what went wrong based on the output... I may need a little more info.

I'll try to loop back around tonight.

I see what's going on; I'll try to figure out how to get around it.

This part of the error message clued me in: /usr/local/include/boost/math/constants/constants.hpp

Somehow, you're compilation instructions are including a different boost version than the one included in math.

I'm looking at the compiler invocation and I don't see anything obvious. I think it's some default compiler option and we've got to disable it.

Just ask!

I'm also trying to compare this with using generate_quantities(). But that I guess is definitely more limited.

dmuck commented

I'm also trying to compare this with using generate_quantities(). But that I guess is definitely more limited.

@goedman what are you comparing with generate_quantities()? Are you trying to call the generated quantities block from ReddingStan?

Not in redding-stan, using cmdstan:

stan5_1 = "
data {
    int < lower = 1 > N; // Sample size
    vector[N] D; // Outcome
    vector[N] A; // Predictor
}
parameters {
    real a; // Intercept
    real bA; // Slope (regression coefficients)
    real < lower = 0 > sigma;    // Error SD
}
transformed parameters {
    vector[N] mu;               // mu is a vector
    for (i in 1:N)
        mu[i] = a + bA * A[i];
}
model {
    a ~ normal(0, 0.2);         //Priors
    bA ~ normal(0, 0.5);
    sigma ~ exponential(1);
    D ~ normal(mu , sigma);     // Likelihood
}
generated quantities {
    vector[N] log_lik;
    for (i in 1:N)
        log_lik[i] = normal_lpdf(D[i] | mu[i], sigma);
}
";

@goedman, I think I found it. I had forgotten about this problem and I had written it up a few years ago:

https://discourse.mc-stan.org/t/clang-include-path-order/2787

For the time being, can you try these two steps in order?

  1. Build src/main.o. I replaced -isystem with -I:
c++ -I src -I stan/src -I stan/lib/stan_math -I stan/lib/stan_math/lib/eigen_3.3.9 -I stan/lib/stan_math/lib/boost_1.75.0 -I stan/lib/stan_math/lib/sundials_6.0.0/include -I stan/lib/stan_math/lib/tbb_2020.3/include -std=c++14 -D_REENTRANT -O3   -c -o src/main.o src/main.cpp
  1. Build the example you were trying:
make examples/bernoulli/bernoulli

If this works, you should be able to continue building any executable since you don't have to rebuild src/main.o. We can patch the build process to change the include flag to -I.

If it doesn't work, we'll have to keep looking for a solution.

I updated the makefile as follows:

STAN_VERSION = v2.29.1
STANCFLAGS ?= --warn-pedantic

CXXFLAGS = -I src
CXXFLAGS += -I stan/src -I stan/lib/stan_math
CXXFLAGS += -I stan/lib/stan_math/lib/eigen_3.3.9 
CXXFLAGS += -I stan/lib/stan_math/lib/boost_1.75.0
CXXFLAGS += -I stan/lib/stan_math/lib/sundials_6.0.0/include
CXXFLAGS += -I stan/lib/stan_math/lib/tbb_2020.3/include
CXXFLAGS += -std=c++14
CXXFLAGS += -D_REENTRANT
CXXFLAGS += -O3

and then I get:

rob@Rob-M1 redding-stan % make examples/bernoulli/bernoulli
bin/stanc examples/bernoulli/bernoulli.stan --o examples/bernoulli/bernoulli.cpp
c++ -I src -I stan/src -I stan/lib/stan_math -I stan/lib/stan_math/lib/eigen_3.3.9  -I stan/lib/stan_math/lib/boost_1.75.0 -I stan/lib/stan_math/lib/sundials_6.0.0/include -I stan/lib/stan_math/lib/tbb_2020.3/include -std=c++14 -D_REENTRANT -O3   -c -o examples/bernoulli/bernoulli.o examples/bernoulli/bernoulli.cpp
c++ -I src -I stan/src -I stan/lib/stan_math -I stan/lib/stan_math/lib/eigen_3.3.9  -I stan/lib/stan_math/lib/boost_1.75.0 -I stan/lib/stan_math/lib/sundials_6.0.0/include -I stan/lib/stan_math/lib/tbb_2020.3/include -std=c++14 -D_REENTRANT -O3    examples/bernoulli/bernoulli.o  src/main.o stan/lib/stan_math/lib/tbb/libtbb.dylib stan/lib/stan_math/lib/tbb/libtbbmalloc.dylib stan/lib/stan_math/lib/tbb/libtbbmalloc_proxy.dylib -Wl,-L,stan/lib/stan_math/lib/tbb -Wl,-rpath,stan/lib/stan_math/lib/tbb -o examples/bernoulli/bernoulli
rm examples/bernoulli/bernoulli.o examples/bernoulli/bernoulli.cpp
rob@Rob-M1 redding-stan % 

So I think that fixed it!!!! Will continue with the README steps.

Thanks for your help!

Yip, it works:

rob@Rob-M1 redding-stan % ./examples/bernoulli/bernoulli

ReddingStan version 1.0

ReddingStan is free software and comes with ABSOLUTELY NO WARRANTY.

Type 'help' for some help, 'list' a list of commands.

[redding]$ list

List of all commands:
  list           list all commands
  help           prints basic usage
  status         status of the current run
  N              number of parameters in the model
  load           loads data
  unload         unloads data
  eval           evaluate log probability of the model at an unconstrained parameter value
                 dropping constants; equivalent to `eval_J_true`
  eval_J_true    evaluate log probability of the model at an unconstrained parameter value
                 with Jacobian adjustment, dropping constants. Output:
                   Line 1:  log prob
                   Line 2:  gradient; length N
                   Line 3:  evaluation time (μs)
                   Line 4+: messages from the program
  eval_J_false   evaluate log probability of the model at an unconstrained parameter value
                 excluding Jacobian adjustment, dropping constants. Output format is the same
                 as `eval_J_true`.
  eval_J_only    evaluate the log absolute determinant of the Jacobian; does not include
                 a gradient. Output:
                   Line 1:  log prob
                   Line 2:  evaluation time (μs)
                   Line 3+: messages from the program
  history        prints history
  quit           quit

[redding]$ load examples/bernoulli/bernoulli.data.R
[INFO] model initialized with data from "examples/bernoulli/bernoulli.data.R"

[redding]$ eval_J_true 0.2
-8.97767
-3.59801
34
""
[redding]$ 

Same results with clang++ (I added CXX=clang++ early on in makefile above CXXFLAGS lines).

Thanks a lot Daniel!

Thanks for reporting back! If you're feeling generous, want to put those changes in a PR? If not, I'm happy to make those changes when I have a minute.

Yip, will make a PR tomorrow.

PR done. Shall I close this?