TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Programming Language Memory Models

180 pointsby thinxeralmost 4 years ago

9 comments

raphlinusalmost 4 years ago
A GPU followup to this article.<p>While on CPU sequentially consistent semantics are efficient to implement, that seems to be much less true on GPU. Thus, Vulkan completely eliminates sequential consistency and provides only acquire&#x2F;release semantics[1].<p>It is extremely difficult to reason about programs using these advanced memory semantics. For example, there is a discussion about whether a spinlock implemented in terms of acquire and release can be reordered in a way to introduce deadlock (see reddit discussion linked from [2]). I was curious enough about this I tried to model it in CDSChecker, but did not get definitive results (the deadlock checker in that tool is enabled for mutexes provided by API, but not for mutexes built out of primitives). I&#x27;ll also note that using AcqRel semantics is not provided by the Rust version of compare_exchange_weak (perhaps a nit on TFA&#x27;s assertion that Rust adopts the C++ memory model wholesale), so if acquire to lock the spinlock is not adequate, it&#x27;s likely it would need to go to SeqCst.<p>Thus, I find myself quite unsure whether this kind of spinlock would work on Vulkan or would be prone to deadlock. It&#x27;s also possible it could be fixed by putting a release barrier before the lock loop.<p>We have some serious experts on HN, so hopefully someone who knows the answer can enlighten us - mixed in of course with all the confidently wrong assertions that inevitably pop up in discussions about memory model semantics.<p>[1]: <a href="https:&#x2F;&#x2F;www.khronos.org&#x2F;blog&#x2F;comparing-the-vulkan-spir-v-memory-model-to-cs" rel="nofollow">https:&#x2F;&#x2F;www.khronos.org&#x2F;blog&#x2F;comparing-the-vulkan-spir-v-mem...</a><p>[2]: <a href="https:&#x2F;&#x2F;rigtorp.se&#x2F;spinlock&#x2F;" rel="nofollow">https:&#x2F;&#x2F;rigtorp.se&#x2F;spinlock&#x2F;</a>
评论 #27753538 未加载
评论 #27759924 未加载
评论 #27759541 未加载
评论 #27758320 未加载
评论 #27753835 未加载
wcarssalmost 4 years ago
The prior article in this series from ~a week ago is &#x27;Hardware Memory Models&#x27;, at <a href="https:&#x2F;&#x2F;research.swtch.com&#x2F;hwmm" rel="nofollow">https:&#x2F;&#x2F;research.swtch.com&#x2F;hwmm</a>, with some hn-discussion here: <a href="https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=27684703" rel="nofollow">https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=27684703</a><p>Another somewhat recently posted (but years-old) page with different but related content is &#x27;Memory Models that Underlie Programming Languages&#x27;: <a href="http:&#x2F;&#x2F;canonical.org&#x2F;~kragen&#x2F;memory-models&#x2F;" rel="nofollow">http:&#x2F;&#x2F;canonical.org&#x2F;~kragen&#x2F;memory-models&#x2F;</a><p>a few previous hn discussions of that one:<p><a href="https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=17099608" rel="nofollow">https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=17099608</a><p><a href="https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=27455509" rel="nofollow">https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=27455509</a><p><a href="https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=13293290" rel="nofollow">https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=13293290</a>
electricshampo1almost 4 years ago
&quot;Java and JavaScript have avoided introducing weak (acquire&#x2F;release) synchronizing atomics, which seem tailored for x86.&quot;<p>This is not true for Java; see<p><a href="http:&#x2F;&#x2F;gee.cs.oswego.edu&#x2F;dl&#x2F;html&#x2F;j9mm.html" rel="nofollow">http:&#x2F;&#x2F;gee.cs.oswego.edu&#x2F;dl&#x2F;html&#x2F;j9mm.html</a><p><a href="https:&#x2F;&#x2F;docs.oracle.com&#x2F;en&#x2F;java&#x2F;javase&#x2F;16&#x2F;docs&#x2F;api&#x2F;java.base&#x2F;java&#x2F;lang&#x2F;invoke&#x2F;VarHandle.html" rel="nofollow">https:&#x2F;&#x2F;docs.oracle.com&#x2F;en&#x2F;java&#x2F;javase&#x2F;16&#x2F;docs&#x2F;api&#x2F;java.base...</a>
评论 #27753913 未加载
knz42almost 4 years ago
A lot of the complexity comes from the lack of expressivity in languages to relate variables (or data structure fields) semantically to each other. If there was a way to tell the compiler &quot;these variables are always accessed in tandem&quot;, the compiler could be smart about ordering and memory fences.<p>The idea to extend programming languages and type systems in that direction is not new: folk who&#x27;ve been using distributed computing for computations have to think about this already, and could teach a few things to folk who use shared memory multi-processors.<p>Here&#x27;s an idea for ISA primitives that could help a language group variables together: bind&#x2F;propagate operators on (combinations of) address ranges. <a href="https:&#x2F;&#x2F;pure.uva.nl&#x2F;ws&#x2F;files&#x2F;1813114&#x2F;109501_19.pdf" rel="nofollow">https:&#x2F;&#x2F;pure.uva.nl&#x2F;ws&#x2F;files&#x2F;1813114&#x2F;109501_19.pdf</a>
评论 #27753298 未加载
评论 #27753321 未加载
mahmoudimusalmost 4 years ago
Fascinating article. I&#x27;ve been doing research in this area and I wonder if there was exploration for JinjaThreads - which operate on Jinja (a Java-like language) that does a formal DRF proof guarantee (coincidentally using Isabelle&#x2F;HOL).<p>You can read more about this here if you&#x27;re interested: <a href="https:&#x2F;&#x2F;www.isa-afp.org&#x2F;entries&#x2F;JinjaThreads.html" rel="nofollow">https:&#x2F;&#x2F;www.isa-afp.org&#x2F;entries&#x2F;JinjaThreads.html</a>
romesmokealmost 4 years ago
I&#x27;m wondering: is the fact that a CS PhD finds resources like this as much amusing as educational&#x2F;pedagogical gold telling something for the Academia, the Culture, or the Self?<p>AKA why can&#x27;t I stumble upon such stuff more often. Thanks OP!
jqpabc123almost 4 years ago
<i>If thread 2 copies done into a register before thread 1 executes, it may keep using that register for the entire loop, never noticing that thread 1 later modifies done.</i><p>Alternative solution: Forget all the &quot;atomic&quot; semantics and simply avoid &quot;optimization&quot; of global variables. Access to any global variable should always occur direct from memory. Sure, this will be less than optimal in some cases but such is the price of using globals. Their use should be discouraged anyway.<p>In other words, make &quot;atomic&quot; the sensible and logical default with globals. Assignment is an &quot;atomic&quot; operation, just don&#x27;t circumvent it by using a local copy as an &quot;optimization&quot;.
评论 #27752327 未加载
评论 #27752005 未加载
评论 #27752023 未加载
voidnullnilalmost 4 years ago
These &quot;memory models&quot; are too complex for languages intended for dilettante developers. It was a disaster in Java&#x2F;C#. Not even more than a handful of programmers in existence know in depth how it works, as in, can they understand any given trivial program in their language. At best they only know some vague stuff like that locking prevents any non visibility issues. It goes far deeper than that though (which is also the fault of complex language designs like Java and C#).<p>The common programmer does not understand that you&#x27;ve just transformed their program - for which they were taught merely that multiple threads needs synchronization - into a new game, which has an entire separate specification, where every shared variable obeys a set of abstruse rules revolving around the happens-before relationship. Locks, mutexes, atomic variables are all one thing. Fences are a completely different thing. At least in the way most people intuit programs to work.<p>Go tries to appeal to programmers as consumers (that is, when given a choice between cleaner design and pleasing the user who just wants to &quot;get stuff done&quot;, they choose the latter), but yet also adds in traditional complexities like this. Yes, there is performance trade off to having shared memory behave intuitively, but that&#x27;s much better than bugs that 99% of your CHOSEN userbase do not know how to avoid. Also remember Go has lots of weird edge cases, like sharing a slice across threads can lead to memory corruption (in the C &#x2F; assembly sense, not merely within that array) despite the rest of the language being memory-safe. Multiply that by the &quot;memory model&quot;.<p>Edit: forgot spaces between paragraphs.
评论 #27755117 未加载
评论 #27760356 未加载
评论 #27753349 未加载
评论 #27758359 未加载
评论 #27757035 未加载
评论 #27757966 未加载
bullenalmost 4 years ago
In a 100 years the main languages used will still be C on the client (with a C++ compiler) and Java on the server.<p>Go has no VM but it has a GC. WASM has a VM but no GC.<p>Eveything has been tried and Java still kicks everythings ass to the moon on the server.<p>Fragmentation is bad, lets stop using bad languages and focus on the products we build instead.<p>&quot;While I&#x27;m on the topic of concurrency I should mention my far too brief chat with Doug Lea. He commented that multi-threaded Java these days far outperforms C, due to the memory management and a garbage collector. If I recall correctly he said &quot;only 12 times faster than C means you haven&#x27;t started optimizing&quot;.&quot; - Martin Fowler <a href="https:&#x2F;&#x2F;martinfowler.com&#x2F;bliki&#x2F;OOPSLA2005.html" rel="nofollow">https:&#x2F;&#x2F;martinfowler.com&#x2F;bliki&#x2F;OOPSLA2005.html</a><p>&quot;Many lock-free structures offer atomic-free read paths, notably concurrent containers in garbage collected languages, such as ConcurrentHashMap in Java. Languages without garbage collection have fewer straightforward options, mostly because safe memory reclamation is a hard problem...&quot; - Travis Downs <a href="https:&#x2F;&#x2F;travisdowns.github.io&#x2F;blog&#x2F;2020&#x2F;07&#x2F;06&#x2F;concurrency-costs.html" rel="nofollow">https:&#x2F;&#x2F;travisdowns.github.io&#x2F;blog&#x2F;2020&#x2F;07&#x2F;06&#x2F;concurrency-co...</a>
评论 #27753946 未加载
评论 #27754174 未加载