|
161 | 161 | <p>
|
162 | 162 | <script type="math/tex; mode=display">y = Ax + z,</script>
|
163 | 163 | </p>
|
164 |
| -<p>where <script type="math/tex">A</script> is a sparse matrix and <script type="math/tex">x</script>, <script type="math/tex">y</script>, and <script type="math/tex">z</script> |
165 |
| -are dense vectors. The computation can also be expressed in <a href="../pycomputations/index.html#specifying-tensor-algebra-computations">index |
| 164 | +<p>where <script type="math/tex">A</script> is a sparse matrix and <script type="math/tex">x</script>, <script type="math/tex">y</script>, and <script type="math/tex">z</script> are dense vectors. |
| 165 | +The computation can also be expressed in <a href="../pycomputations/index.html#specifying-tensor-algebra-computations">index |
166 | 166 | notation</a> as </p>
|
167 | 167 | <p>
|
168 | 168 | <script type="math/tex; mode=display">y_i = A_{ij} \cdot x_j + z_i.</script>
|
169 | 169 | </p>
|
170 |
| -<p>You can use the TACO Python library to easily and efficiently compute SpMV, as |
| 170 | +<p>You can use the TACO C++ library to easily and efficiently compute SpMV, as |
171 | 171 | shown here:</p>
|
| 172 | +<pre class="highlight"><code class="language-c++">// On Linux and MacOS, you can compile and run this program like so: |
| 173 | +// g++ -std=c++11 -O3 -DNDEBUG -DTACO -I ../../include -L../../build/lib spmv.cpp -o spmv -ltaco |
| 174 | +// LD_LIBRARY_PATH=../../build/lib ./spmv |
| 175 | +#include <random> |
| 176 | +#include "taco.h" |
| 177 | +using namespace taco; |
| 178 | +int main(int argc, char* argv[]) { |
| 179 | + std::default_random_engine gen(0); |
| 180 | + std::uniform_real_distribution<double> unif(0.0, 1.0); |
| 181 | + // Predeclare the storage formats that the inputs and output will be stored as. |
| 182 | + // To define a format, you must specify whether each dimension is dense or sparse |
| 183 | + // and (optionally) the order in which dimensions should be stored. The formats |
| 184 | + // declared below correspond to compressed sparse row (csr) and dense vector (dv). |
| 185 | + Format csr({Dense,Sparse}); |
| 186 | + Format dv({Dense}); |
| 187 | + |
| 188 | + // Load a sparse matrix from file (stored in the Matrix Market format) and |
| 189 | + // store it as a compressed sparse row matrix. Matrices correspond to order-2 |
| 190 | + // tensors in taco. The matrix in this example can be downloaded from: |
| 191 | + // https://www.cise.ufl.edu/research/sparse/MM/Boeing/pwtk.tar.gz |
| 192 | + Tensor<double> A = read("pwtk.mtx", csr); |
| 193 | + |
| 194 | + // Generate a random dense vector and store it in the dense vector format. |
| 195 | + // Vectors correspond to order-1 tensors in taco. |
| 196 | + Tensor<double> x({A.getDimension(1)}, dv); |
| 197 | + for (int i = 0; i < x.getDimension(0); ++i) { |
| 198 | + x.insert({i}, unif(gen)); |
| 199 | + } |
| 200 | + x.pack(); |
| 201 | + |
| 202 | + // Generate another random dense vetor and store it in the dense vector format.. |
| 203 | + Tensor<double> z({A.getDimension(0)}, dv); |
| 204 | + for (int i = 0; i < z.getDimension(0); ++i) { |
| 205 | + z.insert({i}, unif(gen)); |
| 206 | + } |
| 207 | + z.pack(); |
| 208 | + |
| 209 | + // Declare and initializing the scaling factors in the SpMV computation. |
| 210 | + // Scalars correspond to order-0 tensors in taco. |
| 211 | + Tensor<double> alpha(42.0); |
| 212 | + Tensor<double> beta(33.0); |
| 213 | + |
| 214 | + // Declare the output matrix to be a sparse matrix with the same dimensions as |
| 215 | + // input matrix B, to be also stored as a doubly compressed sparse row matrix. |
| 216 | + Tensor<double> y({A.getDimension(0)}, dv); |
| 217 | + // Define the SpMV computation using index notation. |
| 218 | + IndexVar i, j; |
| 219 | + y(i) = alpha() * (A(i,j) * x(j)) + beta() * z(i); |
| 220 | + // At this point, we have defined how entries in the output vector should be |
| 221 | + // computed from entries in the input matrice and vectorsbut have not actually |
| 222 | + // performed the computation yet. To do so, we must first tell taco to generate |
| 223 | + // code that can be executed to compute the SpMV operation. |
| 224 | + y.compile(); |
| 225 | + // We can now call the functions taco generated to assemble the indices of the |
| 226 | + // output vector and then actually compute the SpMV. |
| 227 | + y.assemble(); |
| 228 | + y.compute(); |
| 229 | + // Write the output of the computation to file (stored in the FROSTT format). |
| 230 | + write("y.tns", y); |
| 231 | +}</code></pre> |
| 232 | + |
| 233 | +<p>You can also use the TACO Python library to perform the same computation, as |
| 234 | +demonstrated here:</p> |
172 | 235 | <pre class="highlight"><code class="language-python">import pytaco as pt
|
173 | 236 | from pytaco import compressed, dense
|
174 | 237 | import numpy as np
|
|
0 commit comments