Vectorization in C++
Author: Benjamin Qi
Pragmas provide additional information to the compiler. Sometimes you'll see the following lines at the beginning of a program.
#pragma GCC optimize ("Ofast")#pragma GCC target ("avx2")
According to KACTL:
#pragma GCC optimize ("Ofast")will make GCC auto-vectorize for loops and optimizes floating points better (assumes associativity and turns off denormals).
#pragma GCC target ("avx,avx2")can double performance of vectorized code, but causes crashes on old machines.
According to CodinGame,
Modern CPUs can execute up to four instructions at the same time if they are independent.
The occasional Div 1 E is trivialized by this!
- Welcome home, Chtholly
- Awesome Substrings
Whether these pragmas are supported depends on the computer architecture (see here).
avx2works on CF and DMOJ.
avxcause runtime errors on InfoArena and Szkopuł. However,
#pragma GCC optimize("unroll-loops")seems to work on InfoArena (compare TLE and AC).
- I don't have reason to believe that these work for USACO.
From this comment:
To everyone who doesn't know what's going on here: seems that topicstarter doesn't know it either, and it looks like some magic for him.
It's not a good idea to include these pragmas at the start of every program. As mentioned above, these pragmas cause RE on some sites. Other times, they are just ignored by the compiler, or even worse, they might make your code slower instead of faster (ex. see here).
Join the USACO Forum!
Stuck on a problem, or don't understand a module? Join the USACO Forum and get help from other competitive programmers!
Give Us Feedback on Vectorization in C++!
Leave suggestions for us by making a post on the USACO Forum! Ex: Unclear sections, mislabeled problems, etc.