|
71 | 71 |
|
72 | 72 | <li class="toctree-l1"> |
73 | 73 |
|
| 74 | + <span class="caption-text">Python Library</span> |
| 75 | + <ul class="subnav"> |
| 76 | + <li class=""> |
| 77 | + |
| 78 | + <a class="" href="../pytensors/">Defining Tensors</a> |
| 79 | + </li> |
| 80 | + <li class=""> |
| 81 | + |
| 82 | + <a class="" href="../pycomputations/">Computing on Tensors</a> |
| 83 | + </li> |
| 84 | + </ul> |
| 85 | + </li> |
| 86 | + |
| 87 | + <li class="toctree-l1"> |
| 88 | + |
74 | 89 | <span class="caption-text">Example Applications</span> |
75 | 90 | <ul class="subnav"> |
76 | 91 | <li class=" current"> |
|
133 | 148 |
|
134 | 149 | <p>You can use the taco C++ library to easily and efficiently compute the SpMV as demonstrated here:</p> |
135 | 150 | <pre><code class="c++">// On Linux and MacOS, you can compile and run this program like so: |
136 | | -// g++ -std=c++11 -O3 -DNDEBUG -DTACO -I ../../include -L../../build/lib -ltaco spmv.cpp -o spmv |
| 151 | +// g++ -std=c++11 -O3 -DNDEBUG -DTACO -I ../../include -L../../build/lib spmv.cpp -o spmv -ltaco |
137 | 152 | // LD_LIBRARY_PATH=../../build/lib ./spmv |
138 | 153 |
|
139 | 154 | #include <random> |
|
162 | 177 | // Generate a random dense vector and store it in the dense vector format. |
163 | 178 | // Vectors correspond to order-1 tensors in taco. |
164 | 179 | Tensor<double> x({A.getDimension(1)}, dv); |
165 | | - for (int i = 0; i < x.getDimension(0)]; ++i) { |
| 180 | + for (int i = 0; i < x.getDimension(0); ++i) { |
166 | 181 | x.insert({i}, unif(gen)); |
167 | 182 | } |
168 | 183 | x.pack(); |
169 | 184 |
|
170 | 185 | // Generate another random dense vetor and store it in the dense vector format.. |
171 | 186 | Tensor<double> z({A.getDimension(0)}, dv); |
172 | | - for (int i = 0; i < z.getDimension(0)]; ++i) { |
| 187 | + for (int i = 0; i < z.getDimension(0); ++i) { |
173 | 188 | z.insert({i}, unif(gen)); |
174 | 189 | } |
175 | 190 | z.pack(); |
|
203 | 218 | } |
204 | 219 | </code></pre> |
205 | 220 |
|
| 221 | +<p>You can also use the Python library to compute SpMV as shown here:</p> |
| 222 | +<pre><code class="python">import pytaco as pt |
| 223 | +from pytaco import compressed, dense |
| 224 | +import numpy as np |
| 225 | + |
| 226 | +# Declare the storage formats as explained in the C++ sample |
| 227 | +csr = pt.format([dense, compressed]) |
| 228 | +dv = pt.format([dense]) |
| 229 | + |
| 230 | +# Load a sparse matrix stored in the matrix market format) and store it as a csr matrix. |
| 231 | +# The matrix # in this example can be downloaded from: |
| 232 | +# https://www.cise.ufl.edu/research/sparse/MM/Boeing/pwtk.tar.gz |
| 233 | +A = pt.read("pwtk.mtx", csr) |
| 234 | + |
| 235 | +# Generate two random vectors using numpy and pass them into taco |
| 236 | +x = pt.from_numpy_array(np.random.uniform(size=A.shape[0])) |
| 237 | +z = pt.from_numpy_array(np.random.uniform(size=A.shape[0])) |
| 238 | + |
| 239 | +# Declare output vector as dense |
| 240 | +y = pt.tensor([A.shape[0]], dv) |
| 241 | + |
| 242 | +# Create index vars |
| 243 | +i, j = pt.get_index_vars(2) |
| 244 | + |
| 245 | +# Define the SpMV computation |
| 246 | +y[i] = 42 * A[i, j] * x[j] + 33 * z[i] |
| 247 | + |
| 248 | +# Store the output |
| 249 | +pt.write("y.tns", y) |
| 250 | +</code></pre> |
| 251 | + |
206 | 252 | <p>Under the hood, when you run the above C++ program, taco generates the imperative code shown below to compute the SpMV. taco is able to evaluate this compound operation efficiently with a single kernel that avoids materializing the intermediate matrix-vector product.</p> |
207 | 253 | <pre><code class="c++">for (int iA = 0; iA < 217918; iA++) { |
208 | 254 | double tj = 0; |
|
225 | 271 | <a href="../machine_learning/" class="btn btn-neutral float-right" title="Machine Learning: SDDMM">Next <span class="icon icon-circle-arrow-right"></span></a> |
226 | 272 |
|
227 | 273 |
|
228 | | - <a href="../computations/" class="btn btn-neutral" title="Computing on Tensors"><span class="icon icon-circle-arrow-left"></span> Previous</a> |
| 274 | + <a href="../pycomputations/" class="btn btn-neutral" title="Computing on Tensors"><span class="icon icon-circle-arrow-left"></span> Previous</a> |
229 | 275 |
|
230 | 276 | </div> |
231 | 277 |
|
|
259 | 305 | <span class="rst-current-version" data-toggle="rst-current-version"> |
260 | 306 |
|
261 | 307 |
|
262 | | - <span><a href="../computations/" style="color: #fcfcfc;">« Previous</a></span> |
| 308 | + <span><a href="../pycomputations/" style="color: #fcfcfc;">« Previous</a></span> |
263 | 309 |
|
264 | 310 |
|
265 | 311 | <span style="margin-left: 15px"><a href="../machine_learning/" style="color: #fcfcfc">Next »</a></span> |
|
0 commit comments