From af90dd3663be70cf783c5f4c594d6ccd5d30b7dd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Johannes=20Bl=C3=BChdorn?= Date: Mon, 29 Jul 2024 21:14:04 +0200 Subject: [PATCH] Use a custom thread id to resolve ambiguity. --- .../tools/helpers/externalFunctionHelper.hpp | 7 +++++-- .../parallel/openmp/openMPThreadInformation.hpp | 16 +++++++++++++++- 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/include/codi/tools/helpers/externalFunctionHelper.hpp b/include/codi/tools/helpers/externalFunctionHelper.hpp index 3b1eb30f..3607c0e9 100644 --- a/include/codi/tools/helpers/externalFunctionHelper.hpp +++ b/include/codi/tools/helpers/externalFunctionHelper.hpp @@ -599,8 +599,11 @@ namespace codi { Synchronization::synchronize(); // Push the delete handle on at most one thread's tape. - typename ExternalFunction::DeleteFunction delFunc = - 0 == ThreadInformation::getThreadId() ? EvalData::delFunc : nullptr; + typename ExternalFunction::DeleteFunction delFunc = nullptr; + Synchronization::serialize([&]() { + delFunc = EvalData::delFunc; + }); + Type::getTape().pushExternalFunction(ExternalFunction::create( EvalData::evalRevFuncStatic, data, delFunc, EvalData::evalForwFuncStatic, EvalData::evalPrimFuncStatic)); diff --git a/include/codi/tools/parallel/openmp/openMPThreadInformation.hpp b/include/codi/tools/parallel/openmp/openMPThreadInformation.hpp index d8c03d80..01203024 100644 --- a/include/codi/tools/parallel/openmp/openMPThreadInformation.hpp +++ b/include/codi/tools/parallel/openmp/openMPThreadInformation.hpp @@ -37,6 +37,7 @@ #include #include "../threadInformationInterface.hpp" +#include "openMPAtomic.hpp" /** \copydoc codi::Namespace */ namespace codi { @@ -48,13 +49,26 @@ namespace codi { public: /// \copydoc ThreadInformationInterface::getMaxThreads() + ///

Implementation: Limit applies to all threads, also those due to nesting. static CODI_INLINE int getMaxThreads() { return 512; } /// \copydoc ThreadInformationInterface::getThreadId() + ///

Implementation: Returns custom IDs to account for nesting, in particular not omp_get_thread_num(). static CODI_INLINE int getThreadId() { - return omp_get_thread_num(); + static OpenMPAtomic nextThreadId = 0; + + static int myThreadId = -1; + CODI_OMP_THREADPRIVATE(myThreadId) + + if (myThreadId == -1) { + myThreadId = nextThreadId++; + } + + codiAssert(myThreadId < getMaxThreads()); + + return myThreadId; } }; }