Skip to content

Commit cd90d5e

Browse files
committed
Fix: memory leaks in matrix_opt and in tests for opts
1 parent 29bfba9 commit cd90d5e

File tree

2 files changed

+76
-13
lines changed

2 files changed

+76
-13
lines changed

experimental/algorithm/LAGraph_CFL_optimized_matrix_opt.c

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,7 @@ void block_matrix_repeat_into_vector(Matrix *matrix, Matrix *input,
346346

347347
GxB_Matrix_concat(matrix->base, tiles, block_count, 1, GrB_NULL);
348348
CFL_matrix_update(matrix);
349+
free(tiles);
349350
}
350351

351352
// lazy optimization specific methods
@@ -384,6 +385,8 @@ Matrix CFL_matrix_to_base(Matrix *input, int8_t optimizations) {
384385
GrB_Matrix _matrix;
385386
GrB_Matrix_new(&_matrix, GrB_BOOL, input->nrows, input->ncols);
386387
Matrix matrix = CFL_matrix_from_base_lazy(_matrix);
388+
GrB_free(&_matrix);
389+
matrix.base = NULL;
387390

388391
for (size_t i = 0; i < input->base_matrices_count; i++) {
389392
Matrix base = CFL_matrix_create(input->nrows, input->ncols);
@@ -420,6 +423,7 @@ GrB_Info matrix_combine_lazy(Matrix *A, size_t threshold, int8_t optimizations)
420423
GrB_free(&A->base_matrices[i].base);
421424
}
422425

426+
free(A->base_matrices);
423427
A->base_matrices = new_matrices;
424428
A->base_matrices_count = new_size;
425429
CFL_matrix_update(A);
@@ -506,8 +510,24 @@ Matrix CFL_matrix_create(GrB_Index nrows, GrB_Index ncols) {
506510

507511
// TODO: free all base_matrices, free format matrices
508512
void CFL_matrix_free(Matrix *matrix) {
509-
free(matrix->base_matrices);
510-
GrB_free(&matrix->base);
513+
if (matrix->is_lazy) {
514+
for (size_t i = 0; i < matrix->base_matrices_count; i++) {
515+
CFL_matrix_free(&matrix->base_matrices[i]);
516+
}
517+
518+
free(matrix->base_matrices);
519+
matrix->base_matrices = NULL;
520+
return;
521+
}
522+
523+
if (matrix->is_both) {
524+
// matrix->base is base_row or base_col
525+
GrB_Matrix_free(&matrix->base_row);
526+
GrB_Matrix_free(&matrix->base_col);
527+
return;
528+
}
529+
530+
GrB_Matrix_free(&matrix->base);
511531
}
512532

513533
// mxm operations
@@ -613,13 +633,18 @@ GrB_Info matrix_mxm_lazy(Matrix *output, Matrix *first, Matrix *second, bool acc
613633
optimizations);
614634
GrB_free(&acc_matrices[i].base);
615635
}
636+
free(accs);
637+
free(acc_matrices);
616638

617639
if (accum) {
618-
return matrix_wise_empty(output, output, &acc_matrix, false, optimizations);
640+
GrB_Info result =
641+
matrix_wise_empty(output, output, &acc_matrix, false, optimizations);
642+
CFL_matrix_free(&acc_matrix);
643+
return result;
619644
}
620645

621646
GrB_Info result = matrix_dup_block(output, &acc_matrix, optimizations);
622-
GrB_free(&acc_matrix.base);
647+
CFL_matrix_free(&acc_matrix);
623648

624649
return result;
625650
}
@@ -675,7 +700,10 @@ GrB_Info matrix_mxm_block(Matrix *output, Matrix *first, Matrix *second, bool ac
675700

676701
Matrix temp = CFL_matrix_create(first->nrows, diag.ncols);
677702
matrix_mxm_lazy(&temp, first, &diag, false, swap, optimizations);
678-
return matrix_wise_block(output, output, &temp, false, optimizations);
703+
GrB_Info result = matrix_wise_block(output, output, &temp, false, optimizations);
704+
CFL_matrix_free(&temp);
705+
CFL_matrix_free(&diag);
706+
return result;
679707
}
680708

681709
// wise operations

experimental/test/test_CFL_optimized_matrix_opt.c

Lines changed: 43 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -423,14 +423,17 @@ static void test_CFL_format_clear_format(void) {
423423

424424
// NULL Matrix
425425
GrB_Matrix old_base = A.base;
426-
GrB_Matrix old_base_row = A.base;
426+
GrB_Matrix old_base_row = A.base_row;
427+
GrB_Matrix old_base_col = A.base_col;
427428
A.base = NULL;
428429
A.base_row = NULL;
430+
A.base_col = NULL;
429431
GrB_Info result = matrix_clear_format(&A, OPT_FORMAT);
430432
OK(!result);
431433

432434
A.base = old_base;
433435
A.base_row = old_base_row;
436+
A.base_col = old_base_col;
434437
CFL_matrix_free(&A);
435438

436439
teardown();
@@ -455,14 +458,17 @@ static void test_CFL_format_dup_format(void) {
455458

456459
// NULL Matrix
457460
GrB_Matrix old_base = A.base;
458-
GrB_Matrix old_base_row = A.base;
461+
GrB_Matrix old_base_row = A.base_row;
462+
GrB_Matrix old_base_col = A.base_col;
459463
A.base = NULL;
460464
A.base_row = NULL;
465+
A.base_col = NULL;
461466
GrB_Info result = matrix_dup_format(&A, &B, OPT_FORMAT);
462467
OK(!result);
463468

464469
A.base = old_base;
465470
A.base_row = old_base_row;
471+
A.base_col = old_base_col;
466472
CFL_matrix_free(&A);
467473
CFL_matrix_free(&B);
468474

@@ -471,6 +477,8 @@ static void test_CFL_format_dup_format(void) {
471477
B = CFL_matrix_create(5, 5);
472478
OK(matrix_to_format(&A, GrB_COLMAJOR, true));
473479
OK(matrix_dup_format(&A, &B, 0));
480+
CFL_matrix_free(&A);
481+
CFL_matrix_free(&B);
474482

475483
teardown();
476484
#endif
@@ -488,6 +496,10 @@ static void test_CFL_format_mxm_second_greather_then_k(void) {
488496
OK(CFL_mxm(&C, &A, &B, false, false, OPT_FORMAT));
489497
TEST_CHECK(C.nvals == 0);
490498

499+
free_matrix(&A);
500+
free_matrix(&B);
501+
free_matrix(&C);
502+
491503
teardown();
492504
#endif
493505
}
@@ -505,12 +517,15 @@ static void test_CFL_format_wise_when_both(void) {
505517

506518
// NULL Matrix
507519
GrB_Matrix old_base = A.base;
508-
GrB_Matrix old_base_row = A.base;
520+
GrB_Matrix old_base_row = A.base_row;
521+
GrB_Matrix old_base_col = A.base_col;
509522
A.base = NULL;
510523
A.base_row = NULL;
524+
A.base_col = NULL;
511525
GrB_Info result = CFL_wise(&A, &A, &B, false, OPT_FORMAT);
512526
A.base = old_base;
513527
A.base_row = old_base_row;
528+
A.base_col = old_base_col;
514529
OK(!result);
515530

516531
CFL_matrix_free(&A);
@@ -680,6 +695,9 @@ static void test_CFL_empty_rsub_both_empty(void) {
680695
OK(CFL_rsub(&A, &B, OPT_EMPTY));
681696
TEST_CHECK(A.nvals == 0);
682697

698+
free_matrix(&A);
699+
free_matrix(&B);
700+
683701
teardown();
684702
#endif
685703
}
@@ -700,6 +718,8 @@ static void test_CFL_lazy_create(void) {
700718
TEST_CHECK(A.is_lazy == true);
701719
TEST_CHECK(A.nvals == 0);
702720

721+
free_matrix(&A);
722+
703723
teardown();
704724
#endif
705725
}
@@ -737,6 +757,8 @@ static Matrix make_lazy_matrix(size_t base_matrices_count) {
737757
GrB_Matrix _result;
738758
GrB_Matrix_new(&_result, GrB_BOOL, n, n);
739759
Matrix result = CFL_matrix_from_base_lazy(_result);
760+
GrB_free(&_result);
761+
result.base = NULL;
740762

741763
result.is_lazy = true;
742764
result.base_matrices_count = base_matrices_count;
@@ -1010,6 +1032,9 @@ static void test_CFL_block_dup(void) {
10101032

10111033
OK(CFL_dup(&B, &A, OPT_BLOCK));
10121034
TEST_CHECK(B.nvals == A.nvals);
1035+
1036+
free_matrix(&A);
1037+
free_matrix(&B);
10131038
}
10141039

10151040
{
@@ -1018,6 +1043,9 @@ static void test_CFL_block_dup(void) {
10181043

10191044
OK(CFL_dup(&B, &A, OPT_BLOCK));
10201045
TEST_CHECK(B.nvals == A.nvals);
1046+
1047+
free_matrix(&A);
1048+
free_matrix(&B);
10211049
}
10221050

10231051
{
@@ -1026,6 +1054,9 @@ static void test_CFL_block_dup(void) {
10261054

10271055
OK(CFL_dup(&B, &A, OPT_BLOCK));
10281056
TEST_CHECK(B.nvals == A.nvals);
1057+
1058+
free_matrix(&A);
1059+
free_matrix(&B);
10291060
}
10301061

10311062
{
@@ -1034,6 +1065,9 @@ static void test_CFL_block_dup(void) {
10341065

10351066
OK(CFL_dup(&B, &A, OPT_BLOCK));
10361067
TEST_CHECK(B.nvals == A.nvals);
1068+
1069+
free_matrix(&A);
1070+
free_matrix(&B);
10371071
}
10381072

10391073
teardown();
@@ -1114,16 +1148,17 @@ static void test_CFL_block_hyper_rotate(void) {
11141148
GrB_Matrix _A;
11151149
GrB_Matrix_new(&_A, GrB_BOOL, 20 * 20, 20);
11161150
Matrix A = CFL_matrix_from_base_lazy(_A);
1151+
GrB_free(&_A);
11171152

11181153
GrB_Matrix _A0;
11191154
GrB_Matrix_new(&_A0, GrB_BOOL, 20 * 20, 20);
11201155
Matrix A0 = CFL_matrix_from_base(_A0);
11211156
GrB_Matrix _A1;
11221157
GrB_Matrix_new(&_A1, GrB_BOOL, 20 * 20, 20);
1123-
Matrix A1 = CFL_matrix_from_base(_A0);
1158+
Matrix A1 = CFL_matrix_from_base(_A1);
11241159
GrB_Matrix _A2;
11251160
GrB_Matrix_new(&_A2, GrB_BOOL, 20 * 20, 20);
1126-
Matrix A2 = CFL_matrix_from_base(_A0);
1161+
Matrix A2 = CFL_matrix_from_base(_A2);
11271162

11281163
A.base_matrices[0] = A0;
11291164
A.base_matrices[1] = A1;
@@ -1135,9 +1170,9 @@ static void test_CFL_block_hyper_rotate(void) {
11351170
TEST_CHECK(A.ncols == 20 * 20);
11361171

11371172
free_matrix(&A);
1138-
free_matrix(&A0);
1139-
free_matrix(&A1);
1140-
free_matrix(&A2);
1173+
// free_matrix(&A0);
1174+
// free_matrix(&A1);
1175+
// free_matrix(&A2);
11411176
}
11421177

11431178
// cell

0 commit comments

Comments
 (0)