Skip to content

Commit 050f42a

Browse files
Modified integer version of mp, from int32 to int64
1 parent dc8be70 commit 050f42a

File tree

10 files changed

+563
-2108
lines changed

10 files changed

+563
-2108
lines changed

src/data/Matrix_A.txt

Lines changed: 129 additions & 513 deletions
Large diffs are not rendered by default.

src/data/Matrix_B.txt

Lines changed: 129 additions & 513 deletions
Large diffs are not rendered by default.

src/data/Matrix_C.txt

Lines changed: 129 additions & 513 deletions
Large diffs are not rendered by default.

src/data/Matrix_D.txt

Lines changed: 129 additions & 513 deletions
Large diffs are not rendered by default.

src/main.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -518,6 +518,9 @@ int main(){
518518
// clean_file_blocks_MP();
519519
// benchmark_blocks_MP(p, u_overline);
520520

521+
// clean_file_float_integer();
522+
// benchmark_float_integer(p, u_overline, u_b);
523+
521524

522525
return 0;
523526
}

src/main_test.c

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -455,20 +455,19 @@ int main(int argc, char** argv){
455455
fesetround(FE_TONEAREST);
456456
u_int32_t u_b = (int) (pow(2, 54) / p); // Constant for Barrett
457457

458-
int n = 512;
458+
int n = 128;
459459
int bitsize_p = get_bitsize(p);
460460
int b = get_blocksize(bitsize_p, n);
461461

462462
double* A = random_matrix_1D(n, p);
463463
double* B = random_matrix_1D(n, p);
464-
u_int32_t* A_int = convert_float_to_integer(A, n);
465-
u_int32_t* B_temp = convert_float_to_integer(B, n);
466-
u_int32_t* B_int = transpose_matrix_integer(B_temp, n);
467464

465+
u_int64_t* A_int = convert_float_to_integer(A, n);
466+
u_int64_t* B_temp = convert_float_to_integer(B, n);
467+
u_int64_t* B_int = transpose_matrix_integer(B_temp, n);
468468

469469
double* C = zero_matrix_1D(n);
470-
u_int32_t* D = zero_matrix_1D_integer(n);
471-
// u_int32_t* D = zero_matrix_1D_integer(n);
470+
u_int64_t* D = zero_matrix_1D_integer(n);
472471

473472
// PRODUCT STARTS **********************************
474473
mp_naive(A, B, C, n, p);

src/matrix.c

Lines changed: 23 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@ double* zero_matrix_1D(int n){
3838
return mat;
3939
}
4040

41-
u_int32_t* zero_matrix_1D_integer(int n){
42-
u_int32_t* mat = (u_int32_t*) malloc(sizeof(u_int32_t) * n*n);
41+
u_int64_t* zero_matrix_1D_integer(int n){
42+
u_int64_t* mat = (u_int64_t*) malloc(sizeof(u_int64_t) * n*n);
4343
if (mat == NULL){
4444
printf("Malloc error !\n");
4545
return NULL;
@@ -108,6 +108,7 @@ double** convert_1D_to_2D(double* A, int n){
108108
return mat;
109109
}
110110

111+
111112
double* convert_2D_to_1D(double** A, int n){
112113
/* Converts a 2 dimensional square matrix to
113114
a 1 dimensional matrix. */
@@ -123,11 +124,10 @@ double* convert_2D_to_1D(double** A, int n){
123124
}
124125

125126

126-
127-
u_int32_t* convert_float_to_integer(double* A, int n){
127+
u_int64_t* convert_float_to_integer(double* A, int n){
128128
/* Converts the coeffients of a double matrix to u_int32_t matrix.
129129
*/
130-
u_int32_t* mat = zero_matrix_1D_integer(n);
130+
u_int64_t* mat = zero_matrix_1D_integer(n);
131131
for (int i=0; i<n*n; i++){
132132
mat[i] = (int) A[i];
133133
}
@@ -158,7 +158,7 @@ void delete_matrix_1D(double** mat, int n){
158158
*mat = NULL;
159159
}
160160

161-
void delete_matrix_1D_integer(u_int32_t** mat, int n){
161+
void delete_matrix_1D_integer(u_int64_t** mat, int n){
162162
/* Delete the 1D matrix and set it to NULL*/
163163
if (*mat == NULL){
164164
printf("Matrix is NULL cannot delete !\n");
@@ -169,24 +169,9 @@ void delete_matrix_1D_integer(u_int32_t** mat, int n){
169169
}
170170

171171

172-
173-
double** transpose_matrix(double** mat, int n){
174-
/* Tranpose a matrix */
175-
double** mat_t = zero_matrix_2D(n);
176-
if (mat_t == NULL){
177-
printf("Cannot transpose the matrix !\n");
178-
}
179-
for (int i = 0; i < n; i++){
180-
for (int j = 0; j < n; j++){
181-
mat_t[i][j] = mat[j][i];
182-
}
183-
}
184-
return mat_t;
185-
}
186-
187-
u_int32_t* transpose_matrix_integer(u_int32_t* mat, int n){
172+
u_int64_t* transpose_matrix_integer(u_int64_t* mat, int n){
188173
/* Transpose a matrix */
189-
u_int32_t* mat_t = zero_matrix_1D_integer(n);
174+
u_int64_t* mat_t = zero_matrix_1D_integer(n);
190175
if (mat_t == NULL){
191176
printf("Cannot transpose the matrix !\n");
192177
}
@@ -198,6 +183,7 @@ u_int32_t* transpose_matrix_integer(u_int32_t* mat, int n){
198183
return mat_t;
199184
}
200185

186+
201187
void print_matrix_2D(double** mat, int n){
202188
/* Print the matrix. Here is an example:
203189
[ 1 2 3 ]
@@ -241,7 +227,8 @@ void print_matrix_1D(double* mat, int n){
241227
return ;
242228
}
243229

244-
void print_matrix_1D_integer(u_int32_t* mat, int n){
230+
231+
void print_matrix_1D_integer(u_int64_t* mat, int n){
245232
/* Print the matrix. Here is an example:
246233
[ 1 2 3 ]
247234
[ 4 5 6 ]
@@ -255,7 +242,7 @@ void print_matrix_1D_integer(u_int32_t* mat, int n){
255242
if (i % n == 0){
256243
printf("[ ");
257244
}
258-
printf("%d ", mat[i]);
245+
printf("%ld ", mat[i]);
259246
if ((i+1) % n == 0){
260247
printf("]\n");
261248
}
@@ -264,9 +251,6 @@ void print_matrix_1D_integer(u_int32_t* mat, int n){
264251
}
265252

266253

267-
268-
269-
270254
void write_matrix(double** mat, int n, char* filename){
271255
/* Write the matrix into a file. Here is an example:
272256
2
@@ -292,6 +276,7 @@ void write_matrix(double** mat, int n, char* filename){
292276
return;
293277
}
294278

279+
295280
void write_matrix_1D(double* mat, int n, char* filename){
296281
/* Write the matrix into a file. Here is an example:
297282
2
@@ -304,7 +289,8 @@ void write_matrix_1D(double* mat, int n, char* filename){
304289
return;
305290
}
306291

307-
void write_matrix_1D_integer(u_int32_t* mat, int n, char* filename){
292+
293+
void write_matrix_1D_integer(u_int64_t* mat, int n, char* filename){
308294
/* Write the matrix into a file. Here is an example:
309295
2
310296
[ 1 2 ]
@@ -320,7 +306,7 @@ void write_matrix_1D_integer(u_int32_t* mat, int n, char* filename){
320306
for (int i = 0; i < n; i++){
321307
fprintf(f, "[ ");
322308
for(int j = 0; j < n; j++){
323-
fprintf(f, "%d ", mat[i*n + j]);
309+
fprintf(f, "%ld ", mat[i*n + j]);
324310
}
325311
fprintf(f, "]\n");
326312
}
@@ -329,6 +315,7 @@ void write_matrix_1D_integer(u_int32_t* mat, int n, char* filename){
329315
return;
330316
}
331317

318+
332319
double** read_matrix(char* filename, int* n){
333320
/* Read the file, update n, then return the matrix. */
334321
char buffer[1024];
@@ -371,6 +358,7 @@ double** read_matrix(char* filename, int* n){
371358
return mat;
372359
}
373360

361+
374362
double* read_matrix_1D(char* filename, int* n){
375363
double** mat = read_matrix(filename, n);
376364
if (mat == NULL)
@@ -433,26 +421,26 @@ int equals_matrix_1D_1D(double* A, double* B, int n){
433421
return 1;
434422
}
435423

436-
int equals_matrix_float_integer(double* A, u_int32_t* B, int n){
424+
int equals_matrix_float_integer(double* A, u_int64_t* B, int n){
437425
/* Check if A equals B. The return values are:
438426
0: different
439427
1: equals
440428
*/
441429
for (int i=0; i<n*n; i++){
442430
if (A[i] != B[i]){
443431
printf("A[%d] = %f \n", i, A[i]);
444-
printf("B[%d] = %d \n", i, B[i]);
432+
printf("B[%d] = %ld \n", i, B[i]);
445433
return 0;
446434
}
447435
}
448436
return 1;
449437
}
450438

451-
int equals_matrix_integer_integer(u_int32_t* A, u_int32_t* B, int n){
439+
int equals_matrix_integer_integer(u_int64_t* A, u_int64_t* B, int n){
452440
for (int i=0; i<n*n; i++){
453441
if (A[i] != B[i]){
454-
printf("A[%d] = %d \n", i, A[i]);
455-
printf("B[%d] = %d \n", i, B[i]);
442+
printf("A[%d] = %ld \n", i, A[i]);
443+
printf("B[%d] = %ld \n", i, B[i]);
456444
return 0;
457445
}
458446
}

src/matrix.h

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,37 +9,37 @@
99
// Basic operations
1010
double** zero_matrix_2D(int n);
1111
double* zero_matrix_1D(int n);
12-
u_int32_t* zero_matrix_1D_integer(int n);
12+
u_int64_t* zero_matrix_1D_integer(int n);
1313

1414
double** random_matrix_2D(int n, double p);
1515
double* random_matrix_1D(int n, double p);
1616

1717
double* convert_2D_to_1D(double** mat, int n);
1818
double** convert_1D_to_2D(double* mat, int n);
19-
u_int32_t* convert_float_to_integer(double* mat, int n);
19+
u_int64_t* convert_float_to_integer(double* mat, int n);
2020

2121
void delete_matrix_2D(double*** mat, int n);
2222
void delete_matrix_1D(double** mat, int n);
23-
void delete_matrix_1D_integer(u_int32_t** mat, int n);
23+
void delete_matrix_1D_integer(u_int64_t** mat, int n);
2424

25-
double** transpose_matrix(double** mat, int n);
26-
u_int32_t* transpose_matrix_integer(u_int32_t* mat, int n);
25+
u_int64_t* transpose_matrix_integer(u_int64_t* mat, int n);
2726

2827
void print_matrix_2D(double** mat, int n);
2928
void print_matrix_1D(double* mat, int n);
30-
void print_matrix_1D_integer(u_int32_t* mat, int n);
29+
void print_matrix_1D_integer(u_int64_t* mat, int n);
3130

3231
void write_matrix(double** mat, int n, char* filename);
3332
void write_matrix_1D(double* mat, int n, char* filename);
34-
void write_matrix_1D_integer(u_int32_t* mat, int n, char* filename);
33+
void write_matrix_1D_integer(u_int64_t* mat, int n, char* filename);
34+
3535
double** read_matrix(char* filename, int* n);
3636
double* read_matrix_1D(char* filename, int* n);
3737

3838
int equals_matrix_2D_2D(double** A, double** B, int n);
3939
int equals_matrix_2D_1D(double** A, double* B, int n);
4040
int equals_matrix_1D_1D(double* A, double* B, int n);
41-
int equals_matrix_float_integer(double* A, u_int32_t* B, int n);
42-
int equals_matrix_integer_integer(u_int32_t* A, u_int32_t* B, int n);
41+
int equals_matrix_float_integer(double* A, u_int64_t* B, int n);
42+
int equals_matrix_integer_integer(u_int64_t* A, u_int64_t* B, int n);
4343

4444
int equals_matrix_file(char* filename1, char* filename2);
4545

src/matrix_mul.c

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -425,17 +425,18 @@ void mp_kji(double* A, double* B, double* C, int n){
425425
}
426426
}
427427

428-
void mp_integer(u_int32_t* A, u_int32_t* B, u_int32_t* C, int n, u_int32_t p, u_int32_t u){
428+
429+
void mp_integer(u_int64_t* A, u_int64_t* B, u_int64_t* C, int n, u_int32_t p, u_int32_t u){
429430
/* Matrix product using: Decomp Barrett and Trans */
430431

431432
for (int i = 0; i < n; i++){
432433
for (int j = 0; j < n; j++){
433434
u_int64_t h = 0;
434435
u_int64_t l = 0;
435436
for (int k = 0; k < n; k++){
436-
u_int64_t temp = (u_int64_t) A[i*n + k] * B[j*n + k];
437-
h += temp >> 32;
438-
l += temp - ((temp >> 32) << 32);
437+
u_int64_t temp = A[i*n + k] * B[j*n + k];
438+
h = h + (temp >> 32);
439+
l += temp - (((temp) >> 32) << 32);
439440
}
440441

441442
u_int64_t t = 1;

src/matrix_mul.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,6 @@ void mp_ikj(double* A, double* B, double* C, int n);
4343
void mp_jik(double* A, double* B, double* C, int n);
4444
void mp_kji(double* A, double* B, double* C, int n);
4545

46-
void mp_integer(u_int32_t* A, u_int32_t* B, u_int32_t* C, int n, u_int32_t p, u_int32_t u);
47-
void mp_integer_naive(u_int32_t* A, u_int32_t* B, u_int32_t* C, int n, u_int32_t p, u_int32_t u);
46+
void mp_integer(u_int64_t* A, u_int64_t* B, u_int64_t* C, int n, u_int32_t p, u_int32_t u);
47+
4848
#endif

0 commit comments

Comments
 (0)