From 383d3bebfd7323e1233312edb1b4a731563fd876 Mon Sep 17 00:00:00 2001 From: augustin64 Date: Sun, 19 Feb 2023 15:01:58 +0100 Subject: [PATCH] Check device memory block size & add pretty-prints & remove some warnings --- src/cnn/train.c | 20 +++++++++++----- src/colors.c | 56 ++++++++++++++++++++++++++++++++++++++++++++ src/include/colors.h | 35 +++++++++++++++++++++++++++ src/utils.c | 18 +++++++++++--- src/utils.cu | 18 +++++++++++--- 5 files changed, 135 insertions(+), 12 deletions(-) diff --git a/src/cnn/train.c b/src/cnn/train.c index e39b391..fdd74f0 100644 --- a/src/cnn/train.c +++ b/src/cnn/train.c @@ -55,7 +55,7 @@ void* train_thread(void* parameters) { maxi = indice_max(network->input[network->size-1][0][0], 10); if (maxi == -1) { printf("\n"); - printf_error("Le réseau sature.\n"); + printf_error((char*)"Le réseau sature.\n"); exit(1); } @@ -224,7 +224,9 @@ void train(int dataset_type, char* images_file, char* labels_file, char* data_di elapsed_time = end_time - start_time; printf("Taux d'apprentissage initial: %lf\n", network->learning_rate); - printf("Initialisation: %0.2lf s\n\n", elapsed_time); + printf("Initialisation: "); + printf_time(elapsed_time); + printf("\n\n"); for (int i=0; i < epochs; i++) { @@ -314,7 +316,7 @@ void train(int dataset_type, char* images_file, char* labels_file, char* data_di update_weights(network, network); update_bias(network, network); - printf("\rÉpoque [%d/%d]\tImage [%d/%d]\tAccuracy: "YELLOW"%0.4f%%"RESET, i, epochs, BATCHES*(j+1), nb_images_total, current_accuracy*100); + printf("\rÉpoque [%d/%d]\tImage [%d/%d]\tAccuracy: " YELLOW "%0.4f%%" RESET, i, epochs, BATCHES*(j+1), nb_images_total, current_accuracy*100); fflush(stdout); #endif // Il serait intéressant d'utiliser la perte calculée pour @@ -324,9 +326,13 @@ void train(int dataset_type, char* images_file, char* labels_file, char* data_di end_time = omp_get_wtime(); elapsed_time = end_time - start_time; #ifdef USE_MULTITHREADING - printf("\rThreads [%d]\tÉpoque [%d/%d]\tImage [%d/%d]\tAccuracy: " GREEN "%0.4f%%" RESET " \tTemps: %0.2f s\n", nb_threads, i, epochs, nb_images_total, nb_images_total, accuracy*100, elapsed_time); + printf("\rThreads [%d]\tÉpoque [%d/%d]\tImage [%d/%d]\tAccuracy: " GREEN "%0.4f%%" RESET " \tTemps: ", nb_threads, i, epochs, nb_images_total, nb_images_total, accuracy*100); + printf_time(elapsed_time); + printf("\n"); #else - printf("\rÉpoque [%d/%d]\tImage [%d/%d]\tAccuracy: "GREEN"%0.4f%%"RESET" \tTemps: %0.2f s\n", i, epochs, nb_images_total, nb_images_total, accuracy*100, elapsed_time); + printf("\rÉpoque [%d/%d]\tImage [%d/%d]\tAccuracy: " GREEN "%0.4f%%" RESET " \tTemps: ", i, epochs, nb_images_total, nb_images_total, accuracy*100); + printf_time(elapsed_time); + printf("\n"); #endif write_network(out, network); } @@ -363,5 +369,7 @@ void train(int dataset_type, char* images_file, char* labels_file, char* data_di end_time = omp_get_wtime(); elapsed_time = end_time - algo_start; - printf("\nTemps total: %0.1f s\n", elapsed_time); + printf("\nTemps total: "); + printf_time(elapsed_time); + printf("\n"); } diff --git a/src/colors.c b/src/colors.c index 05c7328..f4bcaab 100644 --- a/src/colors.c +++ b/src/colors.c @@ -1,4 +1,5 @@ #include +#include #include "include/colors.h" @@ -12,4 +13,59 @@ void printf_warning(char* string) { void printf_info(char* string) { printf(BOLDBLUE "[ INFO ]" RESET " %s", string); +} + +void printf_time(float time) { + int hours = time/3600; + int minutes = ((int)time %3600)/60; + int seconds = ((int)time) %60; + int milliseconds = (time - (int)time)*1000; + + if (hours != 0) { + printf("%dh %dmn", hours, minutes); + } else if (minutes != 0) { + printf("%dmn %ds", minutes, seconds); + } else if (seconds != 0) { + printf("%ds %dms", seconds, milliseconds); + } else { + printf("%dms", milliseconds); + } +} + +void printf_memory(size_t size) { + size_t gigabytes = size/(1024*1024*1024); + size_t megabytes = size/(1024*1024) %1024; + size_t kilobytes = size/1024 %1024; + size_t bytes = size %1024; + + bool is_null = true; + + if (gigabytes != 0) { + printf("%ldGB", gigabytes); + is_null = false; + } + if (megabytes != 0) { + if (!is_null) { + printf(" "); + } + printf("%ldMB", megabytes); + is_null = false; + } + if (kilobytes != 0) { + if (!is_null) { + printf(" "); + } + printf("%ldkB", kilobytes); + is_null = false; + } + if (bytes != 0) { + if (!is_null) { + printf(" "); + } + printf("%ldB", bytes); + is_null = false; + } + if (is_null) { + printf("OB"); + } } \ No newline at end of file diff --git a/src/include/colors.h b/src/include/colors.h index 58ad2fd..f0bd7e8 100644 --- a/src/include/colors.h +++ b/src/include/colors.h @@ -21,8 +21,43 @@ #define BOLDCYAN "\033[1m\033[36m" /* Bold Cyan */ #define BOLDWHITE "\033[1m\033[37m" /* Bold White */ +#ifdef __CUDACC__ +extern "C" +#endif +/* +* Affiche le texte demandé, précédé d'un [ERROR] en rouge +*/ void printf_error(char* string); + +#ifdef __CUDACC__ +extern "C" +#endif +/* +* Affiche le texte demandé, précédé d'un [WARNING] en orange +*/ void printf_warning(char* string); + +#ifdef __CUDACC__ +extern "C" +#endif +/* +* Affiche le texte demandé, précédé d'un [INFO] en bleu +*/ void printf_info(char* string); +#ifdef __CUDACC__ +extern "C" +#endif +/* +* Affiche un timing en heures minutes secondes millisecondes en limitant la précision aux deux unités les plus significatives +*/ +void printf_time(float time); + +#ifdef __CUDACC__ +extern "C" +#endif +/* +* Affiche une quantité de mémoire de manière humainement lisible +*/ +void printf_memory(size_t size); #endif \ No newline at end of file diff --git a/src/utils.c b/src/utils.c index e613a13..8681646 100644 --- a/src/utils.c +++ b/src/utils.c @@ -6,9 +6,11 @@ #endif #endif -#include "include/utils.h" +#include "include/memory_management.h" #include "include/colors.h" +#include "include/utils.h" + 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); @@ -32,11 +34,21 @@ bool check_cuda_compatibility() { for (int i=0; i < nDevices; i++) { cudaGetDeviceProperties(&prop, i); - printf(" - %s\n", prop.name); + printf(" - %s\n\t - Compute Capability: %d.%d\n\t - Memory available: ", prop.name, prop.major, prop.minor); + printf_memory(prop.totalGlobalMem); + printf("\n\t - Shared Memory per block: "); + printf_memory(prop.sharedMemPerBlock); + printf("\n\n"); } cudaGetDeviceProperties(&prop, 0); - printf("Utilisation du GPU: " BLUE "%s" RESET " (Compute capability: %d.%d)\n\n", prop.name, prop.major, prop.minor); + printf("Utilisation du GPU: " BLUE "%s" RESET "\n\n", prop.name); + + if (prop.sharedMemPerBlock != MEMORY_BLOCK) { + printf_warning((char*)"La taille des blocs mémoire du GPU et celle utilisée dans le code diffèrent.\n"); + printf("\tCela peut mener à une utilisation supplémentaire de VRAM.\n"); + printf("\tChanger MEMORY_BLOCK à %ld dans src/include/memory_management.h\n", prop.sharedMemPerBlock); + } return true; #else printf("Pas d'utilisation du GPU\n\n"); diff --git a/src/utils.cu b/src/utils.cu index e613a13..8681646 100644 --- a/src/utils.cu +++ b/src/utils.cu @@ -6,9 +6,11 @@ #endif #endif -#include "include/utils.h" +#include "include/memory_management.h" #include "include/colors.h" +#include "include/utils.h" + 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); @@ -32,11 +34,21 @@ bool check_cuda_compatibility() { for (int i=0; i < nDevices; i++) { cudaGetDeviceProperties(&prop, i); - printf(" - %s\n", prop.name); + printf(" - %s\n\t - Compute Capability: %d.%d\n\t - Memory available: ", prop.name, prop.major, prop.minor); + printf_memory(prop.totalGlobalMem); + printf("\n\t - Shared Memory per block: "); + printf_memory(prop.sharedMemPerBlock); + printf("\n\n"); } cudaGetDeviceProperties(&prop, 0); - printf("Utilisation du GPU: " BLUE "%s" RESET " (Compute capability: %d.%d)\n\n", prop.name, prop.major, prop.minor); + printf("Utilisation du GPU: " BLUE "%s" RESET "\n\n", prop.name); + + if (prop.sharedMemPerBlock != MEMORY_BLOCK) { + printf_warning((char*)"La taille des blocs mémoire du GPU et celle utilisée dans le code diffèrent.\n"); + printf("\tCela peut mener à une utilisation supplémentaire de VRAM.\n"); + printf("\tChanger MEMORY_BLOCK à %ld dans src/include/memory_management.h\n", prop.sharedMemPerBlock); + } return true; #else printf("Pas d'utilisation du GPU\n\n");