5 ms·
This article gives the impression that everything is the compiler's fault when you end up with conflicting reads in different cores, but that's not right. From
by Dylan16807 3y ago
This article gives the impression that everything is the compiler's fault when you end up with conflicting reads in different cores, but that's not right.
From the point of view of someone outside the CPU, yes you can say that simultaneous reads might never give different answers. But everything happening at that level barely resembles the original software. Dozens of instructions are happening at any moment, overlapping each other, starting and finishing in very different orders from how they're stored in memory. This happens even if you directly write machine code byte by byte.
From the point of view of the software, running a bunch of instructions in sequence, you do get stale values. You can have two threads wait for a signal, then both read a value, and both get different results. You can have a thread set a flag after it's done editing some values, have another thread wait for the flag, and then after waiting it sees the edits as still incomplete.
The exact types of nonsense depend on the memory model, but things as simple as two MOV instructions in a row can violate strict ordering. It's not just that the compiler might stash something in a register, but that the CPU itself will make swaths of very observable changes within the rules set by the memory model.
You can't trust the hardware coherency protocol to do much for you until you follow the platform-specific rules to tell the CPU to make something act coherent.
- zerohp 3y agoThe author has a good grasp of coherence but either ignores or fails to understand memory ordering.
- tylerhou 3y agoWhere did you get this impression? That’s not how I understood the article; I thought it was good.
- rossjudson 3y agoUh, that's uncalled for. I think your hint is right there in the title: Myths Programmers Believe about CPU Caches. Was there an inaccuracy in the article with respect to CPU Caches?
- hinkley 3y agoWhich is why the Java Memory Model exists, and why a number of other languages just copied it. A lot of problems with threading got sorted out under the auspices of making Java work right on your hardware/operating system.
- jcranmer 3y agoStrictly speaking, the Java Memory Model derives from the data-race-free model of the early '90s. Java was the first programming language to explicitly incorporate it as part of the specification, but the main derivation actually comes from the C++ memory model, which built into it the basic atomic memory model that most derivatives rely on--Java doesn't have the weaker atomics support that C++ added, just sequentially-consistent. Java also introduced some frankly confusing (and incorrect, per my understanding) semantics for what happens in the face of data races that everybody else just shrugged and said "let's make it be UB and call it a day."
- hinkley 3y agoUh, this is not the history I recall at all. Why is Hanz Boehm referring to memory model fixes and the JMM in the C++11 spec (Java started tackling this problem in 2004, see Goetz) if Java copied C++?
- jcranmer 3y agoC++ extended the Java memory model to include atomics, and everyone else copied the C++ atomics memory model. Basically, it goes: * Original data-race-free model (early 90s) * Java 5 memory model (~2004-5) [first incorporated into the programming language, as I'm aware] * C++0x memory model (finalized 2007-2008, IIRC) [added atomics] * Compilers build their internal intrinsics on top of C++0x, sans consume * Everyone else adopts either the C++ memory model as is, or without consume (Sibling comment notes that Java 9 backports the atomics from C++).
- hashmash 3y ago> Java doesn't have the weaker atomics support that C++ added Yes it does, but it doesn't have first-class keywords to represent these modes. You have to use the VarHandle class, which is a bit kludgy. https://docs.oracle.com/en/java/javase/20/docs/api/java.base/java/lang/invoke/VarHandle.html https://docs.oracle.com/en/java/javase/20/docs/api/java.base...
- throwawaylinux 3y ago> You can't trust the hardware coherency protocol to do much for you until you follow the platform-specific rules to tell the CPU to make something act coherent. If software has to perform some operation to maintain coherency then you hardware is not cache coherent. Which some aren't, and especially some sub-sets of operations aren't (instruction cache vs data ops, or CPU vs DMA). But for load/store operations by CPUs, they're all coherent. Barrier/fence instructions are about enforcing ordering on when memory operations to more than one location can be performed or become visible, with respect to one another.
- Dylan16807 3y agoIt's coherent behind the scenes but it often presents an incoherent view to the software. It's not acting coherent when the rearrangement of memory operations makes you see different orderings from different cores. When a CPU has a loose memory model and is aggressively making use of the reordering capabilities, the cache being coherent internally is basically just an implementation detail. It's not part of the visible ABI.
- throwawaylinux 3y agoIt never presents an incoherent view to software. I'm using coherency as in the term of art, not a colloquial meaning. Every agent observes stores to a location in the same order[*]. Cache coherency says nothing about observed ordering of stores to different locations. [*] Although store forwarding throws a bit of a spanner in that definition, there can still be reordering occurring absent that local reordering.
- Dylan16807 3y agoFine, with that specific term of art meaning then ignore my second post. I stand by my original statement that you can't trust it to "do much for you". Just replace the last word with "act consistent" or "act ordered". Per-address ordering is nearly useless by itself. And if you had a CPU that didn't guarantee that, you'd observe almost no difference.
- vmfunction 3y ago> From the point of view of the software, running a bunch of instructions in sequence, you do get stale values. You can have two threads wait for a signal, then both read a value, and both get different results. You can have a thread set a flag after it's done editing some values, have another thread wait for the flag, and then after waiting it sees the edits as still incomplete. Isn't that implementation of semaphore? I would imagine that should already being done in the CPU implementation?
- pjc50 3y agoOnly if you use the special instructions for that purpose. If you're just doing a regular read/write, you're vulnerable to this kind of problem. If you do use the atomic instructions, you're guaranteed a consistent view across threads for that location. Check your platform documentation as to whether this is also a memory barrier, affecting ordering between the atomic instruction and other non-atomic instructions.