Skip to content

Commit

Permalink
Use a custom thread id to resolve ambiguity.
Browse files Browse the repository at this point in the history
  • Loading branch information
jblueh committed Jul 29, 2024
1 parent 89cb4c4 commit af90dd3
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
7 changes: 5 additions & 2 deletions include/codi/tools/helpers/externalFunctionHelper.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -599,8 +599,11 @@ namespace codi {
Synchronization::synchronize();

// Push the delete handle on at most one thread's tape.
typename ExternalFunction<Tape>::DeleteFunction delFunc =
0 == ThreadInformation::getThreadId() ? EvalData::delFunc : nullptr;
typename ExternalFunction<Tape>::DeleteFunction delFunc = nullptr;
Synchronization::serialize([&]() {
delFunc = EvalData::delFunc;
});

Type::getTape().pushExternalFunction(ExternalFunction<Tape>::create(
EvalData::evalRevFuncStatic, data, delFunc, EvalData::evalForwFuncStatic, EvalData::evalPrimFuncStatic));

Expand Down
16 changes: 15 additions & 1 deletion include/codi/tools/parallel/openmp/openMPThreadInformation.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
#include <omp.h>

#include "../threadInformationInterface.hpp"
#include "openMPAtomic.hpp"

/** \copydoc codi::Namespace */
namespace codi {
Expand All @@ -48,13 +49,26 @@ namespace codi {
public:

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

/// \copydoc ThreadInformationInterface::getThreadId()
/// <br><br> 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<int> nextThreadId = 0;

static int myThreadId = -1;
CODI_OMP_THREADPRIVATE(myThreadId)

if (myThreadId == -1) {
myThreadId = nextThreadId++;
}

codiAssert(myThreadId < getMaxThreads());

return myThreadId;
}
};
}

0 comments on commit af90dd3

Please sign in to comment.