-
Notifications
You must be signed in to change notification settings - Fork 350
[MicroBenchmarks] Add matrix type benchmarks. #265
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
fhahn
wants to merge
1
commit into
llvm:main
Choose a base branch
from
fhahn:matrix-microbench
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
add_subdirectory(Int128) | ||
add_subdirectory(MatrixType) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
# Enable matrix types benchmarks for compilers supporting -fenable-matrix. | ||
check_c_compiler_flag(-fenable-matrix COMPILER_HAS_MATRIX_FLAG) | ||
if (COMPILER_HAS_MATRIX_FLAG) | ||
set(CMAKE_CXX_STANDARD 20) | ||
set(CMAKE_CXX_STANDARD_REQUIRED TRUE) | ||
|
||
llvm_test_run() | ||
|
||
set_property(SOURCE main.cpp PROPERTY COMPILE_FLAGS -fenable-matrix) | ||
|
||
llvm_test_executable(MatrixType main.cpp) | ||
target_link_libraries(MatrixType benchmark) | ||
endif() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,149 @@ | ||||||
#include <algorithm> | ||||||
#include <cstdint> | ||||||
#include <limits> | ||||||
#include <random> | ||||||
#include <ranges> | ||||||
#include <vector> | ||||||
|
||||||
#if __has_include(<simd/simd.h>) | ||||||
#define HAS_SIMD_HEADER 1 | ||||||
#include <simd/simd.h> | ||||||
#else | ||||||
#define HAS_SIMD_HEADER 0 | ||||||
#endif | ||||||
|
||||||
#include "benchmark/benchmark.h" | ||||||
#include <iostream> | ||||||
|
||||||
namespace { | ||||||
|
||||||
using m44 = double __attribute__((matrix_type(4, 4))); | ||||||
|
||||||
class MatrixMult4x4Benchmark : public benchmark::Fixture { | ||||||
public: | ||||||
void SetUp(const benchmark::State &) override { | ||||||
std::default_random_engine generator; | ||||||
std::uniform_real_distribution<double> distribution(-10.0, 10.0); | ||||||
|
||||||
mats.clear(); | ||||||
mats_res.clear(); | ||||||
for (unsigned X = 0; X < kDataSize; ++X) { | ||||||
m44 M; | ||||||
for (unsigned J = 0; J < 4; ++J) | ||||||
for (unsigned I = 0; I < 4; ++I) | ||||||
M[J][I] = distribution(generator); | ||||||
mats.push_back(M); | ||||||
mats_res.push_back(M); | ||||||
} | ||||||
|
||||||
#ifdef HAS_SIMD_HEADER | ||||||
mats_simd_res.clear(); | ||||||
mats_simd.clear(); | ||||||
for (auto &m : mats) { | ||||||
simd_double4x4 s; | ||||||
s.columns[0] = {m[0][0], m[1][0], m[2][0], m[3][0]}; | ||||||
s.columns[1] = {m[0][1], m[1][1], m[2][1], m[3][1]}; | ||||||
s.columns[2] = {m[0][2], m[1][2], m[2][2], m[3][2]}; | ||||||
s.columns[3] = {m[0][3], m[1][3], m[2][3], m[3][3]}; | ||||||
mats_simd.push_back(s); | ||||||
mats_simd_res.push_back(s); | ||||||
} | ||||||
#endif | ||||||
} | ||||||
|
||||||
protected: | ||||||
static constexpr size_t kDataSize = 1024; | ||||||
std::vector<m44> mats; | ||||||
std::vector<m44> mats_res; | ||||||
#ifdef HAS_SIMD_HEADER | ||||||
std::vector<simd_double4x4> mats_simd; | ||||||
std::vector<simd_double4x4> mats_simd_res; | ||||||
#endif | ||||||
}; | ||||||
|
||||||
BENCHMARK_F(MatrixMult4x4Benchmark, MatrixTypeAB)(benchmark::State &state) { | ||||||
while (state.KeepRunning()) { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
https://google.github.io/benchmark/user_guide.html#a-faster-keep-running-loop |
||||||
size_t N = mats.size(); | ||||||
for (size_t i = 0u; i < N; ++i) { | ||||||
const m44 a = mats[i]; | ||||||
const m44 b = mats[(i + 1) % N]; | ||||||
const m44 prod = a * b; | ||||||
mats_res[i] = prod; | ||||||
} | ||||||
benchmark::ClobberMemory(); | ||||||
} | ||||||
} | ||||||
|
||||||
#ifdef HAS_SIMD_HEADER | ||||||
BENCHMARK_F(MatrixMult4x4Benchmark, SIMDMatrixAB)(benchmark::State &state) { | ||||||
while (state.KeepRunning()) { | ||||||
size_t N = mats.size(); | ||||||
for (size_t i = 0u; i < N; ++i) { | ||||||
const simd_double4x4 a = mats_simd[i]; | ||||||
const simd_double4x4 b = mats_simd[(i + 1) % N]; | ||||||
const simd_double4x4 prod = matrix_multiply(a, b); | ||||||
mats_simd_res[i] = prod; | ||||||
} | ||||||
benchmark::ClobberMemory(); | ||||||
} | ||||||
} | ||||||
#endif | ||||||
|
||||||
BENCHMARK_F(MatrixMult4x4Benchmark, MatrixTypeAtB)(benchmark::State &state) { | ||||||
while (state.KeepRunning()) { | ||||||
size_t N = mats.size(); | ||||||
for (size_t i = 0u; i < N; ++i) { | ||||||
const m44 a = mats[i]; | ||||||
const m44 b = mats[(i + 1) % N]; | ||||||
const m44 prod = __builtin_matrix_transpose(a) * b; | ||||||
benchmark::DoNotOptimize(prod); | ||||||
} | ||||||
} | ||||||
} | ||||||
|
||||||
#ifdef HAS_SIMD_HEADER | ||||||
BENCHMARK_F(MatrixMult4x4Benchmark, SIMDMatrixAtB)(benchmark::State &state) { | ||||||
while (state.KeepRunning()) { | ||||||
size_t N = mats.size(); | ||||||
for (size_t i = 0u; i < N; ++i) { | ||||||
const simd_double4x4 a = mats_simd[i]; | ||||||
const simd_double4x4 b = mats_simd[(i + 1) % N]; | ||||||
const simd_double4x4 prod = matrix_multiply(simd_transpose(a), b); | ||||||
benchmark::DoNotOptimize(prod); | ||||||
} | ||||||
} | ||||||
} | ||||||
#endif | ||||||
|
||||||
BENCHMARK_F(MatrixMult4x4Benchmark, | ||||||
MatrixTypeAtBStoreRes)(benchmark::State &state) { | ||||||
while (state.KeepRunning()) { | ||||||
size_t N = mats.size(); | ||||||
for (size_t i = 0u; i < N; ++i) { | ||||||
const m44 a = mats[i]; | ||||||
const m44 b = mats[(i + 1) % N]; | ||||||
const m44 prod = __builtin_matrix_transpose(a) * b; | ||||||
mats[i] = prod; | ||||||
} | ||||||
benchmark::ClobberMemory(); | ||||||
} | ||||||
} | ||||||
|
||||||
#ifdef HAS_SIMD_HEADER | ||||||
BENCHMARK_F(MatrixMult4x4Benchmark, | ||||||
SIMDMatrixAtBStoreRes)(benchmark::State &state) { | ||||||
while (state.KeepRunning()) { | ||||||
size_t N = mats.size(); | ||||||
for (size_t i = 0u; i < N; ++i) { | ||||||
const simd_double4x4 a = mats_simd[i]; | ||||||
const simd_double4x4 b = mats_simd[(i + 1) % N]; | ||||||
const simd_double4x4 prod = matrix_multiply(simd_transpose(a), b); | ||||||
mats_simd[i] = prod; | ||||||
} | ||||||
benchmark::ClobberMemory(); | ||||||
} | ||||||
} | ||||||
#endif | ||||||
} // namespace | ||||||
|
||||||
BENCHMARK_MAIN(); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is this used?