Caffe: Using c++11 by default

Created on 7 Feb 2017  ·  3Comments  ·  Source: BVLC/caffe

It's been 6 years since the release of c++11, I think it's time Caffe started using it ;)

I noticed in the source code there are some comments which suggest C++11 would preferably be enabled, but it cannot be due to NVCC incompatibilities with C++11? I tested the compilation (using CMake) of Caffe using -std=c++14 and indeed it gave issues regarding NVCC. It turns out that CMake by default propagates host flags to nvcc. If this is turned off, compilation works fine on my system and everything works as expected.

For completeness, I compiled with the following command:

cmake .. \
    -DCMAKE_BUILD_TYPE=Release \
    -DBLAS="Open" \
    -Dpython_version=3 \
    -DBoost_PYTHON_LIBRARY_RELEASE=/usr/lib/libboost_python3.so \
    -DCUDA_HOST_COMPILER=/usr/bin/g++-5 \
    -DCUDA_PROPAGATE_HOST_FLAGS=off \
    -DCMAKE_CXX_FLAGS="-std=c++14"
make -j8

Note that I compile with OpenBLAS, python3, and since I am using gcc6 I have to tell NVCC to use g++5. It is the last two options (CUDA_PROPAGATE_HOST_FLAGS and CMAKE_CXX_FLAGS) that are of importance here, but I thought I'd share the entire build process. In addition, I compiled using c++14 which worked fine. Using c++14 if available would in my opinion also be a valid option, but I figured lets discuss to enable c++11 by default first.

Also as a related side note:

  1. I think it'd be better for Caffe to start using the CMake build system by default, I feel it is far superior to the Makefile.config method.
  2. The current minimum required version according to CMakeLists.txt is 2.8.7, if this is bumped to 3.x then useful features can be used to determine the presence of c++11 etc.
  3. Features from c++11 would make the design of the feature discussed in https://github.com/BVLC/caffe/issues/5243 more clean, as it allows move semantics and other features to be used.

I would love to hear thoughts on enabling c++11 by default, but thoughts regarding a move to CMake or bumping the CMake version are also welcome (though perhaps more suitable for a separate discussion?).

ps. if approved, I'd love to make a PR for these issues.

Most helpful comment

I agree that enabling C++11 would be a good step. Note that simply adding optional C++11 support is not very useful, since it would mean that code can't assume C++11 features are available. So choosing to enable C++11 support means it becomes a requirement. That shouldn't be a problem though, considering that the standard is already 6 years old and all major compilers support it by now.

C++14 is a relatively small but still very useful improvement over C++11. Looking at compiler support [1], it is fully supported in GCC 5.0, Clang 3.4, almost fully supported in MSVC 14.2 and fully supported in MSVC 15 preview 5. The features missing from MSVC 14.2 are relatively harmless. For me that's also a +1 on skipping C++11 and going for C++14

[1] http://en.cppreference.com/w/cpp/compiler_support

I think it'd be better for Caffe to start using the CMake build system by default, I feel it is far superior to the Makefile.config method.

I completely agree. From a packager perspective, the CMake version is much much easier to use since it follows regular CMake conventions and doesn't require editing files.

The current minimum required version according to CMakeLists.txt is 2.8.7, if this is bumped to 3.x then useful features can be used to determine the presence of c++11 etc.

To be specific, the C++ standard selection features of CMake were added in 3.1 (current latest version is already 3.7). So when bumping the minimum required version, we should bump to that to have a real benefit.

All 3 comments

I agree that enabling C++11 would be a good step. Note that simply adding optional C++11 support is not very useful, since it would mean that code can't assume C++11 features are available. So choosing to enable C++11 support means it becomes a requirement. That shouldn't be a problem though, considering that the standard is already 6 years old and all major compilers support it by now.

C++14 is a relatively small but still very useful improvement over C++11. Looking at compiler support [1], it is fully supported in GCC 5.0, Clang 3.4, almost fully supported in MSVC 14.2 and fully supported in MSVC 15 preview 5. The features missing from MSVC 14.2 are relatively harmless. For me that's also a +1 on skipping C++11 and going for C++14

[1] http://en.cppreference.com/w/cpp/compiler_support

I think it'd be better for Caffe to start using the CMake build system by default, I feel it is far superior to the Makefile.config method.

I completely agree. From a packager perspective, the CMake version is much much easier to use since it follows regular CMake conventions and doesn't require editing files.

The current minimum required version according to CMakeLists.txt is 2.8.7, if this is bumped to 3.x then useful features can be used to determine the presence of c++11 etc.

To be specific, the C++ standard selection features of CMake were added in 3.1 (current latest version is already 3.7). So when bumping the minimum required version, we should bump to that to have a real benefit.

I made a PR enabling C++11 in CMake. CMake < 3.x is supported as well. As CUDA does not seem to support C++14 right now, C++11 seems to be the easiest option. It seems mixing C++14 for C++ and C++11 for CUDA is possible but looks pretty hacky.

Might be a good thing to know that OpenCV in is development branch decided to force people to use c+11 for now. If someone uses master branch of OpenCV for now with caffe master branch, this generates issues and conflicts :) Might be good to be aware of that!

Was this page helpful?
0 / 5 - 0 ratings