Line data Source code
1 : //------------------------------------------------------------------------------
2 : // LAGraph/experimental/test/test_mxm
3 : //------------------------------------------------------------------------------
4 :
5 : // LAGraph, (c) 2019-2025 by The LAGraph Contributors, All Rights Reserved.
6 : // SPDX-License-Identifier: BSD-2-Clause
7 : //
8 : // For additional details (including references to third party source code and
9 : // other files) see the LICENSE file or contact permission@sei.cmu.edu. See
10 : // Contributors.txt for a full list of contributors. Created, in part, with
11 : // funding and support from the U.S. Government (see Acknowledgments.txt file).
12 : // DM22-0790
13 :
14 : // Contributed by Timothy A. Davis, Texas A&M University
15 :
16 : //------------------------------------------------------------------------------
17 :
18 : #include <stdio.h>
19 : #include <acutest.h>
20 : #include <LAGraphX.h>
21 : #include <LAGraph_test.h>
22 : #include <LG_Xtest.h>
23 :
24 : //------------------------------------------------------------------------------
25 : // test cases
26 : //------------------------------------------------------------------------------
27 :
28 : char msg [LAGRAPH_MSG_LEN] ;
29 : GrB_Matrix A = NULL, B = NULL, C2 = NULL, Cin = NULL, C = NULL ;
30 :
31 : //------------------------------------------------------------------------------
32 : // test_mxm: test GrB_mxm with different types of inputs
33 : //------------------------------------------------------------------------------
34 :
35 1 : void test_mxm (void)
36 : {
37 :
38 : GrB_Info info ;
39 1 : OK (LAGraph_Init (msg)) ;
40 1 : uint64_t n = 256 ;
41 1 : uint64_t seed = 0 ;
42 :
43 3 : for (int t = 32 ; t <= 64 ; t += 32)
44 : {
45 : // set up the type and semiring
46 2 : printf ("\ntest_mxm ======================= type : FP%d\n", t) ;
47 2 : GrB_Type type = (t == 32) ? GrB_FP32 : GrB_FP64 ;
48 2 : GrB_Semiring semiring = (t == 32) ?
49 2 : GrB_PLUS_TIMES_SEMIRING_FP32 : GrB_PLUS_TIMES_SEMIRING_FP64 ;
50 2 : GrB_BinaryOp accum = (t == 32) ?
51 2 : GrB_PLUS_FP32 : GrB_PLUS_FP64 ;
52 2 : GrB_BinaryOp minus = (t == 32) ?
53 2 : GrB_MINUS_FP32 : GrB_MINUS_FP64 ;
54 2 : GrB_UnaryOp abs_op = (t == 32) ?
55 2 : GrB_ABS_FP32 : GrB_ABS_FP64 ;
56 2 : GrB_Monoid max_monoid = (t == 32) ?
57 2 : GrB_MAX_MONOID_FP32 : GrB_MAX_MONOID_FP64 ;
58 2 : double tol = (t == 32) ? 1e-4 : 1e-11 ;
59 :
60 : // create some random test matrices
61 2 : OK (LAGraph_Random_Matrix (&A, type, n, n, INFINITY, seed, msg)) ;
62 2 : seed += n*n ;
63 2 : OK (LAGraph_Random_Matrix (&B, type, n, n, INFINITY, seed, msg)) ;
64 2 : seed += n*n ;
65 2 : OK (LAGraph_Random_Matrix (&Cin, type, n, n, INFINITY, seed, msg)) ;
66 2 : seed += n*n ;
67 :
68 : // C = Cin + A*B
69 2 : OK (GrB_Matrix_dup (&C, Cin)) ;
70 2 : OK (GrB_mxm (C, NULL, accum, semiring, A, B, NULL)) ;
71 2 : double maxerr = 0 ;
72 :
73 : // test with different sparsity formats and with JIT on/off
74 10 : for (int A_sparsity = 1 ; A_sparsity <= 8 ; A_sparsity *= 2)
75 : {
76 8 : LG_SET_FORMAT_HINT (A, A_sparsity) ;
77 40 : for (int B_sparsity = 1 ; B_sparsity <= 8 ; B_sparsity *= 2)
78 : {
79 32 : LG_SET_FORMAT_HINT (B, B_sparsity) ;
80 160 : for (int C_sparsity = 1 ; C_sparsity <= 8 ; C_sparsity *= 2)
81 : {
82 128 : LG_SET_FORMAT_HINT (Cin, C_sparsity) ;
83 384 : for (int jit = 0 ; jit <= 1 ; jit++)
84 : {
85 256 : OK (LG_SET_JIT (jit ? GxB_JIT_ON : GxB_JIT_OFF)) ;
86 :
87 : // C2 = Cin + A*B
88 256 : OK (GrB_Matrix_dup (&C2, Cin)) ;
89 256 : OK (GrB_mxm (C2, NULL, accum, semiring, A, B, NULL)) ;
90 :
91 : // C2 = abs (C - C2)
92 256 : OK (GrB_eWiseAdd (C2, NULL, NULL, minus, C2, C, NULL)) ;
93 256 : OK (GrB_apply (C2, NULL, NULL, abs_op, C2, NULL)) ;
94 :
95 : // err = max (C2)
96 256 : double err = 0 ;
97 256 : OK (GrB_reduce (&err, NULL, max_monoid, C2, NULL)) ;
98 256 : TEST_CHECK (err < tol) ;
99 256 : maxerr = LAGRAPH_MAX (maxerr, err) ;
100 256 : GrB_free (&C2) ;
101 : }
102 : }
103 : }
104 : }
105 :
106 2 : printf ("max err: %g\n", maxerr) ;
107 2 : GrB_free (&A) ;
108 2 : GrB_free (&B) ;
109 2 : GrB_free (&C) ;
110 2 : GrB_free (&Cin) ;
111 : }
112 :
113 1 : OK (LAGraph_Finalize (msg)) ;
114 1 : }
115 :
116 : //-----------------------------------------------------------------------------
117 : // TEST_LIST: the list of tasks for this entire test
118 : //-----------------------------------------------------------------------------
119 :
120 : TEST_LIST =
121 : {
122 : { "mxm", test_mxm },
123 : { NULL, NULL }
124 : } ;
125 :
|