diff --git a/ql/methods/lattices/lattice.hpp b/ql/methods/lattices/lattice.hpp index e549570ac84..de42b5d19b3 100644 --- a/ql/methods/lattices/lattice.hpp +++ b/ql/methods/lattices/lattice.hpp @@ -166,8 +166,13 @@ namespace QuantLib { template void TreeLattice::stepback(Size i, const Array& values, Array& newValues) const { - #pragma omp parallel for - for (long j=0; j<(long)this->impl().size(i); j++) { + // Only parallelise when the tree is wide enough to amortise + // OMP fork/join overhead (~2-5 us). num_threads caps thread + // participation so each thread gets >= 256 iterations. + #pragma omp parallel for \ + if(!omp_in_parallel() && this->impl().size(i) >= 1024) \ + num_threads((int)(this->impl().size(i) / 256)) + for (Size j=0; jimpl().size(i); j++) { Real value = 0.0; for (Size l=0; limpl().probability(i,j,l) *