clang / g++ on my Debian laptop

252 views
Skip to first unread message

Ben Goodrich

unread,
Dec 14, 2011, 1:29:56 PM12/14/11
to stan development mailing list
For a long time, I could build stan with g++ but not clang. This is
still the case now that I have clang 3.0. The error, after doing make
clean and make all, is eventually:

clang++ -O3 -Wall -g -DNDEBUG -I src -I lib -std=gnu++0x -I lib/
gtest/include -I lib/gtest -DGTEST_HAS_PTHREAD=0 -lpthread src/test/
agrad/agrad_eigen_test.cpp lib/gtest/src/gtest_main.cc ar/libgtest.a -
o test/agrad/agrad_eigen
In file included from src/test/agrad/agrad_eigen_test.cpp:8:
In file included from src/stan/agrad/matrix.hpp:6:
In file included from lib/Eigen/Dense:1:
In file included from lib/Eigen/Core:99:
In file included from /usr/lib/gcc/x86_64-linux-gnu/4.6/include/
emmintrin.h:36:
/usr/lib/gcc/x86_64-linux-gnu/4.6/include/xmmintrin.h:102:19: error:
use of undeclared identifier '__builtin_ia32_addss'
return (__m128) __builtin_ia32_addss ((__v4sf)__A, (__v4sf)__B);

Such errors are apparently due to a GCC-ism, see:

http://clang.llvm.org/compatibility.html#vector_builtins

which also includes a conversion script. But it is not clear whether
stan will still build with g++ if we convert so that stan will build
(for me) with clang. Also unclear is why stan builds for some of you
with Macs under either g++ or clang.

Ben

Bob Carpenter

unread,
Dec 14, 2011, 1:45:55 PM12/14/11
to stan...@googlegroups.com
On 12/14/11 1:29 PM, Ben Goodrich wrote:
> For a long time, I could build stan with g++ but not clang. This is
> still the case now that I have clang 3.0.

Thanks for letting us know.

Can you do anything with Eigen in Clang? I'm wondering
how much of this is our fault versus how much is
intrinsic to Eigen.

Although it's not ideal, we'd be OK with requiring
g++ to build. Mainly because R uses g++ and running
through R is a non-negotiable requirement.

We need to get a linux machine over here and
start getting to the bottom of the cross-platform
build issues.

This is well beyond my knowledge of the vagaries of
C++ compilers. I'm guessing the Mac compilers have
a different set of __builtin functions.

Can you build other things using Eigen in Clang?

- Bob

Daniel Lee

unread,
Dec 14, 2011, 2:43:34 PM12/14/11
to stan...@googlegroups.com
Ben, this may be a problem with my update to the makefile. Can you try
without this flag? -std=gnu++0x

It was only meant to be included when the compiler is g++. It slipped
my mind that clang++ contains g++ (duh). I'll get a fix back up in a
few mins.


Daniel

Ben Goodrich

unread,
Dec 14, 2011, 7:01:16 PM12/14/11
to stan development mailing list
On Dec 14, 1:45 pm, Bob Carpenter <c...@alias-i.com> wrote:
> Can you build other things using Eigen in Clang?

Not at the moment for me personally, although

http://eigen.tuxfamily.org/index.php?title=Main_Page#Compiler_support

unequivocally claims recent versions of clang are supported.

Ben

Ben Goodrich

unread,
Dec 14, 2011, 7:08:45 PM12/14/11
to stan development mailing list
On Dec 14, 2:43 pm, Daniel Lee <bear...@alum.mit.edu> wrote:
> Ben, this may be a problem with my update to the makefile. Can you try
> without this flag? -std=gnu++0x
>
> It was only meant to be included when the compiler is g++. It slipped
> my mind that clang++ contains g++ (duh). I'll get a fix back up in a
> few mins.

I think that is unrelated, or at least it didn't make any difference
when I tried it just now.

Also, setting -DGTEST_HAS_PTHREAD=1 doesn't seem to do anything. I
still have to add -lpthread to the compiler flags to get it to build
with g++.

Also, there are a number of warnings when compiling with g++, most of
which seem minor (comparison between signed and unsigned integers
mostly).

Ben

Bob Carpenter

unread,
Dec 15, 2011, 2:45:02 PM12/15/11
to stan...@googlegroups.com
On 12/14/11 7:01 PM, Ben Goodrich wrote:
> On Dec 14, 1:45 pm, Bob Carpenter<c...@alias-i.com> wrote:
>> Can you build other things using Eigen in Clang?
>
> Not at the moment for me personally, although
>
> http://eigen.tuxfamily.org/index.php?title=Main_Page#Compiler_support
>
> unequivocally claims recent versions of clang are supported.

That's encouraging, though their mailing list after
new releases tends to get clogged with incompatibilities.
They mostly seem to revolve around the same kind of low-level
platform-specific builtins you were seeing. Especially around
their fixed-size and aligned structures.

On 12/14/11 7:08 PM, Ben Goodrich wrote:
> Also, setting -DGTEST_HAS_PTHREAD=1 doesn't seem to do anything. I
> still have to add -lpthread to the compiler flags to get it to build
> with g++.

We'll try to get to the bottom of the -lpthread
dependencies. This is probably also related to some
of the ops we're using in Eigen. What I'll do
is create something independent of Stan that touches
all the Eigen ops we use with doubles and see if
that brings up the same issues.

We might wind up having to do platform-specific makes.

> Also, there are a number of warnings when compiling with g++, most of
> which seem minor (comparison between signed and unsigned integers
> mostly).

We'll clean up all the compiler errors like
unsigned ints. Clang generates some erroneous
function- and constant-not-used errors with things
defined in anonymous namespaces (it must be confusing
the per-file mangling to generate an actual namespace).

- Bob

Ben Goodrich

unread,
Jan 13, 2012, 10:01:23 PM1/13/12
to stan development mailing list
The problem of me not being able to build stan on Debian with clang
can (almost) be solved by installing the development files for clang.
Linux distributions are in the habit of splitting up libraries so that
people can install only the parts that they need. In my case, I needed
to do

apt-get install libeigen-dev

so that the proper *mmintrin.h files will be found before falling back
to the problematic ones in g++.

I added a sentence to this effect in README.txt .

However, it then could not find uintptr_t, so I added

#include<stdint.h>

to src/stan/memory/stack_alloc.hpp , which was actually mixed up in
the previous commit that added another parameterization of the beta
density (I hate subversion).

At that point, with clang++, make demo/gm works. However, make all
segfaults consistently on one of the tests

test/agrad/error_handling --gtest_output="xml:test/agrad/
error_handling.xml"
Running main() from gtest_main.cc
[==========] Running 15 tests from 1 test case.
[----------] Global test environment set-up.
[----------] 15 tests from AgradDistributionsErrorHandling
[ RUN ] AgradDistributionsErrorHandling.CheckNotNanDefaultPolicy
[ OK ] AgradDistributionsErrorHandling.CheckNotNanDefaultPolicy
(0 ms)
[ RUN ] AgradDistributionsErrorHandling.CheckNotNanErrnoPolicy
Segmentation fault
make: *** [test/agrad/error_handling] Error 139

I don't know much about this Policy business so someone else should
have a look. On a related note, my implementation of the
aforementioned beta density is just

beta_ls_log(const T_y& y, const T_mu& mu, const T_theta& theta,
const Policy& = Policy()) {
T_mu alpha = mu * theta;
T_mu beta = (1.0 - mu) * theta;
return beta_log(y, alpha, beta, Policy());
}

The compiler didn't like me just passing Policy and insisted on
passing Policy(), so maybe I am doing something wrong.

Ben

Jiqiang Guo

unread,
Jan 13, 2012, 11:01:00 PM1/13/12
to stan...@googlegroups.com
Just to talk about what I have on Ubuntu regarding clang. 

I am using an old version of Ubuntu, which does not have clang3+.  
So I have to build myself.  However, the problem for clang++ is that
it could not find the libstdc++ header files.  Even I tried to follow 
the following instruction (which I think it is a bit ambiguous) on 

6. If you intend to work on Clang C++ support, you may need to 
tell it how to find your C++ standard library headers. 
If Clang cannot find your system libstdc++ headers, 
please follow these instructions:

'gcc -v -x c++ /dev/null -fsyntax-only' to get the path.

Look for the comment "FIXME: temporary hack: hard-coded paths" 
in clang/lib/Frontend/InitHeaderSearch.cpp and change the lines 
below to include that path.

Currently, I could built clan++ luckily so that it could find 
the header file.  For example, it tells me it would find header 
files as the follow examples shows. 

$ clang++ -v a.cc 
...
clang -cc1 version 3.1 based upon LLVM 3.1svn default target x86_64-unknown-linux-gnu
ignoring nonexistent directory "/include"
#include "..." search starts here:
#include <...> search starts here:
 /opt/gcc/include/c++/4.6.3
 /opt/gcc/include/c++/4.6.3/x86_64-unknown-linux-gnu
 /opt/gcc/include/c++/4.6.3/backward
 /usr/local/include
 /opt/llvm/bin/../lib/clang/3.1/include
 /usr/include/x86_64-linux-gnu
 /usr/include
End of search list.
...

I guess *mmintrin.h is with clang itself (for me, it is under 
/opt/llvm/bin/../lib/clang/3.1/include)

When I get the correct g++ header path for clang, I can make deomo/gm 
using clang++ now with just warning sometimes. 

$ make demo/gm
clang++  -O0 -Wall -DNDEBUG -I src -I lib src/demo/gm.cpp -c -o demo/gm.o
In file included from src/demo/gm.cpp:29:
In file included from src/stan/gm/generator.hpp:34:
src/stan/io/dump.hpp:21:21: warning: function 'product' is not needed and will not be emitted [-Wunneeded-internal-declaration]
       unsigned int product(std::vector<unsigned int> dims) {
                    ^
1 warning generated.
clang++  -O0 -Wall -DNDEBUG -I src -I lib demo/gm.o -o demo/gm

But I could do the test and could not compile models.  The problem 
is like what Ben describes.  Maybe there are still other header files
folder should be included?  

--
Jiqiang 

Ben Goodrich

unread,
Jan 13, 2012, 11:15:55 PM1/13/12
to stan development mailing list
After I do

make demo/gm

I can then compile and run models. What is the error message that you
get?

On Jan 13, 11:01 pm, Jiqiang Guo <guoj...@gmail.com> wrote:
> Just to talk about what I have on Ubuntu regarding clang.
>
> I am using an old version of Ubuntu, which does not have clang3+.
> So I have to build myself.  However, the problem for clang++ is that
> it could not find the libstdc++ header files.  Even I tried to follow
> the following instruction (which I think it is a bit ambiguous) onhttp://clang.llvm.org/get_started.html

Jiqiang Guo

unread,
Jan 13, 2012, 11:32:36 PM1/13/12
to stan...@googlegroups.com
For example, see the attached output by the following
user@machine:~/Desktop/stan/src/models/bugs_examples/vol2/dugongs$ make > make.log 2>&1 
make.log

Ben Goodrich

unread,
Jan 14, 2012, 12:19:03 AM1/14/12
to stan development mailing list
If you edit the makefile to remove this:

-std=c++0x

(and also possibly change -O0 to -03 for more speed), it will compile
(with some warnings) and run.

Apparently, clang with eigen has some conformance issues with the new C
++ standard, which we should probably raise on their lists.

Also, we should probably have some standardization with these per-
example makefiles.

On Jan 13, 11:32 pm, Jiqiang Guo <guoj...@gmail.com> wrote:
> For example, see the attached output by the following
> user@machine:~/Desktop/stan/src/models/bugs_examples/vol2/dugongs$ make >
> make.log 2>&1
>
> On Fri, Jan 13, 2012 at 11:15 PM, Ben Goodrich <goodrich....@gmail.com>wrote:
>
>
>
>
>
>
>
> > After I do
>
> > make demo/gm
>
> > I can then compile and run models. What is the error message that you
> > get?
>
>
>
>  make.log
> 37KViewDownload

Ben Goodrich

unread,
Jan 14, 2012, 2:39:30 AM1/14/12
to stan development mailing list
On Jan 13, 10:01 pm, Ben Goodrich <goodrich....@gmail.com> wrote:
> At that point, with clang++, make demo/gm works. However, make all
> segfaults consistently on one of the tests
>
> test/agrad/error_handling --gtest_output="xml:test/agrad/
> error_handling.xml"
> Running main() from gtest_main.cc
> [==========] Running 15 tests from 1 test case.
> [----------] Global test environment set-up.
> [----------] 15 tests from AgradDistributionsErrorHandling
> [ RUN      ] AgradDistributionsErrorHandling.CheckNotNanDefaultPolicy
> [       OK ] AgradDistributionsErrorHandling.CheckNotNanDefaultPolicy
> (0 ms)
> [ RUN      ] AgradDistributionsErrorHandling.CheckNotNanErrnoPolicy
> Segmentation fault
> make: *** [test/agrad/error_handling] Error 139

This is due to checking whether an uninitialized var is NaN and is now
fixed in revision 530 with a

sed -i 's/var result;/var result = 0;/g' src/test/agrad/
error_handling_test.cpp

However, it raises some other issues. First, it only occurs under clang
++ and only with -03. So, it seems that what happens when a var is
uninitialized varies. Second, the error_handling_test.cpp uses
std::isnan rather than the isnan macro from boost, but I left that
alone.

But now make all works for me with clang.

Ben

Jiqiang Guo

unread,
Jan 14, 2012, 12:58:01 PM1/14/12
to stan...@googlegroups.com
Actually, I do not have -std=c++0x for the example.  
But it is under clang version 3.1 (trunk 148039). 

Since you are using clang3.0, I built a 3.0 version.  Using clang 3.0,
I could make the example and 
make through the makefile with just warnings mainly the following
(you might know):

warning: default template arguments for a function template 
are a C++11 extension [-Wc++11-extensions]
class Policy = policy<> > 

I guess Eigen has problem with clang 3.1 and under 
std=c++0x mode in clang3.0/3.1.  
But Clang 3.1 is not released; it could be buggier. 


--
Jiqiang

Bob Carpenter

unread,
Jan 14, 2012, 3:08:00 PM1/14/12
to stan...@googlegroups.com

On 1/13/12 10:01 PM, Ben Goodrich wrote:
> The problem of me not being able to build stan on Debian with clang
> can (almost) be solved by installing the development files for clang.
> Linux distributions are in the habit of splitting up libraries so that
> people can install only the parts that they need. In my case, I needed
> to do
>
> apt-get install libeigen-dev
>
> so that the proper *mmintrin.h files will be found before falling back
> to the problematic ones in g++.

Like I said, Linux users are tougher than the
rest of us.

> I added a sentence to this effect in README.txt .

Thanks.

> However, it then could not find uintptr_t, so I added
>
> #include<stdint.h>

Odd, that's supposed to be a system built-in.
But apparently it was part of the C99 additions,
so isn't built-in everywhere except stdint.h.

We need to go over all the includes. Each file
should be able to compile on its own, and right now,
they're depending on namespace-level usings and
being included in the right order.

> to src/stan/memory/stack_alloc.hpp , which was actually mixed up in
> the previous commit that added another parameterization of the beta
> density

> (I hate subversion).

Should we be using something else?
I hate changing systems once I learn one.

> At that point, with clang++, make demo/gm works. However, make all
> segfaults consistently on one of the tests
>
> test/agrad/error_handling --gtest_output="xml:test/agrad/
> error_handling.xml"
> Running main() from gtest_main.cc
> [==========] Running 15 tests from 1 test case.
> [----------] Global test environment set-up.
> [----------] 15 tests from AgradDistributionsErrorHandling
> [ RUN ] AgradDistributionsErrorHandling.CheckNotNanDefaultPolicy
> [ OK ] AgradDistributionsErrorHandling.CheckNotNanDefaultPolicy
> (0 ms)
> [ RUN ] AgradDistributionsErrorHandling.CheckNotNanErrnoPolicy
> Segmentation fault
> make: *** [test/agrad/error_handling] Error 139
>
> I don't know much about this Policy business so someone else should
> have a look.

Danie's the man for this job -- he wrote all
of our error policy stuff.

> On a related note, my implementation of the
> aforementioned beta density is just
>
> beta_ls_log(const T_y& y, const T_mu& mu, const T_theta& theta,
> const Policy& = Policy()) {
> T_mu alpha = mu * theta;
> T_mu beta = (1.0 - mu) * theta;
> return beta_log(y, alpha, beta, Policy());
> }
>
> The compiler didn't like me just passing Policy and insisted on
> passing Policy(), so maybe I am doing something wrong.

This is a tricky little pattern here. Bottom line is
that Policy is a (template) type and Policy() is a
constructed instance of type Policy.

Note that there is no variable name in that last argument.
This is intentional. We're only using Policy() for the
type inference! If we ever need an actual Policy instance,
we just construct one in the body of the function using
Policy().

This is apparently a very common pattern with policies.

Daniel went ahead and used the C++11 feature of allowing
default parameter types in function templates (they were
always allowed in class templates).

- Bob

Bob Carpenter

unread,
Jan 14, 2012, 3:13:16 PM1/14/12
to stan...@googlegroups.com
Thanks -- it'll depend on whether they
try to access a stan::agrad::var instances
vari* ptr. If they try to do anything with it,
it'll be a problem, and the behavior of using
an uninitialized pointer is not specified in
the standard.

This is also something that'll seg fault models,
which isn't good, because as I was telling Daniel,
not all of our users are like Ben and able to
pull up gdb and start tracing the error through C++.

At the cost of some potential slowdown, I can
have the behavior be that if someone tries to
access a variable that hasn't been initialized,
it can be:

A. set to 0

This is the behavior of a member variable, not the
behavior of a local variable, which is undefined
before it's initialized.

B. throw a (configurable) error

This would at least let people know why their
program crashed, though I won't be able to give
much diagnostic info on the variable in the model
as that's lost by the time you get to var.

- Bob

Ben Goodrich

unread,
Jan 14, 2012, 3:40:37 PM1/14/12
to stan development mailing list
On Jan 14, 3:08 pm, Bob Carpenter <c...@alias-i.com> wrote:
>  > (I hate subversion).
>
> Should we be using something else?
> I hate changing systems once I learn one.

I was just hating on subversion. It is probably not worth changing
right now, but for a project as complex as stan starting in late 2010
with a half-dozen people committing changes so far, it probably would
have been a better choice to go with something with more features.

> Danie's the man for this job -- he wrote all
> of our error policy stuff.

I fixed it later.

Ben

Ben Goodrich

unread,
Jan 14, 2012, 3:51:09 PM1/14/12
to stan development mailing list
I would hate to slow down the estimation of models just to avoid self-
inflicted problems with our tests. In this case,

var result;
// some stuff that doesn't affect result
if(std::isnan(result)) // segfault

So, I don't have a strong opinion about what to do in general, since
it was simple enough to initialize result here.

Ben

Bob Carpenter

unread,
Jan 14, 2012, 6:11:37 PM1/14/12
to stan...@googlegroups.com
It's not just tests -- anyone who
wrote a model like this would be in trouble:

derived parameters {
double x;
double y;

y <- x * 2; // SEG FAULT, x not defined
}

or this:

derived parameters {
double y[3];

y[0] = 2; // SEG FAULT, y[0] not defined
}

- Bob

Ben Goodrich

unread,
Jan 14, 2012, 6:37:19 PM1/14/12
to stan development mailing list
Would setting the value to 0 cause problems if the parameter were
constrained to be positive?

Bob Carpenter

unread,
Jan 14, 2012, 8:29:25 PM1/14/12
to stan...@googlegroups.com
Now that you mention it, should the test
for

double(0,1) x;

be strict or not? That is, do I use:

0 < x && x < 1

or

0 <= x && x <= 1

?

The transforms don't quite guarantee <= because
of underflow/overflow issues. And we're trying to
set up densities and derivatives to deal with limits
gracefully.

> Would setting the value to 0 cause problems if the parameter were
> constrained to be positive?

If you have

double(1,4) x;

and initialized to 0 and never reset it,
it will wind up failing tests which I'm about
to write for:

data: on read
transformed data: after transform
params: on inits, if any
transformed params: on write (NOT every leapfrog step, but I could)

Local variables can't get any constraints. (The
compiler's catching it if someone tries to declare
them as such, including cov_matrix, etc.; it's also
erroneously complaining if transformed data or transformed
params have constraints, because I wasn't going to check
for them.)

- Bob

Andrew Gelman

unread,
Jan 14, 2012, 8:33:06 PM1/14/12
to stan...@googlegroups.com
I'm reminded of the Pinocchio Principle story of how I switched from invlogit to 0.01 + 0.98*invlogit for computational stability, and then I decided that this new model was actually better. I suppose if we wanted we could even add this transformation to stan at some point. (e.g., bounded_invlogit (x, epsilon1, epsilon2) = epsilon1 + (1-epsilon1-epsilon2)*invlogit). I'm not saying we do it now, just bringing it up while it's on my mind.
Reply all
Reply to author
Forward
0 new messages