Vectorization in C++
Authors: Benjamin Qi, Aryansh Shrivastava
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.
Examples from CF
The occasional Div 1 E is trivialized by this!
- Welcome home, Chtholly
- Awesome Substrings
Where Can I Use These?
Whether these pragmas are supported depends on the computer architecture (see here).
avx2works on CF and DMOJ.
- These have occasionally worked on past USACO problems, such as the unintended quadratic time solution at the end of this problem.
Why Should I Not Use These?
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!