1###############################################################################
2# Copyright (c) Intel Corporation - All rights reserved.                      #
3# This file is part of the LIBXSMM library.                                   #
4#                                                                             #
5# For information on the license, see the LICENSE file.                       #
6# Further information: https://github.com/hfp/libxsmm/                        #
7# SPDX-License-Identifier: BSD-3-Clause                                       #
8###############################################################################
9# Sasikanth Avancha, Dhiraj Kalamkar, Alexander Heinecke (Intel Corp.)
10###############################################################################
11
12PROJECT := gxm
13
14CONFIG_FILE := Makefile.config
15include $(CONFIG_FILE)
16
17BUILD_DIR_LINK := $(BUILD_DIR)
18ifeq ($(RELEASE_BUILD_DIR),)
19	RELEASE_BUILD_DIR := .$(BUILD_DIR)_release
20endif
21ifeq ($(DEBUG_BUILD_DIR),)
22	DEBUG_BUILD_DIR := .$(BUILD_DIR)_debug
23endif
24
25DEBUG ?= 0
26ifeq ($(DEBUG), 1)
27	BUILD_DIR := $(DEBUG_BUILD_DIR)
28	OTHER_BUILD_DIR := $(RELEASE_BUILD_DIR)
29else
30	BUILD_DIR := $(RELEASE_BUILD_DIR)
31	OTHER_BUILD_DIR := $(DEBUG_BUILD_DIR)
32endif
33
34# All of the directories containing code.
35SRC_DIRS := $(shell find * -type d -exec bash -c "find {} -maxdepth 1 \
36	\( -name '*.cpp' -o -name '*.proto' \) | grep -q ." \; -print)
37
38BINARY_NAME := $(PROJECT)
39BIN_BUILD_DIR := $(BUILD_DIR)/bin
40FULL_BIN_NAME := $(BIN_BUILD_DIR)/$(BINARY_NAME)
41
42# Source files
43# CXX_SRCS are the source files excluding the test ones.
44CXX_SRCS := $(shell find src/ ! -name "test_*.cpp" -name "*.cpp" | sort)
45#$(info CXX_SRCS = $(CXX_SRCS))
46# BUILD_INCLUDE_DIR contains any generated header files we want to include.
47BUILD_INCLUDE_DIR := $(BUILD_DIR)
48# PROTO_SRCS are the protocol buffer definitions
49PROTO_SRC_DIR := proto
50PROTO_SRCS := $(wildcard $(PROTO_SRC_DIR)/*.proto)
51# PROTO_BUILD_DIR will contain the .cc and obj files generated from
52# PROTO_SRCS; PROTO_BUILD_INCLUDE_DIR will contain the .h header files
53PROTO_BUILD_DIR := $(BUILD_DIR)/$(PROTO_SRC_DIR)
54PROTO_BUILD_INCLUDE_DIR := $(BUILD_DIR)/proto
55NONGEN_CXX_SRCS := $(shell find \
56	src/ \
57	include/ \
58	-name "*.cpp" -or -name "*.hpp")
59
60# Generated files
61# The generated files for protocol buffers
62PROTO_GEN_HEADER_SRCS := $(addprefix $(PROTO_BUILD_DIR)/, \
63		$(call qndir,${PROTO_SRCS:.proto=.pb.h}))
64PROTO_GEN_HEADER := $(addprefix $(PROTO_BUILD_INCLUDE_DIR)/, \
65		$(call qndir,${PROTO_SRCS:.proto=.pb.h}))
66PROTO_GEN_CC := $(addprefix $(BUILD_DIR)/, ${PROTO_SRCS:.proto=.pb.cc})
67# Source file objects
68CXX_OBJS := $(addprefix $(BUILD_DIR)/, ${CXX_SRCS:.cpp=.o})
69PROTO_OBJS := ${PROTO_GEN_CC:.cc=.o}
70OBJS := $(PROTO_OBJS) $(CXX_OBJS)
71# Output files for automatic dependency generation
72DEPS := ${CXX_OBJS:.o=.d}
73
74# Compiler warning locations
75WARNS_EXT := warnings.txt
76CXX_WARNS := $(addprefix $(BUILD_DIR)/, ${CXX_SRCS:.cpp=.o.$(WARNS_EXT)})
77ALL_WARNS := $(ALL_CXX_WARNS)
78EMPTY_WARN_REPORT := $(BUILD_DIR)/.$(WARNS_EXT)
79NONEMPTY_WARN_REPORT := $(BUILD_DIR)/$(WARNS_EXT)
80
81# include and lib directories
82
83INCLUDE_DIRS += $(BUILD_INCLUDE_DIR) ./src ./include
84LIBRARIES += glog gflags protobuf m
85
86USE_OPENCV ?= 1
87
88ifeq ($(USE_OPENCV), 1)
89	LIBRARIES += opencv_core opencv_highgui opencv_imgproc
90
91	ifeq ($(OPENCV_VERSION), 3)
92		LIBRARIES += opencv_imgcodecs
93	endif
94endif
95
96WARNINGS := -Wall -Wno-sign-compare
97
98# build directories
99LIB_BUILD_DIR := ./lib
100ALL_BUILD_DIRS := $(sort $(BUILD_DIR) $(addprefix $(BUILD_DIR)/, $(SRC_DIRS)) \
101	$(LIB_BUILD_DIR) $(BIN_BUILD_DIR) $(PROTO_BUILD_INCLUDE_DIR))
102
103# build
104# Determine platform
105UNAME := $(shell uname -s)
106ifeq ($(UNAME), Linux)
107	LINUX := 1
108endif
109
110# Linux
111ifeq ($(LINUX), 1)
112	LIBRARIES += stdc++
113endif
114
115# Custom compiler
116ifdef CUSTOM_CXX
117	CXX := $(CUSTOM_CXX)
118endif
119
120# Architecture
121ifeq ($(ARCH), avx2)
122	COMMON_FLAGS += -xCORE-AVX2
123endif
124ifeq ($(ARCH), avx512_common)
125	COMMON_FLAGS += -xCOMMON-AVX512
126	#CXXFLAGS += -DUSE_NTS_SPLIT -DUSE_NTS_BN -DUSE_BLOCKING_BN
127endif
128ifeq ($(ARCH), avx512_mic)
129	COMMON_FLAGS += -xMIC-AVX512
130	#CXXFLAGS += -DUSE_NTS_SPLIT -DUSE_NTS_BN
131endif
132
133# Debugging
134ifeq ($(DEBUG), 1)
135	COMMON_FLAGS += -g -O0
136else
137	COMMON_FLAGS += -DNDEBUG -O2 -ip -ipo #-qopt-report-phase=vec -qopt-report=2
138endif
139
140# configure IO libraries
141ifeq ($(USE_OPENCV), 1)
142	COMMON_FLAGS += -DUSE_OPENCV
143endif
144
145# CPU-only configuration
146ifeq ($(CPU_ONLY), 1)
147	OBJS := $(PROTO_OBJS) $(CXX_OBJS)
148	ALL_WARNS := $(ALL_CXX_WARNS)
149	COMMON_FLAGS += -DCPU_ONLY
150endif
151
152ifeq ($(OPENMP), 1)
153	COMMON_FLAGS += -qopenmp
154endif
155
156# BLAS configuration (default = mkl)
157ifeq ($(BLAS), openblas)
158	# OpenBLAS
159	LIBRARIES += openblas
160
161	BLAS_INCLUDE = $(OPENBLAS)/include
162	BLAS_LIB = $(OPENBLAS)/lib
163	INCLUDE_DIRS += $(BLAS_INCLUDE)
164	LIBRARY_DIRS += $(BLAS_LIB)
165endif
166ifeq ($(BLAS), mkl)
167	COMMON_FLAGS += -mkl
168endif
169
170INCLUDE_DIRS += $(GXM_LIBRARY_PATH)/include
171LIBRARY_DIRS += $(GXM_LIBRARY_PATH)/lib
172
173LIBRARY_DIRS += $(LIB_BUILD_DIR)
174
175# libxsmm paths
176LIBRARIES += xsmm xsmmext
177INCLUDE_DIRS += $(LIBXSMM_PATH)/include
178LIBRARY_DIRS += $(LIBXSMM_PATH)/lib
179
180#MLSL paths
181ifeq ($(MLSL), 1)
182  LIBRARIES += mlsl
183  INCLUDE_DIRS += $(MLSL_ROOT)/intel64/include
184  LIBRARY_DIRS += $(MLSL_ROOT)/intel64/lib
185  CXXFLAGS += -DUSE_MLSL
186  CXX = mpiicpc
187endif
188
189ifeq ($(MLSL_WITH_BF16), 1)
190  CXXFLAGS += -DBF16_MLSL
191endif
192
193ifeq ($(LMDB), 1)
194  LIBRARIES += lmdb
195  LIBRARY_DIRS += $(GXM_LIBRARY_PATH)/lib
196  INCLUDE_DIRS += $(GXM_LIBRARY_PATH)/include
197  CXXFLAGS += -DUSE_LMDB
198endif
199
200# Optimized Buffer allocation for BP
201ifeq ($(BPOPT_ALLOC), 1)
202  CXXFLAGS += -DUSE_OPTBP_ALLOC
203endif
204
205ifeq ($(NUMA_ON), 1)
206  CXXFLAGS += -DUSE_NUMA
207endif
208
209ifeq ($(XSMM_TIMING), 1)
210  CXXFLAGS += -DUSE_XSMM_TIMING
211endif
212
213# Return all
214ifeq ($(RETURN_NC), 1)
215	CXXFLAGS += -DRETURNALL
216endif
217
218# Timing per layer
219ifeq ($(TIME), 1)
220	CXXFLAGS += -DTIMING
221endif
222
223# Stats for layer activations/weights
224ifeq ($(STATS), 1)
225  	CXXFLAGS += -DGETSTATS
226endif
227
228ifeq ($(DUMP_ACT), 1)
229  	CXXFLAGS += -DDUMP_ACT_DATA
230endif
231
232ifeq ($(DUMP_WT), 1)
233  	CXXFLAGS += -DDUMP_WT_DATA
234endif
235
236ifeq ($(CANCHECK), 1)
237  	CXXFLAGS += -DCANARY_CHECK
238endif
239
240ifeq ($(FP32_BU), 1)
241  	CXXFLAGS += -DCHECK_BLOWUP_FP32
242endif
243
244# Complete build flags.
245COMMON_FLAGS += $(foreach includedir,$(INCLUDE_DIRS),-I$(includedir))
246CXXFLAGS += $(COMMON_FLAGS) $(WARNINGS) -std=c++11
247LINKFLAGS += $(COMMON_FLAGS) $(WARNINGS)
248
249LDFLAGS += $(foreach librarydir,$(LIBRARY_DIRS),-L$(librarydir)) \
250		$(foreach library,$(LIBRARIES),-l$(library))
251
252# build targets
253all: bin
254
255bin: $(FULL_BIN_NAME)
256
257$(BUILD_DIR_LINK): $(BUILD_DIR)/.linked
258
259$(BUILD_DIR)/.linked:
260	@ mkdir -p $(BUILD_DIR)
261	@ $(RM) $(OTHER_BUILD_DIR)/.linked
262	@ $(RM) -r $(BUILD_DIR_LINK)
263	@ ln -s $(BUILD_DIR) $(BUILD_DIR_LINK)
264	@ touch $@
265
266$(ALL_BUILD_DIRS): | $(BUILD_DIR_LINK)
267	@ mkdir -p $@
268
269$(FULL_BIN_NAME): $(OBJS)
270		$(CXX) -o $@ $(OBJS) $(LINKFLAGS) $(LDFLAGS)
271
272$(BUILD_DIR)/%.o: %.cpp | $(ALL_BUILD_DIRS)
273	@ echo CXX $<
274	$(CXX) $< $(CXXFLAGS) -c -o $@ 2> $@.$(WARNS_EXT) \
275		|| (cat $@.$(WARNS_EXT); exit 1)
276	@ cat $@.$(WARNS_EXT)
277
278$(PROTO_BUILD_DIR)/%.pb.o: $(PROTO_BUILD_DIR)/%.pb.cc $(PROTO_GEN_HEADER) \
279		| $(PROTO_BUILD_DIR)
280	@ echo CXX $<
281	$(CXX) $< $(CXXFLAGS) -c -o $@ 2> $@.$(WARNS_EXT) \
282		|| (cat $@.$(WARNS_EXT); exit 1)
283	@ cat $@.$(WARNS_EXT)
284
285proto: $(PROTO_GEN_CC) $(PROTO_GEN_HEADER)
286
287$(PROTO_BUILD_DIR)/%.pb.cc $(PROTO_BUILD_DIR)/%.pb.h : \
288		$(PROTO_SRC_DIR)/%.proto | $(PROTO_BUILD_DIR)
289	@ echo PROTOC $<
290	protoc --proto_path=$(PROTO_SRC_DIR) --cpp_out=$(PROTO_BUILD_DIR) $<
291
292clean:
293	@- $(RM) -rf $(ALL_BUILD_DIRS)
294	@- $(RM) -rf $(OTHER_BUILD_DIR)
295	@- $(RM) -rf $(BUILD_DIR_LINK)
296
297-include $(DEPS)
298