-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMakefile
More file actions
97 lines (77 loc) · 3.58 KB
/
Makefile
File metadata and controls
97 lines (77 loc) · 3.58 KB
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
# ─────────────────────────────────────────────────────────────────────────────
# Makefile — GPU Matrix Ops
# Usage:
# make # build with optimisations
# make DEBUG=1 # build with debug info / no optimisation
# make run # build + execute
# make clean # remove build artefacts
# make profile # build with Nsight-profiling flags
# ─────────────────────────────────────────────────────────────────────────────
NVCC := nvcc
CXX := g++
# Detect SM version; fallback to 86 (Ampere RTX 30-series)
SM ?= 86
# ── Directories ───────────────────────────────────────────────────────────────
SRCDIR := src
INCDIR := include
BINDIR := bin
OBJDIR := obj
# ── Sources ───────────────────────────────────────────────────────────────────
CU_SRCS := $(SRCDIR)/main.cu \
$(SRCDIR)/matvec_kernels.cu \
$(SRCDIR)/matmul_kernels.cu
CPP_SRCS := $(SRCDIR)/cpu_ops.cpp
CU_OBJS := $(patsubst $(SRCDIR)/%.cu, $(OBJDIR)/%.cu.o, $(CU_SRCS))
CPP_OBJS := $(patsubst $(SRCDIR)/%.cpp, $(OBJDIR)/%.cpp.o, $(CPP_SRCS))
TARGET := $(BINDIR)/gpu_matrix_ops
# ── Flags ─────────────────────────────────────────────────────────────────────
COMMON_FLAGS := -I$(INCDIR)
ifdef DEBUG
NVCC_FLAGS := -g -G -O0 -arch=sm_$(SM) $(COMMON_FLAGS)
CXX_FLAGS := -g -O0 $(COMMON_FLAGS)
else
NVCC_FLAGS := -O3 --use_fast_math -arch=sm_$(SM) \
--expt-relaxed-constexpr \
-lineinfo \
--ptxas-options=-v \
$(COMMON_FLAGS)
CXX_FLAGS := -O3 -march=native $(COMMON_FLAGS)
endif
# FIX: removed -G from PROFILE flags.
# -G compiles device code with full debug instrumentation and disables all GPU
# optimisations, so profiling a -G binary gives meaningless timings that do not
# reflect real kernel performance. -lineinfo alone gives Nsight Compute and
# nvprof the source-to-PTX line mapping they need without touching optimisations.
ifdef PROFILE
NVCC_FLAGS += -lineinfo
endif
LINK_FLAGS := -lcudart
# ── Default target ────────────────────────────────────────────────────────────
.PHONY: all run clean profile info
all: $(TARGET)
$(TARGET): $(CU_OBJS) $(CPP_OBJS)
@mkdir -p $(BINDIR)
$(NVCC) $(NVCC_FLAGS) $^ -o $@ $(LINK_FLAGS)
@echo ""
@echo " Built: $@"
$(OBJDIR)/%.cu.o: $(SRCDIR)/%.cu
@mkdir -p $(OBJDIR)
$(NVCC) $(NVCC_FLAGS) -dc -c $< -o $@
$(OBJDIR)/%.cpp.o: $(SRCDIR)/%.cpp
@mkdir -p $(OBJDIR)
$(CXX) $(CXX_FLAGS) -c $< -o $@
run: all
@echo ""
./$(TARGET)
profile: PROFILE=1
profile: all
@echo " Binary built with profiling flags."
@echo " Run with: ncu --set full ./$(TARGET)"
@echo " or : nvprof ./$(TARGET)"
clean:
rm -rf $(OBJDIR) $(BINDIR)
@echo " Cleaned."
info:
@echo " Target SM : sm_$(SM)"
@echo " NVCC : $(shell which $(NVCC))"
@echo " CUDA ver : $(shell $(NVCC) --version | tail -1)"