32
32
33
33
#include " matx.h"
34
34
35
+ // Note that sparse tensor support in MatX is still experimental.
36
+
35
37
using namespace matx ;
36
38
37
39
int main ([[maybe_unused]] int argc, [[maybe_unused]] char **argv)
@@ -42,7 +44,7 @@ int main([[maybe_unused]] int argc, [[maybe_unused]] char **argv)
42
44
cudaExecutor exec{stream};
43
45
44
46
//
45
- // Print some formats that are used for the versatile sparse tensor
47
+ // Print some formats that are used for the universal sparse tensor
46
48
// type. Note that common formats like COO and CSR have good library
47
49
// support in e.g. cuSPARSE, but MatX provides a much more general
48
50
// way to define the sparse tensor storage through a DSL (see doc).
@@ -68,25 +70,6 @@ int main([[maybe_unused]] int argc, [[maybe_unused]] char **argv)
68
70
// | 0, 0, 0, 0, 0, 0, 0, 0 |
69
71
// | 0, 0, 3, 4, 0, 5, 0, 0 |
70
72
//
71
-
72
- constexpr index_t m = 4 ;
73
- constexpr index_t n = 8 ;
74
- constexpr index_t nse = 5 ;
75
-
76
- tensor_t <float , 1 > values{{nse}};
77
- tensor_t <int , 1 > row_idx{{nse}};
78
- tensor_t <int , 1 > col_idx{{nse}};
79
-
80
- values.SetVals ({ 1 , 2 , 3 , 4 , 5 });
81
- row_idx.SetVals ({ 0 , 0 , 3 , 3 , 3 });
82
- col_idx.SetVals ({ 0 , 1 , 2 , 3 , 5 });
83
-
84
- // Note that sparse tensor support in MatX is still experimental.
85
- auto Acoo = experimental::make_tensor_coo (values, row_idx, col_idx, {m, n});
86
-
87
- //
88
- // This shows:
89
- //
90
73
// tensor_impl_2_f32: SparseTensor{float} Rank: 2, Sizes:[4, 8], Levels:[4, 8]
91
74
// nse = 5
92
75
// format = ( d0, d1 ) -> ( d0 : compressed(non-unique), d1 : singleton )
@@ -95,6 +78,13 @@ int main([[maybe_unused]] int argc, [[maybe_unused]] char **argv)
95
78
// values = ( 1.0000e+00 2.0000e+00 3.0000e+00 4.0000e+00 5.0000e+00 )
96
79
// space = CUDA managed memory
97
80
//
81
+ auto vals = make_tensor<float >({5 });
82
+ auto idxi = make_tensor<int >({5 });
83
+ auto idxj = make_tensor<int >({5 });
84
+ vals.SetVals ({1 , 2 , 3 , 4 , 5 });
85
+ idxi.SetVals ({0 , 0 , 3 , 3 , 3 });
86
+ idxj.SetVals ({0 , 1 , 2 , 3 , 5 });
87
+ auto Acoo = experimental::make_tensor_coo (vals, idxi, idxj, {4 , 8 });
98
88
print (Acoo);
99
89
100
90
//
@@ -107,9 +97,9 @@ int main([[maybe_unused]] int argc, [[maybe_unused]] char **argv)
107
97
// use sparse operations that are tailored for the sparse data
108
98
// structure (such as scanning by row for CSR).
109
99
//
110
- tensor_t <float , 2 > A{{m, n}} ;
111
- for (index_t i = 0 ; i < m ; i++) {
112
- for (index_t j = 0 ; j < n ; j++) {
100
+ auto A = make_tensor <float >({ 4 , 8 }) ;
101
+ for (index_t i = 0 ; i < 4 ; i++) {
102
+ for (index_t j = 0 ; j < 8 ; j++) {
113
103
A (i, j) = Acoo (i, j);
114
104
}
115
105
}
@@ -119,24 +109,36 @@ int main([[maybe_unused]] int argc, [[maybe_unused]] char **argv)
119
109
// SpMM is implemented on COO through cuSPARSE. This is the
120
110
// correct way of performing an efficient sparse operation.
121
111
//
122
- tensor_t <float , 2 > B{{8 , 4 }};
123
- tensor_t <float , 2 > C{{4 , 4 }};
124
- B.SetVals ({ { 0 , 1 , 2 , 3 },
125
- { 4 , 5 , 6 , 7 },
126
- { 8 , 9 , 10 , 11 },
127
- { 12 , 13 , 14 , 15 },
128
- { 16 , 17 , 18 , 19 },
129
- { 20 , 21 , 22 , 23 },
130
- { 24 , 25 , 26 , 27 },
131
- { 28 , 29 , 30 , 31 } });
112
+ auto B = make_tensor<float , 2 >({8 , 4 });
113
+ auto C = make_tensor<float >({4 , 4 });
114
+ B.SetVals ({
115
+ { 0 , 1 , 2 , 3 }, { 4 , 5 , 6 , 7 }, { 8 , 9 , 10 , 11 }, {12 , 13 , 14 , 15 },
116
+ {16 , 17 , 18 , 19 }, {20 , 21 , 22 , 23 }, {24 , 25 , 26 , 27 }, {28 , 29 , 30 , 31 } });
132
117
(C = matmul (Acoo, B)).run (exec);
133
118
print (C);
134
119
135
120
//
136
- // Verify by computing the equivelent dense GEMM.
121
+ // Creates a CSR matrix which is used to solve the following
122
+ // system of equations AX=Y, where X is the unknown.
137
123
//
138
- (C = matmul (A, B)).run (exec);
139
- print (C);
124
+ // | 1 2 0 0 | | 1 5 | | 5 17 |
125
+ // | 0 3 0 0 | x | 2 6 | = | 6 18 |
126
+ // | 0 0 4 0 | | 3 7 | | 12 28 |
127
+ // | 0 0 0 5 | | 4 8 | | 20 40 |
128
+ //
129
+ auto coeffs = make_tensor<float >({5 });
130
+ auto rowptr = make_tensor<int >({5 });
131
+ auto colidx = make_tensor<int >({5 });
132
+ coeffs.SetVals ({1 , 2 , 3 , 4 , 5 });
133
+ rowptr.SetVals ({0 , 2 , 3 , 4 , 5 });
134
+ colidx.SetVals ({0 , 1 , 1 , 2 , 3 });
135
+ auto Acsr = experimental::make_tensor_csr (coeffs, rowptr, colidx, {4 , 4 });
136
+ print (Acsr);
137
+ auto X = make_tensor<float >({4 , 2 });
138
+ auto Y = make_tensor<float >({4 , 2 });
139
+ Y.SetVals ({ {5 , 17 }, {6 , 18 }, {12 , 28 }, {20 , 40 } });
140
+ (X = solve (Acsr, Y)).run (exec);
141
+ print (X);
140
142
141
143
MATX_EXIT_HANDLER ();
142
144
}
0 commit comments