|
|
Home » U++ Library support » U++ Core » CoWork::Finish() can wait in a worker thread while there are jobs to do
CoWork::Finish() can wait in a worker thread while there are jobs to do [message #47127] |
Thu, 15 December 2016 21:30  |
busiek
Messages: 70 Registered: February 2011 Location: Poland
|
Member |
|
|
Hi Mirek,
It is said that CoWork instances can be nested. Looking into the code I see that it is also possible to use CoWork in a job (i.e. in a worker thread). Therefore CoWork::Finish() can be called inside a worker thread. The current implementation of Finish() waits if there is no more jobs to take from the local queue of CoWork while not all local jobs are finished. This is a waste of resources because a worker should never wait while there are any global jobs to do from the pool. Why not implement CoWork::Finish() like that:
void CoWork::Finish() {
Pool& p = GetPool();
p.lock.Enter();
while(todo) {
if(!jobs.IsEmpty(1)) {
LLOG("Finish: todo: " << todo << " (CoWork " << FormatIntHex(this) << ")");
p.DoJob(*jobs.GetNext(1));
} else if(is_worker && !p.jobs.IsEmpty()) {
LLOG("Do global job while WaitForFinish (CoWork " << FormatIntHex(this) << ")");
p.DoJob(*p.jobs.GetNext());
} else if(is_worker)
p.waiting_threads++;
LLOG("Waiting for job in WaitForFinish");
p.waitforjob.Wait(p.lock);
LLOG("Waiting ended in WaitForFinish");
p.waiting_threads--;
} else {
LLOG("WaitForFinish (CoWork " << FormatIntHex(this) << ")");
waitforfinish.Wait(p.lock);
}
}
p.lock.Leave();
LLOG("CoWork " << FormatIntHex(this) << " finished");
} ?
The only problem is that a worker can wait on different conditional variables. If the worker waits on the global conditional variable (p.waitforjob) and some other worker finishes remaining jobs in local CoWork queue reducing todo to zero, the worker will not be waked up since the other worker signals waitforfinish. Also removing the entire "else if(is_worker)" block and waiting only on waitforfinish is a bad solution either since if a new job is scheduled by PushJob() the worker will not be waked up. Therefore more complicated code is needed. I attach a patch with a proposition. Simply one has to track all waiting workers in Finish() which I call "waiting masters" and singal one of them in PushJob().
Jakub
|
|
|
|
|
|
|
Re: CoWork::Finish() can wait in a worker thread while there are jobs to do [message #47133 is a reply to message #47131] |
Fri, 16 December 2016 15:27  |
 |
mirek
Messages: 14255 Registered: November 2005
|
Ultimate Member |
|
|
busiek wrote on Fri, 16 December 2016 12:54I am working for a real problem. It is very difficult algorithmic problem. After reducing complexity and optimizing all possible bottlenecks the last step is parallelization. There are many dependent chunks. It looks more like DAG of jobs with low branching. Each chunk can be time consuming and because it often happens that I can distinguish just few (often two) parallel local jobs and than move forward to next part, I would loose too much time waiting in Finish(). I had to edit it. I am using Pipe also, it is really helpful. Generally U++ for algorithmic problems is just great.
OK. You might consider:
- maybe you can have single CoWork for the whole problem (but probably not)
- you can also increase the number of threads - then OS thread would be spent waiting in Finish, but not CPU core
Anyway, I am really interested how this goes. How is Pipe working for you? I have created that as sort of experiment, so it is nice to see it used.
Please let me know, after you are done: I expect you to test with 'global' stealing and without; the I shall decide based on results whether it is worth changing things.
Maybe we could allow stealing 'parent' jobs? That would solve the problem, right?
Mirek
[Updated on: Fri, 16 December 2016 15:33] Report message to a moderator
|
|
|
Goto Forum:
Current Time: Mon Apr 28 10:49:02 CEST 2025
Total time taken to generate the page: 0.00550 seconds
|
|
|