-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbench_fmm.cpp
172 lines (139 loc) · 5.4 KB
/
bench_fmm.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
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
// Copyright (C) 2023 Adam Lugowski. All rights reserved.
// Use of this source code is governed by the BSD 2-clause license found in the LICENSE.txt file.
// SPDX-License-Identifier: BSD-2-Clause
#include "common.hpp"
#include <fast_matrix_market/fast_matrix_market.hpp>
template <typename IT, typename VT>
struct triplet_matrix {
int64_t nrows = 0, ncols = 0;
std::vector<IT> rows;
std::vector<IT> cols;
std::vector<VT> vals;
[[nodiscard]] size_t size_bytes() const {
return sizeof(IT)*rows.size() + sizeof(IT)*cols.size() + sizeof(VT)*vals.size();
}
};
template <typename IT, typename VT>
struct csc_matrix {
int64_t nrows = 0, ncols = 0;
std::vector<IT> indptr;
std::vector<IT> indices;
std::vector<VT> vals;
[[nodiscard]] size_t size_bytes() const {
return sizeof(IT)*indptr.size() + sizeof(IT)*indices.size() + sizeof(VT)*vals.size();
}
};
template <typename VT>
struct array_matrix {
int64_t nrows = 0, ncols = 0;
std::vector<VT> vals;
[[nodiscard]] size_t size_bytes() const {
return sizeof(VT)*vals.size();
}
};
/**
* Read MatrixMarket with fast_matrix_market.
*/
void FMM_read(benchmark::State& state) {
problem& prob = get_problem((int)state.range(0));
// read options
fast_matrix_market::read_options options{};
options.parallel_ok = true;
options.num_threads = (int)state.range(1);
std::size_t num_bytes = 0;
for ([[maybe_unused]] auto _ : state) {
fast_matrix_market::matrix_market_header header;
triplet_matrix<INDEX_TYPE, VALUE_TYPE> triplet;
std::ifstream iss(prob.mm_path);
fast_matrix_market::read_matrix_market_triplet(iss, header, triplet.rows, triplet.cols, triplet.vals, options);
num_bytes += std::filesystem::file_size(prob.mm_path);
benchmark::ClobberMemory();
}
state.SetBytesProcessed((int64_t)num_bytes);
state.SetLabel("problem_name=" + prob.name);
}
BENCHMARK(FMM_read)->Name("op:read/impl:FMM/format:MatrixMarket")->UseRealTime()->Iterations(num_iterations)->Apply(BenchmarkArgument);
/**
* Write MatrixMarket with fast_matrix_market.
*/
void FMM_write(benchmark::State& state) {
std::size_t num_bytes = 0;
problem& prob = get_problem((int)state.range(0));
fast_matrix_market::write_options options;
options.parallel_ok = true;
options.num_threads = (int)state.range(1);
// load the problem to be written later
triplet_matrix<INDEX_TYPE, VALUE_TYPE> triplet;
{
std::ifstream f(prob.mm_path);
fast_matrix_market::read_matrix_market_triplet(f, triplet.nrows, triplet.ncols, triplet.rows, triplet.cols, triplet.vals);
}
auto out_path = temporary_write_dir / ("write_" + prob.name + ".mtx");
for ([[maybe_unused]] auto _ : state) {
#define USE_OSS 0
#if USE_OSS
std::ostringstream oss;
#else
std::ofstream oss{out_path, std::ios_base::binary};
#endif
fast_matrix_market::write_matrix_market_triplet(oss,
{triplet.nrows, triplet.ncols},
triplet.rows, triplet.cols, triplet.vals,
options);
#if USE_OSS
num_bytes += oss.str().size();
#else
num_bytes += std::filesystem::file_size(out_path);
#endif
benchmark::ClobberMemory();
}
if (delete_written_files_on_finish) {
std::filesystem::remove(out_path);
}
state.SetBytesProcessed((int64_t)num_bytes);
state.SetLabel("problem_name=" + prob.name);
}
BENCHMARK(FMM_write)->Name("op:write/impl:FMM/format:MatrixMarket")->UseRealTime()->Iterations(num_iterations)->Apply(BenchmarkArgument);
/**
* Write MatrixMarket with fast_matrix_market.
*/
void FMM_write_pattern(benchmark::State& state) {
std::size_t num_bytes = 0;
problem& prob = get_problem((int)state.range(0));
fast_matrix_market::write_options options;
options.parallel_ok = true;
options.num_threads = (int)state.range(1);
// load the problem to be written later
triplet_matrix<INDEX_TYPE, VALUE_TYPE> triplet;
{
std::ifstream f(prob.mm_path);
fast_matrix_market::read_matrix_market_triplet(f, triplet.nrows, triplet.ncols, triplet.rows, triplet.cols, triplet.vals);
// do not care about the values
triplet.vals.clear();
}
auto out_path = temporary_write_dir / ("write_" + prob.name + "-pattern.mtx");
for ([[maybe_unused]] auto _ : state) {
#define USE_OSS 0
#if USE_OSS
std::ostringstream oss;
#else
std::ofstream oss{out_path, std::ios_base::binary};
#endif
fast_matrix_market::write_matrix_market_triplet(oss,
{triplet.nrows, triplet.ncols},
triplet.rows, triplet.cols, triplet.vals,
options);
#if USE_OSS
num_bytes += oss.str().size();
#else
num_bytes += std::filesystem::file_size(out_path);
#endif
benchmark::ClobberMemory();
}
if (delete_written_files_on_finish) {
std::filesystem::remove(out_path);
}
state.SetBytesProcessed((int64_t)num_bytes);
state.SetLabel("problem_name=" + prob.name);
}
BENCHMARK(FMM_write_pattern)->Name("op:write/impl:FMM/format:MatrixMarket(pattern)")->UseRealTime()->Iterations(num_iterations)->Apply(BenchmarkArgument);