1 #
2 # The following defines a variable named "NAME" with a value of "myprogram". By convention,
3 # a lowercase prefix (in this case "program") and an uppercased suffix (in this case "NAME"), separated
4 # by an underscore is used to name attributes for a common element. Think of this like
5 # using program.NAME, program.C_SRCS, etc. There are no structs in Make, so we use this convention
6 # to keep track of attributes that all belong to the same target or program.
7 #
8 PROJECT := caffeine
9 NAME := lib$(PROJECT).so
10 TEST_NAME := test_$(PROJECT)
11 CXX_SRCS := $(shell find caffeine ! -name "test_*.cpp" -name "*.cpp")
12 CU_SRCS := $(shell find caffeine -name "*.cu")
13 TEST_SRCS := $(shell find caffeine -name "test_*.cpp") gtest/gtest-all.cpp
14 PROTO_SRCS := $(wildcard caffeine/proto/*.proto)
15 PROTO_GEN_HEADER := ${PROTO_SRCS:.proto=.pb.h}
16 PROTO_GEN_CC := ${PROTO_SRCS:.proto=.pb.cc}
17 CXX_OBJS := ${CXX_SRCS:.cpp=.o}
18 CU_OBJS := ${CU_SRCS:.cu=.o}
19 PROTO_OBJS := ${PROTO_SRCS:.proto=.pb.o}
20 OBJS := $(PROTO_OBJS) $(CXX_OBJS) $(CU_OBJS)
21 TEST_OBJS := ${TEST_SRCS:.cpp=.o}
23 CUDA_DIR := /usr/local/cuda
24 CUDA_ARCH := -arch=sm_20
25 MKL_DIR := /opt/intel/mkl
27 CUDA_INCLUDE_DIR := $(CUDA_DIR)/include
28 CUDA_LIB_DIR := $(CUDA_DIR)/lib64
29 MKL_INCLUDE_DIR := $(MKL_DIR)/include
30 MKL_LIB_DIR := $(MKL_DIR)/lib $(MKL_DIR)/lib/intel64
32 INCLUDE_DIRS := . /usr/local/include $(CUDA_INCLUDE_DIR) $(MKL_INCLUDE_DIR)
33 LIBRARY_DIRS := . /usr/local/lib $(CUDA_LIB_DIR) $(MKL_LIB_DIR)
34 LIBRARIES := cuda cudart cublas protobuf glog mkl_rt mkl_intel_thread curand
35 WARNINGS := -Wall
37 CXXFLAGS += -fPIC $(foreach includedir,$(INCLUDE_DIRS),-I$(includedir))
38 LDFLAGS += $(foreach librarydir,$(LIBRARY_DIRS),-L$(librarydir))
39 LDFLAGS += $(foreach library,$(LIBRARIES),-l$(library))
41 LINK = $(CXX) $(CXXFLAGS) $(CPPFLAGS) $(LDFLAGS) $(WARNINGS)
42 NVCC = nvcc ${CXXFLAGS:-fPIC=-Xcompiler -fPIC} $(CPPFLAGS) $(CUDA_ARCH)
44 .PHONY: all test clean distclean
46 all: $(NAME)
48 test: $(TEST_NAME)
50 $(TEST_NAME): $(OBJS) $(TEST_OBJS)
51 $(CXX) $(OBJS) $(TEST_OBJS) -o $(TEST_NAME) $(LDFLAGS) $(WARNINGS)
53 $(NAME): $(PROTO_GEN_CC) $(OBJS)
54 $(LINK) -shared $(OBJS) -o $(NAME)
56 $(CU_OBJS): %.o: %.cu
57 $(NVCC) -c $< -o $@
59 $(PROTO_GEN_CC): $(PROTO_SRCS)
60 protoc $(PROTO_SRCS) --cpp_out=. --python_out=.
62 clean:
63 @- $(RM) $(NAME) $(TEST_NAME)
64 @- $(RM) $(OBJS) $(TEST_OBJS)
65 @- $(RM) $(PROTO_GEN_HEADER) $(PROTO_GEN_CC)
67 distclean: clean