gpt4 book ai didi

c++ - 为什么这个 openmp 给 SIGSEGV?

转载 作者:行者123 更新时间:2023-11-28 07:20:35 24 4
gpt4 key购买 nike

代码在不使用 openmp 的情况下工作正常。我已经使用了 private 和 shared 但它仍然在定义我的结构 SinRes 的行上给出 SIGSEGV。

#pragma omp parallel for private(row, dt1, dt1s, dtmp, dat1, atur) shared(turVec)
for(row=0; row<ndicts; ++row)
{
dt1 = kI(keys)[row];
dt1s = to_string(dj(dt1));
dtmp = dt1s.substr(0, 4) + "." + dt1s.substr(4, 2) + "." + dt1s.substr(6, 2);
dat1 = kK(data)[row];
dat1s = kK(dat1->k)[1];
atur = breakLogic(dat1s, mpar, dtmp);
///free(atur);
turVec.push_back(atur);
}

错误信息是

(gdb) run 5 x5.csv
Starting program: /root/cwork/ompTur 5 x5.csv
warning: no loadable sections found in added symbol-file system-supplied DSO at 0x2aaaaaacd000
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".
[New Thread 0x2aaaac2e1700 (LWP 17500)]
[New Thread 0x2aaaac4e2700 (LWP 17501)]
[New Thread 0x2aaaac6e3700 (LWP 17502)]
[New Thread 0x2aaaad1e9700 (LWP 17503)]
[New Thread 0x2aaaad3ea700 (LWP 17504)]
[New Thread 0x2aaaad5eb700 (LWP 17505)]
[New Thread 0x2aaaad7ec700 (LWP 17506)]

Program received signal SIGSEGV, Segmentation fault.
[Switching to Thread 0x2aaaad7ec700 (LWP 17506)]
0x0000000000407a7a in sinRes::sinRes (this=0x2aaab401f430) at ompTur.cpp:33
33 struct sinRes {
(gdb)

我是 openmp 的新手。我是否需要使用任何其他构造,例如 critical

SinRes 的代码很简单

struct sinRes {
double pnl, numTrds, firstVar;
string dt;
};

最佳答案

标准 C++ 容器类在写入操作方面不是线程安全的。必须使用显式同步访问,例如:

#pragma omp critical
turVec.push_back(atur);

operator[] 不会在必要时扩展 vector 对象的内部存储,因此允许并发访问。

关于c++ - 为什么这个 openmp 给 SIGSEGV?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19535431/

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