Most people in scientific computing use OpenMP as a quasi-standard when it comes to shared memory parallelization.
Is there any reason (other than readability) to use OpenMP over pthreads? The latter seems more basic and I suspect it could be faster and easier to optimize.
With pthreads it is also much easier to produce a poorly parallelised code. This assumes OpenMP is implemented using Pthreads. That is not required, although generally true. If OpenMP were implemented to bare metal on a specialized architecture, it could be faster than Pthreads.
OpenMP should be used for loops that have to be computed on all the cores. PThread can do that too but that's a lot of work and it is very hard to maintain, you use PThread usually if you need to start a separate process which shouldn't block the main thread.
You can stop the thread using pthread_cancel : pthread_cancel(thread1); And in readdata : /* call this when you are not ready to cancel the thread */ pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL); ... /* call this when you are ready to cancel the thread */ pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
Parallelizable loops OpenMP is at its best parallelizing loops. If the application has loops which have no loop-carried dependencies, using OpenMP is an ideal choice.
It basically boils down to what level of control you want over your parallelization. OpenMP is great if all you want to do is add a few #pragma statements and have a parallel version of your code quite quickly. If you want to do really interesting things with MIMD coding or complex queueing, you can still do all this with OpenMP, but it is probably a lot more straightforward to use threading in that case. OpenMP also has similar advantages in portability in that a lot of compilers for different platforms support it now, as with pthreads.
So you're absolutely correct - if you need fine-tuned control over your parallelization, use pthreads. If you want to parallelize with as little work as possible, use OpenMP.
Whichever way you decide to go, good luck!
One other reason: the OpenMP is task-based, Pthreads is thread based. It means that OpenMP will allocate the same number of threads as number of cores. So you will get scalable solution. It is not so easy task to do it using raw threads.
The second opinion: OpenMP provides reduction features: when you need to compute partial results in threads and combine them. You can implement it just using single line of code. But using raw threads you should do more job.
Just think about your requirements and try to understand: is OpenMP enough for you? You will save lots of time.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With