@@ -94,6 +94,22 @@ typedef enum CFL_Matrix_block Matrix_block;
9494
9595GrB_Info matrix_print_lazy (Matrix * A , int8_t optimizations );
9696
97+ // Frees the stale copy after a write into one format only
98+ static GrB_Info matrix_drop_other_format (Matrix * matrix ) {
99+ if (!matrix -> is_both ) {
100+ return GrB_SUCCESS ;
101+ }
102+
103+ if (matrix -> base == matrix -> base_row ) {
104+ TRY (GrB_Matrix_free (& matrix -> base_col ));
105+ } else {
106+ TRY (GrB_Matrix_free (& matrix -> base_row ));
107+ }
108+ matrix -> is_both = false;
109+
110+ return GrB_SUCCESS ;
111+ }
112+
97113GrB_Info matrix_to_format (Matrix * matrix , int32_t format , bool is_both ) {
98114 TRY (CFL_matrix_update (matrix ));
99115
@@ -360,7 +376,7 @@ GrB_Info block_matrix_hyper_rotate_i(Matrix *matrix, enum CFL_Matrix_block forma
360376 TRY (GxB_Matrix_build_Scalar (new , nrows , ncols , scalar_true , matrix -> nvals ));
361377 matrix -> base_col = new ;
362378 }
363- matrix -> format = GrB_ROWMAJOR ;
379+ matrix -> format = matrix -> base_row != NULL ? GrB_ROWMAJOR : GrB_COLMAJOR ;
364380 }
365381 TRY (CFL_matrix_update (matrix ));
366382 // TRY(CFL_matrix_free(matrix_p));
@@ -507,6 +523,13 @@ GrB_Info CFL_matrix_to_base(Matrix **matrix_p, Matrix *input, int8_t optimizatio
507523
508524 Matrix * matrix ;
509525 TRY (CFL_matrix_create_lazy (& matrix , input -> nrows , input -> ncols ));
526+ GrB_Info info = LAGraph_Realloc ((void * * )& matrix -> base_matrices ,
527+ input -> base_matrices_count ,
528+ matrix -> base_matrices_count , sizeof (Matrix * ), NULL );
529+ if (info < GrB_SUCCESS ) {
530+ TRY (CFL_matrix_free (& matrix ));
531+ return info ;
532+ }
510533 TRY (CFL_matrix_free (& matrix -> base_matrices [0 ]));
511534 matrix -> base_matrices_count = 0 ;
512535
@@ -534,9 +557,50 @@ GrB_Info CFL_matrix_to_base(Matrix **matrix_p, Matrix *input, int8_t optimizatio
534557 return GrB_SUCCESS ;
535558}
536559
560+ GrB_Info CFL_matrix_extract_base (GrB_Matrix * base , Matrix * matrix , int8_t optimizations ) {
561+ if (matrix -> is_lazy ) {
562+ // GraphBLAS never adds sparse matrices in-place: each addition builds a new
563+ // matrix, so its cost depends on the size of both operands. Smaller base
564+ // matrices are summed first (from the smallest one), and the largest base
565+ // matrix is added only once at the end
566+ TRY (matrix_sort_lazy (matrix , false));
567+
568+ size_t last = matrix -> base_matrices_count - 1 ;
569+ Matrix * largest = matrix -> base_matrices [last ];
570+ if (last > 0 ) {
571+ Matrix * acc = matrix -> base_matrices [0 ];
572+ for (size_t i = 1 ; i < last ; i ++ ) {
573+ TRY (CFL_wise (acc , acc , matrix -> base_matrices [i ], false, optimizations ));
574+ }
575+
576+ TRY (CFL_wise (largest , largest , acc , false, optimizations ));
577+ }
578+
579+ TRY (CFL_matrix_extract_base (base , largest , optimizations ));
580+ return GrB_SUCCESS ;
581+ }
582+
583+ // Matrix contains both formats, so free the one that is not extracted
584+ if (matrix -> is_both ) {
585+ GrB_Matrix other =
586+ matrix -> base == matrix -> base_row ? matrix -> base_col : matrix -> base_row ;
587+ TRY (GrB_Matrix_free (& other ));
588+ }
589+
590+ * base = matrix -> base ;
591+ matrix -> base = NULL ;
592+ matrix -> base_row = NULL ;
593+ matrix -> base_col = NULL ;
594+ matrix -> is_both = false;
595+ matrix -> nvals = 0 ;
596+
597+ return GrB_SUCCESS ;
598+ }
599+
537600GrB_Info matrix_combine_lazy (Matrix * A , size_t threshold , int8_t optimizations ) {
538601 Matrix * * new_matrices ;
539- TRY (LAGraph_Calloc ((void * * )& new_matrices , 50 , sizeof (Matrix * ), NULL ));
602+ TRY (LAGraph_Calloc ((void * * )& new_matrices , A -> base_matrices_count ,
603+ sizeof (Matrix * ), NULL ));
540604
541605 size_t new_size = 0 ;
542606
@@ -654,8 +718,7 @@ GrB_Info CFL_matrix_from_base_lazy(Matrix **matrix_p, GrB_Matrix base) {
654718 matrix -> is_lazy = true;
655719 TRY (LAGraph_Calloc (
656720 (void * * )& matrix -> base_matrices , 40 , sizeof (CFL_Matrix * ),
657- NULL )); // this is enough for this centry i guess, because 40th matrices must
658- // have 10^40 nvals for being putten in base_matrices, this is 2^132
721+ NULL )); // Initial storage; addition and copying resize it as needed.
659722 matrix -> base_matrices [0 ] = result ;
660723 matrix -> base_matrices_count = 1 ;
661724 matrix -> base = NULL ;
@@ -667,9 +730,12 @@ GrB_Info CFL_matrix_from_base_lazy(Matrix **matrix_p, GrB_Matrix base) {
667730GrB_Info CFL_matrix_create (Matrix * * matrix , GrB_Index nrows , GrB_Index ncols ) {
668731 GrB_Matrix _result ;
669732 TRY (GrB_Matrix_new (& _result , GrB_BOOL , nrows , ncols ));
670- TRY (CFL_matrix_from_base (matrix , _result ));
733+ GrB_Info info = CFL_matrix_from_base (matrix , _result );
734+ if (info < GrB_SUCCESS ) {
735+ GrB_Matrix_free (& _result );
736+ }
671737
672- return GrB_SUCCESS ;
738+ return info ;
673739}
674740
675741GrB_Info CFL_matrix_create_lazy (Matrix * * matrix , GrB_Index nrows , GrB_Index ncols ) {
@@ -716,6 +782,7 @@ GrB_Info matrix_mxm(Matrix *output, Matrix *first, Matrix *second, bool accum,
716782 // CFL_matrix_update(output);
717783 TRY (GrB_mxm (output -> base , GrB_NULL , accum ? GxB_ANY_BOOL : GrB_NULL ,
718784 GxB_ANY_PAIR_BOOL , left -> base , right -> base , GrB_NULL ));
785+ TRY (matrix_drop_other_format (output ));
719786 // IS_ISO(output->base, "MXM output");
720787 TRY (CFL_matrix_update (output ));
721788
@@ -848,8 +915,24 @@ GrB_Info matrix_mxm_block(Matrix *output, Matrix *first, Matrix *second, bool ac
848915 }
849916
850917 if (first -> block_type == CELL && second -> block_type == CELL ) {
851- TRY (matrix_mxm_lazy (output , first , second , accum , swap , optimizations ));
852- return GrB_SUCCESS ;
918+ if (output -> block_type == CELL ) {
919+ TRY (matrix_mxm_lazy (output , first , second , accum , swap , optimizations ));
920+ return GrB_SUCCESS ;
921+ }
922+
923+ // N_i -> A B: compute the square product and add it to every output block.
924+ Matrix * temp ;
925+ TRY (CFL_matrix_create (& temp , swap ? second -> nrows : first -> nrows ,
926+ swap ? first -> ncols : second -> ncols ));
927+ GrB_Info info = matrix_mxm_lazy (temp , first , second , false, swap , optimizations );
928+ if (info >= GrB_SUCCESS && !accum ) {
929+ info = matrix_clear_empty (output , optimizations );
930+ }
931+ if (info >= GrB_SUCCESS ) {
932+ info = matrix_wise_block (output , output , temp , false, optimizations );
933+ }
934+ TRY (CFL_matrix_free (& temp ));
935+ return info ;
853936 }
854937
855938 if (first -> block_type == CELL ) {
@@ -860,6 +943,9 @@ GrB_Info matrix_mxm_block(Matrix *output, Matrix *first, Matrix *second, bool ac
860943 TRY (CFL_matrix_create (& temp , swap ? second -> nrows : first -> nrows ,
861944 swap ? first -> ncols : second -> ncols ));
862945 TRY (matrix_mxm_lazy (temp , first , second , accum , swap , optimizations ));
946+ if (!accum ) {
947+ TRY (matrix_clear_empty (output , optimizations ));
948+ }
863949 TRY (matrix_wise_block (output , output , temp , false, optimizations ));
864950 TRY (CFL_matrix_free (& temp ));
865951
@@ -874,6 +960,9 @@ GrB_Info matrix_mxm_block(Matrix *output, Matrix *first, Matrix *second, bool ac
874960 TRY (CFL_matrix_create (& temp , swap ? second -> nrows : first -> nrows ,
875961 swap ? first -> ncols : first -> ncols ));
876962 TRY (matrix_mxm_lazy (temp , first , second , accum , swap , optimizations ));
963+ if (!accum ) {
964+ TRY (matrix_clear_empty (output , optimizations ));
965+ }
877966 TRY (matrix_wise_block (output , output , temp , false, optimizations ));
878967 TRY (CFL_matrix_free (& temp ));
879968
@@ -893,6 +982,9 @@ GrB_Info matrix_mxm_block(Matrix *output, Matrix *first, Matrix *second, bool ac
893982 Matrix * temp ;
894983 TRY (CFL_matrix_create (& temp , first -> nrows , first -> ncols ));
895984 TRY (matrix_mxm_lazy (temp , first , diag , false, swap , optimizations ));
985+ if (!accum ) {
986+ TRY (matrix_clear_empty (output , optimizations ));
987+ }
896988 TRY (matrix_wise_block (output , output , temp , false, optimizations ));
897989
898990 TRY (CFL_matrix_free (& temp ));
@@ -1042,9 +1134,18 @@ GrB_Info matrix_wise_lazy(Matrix *output, Matrix *first, Matrix *second, bool ac
10421134 TRY (CFL_matrix_create (& other , output -> nrows , output -> ncols ));
10431135 TRY (matrix_dup_empty (other , second , optimizations ));
10441136
1045- size_t other_nvals = other -> nvals >= 10 ? other -> nvals : 10 ;
1137+ // Reserve before merging so allocation failure leaves the components intact.
1138+ GrB_Info info = LAGraph_Realloc ((void * * )& first -> base_matrices ,
1139+ first -> base_matrices_count + 1 ,
1140+ first -> base_matrices_count , sizeof (Matrix * ), NULL );
1141+ if (info < GrB_SUCCESS ) {
1142+ TRY (CFL_matrix_free (& other ));
1143+ return info ;
1144+ }
10461145
10471146 while (true) {
1147+ // A merge changes other's size and can make another component comparable.
1148+ size_t other_nvals = other -> nvals >= 10 ? other -> nvals : 10 ;
10481149 bool found = false;
10491150
10501151 for (size_t i = 0 ; i < first -> base_matrices_count ; i ++ ) {
@@ -1147,6 +1248,7 @@ GrB_Info matrix_wise_block(Matrix *output, Matrix *first, Matrix *second, bool a
11471248GrB_Info matrix_rsub (Matrix * output , Matrix * mask ) {
11481249 TRY (GrB_eWiseAdd (output -> base , mask -> base , GrB_NULL , GxB_ANY_BOOL , output -> base ,
11491250 output -> base , GrB_DESC_RSC ));
1251+ TRY (matrix_drop_other_format (output ));
11501252
11511253 TRY (CFL_matrix_update (output ));
11521254
@@ -1335,4 +1437,4 @@ GrB_Info CFL_clear(Matrix *A, int8_t optimizations) {
13351437
13361438 TRY (CFL_matrix_update (A ));
13371439 return GrB_SUCCESS ;
1338- }
1440+ }
0 commit comments