Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compile multi-architecture code using Agner's Vector Class Library

How can I create a library that will dynamically switch between SSE, AVX, and AVX2 code paths depending on the host processor/OS? I am using Agner Fog's VCL (Vector Class Library) and compiling with GCC for Linux.

like image 999
Aleksandr Dubinsky Avatar asked Jun 07 '16 13:06

Aleksandr Dubinsky


2 Answers

See the section "Instruction sets and CPU dispatching" in the manual to the Vector Class Library. In that section Agner writes

The file dispatch_example.cpp shows an example of how to make a CPU dispatcher that selects the appropriate code version.

Read the source code to distpatch_example.cpp. At the start of the file you should see the comment

# Compile dispatch_example.cpp five times for different instruction sets:
| g++ -O3 -msse2    -c dispatch_example.cpp -od2.o
| g++ -O3 -msse4.1  -c dispatch_example.cpp -od5.o
| g++ -O3 -mavx     -c dispatch_example.cpp -od7.o
| g++ -O3 -mavx2    -c dispatch_example.cpp -od8.o
| g++ -O3 -mavx512f -c dispatch_example.cpp -od9.o
| g++ -O3 -msse2 -otest instrset_detect.cpp d2.o d5.o d7.o d8.o d9.o
| ./test

The file instrset_detect.cpp. You should read the source code to this also. This is what calls CPUID.

Here is a summary of some, but not all of, my questions and answers on CPU dispatchers.

like image 110
Z boson Avatar answered Nov 18 '22 13:11

Z boson


The assembly instruction cpuid can give you this information at runtime. Someone has helpfully created a library based on this to just what you need.

You could create a function dispatch table, and populate it with the correct code path functions based on the results of querying using this code.

UPDATE: (answer to question in comments)

To create the different code paths in the first place, you need to compile the different code paths separately, and then link them together. For each one, you specify the architecture needed by using various values of the -march switch in your compile line.

like image 4
Smeeheey Avatar answered Nov 18 '22 14:11

Smeeheey