diff --git a/build/Makefile b/build/Makefile index 84b977e..9d38c99 100644 --- a/build/Makefile +++ b/build/Makefile @@ -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) diff --git a/build/cuda.cu.o b/build/cuda.cu.o new file mode 100644 index 0000000..63691b1 Binary files /dev/null and b/build/cuda.cu.o differ diff --git a/build/libtensor.so b/build/libtensor.so index 5f6e516..80dc3bc 100755 Binary files a/build/libtensor.so and b/build/libtensor.so differ diff --git a/build/tensor.o b/build/tensor.o new file mode 100644 index 0000000..377f3e8 Binary files /dev/null and b/build/tensor.o differ diff --git a/csrc/cuda.cu b/csrc/cuda.cu index 512f68c..dc64303 100644 --- a/csrc/cuda.cu +++ b/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]; -} \ No newline at end of file +#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 \ No newline at end of file diff --git a/csrc/cuda.h b/csrc/cuda.h index 1ac421b..a747fcb 100644 --- a/csrc/cuda.h +++ b/csrc/cuda.h @@ -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 diff --git a/csrc/tensor.cpp b/csrc/tensor.cpp index 0c65d6f..7bf0005 100644 --- a/csrc/tensor.cpp +++ b/csrc/tensor.cpp @@ -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]; } } -} - +} \ No newline at end of file diff --git a/csrc/tensor.o b/csrc/tensor.o deleted file mode 100644 index 633f437..0000000 Binary files a/csrc/tensor.o and /dev/null differ diff --git a/norch/__pycache__/tensor.cpython-38.pyc b/norch/__pycache__/tensor.cpython-38.pyc index 4fbabb1..2068305 100644 Binary files a/norch/__pycache__/tensor.cpython-38.pyc and b/norch/__pycache__/tensor.cpython-38.pyc differ diff --git a/norch/tensor.py b/norch/tensor.py index 07b07e9..779c1c3 100644 --- a/norch/tensor.py +++ b/norch/tensor.py @@ -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)