-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathspmv_main.cpp
111 lines (93 loc) · 2.95 KB
/
spmv_main.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#include <math.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include "common.h"
const int N = 1000;
const int M = 10;
const int REPEAT = 100;
extern "C" {
void spmv(uint64_t n, const uint64_t *row, const uint64_t *col,
const double *mat, const double *x, double *y);
void spmv_compiler_vectorize(uint64_t n, const uint64_t *row,
const uint64_t *col, const double *mat,
const double *x, double *y);
void spmv_rvv(uint64_t n, const uint64_t *row, const uint64_t *col,
const double *mat, const double *x, double *y);
void spmv_rvv2(uint64_t n, const uint64_t *row, const uint64_t *col,
const double *mat, const double *x, double *y);
}
int main() {
uint64_t row[N + 1];
uint64_t col[N * M];
double mat[N * M];
double x[N];
double y[N];
double y1[N];
// each row has M non zero elements
for (int i = 0; i < N; i++) {
row[i] = i * M;
for (int j = 0; j < M; j++) {
mat[i * M + j] = (double)rand() / RAND_MAX;
col[i * M + j] = rand() % N;
}
x[i] = (double)rand() / RAND_MAX;
y[i] = 0.0;
y1[i] = 0.0;
}
row[N] = M * N;
spmv(N, row, col, mat, x, y);
spmv_compiler_vectorize(N, row, col, mat, x, y1);
for (int i = 0; i < N; i++) {
if (fabs(y[i] - y1[i]) > 1e-6) {
printf("Mismatch at %d: %lf and %lf\n", i, y[i], y1[i]);
return 1;
}
}
// check answer
spmv_rvv(N, row, col, mat, x, y1);
for (int i = 0; i < N; i++) {
if (fabs(y[i] - y1[i]) > 1e-6) {
printf("Mismatch at %d: %lf and %lf\n", i, y[i], y1[i]);
return 1;
}
}
spmv_rvv2(N, row, col, mat, x, y1);
for (int i = 0; i < N; i++) {
if (fabs(y[i] - y1[i]) > 1e-6) {
printf("Mismatch at %d: %lf and %lf\n", i, y[i], y1[i]);
return 1;
}
}
printf("Test passed!\n");
uint64_t begin = get_time_us();
for (int i = 0; i < REPEAT; i++) {
spmv(N, row, col, mat, x, y1);
}
uint64_t elapsed = get_time_us() - begin;
double gflops = 2e-3 * N * M * REPEAT / elapsed;
printf("spmv disable vectorize: %.2f us %.2f gflops\n", (double)elapsed / REPEAT,
gflops);
begin = get_time_us();
for (int i = 0; i < REPEAT; i++) {
spmv_compiler_vectorize(N, row, col, mat, x, y1);
}
elapsed = get_time_us() - begin;
gflops = 2e-3 * N * M * REPEAT / elapsed;
printf("spmv compiler vectorize: %.2f us %.2f gflops\n", (double)elapsed / REPEAT, gflops);
begin = get_time_us();
for (int i = 0; i < REPEAT; i++) {
spmv_rvv(N, row, col, mat, x, y1);
}
elapsed = get_time_us() - begin;
gflops = 2e-3 * N * M * REPEAT / elapsed;
printf("spmv rvv: %.2f us %.2f gflops\n", (double)elapsed / REPEAT, gflops);
begin = get_time_us();
for (int i = 0; i < REPEAT; i++) {
spmv_rvv2(N, row, col, mat, x, y1);
}
elapsed = get_time_us() - begin;
gflops = 2e-3 * N * M * REPEAT / elapsed;
printf("spmv rvv2: %.2f us %.2f gflops\n", (double)elapsed / REPEAT, gflops);
return 0;
}