-
Notifications
You must be signed in to change notification settings - Fork 298
Add tests for CPU features #1287
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
AntoinePrv
wants to merge
7
commits into
xtensor-stack:master
Choose a base branch
from
AntoinePrv:cpu-test
base: master
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
Show all changes
7 commits
Select commit
Hold shift + click to select a range
1931ab5
Add cpu_feature test
AntoinePrv 6fcd996
Add run CPU tests in CI
AntoinePrv 761d971
Test manufacturer id
AntoinePrv 1de854c
Improve error message for manufacturer test
AntoinePrv 9d46c28
Map avx512 detection to CPU
AntoinePrv dc14c6e
Code improvements
AntoinePrv 53f2149
Check optmask for avx512
AntoinePrv 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
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
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
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
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
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
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,156 @@ | ||
| /*************************************************************************** | ||
| * Copyright (c) Johan Mabille, Sylvain Corlay, Wolf Vollprecht and * | ||
| * Martin Renou * | ||
| * Copyright (c) QuantStack * | ||
| * Copyright (c) Serge Guelton * | ||
| * * | ||
| * Distributed under the terms of the BSD 3-Clause License. * | ||
| * * | ||
| * The full license is in the file LICENSE, distributed with this software. * | ||
| ****************************************************************************/ | ||
|
|
||
| #include <algorithm> | ||
| #include <array> | ||
| #include <cstdlib> | ||
| #include <string> | ||
|
|
||
| #include <doctest/doctest.h> | ||
|
|
||
| #include "xsimd/xsimd.hpp" | ||
|
|
||
| #define CHECK_IMPLICATION(a, b) CHECK_UNARY(!(a) || (b)) | ||
AntoinePrv marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| namespace detail | ||
| { | ||
| void check_env_flag(const char* env_var, const char* feature_name, bool actual) | ||
| { | ||
| if (const char* val = std::getenv(env_var)) | ||
| { | ||
| // Doctest struggles with string literals and const char * | ||
| // TODO(c++20): use std::format | ||
| auto msg = std::string(env_var) + " = " + val + ", " + feature_name + " = " + (actual ? "true" : "false"); | ||
| INFO(msg); | ||
| CHECK_EQ(actual, val[0] == '1'); | ||
| } | ||
| } | ||
|
|
||
| // TODO(c++23): use str.contains | ||
| bool contains(const std::string& haystack, const char* needle) | ||
| { | ||
| return haystack.find(needle) != std::string::npos; | ||
| } | ||
| } | ||
|
|
||
| #define CHECK_ENV_FEATURE(env_var, feature) detail::check_env_flag(env_var, #feature, feature) | ||
|
|
||
| /** | ||
| * Tests that x86_cpu_features respects the architectural implication chains. | ||
| * | ||
| * These are "always true" assertions: if a higher feature is reported, all | ||
| * features it architecturally implies must also be reported. The test reads | ||
| * the current CPU's features at runtime and verifies every implication. | ||
| */ | ||
| TEST_CASE("[cpu_features] x86 implication chains") | ||
| { | ||
| xsimd::x86_cpu_features cpu; | ||
|
|
||
| // SSE implication chain | ||
| CHECK_IMPLICATION(cpu.sse4_2(), cpu.sse4_1()); | ||
serge-sans-paille marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| CHECK_IMPLICATION(cpu.sse4_1(), cpu.ssse3()); | ||
| CHECK_IMPLICATION(cpu.ssse3(), cpu.sse3()); | ||
| CHECK_IMPLICATION(cpu.sse3(), cpu.sse2()); | ||
|
|
||
| // AVX implication chain | ||
| CHECK_IMPLICATION(cpu.avx(), cpu.sse4_2()); | ||
| CHECK_IMPLICATION(cpu.avx2(), cpu.avx()); | ||
| CHECK_IMPLICATION(cpu.fma4(), cpu.avx()); | ||
| CHECK_IMPLICATION(cpu.fma3(), cpu.avx()); | ||
|
|
||
| // AVX-512 iplication chain | ||
| CHECK_IMPLICATION(cpu.avx512f(), cpu.avx2()); | ||
| CHECK_IMPLICATION(cpu.avx512dq(), cpu.avx512f()); | ||
| CHECK_IMPLICATION(cpu.avx512ifma(), cpu.avx512f()); | ||
| CHECK_IMPLICATION(cpu.avx512pf(), cpu.avx512f()); | ||
| CHECK_IMPLICATION(cpu.avx512er(), cpu.avx512f()); | ||
| CHECK_IMPLICATION(cpu.avx512cd(), cpu.avx512f()); | ||
| CHECK_IMPLICATION(cpu.avx512bw(), cpu.avx512f()); | ||
| CHECK_IMPLICATION(cpu.avx512vbmi(), cpu.avx512f()); | ||
| CHECK_IMPLICATION(cpu.avx512vbmi2(), cpu.avx512f()); | ||
| CHECK_IMPLICATION(cpu.avx512vnni_bw(), cpu.avx512bw()); | ||
| CHECK_IMPLICATION(cpu.avxvnni(), cpu.avx2()); | ||
| } | ||
|
|
||
| TEST_CASE("[cpu_features] x86 manufacturer from environment") | ||
| { | ||
| xsimd::x86_cpu_features cpu; | ||
|
|
||
| const char* val = std::getenv("XSIMD_TEST_CPU_ASSUME_MANUFACTURER"); | ||
| if (val) | ||
| { | ||
| struct entry | ||
| { | ||
| const char* name; | ||
| xsimd::x86_manufacturer value; | ||
| }; | ||
| std::array<entry, 9> manufacturers = { { | ||
| { "intel", xsimd::x86_manufacturer::intel }, | ||
| { "amd", xsimd::x86_manufacturer::amd }, | ||
| { "via", xsimd::x86_manufacturer::via }, | ||
| { "zhaoxin", xsimd::x86_manufacturer::zhaoxin }, | ||
| { "hygon", xsimd::x86_manufacturer::hygon }, | ||
| { "transmeta", xsimd::x86_manufacturer::transmeta }, | ||
| { "elbrus", xsimd::x86_manufacturer::elbrus }, | ||
| { "microsoft_vpc", xsimd::x86_manufacturer::microsoft_vpc }, | ||
| { "unknown", xsimd::x86_manufacturer::unknown }, | ||
| } }; | ||
|
|
||
| auto manufacturer = cpu.known_manufacturer(); | ||
| const std::string allowed(val); | ||
| bool match = std::any_of(manufacturers.begin(), manufacturers.end(), [&](const entry& e) | ||
| { return e.value == manufacturer && detail::contains(allowed, e.name); }); | ||
|
|
||
| auto const msg = std::string("XSIMD_TEST_CPU_ASSUME_MANUFACTURER = ") + val | ||
| + ", actual = " + xsimd::x86_manufacturer_name(manufacturer); | ||
| INFO(msg); | ||
| CHECK_UNARY(match); | ||
| } | ||
| } | ||
|
|
||
| TEST_CASE("[cpu_features] x86 features from environment") | ||
| { | ||
| xsimd::x86_cpu_features cpu; | ||
|
|
||
| CHECK_ENV_FEATURE("XSIMD_TEST_CPU_ASSUME_SSE2", cpu.sse2()); | ||
| CHECK_ENV_FEATURE("XSIMD_TEST_CPU_ASSUME_SSE3", cpu.sse3()); | ||
| CHECK_ENV_FEATURE("XSIMD_TEST_CPU_ASSUME_SSSE3", cpu.ssse3()); | ||
| CHECK_ENV_FEATURE("XSIMD_TEST_CPU_ASSUME_SSE4_1", cpu.sse4_1()); | ||
| CHECK_ENV_FEATURE("XSIMD_TEST_CPU_ASSUME_SSE4_2", cpu.sse4_2()); | ||
| CHECK_ENV_FEATURE("XSIMD_TEST_CPU_ASSUME_FMA3", cpu.fma3()); | ||
| CHECK_ENV_FEATURE("XSIMD_TEST_CPU_ASSUME_FMA4", cpu.fma4()); | ||
| CHECK_ENV_FEATURE("XSIMD_TEST_CPU_ASSUME_AVX", cpu.avx()); | ||
| CHECK_ENV_FEATURE("XSIMD_TEST_CPU_ASSUME_AVX2", cpu.avx2()); | ||
| CHECK_ENV_FEATURE("XSIMD_TEST_CPU_ASSUME_AVX512F", cpu.avx512f()); | ||
| CHECK_ENV_FEATURE("XSIMD_TEST_CPU_ASSUME_AVX512BW", cpu.avx512bw()); | ||
| CHECK_ENV_FEATURE("XSIMD_TEST_CPU_ASSUME_AVX512CD", cpu.avx512cd()); | ||
| CHECK_ENV_FEATURE("XSIMD_TEST_CPU_ASSUME_AVX512DQ", cpu.avx512dq()); | ||
| CHECK_ENV_FEATURE("XSIMD_TEST_CPU_ASSUME_AVXVNNI", cpu.avxvnni()); | ||
| } | ||
|
|
||
| TEST_CASE("[cpu_features] arm implication chains") | ||
| { | ||
| xsimd::arm_cpu_features cpu; | ||
|
|
||
| CHECK_IMPLICATION(cpu.neon64(), cpu.neon()); | ||
| CHECK_IMPLICATION(cpu.sve(), cpu.neon64()); | ||
| CHECK_IMPLICATION(cpu.i8mm(), cpu.neon64()); | ||
| } | ||
|
|
||
| TEST_CASE("[cpu_features] arm features from environment") | ||
| { | ||
| xsimd::arm_cpu_features cpu; | ||
|
|
||
| CHECK_ENV_FEATURE("XSIMD_TEST_CPU_ASSUME_NEON", cpu.neon()); | ||
| CHECK_ENV_FEATURE("XSIMD_TEST_CPU_ASSUME_NEON64", cpu.neon64()); | ||
| CHECK_ENV_FEATURE("XSIMD_TEST_CPU_ASSUME_SVE", cpu.sve()); | ||
| CHECK_ENV_FEATURE("XSIMD_TEST_CPU_ASSUME_I8MM", cpu.i8mm()); | ||
| } | ||
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.