-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathaxpy_main.cpp
98 lines (83 loc) · 2.42 KB
/
axpy_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
#include <math.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include "common.h"
const int N = 1000;
const int REPEAT = 100;
extern "C" {
void axpy(uint64_t n, double a, const double *x, double *y);
void axpy_compiler_vectorize(uint64_t n, double a, const double *x, double *y);
void axpy_rvv(uint64_t n, double a, const double *x, double *y);
void axpy_rvv2(uint64_t n, double a, const double *x, double *y);
}
int main() {
double x[N];
double y[N];
double y1[N];
double y2[N];
double y3[N];
// each row has M non zero elements
for (int i = 0; i < N; i++) {
x[i] = (double)rand() / RAND_MAX;
double val = (double)rand() / RAND_MAX;
y[i] = val;
y1[i] = val;
y2[i] = val;
y3[i] = val;
}
double a = (double)rand() / RAND_MAX;
axpy(N, a, x, y);
axpy_compiler_vectorize(N, a, x, y1);
axpy_rvv(N, a, x, y2);
axpy_rvv2(N, a, x, y3);
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;
}
}
for (int i = 0; i < N; i++) {
if (fabs(y[i] - y2[i]) > 1e-6) {
printf("Mismatch at %d: %lf and %lf\n", i, y[i], y1[i]);
return 1;
}
}
for (int i = 0; i < N; i++) {
if (fabs(y[i] - y3[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++) {
axpy(N, a, x, y);
}
uint64_t elapsed = get_time_us() - begin;
double gflops = 2e-3 * N * REPEAT / elapsed;
printf("axpy disable vectorize: %.2f us %.2f gflops\n", (double)elapsed / REPEAT,
gflops);
begin = get_time_us();
for (int i = 0; i < REPEAT; i++) {
axpy_compiler_vectorize(N, a, x, y);
}
elapsed = get_time_us() - begin;
gflops = 2e-3 * N * REPEAT / elapsed;
printf("axpy compiler vectorize: %.2f us %.2f gflops\n", (double)elapsed / REPEAT, gflops);
begin = get_time_us();
for (int i = 0; i < REPEAT; i++) {
axpy_rvv(N, a, x, y);
}
elapsed = get_time_us() - begin;
gflops = 2e-3 * N * REPEAT / elapsed;
printf("axpy rvv: %.2f us %.2f gflops\n", (double)elapsed / REPEAT, gflops);
begin = get_time_us();
for (int i = 0; i < REPEAT; i++) {
axpy_rvv2(N, a, x, y);
}
elapsed = get_time_us() - begin;
gflops = 2e-3 * N * REPEAT / elapsed;
printf("axpy rvv2: %.2f us %.2f gflops\n", (double)elapsed / REPEAT, gflops);
return 0;
}