Skip to content

Commit

Permalink
Crosscompile-enabled CMake compilation for Caffe.
Browse files Browse the repository at this point in the history
  • Loading branch information
naibaf7 committed Jun 1, 2018
1 parent 8304865 commit 97f4f74
Show file tree
Hide file tree
Showing 24 changed files with 155 additions and 51 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,10 @@ cmake_build
build
build_opencl
build_cuda
build_arm
.build_opencl
.build_cuda
.build_arm
.build
.build_debug/*
.build_release/*
Expand Down
35 changes: 29 additions & 6 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -87,12 +87,14 @@ caffe_option(BUILD_python_layer "Build the Caffe Python layer" ON)
caffe_option(USE_OPENCV "Build with OpenCV support" ON)
caffe_option(USE_LEVELDB "Build with levelDB" ON)
caffe_option(USE_LMDB "Build with lmdb" ON)
caffe_option(USE_HDF5 "Build with hdf5" ON)
caffe_option(ALLOW_LMDB_NOLOCK "Allow MDB_NOLOCK when reading LMDB files (only if necessary)" OFF)
caffe_option(USE_OPENMP "Link with OpenMP (when your BLAS wants OpenMP and you get linker errors)" OFF)
caffe_option(USE_FFT "Build with fftw3 or/and clFFT" OFF)
caffe_option(USE_SQLITE "Build with SQLITE kernel cache" ON)
caffe_option(USE_GEMMLOWP "Build with Gemmlowp support" OFF)
caffe_option(USE_NATIVE_MARCH "Build with -march=native flag, if supported" ON)
caffe_option(USE_ARM_CROSS_COMPILE "Cross compile for Asus Tinkerboard or Raspberry Pi" OFF)

# ---[ Option to force a persistent storage path
set(CAFFE_STORAGE_PATH_OVERRIDE "" CACHE STRING "Caffe persistent storage path override (for sqlite cache)")
Expand All @@ -105,8 +107,32 @@ if(CPU_ONLY)
set(USE_LIBDNN OFF)
set(USE_CLBLAS OFF)
set(USE_CLBLAST OFF)
set(USE_HSA OFF)
set(USE_HIP OFF)
endif()

# ---[ Cross compile for ARM (Asus Tinkerboard, Rasperry Pi)
if(USE_ARM_CROSS_COMPILE)
set(CMAKE_CROSSCOMPILING TRUE)
set(CMAKE_PREFIX_PATH "${CMAKE_FIND_ROOT_PATH}")
set(CMAKE_SYSROOT "${CMAKE_FIND_ROOT_PATH}")
set(CMAKE_SYSROOT_COMPILE "${CMAKE_FIND_ROOT_PATH}")
set(CMAKE_CXX_IMPLICIT_INCLUDE_DIRECTORIES "${CMAKE_FIND_ROOT_PATH}/usr/include")
set(CMAKE_C_IMPLICIT_INCLUDE_DIRECTORIES "${CMAKE_FIND_ROOT_PATH}/usr/include")
set(CMAKE_CUDA_IMPLICIT_INCLUDE_DIRECTORIES "${CMAKE_FIND_ROOT_PATH}/usr/include")
set(CMAKE_INSTALL_OLDINCLUDEDIR "${CMAKE_FIND_ROOT_PATH}/usr/include" CACHE STRING "Header files" FORCE)
set(USE_HDF5 OFF CACHE BOOL "Build with hdf5" FORCE)
set(BOOST_INCLUDEDIR "${CMAKE_FIND_ROOT_PATH}/usr/include" CACHE STRING "Boost inculde path")
set(BOOST_LIBRARYDIR "${CMAKE_FIND_ROOT_PATH}/usr/lib/arm-linux-gnueabihf" CACHE STRING "Caffe logical version")
set_property(GLOBAL PROPERTY TARGET_SUPPORTS_SHARED_LIBS TRUE)
unset(COMPILER_SUPPORTS_ARMV7ANEON CACHE)
CHECK_CXX_COMPILER_FLAG("-march=armv7-a" COMPILER_SUPPORTS_ARMV7ANEON)
if(COMPILER_SUPPORTS_ARMV7ANEON)
message("Using armv7a-neon target")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -march=armv7-a")
endif()
endif()

if(USE_ISAAC)
set(USE_CLBLAS ON)
endif()
Expand All @@ -132,7 +158,6 @@ endif()
include(cmake/WindowsDownloadPrebuiltDependencies.cmake)

# This code is taken from https://github.com/sh1r0/caffe-android-lib
caffe_option(USE_HDF5 "Build with hdf5" ON)
if(ANDROID)
set(CMAKE_FIND_LIBRARY_SUFFIXES ".a;.so")
caffe_enable_cpp11_support()
Expand All @@ -147,21 +172,19 @@ if(DISABLE_DEVICE_HOST_UNIFIED_MEMORY)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DDISABLE_DEVICE_HOST_UNIFIED_MEMORY")
endif()

if(UNIX OR APPLE)
if(UNIX OR APPLE OR USE_ARM_CROSS_COMPILE)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fPIC -Wall -std=c++11 -DCMAKE_BUILD")
endif()

if(USE_NATIVE_MARCH)
unset(COMPILER_SUPPORTS_MARCH_NATIVE CACHE)
CHECK_CXX_COMPILER_FLAG("-march=native" COMPILER_SUPPORTS_MARCH_NATIVE)
if(COMPILER_SUPPORTS_MARCH_NATIVE)
message("Using native target")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -march=native")
endif()
endif()

if(DISABLE_DOUBLE_SUPPORT)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DDISABLE_DOUBLE_SUPPORT")
endif()

if(MSVC)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /MP")
endif()
Expand Down
14 changes: 6 additions & 8 deletions cmake/Dependencies.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,6 @@ else()
endif()
endif()

include_directories(SYSTEM ${HDF5_INCLUDE_DIRS} ${HDF5_HL_INCLUDE_DIR})
list(APPEND Caffe_LINKER_LIBS PUBLIC ${HDF5_LIBRARIES} ${HDF5_HL_LIBRARIES})
list(APPEND Caffe_INCLUDE_DIRS PUBLIC ${HDF5_INCLUDE_DIRS})

# This code is taken from https://github.com/sh1r0/caffe-android-lib
if(USE_HDF5)
find_package(HDF5 COMPONENTS HL REQUIRED)
Expand Down Expand Up @@ -242,7 +238,7 @@ endif()

# ---[ clBLAS
if (USE_CLBLAS AND NOT USE_ISAAC)
find_package(clBLAS)
find_package(clBLAS REQUIRED)
if (NOT CLBLAS_FOUND)
message(FATAL_ERROR "clBLAS required but not found.")
endif()
Expand All @@ -253,7 +249,7 @@ endif()

# ---[ ISAAC
if (USE_ISAAC)
find_package(ISAAC)
find_package(ISAAC REQUIRED)
if (NOT ISAAC_FOUND)
message(FATAL_ERROR "ISAAC required but not found.")
endif()
Expand All @@ -264,8 +260,10 @@ endif()
# ---[ CLBlast
if (USE_CLBLAST)
find_package(CLBlast REQUIRED)
message(STATUS "CLBlast found")
list(APPEND Caffe_LINKER_LIBS PUBLIC clblast)
if (NOT CLBLAST_FOUND)
message(FATAL_ERROR "CLBlast required but not found.")
endif()
list(APPEND Caffe_LINKER_LIBS PUBLIC ${CLBLAST_LIBRARY})
list(APPEND Caffe_DEFINITIONS PUBLIC -DUSE_CLBLAST)
endif()

Expand Down
60 changes: 60 additions & 0 deletions cmake/Modules/FindCLBlast.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
set(CLBLAST_INCLUDE_SEARCH_PATHS
/usr/include
/usr/local/include
/usr/local/include/clblast
/usr/local/include
clblast
CLBlast
..
../clblast
../CLBlast
)

set(CLBLAST_LIBRARY_SEARCH_PATHS
/lib/
/lib64/
/usr/lib
/usr/lib/clblast
/usr/lib64
/usr/local/lib
/usr/local/lib64
clblast/build
CLBlast/build
..
../clblast/build
../CLBlast/build
)

find_path(CLBLAST_INCLUDE_DIR NAMES clblast.h PATHS ${CLBLAST_INCLUDE_SEARCH_PATHS})
find_library(CLBLAST_LIBRARY NAMES clblast PATHS ${CLBLAST_LIBRARY_SEARCH_PATHS})

set(CLBLAST_FOUND ON)

# Check include files
if(NOT CLBLAST_INCLUDE_DIR)
set(CLBLAST_FOUND OFF)
message(STATUS "Could not find CLBLAST include. Turning CLBLAST_FOUND off")
endif()

# Check libraries
if(NOT CLBLAST_LIBRARY)
set(CLBLAST_FOUND OFF)
message(STATUS "Could not find CLBLAST lib. Turning CLBLAST_FOUND off")
endif()

if (CLBLAST_FOUND)
if (NOT CLBLAST_FIND_QUIETLY)
message(STATUS "Found CLBLAST libraries: ${CLBLAST_LIBRARY}")
message(STATUS "Found CLBLAST include: ${CLBLAST_INCLUDE_DIR}")
endif (NOT CLBLAST_FIND_QUIETLY)
else (CLBLAST_FOUND)
if (CLBLAST_FIND_REQUIRED)
message(FATAL_ERROR "Could not find CLBLAST")
endif (CLBLAST_FIND_REQUIRED)
endif (CLBLAST_FOUND)

mark_as_advanced(
CLBLAST_INCLUDE_DIR
CLBLAST_LIBRARY
CLBLAST
)
2 changes: 1 addition & 1 deletion cmake/Modules/FindGFlags.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ set(GFLAGS_ROOT_DIR "" CACHE PATH "Folder contains Gflags")

# We are testing only a couple of files in the include directories
find_path(GFLAGS_INCLUDE_DIR gflags/gflags.h
PATHS ${GFLAGS_ROOT_DIR})
PATHS ${GFLAGS_ROOT_DIR} /usr/include)

if(MSVC)
# rely on gflags-config.cmake
Expand Down
2 changes: 1 addition & 1 deletion cmake/Modules/FindGlog.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ include(FindPackageHandleStandardArgs)
set(GLOG_ROOT_DIR "" CACHE PATH "Folder contains Google glog")

find_path(GLOG_INCLUDE_DIR glog/logging.h
PATHS ${GLOG_ROOT_DIR})
PATHS ${GLOG_ROOT_DIR} /usr/include)

if(MSVC)
# rely on glog-config.cmake
Expand Down
4 changes: 2 additions & 2 deletions cmake/Modules/FindLMDB.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
if(MSVC)
find_package(LMDB NO_MODULE)
else()
find_path(LMDB_INCLUDE_DIR NAMES lmdb.h PATHS "$ENV{LMDB_DIR}/include")
find_library(LMDB_LIBRARIES NAMES lmdb PATHS "$ENV{LMDB_DIR}/lib" )
find_path(LMDB_INCLUDE_DIR NAMES lmdb.h PATHS "$ENV{LMDB_DIR}/include" "/usr/include")
find_library(LMDB_LIBRARIES NAMES lmdb PATHS "$ENV{LMDB_DIR}/lib" "/usr/lib" "/usr/lib64")
endif()

include(FindPackageHandleStandardArgs)
Expand Down
6 changes: 3 additions & 3 deletions cmake/Modules/FindLevelDB.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ if(MSVC)
set(LevelDB_LIBRARY ${LevelDB_LIBRARIES})
else()
find_path(LevelDB_INCLUDE NAMES leveldb/db.h
PATHS $ENV{LEVELDB_ROOT}/include /opt/local/include /usr/local/include /usr/include
PATHS "$ENV{LEVELDB_ROOT}/include" "/opt/local/include" "/usr/local/include" "/usr/include"
DOC "Path in which the file leveldb/db.h is located." )

# Look for the library.
find_library(LevelDB_LIBRARY NAMES leveldb
PATHS /usr/lib $ENV{LEVELDB_ROOT}/lib
DOC "Path to leveldb library." )
PATHS "$ENV{LEVELDB_ROOT}/lib" "/usr/lib" "/usr/lib64"
DOC "Path to leveldb library." )
endif()

include(FindPackageHandleStandardArgs)
Expand Down
2 changes: 1 addition & 1 deletion cmake/Modules/FindProtobuf.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ endif()
# Find the include directory
find_path(PROTOBUF_INCLUDE_DIR
google/protobuf/service.h
PATHS ${PROTOBUF_SRC_ROOT_FOLDER}/src
PATHS ${PROTOBUF_SRC_ROOT_FOLDER}/src /usr/include
)
mark_as_advanced(PROTOBUF_INCLUDE_DIR)

Expand Down
4 changes: 2 additions & 2 deletions cmake/Modules/FindSQLite.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@
# SQLITE3_FOUND - True if sqlite found.

# Look for the header file.
FIND_PATH(SQLITE3_INCLUDE_DIR NAMES sqlite3.h sqlite.h)
FIND_PATH(SQLITE3_INCLUDE_DIR NAMES sqlite3.h sqlite.h PATHS "/usr/include")

# Look for the library.
FIND_LIBRARY(SQLITE3_LIBRARY NAMES sqlite3 sqlite)
FIND_LIBRARY(SQLITE3_LIBRARY NAMES sqlite3 sqlite PATHS "/usr/lib" "/usr/lib64")

# Handle the QUIETLY and REQUIRED arguments and set SQLITE3_FOUND to TRUE if all listed variables are TRUE.
INCLUDE(FindPackageHandleStandardArgs)
Expand Down
4 changes: 2 additions & 2 deletions cmake/Modules/FindSnappy.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ if(MSVC)
find_package(Snappy NO_MODULE)
else()
find_path(Snappy_INCLUDE_DIR NAMES snappy.h
PATHS ${SNAPPY_ROOT_DIR} ${SNAPPY_ROOT_DIR}/include)
PATHS "${SNAPPY_ROOT_DIR}" "${SNAPPY_ROOT_DIR}/include" "/usr/include")

find_library(Snappy_LIBRARIES NAMES snappy
PATHS ${SNAPPY_ROOT_DIR} ${SNAPPY_ROOT_DIR}/lib)
PATHS "${SNAPPY_ROOT_DIR}" "${SNAPPY_ROOT_DIR}/lib" "/usr/lib" "/usr/lib64")
endif()

include(FindPackageHandleStandardArgs)
Expand Down
5 changes: 5 additions & 0 deletions cmake/Summary.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -178,4 +178,9 @@ function(caffe_print_configuration_summary)
caffe_status("Install:")
caffe_status(" Install path : ${CMAKE_INSTALL_PREFIX}")
caffe_status("")
#get_cmake_property(_variableNames VARIABLES)
#list (SORT _variableNames)
# foreach (_variableName ${_variableNames})
# message(STATUS "${_variableName}=${${_variableName}}")
#endforeach()
endfunction()
2 changes: 1 addition & 1 deletion include/caffe/backend/hip/hip_device.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace caffe {

#ifdef USE_HIP

class hip_device : public device {
class HipDevice : public Device {
public:
protected:
private:
Expand Down
Empty file modified include/caffe/data_transformer.hpp
100644 → 100755
Empty file.
Empty file modified include/caffe/util/insert_shared_blob_indices.hpp
100644 → 100755
Empty file.
2 changes: 1 addition & 1 deletion src/caffe/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ endif()
configure_file(${caffe_export_hdr_in} ${caffe_export_hdr})

# ---[ Tests
add_subdirectory(test)
add_subdirectory(test)

# ---[ Install
install(DIRECTORY ${Caffe_INCLUDE_DIR}/caffe DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})
Expand Down
Empty file modified src/caffe/data_transformer.cpp
100644 → 100755
Empty file.
Empty file modified src/caffe/layers/data_layer.cpp
100644 → 100755
Empty file.
Empty file modified src/caffe/layers/image_data_layer.cpp
100644 → 100755
Empty file.
16 changes: 16 additions & 0 deletions src/caffe/libdnn/libdnn.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,14 @@ string LibDNN<MItype, MOtype>::generate_gemm_core(
<< " = Bsub[k][col + " << (i * rtsn)
<< "];" << std::endl;
}
if (is_integer_type<MItype>()) {
ss << "if (tidm == 0) {" << std::endl;
for (int i = 0; i < vwn; ++i) {
ss << "Bsubcols[col + " << (i * rtsn) << "] += VEC_" << vwn << "_" << i
<< "(Breg[wn]);" << std::endl;
}
ss << "}" << std::endl;
}
ss << "}" << std::endl;

// Perform the computation
Expand All @@ -63,6 +71,14 @@ string LibDNN<MItype, MOtype>::generate_gemm_core(
ss << "VEC_" << vwm << "_" << i << "(Areg)" << " = Asub[row + " << (i*rtsm)
<< "][k];" << std::endl;
}
if (is_integer_type<MItype>()) {
ss << "if (tidn == 0) {" << std::endl;
for (int i = 0; i < vwm; ++i) {
ss << "Asubrows[row + " << (i*rtsm) << "] += VEC_" << vwm << "_" << i
<< "(Areg);" << std::endl;
}
ss << "}" << std::endl;
}
if (dterm) {
if (unroll) {
for (int i = 0; i < vwm; ++i) {
Expand Down
4 changes: 2 additions & 2 deletions src/caffe/libdnn/libdnn_blas_3.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,7 @@ string LibDNNBlas<MItype, MOtype>::generate_gemm_source(
ss << this->generate_gemm_core(tuner, false, alpha_term,
alpha_exactly_one);

if (is_integer_type<MItype>()) {
/*if (is_integer_type<MItype>()) {
// Add up columns of A
ss << "for (int_tp k = 0; k < TSK; ++k) {" << std::endl;
ss << "if (tidn == 0) {" << std::endl;
Expand All @@ -356,7 +356,7 @@ string LibDNNBlas<MItype, MOtype>::generate_gemm_source(
<< std::endl;
ss << "}}" << std::endl;
ss << "}" << std::endl;
}
}*/

// Synchronize before loading the next tile
ss << program->local_barrier() << std::endl;
Expand Down
6 changes: 3 additions & 3 deletions src/caffe/libdnn/libdnn_conv.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ LibDNNConv<MItype, MOtype>::LibDNNConv(LibDNNConvConfig config)
for (int id = 0; id < 2; ++id) {
vector<int_tp> workgroup_sizes;
for (int_tp i = 0; i < this->dev_ptr_->workgroup_size(id);
i += 4) {
i += 1) {
workgroup_sizes.push_back(i);
}
fw_tuner_->add_set_param <int_tp>("workgroup_size_" + std::to_string(id),
Expand Down Expand Up @@ -956,7 +956,7 @@ string LibDNNConv<MItype, MOtype>::generate_fw_kernels(string name) {

ss << this->generate_gemm_core(fw_tuner_, false, false, false) << std::endl;

if (is_integer_type<MItype>()) {
/*if (is_integer_type<MItype>()) {
// Add up columns of A
ss << "for (int_tp k = 0; k < TSK; ++k) {" << std::endl;
ss << "if (tidn == 0) {" << std::endl;
Expand All @@ -973,7 +973,7 @@ string LibDNNConv<MItype, MOtype>::generate_fw_kernels(string name) {
<< std::endl;
ss << "}}" << std::endl;
ss << "}" << std::endl;
}
}*/

// Synchronize before loading the next tile
ss << this->program_->local_barrier() << std::endl;
Expand Down
Loading

0 comments on commit 97f4f74

Please sign in to comment.