Skip to content

Commit

Permalink
First attemp to remove boost dependecies.
Browse files Browse the repository at this point in the history
Still alive only for python wrapper.
  • Loading branch information
ste-m5s committed Jun 12, 2015
1 parent 543afd3 commit 89c3791
Show file tree
Hide file tree
Showing 28 changed files with 153 additions and 125 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ include(cmake/Dependencies.cmake)

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

if(USE_libstdcpp)
Expand Down
16 changes: 9 additions & 7 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ ifneq ($(CPU_ONLY), 1)
LIBRARIES := cudart cublas curand
endif
LIBRARIES += glog gflags protobuf leveldb snappy \
lmdb boost_system hdf5_hl hdf5 m \
lmdb hdf5_hl hdf5 m \
opencv_core opencv_highgui opencv_imgproc
PYTHON_LIBRARIES := boost_python python2.7
WARNINGS := -Wall -Wno-sign-compare
Expand Down Expand Up @@ -233,7 +233,7 @@ ifeq ($(LINUX), 1)
endif
# boost::thread is reasonably called boost_thread (compare OS X)
# We will also explicitly add stdc++ to the link target.
LIBRARIES += boost_thread stdc++
LIBRARIES += stdc++
endif

# OS X:
Expand All @@ -253,7 +253,7 @@ ifeq ($(OSX), 1)
# gtest needs to use its own tuple to not conflict with clang
COMMON_FLAGS += -DGTEST_USE_OWN_TR1_TUPLE=1
# boost::thread is called boost_thread-mt to mark multithreading on OS X
LIBRARIES += boost_thread-mt
#LIBRARIES += boost_thread-mt
# we need to explicitly ask for the rpath to be obeyed
DYNAMIC_FLAGS := -install_name @rpath/libcaffe.so
ORIGIN := @loader_path
Expand Down Expand Up @@ -344,16 +344,18 @@ LIBRARY_DIRS += $(BLAS_LIB)

LIBRARY_DIRS += $(LIB_BUILD_DIR)

INCLUDE_DIRS += /usr/include/hdf5/serial

# Automatic dependency generation (nvcc is handled separately)
CXXFLAGS += -MMD -MP

# Complete build flags.
COMMON_FLAGS += $(foreach includedir,$(INCLUDE_DIRS),-I$(includedir))
CXXFLAGS += -pthread -fPIC $(COMMON_FLAGS) $(WARNINGS)
NVCCFLAGS += -ccbin=$(CXX) -Xcompiler -fPIC $(COMMON_FLAGS)
CXXFLAGS += -pthread -fPIC -std=c++11 $(COMMON_FLAGS) $(WARNINGS)
NVCCFLAGS += -ccbin=$(CXX) -Xcompiler -fPIC -std=c++11 $(COMMON_FLAGS)
# mex may invoke an older gcc that is too liberal with -Wuninitalized
MATLAB_CXXFLAGS := $(CXXFLAGS) -Wno-uninitialized
LINKFLAGS += -pthread -fPIC $(COMMON_FLAGS) $(WARNINGS)
LINKFLAGS += -pthread -fPIC -std=c++11 $(COMMON_FLAGS) $(WARNINGS)

USE_PKG_CONFIG ?= 0
ifeq ($(USE_PKG_CONFIG), 1)
Expand Down Expand Up @@ -441,7 +443,7 @@ py: $(PY$(PROJECT)_SO) $(PROTO_GEN_PY)

$(PY$(PROJECT)_SO): $(PY$(PROJECT)_SRC) $(PY$(PROJECT)_HXX) | $(DYNAMIC_NAME)
@ echo CXX/LD -o $@ $<
$(Q)$(CXX) -shared -o $@ $(PY$(PROJECT)_SRC) \
$(Q)$(CXX) -std=c++11 -shared -o $@ $(PY$(PROJECT)_SRC) \
-o $@ $(LINKFLAGS) -l$(PROJECT) $(PYTHON_LDFLAGS) \
-Wl,-rpath,$(ORIGIN)/../../build/lib

Expand Down
6 changes: 3 additions & 3 deletions cmake/Dependencies.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
set(Caffe_LINKER_LIBS "")

# ---[ Boost
find_package(Boost 1.46 REQUIRED COMPONENTS system thread)
include_directories(SYSTEM ${Boost_INCLUDE_DIR})
list(APPEND Caffe_LINKER_LIBS ${Boost_LIBRARIES})
#find_package(Boost 1.46 REQUIRED COMPONENTS system thread)
#include_directories(SYSTEM ${Boost_INCLUDE_DIR})
#list(APPEND Caffe_LINKER_LIBS ${Boost_LIBRARIES})

# ---[ Threads
find_package(Threads REQUIRED)
Expand Down
10 changes: 5 additions & 5 deletions examples/cifar10/convert_cifar_data.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
// http://www.cs.toronto.edu/~kriz/cifar.html

#include <fstream> // NOLINT(readability/streams)
#include <memory>
#include <string>

#include "boost/scoped_ptr.hpp"
#include "glog/logging.h"
#include "google/protobuf/text_format.h"
#include "stdint.h"
Expand All @@ -18,7 +18,7 @@
#include "caffe/util/db.hpp"

using caffe::Datum;
using boost::scoped_ptr;
using std::unique_ptr;
using std::string;
namespace db = caffe::db;

Expand All @@ -37,9 +37,9 @@ void read_image(std::ifstream* file, int* label, char* buffer) {

void convert_dataset(const string& input_folder, const string& output_folder,
const string& db_type) {
scoped_ptr<db::DB> train_db(db::GetDB(db_type));
unique_ptr<db::DB> train_db(db::GetDB(db_type));
train_db->Open(output_folder + "/cifar10_train_" + db_type, db::NEW);
scoped_ptr<db::Transaction> txn(train_db->NewTransaction());
unique_ptr<db::Transaction> txn(train_db->NewTransaction());
// Data buffer
int label;
char str_buffer[kCIFARImageNBytes];
Expand Down Expand Up @@ -71,7 +71,7 @@ void convert_dataset(const string& input_folder, const string& output_folder,
train_db->Close();

LOG(INFO) << "Writing Testing data";
scoped_ptr<db::DB> test_db(db::GetDB(db_type));
unique_ptr<db::DB> test_db(db::GetDB(db_type));
test_db->Open(output_folder + "/cifar10_test_" + db_type, db::NEW);
txn.reset(test_db->NewTransaction());
// Open files
Expand Down
7 changes: 6 additions & 1 deletion examples/cpp_classification/classification.cpp
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
#include <caffe/caffe.hpp>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>

#include <caffe/caffe.hpp>

#include <iomanip>
#include <iosfwd>
#include <memory>
#include <string>
#include <utility>
#include <vector>



using namespace caffe; // NOLINT(build/namespaces)
using std::string;

Expand Down
1 change: 0 additions & 1 deletion include/caffe/blob.hpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#ifndef CAFFE_BLOB_HPP_
#define CAFFE_BLOB_HPP_

#include <algorithm>
#include <string>
#include <vector>

Expand Down
15 changes: 7 additions & 8 deletions include/caffe/common.hpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#ifndef CAFFE_COMMON_HPP_
#define CAFFE_COMMON_HPP_

#include <boost/shared_ptr.hpp>
#include <gflags/gflags.h>
#include <glog/logging.h>

Expand All @@ -10,6 +9,7 @@
#include <fstream> // NOLINT(readability/streams)
#include <iostream> // NOLINT(readability/streams)
#include <map>
#include <memory>
#include <set>
#include <sstream>
#include <string>
Expand Down Expand Up @@ -70,9 +70,8 @@ namespace cv { class Mat; }

namespace caffe {

// We will use the boost shared_ptr instead of the new C++11 one mainly
// because cuda does not work (at least now) well with C++11 features.
using boost::shared_ptr;

using std::shared_ptr;

// Common functions and classes from std that caffe often uses.
using std::fstream;
Expand Down Expand Up @@ -106,7 +105,7 @@ class Caffe {
}
enum Brew { CPU, GPU };

// This random number generator facade hides boost and CUDA rng
// This random number generator facade hides std and CUDA rng
// implementation from one another (for cross-platform compatibility).
class RNG {
public:
Expand All @@ -117,10 +116,10 @@ class Caffe {
void* generator();
private:
class Generator;
shared_ptr<Generator> generator_;
std::shared_ptr<Generator> generator_;
};

// Getters for boost rng, curand, and cublas handles
// Getters for std rng, curand, and cublas handles
inline static RNG& rng_stream() {
if (!Get().random_generator_) {
Get().random_generator_.reset(new RNG());
Expand All @@ -142,7 +141,7 @@ class Caffe {
// freed in a non-pinned way, which may cause problems - I haven't verified
// it personally but better to note it here in the header file.
inline static void set_mode(Brew mode) { Get().mode_ = mode; }
// Sets the random seed of both boost and curand
// Sets the random seed of both std and curand
static void set_random_seed(const unsigned int seed);
// Sets the device. Since we have cublas and curand stuff, set device also
// requires us to reset those values.
Expand Down
1 change: 0 additions & 1 deletion include/caffe/data_layers.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
#include <utility>
#include <vector>

#include "boost/scoped_ptr.hpp"
#include "hdf5.h"

#include "caffe/blob.hpp"
Expand Down
11 changes: 3 additions & 8 deletions include/caffe/internal_thread.hpp
Original file line number Diff line number Diff line change
@@ -1,18 +1,13 @@
#ifndef CAFFE_INTERNAL_THREAD_HPP_
#define CAFFE_INTERNAL_THREAD_HPP_
#include <thread>

#include "caffe/common.hpp"

/**
Forward declare boost::thread instead of including boost/thread.hpp
to avoid a boost/NVCC issues (#1009, #1010) on OSX.
*/
namespace boost { class thread; }

namespace caffe {

/**
* Virtual class encapsulate boost::thread for use in base class
* Virtual class encapsulate std::thread for use in base class
* The child class will acquire the ability to run a single thread,
* by reimplementing the virutal function InternalThreadEntry.
*/
Expand All @@ -34,7 +29,7 @@ class InternalThread {
with the code you want your thread to run. */
virtual void InternalThreadEntry() {}

shared_ptr<boost::thread> thread_;
shared_ptr<std::thread> thread_;
};

} // namespace caffe
Expand Down
1 change: 0 additions & 1 deletion include/caffe/layer.hpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#ifndef CAFFE_LAYER_H_
#define CAFFE_LAYER_H_

#include <algorithm>
#include <string>
#include <vector>

Expand Down
9 changes: 6 additions & 3 deletions include/caffe/util/benchmark.hpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#ifndef CAFFE_UTIL_BENCHMARK_H_
#define CAFFE_UTIL_BENCHMARK_H_

#include <boost/date_time/posix_time/posix_time.hpp>
#include <chrono>

#include "caffe/util/device_alternate.hpp"

Expand Down Expand Up @@ -31,8 +31,11 @@ class Timer {
cudaEvent_t start_gpu_;
cudaEvent_t stop_gpu_;
#endif
boost::posix_time::ptime start_cpu_;
boost::posix_time::ptime stop_cpu_;
typedef std::chrono::high_resolution_clock clock;
typedef std::chrono::microseconds microseconds;
typedef std::chrono::milliseconds milliseconds;
clock::time_point start_cpu_;
clock::time_point stop_cpu_;
float elapsed_milliseconds_;
float elapsed_microseconds_;
};
Expand Down
17 changes: 17 additions & 0 deletions include/caffe/util/io.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

#include <unistd.h>
#include <string>
#include <vector>


#include "google/protobuf/message.h"
#include "hdf5.h"
Expand Down Expand Up @@ -140,6 +142,21 @@ cv::Mat DecodeDatumToCVMat(const Datum& datum, bool is_color);

void CVMatToDatum(const cv::Mat& cv_img, Datum* datum);

inline void string_split(vector<string>* tokens, const string& str,
const string& delimiters) {
// Skip delimiters at beginning.
string::size_type lastPos = str.find_first_not_of(delimiters, 0);
// Find first "non-delimiter".
string::size_type pos = str.find_first_of(delimiters, lastPos);
while (string::npos != pos || string::npos != lastPos) {
// Found a token, add it to the vector.
tokens->push_back(str.substr(lastPos, pos - lastPos));
// Skip delimiters. Note the "not_of"
lastPos = str.find_first_not_of(delimiters, pos);
// Find next "non-delimiter"
pos = str.find_first_of(delimiters, lastPos);
}
}
template <typename Dtype>
void hdf5_load_nd_dataset_helper(
hid_t file_id, const char* dataset_name_, int min_dim, int max_dim,
Expand Down
7 changes: 3 additions & 4 deletions include/caffe/util/rng.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,13 @@
#include <algorithm>
#include <iterator>

#include "boost/random/mersenne_twister.hpp"
#include "boost/random/uniform_int.hpp"
#include <random>

#include "caffe/common.hpp"

namespace caffe {

typedef boost::mt19937 rng_t;
typedef std::mt19937 rng_t;

inline rng_t* caffe_rng() {
return static_cast<caffe::rng_t*>(Caffe::rng_stream().generator());
Expand All @@ -23,7 +22,7 @@ inline void shuffle(RandomAccessIterator begin, RandomAccessIterator end,
RandomGenerator* gen) {
typedef typename std::iterator_traits<RandomAccessIterator>::difference_type
difference_type;
typedef typename boost::uniform_int<difference_type> dist_type;
typedef typename std::uniform_int_distribution<difference_type> dist_type;

difference_type length = std::distance(begin, end);
if (length <= 0) return;
Expand Down
1 change: 1 addition & 0 deletions include/caffe/vision_layers.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#ifndef CAFFE_VISION_LAYERS_HPP_
#define CAFFE_VISION_LAYERS_HPP_

#include <memory>
#include <string>
#include <utility>
#include <vector>
Expand Down
Loading

0 comments on commit 89c3791

Please sign in to comment.