1
Fork 0
mirror of https://github.com/Steffo99/unimore-hpc-assignments.git synced 2024-11-22 08:04:25 +00:00

Use unsigned int as indexes to make overflows harder

This commit is contained in:
Steffo 2022-11-29 17:24:48 +01:00 committed by GitHub
parent 029010045c
commit ee33d3861b
Signed by: github
GPG key ID: 4AEE18F83AFDEB23

View file

@ -28,7 +28,7 @@
__host__ static void init_array(DATA_TYPE** A, DATA_TYPE* X) __host__ static void init_array(DATA_TYPE** A, DATA_TYPE* X)
{ {
/* X = [ 3.14, 6.28, 9.42, ... ] */ /* X = [ 3.14, 6.28, 9.42, ... ] */
for (int y = 0; y < NY; y++) for (unsigned int y = 0; y < NY; y++)
{ {
X[y] = y * M_PI; X[y] = y * M_PI;
} }
@ -42,11 +42,11 @@ __host__ static void init_array(DATA_TYPE** A, DATA_TYPE* X)
* ... * ...
* ] * ]
*/ */
for (int x = 0; x < NX; x++) for (unsigned int x = 0; x < NX; x++)
{ {
for (int y = 0; y < NY; y++) for (unsigned int y = 0; y < NY; y++)
{ {
A[x][y] = ((DATA_TYPE)x * (y + 1)) / NX; A[x][y] = (DATA_TYPE)(x * (y + 1)) / NX;
} }
} }
} }
@ -61,7 +61,7 @@ __host__ static void init_array(DATA_TYPE** A, DATA_TYPE* X)
*/ */
__host__ static void print_array(DATA_TYPE* Y) __host__ static void print_array(DATA_TYPE* Y)
{ {
for (int x = 0; x < NX; x++) for (unsigned int x = 0; x < NX; x++)
{ {
fprintf(stderr, DATA_PRINTF_MODIFIER, Y[x]); fprintf(stderr, DATA_PRINTF_MODIFIER, Y[x]);
} }
@ -78,21 +78,21 @@ __host__ static void print_array(DATA_TYPE* Y)
*/ */
__host__ static void kernel_atax(DATA_TYPE** A, DATA_TYPE* X, DATA_TYPE* Y) __host__ static void kernel_atax(DATA_TYPE** A, DATA_TYPE* X, DATA_TYPE* Y)
{ {
for (int x = 0; x < NY; x++) for (unsigned int x = 0; x < NY; x++)
{ {
Y[i] = 0; Y[i] = 0;
} }
for (int i = 0; i < NX; i++) for (unsigned int i = 0; i < NX; i++)
{ {
DATA_TYPE tmp = 0; DATA_TYPE tmp = 0;
for (int j = 0; j < NY; j++) for (unsigned int j = 0; j < NY; j++)
{ {
tmp += A[i][j] * X[j]; tmp += A[i][j] * X[j];
} }
for (int j = 0; j < NY; j++) for (unsigned int j = 0; j < NY; j++)
{ {
Y[j] = Y[j] + A[i][j] * tmp; Y[j] = Y[j] + A[i][j] * tmp;
} }