-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMakefile
More file actions
86 lines (68 loc) · 2.67 KB
/
Makefile
File metadata and controls
86 lines (68 loc) · 2.67 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
# Variables
ROOT_DIR := $(shell dirname $(realpath $(lastword $(MAKEFILE_LIST))))
CUDA_LIB_PATH := /usr/local/cuda/lib64
CUDA_MATRIX_SRC := $(ROOT_DIR)/matrix/cuda/matrix_ops.cu
C_MATRIX_SRC := $(ROOT_DIR)/matrix/cuda/matrix_ops.c
BUILD_DIR := $(ROOT_DIR)/build
LIB_DIR := $(BUILD_DIR)/lib
BIN_DIR := $(BUILD_DIR)/bin
CUDA_LIB := $(LIB_DIR)/libmatrix_ops.so
GO_TAGS := cuda
# Debug flag (set to 1 to enable debug prints)
DEBUG ?= 0
# Compiler flags
CFLAGS := -I./cuda
NVCCFLAGS := -shared -Xcompiler -fPIC
# Add debug flag if enabled
ifeq ($(DEBUG), 1)
CFLAGS += -DDEBUG
NVCCFLAGS += -DDEBUG
endif
# Ensure build directories exist
$(BUILD_DIR) $(LIB_DIR) $(BIN_DIR):
@mkdir -p $@
# Compile CUDA and C code into a shared library
$(CUDA_LIB): $(CUDA_MATRIX_SRC) $(C_MATRIX_SRC) | $(LIB_DIR)
@nvcc $(NVCCFLAGS) -o $(CUDA_LIB) $(CUDA_MATRIX_SRC) $(C_MATRIX_SRC)
# Default target
all: build
# Build CUDA shared library
cuda: $(CUDA_LIB)
# Build Go code with CUDA support (embed RPATH using -extldflags)
build-cuda: $(CUDA_LIB) | $(BIN_DIR)
@go build -tags $(GO_TAGS) -ldflags "-extldflags '-Wl,-rpath,$(LIB_DIR)'" -o $(BIN_DIR)/main cmd/main.go
# Build Go code without CUDA support
build: | $(BIN_DIR)
@go build -o $(BIN_DIR)/main cmd/main.go
# Run the compiled binary with or without CUDA support
run: | $(BIN_DIR)
@$(BIN_DIR)/main
# Run tests all without CUDA support
test:
@echo "Testing without CUDA support"
@go test -timeout 0 -v -count=1 ./neural/tests ./matrix/tests
# Run tests all with CUDA support (embed RPATH using -extldflags)
test-cuda:
@echo "Testing with CUDA support"
@go test -v -count=1 -tags $(GO_TAGS) -ldflags "-extldflags '-Wl,-rpath,$(LIB_DIR)'" ./neural/tests ./matrix/tests
# Run tests without CUDA support
test-nerual:
@echo "Testing Neural Network package without CUDA support"
@go test -timeout 0 -v -count=1 ./neural/tests
# Run tests with CUDA support (embed RPATH using -extldflags)
test-nerual-cuda: $(CUDA_LIB)
@echo "Testing Neural Network package with CUDA support"
@go test -v -count=1 -tags $(GO_TAGS) -ldflags "-extldflags '-Wl,-rpath,$(LIB_DIR)'" ./neural/tests
# Run matrix tests without CUDA support
test-matrix:
@echo "Testing Matrix package without CUDA support"
@go test -timeout 0 -v -count=1 ./matrix/tests
# Run matrix tests with CUDA support (embed RPATH using -extldflags)
test-matrix-cuda: $(CUDA_LIB)
@echo "Testing Matrix package with CUDA support"
@go test -v -count=1 -tags $(GO_TAGS) -ldflags "-extldflags '-Wl,-rpath,$(LIB_DIR)'" ./matrix/tests
# Clean up build artifacts
clean:
@rm -rf $(BUILD_DIR)
# Phony targets
.PHONY: all build build-cuda run run-cuda test test-cuda clean test-matrix test-matrix-cuda test-nerual test-nerual-cuda