diff --git a/kratos/utilities/parallel_utilities.h b/kratos/utilities/parallel_utilities.h index 42ffffbd13af..8da20f4510f1 100644 --- a/kratos/utilities/parallel_utilities.h +++ b/kratos/utilities/parallel_utilities.h @@ -4,8 +4,8 @@ // _|\_\_| \__,_|\__|\___/ ____/ // Multi-Physics // -// License: BSD License -// Kratos default license: kratos/license.txt +// License: BSD License +// Kratos default license: kratos/license.txt // // Main authors: Riccardo Rossi // Denis Demidov @@ -67,10 +67,6 @@ namespace Kratos class KRATOS_API(KRATOS_CORE) ParallelUtilities { public: - ///@name Life Cycle - ///@{ - - ///@} ///@name Operations ///@{ @@ -281,6 +277,41 @@ class BlockPartition return global_reducer.GetValue(); } + template ,void>,bool> = true> + void for_each(const TThreadLocalStorage& rTls, + TFunction&& rFunction, + TThreadLocalReduction&& rTLSReducer) + { + // Check type requirements + static_assert(std::is_copy_constructible::value, "TThreadLocalStorage must be copy constructible!"); + + KRATOS_PREPARE_CATCH_THREAD_EXCEPTION + + #pragma omp parallel + { + TThreadLocalStorage tls(rTls); + + #pragma omp for + for (int i=0; i mBlockPartition; @@ -438,6 +469,29 @@ template (v.begin(), v.end(), tls, std::forward(func)); } +template ,void>,bool> = true> +void block_for_each(TContainer&& rContainer, + const TThreadLocalStorage& rTls, + TFunction&& rFunction, + TThreadLocalReduction&& rReduction) +{ + using ContainerType = std::remove_reference_t; + using iterator_type = std::conditional_t< + std::is_const_v, + typename ContainerType::const_iterator, + typename ContainerType::iterator + >; + return BlockPartition(rContainer.begin(), rContainer.end()).for_each( + rTls, + std::forward(rFunction), + std::forward(rReduction) + ); +} + //*********************************************************************************** //*********************************************************************************** //***********************************************************************************