@shelhamer @jeffdonahue @baeuml @kloudkl @akosiorek @Yangqing @BlGene
Hello all,
hope I referenced everyone who participated cmake development (at least I didn't find others)
Following discussion here https://github.com/BVLC/caffe/pull/1586, as I promised, I prepared slightly improved caffe cmake scripts. The improvement was developed using Ubuntu 14.04 and tested on Yosemite (with libstdc++). I believe Windows support now is as difficult as compiling all dependencies. But I prefer to postpone testing on Windows until current very linux-ish build scripts and behaviour are slightly adapted for cross-platform use and some dependencies are made optional.
Description of changes and new features added
Added OpenCV like formatted configuration log
https://travis-ci.org/BVLC/caffe/jobs/45700248
Added CaffeConfig.cmake generation for build/install cases.
This allows you to connect Caffe to your application using CMake's find_package(Caffe)
. For more detailed description see below.
BUILD_SHARED_LIB=ON (default) or OFF
build caffe as shared library. In CMake it not good practice to build both shared and static simultaneously. That’s why the switch.
CPU_ONLY = OFF(default) or ON
Forces excluding CUDA support. Also Caffe will compile in CPU_ONLY
mode if CUDA Toolkit is not installed or found by cmake. Before build please read chic configuration log dumped by cmake to control this case.
USE_CUDNN = ON (default)
If enabled and cudnn
is found build with it, otherwise build without.
CUDA_ARCH_NAME=Auto(default), All, Fermi, Kepler, Maxwell, Manual
specifies target GPU architecture, Selecting concrete value reduces CUDA code compilation time (for instance compilation for sm_20 and sm_30 is twice longer than just for one of them). In case of Auto, cmake will make an attempt to detect GPUS installed in your computer and compile only for them. In case of manual, new CUDA_ARCH_BIN/PTX
cmake variables are created where space separated list of architectures should be set. Example, CUDA_ARCH_BIN=”20 21(20) 50”
BUILD_docs = ON (default)
- If doxygen installed and found enables doc target. Use
make docs
to build and make jekyll
to run web server. html docs are built in <source folder>/doxygen
, and next symlink is created in <source folder>/docs/doxygen
. Functionality from scripts/gather_examples.sh
is now implemented in cmake
, but copy_notebook.py
is still required.
- Source folder for generation is used because
.Doxyfile
contains relative paths and I prefer not to modify it now, but think generation in binary folder is better
BUILD_python = ON (default)
Build python interface if all required dependencies found, otherwise excluded from build automatically
BUILD_matlab = OFF(default)
Enables building matlab interfaces. Currently it supports both Octave and Matlab. For Octave set Octave_compiler
to mkoctfile if not found automatically. For Matlab specify Matlab_DIR
or Matlab_mex
and Matlab_mexext
if again not found automatically. If both installed and found, to select which one to use, set Matlab_build_mex_using=Matlab(default) or Octave
. Note matlab wrappers can only be built if BUILD_SHARED_LIB=On
. On macos both doesn’t compile.
Proto-files
Now protobuf files ARE NOT copied to <caffe_root>/include/caffe/proto
anymore. Instead they are generated to <build_dir>/include/caffe/proto
. Know one may include old headers, but this is interest rates to payback of technical debt appeared due to incorrect original cmake scripts design. Also removed them from .gitignore
test.testbin
- Now NO
cmake_test_defines.hpp
and sample_data_list.txt
are configured by cmake to source directory and NO -DCMAKE_BUILD
definition added and all *.in
templates were removed. This is because make runtest
command is executed in source directory, and embedding absolute paths to test cpp-files is not required! Consider configure_file() to source folder as antipattern. However, one may return such embedding by uncommenting couple lines in srcs/test/CMakeLists.txt
.
- All garbage targets (one per each test file) were removed because they flood IDEs while compilation time reduction is controversial. I replaced them with option BUILD_only_tests that allows quickly include only selected tests. Example: cmake
-DBUILD_only_tests=="common,net,blob,im2col_kernel"
Yosemite support
I was able to compile with CUDA support using the Caffe instruction with libstdc++ and patching opencv as here https://github.com/Itseez/opencv/commit/32f6e1a554dea1849ee3a53fea171cbd5969ef41. Accelerate.framework
support added. Matlab interface was failed to compile.
Temporary changes
make symlink
creates symlink [caffe_root]/build -> cmake_build_directory
- Now all examples are built without
.bin
suffix and next symlink with .bin
suffix created nearby. So that tutorials could work. Once naming standardized, should remove this.
Including Caffe in your CMake project via find_package()
git clone [email protected]:BVLC/caffe.git.
cd caffe && mkdir cmake_build && cd cmake_build
cmake .. -DBUILD_SHARED_LIB=ON
Verify that cmake found everything and in proper locations. After can run make -j 12
right now or better do this:
cmake . -DCMAKE_BUILD_TYPE=Debug # switch to debug
make -j 12 && make install # installs by default to build_dir/install
cmake . -DCMAKE_BUILD_TYPE=Release # switch to release
make -j 12 && make install # doesn’t overwrite debug install
make symlink
After the operations complete, caffe tutorials should work from caffe root directory. Let’s now see how to connect caffe to a C++ application that uses Caffe API with cmake. Prepare the following script:
cmake_minimum_required(VERSION 2.8.8)
find_package(Caffe)
include_directories(${Caffe_INCLUDE_DIRS})
add_definitions(${Caffe_DEFINITIONS}) # ex. -DCPU_ONLY
add_executable(caffeinated_application main.cpp)
target_link_libraries(caffeinated_application ${Caffe_LIBRARIES})
Run CMake to configure this application and generate build scripts or IDE project. It will automatically find Caffe in its build directory and pick up all necessarily dependencies (includes, libraries, definitions) and application will compile without any additional actions. Caffe dependencies will also have been included. If you have several Caffe builds or for some reason Cmake wasn’t able find Caffe automatically, you may specify Caffe_DIR=<path-to-caffe-build-dir>
in Cmake and this guarantees that everything will work.
Specified Caffe_DIR to build directory leads to always using a build configuration (say, Release or Debug) Caffe compiled last time for. If you set Caffe_DIR=<caffe-install-dir>/share/Caffe
where both configurations have been installed, proper debug or release caffe binaries will be selected depending on for which configuration you compile your caffeinated_application
.
Enjoy!!
(Fixed typos in CUDA architectures - @Noiredd)
compatibility focus ready for review