Skip to content

Commit

Permalink
Simd(avx512) tests and binary_search tests (frozenca#9)
Browse files Browse the repository at this point in the history
* prepare simd improvements

* fix compile options for release build
add cstdint into details.h

* use ptrdiff_t instead of in for pointer subst

* add intel sde for avx512f testing

* fix github action

* apt needs sudo

* add clang build

* fix indentation

* use clang-15 for ubuntu

* try to fix clang usage

* try to use libc++ for clang

* we cant use clang on ubuntu without gcc

* ok revert libc++ usage

* add macos

* syntax error

* use macos-13

* try to use macos-14

* revert macos to 13 and wrap header memory_resource

* wrap std::pmr and std::experimental::pmr

* fix error from clang-14

* add experimental link option for clang less or equal 14

---------

Co-authored-by: Alexander B <[email protected]>
  • Loading branch information
bas524 and Alexander B authored Jan 15, 2024
1 parent c594ca6 commit 82b4dd9
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 28 deletions.
30 changes: 30 additions & 0 deletions .github/workflows/action-cpp.yml
Original file line number Diff line number Diff line change
Expand Up @@ -76,3 +76,33 @@ jobs:
with:
name: ${{ format('BTree-{0}.{1}', steps.get-version.outputs.prj_ver, steps.container.outputs.value) }}
path: cmake-build-release/${{ format('BTree-{0}-noarch.???', steps.get-version.outputs.prj_ver) }}

avx512f-gcc-cmake:
runs-on: ubuntu-22.04
steps:
- uses: actions/checkout@v3
- run: sudo apt-get update -y && sudo apt-get install -yq binutils git make cmake catch2 gcc g++ lsb-release
- name: mkdir
run: mkdir cmake-build-release
- name: cmake cmake-build-release
run: cmake -DFORCE_USE_SIMD=ON -DCMAKE_BUILD_TYPE=Release -Bcmake-build-release -H.
- name: cmake make
run: cmake --build cmake-build-release/ --target all --parallel
- name: sde-test
uses: petarpetrovt/[email protected]
- name: test
run: cp cmake-build-release/fc_tests ${SDE_PATH} && cd ${SDE_PATH} && ./sde64 -knl -- ./fc_tests --benchmark-samples=1

macos-clang-cmake:
runs-on: macos-13
steps:
- uses: actions/checkout@v3
- run: brew install git ninja cmake catch2
- name: mkdir
run: mkdir cmake-build-release
- name: cmake cmake-build-release
run: cmake -DFORCE_PREFER_BINARY_SEARCH=ON -DCMAKE_BUILD_TYPE=Release -Bcmake-build-release -H.
- name: cmake make
run: cmake --build cmake-build-release/ --target all --parallel
- name: test
run: cd cmake-build-release && ctest && cd ..
22 changes: 18 additions & 4 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ option(FORCE_USE_SIMD "force define FC_USE_SIMD (apllicable only for x86_64)" OF
option(FORCE_PREFER_BINARY_SEARCH "force define FC_PREFER_BINARY_SEARCH (recommended for clang only)" OFF)

set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

find_package(VersionHeader)
set(PROJECT_VERSION "${MAJOR}.${MINOR}.${PATCH}.${COMMITTER_SHORTSHA}")
Expand Down Expand Up @@ -103,11 +104,24 @@ target_link_libraries(fc_tests PRIVATE ${CATCH_LIBS_ALIASES} BTree::BTree)

if(MSVC)
target_compile_options(fc_tests PRIVATE /W4 /WX /nologo /MDd /EHsc /std:c++latest /experimental:module)
if (CMAKE_BUILD_TYPE STREQUAL "Release")
add_compile_options(/Ox)
endif()
set(CMAKE_C_FLAGS_RELEASE "/Ox")
set(CMAKE_CXX_FLAGS_RELEASE "/Ox")
set(CMAKE_CXX_FLAGS_RELWITHDEBINFO "/Ox")
else()
target_compile_options(fc_tests PRIVATE -Wall -Wextra -Wpedantic -Werror -march=native)
set(CMAKE_C_FLAGS_RELEASE "-O3")
set(CMAKE_CXX_FLAGS_RELEASE "-O3")
set(CMAKE_CXX_FLAGS_RELWITHDEBINFO "-O3")
if (FORCE_USE_SIMD)
target_compile_options(fc_tests PRIVATE -Wall -Wextra -Wpedantic -Werror -mavx512f)
else ()
target_compile_options(fc_tests PRIVATE -Wall -Wextra -Wpedantic -Werror -march=native -mtune=generic)
endif ()
if(CMAKE_CXX_COMPILER_ID STREQUAL "AppleClang" AND CMAKE_CXX_COMPILER_VERSION VERSION_LESS_EQUAL 14.0.3.14030022)
target_link_options(fc_tests PRIVATE -fexperimental-library)
endif()
if(CMAKE_CXX_COMPILER_ID STREQUAL "Clang" AND CMAKE_CXX_COMPILER_VERSION VERSION_LESS_EQUAL 14)
target_link_options(fc_tests PRIVATE -fexperimental-library)
endif()
endif()

include(CTest)
Expand Down
36 changes: 18 additions & 18 deletions include/fc/comp.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,54 +22,54 @@ using regi = __m512i;
using regf = __m512;
using regd = __m512d;

regi broadcast(std::int32_t key) { return _mm512_set1_epi32(key); }
inline regi broadcast(std::int32_t key) { return _mm512_set1_epi32(key); }

regi broadcast(std::int64_t key) { return _mm512_set1_epi64(key); }
inline regi broadcast(std::int64_t key) { return _mm512_set1_epi64(key); }

regf broadcast(float key) { return _mm512_set1_ps(key); }
inline regf broadcast(float key) { return _mm512_set1_ps(key); }

regd broadcast(double key) { return _mm512_set1_pd(key); }
inline regd broadcast(double key) { return _mm512_set1_pd(key); }

unsigned int cmp(regi key, const std::int32_t *key_ptr) {
inline unsigned int cmp(regi key, const std::int32_t *key_ptr) {
regi keys_to_comp =
_mm512_load_si512(reinterpret_cast<const regi *>(key_ptr));
return _mm512_cmpgt_epi32_mask(key, keys_to_comp);
}

unsigned int cmp(regi key, const std::int64_t *key_ptr) {
inline unsigned int cmp(regi key, const std::int64_t *key_ptr) {
regi keys_to_comp =
_mm512_load_si512(reinterpret_cast<const regi *>(key_ptr));
return _mm512_cmpgt_epi64_mask(key, keys_to_comp);
}

unsigned int cmp(regf key, const float *key_ptr) {
inline unsigned int cmp(regf key, const float *key_ptr) {
regf keys_to_comp = _mm512_load_ps(key_ptr);
return _mm512_cmp_ps_mask(key, keys_to_comp, _MM_CMPINT_GT);
}

unsigned int cmp(regd key, const double *key_ptr) {
inline unsigned int cmp(regd key, const double *key_ptr) {
regd keys_to_comp = _mm512_load_pd(key_ptr);
return _mm512_cmp_pd_mask(key, keys_to_comp, _MM_CMPINT_GT);
}

unsigned int cmp(const std::int32_t *key_ptr, regi key) {
inline unsigned int cmp(const std::int32_t *key_ptr, regi key) {
regi keys_to_comp =
_mm512_load_si512(reinterpret_cast<const regi *>(key_ptr));
return _mm512_cmpgt_epi32_mask(keys_to_comp, key);
}

unsigned int cmp(const std::int64_t *key_ptr, regi key) {
inline unsigned int cmp(const std::int64_t *key_ptr, regi key) {
regi keys_to_comp =
_mm512_load_si512(reinterpret_cast<const regi *>(key_ptr));
return _mm512_cmpgt_epi64_mask(keys_to_comp, key);
}

unsigned int cmp(const float *key_ptr, regf key) {
inline unsigned int cmp(const float *key_ptr, regf key) {
regf keys_to_comp = _mm512_load_ps(key_ptr);
return _mm512_cmp_ps_mask(keys_to_comp, key, _MM_CMPINT_GT);
}

unsigned int cmp(const double *key_ptr, regd key) {
inline unsigned int cmp(const double *key_ptr, regd key) {
regd keys_to_comp = _mm512_load_pd(key_ptr);
return _mm512_cmp_pd_mask(keys_to_comp, key, _MM_CMPINT_GT);
}
Expand All @@ -81,12 +81,12 @@ template <CanUseSimd K> struct SimdTrait {
};

template <CanUseSimd K, bool less>
inline std::int32_t get_lb_simd(K key, const K *first, const K *last) {
const auto len = static_cast<std::int32_t>(last - first);
inline std::ptrdiff_t get_lb_simd(K key, const K *first, const K *last) {
const auto len = static_cast<std::ptrdiff_t>(last - first);
const K *curr = first;
auto key_broadcast = broadcast(key);
int mask = 0;
int offset = 0;
ptrdiff_t offset = 0;
while (offset < len) {
if constexpr (less) {
mask = ~cmp(key_broadcast, curr);
Expand All @@ -109,12 +109,12 @@ inline std::int32_t get_lb_simd(K key, const K *first, const K *last) {
}

template <CanUseSimd K, bool less>
inline std::int32_t get_ub_simd(K key, const K *first, const K *last) {
const auto len = static_cast<std::int32_t>(last - first);
inline std::ptrdiff_t get_ub_simd(K key, const K *first, const K *last) {
const auto len = static_cast<std::ptrdiff_t>(last - first);
const K *curr = first;
auto key_broadcast = broadcast(key);
int mask = 0;
int offset = 0;
ptrdiff_t offset = 0;
while (offset < len) {
if constexpr (less) {
mask = cmp(curr, key_broadcast);
Expand Down
1 change: 1 addition & 0 deletions include/fc/details.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#define FC_DETAILS_H

#include <concepts>
#include <cstdint>

namespace frozenca {

Expand Down
2 changes: 1 addition & 1 deletion include/fc/disk_btree.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class DiskBTreeBase
: public BTreeBase<K, V, Fanout, Comp, AllowDup, AllocatorFixed> {
public:
using Base = BTreeBase<K, V, Fanout, Comp, AllowDup, AllocatorFixed>;
using Node = Base::node_type;
using Node = typename Base::node_type;

private:
MemoryMappedFile mm_file_;
Expand Down
16 changes: 11 additions & 5 deletions include/fc/disk_fixed_alloc.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,20 @@
#include <concepts>
#include <cstdint>
#include <iostream>
#if defined(__clang__) && __clang_major__ < 15
#include <experimental/memory_resource>
namespace stdpmr = std::experimental::pmr;
#elif defined(__clang__) || (__GNUC__)
#include <memory_resource>
namespace stdpmr = std::pmr;
#endif
#include <stdexcept>
#include <type_traits>

namespace frozenca {

template <typename T>
class MemoryResourceFixed : public std::pmr::memory_resource {
class MemoryResourceFixed : public stdpmr::memory_resource {
T *pool_ = nullptr;
std::size_t pool_size_ = 0;
T *free_ = nullptr;
Expand Down Expand Up @@ -72,7 +78,7 @@ class MemoryResourceFixed : public std::pmr::memory_resource {
}

[[nodiscard]] bool
do_is_equal(const std::pmr::memory_resource &other) const noexcept override {
do_is_equal(const stdpmr::memory_resource &other) const noexcept override {
if (this == &other) {
return true;
}
Expand All @@ -83,7 +89,7 @@ class MemoryResourceFixed : public std::pmr::memory_resource {
};

template <typename T> class AllocatorFixed {
std::pmr::memory_resource *mem_res_;
stdpmr::memory_resource *mem_res_;

public:
template <typename Other> struct rebind {
Expand All @@ -93,7 +99,7 @@ template <typename T> class AllocatorFixed {
using value_type = T;

explicit AllocatorFixed(
std::pmr::memory_resource *mem_res = std::pmr::get_default_resource())
stdpmr::memory_resource *mem_res = stdpmr::get_default_resource())
: mem_res_{mem_res} {}

template <typename Other>
Expand All @@ -110,7 +116,7 @@ template <typename T> class AllocatorFixed {
std::alignment_of_v<T>);
}

[[nodiscard]] std::pmr::memory_resource *
[[nodiscard]] stdpmr::memory_resource *
get_memory_resource() const noexcept {
return mem_res_;
}
Expand Down

0 comments on commit 82b4dd9

Please sign in to comment.