Skip to content
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

runtime dispatching #1800

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#include <ydb/library/yql/utils/simd/exec/runtime_dispatching/algo.h>
#include <immintrin.h>
#include <avxintrin.h>

template<>
THolder<Perfomancer::Interface> Perfomancer::Create<AVX2Trait>() {
Cerr << "AVXTrait";
return MakeHolder<Algo<AVX2Trait>>();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
LIBRARY()

OWNER(g:yql)

CFLAGS(-mavx2)

SRCS(avx2_algo.cpp)

END()
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#include <ydb/library/yql/utils/simd/exec/runtime_dispatching/algo.h>

template<>
THolder<Perfomancer::Interface> Perfomancer::Create<FallbackTrait>() {
Cerr << "FallbackTrait";
return MakeHolder<Algo<FallbackTrait>>();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
LIBRARY()

OWNER(g:yql)

SRCS(fallback_algo.cpp)

END()
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#include <ydb/library/yql/utils/simd/exec/runtime_dispatching/algo.h>
#include <immintrin.h>

template<>
THolder<Perfomancer::Interface> Perfomancer::Create<SSE42Trait>() {
Cerr << "SSETrait";
return MakeHolder<Algo<SSE42Trait>>();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
LIBRARY()

OWNER(g:yql)

CFLAGS(-msse4.2)

SRCS(sse42_algo.cpp)

END()
92 changes: 92 additions & 0 deletions ydb/library/yql/utils/simd/exec/runtime_dispatching/algo.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
#include "util/system/cpu_id.h"
#include <ydb/library/yql/utils/simd/simd_avx2.h>
#include <ydb/library/yql/utils/simd/simd_fallback.h>
#include <ydb/library/yql/utils/simd/simd_sse42.h>
#include <vector>

using vl = std::vector<ui64>;
using vvl = std::vector<std::vector<ui64>>;

template <int RegisterSize, template<typename> typename Intrinsic>
struct TTrait {
static constexpr int Size = RegisterSize;
};

using AVX2Trait = NSimd::NAVX2::TSimd8<ui64>;

using SSE42Trait = NSimd::NSSE42::TSimd8<ui64>;

using FallbackTrait = NSimd::NFallback::FallbackTrait<ui64>;

struct Perfomancer {

Perfomancer() = default;

struct Interface {

virtual ~Interface() = default;

inline virtual void Add(vvl& columns, vl& result) {

// to avoid clang(-Wunused-parameter)
columns[0];
result[0];
}

};


template <typename Trait>
struct Algo : Interface {

Algo() {}

inline void Add(vvl& columns, vl& result) override {
std::vector<Trait> Registers(columns.size());

for (size_t j = 0; j < result.size(); j += Trait::Size) {

for (size_t i = 0; i < columns.size(); ++i) {
Registers[i] = Trait(&columns[i][j]);
}

for (size_t i = 1; i < columns.size(); ++i) {
Registers[i] += Registers[i - 1];
}

Registers.back().Store(&result[j]);
}
}

~Algo() = default;
};

template <typename Trait>
inline THolder<Interface> Create() {
return MakeHolder<Interface>();
}

};

template<>
THolder<Perfomancer::Interface> Perfomancer::Create<AVX2Trait>();

template<>
THolder<Perfomancer::Interface> Perfomancer::Create<SSE42Trait>();

template<>
THolder<Perfomancer::Interface> Perfomancer::Create<FallbackTrait>();

template <typename TFactory>
auto ChooseTrait(TFactory& factory) {

if (NX86::HaveAVX2()) {
return factory.template Create<AVX2Trait>();

} else if (NX86::HaveSSE42()) {
return factory.template Create<SSE42Trait>();

}

return factory.template Create<FallbackTrait>();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#include <ydb/library/yql/utils/simd/exec/runtime_dispatching/algo.h>

int main() {

std::vector<std::vector<ui64>> columns(10, std::vector<ui64>(64e6, 1e8));
std::vector<ui64> result(64e6);

Perfomancer perfomancer;

auto worker = ChooseTrait(perfomancer);

worker->Add(columns, result);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
OWNER(g:yql)

PROGRAM()

SRCS(main.cpp)

PEERDIR(ydb/library/yql/utils/simd/exec/runtime_dispatching)

END()
19 changes: 19 additions & 0 deletions ydb/library/yql/utils/simd/exec/runtime_dispatching/ya.make
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
OWNER(g:yql)

LIBRARY()

PEERDIR(
ydb/library/yql/utils/simd
ydb/library/yql/utils/simd/exec/runtime_dispatching/AVX2_algo
ydb/library/yql/utils/simd/exec/runtime_dispatching/SSE42_algo
ydb/library/yql/utils/simd/exec/runtime_dispatching/Fallback_algo
)

END()

RECURSE(
AVX2_algo
SSE42_algo
Fallback_algo
main
)
Loading