This repository has been archived by the owner on Apr 11, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 15
/
cuda.mk
315 lines (260 loc) · 7.91 KB
/
cuda.mk
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
################################################################################
#
# Common build script
#
################################################################################
#.SUFFIXES : .cu .cu_dbg.o .c_dbg.o .cpp_dbg.o .cu_rel.o .c_rel.o .cpp_rel.o .cubin
# Add new SM Versions here as devices with new Compute Capability are released
SM_VERSIONS := sm_10 sm_11 sm_12 sm_13
CUDA_INSTALL_PATH ?= /usr/local/cuda
ifdef cuda-install
CUDA_INSTALL_PATH := $(cuda-install)
endif
CUDA_SDK_PATH ?= /home/$(USER)/NVIDIA_CUDA_SDK
ifdef cuda-sdk
CUDA_SDK_PATH := $(cuda-sdk)
endif
# detect OS
OSUPPER = $(shell uname -s 2>/dev/null | tr [:lower:] [:upper:])
OSLOWER = $(shell uname -s 2>/dev/null | tr [:upper:] [:lower:])
# 'linux' is output for Linux system, 'darwin' for OS X
DARWIN = $(strip $(findstring DARWIN, $(OSUPPER)))
# Basic directory setup for SDK
# (override directories only if they are not already defined)
SRCDIR ?=
#ROOTDIR ?= .
ROOTBINDIR ?= .
#$(ROOTDIR)/bin
BINDIR ?= $(ROOTBINDIR)
#/$(OSLOWER)
ROOTOBJDIR ?= .
#obj
LIBDIR := $(CUDA_SDK_PATH)/lib
COMMONDIR := $(CUDA_SDK_PATH)/common
# Compilers
NVCC := $(CUDA_INSTALL_PATH)/bin/nvcc
CXX := g++
CC := gcc
# Includes
INCLUDES += -I. -I$(CUDA_INSTALL_PATH)/include -I$(COMMONDIR)/inc
#-I$(COMMONDIR)/inc
# architecture flag for cubin build
CUBIN_ARCH_FLAG := -m32
# Warning flags
CXXWARN_FLAGS := \
-W -Wall \
-Wimplicit \
-Wswitch \
-Wformat \
-Wchar-subscripts \
-Wparentheses \
-Wmultichar \
-Wtrigraphs \
-Wpointer-arith \
-Wcast-align \
-Wreturn-type \
-Wno-unused-function \
$(SPACE)
CWARN_FLAGS := $(CXXWARN_FLAGS) \
-Wstrict-prototypes \
-Wmissing-prototypes \
-Wmissing-declarations \
-Wnested-externs \
-Wmain \
# Compiler-specific flags
NVCCFLAGS :=
CUDACFLAGS := $(CWARN_FLAGS)
# Common flags
COMMONFLAGS += $(INCLUDES) -DUNIX
# Debug/release configuration
ifeq ($(dbg),1)
COMMONFLAGS += -g
CFLAGS += -g
NVCCFLAGS += -D_DEBUG -g
BINSUBDIR := .
#debug
LIBSUFFIX := D
else
##COMMONFLAGS += -O3
BINSUBDIR := .
#release
LIBSUFFIX :=
##NVCCFLAGS += --compiler-options -fno-strict-aliasing
##CFLAGS += -fno-strict-aliasing
endif
# append optional arch/SM version flags (such as -arch sm_11)
#NVCCFLAGS += $(SMVERSIONFLAGS)
# architecture flag for cubin build
CUBIN_ARCH_FLAG := -m32
# detect if 32 bit or 64 bit system
HP_64 = $(shell uname -m | grep 64)
# OpenGL is used or not (if it is used, then it is necessary to include GLEW)
ifeq ($(USEGLLIB),1)
ifneq ($(DARWIN),)
OPENGLLIB := -L/System/Library/Frameworks/OpenGL.framework/Libraries -lGL -lGLU $(COMMONDIR)/lib/$(OSLOWER)/libGLEW.a
else
OPENGLLIB := -lGL -lGLU -lX11 -lXi -lXmu
ifeq "$(strip $(HP_64))" ""
OPENGLLIB += -lGLEW -L/usr/X11R6/lib
else
OPENGLLIB += -lGLEW_x86_64 -L/usr/X11R6/lib64
endif
endif
CUBIN_ARCH_FLAG := -m64
endif
ifeq ($(USEGLUT),1)
ifneq ($(DARWIN),)
OPENGLLIB += -framework GLUT
else
OPENGLLIB += -lglut
endif
endif
ifeq ($(USEPARAMGL),1)
PARAMGLLIB := -lparamgl$(LIBSUFFIX)
endif
ifeq ($(USERENDERCHECKGL),1)
RENDERCHECKGLLIB := -lrendercheckgl$(LIBSUFFIX)
endif
ifeq ($(USECUDPP), 1)
ifeq "$(strip $(HP_64))" ""
CUDPPLIB := -lcudpp
else
CUDPPLIB := -lcudpp64
endif
CUDPPLIB := $(CUDPPLIB)$(LIBSUFFIX)
ifeq ($(emu), 1)
CUDPPLIB := $(CUDPPLIB)_emu
endif
endif
# Libs
LIB := -L$(CUDA_INSTALL_PATH)/lib -L$(LIBDIR) -L$(COMMONDIR)/lib/$(OSLOWER)
CUDALFFLAGS := -L$(CUDA_INSTALL_PATH)/lib -L$(LIBDIR) -L$(COMMONDIR)/lib/$(OSLOWER)
ifeq ($(USEDRVAPI),1)
LIB += -lcuda $(CUDPPLIB)
CUDALFFLAGS += -lcuda $(CUDPPLIB) ${OPENGLLIB} $(PARAMGLLIB) $(RENDERCHECKGLLIB)
else
LIB += -lcudart $(CUDPPLIB)
CUDALFFLAGS += -lcudart $(CUDPPLIB) ${OPENGLLIB} $(PARAMGLLIB) $(RENDERCHECKGLLIB)
endif
ifeq ($(USECUFFT),1)
ifeq ($(emu),1)
LIB += -lcufftemu
CUDALFFLAGS += -lcufftemu
else
LIB += -lcufft
CUDALFFLAGS += -lcufft
endif
endif
ifeq ($(USECUBLAS),1)
ifeq ($(emu),1)
LIB += -lcublasemu
CUDALFFLAGS += -lcublasemu
else
LIB += -lcublas
CUDALFFLAGS += -lcublas
endif
endif
# Lib/exe configuration
ifneq ($(STATIC_LIB),)
else
LIB += -lcutil$(LIBSUFFIX)
# Device emulation configuration
ifeq ($(emu), 1)
NVCCFLAGS += -deviceemu
CUDACCFLAGS +=
#BINSUBDIR := emu$(BINSUBDIR)
# consistency, makes developing easier
CFLAGS += -D__DEVICE_EMULATION__
endif
endif
# check if verbose
ifeq ($(verbose), 1)
VERBOSE :=
else
VERBOSE := @
endif
################################################################################
# Check for input flags and set compiler flags appropriately
################################################################################
ifeq ($(fastmath), 1)
NVCCFLAGS += -use_fast_math
endif
ifeq ($(keep), 1)
NVCCFLAGS += -keep
NVCC_KEEP_CLEAN := *.i* *.cubin *.cu.c *.cudafe* *.fatbin.c *.ptx
endif
ifdef maxregisters
NVCCFLAGS += -maxrregcount $(maxregisters)
endif
# Add cudacc flags
NVCCFLAGS += $(CUDACCFLAGS)
# workaround for mac os x cuda 1.1 compiler issues
ifneq ($(DARWIN),)
NVCCFLAGS += --host-compilation=C
endif
# Add common flags
NVCCFLAGS += --host-compilation=c
NVCCFLAGS += $(COMMONFLAGS)
CUDACFLAGS += $(COMMONFLAGS)
ifeq ($(nvcc_warn_verbose),1)
NVCCFLAGS += $(addprefix --compiler-options ,$(CXXWARN_FLAGS))
NVCCFLAGS += --compiler-options -fno-strict-aliasing
endif
################################################################################
# Set up object files
################################################################################
OBJDIR := $(ROOTOBJDIR)
#$(ROOTOBJDIR)/$(BINSUBDIR)
#OBJS += $(patsubst %.c,$(OBJDIR)/%.c.o,$(notdir $(CUDACFILES)))
CUDAOBJS += $(patsubst %.cu,$(OBJDIR)/%.cuo, $(CUFILES))
################################################################################
# Set up cubin files
################################################################################
CUBINDIR := $(SRCDIR)data
CUBINS += $(patsubst %.cu,$(CUBINDIR)/%.cubin,$(notdir $(CUBINFILES)))
################################################################################
# Rules
################################################################################
#$(OBJDIR)/%.c.o : $(SRCDIR)%.c $(C_DEPS)
# $(VERBOSE)$(CC) $(CFLAGS) -o $@ -c $<
$(OBJDIR)/%.cuo : $(SRCDIR)%.cu $(CU_DEPS)
@$(VERBOSE)$(NVCC) $(NVCCFLAGS) --no-align-double $(SMVERSIONFLAGS) -o $@ -c $<
$(CUBINDIR)/%.cubin : $(SRCDIR)%.cu cubindirectory
$(VERBOSE)$(NVCC) $(CUBIN_ARCH_FLAG) $(NVCCFLAGS) $(SMVERSIONFLAGS) -o $@ -cubin $<
#
# The following definition is a template that gets instantiated for each SM
# version (sm_10, sm_13, etc.) stored in SMVERSIONS. It does 2 things:
# 1. It adds to OBJS a .cu_sm_XX.o for each .cu file it finds in CUFILES_sm_XX.
# 2. It generates a rule for building .cu_sm_XX.o files from the corresponding
# .cu file.
#
# The intended use for this is to allow Makefiles that use common.mk to compile
# files to different Compute Capability targets (aka SM arch version). To do
# so, in the Makefile, list files for each SM arch separately, like so:
#
# CUFILES_sm_10 := mycudakernel_sm10.cu app.cu
# CUFILES_sm_12 := anothercudakernel_sm12.cu
#
define SMVERSION_template
OBJS += $(patsubst %.cu,$(OBJDIR)/%.cu_$(1).o,$(notdir $(CUFILES_$(1))))
$(OBJDIR)/%.cu_$(1).o : $(SRCDIR)%.cu $(CU_DEPS)
$(VERBOSE)$(NVCC) -o $$@ -c $$< $(NVCCFLAGS) -arch $(1)
endef
# This line invokes the above template for each arch version stored in
# SM_VERSIONS. The call funtion invokes the template, and the eval
# function interprets it as make commands.
$(foreach smver,$(SM_VERSIONS),$(eval $(call SMVERSION_template,$(smver))))
cubindirectory:
$(VERBOSE)mkdir -p $(CUBINDIR)
makedirectories:
#$(VERBOSE)mkdir -p $(LIBDIR)
$(VERBOSE)mkdir -p $(OBJDIR)
tidy :
$(VERBOSE)find . | egrep "#" | xargs rm -f
$(VERBOSE)find . | egrep "\~" | xargs rm -f
cleancuda : tidy
$(VERBOSE)rm -f $(CUDAOBJS)
$(VERBOSE)rm -f $(CUBINS)
$(VERBOSE)rm -f $(NVCC_KEEP_CLEAN)
clobber : clean
$(VERBOSE)rm -rf $(ROOTOBJDIR)