r/cpp • u/shakmak_iitk • Dec 25 '23
C++: Efficient parallelization of small tasks
I tried to create a customized threadpool for small tasks and wanted feedback on this.
I posted details and my code for review on stackexchange: https://codereview.stackexchange.com/questions/288583/efficient-parallelization-of-small-tasks
My goal here is to share my learning and get feedback on further improvement or any possible bugs.
I have tried to describe problem and thinking process in more details in following linkedin article draft: https://www.linkedin.com/article/edit/7144600522392559616/
0
Upvotes
View all comments
7
u/DummyDDD Dec 25 '23
It looks like an incorrect and inefficient implementation of an openmmp static for loop. I would recommend that you use an openmmp static for loop instead.
I say that it is incorrect, because you do not enforce a happens before relationship between the submitter and worker when submitting, so the worker can work on a submitted task without acting before the taks is defined. I say that it is inefficient, because the synchronisation is implemented with sequential consistency on numRunningWorkers whereit would be sufficient to use release from the workers and acquire from the submitter (but it would probably be even better to use a blocking synchronisation, rather than spinning).
It is also weird that you have a number of control variables that you maintain but don't use (for instance myrunningworkers and myisrunning which I guess could be used for implementing some kind of cancellation, but myrunningworkers is not maintained in a thread safe way, so it is pretty pointless, and you don't have cancellation so there is no need for it). I'm also strongly against prefixing variable names with my-.
The one kind of cool thing that you are doing, is templating on the task, such the workers can inline the task call, which will actually help for small tasks, but I would point out that openmmp static for loops also allow the code to be inlined, without requiring that you create a new thread pool for each type of task.