Open main menu

CDOT Wiki β

Changes

DPS921/ND-R&D

443 bytes removed, 11:57, 28 November 2018
no edit summary
Nick Krilis
= Threading in OpenMP =
OpenMP (Open Multi-Processing) is an API specification for compilers that implement an explicit SPMD programming model on shared memory architectures.OpenMP implements threading through the main thread which will fork a specific number of child threads and divide the task amongst them. The runtime environment will then allocate the threads onto multiple processors.
=C++11 =  Threading in C++11 is available through the <thread> library. C++11 relies mostly on joining or detaching forked subthreads.   === Join vs Detach ===  A thread will begin running on initialization. While running in parallel, the child thread’s scope could exit before the child thread is finished. This will result in an error. The two main ways of dealing with this problem is through joining or detaching the child thread to/from the parent thread.  The following example shows how join works with the C++11 thread library. The thread (t1) forks of when creating a new child thread (t2). Both of these threads run in parallel. To prevent t2 from going out of scope in case t1 finishes first, t1 will call t2.join(). This will block t1 from executing code until t2 returns. Once t2 joins back, t1 can continue to execute. [[File:Cppjoin.png | 500px]] Detach, on the other hand, separates the two threads entirely. When t1 creates the new t2 thread, they both run in parallel. This time, t1 will call the detach function on t2. This will cause the two threads to continue running in parallel without t1’s scope affecting t2. Therefore, if t1 exits before t2 finishes, t2 can continue to run without any errors occurring - deallocating any memory after it itself finishes.  [[File:Cppdetach.png | 500px]]  = Creating a Thread = == OpenMP ==
=== Synchronization ===  * Synchronization is a way of telling a parallel region(threads) to be completed in a specific order to the sequence in which they do things.* The most common form of synchronization is the use of barriers. Essentially the threads will wait at a barrier until every thread in the scope of the parallel region has reached the same point.* There are some constructs that help implement synchronization such as master. The master construct defines a block that is only executed by the master thread, which makes the other threads skip it. Another example is the ordered region. This allows the parallel region to be executed in sequential order. ==== Implicit Barrier ====  [[File:Openmpbarrier.png | 500px]] ==== Barrier Example ==== [[File:Example2.png | 500px]] = Threading in C++11 =  Threading in C++11 is available through the <thread> library. C++11 relies mostly on joining or detaching forked subthreads.   === Join vs Detach ===  A thread will begin running on initialization. While running in parallel, the child thread’s scope could exit before the child thread is finished. This will result in an error. The two main ways of dealing with this problem is through joining or detaching the child thread to/from the parent thread.  The following example shows how join works with the C++11 thread library. The thread (t1) forks of when creating a new child thread (t2). Both of these threads run in parallel. To prevent t2 from going out of scope in case t1 finishes first, t1 will call t2.join(). This will block t1 from executing code until t2 returns. Once t2 joins back, t1 can continue to execute. [[File:Cppjoin.png | 500px]] Detach, on the other hand, separates the two threads entirely. When t1 creates the new t2 thread, they both run in parallel. This time, t1 will call the detach function on t2. This will cause the two threads to continue running in parallel without t1’s scope affecting t2. Therefore, if t1 exits before t2 finishes, t2 can continue to run without any errors occurring - deallocating any memory after it itself finishes.  [[File:Cppdetach.png | 500px]]  === Creating a Thread ===
=== Synchronization ===
= Synchronization =
==OpenMP == Using Atomic   * Synchronization is a way of telling a parallel region(threads) to be completed in a specific order to the sequence in which they do things.* The most common form of synchronization is the use of barriers. Essentially the threads will wait at a barrier until every thread in the scope of the parallel region has reached the same point.* There are some constructs that help implement synchronization such as master. The master construct defines a block that is only executed by the master thread, which makes the other threads skip it. Another example is the ordered region. This allows the parallel region to be executed in sequential order. ====Implicit Barrier ====  [[File:Openmpbarrier.png | 500px]] ==== Barrier Example ==== [[File:Example2.png | 500px]]
Another way to manage shared data access between multiple threads is through the use of the atomic structure defined in the <atomic> library.
Atomic in the == C++11 library works very similarly to how Atomic works in OpenMP. In C++, atomic is used as a wrapper for a variable type in order to give the variable atomic properties - that is, it can only be written by one thread at a time.==
44
edits