使用原子内置的旋转线程屏障

mas*_*sab 1 c++ multithreading atomic barrier compare-and-swap

我正在尝试使用原子,特别是 __sync_fetch_and_add 来实现旋转线程屏障。https://gcc.gnu.org/onlinedocs/gcc-4.4.5/gcc/Atomic-Builtins.html

我基本上想要一个 pthread 屏障的替代方案。我在一个可以并行运行大约一百个线程的系统上使用 Ubuntu。

int bar = 0;                      //global variable
 int P = MAX_THREADS;              //number of threads

 __sync_fetch_and_add(&bar,1);     //each thread comes and adds atomically
 while(bar<P){}                    //threads spin until bar increments to P
 bar=0;                            //a thread sets bar=0 to be used in the next spinning barrier
Run Code Online (Sandbox Code Playgroud)

由于明显的原因,这不起作用(一个线程可能设置 bar=0,而另一个线程陷入无限 while 循环等)。我在这里看到了一个实现:使用 c++11 原子编写(旋转)线程屏障,但它似乎太复杂了,我认为它的性能可能比 pthread 屏障更差。

由于 bar 的缓存线在线程之间进行乒乓球交换,因此该实现预计还会在内存层次结构中产生更多流量。

关于如何使用这些原子指令来制作简单的屏障有什么想法吗?另外,通信优化方案也会有所帮助。

Tsy*_*rev 5

与其在线程的计数器上旋转,不如在通过的屏障数量上旋转,该数量仅会随着面临屏障的最后一个线程而增加。这样您还可以减少内存缓存压力,因为旋转变量现在仅由单个线程更新。

int P = MAX_THREADS;
int bar = 0; // Counter of threads, faced barrier.
volatile int passed = 0; // Number of barriers, passed by all threads.

void barrier_wait()
{
    int passed_old = passed; // Should be evaluated before incrementing *bar*!

    if(__sync_fetch_and_add(&bar,1) == (P - 1))
    {
        // The last thread, faced barrier.
        bar = 0;
        // *bar* should be reseted strictly before updating of barriers counter.
        __sync_synchronize(); 
        passed++; // Mark barrier as passed.
    }
    else
    {
        // Not the last thread. Wait others.
        while(passed == passed_old) {};
        // Need to synchronize cache with other threads, passed barrier.
        __sync_synchronize();
    }
}
Run Code Online (Sandbox Code Playgroud)

请注意,您需要使用volatile修饰符来旋转变量。

C++ 代码可能比 C 代码快一些,因为它可以使用获取/释放内存屏障而不是完整的__sync内存屏障,这是函数中唯一可用的屏障:

int P = MAX_THREADS;
std::atomic<int> bar = 0; // Counter of threads, faced barrier.
std::atomic<int> passed = 0; // Number of barriers, passed by all threads.

void barrier_wait()
{
    int passed_old = passed.load(std::memory_order_relaxed);

    if(bar.fetch_add(1) == (P - 1))
    {
        // The last thread, faced barrier.
        bar = 0;
        // Synchronize and store in one operation.
        passed.store(passed_old + 1, std::memory_order_release);
    }
    else
    {
        // Not the last thread. Wait others.
        while(passed.load(std::memory_order_relaxed) == passed_old) {};
        // Need to synchronize cache with other threads, passed barrier.
        std::atomic_thread_fence(std::memory_order_acquire);
    }
}
Run Code Online (Sandbox Code Playgroud)