mercury-hpc / mercury

Mercury is a C library for implementing RPC, optimized for HPC.
http://www.mcs.anl.gov/projects/mercury/
BSD 3-Clause "New" or "Revised" License
172 stars 62 forks source link

Use lockless queue #127

Closed soumagne closed 6 years ago

soumagne commented 8 years ago

Currently mercury_queue requires lock/unlock on push/pop operations. We could make use of atomics to add a lockless queue. For reference: http://stackoverflow.com/questions/6089029/lock-free-queue

JohnPJenkins commented 8 years ago

Should note that we specifically need a multi-producer, multi-consumer queue under the current progress/trigger model.

soumagne commented 8 years ago

Just for reference:

soumagne commented 8 years ago

I think that eventually we could restrict ourselves to multi-producer single consumer queues, plugins may do different things but in general the only place where we dequeue is in HG_Trigger() or NA_Trigger(). So far we never allow multiple threads to call trigger at the same time...

roblatham00 commented 8 years ago

Halim Amer's Izem project (https://github.com/pmodels/izem) might do this for us -- and as with argobots, you get someone else to fix bugs.

soumagne commented 8 years ago

Thanks for the pointer, I didn't know about it. Looked at it quickly, not sure which queue models are which though. They also do the same mistake as us before, i.e. they do an allocation on every insert, bad start :)

roblatham00 commented 8 years ago

good news! halim intends to either remove the mallocs or provide malloc-free routines. https://github.com/pmodels/izem/issues/8

halimamer commented 8 years ago

@soumagne: one starts with simple :) Apart from our plan to fix the double allocation issue, the MPMC lock-free queue that is currently working is Micheal Scott's nonblocking queue (at https://github.com/pmodels/izem/blob/master/src/queue/zm_msqueue.c). I know it is one of the worst in the literature and we plan to port faster ones in the near future. I cannot estimate when this feature will be available though.

soumagne commented 8 years ago

@roblatham00 thanks for the follow-up. @halimamer :) yes we did the same. OK. I'm also thinking a multi-producer single consumer queue might be sufficient for the time being, assuming we only have one thread at the same time going into trigger on a specific context. Do you have a queue for that as well? Main issue is ABA problem but we could probably be fine with a bounded queue.

halimamer commented 8 years ago

Working on mpsc queues is part of our high priority items. We have what seems to be a working prototype called SWPQueue at https://github.com/pmodels/izem/blob/master/src/queue/zm_swpqueue.c. I am being defensive because we are still working on the theoretical proof for that queue. It is unbounded, uses only a SWAP operation on the critical path of enqueues, but still suffers the double allocation problem. In practice, that queue worked fine without any special memory reclamation problem. Once we have the theoretical proof, if memory reclamation problems occur, we have at least a hazard pointers infrastructure in place for a quick fix. You are welcome to try out this queue; it should be significantly faster than the MSQueue (Michael Scott's).

soumagne commented 6 years ago

Fixed in f49751f92a42b274f636a770721fa9cf490d2009 and c0afa142ac464f629480e4dce1343397af5b9335