Added a "diff" SIMD helper, and some more benchmarking...

This commit is contained in:
paulfd 2019-09-15 21:14:02 +02:00
parent a114b1e144
commit 9afa1918a0
8 changed files with 268 additions and 1 deletions

87
benchmarks/BM_diff.cpp Normal file
View file

@ -0,0 +1,87 @@
// Copyright (c) 2019, Paul Ferrand
// All rights reserved.
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
// 1. Redistributions of source code must retain the above copyright notice, this
// list of conditions and the following disclaimer.
// 2. Redistributions in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
// ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
// ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include <benchmark/benchmark.h>
#include <random>
#include <numeric>
#include <vector>
#include <cmath>
#include <iostream>
#include "../sfizz/SIMDHelpers.h"
#include "absl/types/span.h"
class DiffArray : public benchmark::Fixture {
public:
void SetUp(const ::benchmark::State& state) {
std::random_device rd { };
std::mt19937 gen { rd() };
std::uniform_real_distribution<float> dist { 0.1, 1 };
input = std::vector<float>(state.range(0));
output = std::vector<float>(state.range(0));
std::generate(input.begin(), input.end(), [&]() { return dist(gen); });
cumsum<float, false>(input, absl::MakeSpan(input));
}
void TearDown(const ::benchmark::State& state [[maybe_unused]]) {
}
std::vector<float> input;
std::vector<float> output;
};
BENCHMARK_DEFINE_F(DiffArray, Diff_Scalar)(benchmark::State& state) {
for (auto _ : state)
{
diff<float, false>(input, absl::MakeSpan(output));
}
}
BENCHMARK_DEFINE_F(DiffArray, Diff_SIMD)(benchmark::State& state) {
for (auto _ : state)
{
diff<float, true>(input, absl::MakeSpan(output));
}
}
BENCHMARK_DEFINE_F(DiffArray, Diff_Scalar_Unaligned)(benchmark::State& state) {
for (auto _ : state)
{
diff<float, false>(absl::MakeSpan(input).subspan(1), absl::MakeSpan(output).subspan(1));
}
}
BENCHMARK_DEFINE_F(DiffArray, Diff_SIMD_Unaligned)(benchmark::State& state) {
for (auto _ : state)
{
diff<float, true>(absl::MakeSpan(input).subspan(1), absl::MakeSpan(output).subspan(1));
}
}
BENCHMARK_REGISTER_F(DiffArray, Diff_Scalar)->RangeMultiplier(4)->Range(1 << 2, 1 << 12);
BENCHMARK_REGISTER_F(DiffArray, Diff_SIMD)->RangeMultiplier(4)->Range(1 << 2, 1 << 12);
BENCHMARK_REGISTER_F(DiffArray, Diff_Scalar_Unaligned)->RangeMultiplier(4)->Range(1 << 2, 1 << 12);
BENCHMARK_REGISTER_F(DiffArray, Diff_SIMD_Unaligned)->RangeMultiplier(4)->Range(1 << 2, 1 << 12);
BENCHMARK_MAIN();

View file

@ -0,0 +1,86 @@
// Copyright (c) 2019, Paul Ferrand
// All rights reserved.
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
// 1. Redistributions of source code must retain the above copyright notice, this
// list of conditions and the following disclaimer.
// 2. Redistributions in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
// ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
// ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include <benchmark/benchmark.h>
#include <random>
#include <absl/algorithm/container.h>
#include "../sfizz/SIMDHelpers.h"
#include "absl/types/span.h"
inline constexpr int bigNumber { 2399132 };
class IterOffset : public benchmark::Fixture {
public:
void SetUp(const ::benchmark::State& state [[maybe_unused]]) {
std::random_device rd { };
std::mt19937 gen { rd() };
std::uniform_real_distribution<float> dist { 0.001, 1 };
std::uniform_int_distribution<int> jumpDist { 0, 3 };
source = std::vector<float>(bigNumber);
result.resize(state.range(0));
absl::c_generate(source, [&]() { return dist(gen); });
jumps.resize(state.range(0));
offsets.resize(state.range(0));
absl::c_generate(jumps, [&]() { return jumpDist(gen); });
cumsum<int>(jumps, absl::MakeSpan(offsets));
}
void TearDown(const ::benchmark::State& state [[maybe_unused]]) {
}
std::vector<float> source;
std::vector<float> result;
std::vector<int> offsets;
std::vector<int> jumps;
};
BENCHMARK_DEFINE_F(IterOffset, Pointers)(benchmark::State& state) {
for (auto _ : state)
{
diff<int>(offsets, absl::MakeSpan(jumps));
auto jump = jumps.begin();
auto in = source.begin();
auto out = result.begin();
while (out < result.end() && in < source.end()) {
*out++ = *in;
in += *jump++;
}
}
}
BENCHMARK_DEFINE_F(IterOffset, Offsets)(benchmark::State& state) {
for (auto _ : state)
{
auto offset = offsets.begin();
auto out = result.begin();
while (offset < offsets.end())
*out++ = source[*offset++];
}
}
// Register the function as a benchmark
BENCHMARK_REGISTER_F(IterOffset, Pointers)->RangeMultiplier(4)->Range((1 << 2), (1 << 12));
BENCHMARK_REGISTER_F(IterOffset, Offsets)->RangeMultiplier(4)->Range((1 << 2), (1 << 12));
BENCHMARK_MAIN();

View file

@ -78,18 +78,26 @@ target_link_libraries(bm_meanSquared benchmark absl::span absl::algorithm)
add_executable(bm_cumsum BM_cumsum.cpp ${SFIZZ_SIMD_SOURCES})
target_link_libraries(bm_cumsum benchmark absl::span absl::algorithm)
add_executable(bm_diff BM_diff.cpp ${SFIZZ_SIMD_SOURCES})
target_link_libraries(bm_diff benchmark absl::span absl::algorithm)
add_executable(bm_interpolationCast BM_interpolationCast.cpp ${SFIZZ_SIMD_SOURCES})
target_link_libraries(bm_interpolationCast benchmark absl::span absl::algorithm)
add_executable(bm_pointerIterationOrOffsets BM_pointerIterationOrOffsets.cpp ${SFIZZ_SIMD_SOURCES})
target_link_libraries(bm_pointerIterationOrOffsets benchmark absl::span absl::algorithm)
add_custom_target(sfizz_benchmarks)
add_dependencies(sfizz_benchmarks
bm_opf_high_vs_low
bm_write
bm_pointerIterationOrOffsets
bm_read
bm_mean
bm_meanSquared
bm_fill
bm_cumsum
bm_diff
bm_interpolationCast
bm_mathfuns
bm_gain

View file

@ -62,6 +62,7 @@ namespace SIMDConfig {
constexpr bool copy { false };
constexpr bool pan { true };
constexpr bool cumsum { true };
constexpr bool diff { false };
constexpr bool sfzInterpolationCast { true };
constexpr bool mean { false };
constexpr bool meanSquared { false };

View file

@ -160,4 +160,10 @@ template<>
void sfzInterpolationCast<float, true>(absl::Span<const float> floatJumps, absl::Span<int> jumps, absl::Span<float> leftCoeffs, absl::Span<float> rightCoeffs) noexcept
{
sfzInterpolationCast<float, false>(floatJumps, jumps, leftCoeffs, rightCoeffs);
}
template <>
void diff<float, true>(absl::Span<const float> input, absl::Span<float> output) noexcept
{
diff<float, false>(input, output);
}

View file

@ -525,4 +525,31 @@ void sfzInterpolationCast(absl::Span<const T> floatJumps, absl::Span<int> jumps,
}
template<>
void sfzInterpolationCast<float, true>(absl::Span<const float> floatJumps, absl::Span<int> jumps, absl::Span<float> leftCoeffs, absl::Span<float> rightCoeffs) noexcept;
void sfzInterpolationCast<float, true>(absl::Span<const float> floatJumps, absl::Span<int> jumps, absl::Span<float> leftCoeffs, absl::Span<float> rightCoeffs) noexcept;
template <class T>
inline void snippetDiff(const T*& input, T*& output)
{
*output = *input - *(input - 1);
output++;
input++;
}
template <class T, bool SIMD = SIMDConfig::diff>
void diff(absl::Span<const T> input, absl::Span<T> output) noexcept
{
ASSERT(output.size() >= input.size());
if (input.size() == 0)
return;
auto out = output.data();
auto in = input.data();
const auto sentinel = in + std::min(input.size(), output.size());
*out++ = *in++;
while (in < sentinel)
snippetDiff(in, out);
}
template <>
void cumsum<float, true>(absl::Span<const float> input, absl::Span<float> output) noexcept;

View file

@ -757,4 +757,36 @@ void sfzInterpolationCast<float, true>(absl::Span<const float> floatJumps, absl:
while(floatJump < sentinel)
snippetSFZInterpolationCast(floatJump, jump, leftCoeff, rightCoeff);
}
template <>
void diff<float, true>(absl::Span<const float> input, absl::Span<float> output) noexcept
{
ASSERT(output.size() >= input.size());
if (input.size() == 0)
return;
auto out = output.data();
auto in = input.data();
const auto sentinel = in + std::min(input.size(), output.size());
const auto lastAligned = prevAligned(sentinel);
*out++ = *in++;
while (unaligned(in, out) && in < lastAligned)
snippetDiff(in, out);
auto mmBase = _mm_set_ps1(*(out - 1));
while (in < lastAligned) {
auto mmOutput = _mm_load_ps(in);
auto mmNextBase = _mm_shuffle_ps(mmOutput, mmOutput, _MM_SHUFFLE(3, 3, 3, 3));
mmOutput = _mm_sub_ps(mmOutput, mmBase);
mmBase = mmNextBase;
mmOutput = _mm_sub_ps(mmOutput, _mm_castsi128_ps(_mm_slli_si128(_mm_castps_si128(mmOutput), 4)));
_mm_store_ps(out, mmOutput);
in += TypeAlignment;
out += TypeAlignment;
}
while (in < sentinel)
snippetDiff(in, out);
}

View file

@ -731,4 +731,24 @@ TEST_CASE("[Helpers] Cumulative sum (SIMD vs Scalar)")
cumsum<float, false>(input, absl::MakeSpan(outputScalar));
cumsum<float, true>(input, absl::MakeSpan(outputSIMD));
REQUIRE(approxEqual<float>(outputScalar, outputSIMD));
}
TEST_CASE("[Helpers] Diff ")
{
std::array<float, 6> input { 1.1f, 2.3f, 3.6f, 5.0f, 6.5f, 8.1f };
std::array<float, 6> output;
std::array<float, 6> expected { 1.1f, 1.2f, 1.3f, 1.4f, 1.5f, 1.6f };
diff<float, false>(input, absl::MakeSpan(output));
REQUIRE(approxEqual<float>(output, expected));
}
TEST_CASE("[Helpers] Diff (SIMD vs Scalar)")
{
std::vector<float> input(bigBufferSize);
std::vector<float> outputScalar(bigBufferSize);
std::vector<float> outputSIMD(bigBufferSize);
linearRamp<float>(absl::MakeSpan(input), 0.0, 0.1);
diff<float, false>(input, absl::MakeSpan(outputScalar));
diff<float, true>(input, absl::MakeSpan(outputSIMD));
REQUIRE(approxEqual<float>(outputScalar, outputSIMD));
}