-
Notifications
You must be signed in to change notification settings - Fork 39
/
GoogleBenchmarkMain.cpp
142 lines (126 loc) · 5.39 KB
/
GoogleBenchmarkMain.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
//===- GoogleBenchmarkMain.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 file implements the benchmark for mulf operation.
//
//===----------------------------------------------------------------------===//
#include <benchmark/benchmark.h>
#include <buddy/Core/Container.h>
#include <iostream>
#include <random>
// Define target layout.
#define INPUT_N 1
#define INPUT_H 32
#define INPUT_W 32
#define INPUT_C 16
#define OUTPUT_N 1
#define OUTPUT_H 32
#define OUTPUT_W 32
#define OUTPUT_C 16
// Helper functions and variables.
namespace {
const std::string PASS = "\033[32mPASS\033[0m";
const std::string FAIL = "\033[31mFAIL\033[0m";
bool areArraysEqual(float array1[], float array2[], int size) {
for (int i = 0; i < size; ++i) {
if (array1[i] != array2[i]) {
return false;
}
}
return true;
}
} // namespace
namespace {
// Declare the math.mulf C interface.
extern "C" {
void _mlir_ciface_mulf_scalar(MemRef<float, 4> *input1, MemRef<float, 4> *input2, MemRef<float, 4> *output);
void _mlir_ciface_mulf_auto_vectorization(MemRef<float, 4> *input1, MemRef<float, 4> *input2, MemRef<float, 4> *output);
}
#define DEFINE_MULF_BENCHMARK(name, func) \
void BM_MULF_##name(benchmark::State &state) { \
intptr_t sizesInput[4] = {INPUT_N, INPUT_H, INPUT_W, INPUT_C}; \
intptr_t sizesOutput[4] = {OUTPUT_N, OUTPUT_H, OUTPUT_W, OUTPUT_C}; \
\
MemRef<float, 4> input1(sizesInput, 1.0); \
MemRef<float, 4> input2(sizesInput, 1.0); \
MemRef<float, 4> output(sizesOutput, 0.0); \
\
for (auto _ : state) { \
func(&input1, &input2, &output); \
} \
}
DEFINE_MULF_BENCHMARK(SCALAR, _mlir_ciface_mulf_scalar)
DEFINE_MULF_BENCHMARK(AutoVectorization, _mlir_ciface_mulf_auto_vectorization)
} // namespace
// Register benchmark cases.
BENCHMARK(BM_MULF_SCALAR)->Unit(benchmark::kMillisecond);
BENCHMARK(BM_MULF_AutoVectorization)->Unit(benchmark::kMillisecond);
/// Correctness Verification
/// The verification does not affect the performance.
/// - Set the scalar case as the criteria.
/// - Input elements are random numbers.
/// - Output elements are initialized to zero.
/// - Compare the output of various optimizations with the scalar version to
/// verify correctness.
void verification() {
// Set the random number generator.
std::random_device rd;
std::mt19937 generator(rd());
std::uniform_real_distribution<float> distribution(0.0, 1.0);
// Set the layout sizes of input and output memref container.
intptr_t sizesInput[4] = {INPUT_N, INPUT_H, INPUT_W, INPUT_C};
intptr_t sizesOutput[4] = {OUTPUT_N, OUTPUT_H, OUTPUT_W, OUTPUT_C};
// Generate input memref containers with random numbers.
const int inputSize = INPUT_N * INPUT_H * INPUT_W * INPUT_C;
float inputRand1[inputSize];
float inputRand2[inputSize];
for (int i = 0; i < inputSize; ++i) {
inputRand1[i] = distribution(generator);
inputRand2[i] = distribution(generator);
}
MemRef<float, 4> inputMemRef1(inputRand1, sizesInput);
MemRef<float, 4> inputMemRef2(inputRand2, sizesInput);
// Generate output memref containers with zero.
const int outputSize = OUTPUT_N * OUTPUT_H * OUTPUT_W * OUTPUT_C;
MemRef<float, 4> outputScalar(sizesOutput, 0.0);
MemRef<float, 4> outputAutoVectorization(sizesOutput, 0.0);
// Perform all the mulf implementations.
_mlir_ciface_mulf_scalar(&inputMemRef1, &inputMemRef2, &outputScalar);
_mlir_ciface_mulf_auto_vectorization(&inputMemRef1, &inputMemRef2, &outputAutoVectorization);
// Get the result array.
auto resultScalar = outputScalar.getData();
auto resultAutoVectorization = outputAutoVectorization.getData();
// Print the verification result.
std::cout << "-----------------------------------------------------------"
<< std::endl;
std::cout << "Correctness Verification:" << std::endl;
std::cout << "Transform case: "
<< (areArraysEqual(resultScalar, resultAutoVectorization,
outputSize)
? PASS
: FAIL)
<< std::endl;
std::cout << "-----------------------------------------------------------"
<< std::endl;
}
int main(int argc, char **argv) {
// Run benchmark.
::benchmark::Initialize(&argc, argv);
::benchmark::RunSpecifiedBenchmarks();
// Run correctness verification.
verification();
return 0;
}