1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
|
#include <iostream>
#include <stdlib.h>
#include <time.h>
class CSRMatrix{
protected:
long rows;
long cols;
long nz;
double* val;
long* col_ind;
long* row_ptr;
public:
CSRMatrix(long inrows, long incols, long innz) {
rows = inrows; cols = incols; nz = innz;
val = new double[nz];
col_ind = new long[nz];
row_ptr = new long[inrows+1];
long col_ind1[] = {0,1,2,3,0,1,2,3,2};
long row_ptr1[] = {0,4,6,8,9};
std::copy(col_ind1,col_ind1+nz,col_ind);
std::copy(row_ptr1,row_ptr1+inrows+1,row_ptr);
srand(static_cast<unsigned>(time(NULL)));
// Create random values for val
for (int i=0;i<innz;i++){
val[i] = rand() % 10 + 1;
}
};
//˜CSRMatrix(){ };
void multiply(double* x,double* y) {
for (int i=0; i < rows; i++) {
y[i] = 0;
for (int j=row_ptr[i]; j<row_ptr[i+1]; j++) {
y[i] += val[j] * x[col_ind[j]];
}
}
return;
}
};
|