Update utils.c/u

This commit is contained in:
augustin64 2023-01-29 09:40:55 +01:00
parent 8662704faa
commit bc18a7cb34
5 changed files with 151 additions and 60 deletions

View File

@ -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

View File

@ -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;

View File

@ -1,7 +1,14 @@
#include <stdio.h>
#include <stdbool.h>
#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

View File

@ -1,10 +1,11 @@
#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>
#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
#ifdef __CUDACC__
extern "C" {
#endif
void gree(void* ptr) {
cudaFree(ptr);
}
#ifdef __CUDACC__
}
#endif
#endif

View File

@ -1,5 +1,10 @@
#include <stdlib.h>
#include <stdio.h>
#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