Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,18 @@ install:

before_script:
- mv Makefile.config.example Makefile.config
- export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/cuda/lib64:/usr/local/lib
- export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/lib
- export NUM_THREADS=4

script:
# CPU-GPU: build only.
- export CPU_ONLY=0
- make --keep-going --jobs=$NUM_THREADS all
- make clean
# CPU-only: comprehensive.
- export CPU_ONLY=1
- make --keep-going --jobs=$NUM_THREADS all test warn lint
- make runtestnogpu
- make runtest
- make --jobs=$NUM_THREADS all
- make --jobs=$NUM_THREADS test
- make --jobs=$NUM_THREADS warn
Expand Down
33 changes: 21 additions & 12 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -149,11 +149,13 @@ NONEMPTY_WARN_REPORT := $(BUILD_DIR)/$(WARNS_EXT)
CUDA_INCLUDE_DIR := $(CUDA_DIR)/include
CUDA_LIB_DIR := $(CUDA_DIR)/lib64 $(CUDA_DIR)/lib

INCLUDE_DIRS += $(BUILD_INCLUDE_DIR)
INCLUDE_DIRS += ./src ./include $(CUDA_INCLUDE_DIR)
LIBRARY_DIRS += $(CUDA_LIB_DIR)
LIBRARIES := cudart cublas curand \
pthread \
INCLUDE_DIRS += $(BUILD_INCLUDE_DIR) ./src ./include
ifneq ($(CPU_ONLY), 1)
INCLUDE_DIRS += $(CUDA_INCLUDE_DIR)
LIBRARY_DIRS += $(CUDA_LIB_DIR)
LIBRARIES := cudart cublas curand
endif
LIBRARIES += pthread \
glog protobuf leveldb snappy \
lmdb \
boost_system \
Expand Down Expand Up @@ -202,6 +204,16 @@ ifeq ($(LINUX), 1)
endif
endif

# CPU-only configuration
ifeq ($(CPU_ONLY), 1)
OBJS := $(PROTO_OBJS) $(CXX_OBJS)
TEST_OBJS := $(TEST_CXX_OBJS)
TEST_BINS := $(TEST_CXX_BINS)
ALL_WARNS := $(ALL_CXX_WARNS)
TEST_FILTER := --gtest_filter="-*GPU*"
COMMON_FLAGS += -DCPU_ONLY
endif

# OS X:
# clang++ instead of g++
# libstdc++ instead of libc++ for CUDA compatibility on 10.9
Expand All @@ -222,9 +234,9 @@ endif

# Debugging
ifeq ($(DEBUG), 1)
COMMON_FLAGS := -DDEBUG -g -O0
COMMON_FLAGS += -DDEBUG -g -O0
else
COMMON_FLAGS := -DNDEBUG -O2
COMMON_FLAGS += -DNDEBUG -O2
endif

# BLAS configuration (default = ATLAS)
Expand Down Expand Up @@ -282,7 +294,7 @@ SUPERCLEAN_EXTS := .so .a .o .bin .testbin .pb.cc .pb.h _pb2.py .cuo
# Define build targets
##############################
.PHONY: all test clean linecount lint tools examples $(DIST_ALIASES) \
py mat py$(PROJECT) mat$(PROJECT) proto runtest runtestnogpu \
py mat py$(PROJECT) mat$(PROJECT) proto runtest \
superclean supercleanlist supercleanfiles warn

all: $(NAME) $(STATIC_NAME) tools examples
Expand Down Expand Up @@ -342,10 +354,7 @@ $(MAT$(PROJECT)_SO): $(MAT$(PROJECT)_SRC) $(STATIC_NAME)
@ echo

runtest: $(TEST_ALL_BIN)
$(TEST_ALL_BIN) $(TEST_GPUID) --gtest_shuffle

runtestnogpu: $(TEST_ALL_BIN)
$(TEST_ALL_BIN) --gtest_shuffle --gtest_filter="-*GPU*:*/2.*:*/3.*"
$(TEST_ALL_BIN) $(TEST_GPUID) --gtest_shuffle $(TEST_FILTER)

warn: $(EMPTY_WARN_REPORT)

Expand Down
3 changes: 3 additions & 0 deletions Makefile.config.example
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
## Refer to http://caffe.berkeleyvision.org/installation.html
# Contributions simplifying and improving our build system are welcome!

# CPU-only switch (uncomment to build without GPU support).
# CPU_ONLY := 1

# To customize your choice of compiler, uncomment and set the following.
# N.B. the default for Linux is g++ and the default for OSX is clang++
# CUSTOM_CXX := g++
Expand Down
17 changes: 13 additions & 4 deletions include/caffe/common.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,10 @@
#define CAFFE_COMMON_HPP_

#include <boost/shared_ptr.hpp>
#include <cublas_v2.h>
#include <cuda.h>
#include <curand.h>
#include <driver_types.h> // cuda driver types
#include <glog/logging.h>

#include "caffe/util/device_alternate.hpp"

// Disable the copy and assignment operator for a class.
#define DISABLE_COPY_AND_ASSIGN(classname) \
private:\
Expand All @@ -25,6 +23,8 @@ private:\
// is executed we will see a fatal log.
#define NOT_IMPLEMENTED LOG(FATAL) << "Not Implemented Yet"

#ifndef CPU_ONLY

// CUDA: various checks for different function calls.
#define CUDA_CHECK(condition) \
/* Code block avoids redefinition of cudaError_t error */ \
Expand Down Expand Up @@ -56,6 +56,8 @@ private:\
// CUDA: check for error after kernel execution and exit loudly if there is one.
#define CUDA_POST_KERNEL_CHECK CUDA_CHECK(cudaPeekAtLastError())

#endif // CPU_ONLY

namespace caffe {

// We will use the boost shared_ptr instead of the new C++11 one mainly
Expand Down Expand Up @@ -99,10 +101,12 @@ class Caffe {
}
return *(Get().random_generator_);
}
#ifndef CPU_ONLY
inline static cublasHandle_t cublas_handle() { return Get().cublas_handle_; }
inline static curandGenerator_t curand_generator() {
return Get().curand_generator_;
}
#endif

// Returns the mode: running on CPU or GPU.
inline static Brew mode() { return Get().mode_; }
Expand All @@ -125,8 +129,10 @@ class Caffe {
static void DeviceQuery();

protected:
#ifndef CPU_ONLY
cublasHandle_t cublas_handle_;
curandGenerator_t curand_generator_;
#endif
shared_ptr<RNG> random_generator_;

Brew mode_;
Expand All @@ -140,6 +146,8 @@ class Caffe {
DISABLE_COPY_AND_ASSIGN(Caffe);
};

#ifndef CPU_ONLY

// NVIDIA_CUDA-5.5_Samples/common/inc/helper_cuda.h
const char* cublasGetErrorString(cublasStatus_t error);
const char* curandGetErrorString(curandStatus_t error);
Expand All @@ -158,6 +166,7 @@ inline int CAFFE_GET_BLOCKS(const int N) {
return (N + CAFFE_CUDA_NUM_THREADS - 1) / CAFFE_CUDA_NUM_THREADS;
}

#endif // CPU_ONLY

} // namespace caffe

Expand Down
1 change: 1 addition & 0 deletions include/caffe/layer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include "caffe/blob.hpp"
#include "caffe/common.hpp"
#include "caffe/proto/caffe.pb.h"
#include "caffe/util/device_alternate.hpp"

using std::string;
using std::vector;
Expand Down
9 changes: 8 additions & 1 deletion include/caffe/test/test_caffe_main.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
#ifndef CAFFE_TEST_TEST_CAFFE_MAIN_HPP_
#define CAFFE_TEST_TEST_CAFFE_MAIN_HPP_

#include <cuda_runtime.h>
#include <glog/logging.h>
#include <gtest/gtest.h>

Expand Down Expand Up @@ -44,6 +43,12 @@ struct DoubleCPU {
static const Caffe::Brew device = Caffe::CPU;
};

#ifdef CPU_ONLY

typedef ::testing::Types<FloatCPU, DoubleCPU> TestDtypesAndDevices;

#else

struct FloatGPU {
typedef float Dtype;
static const Caffe::Brew device = Caffe::GPU;
Expand All @@ -57,6 +62,8 @@ struct DoubleGPU {
typedef ::testing::Types<FloatCPU, DoubleCPU, FloatGPU, DoubleGPU>
TestDtypesAndDevices;

#endif

} // namespace caffe

#endif // CAFFE_TEST_TEST_CAFFE_MAIN_HPP_
5 changes: 4 additions & 1 deletion include/caffe/util/benchmark.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
#define CAFFE_UTIL_BENCHMARK_H_

#include <boost/date_time/posix_time/posix_time.hpp>
#include <cuda_runtime.h>

#include "caffe/util/device_alternate.hpp"

namespace caffe {

Expand All @@ -27,8 +28,10 @@ class Timer {
bool initted_;
bool running_;
bool has_run_at_least_once_;
#ifndef CPU_ONLY
cudaEvent_t start_gpu_;
cudaEvent_t stop_gpu_;
#endif
boost::posix_time::ptime start_cpu_;
boost::posix_time::ptime stop_cpu_;
float elapsed_milliseconds_;
Expand Down
44 changes: 44 additions & 0 deletions include/caffe/util/device_alternate.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// Copyright 2014 BVLC and contributors.

#ifndef CAFFE_UTIL_DEVICE_ALTERNATE_H_
#define CAFFE_UTIL_DEVICE_ALTERNATE_H_

#ifdef CPU_ONLY // CPU-only Caffe.

#include <vector>

// Stub out GPU calls as unavailable.

#define NO_GPU LOG(FATAL) << "CPU-only Mode"

#define STUB_GPU(classname) \
template <typename Dtype> \
Dtype classname<Dtype>::Forward_gpu(const vector<Blob<Dtype>*>& bottom, \
vector<Blob<Dtype>*>* top) { NO_GPU; } \
template <typename Dtype> \
void classname<Dtype>::Backward_gpu(const vector<Blob<Dtype>*>& top, \
const vector<bool>& propagate_down, \
vector<Blob<Dtype>*>* bottom) { NO_GPU; } \

#define STUB_GPU_FORWARD(classname, funcname) \
template <typename Dtype> \
Dtype classname<Dtype>::funcname##_##gpu(const vector<Blob<Dtype>*>& bottom, \
vector<Blob<Dtype>*>* top) { NO_GPU; } \

#define STUB_GPU_BACKWARD(classname, funcname) \
template <typename Dtype> \
void classname<Dtype>::funcname##_##gpu(const vector<Blob<Dtype>*>& top, \
const vector<bool>& propagate_down, \
vector<Blob<Dtype>*>* bottom) { NO_GPU; } \

#else // Normal GPU + CPU Caffe.

#include <cublas_v2.h>
#include <cuda.h>
#include <cuda_runtime.h>
#include <curand.h>
#include <driver_types.h> // cuda driver types

#endif // CPU_ONLY

#endif // CAFFE_UTIL_DEVICE_ALTERNATE_H_
Loading