4 ms·
How can one go about making one's code apt for a compiler to be able to do these kinds of things?
by parenthesis 4y ago
How can one go about making one's code apt for a compiler to be able to do these kinds of things?
- DangitBobby 4y agoI also wonder if there's any compiler that allows you to hint that you expect certain optinizations to occur (like vectorization), and if they do not, fails to compile at all.
- dragontamer 4y agoYou study autovectorizers, then you enable autovectorization warning flags, and carefully read your compilers output. If the compiler says autovectorization failed, you rewrite the code until the autovectorizer works. https://docs.microsoft.com/en-us/cpp/build/reference/qvec-report-auto-vectorizer-reporting-level?view=msvc-170 https://docs.microsoft.com/en-us/cpp/build/reference/qvec-re...
- janwas 4y agohm, I have had limited success with such nudging - it's certainly not a programming model with a predictable outcome. And as soon as the compiler or its flags are updated, maybe the situation changes.
- skavi 4y agoA good way is to have your data arranged in structs of arrays rather than in arrays of structs. This allows the compiler to generate code which just loads in linear sections of memory to SIMD registers. It’s also just more cache efficient in general. Check out data oriented design if you aren’t already familiar.
- djmips 4y agoNote that sometimes it's sufficient to have arrays of structs of arrays. That is you don't need to necessarily go whole hog.
- ummonk 4y agoThat very much depends on access patterns. If you’re performing an operation I’ve ever object with a certain field, struct of array makes sense. If you’re doing an operation which uses many fields on some arbitrary dynamic randomly ordered subset of objects, then array of structs will yield better because at least you recover some memory locality.
- astrange 4y agoDon’t bother. Autovectorization almost fundamentally doesn’t work. You can write code using explicit vectors and it’s not too bad, though less cross platform than you’d like. An actual answer involves things like restrict and alignment keywords.
- janwas 4y agoExplicit vectors can be made portable (to SSE4/AVX2/AVX-512/NEON/SVE/SVE2/RISC-V V/WASM SIMD) using the Highway library which gives you wrapper functions over the platform's intrinsics, emulating missing functionality where required: github.com/google/highway. Disclosure: I am the main author.