-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbenchmark.cpp
More file actions
173 lines (132 loc) · 6.03 KB
/
benchmark.cpp
File metadata and controls
173 lines (132 loc) · 6.03 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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#include "CSF/SparseMatrix"
#include "misc/matrix_creator.cpp"
#include <functional>
#include <chrono>
#include <iostream>
#include <vector>
#include <numeric>
#define SEED 1
#define COMPRESSION_LEVEL 3
#define VAL_TYPE int
#define INDEX_TYPE uint64_t
// Test Driver
template <typename T> void testDriver(int numRows, int numCols, int sparsity, int iterations);
// Generative Functions
template <typename T> Eigen::SparseMatrix<T> generateMatrix(Eigen::SparseMatrix<T>& eigenMatrix, int sparsity);
template <typename T> Eigen::SparseMatrix<T> generateMatrix(Eigen::SparseMatrix<T>& eigenMatrix, int sparsity, int value);
// Benchmark Functions
template <typename T> void vectorBenchmark(int rows, int cols, int sparsity);
/************************************************************
* TODO * *
* CSF VS EIGEN *
* - MATRIX COLUMN SUMS *
* - SCALAR MULTIPLICATION *
* - VECTOR MULTIPLICATION *
* - CONSTRUCTION FROM FILE *
* *
* CSF<A> VS CSF<B> *
* NEW CSF CONSTRUCTOR VS OLD CSF CONSTRUCTOR *
* NEW ITERATOR VS OLD ITERATOR *
* *
* *
* *
* CURRENT FOCUS *
* - SMART METHOD FOR CONDUCTING TESTS *
* - ANALYSIS FUNCTIONS *
* - WRITING RESULTS TO FILE (JSON?) *
* *
* *
***********************************************************/
int main() {
//Commented out areas for specific matrices
// InnerIteratorBenchmark<VAL_TYPE>(100, 100, 50, 1288115545);
// ScalarMultiplicationBench<VAL_TYPE>(100, 100, 50, 1296273897);
testDriver<VAL_TYPE>(100000, 1, 1, 10);
std::cout << "\u001b[32mDone!\u001b[0m" << std::endl;
return 1;
}
template <typename T>
void testDriver(int numRows, int numCols, int sparsity, int iterations) {
float average;
std::vector<uint64_t> times = std::vector<uint64_t>();
for (int i = 0; i < iterations; i++) {
vectorBenchmark<T>(numRows, 1, sparsity);
}
average = std::reduce(times.begin(), times.end()) / times.size();
std::cout << "Average Time: " << average << std::endl;
}
template <typename T>
void vectorBenchmark(int rows, int cols, int sparsity) {
std::chrono::time_point<std::chrono::system_clock> start, end;
//Create Eigen Matrix
start = std::chrono::system_clock::now();
Eigen::SparseMatrix<T> eigenMatrix(rows, 1);
eigenMatrix = generateMatrix<T>(eigenMatrix, sparsity, 1);
eigenMatrix.makeCompressed();
end = std::chrono::system_clock::now();
uint64_t eigenConstructTime = std::chrono::duration_cast<std::chrono::nanoseconds>(end - start).count();
std::cout << "Eigen Matrix Filled With 1's (1xN) Generation Time: " << eigenConstructTime << "ns" << std::endl;
//Create CSF Matrix
start = std::chrono::system_clock::now();
CSF::SparseMatrix<T, INDEX_TYPE, COMPRESSION_LEVEL> csfMatrix(eigenMatrix);
end = std::chrono::system_clock::now();
uint64_t csfConstructTime = std::chrono::duration_cast<std::chrono::nanoseconds>(end - start).count();
std::cout << "CSF Matrix (1xN) Generation time (Eigen Constructor): " << csfConstructTime << "ns" << std::endl;
start = std::chrono::system_clock::now();
T eigenTotal = 0;
for (typename Eigen::SparseMatrix<T>::InnerIterator it(eigenMatrix, 0); it; ++it) {
eigenTotal += it.value();
}
end = std::chrono::system_clock::now();
uint64_t eigenSumTime = std::chrono::duration_cast<std::chrono::nanoseconds>(end - start).count();
std::cout << "Eigen Matrix (1xN) Column Sum Time: " << eigenSumTime << "ns" << std::endl;
start = std::chrono::system_clock::now();
T csfTotal = 0;
for (typename CSF::SparseMatrix<T, INDEX_TYPE, COMPRESSION_LEVEL>::InnerIterator it(csfMatrix, 0); it; it++) {
csfTotal += *it;
}
end = std::chrono::system_clock::now();
uint64_t csfSumTime = std::chrono::duration_cast<std::chrono::nanoseconds>(end - start).count();
std::cout << "CSF Matrix (1xN) Column Sum Time: " << csfSumTime << "ns" << std::endl;
assert(eigenTotal == csfTotal);
}
/**
* @brief Fills a matrix with random values at a specified sparsity
*
*/
template <typename T>
Eigen::SparseMatrix<T> generateMatrix(Eigen::SparseMatrix<T>& eigenMatrix, int sparsity) {
// generate a random sparse matrix
rng randMatrixGen = rng(SEED);
int numRows = eigenMatrix.rows();
int numCols = eigenMatrix.cols();
eigenMatrix.reserve(Eigen::VectorXi::Constant(numRows * 100, numCols * 100));
for (int i = 0; i < numRows; i++) {
for (int j = 0; j < numCols; j++) {
if (randMatrixGen.draw<int>(i, j, sparsity)) {
eigenMatrix.insert(i, j) = ceil(10 * randMatrixGen.uniform<double>(j));
}
}
}
return eigenMatrix;
}
/**
* @brief Fills matrix with a specified value. Addition option for sparsity is inclduded
*
*/
template <typename T>
Eigen::SparseMatrix<T> generateMatrix(Eigen::SparseMatrix<T>& eigenMatrix, int sparsity, int value) {
// generate a random sparse matrix
rng randMatrixGen = rng(SEED);
int numRows = eigenMatrix.rows();
int numCols = eigenMatrix.cols();
eigenMatrix.reserve(Eigen::VectorXi::Constant(numRows, numCols * numRows));
for (int i = 0; i < numRows; i++) {
for (int j = 0; j < numCols; j++) {
if (randMatrixGen.draw<int>(i, j, sparsity)) {
eigenMatrix.insert(i, j) = value;
}
}
}
return eigenMatrix;
}