gpt4 book ai didi

c++ - OpenMP 减慢不相关的串行循环

转载 作者:行者123 更新时间:2023-12-04 08:06:18 24 4
gpt4 key购买 nike

我有两个不相关的for循环,一个是串行执行的,一个是使用 OpenMP 并行执行的。
我使用的 OpenMP 线程越多,下一个串行代码就越慢。

class Foo {
public:
Foo(size_t size) {
parallel_vector.resize(size, 0.0);
serial_vector.resize(size, 0.0);
}

void do_serial_work() {
std::mt19937 random_number_generator;
std::uniform_real_distribution<double> random_number_distribution{ 0.0, 1.0 };

for (size_t i = 0; i < serial_vector.size(); i++) {
serial_vector[i] = random_number_distribution(random_number_generator);
}
}

void do_parallel_work() {
#pragma omp parallel for
for (auto i = 0; i < parallel_vector.size(); ++i) {
for (auto integration_steps = 0; integration_steps < 30; integration_steps++) {
parallel_vector[i] += (0.05 - parallel_vector[i]) / 30.0;
}
}
}

private:
std::vector<double> parallel_vector;
std::vector<double> serial_vector;
};

void test_with_size(size_t size, int num_threads) {
std::cout << "Testing with " << num_threads << " and size: " << size << "\n";
omp_set_num_threads(num_threads);

Foo foo{ size };

long long total_dur_1 = 0;
long long total_dur_2 = 0;

for (auto i = 0; i < 500; i++) {
const auto tp_1 = std::chrono::high_resolution_clock::now();
foo.do_serial_work();

const auto tp_2 = std::chrono::high_resolution_clock::now();
foo.do_parallel_work();

const auto tp_3 = std::chrono::high_resolution_clock::now();
const auto dur_1 = std::chrono::duration_cast<std::chrono::microseconds>(tp_2 - tp_1).count();
const auto dur_2 = std::chrono::duration_cast<std::chrono::microseconds>(tp_3 - tp_2).count();

total_dur_1 += dur_1;
total_dur_2 += dur_2;
}

std::cout << total_dur_1 << "\t" << total_dur_2 << "\n";
}

int main(int argc, char** argv) {
test_with_size(100000, 1);
test_with_size(100000, 2);
test_with_size(100000, 4);
test_with_size(100000, 8);

return 0;
}
速度变慢发生在我的本地机器上,一台 Win10 笔记本电脑,配备 4 核和超线程的 Intel Core i7-7700,24 GB RAM。编译器是 VisualStudio 2019 中的最新版本。在 RelWithDebugMode 中编译(来自 CMake,包括 /O2/openmp)。
当我使用更强大的机器时不会发生这种情况,CentOS 8 带有 2 个 Intel Xeon Platinum 9242,每个内核有 48 个内核,没有超线程,769 GB 的 RAM。编译器是 gcc/8.3.1。编译 g++ --std=c++17 -O3 -fopenmp .
Win10 i7-7700 上的时间:
Testing with 1 and size: 100000
3043846 10536315
Testing with 2 and size: 100000
3276611 5350204
Testing with 4 and size: 100000
3937311 2735655
Testing with 8 and size: 100000
5002727 1598775
在 CentOS 8、2x Xeon Platinum 9242 上:
Testing with 1 and size: 100000
727756 4111363
Testing with 2 and size: 100000
731649 2069257
Testing with 4 and size: 100000
734019 1056157
Testing with 8 and size: 100000
752584 544373
所以我最初的想法是“缓存压力太大”。但是,当我从并行部分中删除几乎所有内容但循环时,速度再次下降。

更新了并行部分并删除了工作:
    void do_parallel_work() {
#pragma omp parallel for
for (auto i = 0; i < 8; ++i) {
//for (auto integration_steps = 0; integration_steps < 30; integration_steps++) {
// parallel_vector[i] += (0.05 - parallel_vector[i]) / 30.0;
//}
}
}
Win10 上更新并行部分的时间:
Testing with 1 and size: 100000
3206293 636
Testing with 2 and size: 100000
3218667 2672
Testing with 4 and size: 100000
3928818 8689
Testing with 8 and size: 100000
5106605 10797
查看 OpenMP 2.0 标准(VS 仅支持 2.0)(在此处找到: https://www.openmp.org/specifications/ ),它在 2.7.2.5 第 7,8 行中说:

In the absence of an explicit default clause, the default behavior is thesame as if the default(shared) were specified.


在 2.7.2.4 第 30 行:

All threads within the team access the same storage area for shared variables.


对我来说,这排除了 OpenMP 线程每个拷贝 serial_vector ,这是我能想到的最后一个解释。
我很高兴就此事进行任何解释/讨论,即使我只是明显遗漏了一些东西。
编辑:
出于好奇,我还在装有 WSL 的 Win10 机器上进行了测试。运行 gcc/9.3.0,时间为:
Testing with 1 and size: 100000
833678 2752
Testing with 2 and size: 100000
762877 1863
Testing with 4 and size: 100000
816440 1860
Testing with 8 and size: 100000
991184 2350
老实说,我不确定为什么 Windows 可执行文件在与 linux 相同的机器上花费的时间要长得多(VC++ 的优化/O2 是最大的),但有趣的是,这里不会发生相同的工件。

最佳答案

Windows 上的 OpenMP 默认具有 200 毫秒的自旋锁。这意味着当您离开 omp 块时,所有 omp 工作线程都在旋转等待新工作。如果您有许多彼此相邻的 omp 块,它会有好处。在您的情况下,线程只消耗 CPU 功率。
要禁用/控制自旋锁,您有多种选择:

  • 定义环境变量 OMP_WAIT_POLICY并将其设置为 PASSIVE完全禁用自旋锁,
  • 切换到 Intel OMP Runtime随 OneAPI 一起提供。然后你可以通过定义KMP_BLOCKTIME来完全控制自旋锁定时间。环境变量,
  • 安装 Visual Studio 2019 Preview(很快应该会在正式版本中)并使用
    llvm omp .然后你也可以通过定义 KMP_BLOCKTIME 来控制自旋锁时间环境变量。
  • 关于c++ - OpenMP 减慢不相关的串行循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66206296/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com