From bc18a7cb34a5f628ef3a7eaced3544085e8e8340 Mon Sep 17 00:00:00 2001 From: augustin64 Date: Sun, 29 Jan 2023 09:40:55 +0100 Subject: [PATCH] Update utils.c/u --- Makefile | 24 +++++++------- src/cnn/train.c | 6 ++++ src/include/utils.h | 29 +++++++++++++++-- src/utils.c | 74 +++++++++++++++++++++++++++++------------- src/utils.cu | 78 ++++++++++++++++++++++++++++++++------------- 5 files changed, 151 insertions(+), 60 deletions(-) diff --git a/Makefile b/Makefile index d6a9568..5495af5 100644 --- a/Makefile +++ b/Makefile @@ -2,6 +2,7 @@ BUILDDIR := ./build SRCDIR := ./src CACHE_DIR := ./cache NVCC := nvcc +CUDA_INCLUDE := /opt/cuda/include # Default instalmlation path for ArchLinux, may be different NVCC_INSTALLED := $(shell command -v $(NVCC) 2> /dev/null) @@ -59,7 +60,7 @@ $(BUILDDIR)/mnist.o: $(MNIST_SRCDIR)/mnist.c $(MNIST_SRCDIR)/include/mnist.h $(CC) -c $< -o $@ $(CFLAGS) $(BUILDDIR)/mnist.cuda.o: $(MNIST_SRCDIR)/mnist.c $(MNIST_SRCDIR)/include/mnist.h - $(CC) -c $< -o $@ $(CFLAGS) -DUSE_CUDA -lcuda -I/opt/cuda/include + $(CC) -c $< -o $@ $(CFLAGS) -DUSE_CUDA -lcuda -I$(CUDA_INCLUDE) $(BUILDDIR)/mnist_%.o: $(MNIST_SRCDIR)/%.c $(MNIST_SRCDIR)/include/%.h $(CC) -c $< -o $@ $(CFLAGS) @@ -94,21 +95,20 @@ ifdef NVCC_INSTALLED $(BUILDDIR)/cnn-main-cuda: $(BUILDDIR)/cnn_main.cuda.o \ $(BUILDDIR)/cnn_train.cuda.o \ $(BUILDDIR)/cnn_test_network.cuda.o \ - $(BUILDDIR)/cnn_cnn.cuda.o \ + $(BUILDDIR)/cnn_cnn.o \ $(BUILDDIR)/cnn_creation.cuda.o \ - $(BUILDDIR)/cnn_initialisation.cuda.o \ - $(BUILDDIR)/cnn_make.cuda.o \ + $(BUILDDIR)/cnn_initialisation.o \ + $(BUILDDIR)/cnn_make.o \ $(BUILDDIR)/cnn_neuron_io.cuda.o \ - $(BUILDDIR)/cnn_function.cuda.o \ + $(BUILDDIR)/cnn_function.o \ $(BUILDDIR)/cnn_utils.cuda.o \ - $(BUILDDIR)/cnn_update.cuda.o \ + $(BUILDDIR)/cnn_update.o \ $(BUILDDIR)/cnn_free.cuda.o \ $(BUILDDIR)/cnn_jpeg.cuda.o \ $(BUILDDIR)/cnn_cuda_convolution.o \ - $(BUILDDIR)/cnn_backpropagation.cuda.o \ - $(BUILDDIR)/colors.cuda.o \ + $(BUILDDIR)/cnn_backpropagation.o \ + $(BUILDDIR)/colors.o \ $(BUILDDIR)/mnist.cuda.o \ - $(BUILDDIR)/utils.cuda.o \ $(BUILDDIR)/cuda_utils.o $(NVCC) $(NVCCFLAGS) $^ -o $@ else @@ -123,7 +123,7 @@ $(BUILDDIR)/cnn_%.o: $(CNN_SRCDIR)/%.c $(CNN_SRCDIR)/include/%.h $(CC) -c $< -o $@ $(CFLAGS) $(BUILDDIR)/cnn_%.cuda.o: $(CNN_SRCDIR)/%.c $(CNN_SRCDIR)/include/%.h - $(CC) -c $< -o $@ $(CFLAGS) -DUSE_CUDA -lcuda -I/opt/cuda/include + $(CC) -c $< -o $@ $(CFLAGS) -DUSE_CUDA -lcuda -I$(CUDA_INCLUDE) ifdef NVCC_INSTALLED $(BUILDDIR)/cnn_cuda_%.o: $(CNN_SRCDIR)/%.cu $(CNN_SRCDIR)/include/%.h @@ -139,7 +139,7 @@ $(BUILDDIR)/%.o: $(SRCDIR)/%.c $(SRCDIR)/include/%.h $(CC) -c $< -o $@ $(CFLAGS) $(BUILDDIR)/%.cuda.o: $(SRCDIR)/%.c $(SRCDIR)/include/%.h - $(CC) -c $< -o $@ $(CFLAGS) -DUSE_CUDA -lcuda -I/opt/cuda/include + $(CC) -c $< -o $@ $(CFLAGS) -DUSE_CUDA -lcuda -I$(CUDA_INCLUDE) ifdef NVCC_INSTALLED $(BUILDDIR)/cuda_%.o: $(SRCDIR)/%.cu $(SRCDIR)/include/%.h @@ -173,7 +173,7 @@ ifdef NVCC_INSTALLED $(BUILDDIR)/test-cnn_%: test/cnn_%.cu \ $(BUILDDIR)/cnn_cuda_%.o \ $(BUILDDIR)/cuda_utils.o \ - $(BUILDDIR)/colors.cuda.o \ + $(BUILDDIR)/colors.o \ $(BUILDDIR)/mnist.cuda.o $(NVCC) $(NVCCFLAGS) $^ -o $@ else diff --git a/src/cnn/train.c b/src/cnn/train.c index df72b45..ba84848 100644 --- a/src/cnn/train.c +++ b/src/cnn/train.c @@ -94,6 +94,12 @@ void* train_thread(void* parameters) { void train(int dataset_type, char* images_file, char* labels_file, char* data_dir, int epochs, char* out, char* recover) { + #ifdef USE_CUDA + bool compatibility = check_cuda_compatibility(); + if (!compatibility) { + printf("Exiting.\n"); + } + #endif srand(time(NULL)); Network* network; int input_dim = -1; diff --git a/src/include/utils.h b/src/include/utils.h index 77e5862..5bc7ecc 100644 --- a/src/include/utils.h +++ b/src/include/utils.h @@ -1,7 +1,14 @@ #include #include + #ifdef USE_CUDA - #include "cuda_runtime.h" + #ifndef __CUDACC__ + #include "cuda_runtime.h" + #endif +#else + #ifdef __CUDACC__ + #define USE_CUDA + #endif #endif #ifndef DEF_UTILS_CU_H @@ -26,10 +33,28 @@ int i_div_up(int a, int b); /* * Vérification de la compatibilité CUDA */ +#ifdef __CUDACC__ +extern "C" { +#endif bool check_cuda_compatibility(); +#ifdef __CUDACC__ +} +#endif - +#ifdef __CUDACC__ +extern "C" { +#endif void* nalloc(size_t sz); +#ifdef __CUDACC__ +} +#endif +#ifdef __CUDACC__ +extern "C" { +#endif void gree(void* ptr); +#ifdef __CUDACC__ +} +#endif + #endif \ No newline at end of file diff --git a/src/utils.c b/src/utils.c index 6f5c126..5868153 100644 --- a/src/utils.c +++ b/src/utils.c @@ -1,10 +1,11 @@ #include #include -#include - #ifdef USE_CUDA - #include "cuda_runtime.h" + #ifndef __CUDACC__ + #include "cuda_runtime.h" + #endif #endif + #include "include/utils.h" #include "include/colors.h" @@ -13,6 +14,9 @@ int i_div_up(int a, int b) { // Partie entière supérieure de a/b return ((a % b) != 0) ? (a / b + 1) : (a / b); } +#ifdef __CUDACC__ +extern "C" { +#endif bool check_cuda_compatibility() { #ifdef __CUDACC__ int nDevices; @@ -39,28 +43,52 @@ bool check_cuda_compatibility() { return false; #endif } +#ifdef __CUDACC__ +} +#endif + #ifndef USE_CUDA + #ifdef __CUDACC__ + extern "C" { + #endif + void* nalloc(size_t sz) { + void* ptr = malloc(sz); + return ptr; + } + #ifdef __CUDACC__ + } + #endif -void* nalloc(size_t sz) { - void* ptr = malloc(sz); - return ptr; -} - -void gree(void* ptr) { - free(ptr); -} - + #ifdef __CUDACC__ + extern "C" { + #endif + void gree(void* ptr) { + free(ptr); + } + #ifdef __CUDACC__ + } + #endif #else + #ifdef __CUDACC__ + extern "C" { + #endif + void* nalloc(size_t sz) { + void* ptr; + cudaMallocManaged(&ptr, sz, cudaMemAttachHost); + return ptr; + } + #ifdef __CUDACC__ + } + #endif -void* nalloc(size_t sz) { - void* ptr; - cudaMallocManaged(&ptr, sz, cudaMemAttachHost); - return ptr; -} - -void gree(void* ptr) { - cudaFree(ptr); -} - -#endif \ No newline at end of file + #ifdef __CUDACC__ + extern "C" { + #endif + void gree(void* ptr) { + cudaFree(ptr); + } + #ifdef __CUDACC__ + } + #endif +#endif diff --git a/src/utils.cu b/src/utils.cu index a8d4787..5868153 100644 --- a/src/utils.cu +++ b/src/utils.cu @@ -1,5 +1,10 @@ #include #include +#ifdef USE_CUDA + #ifndef __CUDACC__ + #include "cuda_runtime.h" + #endif +#endif #include "include/utils.h" #include "include/colors.h" @@ -9,6 +14,9 @@ int i_div_up(int a, int b) { // Partie entière supérieure de a/b return ((a % b) != 0) ? (a / b + 1) : (a / b); } +#ifdef __CUDACC__ +extern "C" { +#endif bool check_cuda_compatibility() { #ifdef __CUDACC__ int nDevices; @@ -35,28 +43,52 @@ bool check_cuda_compatibility() { return false; #endif } - -#ifndef __CUDACC__ - -void* nalloc(size_t sz) { - void* ptr = malloc(sz); - return ptr; +#ifdef __CUDACC__ } - -void gree(void* ptr) { - free(ptr); -} - -#else - -void* nalloc(size_t sz) { - void* ptr; - cudaMallocManaged(&ptr, sz, cudaMemAttachHost); - return ptr; -} - -void gree(void* ptr) { - cudaFree(ptr); -} - +#endif + + +#ifndef USE_CUDA + #ifdef __CUDACC__ + extern "C" { + #endif + void* nalloc(size_t sz) { + void* ptr = malloc(sz); + return ptr; + } + #ifdef __CUDACC__ + } + #endif + + #ifdef __CUDACC__ + extern "C" { + #endif + void gree(void* ptr) { + free(ptr); + } + #ifdef __CUDACC__ + } + #endif +#else + #ifdef __CUDACC__ + extern "C" { + #endif + void* nalloc(size_t sz) { + void* ptr; + cudaMallocManaged(&ptr, sz, cudaMemAttachHost); + return ptr; + } + #ifdef __CUDACC__ + } + #endif + + #ifdef __CUDACC__ + extern "C" { + #endif + void gree(void* ptr) { + cudaFree(ptr); + } + #ifdef __CUDACC__ + } + #endif #endif