From 6932e610811f16bbe4a88b6e5da7fe43b56a1baf Mon Sep 17 00:00:00 2001 From: "nicolas.marie" <nicolas.marie@ensiie.eu> Date: Thu, 21 Dec 2023 23:11:22 +0100 Subject: [PATCH] add error checking function to apm_omp, fixe memory leaks in amp_gpu --- Projet/CODE/apm/src/apm.c | 1 + Projet/CODE/apm/src/apm_gpu.cu | 4 +- Projet/CODE/apm/src/apm_omp.c | 104 ++++++++++++--------------------- 3 files changed, 41 insertions(+), 68 deletions(-) diff --git a/Projet/CODE/apm/src/apm.c b/Projet/CODE/apm/src/apm.c index 7fa4baa..802e016 100644 --- a/Projet/CODE/apm/src/apm.c +++ b/Projet/CODE/apm/src/apm.c @@ -12,6 +12,7 @@ #include <sys/stat.h> #include <stdarg.h> + #define APM_DEBUG 0 // can't be inline as it is a variadic function diff --git a/Projet/CODE/apm/src/apm_gpu.cu b/Projet/CODE/apm/src/apm_gpu.cu index 555e769..85d58e5 100644 --- a/Projet/CODE/apm/src/apm_gpu.cu +++ b/Projet/CODE/apm/src/apm_gpu.cu @@ -57,7 +57,7 @@ read_input_file_max(int fd, int *size, off_t offset) printf("diff: %i\n", to_read); #endif /* Allocate data to copy the target text */ - buf = (char *)malloc(fsize * sizeof(char)); + buf = (char *) malloc(fsize * sizeof(char)); if (buf == NULL) { @@ -303,6 +303,8 @@ main(int argc, char **argv) offset += (MAX_BUFFER_SIZE - size_pattern + 1); cudaFree(buf_dev); __cudaCheckErrors("Unable to free memory for dev on device"); + + free(buf); // TODO check errors } //free memory - and then get onto the next pattern. diff --git a/Projet/CODE/apm/src/apm_omp.c b/Projet/CODE/apm/src/apm_omp.c index 09a25be..4cc6499 100644 --- a/Projet/CODE/apm/src/apm_omp.c +++ b/Projet/CODE/apm/src/apm_omp.c @@ -10,11 +10,29 @@ #include <unistd.h> #include <sys/time.h> #include <sys/stat.h> +#include <stdarg.h> #include "omp.h" #define APM_DEBUG 0 +// can't be inline as it is a variadic function +void +__checkErrors(const int isBad, const char *fmt, ...) +{ + va_list args; + + if (isBad) + { + va_start(args, fmt); + vfprintf(stderr, fmt, args); + va_end(args); + fputc('\n', stderr); + exit(EXIT_FAILURE); + } +} + + char * read_input_file(char *filename, size_t *size) { @@ -27,15 +45,11 @@ read_input_file(char *filename, size_t *size) /* Open the text file */ fd = open(filename, O_RDONLY); - - if (fd == -1) - { - fprintf(stderr, "Unable to open the text file <%s>\n", filename); - return NULL; - } + __checkErrors(fd == -1, "Unable to open the text file <%s>\n", filename); /* Get the number of characters in the textfile */ - fstat(fd, &fs); + int stat = fstat(fd, &fs); + __checkErrors(stat < 0, "Stat of file <%s> failed\n", filename); fsize = fs.st_size; #if APM_DEBUG @@ -44,13 +58,8 @@ read_input_file(char *filename, size_t *size) /* Allocate data to copy the target text */ buf = malloc(fsize * sizeof(char)); - - if (buf == NULL) - { - fprintf(stderr, "Unable to allocate %ld byte(s) for main array\n", - fsize); - return NULL; - } + __checkErrors(buf == NULL, + "Unable to allocate %ld byte(s) for main array\n", fsize); do { @@ -59,14 +68,8 @@ read_input_file(char *filename, size_t *size) } while (read_bytes != 0); - if (total_bytes != fsize) - { - fprintf(stderr, - "Unable to copy %ld byte(s) from text file " - "(%ld byte(s) copied)\n", - fsize, total_bytes); - return NULL; - } + __checkErrors(total_bytes != fsize, "Unable to copy %ld byte(s) " + "from text file (%ld byte(s) copied)\n", fsize, total_bytes); #if APM_DEBUG printf("Number of read bytes: %ld\n", total_bytes); @@ -135,33 +138,18 @@ main(int argc, char **argv) filename = argv[2];/* Grab the filename containing the target text */ nb_patterns = argc - 3;/* Get the number of patterns to search for */ pattern = malloc(nb_patterns * sizeof(char *)); - - if (pattern == NULL)/*Fill the pattern*/ - { - fprintf(stderr, - "Unable to allocate array of pattern of size %ld\n", - nb_patterns); - return 1; - } + __checkErrors(pattern == NULL, "Unable to allocate array " + "of pattern of size %ld\n", nb_patterns); for (i = 0; i < nb_patterns; i++) /* Grab the patterns */ { int l; l = strlen(argv[i + 3]); - - if (l <= 0) - { - fprintf(stderr, "Error while parsing argument %ld\n", i + 3); - return 1; - } + __checkErrors(l <= 0, "Error while parsing argument %ld\n", i + 3); pattern[i] = (char *)malloc((l + 1) * sizeof(char)); - - if (pattern[i] == NULL) - { - fprintf(stderr, "Unable to allocate string of size %d\n", l); - return 1; - } + __checkErrors(pattern[i] == NULL, "Unable to allocate string " + "of size %d\n", l); strncpy(pattern[i], argv[i + 3], (l + 1)); } @@ -169,26 +157,13 @@ main(int argc, char **argv) printf("Approximate Pattern Mathing: " "looking for %ld pattern(s) in file %s w/ distance of %d\n", nb_patterns, filename, approx_factor); - buf = read_input_file(filename, &n_bytes); - if (buf == NULL) - { - fprintf(stderr, "Error: NULL pointer from reading input file."); - return 1; - } + buf = read_input_file(filename, &n_bytes); + __checkErrors(buf == NULL, "Error: NULL pointer from reading input file."); n_matches = malloc(nb_patterns * sizeof(size_t));/*Alloc the matches*/ - - if (n_matches == NULL) - { - fprintf(stderr, "Error: unable to allocate memory for %ldB\n", - nb_patterns * sizeof(int)); - return 1; - } - -#if APM_DEBUG - printf("Number of threads: %d\n", num_threads); -#endif + __checkErrors(n_matches == NULL, "Error: unable to " + "allocate memory for %ldB\n", nb_patterns * sizeof(int)); /***** * BEGIN MAIN LOOP @@ -206,14 +181,9 @@ main(int argc, char **argv) { unsigned int *column; column = malloc((size_pattern + 1) * sizeof(unsigned int)); - - if (column == NULL) - { - fprintf(stderr, - "Error: unable to allocate memory for column (%ldB)\n", - (size_pattern + 1) * sizeof(unsigned int)); - exit(EXIT_FAILURE); - } + __checkErrors(column == NULL, + "Error: unable to allocate memory for column (%ldB)\n", + (size_pattern + 1) * sizeof(unsigned int)); #pragma omp for schedule(dynamic, 40960) -- GitLab