c++ssesimdavxvector-class-library

Vector class library for processing speed


I am looking at parallel processing algorithm for processing speed improvement. I want to test Agner Fog's vector class library, VCL.

I am wondering how to select different vector classes for example Vec16c (SSE2 instruction set) and Vec32c (AVX instruction set).

I am using Intel® Atom™ x5-Z8350 Processor and according to the specs, it supports SSE4.2 instruction sets.

How can I effectively choose vector class with regards to the hardware support? Say for my processor, can I use Vec32c recommended for AVX instruction set?


Solution

  • You can use compiler defined macros to detect what instruction-sets are enabled for the target you're compiling for, such as:

    // Assume SSE2 as a baseline
    #include  <vectori128.h>
    
    #if defined(__AVX2__)
    #include  <vectori256.h>
    using vector_type = Vec32c;
    #else
    // Vec16c uses whatever is enabled, so you don't have to check for SSE4 yourself
    using vector_type = Vec16c;
    #endif
    

    This doesn't do run-time detection, so only enable AVX2 if you want to make a binary that only runs on CPUs with AVX2.

    If you want your code to work on non-x86 platforms, or x86 without SSE2 where VCL isn't supported at all, you need to protect the #include <vectori128.h> with #if as well.