异步写入位数组

yo'*_*yo' 6 c++ multithreading bit-manipulation c++11

TL; DR如何安全perfom单个位更新A[n/8] |= (1<<n%8);A是一个巨大的阵列charS(即设置nA使用C ++ 11的并行计算时真)<thread>库?


我正在执行易于并行化的计算。我正在计算自然数的某个子集的元素,我想找到不在该子集中的元素。为此,我创建了一个巨大的数组(例如A = new char[20l*1024l*1024l*1024l]20GiB)。一n有点这个阵列的是,如果真的n就在于我的设置。

当并行这样做,并设置真实使用的位A[n/8] |= (1<<n%8);,我似乎得到的信息损耗小,据称是因为有并存于同一工作字节A(每个线程有先读取字节,更新单位和写字节背部)。我该如何解决?有没有一种方法可以作为原子操作进行此更新?

代码如下。GCC版本:g++ (Ubuntu 5.4.0-6ubuntu1~16.04.11) 5.4.0 20160609。该计算机是8核Intel®Xeon®CPU E5620 @ 2.40GHz,37GB RAM。编译器选项:g++ -std=c++11 -pthread -O3

#include <iostream>
#include <thread>

typedef long long myint; // long long to be sure

const myint max_A = 20ll*1024ll*1024ll; // 20 MiB for testing
//const myint max_A = 20ll*1024ll*1024ll*1024ll; // 20 GiB in the real code
const myint n_threads = 1; // Number of threads
const myint prime = 1543; // Tested prime

char *A; 
const myint max_n = 8*max_A;

inline char getA(myint n) { return A[n/8] & (1<<(n%8)); }
inline void setAtrue(myint n) { A[n/8] |= (1<<n%8); }

void run_thread(myint startpoint) {
    // Calculate all values of x^2 + 2y^2 + prime*z^2 up to max_n
    // We loop through x == startpoint (mod n_threads)
    for(myint x = startpoint; 1*x*x < max_n; x+=n_threads)
        for(myint y = 0; 1*x*x + 2*y*y < max_n; y++)
            for(myint z = 0; 1*x*x + 2*y*y + prime*z*z < max_n; z++)
                setAtrue(1*x*x + 2*y*y + prime*z*z);
}

int main() {
    myint n;

    // Only n_threads-1 threads, as we will use the master thread as well
    std::thread T[n_threads-1];

    // Initialize the array
    A = new char[max_A]();

    // Start the threads
    for(n = 0; n < n_threads-1; n++) T[n] = std::thread(run_thread, n); 
    // We use also the master thread
    run_thread(n_threads-1);
    // Synchronize
    for(n = 0; n < n_threads-1; n++) T[n].join();

    // Print and count all elements not in the set and n != 0 (mod prime)
    myint cnt = 0;
    for(n=0; n<max_n; n++) if(( !getA(n) )&&( n%1543 != 0 )) {
        std::cout << n << std::endl;
        cnt++;
    }   
    std::cout << "cnt = " << cnt << std::endl;

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

什么时候n_threads = 1,我得到正确的值cnt = 29289。当n_threads = 7,我cnt = 29314cnt = 29321两个不同的电话,暗示一些位操作上的一个字节都赞同。

Max*_*hof 5

std::atomic 在这里提供您需要的所有设施:

std::array<std::atomic<char>, max_A> A;

static_assert(sizeof(A[0]) == 1, "Shall not have memory overhead");
static_assert(std::atomic<char>::is_always_lock_free,
              "No software-level locking needed on common platforms");

inline char getA(myint n) { return A[n / 8] & (1 << (n % 8)); }
inline void setAtrue(myint n) { A[n / 8].fetch_or(1 << n % 8); }
Run Code Online (Sandbox Code Playgroud)

in的负载getA是原子的(等效于load()),当然std::atomic还内置支持or将存储的值与另一个(fetch_or)原子连接。

初始化时A,天真的方法for (auto& a : A) a = 0;需要在每个存储区之后进行同步,您可以通过放弃一些线程安全性来避免这种情况。std::memory_order_release仅要求我们写的内容对其他线程可见(但对其他线程的写内容不可见)。确实,如果您这样做

// Initialize the array
for (auto& a : A)
  a.store(0, std::memory_order_release);
Run Code Online (Sandbox Code Playgroud)

您无需在x86上进行任何程序集级同步即可获得所需的安全性。您可以在线程完成之后对负载进行相反的操作,但这在x86上没有任何额外的好处(这只是一种mov方式)。

演示完整代码:https : //godbolt.org/z/nLPlv1