From 5b4e34b6ecab55a1274d8c7b267ec72ea4f1a2b9 Mon Sep 17 00:00:00 2001 From: "nicolas.marie" <nicolas.marie@ensiie.eu> Date: Thu, 21 Dec 2023 20:18:12 +0100 Subject: [PATCH] correctly read file size & cleanup gpu unused fonctions --- Projet/CODE/apm/src/apm.c | 8 +-- Projet/CODE/apm/src/apm_gpu.cu | 89 ++-------------------------------- Projet/CODE/apm/src/apm_omp.c | 8 +-- 3 files changed, 14 insertions(+), 91 deletions(-) diff --git a/Projet/CODE/apm/src/apm.c b/Projet/CODE/apm/src/apm.c index 8de453a..546a1c5 100644 --- a/Projet/CODE/apm/src/apm.c +++ b/Projet/CODE/apm/src/apm.c @@ -9,6 +9,7 @@ #include <fcntl.h> #include <unistd.h> #include <sys/time.h> +#include <sys/stat.h> #define APM_DEBUG 0 @@ -17,6 +18,7 @@ char * read_input_file(char *filename, size_t *size) { char *buf; + struct stat fs; size_t fsize; int fd = 0; size_t read_bytes = 0; @@ -32,9 +34,9 @@ read_input_file(char *filename, size_t *size) } /* Get the number of characters in the textfile */ - fsize = lseek(fd, 0, SEEK_END); - lseek(fd, 0, SEEK_SET); - /* TODO check return of lseek */ + fstat(fd, &fs); + fsize = fs.st_size; + #if APM_DEBUG printf("File length: %ld\n", fsize); #endif diff --git a/Projet/CODE/apm/src/apm_gpu.cu b/Projet/CODE/apm/src/apm_gpu.cu index 4c070a1..54c0a81 100644 --- a/Projet/CODE/apm/src/apm_gpu.cu +++ b/Projet/CODE/apm/src/apm_gpu.cu @@ -9,6 +9,7 @@ #include <fcntl.h> #include <unistd.h> #include <sys/time.h> +#include <sys/stat.h> #define APM_DEBUG 0 #define NUMBER_THREADS_BY_BLOCK 1024 @@ -32,66 +33,12 @@ __cudaCheckErrors(char const *msg) } } - -char * -read_input_file(char *filename, int *size) -{ - char *buf; - off_t fsize; - int fd = 0; - int n_bytes = 1; - /* 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; - } - - /* Get the number of characters in the textfile */ - fsize = lseek(fd, 0, SEEK_END); - lseek(fd, 0, SEEK_SET); - /* TODO check return of lseek */ -#if APM_DEBUG - printf("File length: %lld\n", fsize); -#endif - /* Allocate data to copy the target text */ - buf = (char *)malloc(fsize * sizeof(char)); - - if (buf == NULL) - { - fprintf(stderr, "Unable to allocate %ld byte(s) for main array\n", - fsize); - return NULL; - } - - n_bytes = read(fd, buf, fsize); - - if (n_bytes != fsize) - { - fprintf(stderr, - "Unable to copy %ld byte(s) from text file " - "(%d byte(s) copied)\n", - fsize, n_bytes); - return NULL; - } - -#if APM_DEBUG - printf("Number of read bytes: %d\n", n_bytes); -#endif - *size = n_bytes; - close(fd); - return buf; -} - off_t get_file_size(int fd) { - off_t size; - size = lseek(fd, 0, SEEK_END); - lseek(fd, 0, SEEK_SET); - return size; + struct stat fs; + fstat(fd, &fs); + return fs.st_size; } char * @@ -142,34 +89,6 @@ read_input_file_max(int fd, int *size, off_t offset) #define MIN3(a, b, c) ((a)<(b) ? ((a)<(c) ? (a) : (c)) : ((b)<(c) ? (b) : (c))) #define MIN2(a, b) ((a)<(b) ? (a) : (b)) -int -levenshtein(char *s1, char *s2, int len, int *column, int approx_factor) -{ - int x, y, lastdiag, olddiag; - - for (y = 1; y <= len; y++) - { - column[y] = y; - } - - for (x = 1; x <= len; x++) - { - column[0] = x; - lastdiag = x - 1; - - for (y = 1; y <= len; y++) - { - olddiag = column[y]; - column[y] = MIN3(column[y] + 1, - column[y - 1] + 1, - lastdiag + (s1[y - 1] == s2[x - 1] ? 0 : 1)); - lastdiag = olddiag; - } - } - - return (column[len] <= approx_factor) ? 1 : 0; -} - __global__ void levenshtein_cu(char *find, char *buf, int len, int n_bytes, int approx_factor, int *result) diff --git a/Projet/CODE/apm/src/apm_omp.c b/Projet/CODE/apm/src/apm_omp.c index 14fd2c6..09a25be 100644 --- a/Projet/CODE/apm/src/apm_omp.c +++ b/Projet/CODE/apm/src/apm_omp.c @@ -9,6 +9,7 @@ #include <fcntl.h> #include <unistd.h> #include <sys/time.h> +#include <sys/stat.h> #include "omp.h" @@ -18,6 +19,7 @@ char * read_input_file(char *filename, size_t *size) { char *buf; + struct stat fs; size_t fsize; int fd = 0; size_t read_bytes = 0; @@ -33,9 +35,9 @@ read_input_file(char *filename, size_t *size) } /* Get the number of characters in the textfile */ - fsize = lseek(fd, 0, SEEK_END); - lseek(fd, 0, SEEK_SET); - /* TODO check return of lseek */ + fstat(fd, &fs); + fsize = fs.st_size; + #if APM_DEBUG printf("File length: %ld\n", fsize); #endif -- GitLab