Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do OpenMP, MPI, POSIX threads, std::thread, boost::thread correlate?

There are several ways to implement multithreading. std::thread was eventually brought by C++11 standard but boost::thread could be effectively used instead. Each technology has specific syntax and stuff, but - roughly - used for CPU parallel programming. But they have different effect. I know that, for instance, MPI and OpenMP used for different memory models.

I also know that the choice of a technology is not actually exclusive so another one can be used (again, MPI and OpenMP). How come they are used for different effect but still working with the same source (CPU)?

What the difference would be (from the operation system and hardware point of view) if I compile a C++ program with parallelism based on each of those technologies? Does, for example, OpenMP or std::thread use POSIX threads? If so, how does C++11's threads work on Windows? Or does each of these technologies work directly with CPU via assembly language or something?

like image 364
kazarey Avatar asked Apr 23 '14 18:04

kazarey


1 Answers

OS provides threads (syscalls to create new threads; scheduling services).

Unix libc has wrapper around OS threads with lot useful functions (like mutexes, cond vars, etc). Usually external interface of such system libraries is "POSIX threads" (functions named pthread_*): http://en.wikipedia.org/wiki/POSIX_Threads

Windows has its own hard-to-use threading API (WINAPI's CreateThread, etc). But there are wrappers around Windows API to get something like POSIX threads api (e.g. there is such libraries for mingw32 and cygwin; check wikipedia section)

C++11 std::thread, boost's boost::thread are just modern OS-independent wrappers around system threading API. They are used to create portable programs which can be compiled on any supported platform, without creating #ifdef hell and/or writing own custom wrappers around system threading library. If your are creating new program, consider using this way.

There are several other threading wrappers, e.g. included in graphic libraries like QT or GTK+.

OpenMP implementations has internal support library (for example, gcc has libgomp) which uses system/libc threading APIs, for example libgomp uses POSIX threads. Some implementations may also include user-space thread switching via assembly (M:N threading model).

MPI has no thread library inside. MPI is used to create several processes and setup communication between them. But when MPI is used in multithreaded programs, it will use some threading API to do synchronization. For example, MPICH will use pthreads on unix.

like image 183
osgx Avatar answered Nov 10 '22 00:11

osgx