makefile cuda
This commit is contained in:
parent
65d5659571
commit
83b7b89e7f
10 changed files with 45 additions and 51 deletions
|
|
@ -1,50 +1,38 @@
|
|||
# Compiler
|
||||
CXX := g++
|
||||
NVCC := $(shell command -v nvcc 2>/dev/null)
|
||||
CC = g++
|
||||
NVCC = nvcc
|
||||
|
||||
# Compiler flags
|
||||
CXXFLAGS := -Wall -Werror -fpic
|
||||
NVCCFLAGS := -Xcompiler -fPIC
|
||||
CFLAGS = -Wall -Wextra -std=c++11
|
||||
NVCCFLAGS = -std=c++11
|
||||
|
||||
# CUDA flags
|
||||
CUDAFLAGS := -arch=sm_75
|
||||
# Directories
|
||||
SRCDIR = ../csrc
|
||||
BUILDDIR = ../build
|
||||
TARGET = ../build/libtensor.so
|
||||
|
||||
# Source files
|
||||
SRCS := ../csrc/tensor.cpp
|
||||
CU_SRCS := $(wildcard ../csrc/*.cu)
|
||||
# Files
|
||||
SRCS = $(wildcard $(SRCDIR)/*.cpp)
|
||||
CU_SRCS = $(wildcard $(SRCDIR)/*.cu)
|
||||
OBJS = $(patsubst $(SRCDIR)/%.cpp, $(BUILDDIR)/%.o, $(SRCS))
|
||||
CU_OBJS = $(patsubst $(SRCDIR)/%.cu, $(BUILDDIR)/%.cu.o, $(CU_SRCS))
|
||||
|
||||
# Object files
|
||||
OBJS := $(SRCS:.cpp=.o)
|
||||
# CUDA flags and libraries
|
||||
CUDAFLAGS = -arch=sm_75
|
||||
CUDALIBS = -lcudart
|
||||
|
||||
# CUDA object files (only if nvcc is available)
|
||||
ifeq ($(NVCC),)
|
||||
CU_OBJS :=
|
||||
else
|
||||
CU_OBJS := $(CU_SRCS:%.cu=%.cu.o)
|
||||
endif
|
||||
|
||||
# Target shared object file
|
||||
TARGET := libtensor.so
|
||||
|
||||
# Default target
|
||||
all: $(TARGET)
|
||||
|
||||
# Rule to build the shared object file
|
||||
# Rule to build the target
|
||||
$(TARGET): $(OBJS) $(CU_OBJS)
|
||||
$(CXX) -shared -o $@ $^
|
||||
$(NVCC) --shared -o $(TARGET) $(OBJS) $(CU_OBJS) $(CUDALIBS)
|
||||
|
||||
# Rule to compile each source file into an object file
|
||||
%.o: %.cpp
|
||||
$(CXX) $(CXXFLAGS) -c $< -o $@
|
||||
# Rule to compile C++ source files
|
||||
$(BUILDDIR)/%.o: $(SRCDIR)/%.cpp
|
||||
$(CC) $(CFLAGS) -fPIC -c $< -o $@
|
||||
|
||||
# Rule to compile each CUDA source file into an object file (only if nvcc is available)
|
||||
%.cu.o: %.cu
|
||||
ifneq ($(NVCC),)
|
||||
$(NVCC) $(NVCCFLAGS) $(CUDAFLAGS) -c $< -o $@
|
||||
else
|
||||
@echo "No CUDA compiler found, skipping CUDA compilation of $<"
|
||||
endif
|
||||
# Rule to compile CUDA source files
|
||||
$(BUILDDIR)/%.cu.o: $(SRCDIR)/%.cu
|
||||
$(NVCC) $(NVCCFLAGS) $(CUDAFLAGS) -Xcompiler -fPIC -c $< -o $@
|
||||
|
||||
# Clean rule
|
||||
clean:
|
||||
$(RM) $(OBJS) $(CU_OBJS) $(TARGET)
|
||||
rm -f $(BUILDDIR)/*.o $(TARGET)
|
||||
|
|
|
|||
BIN
build/cuda.cu.o
Normal file
BIN
build/cuda.cu.o
Normal file
Binary file not shown.
Binary file not shown.
BIN
build/tensor.o
Normal file
BIN
build/tensor.o
Normal file
Binary file not shown.
12
csrc/cuda.cu
12
csrc/cuda.cu
|
|
@ -1,5 +1,7 @@
|
|||
__global__ void add_tensor_cuda(float* data1, float* data2, float* result_data, int size) {
|
||||
int i = blockIdx.x * blockDim.x + threadIdx.x;
|
||||
if (i < size)
|
||||
result_data[i] = data1[i] + data2[i];
|
||||
}
|
||||
#ifdef __CUDACC__
|
||||
__global__ void add_tensor_cuda(float* data1, float* data2, float* result_data, int size) {
|
||||
int i = blockIdx.x * blockDim.x + threadIdx.x;
|
||||
if (i < size)
|
||||
result_data[i] = data1[i] + data2[i];
|
||||
}
|
||||
#endif
|
||||
|
|
@ -1,7 +1,7 @@
|
|||
#ifndef CUDA_KERNEL_H_
|
||||
#define CUDA_KERNEL_H_
|
||||
|
||||
#ifdef __NVCC__
|
||||
#ifdef __CUDACC__
|
||||
__global__ void add_tensor_cuda(float* data1, float* data2, float* result_data, int size);
|
||||
#endif
|
||||
|
||||
|
|
|
|||
|
|
@ -74,7 +74,8 @@ extern "C" {
|
|||
}
|
||||
|
||||
void to_device(Tensor* tensor, char* device) {
|
||||
#ifdef __NVCC__
|
||||
printf("Sending tensor to device: %s\n", device);
|
||||
#ifdef __CUDACC__
|
||||
if ((strcmp(device, "cuda") == 0) && (strcmp(tensor->device, "cpu") == 0)) {
|
||||
|
||||
float* data_tmp;
|
||||
|
|
@ -84,6 +85,7 @@ extern "C" {
|
|||
|
||||
free(tensor_data);
|
||||
tensor->data = data_tmp;
|
||||
tensor->device = device;
|
||||
}
|
||||
|
||||
else if ((strcmp(device, "cpu") == 0) && (strcmp(tensor->device, "cuda") == 0)) {
|
||||
|
|
@ -93,7 +95,10 @@ extern "C" {
|
|||
cudaFree(tensor->data);
|
||||
|
||||
tensor->data = data_tmp;
|
||||
tensor->device = device;
|
||||
}
|
||||
#else
|
||||
printf("Warning: CUDA is not available. Cannot perform GPU operations.\n");
|
||||
#endif
|
||||
}
|
||||
|
||||
|
|
@ -107,7 +112,7 @@ extern "C" {
|
|||
}
|
||||
|
||||
if (strcmp(tensor1->device, tensor2->device) != 0) {
|
||||
fprintf(stderr, "Tensors must be on the same device\n");
|
||||
fprintf(stderr, "Tensors must be on the same device: %s and %s\n", tensor1->device, tensor2->device);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
|
|
@ -165,7 +170,7 @@ extern "C" {
|
|||
shape[i] = tensor1->shape[i];
|
||||
}
|
||||
|
||||
#ifdef __NVCC__
|
||||
#ifdef __CUDACC__
|
||||
if (strcmp(tensor1->device, "cuda") != 0) {
|
||||
|
||||
float* result_data;
|
||||
|
|
@ -289,7 +294,7 @@ extern "C" {
|
|||
}
|
||||
|
||||
if (strcmp(tensor1->device, tensor2->device) != 0) {
|
||||
fprintf(stderr, "Tensors must be on the same device\n");
|
||||
fprintf(stderr, "Tensors must be on the same device: %s and %s\n", tensor1->device, tensor2->device);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
|
|
@ -369,7 +374,7 @@ extern "C" {
|
|||
}
|
||||
|
||||
if (strcmp(tensor1->device, tensor2->device) != 0) {
|
||||
fprintf(stderr, "Tensors must be on the same device\n");
|
||||
fprintf(stderr, "Tensors must be on the same device: %s and %s\n", tensor1->device, tensor2->device);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
|
|
@ -496,5 +501,4 @@ extern "C" {
|
|||
stride *= new_shape[i];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
BIN
csrc/tensor.o
BIN
csrc/tensor.o
Binary file not shown.
Binary file not shown.
|
|
@ -233,7 +233,7 @@ if __name__ == "__main__":
|
|||
import numpy as np
|
||||
|
||||
|
||||
a = Tensor([[1, 2, 3], [1, 2, 3], [1, 2, 3]])
|
||||
a = Tensor([[1, 2, 3], [1, 2, 3], [1, 2, 3]]).to("cuda")
|
||||
print(a ** 2)
|
||||
|
||||
"""#print(a)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue