Aussie AI

Detecting CPU Acceleration in C++

  • Book Excerpt from "Generative AI in C++"
  • by David Spuler, Ph.D.

Detecting CPU Acceleration in C++

It is tricky to check what CPU or GPU support is available to your C++ program. There are different methods for Microsoft Visual Studio, GCC, and Apple.

Preprocessor macros. The first point is that you can only use preprocessor macros if the “single platform” assumption is true. In other words, if you're building on the single platform that you're running in production, or you're a developer toying with an engine on your own single PC.

In such cases, you can detect the current build environment using preprocessor macros. For example, if you're on a Windows box with Microsoft Visual Studio, you might try this:

    #if __AVX2__
       // ... supports AVX2
    #endif

This works fine if you are running C++ on your developer desktop machine, and don't plan to run it anywhere else. But this doesn't check runtime availability of AVX2 on your user's machine. It's only testing whether you've got the AVX2 architecture flag enabled in your compiler on your build machine. Hence, it's misleading and although you can do a #if or #ifdef test for whatever macro you like, it isn't very helpful for multi-platform programming.

Run-time platform testing. The #if method can check the major platforms that you're compiling on (e.g. Windows vs Linux vs Apple), but you cannot check what exact CPU you are running on, or what capabilities it has. The preprocessor macros are processed at compile-time, and can only detect what machine it's building on. This isn't very useful in determining if your user is running the code on a CPU that supports SIMD instructions, or if their box has a GPU on it.

Instead, you need to call C++ intrinsics to detect CPU capabilities at runtime. On the x86/x64 architecture this intrinsic uses the “CPUID” opcode. The C++ intrinsic calls differ by compile platform:

  • MSVS: __cpuid or __cpuidex (superseding __isa_available in <isa_availability.h>)
  • GCC/Clang: __builtin_cpu_supports or __builtin_cpu_is functions.

 

Next:

Up: Table of Contents

Buy: Generative AI in C++: Coding Transformers and LLMs

Generative AI in C++ The new AI programming book by Aussie AI co-founders:
  • AI coding in C++
  • Transformer engine speedups
  • LLM models
  • Phone and desktop AI
  • Code examples
  • Research citations

Get your copy from Amazon: Generative AI in C++