Advertisement
cyphric

Untitled

Apr 5th, 2022
769
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.49 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <sys/time.h>
  4.  
  5. double mysecond(){
  6.   struct timeval tp;
  7.   struct timezone tzp;
  8.   int i;
  9.  
  10.   i = gettimeofday(&tp,&tzp);
  11.   return ( (double) tp.tv_sec + (double) tp.tv_usec * 1.e-6 );
  12. }
  13.  
  14. /*
  15.  * Example of sparse matrix-vector multiply, using CSR (compressed sparse
  16.  * row format).
  17.  *
  18.  */
  19. int main(int argc, char *argv[])
  20. {
  21.     int    *ia, *ja;
  22.     double *a, *x, *y;
  23.     int    row, i, j, idx, n, nnzMax, nnz, nrows;
  24.     double ts, t, rate;
  25.  
  26.     n = 10;
  27.     if (argc > 1) n = atoi(argv[1]);
  28.  
  29.     /* Warning: if n > sqrt(2^31), you may get integer overflow */
  30.  
  31.     /* Allocate enough storage for the matix.  We allocate more than
  32.        is needed in order to simplify the code */
  33.  
  34.     nrows  = n * n;
  35.     nnzMax = nrows * 5;
  36.     ia = (int*)malloc(nrows*sizeof(int));
  37.     ja = (int*)malloc(nnzMax*sizeof(int));
  38.     a  = (double*)malloc(nnzMax*sizeof(double));
  39.     /* Allocate the source and result vectors */
  40.     x = (double*)malloc(nrows*sizeof(double));
  41.     y = (double*)malloc(nrows*sizeof(double));
  42.  
  43.     /* Create a pentadiagonal matrix, representing very roughly a finite
  44.        difference approximation to the Laplacian on a square n x n mesh */
  45.     row = 0;
  46.     nnz = 0;
  47.     for (i=0; i<n; i++) {
  48.     for (j=0; j<n; j++) {
  49.         ia[row] = nnz;
  50.         if (i>0) { ja[nnz] = row - n; a[nnz] = -1.0; nnz++; }
  51.         if (j>0) { ja[nnz] = row - 1; a[nnz] = -1.0; nnz++; }
  52.         ja[nnz] = row; a[nnz] = 4.0; nnz++;
  53.         if (j<n-1) { ja[nnz] = row + 1; a[nnz] = -1.0; nnz++; }
  54.         if (i<n-1) { ja[nnz] = row + n; a[nnz] = -1.0; nnz++; }
  55.         row++;
  56.     }
  57.     }
  58.     ia[row] = nnz;
  59.  
  60.     /* Create the source (x) vector */
  61.     for (i=0; i<nrows; i++) x[i] = 1.0;
  62.  
  63.     /* Perform a matrix-vector multiply: y = A*x */
  64.     /* Warning: To use this for timing, you need to (a) handle cold start
  65.        (b) perform enough tests to make timing quantum relatively small */
  66.     ts = mysecond();
  67.     for (row=0; row<nrows; row++) {
  68.     double sum = 0.0;
  69.     for (idx=ia[row]; idx<ia[row+1]; idx++) {
  70.         sum += a[idx] * x[ja[idx]];
  71.     }
  72.     y[row] = sum;
  73.     }
  74.     t = mysecond() - ts;
  75.     /* Compute with the result to keep the compiler for marking the
  76.        code as dead */
  77.     for (row=0; row<nrows; row++) {
  78.     if (y[row] < 0) {
  79.         fprintf(stderr,"y[%d]=%f, fails consistency test\n", row, y[row]);
  80.     }
  81.     }
  82.     printf("Time for Sparse Ax, nrows=%d, nnz=%d, T = %f\n", nrows, nnz, t);
  83.  
  84.     free(ia); free(ja); free(a); free(x); free(y);
  85.     return 0;
  86. }
  87.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement