diff --git a/Projet/CODE/apm/src/apm.c b/Projet/CODE/apm/src/apm.c index 546a1c5a500ccdf42dc93931ad1245c0d357a569..7fa4baaa69a3824e367c131a174fe58ee9351e33 100644 --- a/Projet/CODE/apm/src/apm.c +++ b/Projet/CODE/apm/src/apm.c @@ -10,10 +10,27 @@ #include <unistd.h> #include <sys/time.h> #include <sys/stat.h> - +#include <stdarg.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) { @@ -26,15 +43,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 @@ -43,13 +56,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 { @@ -58,14 +66,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); @@ -134,33 +136,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)); } @@ -168,22 +155,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; - } + __checkErrors(n_matches == NULL, "Error: unable to " + "allocate memory for %ldB\n", nb_patterns * sizeof(int)); /***** * BEGIN MAIN LOOP @@ -197,14 +175,9 @@ main(int argc, char **argv) unsigned int *column; n_matches[i] = 0; 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)); - return 1; - } + __checkErrors(column == NULL, "Error: unable to " + "allocate memory for column (%ldB)\n", + (size_pattern + 1) * sizeof(unsigned int)); for (j = 0; j < n_bytes - size_pattern; j++) { diff --git a/Projet/CODE/apm/src/apm_gpu.cu b/Projet/CODE/apm/src/apm_gpu.cu index 95ecb2e2238ad39e47b98f8cd12dccfbcab0cd96..555e769cfc1c41114f3689d0eeb46702e74f056d 100644 --- a/Projet/CODE/apm/src/apm_gpu.cu +++ b/Projet/CODE/apm/src/apm_gpu.cu @@ -20,8 +20,7 @@ //If you get out of memory errors, you should reduce this value -inline -void +inline void __cudaCheckErrors(char const *msg) { cudaError_t cu_err; @@ -285,7 +284,7 @@ main(int argc, char **argv) __cudaCheckErrors("Unable to copy buffer onto device"); NB = (n_bytes / NTBB) + (((n_bytes % NTBB) > 0) ? 1 : 0); - + cudaMemset(result_dev, 0, sizeof(int)); __cudaCheckErrors("Unable to init result to 0 on device");