This repository was archived by the owner on Oct 31, 2023. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathSparseArithmeticPerfTest.cpp
More file actions
150 lines (127 loc) · 4.05 KB
/
Copy pathSparseArithmeticPerfTest.cpp
File metadata and controls
150 lines (127 loc) · 4.05 KB
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
#include "gtest/gtest.h"
#include "Sparse/Arithmetic.h"
#include "Sparse/SparseFloatTensor.h"
#include <iostream>
#include <omp.h>
using namespace ops;
MemWrapper<SpMatMap> genBandCSR(DimensionType rows, DimensionType cols, DimensionType bandwidth)
{
EXPECT_GT(rows, 0);
EXPECT_GT(cols, 0);
EXPECT_GT(bandwidth, 0);
EXPECT_EQ(rows, cols);
EXPECT_EQ(bandwidth%2, 1);
DimensionType halfsize = bandwidth / 2;
DimensionType nnz = rows * bandwidth - (halfsize + 1) * halfsize;
OrdinalType * outer;
DimensionType * inner;
DataType * values;
Malloc(outer, (rows + 1) * sizeof(OrdinalType));
Malloc(inner, nnz * sizeof(DimensionType));
Malloc(values, nnz * sizeof(DataType));
OrdinalType offset = 0;
for (DimensionType i=0; i<rows; ++i) {
outer[i] = offset;
DimensionType s = std::max((DimensionType)0, i-halfsize);
DimensionType e = std::min(cols, i+halfsize+1);
for (DimensionType j=s; j<e; ++j, ++offset) {
inner[offset] = j;
values[offset] = 1;
}
}
EXPECT_EQ(nnz, offset);
outer[rows] = nnz;
return MemWrapper<SpMatMap>(SpMatMap(rows, cols, nnz, outer, inner, values), std::vector<void *>{ values, outer, inner });
}
COO genBandCOO(DimensionType rows, DimensionType cols, DimensionType bandwidth)
{
EXPECT_GT(rows, 0);
EXPECT_GT(cols, 0);
EXPECT_GT(bandwidth, 0);
EXPECT_EQ(rows, cols);
EXPECT_EQ(bandwidth%2, 1);
DimensionType halfsize = bandwidth / 2;
DimensionType nnz = rows * bandwidth - (halfsize + 1) * halfsize;
Array<DimensionType> row_index(nnz);
Array<DimensionType> col_index(nnz);
Array<DataType> values(nnz);
OrdinalType offset = 0;
for (DimensionType i=0; i<rows; ++i) {
DimensionType s = std::max((DimensionType)0, i-halfsize);
DimensionType e = std::min(cols, i+halfsize+1);
for (DimensionType j=s; j<e; ++j, ++offset) {
row_index[offset] = i;
col_index[offset] = j;
values[offset] = 1;
}
}
EXPECT_EQ(nnz, offset);
return COO(rows, cols, row_index, col_index, values);
}
size_t widthstart = 1000;
size_t widthend = 1024000;
size_t bandsize = 101;
size_t runs = 10;
template<class T>
void perfTestUnary(T op, const char * opname) {
for (size_t x=widthstart; x<=widthend; x*=2) {
for (size_t it=0; it<=runs; it++) {
auto A = genBandCSR(x, x, bandsize);
double timebegin, timeend;
timebegin = omp_get_wtime();
auto B = op(A.get());
timeend = omp_get_wtime();
printf("PerfTest: %s %ld th run with matrix width %ld band-size %ld took %f seconds\n",
opname, it, x, bandsize, timeend - timebegin);
}
}
}
template<class T>
void perfTestBinary(T op, const char * opname) {
for (size_t x=widthstart; x<=widthend; x*=2) {
for (size_t it=0; it<=runs; it++) {
auto A = genBandCSR(x, x, bandsize);
auto B = genBandCSR(x, x, bandsize);
double timebegin, timeend;
timebegin = omp_get_wtime();
auto C = op(A.get(), B.get());
timeend = omp_get_wtime();
printf("PerfTest: %s %ld th run with matrix width %ld band-size %ld took %f seconds\n",
opname, it, x, bandsize, timeend - timebegin);
}
}
}
TEST(OnBandmatrices, matmul) {
perfTestBinary(matmul, "matmul");
}
TEST(OnBandmatrices, times) {
perfTestBinary(times, "times");
}
TEST(OnBandmatrices, sub) {
perfTestBinary(sub, "sub");
}
TEST(OnBandmatrices, add) {
perfTestBinary(add, "add");
}
TEST(OnBandmatrices, transpose) {
perfTestUnary(transpose, "transpose");
}
TEST(OnBandmatrices, cooTocsr) {
for (size_t x=widthstart; x<=widthend; x*=2) {
auto A = genBandCOO(x, x, bandsize);
for (size_t it=0; it<=runs; it++) {
double timebegin, timeend;
timebegin = omp_get_wtime();
auto B = cooTocsr(A);
timeend = omp_get_wtime();
printf("PerfTest: cooTocsr %ld th run with matrix width %ld band-size %ld took %f seconds\n",
it, x, bandsize, timeend - timebegin);
}
}
}