the code below crashes with
terminate called after throwing an instance of 'std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >'
Aborted (core dumped) ./a.out
when compiled with g++ -fopenmp
#include <string>
#include <vector>
#include <iostream>
void bla2(){
try{
throw std::string("bla2");
}catch(std::string x){
throw std::string("error ")+x;
}
}
void bla1(){
try{
std::vector<std::string> y;
y.resize(10);
#pragma omp parallel for ordered schedule(static) num_threads(10)
for(int i=0;i<10;++i){
try{
#pragma omp ordered
{
bla2();
}
}catch(std::string x){
y[i]=x;
}
}
for(auto &x:y) if(x.size()>0) throw x;
}catch(std::string x){
throw std::string("error ")+x;
}
}
int main(){
try{
bla1();
}catch(std::string x){
std::cout<<x<<std::endl;
}
}
Note that the segmentation fault can be avoid by omitting #pragma omp ordered
OS: Linux kernel 6.17.5
g++: 15.2.1
Any suggestions?
orderedaround the whole try/catch?throwexecuted inside aorderedregion must cause execution to resume within the same ordered region, and the same thread that threw the exception must catch it."std::stringis not a good idea. Moreover, you should catch object by reference so to avoid necessary copies in this case. Catching an exception from a parallel section seems a red flag to me (I highly doubt this is supported), independently of the ordered construct. Finally, a parallel for with only an ordered construct is useless performance wise (no actual parallelism). Please note that usingnum_threads(10)is also a bad idea in real-world application (OK for a prototype).const &(const reference).return.