尽管错误共享,速度仍会提高

Ins*_*oop 6 c++ multithreading openmp false-sharing

我一直在对OpenMP进行一些测试,并且由于错误共享数组"sum"而使得该程序不应该扩展.我遇到的问题是它确实可以扩展.更糟":

  • 1个线程:4秒(icpc),4秒(g ++)
  • 2线程:2秒(icpc),2秒(g ++)
  • 4线程:0.5秒(icpc),1秒(g ++)

我真的没有通过英特尔编译器获得从2个线程到4个线程的加速.但最重要的是:为什么扩展如此好,即使它应该表现出错误的共享?

#include <iostream>
#include <chrono>

#include <array>

#include <omp.h>

int main(int argc, const char *argv[])
{
    const auto nb_threads = std::size_t{4};
    omp_set_num_threads(nb_threads);

    const auto num_steps = std::size_t{1000000000};
    const auto step = double{1.0 / num_steps};
    auto sum = std::array<double, nb_threads>{0.0};
    std::size_t actual_nb_threads;

    auto start_time = std::chrono::high_resolution_clock::now();
    #pragma omp parallel
    {
        const auto id = std::size_t{omp_get_thread_num()};
        if (id == 0) {
            // This is needed because OMP might give us less threads
            // than the numbers of threads requested
            actual_nb_threads = omp_get_num_threads();
        }
        for (auto i = std::size_t{0}; i < num_steps; i += nb_threads) {
            auto x = double{(i + 0.5) * step};
            sum[id] += 4.0 / (1.0 + x * x);
        }
    }
    auto pi = double{0.0};
    for (auto id = std::size_t{0}; id < actual_nb_threads; id++) {
        pi += step * sum[id];
    }
    auto end_time = std::chrono::high_resolution_clock::now();
    auto time = std::chrono::duration_cast<std::chrono::nanoseconds>(end_time - start_time).count();

    std::cout << "Pi: " << pi << std::endl;
    std::cout << "Time: " << time / 1.0e9 << " seconds" << std::endl;
    std::cout << "Total nb of threads actually used: " << actual_nb_threads << std::endl;

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

Sne*_*tel 7

如果编译器选择以这种方式实现它,那么该代码肯定出现错误共享.但这对编译器来说是一件愚蠢的事情.

在第一个循环中,每个线程只访问一个元素sum.没有理由num_steps写入存储该元素的实际堆栈内存; 将值保存在寄存器中要快得多,并在for循环结束后将其写回.由于数组不是易失性的或原子的,所以没有什么能阻止编译器以这种方式运行.

当然,在第二个循环中没有写入数组,因此没有错误共享.