From 3b9ad3db4d3c4fda559bf5375bc65cdcbf77d8b6 Mon Sep 17 00:00:00 2001 From: julienChemillier Date: Sun, 14 May 2023 15:56:58 +0200 Subject: [PATCH] Merge of function in 'src/common/include/utils.h' --- src/cnn/backpropagation.c | 5 ----- src/cnn/backpropagation.cu | 5 ----- src/cnn/convolution.c | 7 ++----- src/cnn/convolution.cu | 11 ++--------- src/cnn/include/convolution.h | 6 ------ src/cnn/include/make.h | 6 ------ src/cnn/make.c | 11 ++++------- src/cnn/make.cu | 11 ++++------- src/common/include/utils.h | 7 +++++++ src/common/utils.c | 3 +++ 10 files changed, 22 insertions(+), 50 deletions(-) diff --git a/src/cnn/backpropagation.c b/src/cnn/backpropagation.c index 12a231c..2285de3 100644 --- a/src/cnn/backpropagation.c +++ b/src/cnn/backpropagation.c @@ -10,11 +10,6 @@ #include "include/config.h" -int not_outside(int x, int y, int lower_bound, int upper_bound) { - return !(x < lower_bound || y < lower_bound || x >= upper_bound || y>= upper_bound); -} - - /* * Softmax backward MSE */ diff --git a/src/cnn/backpropagation.cu b/src/cnn/backpropagation.cu index 12a231c..2285de3 100644 --- a/src/cnn/backpropagation.cu +++ b/src/cnn/backpropagation.cu @@ -10,11 +10,6 @@ #include "include/config.h" -int not_outside(int x, int y, int lower_bound, int upper_bound) { - return !(x < lower_bound || y < lower_bound || x >= upper_bound || y>= upper_bound); -} - - /* * Softmax backward MSE */ diff --git a/src/cnn/convolution.c b/src/cnn/convolution.c index be96644..9ad5450 100644 --- a/src/cnn/convolution.c +++ b/src/cnn/convolution.c @@ -10,9 +10,6 @@ #ifdef __CUDACC__ __host__ __device__ #endif -int convolution_not_outside(int x, int y, int lower_bound, int upper_bound) { - return !(x < lower_bound || y < lower_bound || x >= upper_bound || y>= upper_bound); -} void make_convolution_cpu(Kernel_cnn* kernel, float*** input, float*** output, int output_width, int stride, int padding) { // c'est le kernel de input @@ -34,7 +31,7 @@ void make_convolution_cpu(Kernel_cnn* kernel, float*** input, float*** output, i for (int c=-padding; c < max_move; c++) { // colonne du filtre int x = (stride*j+b); int y = (stride*k+c); - if (convolution_not_outside(x, y, 0, input_width)) { + if (not_outside(x, y, 0, input_width)) { f += kernel->weights[a][i][b][c]*input[a][stride*j+b][stride*k+c]; } } @@ -67,7 +64,7 @@ __global__ void make_convolution_kernel(Kernel_cnn* kernel, float*** input, floa for (int c=-padding; c < max_move; c++) { int idy_2 = idy*stride+b; int idz_2 = idz*stride+c; - if (convolution_not_outside(idy_2, idz_2, 0, input_width)) { + if (not_outside(idy_2, idz_2, 0, input_width)) { f += kernel->weights[a][idx][b][c]*input[a][idy_2][idz_2]; } } diff --git a/src/cnn/convolution.cu b/src/cnn/convolution.cu index b37afa9..9ad5450 100644 --- a/src/cnn/convolution.cu +++ b/src/cnn/convolution.cu @@ -10,13 +10,6 @@ #ifdef __CUDACC__ __host__ __device__ #endif -int convolution_not_outside(int x, int y, int lower_bound, int upper_bound) { - // On renvoie true si et seulement si _ et _: - // lower_bound <= x < upper_bound - // lower_bound <= y < upper_bound - - return !(x < lower_bound || y < lower_bound || x >= upper_bound || y>= upper_bound); -} void make_convolution_cpu(Kernel_cnn* kernel, float*** input, float*** output, int output_width, int stride, int padding) { // c'est le kernel de input @@ -38,7 +31,7 @@ void make_convolution_cpu(Kernel_cnn* kernel, float*** input, float*** output, i for (int c=-padding; c < max_move; c++) { // colonne du filtre int x = (stride*j+b); int y = (stride*k+c); - if (convolution_not_outside(x, y, 0, input_width)) { + if (not_outside(x, y, 0, input_width)) { f += kernel->weights[a][i][b][c]*input[a][stride*j+b][stride*k+c]; } } @@ -71,7 +64,7 @@ __global__ void make_convolution_kernel(Kernel_cnn* kernel, float*** input, floa for (int c=-padding; c < max_move; c++) { int idy_2 = idy*stride+b; int idz_2 = idz*stride+c; - if (convolution_not_outside(idy_2, idz_2, 0, input_width)) { + if (not_outside(idy_2, idz_2, 0, input_width)) { f += kernel->weights[a][idx][b][c]*input[a][idy_2][idz_2]; } } diff --git a/src/cnn/include/convolution.h b/src/cnn/include/convolution.h index ed90309..0d555ed 100644 --- a/src/cnn/include/convolution.h +++ b/src/cnn/include/convolution.h @@ -3,12 +3,6 @@ #ifdef __CUDACC__ __host__ __device__ #endif -/* -On renvoie true si et seulement si _ et _: -lower_bound <= x < upper_bound -lower_bound <= y < upper_bound -*/ -int convolution_not_outside(int x, int y, int lower_bound, int upper_bound); /* * Effectue la convolution naïvement sur le processeur diff --git a/src/cnn/include/make.h b/src/cnn/include/make.h index e878fda..a115079 100644 --- a/src/cnn/include/make.h +++ b/src/cnn/include/make.h @@ -6,12 +6,6 @@ #ifdef __CUDACC__ __host__ __device__ #endif -/* -* On renvoie true si et seulement si _ et _: -* lower_bound <= y < upper_bound -* lower_bound <= x < upper_bound -*/ -int pooling_not_outside(int x, int y, int lower_bound, int upper_bound); /* * Effectue la propagation d'une convolution avec stride et padding choisis sur le processeur diff --git a/src/cnn/make.c b/src/cnn/make.c index 4dfc55f..3cdc8bd 100644 --- a/src/cnn/make.c +++ b/src/cnn/make.c @@ -13,9 +13,6 @@ #ifdef __CUDACC__ __host__ __device__ #endif -int pooling_not_outside(int x, int y, int lower_bound, int upper_bound) { - return !(x < lower_bound || y < lower_bound || x >= upper_bound || y>= upper_bound); -} /* * Average Pooling @@ -40,7 +37,7 @@ __global__ void make_average_pooling_kernel(float*** input, float*** output, int for (int b=-padding; b < max_move; b++) { int idy_2 = stride*idy +a; int idz_2 = stride*idz +b; - if (pooling_not_outside(idy_2, idz_2, 0, input_width)) { + if (not_outside(idy_2, idz_2, 0, input_width)) { sum += input[idx][idy_2][idz_2]; nb_elements++; } @@ -75,7 +72,7 @@ void make_average_pooling_cpu(float*** input, float*** output, int size, int out for (int b=-padding; b < max_move; b++) { int j_2 = stride*j +a; int k_2 = stride*k +b; - if (pooling_not_outside(j_2, k_2, 0, input_width)) { + if (not_outside(j_2, k_2, 0, input_width)) { sum += input[i][j_2][k_2]; nb_elements++; } @@ -125,7 +122,7 @@ __global__ void make_max_pooling_kernel(float*** input, float*** output, int siz for (int b=-padding; b < max_move; b++) { int idy_2 = stride*idy +a; int idz_2 = stride*idz +b; - if (pooling_not_outside(idy_2, idz_2, 0, input_width)) { + if (not_outside(idy_2, idz_2, 0, input_width)) { temp = input[idx][idy_2][idz_2]; m = m > temp ? m : temp; // max(m, temp) } @@ -159,7 +156,7 @@ void make_max_pooling_cpu(float*** input, float*** output, int size, int output_ for (int b=-padding; b < max_move; b++) { int j_2 = stride*j +a; int k_2 = stride*k +b; - if (pooling_not_outside(j_2, k_2, 0, input_width)) { + if (not_outside(j_2, k_2, 0, input_width)) { m = fmaxf(m, input[i][j_2][k_2]); } } diff --git a/src/cnn/make.cu b/src/cnn/make.cu index 4dfc55f..3cdc8bd 100644 --- a/src/cnn/make.cu +++ b/src/cnn/make.cu @@ -13,9 +13,6 @@ #ifdef __CUDACC__ __host__ __device__ #endif -int pooling_not_outside(int x, int y, int lower_bound, int upper_bound) { - return !(x < lower_bound || y < lower_bound || x >= upper_bound || y>= upper_bound); -} /* * Average Pooling @@ -40,7 +37,7 @@ __global__ void make_average_pooling_kernel(float*** input, float*** output, int for (int b=-padding; b < max_move; b++) { int idy_2 = stride*idy +a; int idz_2 = stride*idz +b; - if (pooling_not_outside(idy_2, idz_2, 0, input_width)) { + if (not_outside(idy_2, idz_2, 0, input_width)) { sum += input[idx][idy_2][idz_2]; nb_elements++; } @@ -75,7 +72,7 @@ void make_average_pooling_cpu(float*** input, float*** output, int size, int out for (int b=-padding; b < max_move; b++) { int j_2 = stride*j +a; int k_2 = stride*k +b; - if (pooling_not_outside(j_2, k_2, 0, input_width)) { + if (not_outside(j_2, k_2, 0, input_width)) { sum += input[i][j_2][k_2]; nb_elements++; } @@ -125,7 +122,7 @@ __global__ void make_max_pooling_kernel(float*** input, float*** output, int siz for (int b=-padding; b < max_move; b++) { int idy_2 = stride*idy +a; int idz_2 = stride*idz +b; - if (pooling_not_outside(idy_2, idz_2, 0, input_width)) { + if (not_outside(idy_2, idz_2, 0, input_width)) { temp = input[idx][idy_2][idz_2]; m = m > temp ? m : temp; // max(m, temp) } @@ -159,7 +156,7 @@ void make_max_pooling_cpu(float*** input, float*** output, int size, int output_ for (int b=-padding; b < max_move; b++) { int j_2 = stride*j +a; int k_2 = stride*k +b; - if (pooling_not_outside(j_2, k_2, 0, input_width)) { + if (not_outside(j_2, k_2, 0, input_width)) { m = fmaxf(m, input[i][j_2][k_2]); } } diff --git a/src/common/include/utils.h b/src/common/include/utils.h index 7c0e8f9..6758feb 100644 --- a/src/common/include/utils.h +++ b/src/common/include/utils.h @@ -39,6 +39,13 @@ int max(int a, int b); #endif +/* +* On renvoie true si et seulement si _ et _: +* lower_bound <= x < upper_bound +* lower_bound <= y < upper_bound +*/ +int not_outside(int x, int y, int lower_bound, int upper_bound); + /* * Partie entière supérieure de a/b */ diff --git a/src/common/utils.c b/src/common/utils.c index 03ee100..76e612e 100644 --- a/src/common/utils.c +++ b/src/common/utils.c @@ -26,6 +26,9 @@ int max(int a, int b) { } #endif +int not_outside(int x, int y, int lower_bound, int upper_bound) { + return !(x < lower_bound || y < lower_bound || x >= upper_bound || y>= upper_bound); +} 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);