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")
About
Resources | ||||
---|---|---|---|---|
CF | ||||
GCC | ||||
KACTL | ||||
CF | ||||
CF | ||||
CodinGame | some bugs :(, seems ok for most part? |
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.
Can also check Wikipedia articles about SSE and AVX (AVX is the more advanced version).
Examples from CF
The occasional Div 1 E is trivialized by this!
- Nagini
- no pragmas: TLE 30
O3
: 3509msOfast
: 3525msOfast
,avx
: 2339msOfast
,avx2
: 1544msOfast
,sse4
: 2479ms
- Welcome home, Chtholly
- only
Ofast
: TLE avx
: 2354msavx2
: 1684mssse4
: 2183ms
- Awesome Substrings
sse4
/avx
: TLEavx2
: 6894ms
Where Can I Use These?
Whether these pragmas are supported depends on the computer architecture (see here).
Ofast
withavx2
works 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).
Problems
Status | Source | Problem Name | Difficulty | Tags | |
---|---|---|---|---|---|
Kattis | Hard |
Module Progress:
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!