Barrier (computer science)

In parallel computing, a barrier is a type of synchronization method. A barrier for a group of threads or processes in the source code means any thread/process must stop at this point and cannot proceed until all other threads/processes reach this barrier.

Many collective routines and directive-based parallel languages impose implicit barriers. For example, a parallel do loop in Fortran with OpenMP will not be allowed to continue on any thread until the last iteration is completed. This is in case the program relies on the result of the loop immediately after its completion. In message passing, any global communication (such as reduction or scatter) may imply a barrier.

Implementation[1]

The basic barrier has mainly two variables, one of which records the pass/stop state of the barrier, the other of which keeps the total number of threads that have entered in the barrier. The barrier state was initialized to be "stop" by the first threads coming into the barrier. Whenever a thread enters, based on the number of threads already in the barrier, only if it is the last one, the thread set the barrier state to be "pass" so that all the threads can get out of the barrier. On the other hand, when the incoming thread is not the last one, it is trapped in the barrier and keeps testing if the barrier state is changed from "stop" to "pass" and it gets out only when the barrier state changes to be "pass". The pseudocode below demonstrate this.

int barrierCounter
int barrierFlag

function barrier()
{
    Lock()

    if (barrierCounter == 0)
       barrierFlag = 0                          //reset flag is first
       
    barrierCounter = barrierCounter + 1   
    myCount = barrierCounter                    //myCount local
    
    UnLock()
    
    if (myCount == numProcessors)               //last thread
        barrierCounter = 0                      //reset
        barrierFlag = 1                         //release
    else
        wait for barrierFlag == 0

}

The potential problems are as follows:

1.When sequential barriers using the same pass/block state variable are implemented, a deadlock could happen in the first barrier whenever a thread reaches the second and there are still some threads have not got out of the first barrier.

2.Due to all the threads repeatedly accessing the global variable for pass/stop, the communication traffic is rather high, which decreases the scalability.

The following Sense-Reversal Centralized Barrier is designed to resolve the first problem. And the second problem can be resolved by regrouping the threads and using multi-level barrier, e.g. Combining Tree Barrier. Also hardware implementations may have the advantage of higher scalability.

Sense-Reversal Centralized Barrier[1][2]

A Sense-Reversal Centralized Barrier solves the potential deadlock problem arising when sequential barriers are used. Instead of using the same value to represent pass/stop, sequential barriers use opposite values for pass/stop state. For example, if barrier 1 uses 0 to stop the threads, barrier 2 will use 1 to stop threads and barrier 3 will use 0 to stop threads again and so on.[3] The pseudo code below demonstrates this.

int barrierCounter
int barrierFlag

function barrier()
{
    sense = !sense                               //toggle private sense
    
    Lock()

    barrierCounter = barrierCounter + 1   
    myCount = barrierCounter                    //myCount local
    
    UnLock()
    
    if (myCount == numProcessors)               //last thread
        barrierCounter = 0                      //reset
        barrierFlag = sense                     //release
    else
        wait for barrierFlag != sense

}

Combining Tree Barrier[2][4]

A Combining Tree Barrier is a hierarchical way of implementing barrier to resolve the scalibility by avoiding the case that all threads spinning on a same location.[3]

In k-Tree Barrier, all threads are equally divided into subgroups of k threads and a first-round synchronizations are done within these subgroups. Once all subgroups have done their synchronizations, the first thread in each subgroup enters the second level for further synchronization. In the second level, like in the first level, the threads form new subgroups of k threads and synchronize within groups, sending out one thread in each subgroup to next level and so on. Eventually, in the final level there is only one subgroup to be synchronized. After the final-level synchronization, the releasing signal is transmitted to upper levels and all threads get past the barrier.

Hardware Barrier Implementation[1]

The hardware barrier uses hardware to implement the above basic barrier model.

The simplest hardware implementation uses dedicated wires to transmit signal to implement barrier. This dedicated wire performs OR/AND operation to act as the pass/block flags and thread counter. For small systems, such a model works and communication speed is not a major concern. In large multiprocessor systems this hardware design can make barrier implementation have high latency. The network connection among processors is one implementation to lower the latency, which is analogous to Combining Tree Barrier.[5]

See also

References

  1. 1 2 3 Solihin, Yan (2015-01-01). Fundamentals of Parallel Multicore Architecture (1st ed.). Chapman & Hall/CRC. ISBN 1482211181.
  2. 1 2 Nanjegowda, Ramachandra; Hernandez, Oscar; Chapman, Barbara; Jin, Haoqiang H. (2009-06-03). Müller, Matthias S.; Supinski, Bronis R. de; Chapman, Barbara M., eds. Evolving OpenMP in an Age of Extreme Parallelism. Lecture Notes in Computer Science. Springer Berlin Heidelberg. pp. 42–52. doi:10.1007/978-3-642-02303-3_4. ISBN 9783642022845.
  3. 1 2 Culler, David (1998). Parallel Computer Architecture, A Hardware/Software Approach. ISBN 978-1558603431.
  4. Nikolopoulos, Dimitrios S.; Papatheodorou, Theodore S. (1999-01-01). "A Quantitative Architectural Evaluation of Synchronization Algorithms and Disciplines on ccNUMA Systems: The Case of the SGI Origin2000". Proceedings of the 13th International Conference on Supercomputing. ICS '99. New York, NY, USA: ACM: 319–328. doi:10.1145/305138.305209. ISBN 158113164X.
  5. N.R. Adiga, et al. An Overview of the BlueGene/L Supercomputer. Proceedings of the Conference on High Performance Networking and Computing, 2002.

[1]

  1. http://blogs.sourceallies.com/2012/03/parallel-programming-with-barrier-synchronization/
This article is issued from Wikipedia - version of the 11/26/2016. The text is available under the Creative Commons Attribution/Share Alike but additional terms may apply for the media files.