Skip to content

Commit

Permalink
Improved test with multiple threads
Browse files Browse the repository at this point in the history
In the previous version, an error leading to an exception thrown in a thread
was reported as a crash.

With this fix, errors (per thread) are stored and reported in the output.
This makes the possible test failure easier to interpret.
In particular, the failure discussed in #990 in rt-make-threads
should be now visible as a numerical error.
  • Loading branch information
GiovanniBussi committed Nov 29, 2023
1 parent 82941c7 commit 41e9dd5
Showing 1 changed file with 30 additions and 3 deletions.
33 changes: 30 additions & 3 deletions regtest/basic/rt-make-threads/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,50 @@
using namespace PLMD;

void run(std::ostream & os, const std::string & name,std::function<void(int)> f,unsigned nthreads=4,unsigned nrepeats=10){

// vector containing possible error messages from threads
std::vector<std::string> msgs(nthreads);

// wrapper function that catch exceptions and store their messages
auto g=[&](int i){
try {
f(i);
} catch(const std::exception& e) {
char buffer[1024];
std::sprintf(buffer,"(thread %d)\n",i);
msgs[i]=std::string(buffer)+e.what();
}
};

os<<"Test "<<name<<" with OpenMP...\n";
{
for(unsigned i=0;i<nrepeats;i++) {
#if defined(_OPENMP)
#pragma omp parallel num_threads(nthreads)
f(omp_get_thread_num());
g(omp_get_thread_num());
#else
f(0);
g(0);
#endif
std::string msg;
for(unsigned j=0;j<nthreads;j++) if(msgs[j].length()>0) msg+=msgs[j];
// one could propagate the exception with plumed_error()<<msg;
// I instead just write the error on the os file, which will be shown in diff
// This allows running both tests (openmp here and threads below)
if(msg.length()>0) os<<"failed with error "<<msg;
msg.clear();
}
}
os<<"OK"<<std::endl;

os<<"Test "<<name<<" with C++11 threads...\n";
for(unsigned i=0;i<nrepeats;i++) {
std::vector<std::thread> threads;
for(unsigned j=0;j<nthreads;j++) threads.emplace_back(std::thread(f,j));
for(unsigned j=0;j<nthreads;j++) threads.emplace_back(std::thread(g,j));
for(unsigned j=0;j<nthreads;j++) threads[j].join();
std::string msg;
for(unsigned j=0;j<nthreads;j++) if(msgs[j].length()>0) msg+=msgs[j];
if(msg.length()>0) os<<"failed with error "<<msg;
msg.clear();
}
os<<"OK"<<std::endl;
}
Expand Down

1 comment on commit 41e9dd5

@PlumedBot
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Found broken examples in automatic/a-masterclass-22-09.txt
Found broken examples in automatic/a-masterclass-22-11.txt
Found broken examples in automatic/a-masterclass-22-12.txt
Found broken examples in automatic/performance-optimization.txt
Found broken examples in automatic/a-trieste-6.txt
Found broken examples in automatic/munster.txt
Found broken examples in automatic/ANN.tmp
Found broken examples in automatic/EDS.tmp
Found broken examples in automatic/EMMI.tmp
Found broken examples in automatic/ENVIRONMENTSIMILARITY.tmp
Found broken examples in automatic/FOURIER_TRANSFORM.tmp
Found broken examples in automatic/FUNCPATHGENERAL.tmp
Found broken examples in automatic/FUNCPATHMSD.tmp
Found broken examples in automatic/FUNNEL.tmp
Found broken examples in automatic/FUNNEL_PS.tmp
Found broken examples in automatic/GHBFIX.tmp
Found broken examples in automatic/INCLUDE.tmp
Found broken examples in automatic/MAZE_MEMETIC_SAMPLING.tmp
Found broken examples in automatic/MAZE_OPTIMIZER_BIAS.tmp
Found broken examples in automatic/MAZE_RANDOM_ACCELERATION_MD.tmp
Found broken examples in automatic/MAZE_RANDOM_WALK.tmp
Found broken examples in automatic/MAZE_SIMULATED_ANNEALING.tmp
Found broken examples in automatic/MAZE_STEERED_MD.tmp
Found broken examples in automatic/PIV.tmp
Found broken examples in automatic/PLUMED.tmp
Found broken examples in MiscelaneousPP.md

Please sign in to comment.