I need to switch from boost::thread to OpenMP because boss says so.
The problem is quiet simple: the result of a simulation is written to disk every 5 iteration (int it = 5,10,15...). For the sake of simplicity, suppose I have an 8-core CPU. I created 9 threads; thread 0 is used for IO, other 8 for computation. When (it%5 == 0), I check thread 0 to see if it has finished. If yes, I create another thread, call 0, and ask it to write the result to disk. If not, all threads have to wait. Usually, the time it takes to write out a result is less than 5 iterations, so I effectively "hide" the IO cost.
I have spent a few hours looking into OpenMP and I guess the same algorithm can be done with the "task" construct but I don't see how I can synchronize the threads. OpenMP experts please help. Thanks.
The current pseudo code look like this
boost::thread pool[9];
for(int it=0;it<1000;it++)
{
- simulate using pool[1,8]
- if(it%5 == 0)
+ check pool[0]
+ if finished: create new thread, assign to pool[0], write data out
+ if not, wait
}