Enable at least -O2
for -fopenmp
to work, and for performance in general
gcc simd.c -S -fopenmp
GCC's default is -O0
, anti-optimized for consistent debugging. It's never going to auto-vectorize with -O0
because it's pointless when every i
value from the C source has to exist in memory, and so on. Why does clang produce inefficient asm with -O0 (for this simple floating point sum)?
Also impossible when you have to be able to single-step source lines one at a time, and even modify i
or memory contents at runtime with the debugger, and have the program keep running like you'd expect the C abstract machine would.
Building without any optimization is utter garbage for performance; it's insane to even consider if you care about performance enough to be using OpenMP. (Except of course for actual debugging.) Often the speedup from anti-optimized to optimized scalar is more than what you could gain from vectorizing that scalar code, but both can be large factors so you definitely want optimizations beyond auto-vectorization.
I can use SIMD registers without OpenMP using the option -O3
because according to GCC documentation it includes the -ftree-vectorize
flag.
Right, so do that. -O3 -march=native -flto
is usually your best bet for code that will run on the compile host. Also -fno-trapping-math -fno-math-errno
should be safe for everything and enable some better FP function inlining, even if you don't want -ffast-math
. Also preferably -fprofile-generate
/ -fprofile-use
profile-guided optimization (PGO), to unroll hot loops and choose branchy vs. branchless appropriately, etc.
#pragma omp parallel
is still effective at -O3 -fopenmp
- GCC doesn't enable autoparallelization by default.
Also, #pragma omp simd
will use a different vectorization style sometimes. In your case, it seems to make GCC forget that it knows the arrays are 16-byte aligned, and use movdqu
loads (when AVX isn't available for an unaligned memory source operand for paddd xmm0, [rax]
). Compare https://godbolt.org/z/8q8Dqm - the main._omp_fn.0:
helper function that main
calls doesn't assume alignment. (Although maybe it can't after division by number of threads splits up the array into ranges, if GCC doesn't bother to do vector-sized chunks?)
Use -O2 -fopenmp
to get what you were expecting
OpenMP will let gcc vectorize more easily or efficiently for loops where you didn't use restrict
on pointer args to functions to let it know that arrays don't overlap, or for floating point to let it pretend that FP math is associative even if you didn't use -ffast-math
.
Or if you enable some optimization but not full optimization (e.g. -O2
which doesn't include -ftree-vectorize
), then #pragma omp
will work the way you expected.
Note that the x[i] = y[i] = i;
init loop doesn't get auto-vectorized at -O2
, but the #pragma
loops are. And that without -fopenmp
, pure scalar. Godbolt compiler explorer
The serial -O3
code will run faster for this small N
because thread-startup overhead is nowhere near worth it. But for large N, parallelization could help if a single core can't saturate memory bandwidth (e.g. on a Xeon, but most dual/quad-core desktop CPUs can almost saturate mem bandwidth with one core). Or if your arrays are hot in cache on different cores.
Unfortunately(?) even GCC -O3 doesn't manage to do constant-propagation through your whole code and just print the result. Or to fuse the z[i] = x[i]+y[i]
loop with the sum(x[])
loop.