Skip to content

Commit

Permalink
Merge pull request BVLC#29 from kloudkl/master
Browse files Browse the repository at this point in the history
Separate build, distribute, and src directories + build more cuda archs
  • Loading branch information
shelhamer committed Jan 19, 2014
2 parents 465e9cb + 5425831 commit d3c9527
Show file tree
Hide file tree
Showing 2 changed files with 109 additions and 52 deletions.
153 changes: 102 additions & 51 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -16,26 +16,26 @@ STATIC_NAME := lib$(PROJECT).a
# Get all source files
##############################
# CXX_SRCS are the source files excluding the test ones.
CXX_SRCS := $(shell find src/caffe ! -name "test_*.cpp" -name "*.cpp")
CXX_SRCS := $(shell find src/$(PROJECT) ! -name "test_*.cpp" -name "*.cpp")
# HXX_SRCS are the header files
HXX_SRCS := $(shell find include/caffe ! -name "*.hpp")
HXX_SRCS := $(shell find include/$(PROJECT) ! -name "*.hpp")
# CU_SRCS are the cuda source files
CU_SRCS := $(shell find src/caffe -name "*.cu")
CU_SRCS := $(shell find src/$(PROJECT) -name "*.cu")
# TEST_SRCS are the test source files
TEST_SRCS := $(shell find src/caffe -name "test_*.cpp")
TEST_SRCS := $(shell find src/$(PROJECT) -name "test_*.cpp")
GTEST_SRC := src/gtest/gtest-all.cpp
# TEST_HDRS are the test header files
TEST_HDRS := $(shell find src/caffe -name "test_*.hpp")
TEST_HDRS := $(shell find src/$(PROJECT) -name "test_*.hpp")
# EXAMPLE_SRCS are the source files for the example binaries
EXAMPLE_SRCS := $(shell find examples -name "*.cpp")
# PROTO_SRCS are the protocol buffer definitions
PROTO_SRCS := $(wildcard src/caffe/proto/*.proto)
# PYCAFFE_SRC is the python wrapper for caffe
PYCAFFE_SRC := python/caffe/pycaffe.cpp
PYCAFFE_SO := python/caffe/pycaffe.so
# MATCAFFE_SRC is the matlab wrapper for caffe
MATCAFFE_SRC := matlab/caffe/matcaffe.cpp
MATCAFFE_SO := matlab/caffe/caffe
PROTO_SRCS := $(wildcard src/$(PROJECT)/proto/*.proto)
# PY$(PROJECT)_SRC is the python wrapper for $(PROJECT)
PY$(PROJECT)_SRC := python/$(PROJECT)/py$(PROJECT).cpp
PY$(PROJECT)_SO := python/$(PROJECT)/py$(PROJECT).so
# MAT$(PROJECT)_SRC is the matlab wrapper for $(PROJECT)
MAT$(PROJECT)_SRC := matlab/$(PROJECT)/mat$(PROJECT).cpp
MAT$(PROJECT)_SO := matlab/$(PROJECT)/$(PROJECT)

##############################
# Derive generated files
Expand All @@ -47,16 +47,16 @@ PROTO_GEN_PY := ${PROTO_SRCS:.proto=_pb2.py}
# The objects corresponding to the source files
# These objects will be linked into the final shared library, so we
# exclude the test and example objects.
CXX_OBJS := ${CXX_SRCS:.cpp=.o}
CU_OBJS := ${CU_SRCS:.cu=.cuo}
PROTO_OBJS := ${PROTO_GEN_CC:.cc=.o}
CXX_OBJS := $(addprefix $(BUILD_DIR)/, ${CXX_SRCS:.cpp=.o})
CU_OBJS := $(addprefix $(BUILD_DIR)/, ${CU_SRCS:.cu=.cuo})
PROTO_OBJS := $(addprefix $(BUILD_DIR)/, ${PROTO_GEN_CC:.cc=.o})
OBJS := $(PROTO_OBJS) $(CXX_OBJS) $(CU_OBJS)
# program and test objects
EXAMPLE_OBJS := ${EXAMPLE_SRCS:.cpp=.o}
TEST_OBJS := ${TEST_SRCS:.cpp=.o}
GTEST_OBJ := ${GTEST_SRC:.cpp=.o}
EXAMPLE_OBJS := $(addprefix $(BUILD_DIR)/, ${EXAMPLE_SRCS:.cpp=.o})
TEST_OBJS := $(addprefix $(BUILD_DIR)/, ${TEST_SRCS:.cpp=.o})
GTEST_OBJ := $(addprefix $(BUILD_DIR)/, ${GTEST_SRC:.cpp=.o})
# program and test bins
EXAMPLE_BINS :=${EXAMPLE_OBJS:.o=.bin}
EXAMPLE_BINS := ${EXAMPLE_OBJS:.o=.bin}
TEST_BINS := ${TEST_OBJS:.o=.testbin}

##############################
Expand Down Expand Up @@ -86,32 +86,47 @@ PYTHON_LDFLAGS := $(LDFLAGS) $(foreach library,$(PYTHON_LIBRARIES),-l$(library))
##############################
# Define build targets
##############################
.PHONY: all test clean linecount examples pycaffe distribute
.PHONY: all init test clean linecount examples py mat distribute py$(PROJECT) mat$(PROJECT) proto

all: $(NAME) $(STATIC_NAME) examples
all: init $(NAME) $(STATIC_NAME) examples
@echo $(CXX_OBJS)

init:
@ mkdir -p $(foreach obj,$(OBJS),$(dir $(obj)))
@ mkdir -p $(foreach obj,$(EXAMPLE_OBJS),$(dir $(obj)))
@ mkdir -p $(foreach obj,$(TEST_OBJS),$(dir $(obj)))
@ mkdir -p $(foreach obj,$(GTEST_OBJ),$(dir $(obj)))

linecount: clean
cloc --read-lang-def=caffe.cloc src/caffe/
cloc --read-lang-def=$(PROJECT).cloc src/$(PROJECT)/

test: $(TEST_BINS)
test: init $(TEST_BINS)

examples: $(EXAMPLE_BINS)
examples: init $(EXAMPLE_BINS)

pycaffe: $(STATIC_NAME) $(PYCAFFE_SRC) $(PROTO_GEN_PY)
$(CXX) -shared -o $(PYCAFFE_SO) $(PYCAFFE_SRC) \
py$(PROJECT): py

py: init $(STATIC_NAME) $(PY$(PROJECT)_SRC) $(PROTO_GEN_PY)
$(CXX) -shared -o $(PY$(PROJECT)_SO) $(PY$(PROJECT)_SRC) \
$(STATIC_NAME) $(CXXFLAGS) $(PYTHON_LDFLAGS)
@echo

mat$(PROJECT): mat

matcaffe: $(STATIC_NAME) $(MATCAFFE_SRC)
$(MATLAB_DIR)/bin/mex $(MATCAFFE_SRC) $(STATIC_NAME) \
mat: init $(STATIC_NAME) $(MAT$(PROJECT)_SRC)
$(MATLAB_DIR)/bin/mex $(MAT$(PROJECT)_SRC) $(STATIC_NAME) \
CXXFLAGS="\$$CXXFLAGS $(CXXFLAGS) $(WARNINGS)" \
CXXLIBS="\$$CXXLIBS $(LDFLAGS)" \
-o $(MATCAFFE_SO)

$(NAME): $(PROTO_OBJS) $(OBJS)
-o $(MAT$(PROJECT)_SO)
@echo

$(NAME): init $(PROTO_OBJS) $(OBJS)
$(CXX) -shared -o $(NAME) $(OBJS) $(LDFLAGS) $(WARNINGS)
@echo

$(STATIC_NAME): $(PROTO_OBJS) $(OBJS)
$(STATIC_NAME): init $(PROTO_OBJS) $(OBJS)
ar rcs $(STATIC_NAME) $(PROTO_OBJS) $(OBJS)
@echo

runtest: test
for testbin in $(TEST_BINS); do $$testbin $(TEST_GPUID); done
Expand All @@ -121,40 +136,76 @@ $(TEST_BINS): %.testbin : %.o $(GTEST_OBJ) $(STATIC_NAME) $(TEST_HDRS)

$(EXAMPLE_BINS): %.bin : %.o $(STATIC_NAME)
$(CXX) $< $(STATIC_NAME) -o $@ $(LDFLAGS) $(WARNINGS)
@echo

$(OBJS): $(PROTO_GEN_CC) $(HXX_SRCS)

$(EXAMPLE_OBJS): $(PROTO_GEN_CC)

$(CU_OBJS): %.cuo: %.cu
$(BUILD_DIR)/src/$(PROJECT)/%.o: src/$(PROJECT)/%.cpp
$(CXX) $< $(CXXFLAGS) -c -o $@ $(LDFLAGS)
@echo

$(BUILD_DIR)/src/$(PROJECT)/layers/%.o: src/$(PROJECT)/layers/%.cpp
$(CXX) $< $(CXXFLAGS) -c -o $@ $(LDFLAGS)
@echo

$(BUILD_DIR)/src/$(PROJECT)/proto/%.o: src/$(PROJECT)/proto/%.cc
$(CXX) $< $(CXXFLAGS) -c -o $@ $(LDFLAGS)
@echo

$(BUILD_DIR)/src/$(PROJECT)/test/%.o: src/test/%.cpp
$(CXX) $< $(CXXFLAGS) -c -o $@ $(LDFLAGS)
@echo

$(BUILD_DIR)/src/$(PROJECT)/util/%.o: src/$(PROJECT)/util/%.cpp
$(CXX) $< $(CXXFLAGS) -c -o $@ $(LDFLAGS)
@echo

$(BUILD_DIR)/src/gtest/%.o: src/gtest/%.cpp
$(CXX) $< $(CXXFLAGS) -c -o $@ $(LDFLAGS)
@echo

$(BUILD_DIR)/src/$(PROJECT)/layers/%.cuo: src/$(PROJECT)/layers/%.cu
$(CUDA_DIR)/bin/nvcc $(NVCCFLAGS) $(CUDA_ARCH) -c $< -o $@
@echo

$(BUILD_DIR)/src/$(PROJECT)/util/%.cuo: src/$(PROJECT)/util/%.cu
$(CUDA_DIR)/bin/nvcc $(NVCCFLAGS) $(CUDA_ARCH) -c $< -o $@
@echo

$(BUILD_DIR)/examples/%.o: examples/%.cpp
$(CXX) $< $(CXXFLAGS) -c -o $@ $(LDFLAGS)
@echo

$(PROTO_GEN_PY): $(PROTO_SRCS)
protoc --proto_path=src --python_out=python $(PROTO_SRCS)
@echo

proto: $(PROTO_GEN_CC)

$(PROTO_GEN_CC): $(PROTO_SRCS)
protoc --proto_path=src --cpp_out=src $(PROTO_SRCS)
mkdir -p include/caffe/proto
cp $(PROTO_GEN_HEADER) include/caffe/proto/

mkdir -p include/$(PROJECT)/proto
cp $(PROTO_GEN_HEADER) include/$(PROJECT)/proto/
@echo

clean:
@- $(RM) $(NAME) $(STATIC_NAME) $(TEST_BINS) $(EXAMPLE_BINS)
@- $(RM) $(OBJS) $(TEST_OBJS) $(EXAMPLE_OBJS)
@- $(RM) $(NAME) $(STATIC_NAME)
@- $(RM) $(PROTO_GEN_HEADER) $(PROTO_GEN_CC) $(PROTO_GEN_PY)
@- $(RM) include/caffe/proto/caffe.pb.h
@- $(RM) python/caffe/proto/caffe_pb2.py
@- $(RM) -rf build
@- $(RM) include/$(PROJECT)/proto/$(PROJECT).pb.h
@- $(RM) python/$(PROJECT)/proto/$(PROJECT)_pb2.py
@- $(RM) -rf $(BUILD_DIR)
@- $(RM) -rf $(DISTRIBUTE_DIR)

distribute: all
mkdir build
mkdir $(DISTRIBUTE_DIR)
# add include
cp -r include build/
cp -r include $(DISTRIBUTE_DIR)/
# add example binaries
mkdir build/bin
cp $(EXAMPLE_BINS) build/bin
mkdir $(DISTRIBUTE_DIR)/bin
cp $(EXAMPLE_BINS) $(DISTRIBUTE_DIR)/bin
# add libraries
mkdir build/lib
cp $(NAME) build/lib
cp $(STATIC_NAME) build/lib
mkdir $(DISTRIBUTE_DIR)/lib
cp $(NAME) $(DISTRIBUTE_DIR)/lib
cp $(STATIC_NAME) $(DISTRIBUTE_DIR)/lib
# add python - it's not the standard way, indeed...
cp -r python build/python
cp -r python $(DISTRIBUTE_DIR)/python
8 changes: 7 additions & 1 deletion Makefile.config
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@
# CHANGE YOUR CUDA PATH IF IT IS NOT THIS
CUDA_DIR := /usr/local/cuda
# CHANGE YOUR CUDA ARCH IF IT IS NOT THIS
CUDA_ARCH := -arch=sm_30
CUDA_ARCH := -gencode arch=compute_20,code=sm_20 \
-gencode arch=compute_20,code=sm_21 \
-gencode arch=compute_30,code=sm_30 \
-gencode arch=compute_35,code=sm_35
# CHANGE YOUR MKL PATH IF IT IS NOT THIS
MKL_DIR := /opt/intel/mkl
# CHANGE YOUR MATLAB PATH IF IT IS NOT THIS
Expand All @@ -15,3 +18,6 @@ LIBRARY_DIRS := /usr/lib /usr/local/lib

# DEFINE THE CXX PATH
CXX=/usr/bin/g++

BUILD_DIR=build
DISTRIBUTE_DIR=distribute

0 comments on commit d3c9527

Please sign in to comment.