# Sparse matrix vector product using PETSC

I am trying to do a simple parallel sparse matrix vector multiplications using PETSC. My sparse matrix is a simple tridiagonal laplacian matrix, which is distributed over multiple processors using PETSC.

My main question is that if I do the same operation by simply iterating over the vector and updating each value A[i] = -1*A[i-1] + 2*A[i] + -1*A[i+1], it takes much lesser time than by using the PETSC SpMV. Why is it so? Or am I doing something wrong?

• One note that may or may not be relevant: unless you're doing a Gauss-Seidel solve of a tridiagonal system, you likely have a bug in your hard-coded method. As written, you're updating A[i] in one loop iteration and using it again in the next, which is different than computing the matrix-vector product. Jun 17 '15 at 2:12

• I assume that you are comparing multiplication with an assembled PETSc matrix with your hand-coded matrix-free method. The latter may indeed be faster, but this could be because no entries of A need be loaded from memory. A more meaningful comparison might be to a matrix-free operator in PETSc (see Section 3.3 in the manual).
• Once you have an apples-to-apples comparison, you can try to determine if any remaining speed differences are due to PETSc overhead or not by using an optimized build (configure --with-debugging=0) and running your code with the -log_summary option to see timings of various operations.
• Should be configure --enable-debug=0, no? Dec 6 '16 at 19:10
• No (this is PETSc's configure, not autotools if that's where you are getting that flag). Dec 7 '16 at 9:04