Vector class library for processing speed

1.3k Views Asked by At

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?

3

There are 3 best solutions below

2
Jonas On BEST ANSWER

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.

0
Peter Cordes On

AVX is required for 32-byte vectors. (And AVX2 for 32B integer vectors like Vec32c). Since your Atom doesn't have AVX, don't include Agner's vectorclassi256.h or vectorclassf256.h, just the 128 headers.

Compile with -march=native to get the compiler to enable all the instruction-sets your host-CPU supports.

The implementations of the Vec16c functions will automatically use SSE4.2 intrinsics when they're enabled, because Vectorclass checks macros to see what's enabled. So just use Vec16c and you will automatically get the best implementations of every function that your target supports.

(This is true since you're doing compile-time CPU / target options. If you wanted to do run-time dispatching yourself, it would be harder.)

1
A Fog On

The vector class library has been updated and improved. It is moved to Github:

https://github.com/vectorclass