PrevNext
Has Not Appeared

Vectorization in C++

Authors: Benjamin Qi, Aryansh Shrivastava

?

Edit This Page

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

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!

Where Can I Use These?

Whether these pragmas are supported depends on the computer architecture (see here).

  • Ofast with avx2 works on CF and DMOJ.
  • sse4 and avx cause runtime errors on InfoArena and Szkopuł. However, #pragma GCC optimize("unroll-loops") seems to work on InfoArena (compare TLE and AC).
  • 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).

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!

PrevNext