-
Notifications
You must be signed in to change notification settings - Fork 1
/
serial.cpp
90 lines (72 loc) · 2.21 KB
/
serial.cpp
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
// SpMV Code
// Created: 03-12-2019
// Author: Najeeb Ahmad
// Updated: 13-05-2020
// Author: Muhammad Aditya Sasongko
#include "stdc++.h"
#include "../include/common.h"
#include "../include/matrix.h"
#include "../include/mmio.h"
using namespace std;
int main(int argc, char **argv)
{
if(argc < 3)
{
cout << "Error: Missing arguments\n";
cout << "Usage: " << argv[0] << " matrix.mtx\n" << " time_step_count";
return EXIT_FAILURE;
}
csr_matrix matrix;
string matrix_name;
printf("Reading .mtx file\n");
int retCode = 0;
int time_steps = atoi(argv[2]);
matrix_name = argv[1];
cout << matrix_name << endl;
double *rhs;
double *result;
retCode = mm_read_unsymmetric_sparse(argv[1], &matrix.m, &matrix.n, &matrix.nnz,
&matrix.csrVal, &matrix.csrRowPtr, &matrix.csrColIdx);
// Allocate vector rhs
rhs = (double *)malloc(sizeof(double) * matrix.n);
// Allocate vector result
result = (double *)malloc(sizeof(double) * matrix.n);
if(retCode == -1)
{
cout << "Error reading input .mtx file\n";
return EXIT_FAILURE;
}
printf("Matrix Rows: %d\n", matrix.m);
printf("Matrix Cols: %d\n", matrix.n);
printf("Matrix nnz: %d\n", matrix.nnz);
coo2csr_in(matrix.m, matrix.nnz, matrix.csrVal, matrix.csrRowPtr, matrix.csrColIdx);
printf("Done reading file\n");
// Initialize right-hand-side
for(int i = 0; i < matrix.n; i++)
rhs[i] = (double) 1.0/matrix.n;
clock_t start, end;
start = clock();
for(int k = 0; k < time_steps; k++) {
for(int i = 0; i < matrix.m; i++)
{
result[i] = 0.0;
for(int j = matrix.csrRowPtr[i]; j < matrix.csrRowPtr[i+1]; j++)
{
result[i] += matrix.csrVal[j] * rhs[matrix.csrColIdx[j]];
}
}
for(int i = 0; i < matrix.m; i++)
{
rhs[i] = result[i];
}
}
end = clock();
double time_taken = double(end - start) / double(CLOCKS_PER_SEC);
cout << "Time taken by program is : " << fixed
<< time_taken << setprecision(5);
cout << " sec " << endl;
// for(int i = 0; i < matrix.m; i++)
// cout << rhs[i] << endl;
// cout << endl;
return EXIT_SUCCESS;
}