-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy pathMain.cpp
154 lines (131 loc) · 6.01 KB
/
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
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
//===- Main.cpp -----------------------------------------------------------===//
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
//===----------------------------------------------------------------------===//
//
// This is the main file of FIROp benchmark.
//
//===----------------------------------------------------------------------===//
#include "Utils.hpp"
#include <benchmark/benchmark.h>
using namespace std;
// -----------------------------------------------------------------------------
// Benchmark Configuration. Modify the numbers here for a custom benchmark.
// -----------------------------------------------------------------------------
#define _NUM_ITER 10
#define _IN_OUT_SIZE 2000000
#define _KERNEL_SIZE 127
#define _PRINT true
// -----------------------------------------------------------------------------
// Global Variables and Functions. Please do not modify the code here.
// -----------------------------------------------------------------------------
univector<float, _IN_OUT_SIZE> firInput;
univector<float, _IN_OUT_SIZE> firOutput;
univector<float, _KERNEL_SIZE> taps127;
intptr_t sizeofAud[1] = {_IN_OUT_SIZE};
intptr_t sizeofKernel[1] = {_KERNEL_SIZE};
MemRef<float, 1> audRef(sizeofAud);
MemRef<float, 1> resRef(sizeofAud);
MemRef<float, 1> kernelRef(sizeofKernel);
using MLIRFunctionType = void (*)(MemRef<float, 1> *, MemRef<float, 1> *,
MemRef<float, 1> *);
using BuddyFunctionType = void (*)(MemRef<float, 1> *, MemRef<float, 1> *,
MemRef<float, 1> *, bool);
// Benchmarking function for MLIR based FIR method.
void DAP_OPS_FIR(benchmark::State &state, MLIRFunctionType func) {
MemRef<float, 1> resRef(sizeofAud, 0.0);
for (auto _ : state) {
func(&audRef, &kernelRef, &resRef);
}
benchmark::DoNotOptimize(resRef);
}
void DAP_OPS_FIR(benchmark::State &state, BuddyFunctionType func,
bool isVectorization) {
MemRef<float, 1> resRef(sizeofAud, 0.0);
for (auto _ : state) {
func(&audRef, &kernelRef, &resRef, isVectorization);
}
benchmark::DoNotOptimize(resRef);
}
// Benchmarking function for KFR FIR method.
static void KFR_FIR(benchmark::State &state) {
for (auto _ : state) {
firOutput = kfr::fir(firInput, taps127);
}
}
// Verifies the result of an MLIR-based function against expected output.
void Verification(const univector<float, _IN_OUT_SIZE> &outputExpected,
MLIRFunctionType MLIRFunc, const std::string &name) {
// Initialize MemRef with all zeros.
MemRef<float, 1> outputGenerated(sizeofAud, 0.0);
MLIRFunc(&audRef, &kernelRef, &outputGenerated);
firOp::printMemRef(outputGenerated, name, /*doPrint=*/_PRINT);
firOp::verify(outputExpected, outputGenerated, _IN_OUT_SIZE, name);
}
void Verification(const univector<float, _IN_OUT_SIZE> &outputExpected,
BuddyFunctionType BuddyFunc, bool isVectorization,
const std::string &name) {
// Initialize MemRef with all zeros.
MemRef<float, 1> outputGenerated(sizeofAud, 0.0);
BuddyFunc(&audRef, &kernelRef, &outputGenerated, isVectorization);
firOp::printMemRef(outputGenerated, name, /*doPrint=*/_PRINT);
firOp::verify(outputExpected, outputGenerated, _IN_OUT_SIZE, name);
}
// -----------------------------------------------------------------------------
// Register Benchmark.
// -----------------------------------------------------------------------------
BENCHMARK_CAPTURE(DAP_OPS_FIR, mlir_scalar, _mlir_ciface_fir_scalar)
->Unit(benchmark::kMillisecond)
->Iterations(_NUM_ITER);
BENCHMARK_CAPTURE(DAP_OPS_FIR, buddy_scalar, dap::FIR<float, 1>, false)
->Unit(benchmark::kMillisecond)
->Iterations(_NUM_ITER);
BENCHMARK_CAPTURE(DAP_OPS_FIR, mlir_vectorize, _mlir_ciface_fir_vectorization)
->Unit(benchmark::kMillisecond)
->Iterations(_NUM_ITER);
BENCHMARK_CAPTURE(DAP_OPS_FIR, mlir_tiled_vectorize,
_mlir_ciface_fir_tiled_vectorization)
->Unit(benchmark::kMillisecond)
->Iterations(_NUM_ITER);
BENCHMARK_CAPTURE(DAP_OPS_FIR, buddy_tiled_vectorize, dap::FIR<float, 1>, true)
->Unit(benchmark::kMillisecond)
->Iterations(_NUM_ITER);
BENCHMARK(KFR_FIR)->Unit(benchmark::kMillisecond)->Iterations(_NUM_ITER);
// -----------------------------------------------------------------------------
// Main Function.
// -----------------------------------------------------------------------------
int main(int argc, char **argv) {
// Initialize univectors and MemRefs.
firOp::initializeKFRFIR(firInput, taps127);
audRef = std::move(MemRef<float, 1>(firInput.data(), sizeofAud));
kernelRef = std::move(MemRef<float, 1>(taps127.data(), sizeofKernel));
resRef = std::move(MemRef<float, 1>(sizeofAud, 0.0));
// Run benchmark.
::benchmark::Initialize(&argc, argv);
::benchmark::RunSpecifiedBenchmarks();
std::cout << "\033[34m---------- Verification ----------\033[0m" << std::endl;
// Obtain KFR output results as expected results in verification.
firOutput = kfr::fir(firInput, taps127);
firOp::printUnivector(firOutput, /*doPrint=*/_PRINT);
// Verify the correctness of all methods.
Verification(firOutput, _mlir_ciface_fir_scalar, "MLIRScalar");
Verification(firOutput, dap::FIR<float, 1>, /*isVectorization=*/false,
"BuddyScalar");
Verification(firOutput, _mlir_ciface_fir_vectorization, "MLIRVectorize");
Verification(firOutput, _mlir_ciface_fir_tiled_vectorization,
"MLIRTiledVectorize");
Verification(firOutput, dap::FIR<float, 1>, /*isVectorization=*/true,
"BuddyTiledVectorize");
return 0;
}