4

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
}
JohnC
  • 51
  • 1
  • 2
  • 1
    I love the "boss says so" educate the Boss, OpenMP is NOT threads, for that you may look at Intel TBB. – whatnick Jun 29 '11 at 23:35
  • 2
    @whatnick: though it has a thread class, Intel TBB is not about threads either. – Alexey Kukanov Jun 30 '11 at 20:11
  • OpenMP has "lock" (similar to mutex) that I think one can use to track threads. In this case, I think the solution is to have the IO task unlock the lock, and to make sure the lock is unlocked before spawning another IO task. When I asked this question, I did not know of "locks". I think I have an answer to my own question; but I still need OpenMP experts to verify. – JohnC Jul 03 '11 at 03:36

2 Answers2

2

Intel has a very nice answer to the OpenMP vs Threads dilemma, I would defer to them and ask your boss to get some education.

OpenMP is very loop oriented, you parallize the entire loop rather than have synchronised threads.

whatnick
  • 5,400
  • 3
  • 19
  • 35
  • educating boss is out of the question. Same with boost, which is nice but boss doesn't want to package the library with our software. OpenMP 3 has a new "task" construct to spawn threads at will so there must be some way to know when the thread has finished... – JohnC Jun 30 '11 at 03:56
  • Intel libraries... not too fond of. Wait until you have to read the manual in Rusnglish (i am talking about MKL library) then you see what i mean. My company sticks with gcc, and I think boss is right about this... – JohnC Jun 30 '11 at 04:00
0

Your design seems right to me overall: having a separate thread for I/O and a thread pool for computations is right. You might possibly replace Boost threads in pool[1..8] with OpenMP for the computational part, but I would not go beyond that. If you can't use Boost, use POSIX threads.

Alexey Kukanov
  • 12,479
  • 2
  • 36
  • 55