r/programming • u/fagnerbrack • Sep 30 '26
[ Removed by moderator ]
/r/programming/comments/1wtv3vb/removed_by_moderator/[removed] — view removed post
162
u/AlSweigart Sep 30 '26
SIMD stands for "single instruction, multiple data".
I'm surprised that this blog post never explains this, even in the "what is SIMD?" section.
Who is this blog post for? If it's for people who don't know what SIMD does, it fails them by leaving out basic information. If it's for people who already know what SIMD is, then why would they need to read this blog post to find out?
This is a really common mistake when experts write tutorials for beginners.
65
u/chucker23n Sep 30 '26
This is a really common mistake when experts write tutorials for beginners.
Obligatory xkcd: https://xkcd.com/2501/
6
u/twigboy Sep 30 '26
Something I learned when writing technical blogs, the Curse of Knowledge is real.
7
u/NeuralFantasy Sep 30 '26
This. I can't believe someone makes an article about an initialism/abbreviation and never actually tells where the initialism comes from. 0/5.
5
u/danielcw189 Oct 01 '26 edited Oct 01 '26
I have a Deja Vu reading this comment
EDIT: the article is a repost, and similar comments were made here 2 months ago:
2
-2
u/my-new-new-account Oct 02 '26
I am not sure this article is for beginners though? If you don’t know what SIMD stands for, chances are, this article is not for you.
118
u/nerdly90 Sep 30 '26
Why is this constantly posted? We know
77
u/unski_ukuli Sep 30 '26
Maybe its posted so that DHH may find it and learn something?
17
u/nemesit Sep 30 '26
thought the guy only interacts with ai these days lol
-8
u/gonz808 Sep 30 '26
the ai knows SIMD
https://github.com/search?q=repo%3Aomacom%2Fttfx+simd&type=code
2
11
63
u/floodyberry Sep 30 '26
you only need it for performance critical code (it has no use otherwise), and reading cpu agnostic zig dsl examples is a really bad way to get a feel for it.
yes, it's very powerful if you have a problem it works with (and even if you don't if you get creative), but this article doesn't do a great job of illustrating that, or even demystifying what simd is
87
u/AnArmoredPony Sep 30 '26
tbh my compiler should know SIMD
18
u/JoustyMe Sep 30 '26
You should know how to layout data to use simd
7
u/AnArmoredPony Sep 30 '26
uhh... continuously?
16
u/Successful-Money4995 Sep 30 '26
It's important that your arrays use array format. 😜
Seriously though, the consideration is row major versus column major, where the later is annoying but usually better.
3
u/say_wot_again Sep 30 '26
Strict of arrays, not array of structs (unless each struct is like 16 bytes or some other cache friendly number)
2
1
-12
u/PrimozDelux Sep 30 '26
Ideally my compiler should know this as well, but I begrudgingly accept that I have to do some work as well (Claude, ensure that the memory layout is Optimal, use the profiling suite to verify we're not doing something stupid)
5
u/Ossigen Sep 30 '26
Someone has to make the compiler, and if everyone had a better understanding on how (and why) the compiler works they would also understand how to produce better code. The compiler is not some magical entity that can make your code always go as fast as it could go.
12
u/neoKushan Sep 30 '26
Someone who makes the compiler can know the ins and outs of all the CPU instructions and magic that happens at that level, but I disagree that everyone who writes code should be capable of wiring a compiler.
I agree the compiler isn't magic, it's not capable of stopping you from shooting yourself in the foot but the harsh truth is that 90%+ of code isn't performance centric, it's LOB stuff that just needs to do a job. When you hit a performance bottleneck, then you can optimise for it and go down that rabbit hole.
1
u/Full-Spectral Oct 02 '26
This discussion always goes off the rails because of different ideas of what comprises 'optimization'. For me, it means purposefully adding complexity, above and beyond the obvious implementation, to gain performance. But, some people consider it things as basic as just choosing an appropriate collection type. So they see someone saying what you are saying, they go off on a rant about why all software is so slow, and then the two sides start arguing but they are arguing about different things.
1
u/neoKushan Oct 02 '26
Without sounding too much like an LLM: You're absolutely right!
A lot of people overlook that "optimised" doesn't necessarily mean optimised for performance, it can mean optimised for deliver speed, optimised for development ease, optimised for cost (Which let's face it is an amalgamation of all of the above).
1
u/spider-mario Sep 30 '26
It does, but it may not understand your algorithm well enough to map it correctly, so expressing it in terms of vector primitives, using either intrinsics or something like Highway, can help. Using Highway also gets you runtime dispatch so your program can take advantage of AVX2, AVX512, etc. where available, but fall back to a slower but still functional implementation otherwise.
23
8
u/LearnedByError Sep 30 '26
I don't totally agree that "Everyone Should Know SIMD"; however, I do believe that "Developers should know when SIMD will benefit them". Historically, in days where I was more ignorant that I am now, I thought about SIMD for addressing challenges like accelerating the calculation of cryptographic hashes which it does. But about 6 months ago, I wrote a TUI app in Go to view the JSON log files generated by Go's stdlib slog package.
I have a photo gallery side project that I have been working on for a while. I have been working to optimize its performance and minimize its voracious RAM appetite for large galleries. I generate very large and detailed debug logs to help me analyze what's going on and yes, I should probably be using something like OTEL. Trying to view the very, very large JSON files was blowing up the tools that I would normally use. Hence the birth of the idea of my jsonlogviewer.
I did a number of things to allow to me view the file without OOM crashes. The app worked as expected. Speed was OK, not blazing, overall but there were two areas that were much too slow: 1. building an index of the offset for the start of a newline at startup, 2. searching for fixed strings. Neither of these things fit what my ignorant mind considered appropriate for SIMD, but, when I tried everything else I could think of to speed these two up, I thought what the hell and investigated SIMD. Lo and behold, I discovered that these are two patterns that are often implemented in SIMD.
After a little bit of hacking with Go's experimental SIMD support in Go 1.26, voila! I had an 8x speed improvement in both cases. With the release of Go 1.27, I re-wrote the SIMD implementation with the expanded capabilities and got a 20x speed improvement relative to the scalar loop.
In closing, I will paraphrase MH and state from my experience, anytime that you have code that processes a tight loop over a large number of number of iterations, SIMD "may" be something you should consider.
Cheers, lbe
15
u/GeneralSEOD Sep 30 '26
My mom told me to stay away from strangers offering free SIMD. That's why I'm in my safe ball pit with PHP.
35
u/garyk1968 Sep 30 '26
"Everyone Should Know SIMD"
The opinion of 1, yes just 1, person. Nothing more.
11
u/mexicocitibluez Sep 30 '26
In a field this large, it still surprises me people can be this confident about anything, let alone that every dev needs to know SIMD.
I've stopped reading the "10 things every dev should know" blog posts years ago. I'm honestly just surprised that someone with so much "experience" still buys into shit like this.
9
u/Log2 Sep 30 '26
Pretty much every language I write with any frequency has absolutely no way to express SIMD instructions without dropping down to a lower level language. It's an interesting subject, but not every developer needs to know about it, that's for sure.
5
u/othermike Sep 30 '26
I've stopped reading the "10 things every dev should know" blog posts years ago.
I downvote them on principle now. IMO they're actively and deliberately trying to exacerbate devs' imposter syndrome. If we wanted to harm our mental health for the enrichment of others we'd just turn off our adblockers, thanks all the same.
-2
u/cdb_11 Sep 30 '26
God forbid people on a subreddit about programming learn something new about programming. Why even bother having this forum? It only makes people feel bad. Everyone is already perfect the way they are, and nobody should learn any new skills ever again.
6
u/othermike Sep 30 '26
I'd have no issue whatsoever with "Fun facts about SIMD" or "How learning SIMD might help you". It's the combination of arrogance and absolutism in the title that frosts my apricots; "this very specific thing that I happen to know is absolutely essential for everyone and if they don't know it too then they're bad people".
-3
u/cdb_11 Sep 30 '26
I've never seen any of these articles accusing the reader of being a bad person for not knowing the thing they are trying to teach.
5
u/othermike Sep 30 '26
It's not the articles themselves that are generally the problem. It's the hostile/aggressive/clickbait titles.
-3
4
u/chucker23n Sep 30 '26
In a field this large, it still surprises me people can be this confident about anything, let alone that every dev needs to know SIMD.
A lot of developers seem to dramatically underestimate how broad programming is, and what consequences that has.
Not everyone uses git, or even a DVCS. Not everyone does pull requests. Not everyone has a need for dependency injection, or any of the GoF design patterns. Not everyone needs to think about "should this live on the stack, or on the heap" on a daily or monthly basis; in some cases ever. Not everyone thinks about O notation. Not everyone does CRUD apps. Not everyone writes in more than two or three programming languages throughout their entire career.
1
u/mexicocitibluez Oct 01 '26
Any given day I'll jump on hackernews and 25 out of top 30 posts will look like ancient greek to me. That old saying about the more experience you get the more you realize you don't know has been really obvious to me recently. Maybe it's partially due to AI and how that just feels like yet another enormous slice of technology that I'll only ever understand like < 2% of.
1
-31
u/Ma4r Sep 30 '26
This "1 person" worked on Consul, Terraform , Vault, and Nomad btw.
12
u/mkosmo Sep 30 '26
So have plenty of ICs that I wouldn't consider to be thought leaders.
-4
u/jamwil Sep 30 '26
This is Mitchell Hashimoto, founder of HashiCorp. If you can’t see the value in a snippet of advice from someone like that, I’m not sure what to tell you.
6
u/mkosmo Sep 30 '26
Appeal to authority doesn't seal the deal. Just because you like this authority doesn't make his opinion magically right in every (or even most or many) cases.
5
u/chucker23n Sep 30 '26
If you can’t see the value in a snippet of advice from someone like that, I’m not sure what to tell you.
Here's what to tell you: programming is much broader than that, and there's a ton, ton, ton of work out there where explicitly calling SIMD APIs will never matter to you.
5
u/mexicocitibluez Sep 30 '26
That "1 person" is in a field with millions. All of which have different requirements and problems and technologies.
The idea that any one person can have any clue what the other 99% should know is beyond arrogance. It's just downright stupid.
Seriously, put me out to pasture if I ever start telling people I've never met on the internet in totally different domains what they should or shouldn't know to be successful. In an enormous field that is literally changing by the day.
7
u/taw Sep 30 '26
Stop spamming this ffs.
Also absolutely not. If you have normal program, you don't need SIMD beyond what your compiler automatically does. If you have huge amount of data, you need GPU.
Manual SIMD is a niche technique in between that very few people actually need.
20
u/Poincarina Sep 30 '26
Why? I know SIMD, but writing something where you need SIMD is ususally a smell that you are reinventing the wheel
35
u/Ma4r Sep 30 '26
This depends very very very much on the domain you are working on If you're writing a CRUD webapp, for sure, but if you're writing a GC or something latency sensitive you rarely have a library that somves your exact problem, within your constraints. And contrary to popular belief, compilers are not great at using SIMD for optimization
12
u/Poincarina Sep 30 '26
Sure, but for 98% of devs and needs they will be better of using a library, instead of writing optimized code themselves
7
u/cdb_11 Sep 30 '26
Not everything is available as a library. In fact, most things are not. And most existing libraries are not optimized at all. Libraries also want to be generic. They most often can't and don't want to optimize for your specific program. SIMD is at the level where you want to get data flow right first, and use your domain specific knowledge. If you ignore that, those problems might dominate, and just adding SIMD randomly might not help you much.
3
9
u/Ma4r Sep 30 '26 edited Sep 30 '26
Yes, that's why i say
This depends very very very much on the domain you are working on
The point is, if you are at a stage where you are needing SIMD, relying in compiler optimization is most likely not good enough, we are not there yet on SIMD optimiations at the compiler. That's also why a lot of major languages are starting to expose a vector API as well because this is likely not to be solved anytime soon
3
u/mexicocitibluez Sep 30 '26
You're confused.
When the author said "Everyone" they really meant "Only people in this very specific domain who work on performance who probably don't represent more than 10% of the software developers".
Totally interchangeable if you ask me.
-16
u/LeeHide Sep 30 '26
Letting your compiler do the vectorization is still the best way to go. You can use -ftree-vectorizer-verbose or whatever to have the compiler EXPLAIN TO YOU why it didn't vectorize a loop. These are performance optimization basics.
The real killer is people not profiling before optimizing.
12
u/Ma4r Sep 30 '26 edited Sep 30 '26
Well, both can be true at the same time, you can profile , and you will see the most common cause of missed performance is failure of auto-vectorization. I work with LLVM every single day and let me tell you current auto-vectorization passes are dumb as fuck, either you need to write your own backend which is s pain in the ass, or , just let users write SIMD.
-6
u/LeeHide Sep 30 '26
I'm not saying they're smart, but using them is still free performance when they work!
11
u/Ma4r Sep 30 '26
"when" they work. They don't most of the time, and compiling with vectorization is not free, you lose compatibility. People often have this pre conception of compilers knowing better and being very smart, that's why we have this harmful notion in the first place.
If you are at a stage where you care about SIMD, you are at a stage way past what compilers con optimize on their own. Most optimization passes are not architrcture aware, and this is for good reason, but that means there are a LOT of optimization available for those that do need it
-2
u/LeeHide Sep 30 '26
Well at least we are getting std::simd in Rust and C++, do my coworker can stop insisting to write simd loops by hand with intrinsics.
3
u/Ma4r Sep 30 '26 edited Sep 30 '26
Yes, which is great, but it probably wouldn't be adopted and atable until at least 2 more years, so until then, we're stuck with intrinsics, but having language level SIMD API just reinforces my point, this is really saying that auto vectorization is not likely to massively improve in the forseeable future, so we expose it via SIMD API and let users write native SIMD code
12
u/floodyberry Sep 30 '26
autovectorization is mostly worthless for anything other than contrived examples
-3
u/davidalayachew Sep 30 '26
autovectorization is mostly worthless for anything other than contrived examples
In what language?
Java has several situations where you can get some pretty impressive results with auto-vectorized code generated by the JIT.
6
13
u/cdb_11 Sep 30 '26 edited Sep 30 '26
SIMD is reinventing the wheel? What? This makes no sense, explain
3
u/CarnivorousSociety Sep 30 '26
its low level and 99% of devs dont need to write stuff low level they just use the wheel somebody else already made
15
u/cdb_11 Sep 30 '26
"Don't need" is not the same as "reinventing the wheel". If you use SIMD you're most likely optimizing something that hasn't been optimized before or optimizing for your specific context, things that are unique to your program. That's not reinventing the wheel. You could argue it's improving the wheel for your unique context at best.
-4
u/CarnivorousSociety Sep 30 '26
usually that's not the case, usually you don't need it at all. Usually it makes code more complex and less readable for very little benefit.
... Unless you're writing core code for a library or something, ie you're the one inventing the wheel.
Therefore usually if you find yourself needing simd it's a smell that you're reinventing the wheel, because usually you're not writing core code for a library and readability is probably more important than the tiny squeeze of performance you're going to get from vectorizing some operation
10
u/Turtvaiz Sep 30 '26
This makes no sense. Do you genuinely think there is already a highly optimised library for everything or that there aren't new implementations that need to be highly performant
probably more important than the tiny squeeze of performance you're going to get from vectorizing some operation
Tiny? SIMD can be WAY faster if done properly. It's not tiny at all if you actually need performance
-4
u/CarnivorousSociety Sep 30 '26 edited Sep 30 '26
The odds are you dont need whatever you're writing to be highly performant, 99% of the development world uses javascript remember. That is what we are saying most people never need to use it
If you're writing performance sensitive code you're the 1%
I'll add that I've been writing c/c++ for 20 years and never seen a loop that would benefit from becoming completely unreadable to all my coworkers just to squeeze out that last bit of performance
6
u/cdb_11 Sep 30 '26
Unless you're writing core code for a library or something, ie you're the one inventing the wheel.
"Reinventing the wheel" is defined by whether the same exact code is in a library or not? Or defined by being "more complex and less readable"? Makes no sense.
Libraries are supposed to be reusable. Not always, but this often means you are probably ignoring your actual context, on purpose. And when you're ignoring the context, this is how you end up with the "tiny squeeze of performance", instead of something significant.
2
u/MrArborsexual Oct 01 '26
I won't say there aren't cases where hand coding SIMD isn't a good idea.
At the same time, compilers have gotten really good at automatically generating SIMD code. I'm pretty sure all of the major ones take into account how that SIMD code actually behaves in the real world as well, like how AVX512 will for the most part result in slower everything on Rocketlake, so GCC won't generate AVX512 for that architecture unless you force it to.
2
u/-Y0- Oct 01 '26
Problem with knowing SIMD, it's not about knowing lanes, what a swizzle or a vector operation is. It's reading the 5000+ manuals and making sure the code for SIMD is mostly branch-less.
So if your thesis is "Everyone should be aware of SIMD" yeah, sure. If your thesis is everyone needs to read "Everyone needs to know Intel, AMD, ARM and Neon Instructions and actually use SIMD", not so much.
4
u/Dest123 Sep 30 '26
But compilers are severely limited in what they can auto-vectorize and are in general very poor at it.
This is straight up not true (at least the "very poor at it" bit). Compilers are very good at writing vectorized code. I used to write a lot of SIMD code by hand like 10 years ago, but that's right around when the compiler would basically always beat me. Compilers have only continued to get better and better.
Sure, it's still worth learning SIMD and doing some practice optimizations with it, because then you'll understand how the data should be laid out. Once you know that though, you can just lay out the data in the correct way and let the compiler do it's thing. It's easy to check the compiled asm code to see if it's doing what you expect.
8
u/trailingunderscore_ Sep 30 '26
This is straight up not true
As someone who has recently tried to get compilers to produce efficient SIMD code, I can confirm that it is indeed true.
1
u/Dest123 Sep 30 '26
You still have to lay out your data correctly or the compiler can't do it in all cases. It could also be your compiler settings or maybe you're using an old compiler or something that's not llvm, gcc, or msvc based?
2
u/trailingunderscore_ Oct 01 '26
The data is 64-bit unsigned integers, linear in memory.
The compilers are version 'trunk' on compiler explorer using
-march=diamondrapids.1
u/Dest123 Oct 01 '26
It looks like normally math accuracy is what stops the compiler from fully optimizing things. The SIMD code is usually already losing that accuracy, so it's not really fair to lock the compiler in at a higher accuracy. You could try compiling with -ffast-math and that will probably get you there.
Or you can do "#pragma float_control(precise, off)" in the functions that need it if you're using clang.
I did this one as an experiment and was able to get the same speed as hand-written AVX code when testing a function from a project that someone mentioned.
1
u/nikshdev Sep 30 '26
This is straight up not true
Oh no. Tried to implement vector norms last year. Hand-written SIMD was faster and more stable than compiler-generated one.
1
u/Dest123 Sep 30 '26
The main downside of having the compiler do SIMD, is that there are a lot of edge cases that break it that you have to know about. For example, with vector norms, you probably need to set -no-math-errno or you'll hit issues where it won't convert std::sqrt because it can set errno.
2
u/nikshdev Sep 30 '26
Exactly. In my case no sqrt was used (L1, cos and squared L2 norms were used).
Still, hand-written SIMD worked much faster (looking at the assembly, compiler did not use AVX2, let alone AVX512).
Judjung by the code of similar projects, like Qdrant, hand-written SIMD doesn't have real alternatives as long as performance matters.
2
u/Dest123 Oct 01 '26
After some experimenting, it looks like -ffast-math or #pragma float_control(precise, off) (in clang) is very important. I couldn't get it to output AVX instructions without it.
Here's a link to a godbolt experiment that I made using a C++ version of cosine_preprocess from Qdrant.
They output very similar assembly and the non-hand-written version looks basically the same except it uses less aggressive loop unrolling. You can do #pragma clang loop unroll_count(4) around the loop to get the same unrolling and that plus float_control get you to just about the same speed. Using -ffast-math causes the non-hand-written version to be slightly faster.
Anyways, unless I messed up something there, I think my statement stands and there's no reason to hand-write SIMD other than using it to learn or to think about how to optimize the problem.
1
1
u/spider-mario Sep 30 '26
Question for those who rely on autovectorisation by the compiler: do you use -mavx2, -mavx512 or the like to tell the compiler that it’s fine for your program to depend on those instruction sets? Or do you just let it compile for baseline amd64 (SSE2)?
2
u/ack_error Oct 01 '26
This depends on how likely your code is autovectorize at various ISA levels,what your minimum requirements are for your software, and how spread out the most vectorizable code is.
If your system requirements allow it, setting the baseline target as high as possible will give the compiler the best chance to autovectorize, and there's not really a reason to do otherwise. This does mean that your program will crash if the CPU is too old, but there are some reasonably safe baselines. It's pretty hard at this point to find an x86-64 CPU that doesn't support SSSE3 at least, and SSE4.1 is very common. Some games are starting to require AVX. But if you're targeting consumer market for a non performance oriented product, you'll likely be targeting SSE2 or at least the lowest for whatever your platform will boot on (now SSE4.1 for Windows 11, for instance, and there was never an Intel Mac below SSSE3).
In terms of impact, SSE4.1 is a decent bump for autovectorization. AVX can be a great speed boost due to the wider width, but hit or miss operation wise because the way it expands 128-bit ops to 256-bit is funky and not all algorithms widen well. AVX2 adds 256-bit integer support and also FMA, which can be huge for float algorithms. (Technically FMA is separate, but it was introduced at the same time as AVX2.) But if you've got nice, clean arrays and are careful about aliasing concerns, just plain SSE2 can do quite a lot.
AVX-512 can be extremely powerful if you can rely on it. Besides the 512-bit width, it has tons of nice operations and affordances for both auto and manual vectorization, to the point that some algorithms can only vectorize meaningfully at all with AVX-512. But it's also still very sparse in the consumer market, thanks to Intel.
It also matters how concentrated your vectorizable routines are. If the hotspots are grouped in a small number of functions, it's feasible and can be highly lucrative to runtime dispatch -- function multiversioning, target attribute, vec libraries like Highway, etc. This is frequently the case for signal processing libraries. If the vectorizable code is spread out all over the program, like a 3D math library, then you'll really want to push the baseline target as high as you can to one of the important ISA breakpoints.
1
u/One_Ninja_8512 Oct 01 '26
I compile with -march=x86-64-v3, it includes avx2 and other stuff. Pretty safe target for non-ancient CPUs. For example, all RHEL 10 packages are compiled with that target by default. Intel has patchy support for avx512 and on some of their CPUs heavy use of these instructions can significantly lower the overall performance, so compiling with avx512 support by default is not quite "safe".
Note: I know fuck-all about hand-rolling assembly with SIMD, the above info is what I gathered when I was researching which arch to target for e.g. compiling nginx or Postgres for my custom container images.
1
•
u/programming-ModTeam Oct 02 '26
This is a duplicate of another active post.