#include
#include
extern double mysecond(void);
/*
* Example of sparse matrix-vector multiply, using CSR (compressed sparse
* row format). Because this code is in C, the index values in the ia and ja
* arrays are zero origin rather than one origin as they would be in Fortran.
*
* While this code does time the sparse matrix-vector product, as noted below,
* these timings are too simple to be used for serious analysis.
*/
int main(int argc, char *argv[])
{
int *ia, *ja;
double *a, *x, *y;
int row, i, j, idx, n, nnzMax, nnz, nrows;
double ts, t, rate;
n = 10;
if (argc > 1) n = atoi(argv[1]);
/* Warning: if n > sqrt(2^31), you may get integer overflow */
/* Allocate enough storage for the matix. We allocate more than
is needed in order to simplify the code */
nrows = n * n;
nnzMax = nrows * 5;
ia = (int*)malloc(nrows*sizeof(int));
ja = (int*)malloc(nnzMax*sizeof(int));
a = (double*)malloc(nnzMax*sizeof(double));
/* Allocate the source and result vectors */
x = (double*)malloc(nrows*sizeof(double));
y = (double*)malloc(nrows*sizeof(double));
/* Create a pentadiagonal matrix, representing very roughly a finite
difference approximation to the Laplacian on a square n x n mesh */
row = 0;
nnz = 0;
for (i=0; i0) { ja[nnz] = row - n; a[nnz] = -1.0; nnz++; }
if (j>0) { ja[nnz] = row - 1; a[nnz] = -1.0; nnz++; }
ja[nnz] = row; a[nnz] = 4.0; nnz++;
if (j