我有一个非常占用 CPU 的循环过程 (5000 万次以上的调用和超过 1000 亿次的循环阶段),例如:
for(int i=0;i<*string;i++){
if(!check_some_stuff(string+i)) {
do_some_stuff(i,string-2);
if(!string)
break;
do_yet_other_stuff(string);
}
}
由于在#pragma omp parallel for ordered
中不允许使用break语句,所以我想将i
设置为非常大的值。
for(int i=0;i<*string;i++){
if(!check_some_stuff(string+i)) {
do_some_stuff(i,string-2);
if(!string)
i=0x7FFFFFFB;
do_yet_other_stuff(string);
}
}
这个功能可以在没有 OpenMP 的情况下完美运行。但是,当我添加
#pragma omp parallel for ordered shared(string)
for(int i=0;i<*string;i++){
if(!check_some_stuff(string+i)) {
do_some_stuff(i,string-2);
#pragma omp critical
if(!string)
i=0x7FFFFFFB; // it seems the assignment has no effect on the value of i.
do_yet_other_stuff(*string);
}
}
i
的值似乎没有改变,所以它进入了一个无限循环。
使用#pragma,i=0x7FFFFFFB;成为一个本地更改,不影响外部循环
。你说的是关键的那个吗?我加上它是因为我认为需要防止其他线程在i=0x7FFFFFFB;
之后修改字符串,所以可以安全地将其删除。 - user2284570#pragma omp cancel
- Ilya Verbin