From e39842f7d1ba745b576056391c708b1dd763a058 Mon Sep 17 00:00:00 2001 From: Kenneth Heafield Date: Fri, 23 Jul 2021 23:14:57 +0100 Subject: Support -fno-exceptions. Fixes #91 --- .github/workflows/ubuntu-noexceptions.yml | 25 +++++++++++++++++++++++ intgemm/aligned.h | 16 ++++++++++++++- intgemm/intgemm.cc | 15 ++++++++++++-- intgemm/intgemm.h | 34 ++++++++++++++++--------------- 4 files changed, 71 insertions(+), 19 deletions(-) create mode 100644 .github/workflows/ubuntu-noexceptions.yml diff --git a/.github/workflows/ubuntu-noexceptions.yml b/.github/workflows/ubuntu-noexceptions.yml new file mode 100644 index 0000000..ddbe216 --- /dev/null +++ b/.github/workflows/ubuntu-noexceptions.yml @@ -0,0 +1,25 @@ +name: Ubuntu + +on: + push: + branches: [master, static] + pull_request: + branches: [master, static] + +jobs: + build: + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v2 + - name: cmake + run: | + cmake -E make_directory build + cd build + cmake -DCMAKE_CXX_FLAGS=-fno-exceptions .. + - name: Compile + working-directory: build + run: cmake --build . -j2 + - name: Test + working-directory: build + run: ctest -j2 diff --git a/intgemm/aligned.h b/intgemm/aligned.h index 6fda369..112aeee 100644 --- a/intgemm/aligned.h +++ b/intgemm/aligned.h @@ -5,6 +5,10 @@ #include #endif +#if defined(_MSC_VER) ? !defined(_HAS_EXCEPTIONS) : !defined(__EXCEPTIONS) +#include +#endif + // Aligned simple vector. namespace intgemm { @@ -17,10 +21,20 @@ template class AlignedVector { : size_(size) { #ifdef _MSC_VER mem_ = static_cast(_aligned_malloc(size * sizeof(T), alignment)); - if (!mem_) throw std::bad_alloc(); + if (!mem_) { +# ifdef __EXCEPTIONS + throw std::bad_alloc(); +# else + std::abort(); +# endif + } #else if (posix_memalign(reinterpret_cast(&mem_), alignment, size * sizeof(T))) { +# ifdef __EXCEPTIONS throw std::bad_alloc(); +# else + std::abort(); +# endif } #endif } diff --git a/intgemm/intgemm.cc b/intgemm/intgemm.cc index 9b38e08..dfb54f3 100644 --- a/intgemm/intgemm.cc +++ b/intgemm/intgemm.cc @@ -105,12 +105,23 @@ CPUType GetCPUID() { const CPUType kCPU = GetCPUID(); -float Unsupported_MaxAbsolute(const float * /*begin*/, const float * /*end*/) { +void UnsupportedCPUError() { +#if defined(_MSC_VER) ? defined(_HAS_EXCEPTIONS) : defined(__EXCEPTIONS) throw UnsupportedCPU(); +#else + std::cerr << "intgemm does not support this CPU" << std::endl; + abort(); +#endif +} + +float Unsupported_MaxAbsolute(const float * /*begin*/, const float * /*end*/) { + UnsupportedCPUError(); + return 0.0f; } MeanStd Unsupported_VectorMeanStd(const float * /*begin*/, const float * /*end*/, bool /*absolute*/) { - throw UnsupportedCPU(); + UnsupportedCPUError(); + return MeanStd(); } void (*Int16::Quantize)(const float *input, int16_t *output, float quant_mult, Index size) = ChooseCPU(AVX512BW::Kernels16::Quantize, AVX512BW::Kernels16::Quantize, AVX2::Kernels16::Quantize, SSE2::Kernels16::Quantize, SSE2::Kernels16::Quantize, Unsupported_16bit::Quantize); diff --git a/intgemm/intgemm.h b/intgemm/intgemm.h index 977210d..bcd3be6 100644 --- a/intgemm/intgemm.h +++ b/intgemm/intgemm.h @@ -64,62 +64,64 @@ namespace intgemm { +void UnsupportedCPUError(); + struct Unsupported_16bit { static void Quantize(const float *, int16_t *, float, Index) { - throw UnsupportedCPU(); + UnsupportedCPUError(); } static void PrepareB(const float *, int16_t *, float, Index, Index) { - throw UnsupportedCPU(); + UnsupportedCPUError(); } static void PrepareBQuantizedTransposed(const int16_t *, int16_t *, Index, Index) { - throw UnsupportedCPU(); + UnsupportedCPUError(); } static void PrepareBTransposed(const float *, int16_t *, float, Index, Index) { - throw UnsupportedCPU(); + UnsupportedCPUError(); } static void SelectColumnsB(const int16_t *, int16_t *, Index, const Index *, const Index *) { - throw UnsupportedCPU(); + UnsupportedCPUError(); } template static void Multiply(const int16_t *, const int16_t *, Index, Index, Index, Callback) { - throw UnsupportedCPU(); + UnsupportedCPUError(); } constexpr static const char *const kName = "16-bit Unsupported"; }; struct Unsupported_8bit { static void Quantize(const float *, int8_t *, float, Index) { - throw UnsupportedCPU(); + UnsupportedCPUError(); } static void QuantizeU(const float *, uint8_t *, float, Index) { - throw UnsupportedCPU(); + UnsupportedCPUError(); } static void PrepareA(const float *, int8_t *, float, Index, Index) { - throw UnsupportedCPU(); + UnsupportedCPUError(); } static void PrepareBQuantizedTransposed(const int8_t *, int8_t *, Index, Index) { - throw UnsupportedCPU(); + UnsupportedCPUError(); } static void PrepareBTransposed(const float *, int8_t *, float, Index, Index) { - throw UnsupportedCPU(); + UnsupportedCPUError(); } static void PrepareB(const float *, int8_t *, float, Index, Index) { - throw UnsupportedCPU(); + UnsupportedCPUError(); } template static void PrepareBias(const int8_t *, Index, Index, Callback) { - throw UnsupportedCPU(); + UnsupportedCPUError(); } static void SelectColumnsB(const int8_t *, int8_t *, Index, const Index *, const Index *) { - throw UnsupportedCPU(); + UnsupportedCPUError(); } template static void Multiply(const int8_t *, const int8_t *, Index, Index, Index, Callback) { - throw UnsupportedCPU(); + UnsupportedCPUError(); } template static void Multiply8Shift(const uint8_t *, const int8_t *, Index, Index, Index, Callback) { - throw UnsupportedCPU(); + UnsupportedCPUError(); } constexpr static const char *const kName = "8-bit Unsupported"; -- cgit v1.2.3 From f1f59bb3b32aad5686eeb41c742279d47be71ce8 Mon Sep 17 00:00:00 2001 From: Kenneth Heafield Date: Fri, 23 Jul 2021 23:16:28 +0100 Subject: Fix name for Ubuntu with -fno-exceptions --- .github/workflows/ubuntu-noexceptions.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/ubuntu-noexceptions.yml b/.github/workflows/ubuntu-noexceptions.yml index ddbe216..371df05 100644 --- a/.github/workflows/ubuntu-noexceptions.yml +++ b/.github/workflows/ubuntu-noexceptions.yml @@ -1,4 +1,4 @@ -name: Ubuntu +name: Ubuntu no exceptions on: push: -- cgit v1.2.3 From 768aa689dda91f49024e647b5aee23e6b5d08556 Mon Sep 17 00:00:00 2001 From: Kenneth Heafield Date: Wed, 8 Dec 2021 21:53:30 +0000 Subject: Reduce header noise (#94) --- intgemm/intgemm.cc | 11 +++++++++++ intgemm/intgemm.h | 12 ------------ intgemm/types.h | 2 ++ 3 files changed, 13 insertions(+), 12 deletions(-) diff --git a/intgemm/intgemm.cc b/intgemm/intgemm.cc index dfb54f3..e5fe1b0 100644 --- a/intgemm/intgemm.cc +++ b/intgemm/intgemm.cc @@ -1,3 +1,14 @@ +#if defined(WASM) +// No header for CPUID since it's hard-coded. +#elif defined(__INTEL_COMPILER) +#include +#elif defined(_MSC_VER) +#include +#else +// Assume GCC and clang style. +#include +#endif + #include "intgemm.h" #include "stats.h" diff --git a/intgemm/intgemm.h b/intgemm/intgemm.h index bcd3be6..2528fdb 100644 --- a/intgemm/intgemm.h +++ b/intgemm/intgemm.h @@ -41,7 +41,6 @@ #include -#include "intgemm/intgemm_config.h" #include "types.h" #include "sse2_gemm.h" #include "ssse3_gemm.h" @@ -49,17 +48,6 @@ #include "avx512_gemm.h" #include "avx512vnni_gemm.h" -#if defined(WASM) -// No header for CPUID since it's hard-coded. -#elif defined(__INTEL_COMPILER) -#include -#elif defined(_MSC_VER) -#include -#else -// Assume GCC and clang style. -#include -#endif - /* Dispatch to functions based on runtime CPUID. This adds one call-by-variable to each call. */ namespace intgemm { diff --git a/intgemm/types.h b/intgemm/types.h index 81b38af..a4b35b4 100644 --- a/intgemm/types.h +++ b/intgemm/types.h @@ -1,4 +1,6 @@ #pragma once +#include "intgemm/intgemm_config.h" + #include #ifdef INTGEMM_COMPILER_SUPPORTS_AVX2 #include -- cgit v1.2.3 From 914aba846a266eb20fe1a74402dd65f2af697be9 Mon Sep 17 00:00:00 2001 From: Kenneth Heafield Date: Wed, 8 Dec 2021 22:04:53 +0000 Subject: clang-cl compilation --- compile_test/avx2.cc | 10 +++++++++- compile_test/avx512bw.cc | 13 ++++++++++++- compile_test/avx512vnni.cc | 14 +++++++++++++- intgemm/intgemm.cc | 2 +- intgemm/types.h | 19 ++++++++++++++++--- 5 files changed, 51 insertions(+), 7 deletions(-) diff --git a/compile_test/avx2.cc b/compile_test/avx2.cc index 8460fc0..9ed534e 100644 --- a/compile_test/avx2.cc +++ b/compile_test/avx2.cc @@ -1,7 +1,15 @@ // Some compilers don't have AVX2 support. Test for them. #include -#if defined(_MSC_VER) +// clang-cl bug doesn't include these headers when pretending to be MSVC +// https://github.com/llvm/llvm-project/blob/e9a294449575a1e1a0daca470f64914695dc9adc/clang/lib/Headers/immintrin.h#L69-L72 +#if defined(_MSC_VER) && defined(__clang__) +#include +#include +#include +#endif + +#if defined(_MSC_VER) && !defined(__clang__) #define INTGEMM_AVX2 #else #define INTGEMM_AVX2 __attribute__ ((target ("avx2"))) diff --git a/compile_test/avx512bw.cc b/compile_test/avx512bw.cc index 2cd4c6a..2361f75 100644 --- a/compile_test/avx512bw.cc +++ b/compile_test/avx512bw.cc @@ -1,7 +1,18 @@ // Some compilers don't have AVX512BW support. Test for them. #include -#if defined(_MSC_VER) +// clang-cl bug doesn't include these headers when pretending to be MSVC +// https://github.com/llvm/llvm-project/blob/e9a294449575a1e1a0daca470f64914695dc9adc/clang/lib/Headers/immintrin.h#L69-L72 +#if defined(_MSC_VER) && defined(__clang__) +#include +#include +#include +#include +#include +#include +#endif + +#if defined(_MSC_VER) && !defined(__clang__) #define INTGEMM_AVX512BW #elif defined(__INTEL_COMPILER) #define INTGEMM_AVX512BW __attribute__ ((target ("avx512f"))) diff --git a/compile_test/avx512vnni.cc b/compile_test/avx512vnni.cc index 1485cde..59035e4 100644 --- a/compile_test/avx512vnni.cc +++ b/compile_test/avx512vnni.cc @@ -1,6 +1,18 @@ #include -#if defined(_MSC_VER) +// clang-cl bug doesn't include these headers when pretending to be MSVC +// https://github.com/llvm/llvm-project/blob/e9a294449575a1e1a0daca470f64914695dc9adc/clang/lib/Headers/immintrin.h#L69-L72 +#if defined(_MSC_VER) && defined(__clang__) +#include +#include +#include +#include +#include +#include +#include +#endif + +#if defined(_MSC_VER) && !defined(__clang__) #elif defined(__INTEL_COMPILER) __attribute__ ((target ("avx512f"))) #else diff --git a/intgemm/intgemm.cc b/intgemm/intgemm.cc index e5fe1b0..d0cfb43 100644 --- a/intgemm/intgemm.cc +++ b/intgemm/intgemm.cc @@ -117,7 +117,7 @@ CPUType GetCPUID() { const CPUType kCPU = GetCPUID(); void UnsupportedCPUError() { -#if defined(_MSC_VER) ? defined(_HAS_EXCEPTIONS) : defined(__EXCEPTIONS) +#if (defined(_MSC_VER) && !defined(__clang__)) ? defined(_HAS_EXCEPTIONS) : defined(__EXCEPTIONS) throw UnsupportedCPU(); #else std::cerr << "intgemm does not support this CPU" << std::endl; diff --git a/intgemm/types.h b/intgemm/types.h index a4b35b4..44fb4e2 100644 --- a/intgemm/types.h +++ b/intgemm/types.h @@ -7,9 +7,22 @@ #endif #include -#if defined(_MSC_VER) || defined(__INTEL_COMPILER) -/* MSVC does not appear to have target attributes but is also fine with just - * using intrinsics anywhere. +// clang-cl bug doesn't include these headers when pretending to be MSVC +// https://github.com/llvm/llvm-project/blob/e9a294449575a1e1a0daca470f64914695dc9adc/clang/lib/Headers/immintrin.h#L69-L72 +#if defined(_MSC_VER) && defined(__clang__) +#include +#include +#include +#include +#include +#include +#include +#endif + +#if (defined(_MSC_VER) && !defined(__clang__)) || defined(__INTEL_COMPILER) +/* Real MSVC does not appear to have target attributes but is also fine with + * just using intrinsics anywhere. clang-cl pretending to be MSVC requires + * target attributes, so it's excluded from the above. * * The Intel compiler has a bug whereby constructors with target attributes do * not link. Like this program doesn't compile with icpc: -- cgit v1.2.3 From 5a7f5c528b30f87a7235b1cd575ca79d01253d25 Mon Sep 17 00:00:00 2001 From: Abhishek Aggarwal <66322306+abhi-agg@users.noreply.github.com> Date: Fri, 10 Dec 2021 13:49:58 +0100 Subject: More robust #if directive for activate/disable exception code (#95) Merging then fixing by adding header. --- intgemm/aligned.h | 8 ++++---- intgemm/intgemm.cc | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/intgemm/aligned.h b/intgemm/aligned.h index 112aeee..08acfe7 100644 --- a/intgemm/aligned.h +++ b/intgemm/aligned.h @@ -5,7 +5,7 @@ #include #endif -#if defined(_MSC_VER) ? !defined(_HAS_EXCEPTIONS) : !defined(__EXCEPTIONS) +#if defined(_MSC_VER) ? !_HAS_EXCEPTIONS : !__EXCEPTIONS #include #endif @@ -22,15 +22,15 @@ template class AlignedVector { #ifdef _MSC_VER mem_ = static_cast(_aligned_malloc(size * sizeof(T), alignment)); if (!mem_) { -# ifdef __EXCEPTIONS +# if _HAS_EXCEPTIONS != 0 throw std::bad_alloc(); # else std::abort(); # endif } -#else +#else if (posix_memalign(reinterpret_cast(&mem_), alignment, size * sizeof(T))) { -# ifdef __EXCEPTIONS +# if __EXCEPTIONS != 0 throw std::bad_alloc(); # else std::abort(); diff --git a/intgemm/intgemm.cc b/intgemm/intgemm.cc index e5fe1b0..340021f 100644 --- a/intgemm/intgemm.cc +++ b/intgemm/intgemm.cc @@ -117,7 +117,7 @@ CPUType GetCPUID() { const CPUType kCPU = GetCPUID(); void UnsupportedCPUError() { -#if defined(_MSC_VER) ? defined(_HAS_EXCEPTIONS) : defined(__EXCEPTIONS) +#if defined(_MSC_VER) ? (_HAS_EXCEPTIONS != 0) : (__EXCEPTIONS != 0) throw UnsupportedCPU(); #else std::cerr << "intgemm does not support this CPU" << std::endl; -- cgit v1.2.3 From 84d03709ab39f8b531eb455dedd341c09d29acaf Mon Sep 17 00:00:00 2001 From: Kenneth Heafield Date: Fri, 10 Dec 2021 13:04:25 +0000 Subject: Simplify exceptions if statements, ensure headers I see #if !_HAS_EXCEPTIONS and #if _HAS_EXCEPTIONS in official headers so assume this is fine. --- intgemm/aligned.h | 8 +++++--- intgemm/intgemm.cc | 2 +- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/intgemm/aligned.h b/intgemm/aligned.h index 08acfe7..ccd3aff 100644 --- a/intgemm/aligned.h +++ b/intgemm/aligned.h @@ -2,10 +2,12 @@ #include #include #ifdef _MSC_VER +// Ensure _HAS_EXCEPTIONS is defined +#include #include #endif -#if defined(_MSC_VER) ? !_HAS_EXCEPTIONS : !__EXCEPTIONS +#if !(defined(_MSC_VER) ? (_HAS_EXCEPTIONS) : (__EXCEPTIONS)) #include #endif @@ -22,7 +24,7 @@ template class AlignedVector { #ifdef _MSC_VER mem_ = static_cast(_aligned_malloc(size * sizeof(T), alignment)); if (!mem_) { -# if _HAS_EXCEPTIONS != 0 +# if _HAS_EXCEPTIONS throw std::bad_alloc(); # else std::abort(); @@ -30,7 +32,7 @@ template class AlignedVector { } #else if (posix_memalign(reinterpret_cast(&mem_), alignment, size * sizeof(T))) { -# if __EXCEPTIONS != 0 +# if __EXCEPTIONS throw std::bad_alloc(); # else std::abort(); diff --git a/intgemm/intgemm.cc b/intgemm/intgemm.cc index 340021f..d374aa0 100644 --- a/intgemm/intgemm.cc +++ b/intgemm/intgemm.cc @@ -117,7 +117,7 @@ CPUType GetCPUID() { const CPUType kCPU = GetCPUID(); void UnsupportedCPUError() { -#if defined(_MSC_VER) ? (_HAS_EXCEPTIONS != 0) : (__EXCEPTIONS != 0) +#if defined(_MSC_VER) ? (_HAS_EXCEPTIONS) : (__EXCEPTIONS) throw UnsupportedCPU(); #else std::cerr << "intgemm does not support this CPU" << std::endl; -- cgit v1.2.3 From a56f8225c2ebade4a742b1e691514c7d6c9def3d Mon Sep 17 00:00:00 2001 From: Kenneth Heafield Date: Fri, 10 Dec 2021 13:12:01 +0000 Subject: Of course clang-cl pretending to be MSVC has a different exception macro --- intgemm/aligned.h | 6 +++--- intgemm/intgemm.cc | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/intgemm/aligned.h b/intgemm/aligned.h index ccd3aff..cba7d06 100644 --- a/intgemm/aligned.h +++ b/intgemm/aligned.h @@ -7,7 +7,7 @@ #include #endif -#if !(defined(_MSC_VER) ? (_HAS_EXCEPTIONS) : (__EXCEPTIONS)) +#if !((defined(_MSC_VER) && !defined(__clang__)) ? (_HAS_EXCEPTIONS) : (__EXCEPTIONS)) #include #endif @@ -24,7 +24,7 @@ template class AlignedVector { #ifdef _MSC_VER mem_ = static_cast(_aligned_malloc(size * sizeof(T), alignment)); if (!mem_) { -# if _HAS_EXCEPTIONS +# if (defined(_MSC_VER) && !defined(__clang__)) ? (_HAS_EXCEPTIONS) : (__EXCEPTIONS) throw std::bad_alloc(); # else std::abort(); @@ -32,7 +32,7 @@ template class AlignedVector { } #else if (posix_memalign(reinterpret_cast(&mem_), alignment, size * sizeof(T))) { -# if __EXCEPTIONS +# if (defined(_MSC_VER) && !defined(__clang__)) ? (_HAS_EXCEPTIONS) : (__EXCEPTIONS) throw std::bad_alloc(); # else std::abort(); diff --git a/intgemm/intgemm.cc b/intgemm/intgemm.cc index d374aa0..31370e2 100644 --- a/intgemm/intgemm.cc +++ b/intgemm/intgemm.cc @@ -117,7 +117,7 @@ CPUType GetCPUID() { const CPUType kCPU = GetCPUID(); void UnsupportedCPUError() { -#if defined(_MSC_VER) ? (_HAS_EXCEPTIONS) : (__EXCEPTIONS) +#if (defined(_MSC_VER) && !defined(__clang__)) ? (_HAS_EXCEPTIONS) : (__EXCEPTIONS) throw UnsupportedCPU(); #else std::cerr << "intgemm does not support this CPU" << std::endl; -- cgit v1.2.3 From 1b8cbd6f611c21011325cfe0312940f0635dea33 Mon Sep 17 00:00:00 2001 From: jlquinn Date: Sat, 29 Jan 2022 17:37:17 -0500 Subject: Fix memory leak in AlignedVector. (#96) Co-authored-by: Jerry Quinn --- intgemm/aligned.h | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/intgemm/aligned.h b/intgemm/aligned.h index cba7d06..6b55ff2 100644 --- a/intgemm/aligned.h +++ b/intgemm/aligned.h @@ -47,6 +47,8 @@ template class AlignedVector { } AlignedVector &operator=(AlignedVector &&from) { + if (this == &from) return *this; + release(); mem_ = from.mem_; size_ = from.size_; from.mem_ = nullptr; @@ -57,13 +59,7 @@ template class AlignedVector { AlignedVector(const AlignedVector&) = delete; AlignedVector& operator=(const AlignedVector&) = delete; - ~AlignedVector() { -#ifdef _MSC_VER - _aligned_free(mem_); -#else - std::free(mem_); -#endif - } + ~AlignedVector() { release(); } std::size_t size() const { return size_; } @@ -81,6 +77,14 @@ template class AlignedVector { private: T *mem_; std::size_t size_; + + void release() { +#ifdef _MSC_VER + _aligned_free(mem_); +#else + std::free(mem_); +#endif + } }; } // namespace intgemm -- cgit v1.2.3 From 24ff82711ed0d7ade46a88e1ab14fe281ede42e3 Mon Sep 17 00:00:00 2001 From: Kenneth Heafield Date: Tue, 8 Feb 2022 22:25:08 +0000 Subject: Replace iostream with fprintf This resolves a potential static initialization ordering problem if INTGEMM_CPUID is an invalid value --- intgemm/intgemm.cc | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/intgemm/intgemm.cc b/intgemm/intgemm.cc index 31370e2..7ccd21b 100644 --- a/intgemm/intgemm.cc +++ b/intgemm/intgemm.cc @@ -12,10 +12,9 @@ #include "intgemm.h" #include "stats.h" +#include #include -#include - namespace intgemm { namespace { @@ -103,7 +102,7 @@ CPUType EnvironmentCPUID() { if (!strcmp(env_override, "AVX2")) return CPUType::AVX2; if (!strcmp(env_override, "SSSE3")) return CPUType::SSSE3; if (!strcmp(env_override, "SSE2")) return CPUType::SSE2; - std::cerr << "Unrecognized INTGEMM_CPUID " << env_override << std::endl; + fprintf(stderr, "Ignoring unrecognized INTGEMM_CPUID %s\n", env_override); return CPUType::AVX512VNNI; } @@ -120,7 +119,7 @@ void UnsupportedCPUError() { #if (defined(_MSC_VER) && !defined(__clang__)) ? (_HAS_EXCEPTIONS) : (__EXCEPTIONS) throw UnsupportedCPU(); #else - std::cerr << "intgemm does not support this CPU" << std::endl; + fprintf(stderr, "intgemm does not support this CPU.\n"); abort(); #endif } -- cgit v1.2.3 From fc3a614351ce6e667197307d97f45db5265c96af Mon Sep 17 00:00:00 2001 From: Kenneth Heafield Date: Wed, 9 Feb 2022 14:56:05 +0000 Subject: Option to turn off environment variable reading (#98) --- .github/workflows/ubuntu-no-cpuid-environment.yml | 25 +++++++++++++++++++++++ CMakeLists.txt | 5 +++++ intgemm/intgemm.cc | 15 ++++++++++---- 3 files changed, 41 insertions(+), 4 deletions(-) create mode 100644 .github/workflows/ubuntu-no-cpuid-environment.yml diff --git a/.github/workflows/ubuntu-no-cpuid-environment.yml b/.github/workflows/ubuntu-no-cpuid-environment.yml new file mode 100644 index 0000000..dc1862f --- /dev/null +++ b/.github/workflows/ubuntu-no-cpuid-environment.yml @@ -0,0 +1,25 @@ +name: Ubuntu No CPUID Environment Variable + +on: + push: + branches: [master, static] + pull_request: + branches: [master, static] + +jobs: + build: + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v2 + - name: cmake + run: | + cmake -E make_directory build + cd build + cmake -DINTGEMM_CPUID_ENVIRONMENT=OFF .. + - name: Compile + working-directory: build + run: cmake --build . -j2 + - name: Test + working-directory: build + run: ctest -j2 diff --git a/CMakeLists.txt b/CMakeLists.txt index af27542..c9f78fa 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -84,6 +84,11 @@ if (WORMHOLE) target_compile_definitions(intgemm PUBLIC INTGEMM_WORMHOLE) endif() +option(INTGEMM_CPUID_ENVIRONMENT "Allow INTGEMM_CPUID environment variable to downgrade CPU model, which is mainly for testing." ON) +if (INTGEMM_CPUID_ENVIRONMENT) + target_compile_definitions(intgemm PRIVATE INTGEMM_CPUID_ENVIRONMENT) +endif() + if(INTGEMM_DONT_BUILD_TESTS) return() endif() diff --git a/intgemm/intgemm.cc b/intgemm/intgemm.cc index 7ccd21b..d6c26b9 100644 --- a/intgemm/intgemm.cc +++ b/intgemm/intgemm.cc @@ -87,16 +87,17 @@ CPUType RealCPUID() { #endif } +#ifdef INTGEMM_CPUID_ENVIRONMENT CPUType EnvironmentCPUID() { -#if defined(_MSC_VER) +# if defined(_MSC_VER) char env_override[11]; size_t len = 0; if (getenv_s(&len, env_override, sizeof(env_override), "INTGEMM_CPUID")) return CPUType::AVX512VNNI; if (!len) return CPUType::AVX512VNNI; -#else +# else const char *env_override = getenv("INTGEMM_CPUID"); if (!env_override) return CPUType::AVX512VNNI; /* This will be capped to actual ID */ -#endif +# endif if (!strcmp(env_override, "AVX512VNNI")) return CPUType::AVX512VNNI; if (!strcmp(env_override, "AVX512BW")) return CPUType::AVX512BW; if (!strcmp(env_override, "AVX2")) return CPUType::AVX2; @@ -105,11 +106,17 @@ CPUType EnvironmentCPUID() { fprintf(stderr, "Ignoring unrecognized INTGEMM_CPUID %s\n", env_override); return CPUType::AVX512VNNI; } +#endif } // namespace CPUType GetCPUID() { - static const CPUType kLocalCPU = std::min(RealCPUID(), EnvironmentCPUID()); + static const CPUType kLocalCPU = +#ifdef INTGEMM_CPUID_ENVIRONMENT + std::min(RealCPUID(), EnvironmentCPUID()); +#else + RealCPUID(); +#endif return kLocalCPU; } -- cgit v1.2.3