SWMR (single write multiple read)

Discussions on more advanced topics such as monolithic vs micro-kernels, transactional memory models, and paging vs segmentation should go here. Use this forum to expand and improve the wiki!
Post Reply
ggodw000
Member
Member
Posts: 396
Joined: Wed Nov 18, 2015 3:04 pm
Location: San Jose San Francisco Bay Area
Contact:

SWMR (single write multiple read)

Post by ggodw000 »

the primer on cache coherency book I am going through has one interesting assumption which says in a typical modern systems, "it is not possible to for one core to be writing to the first byte of a block while another core is writing to another byte within that block." and defines it to be SWMR (single write and multiple read) invariant, that made me wonder why? Would not it be possible that two or more cores processing completely unrelated process or threads attempt to write to same block of memory? Or is it prevented by some hardware mechanism if so how and why?
key takeaway after spending yrs on sw industry: big issue small because everyone jumps on it and fixes it. small issue is big since everyone ignores and it causes catastrophy later. #devilisinthedetails
User avatar
bluemoon
Member
Member
Posts: 1761
Joined: Wed Dec 01, 2010 3:41 am
Location: Hong Kong

Re: SWMR (single write multiple read)

Post by bluemoon »

That processor running completely unrelated process or thread would have to wait the other processor finish its write, (or do hyper-threading while waiting).
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Re: SWMR (single write multiple read)

Post by Combuster »

If two processors write a different value to the same location at the exact same time, what happens then? Would you want two caches with different content? Would the result be a mixture of both values written? Neither is desired, so instead writes take turns.

The next thing is that "locations" are bus-sized cache lines and thus typically worth 16 bytes and up. This is an efficiency trick, but it does mean that writing different bytes in the same cache line still block each other as if it were the same byte.

The last step is that when you want to write somewhere, you must prevent anything else from writing there. So if you write to two locations, you have to make sure both locations are safe from writing by other processes. This introduces the possibility of deadlocks if two processes want to write to A and B, and one claims A while the other claims B and neither can claim the other half. Reads require less blocking so it's only a technical requirement to have single writes per operation.
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
ggodw000
Member
Member
Posts: 396
Joined: Wed Nov 18, 2015 3:04 pm
Location: San Jose San Francisco Bay Area
Contact:

Re: SWMR (single write multiple read)

Post by ggodw000 »

OK got it, then it is a deliberate design, obviously. Thanks!
key takeaway after spending yrs on sw industry: big issue small because everyone jumps on it and fixes it. small issue is big since everyone ignores and it causes catastrophy later. #devilisinthedetails
Post Reply