Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix data race in aggregateLikelihood #411

Merged
merged 1 commit into from
Nov 19, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion src/parpeamici/multiConditionProblem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include <cassert>
#include <cstring>
#include <ctime>
#include <mutex>
#include <numeric>
#include <utility>

Expand Down Expand Up @@ -719,6 +720,9 @@ int AmiciSummedGradientFunction::runSimulations(
std::vector<int> const& dataIndices, Logger *logger, double *cpuTime) const {

int errors = 0;
// Mutex to protect likelihood and gradient that are potentially updated
// by multiple threads
std::mutex mutex;

auto parameterVector = std::vector<double>(
optimizationParameters.begin(),
Expand All @@ -732,7 +736,10 @@ int AmiciSummedGradientFunction::runSimulations(
: amici::SensitivityOrder::none,
dataIndices,
[&nllh, &objectiveFunctionGradient, &simulationTimeSec,
&optimizationParameters, &errors, this](JobData *job, int /*jobIdx*/) {
&optimizationParameters, &errors, &mutex, this](JobData *job, int /*jobIdx*/) {
// protect shared variables llh, gradient, simulationTimeSec
std::lock_guard<std::mutex> lock(mutex);

errors += this->aggregateLikelihood(*job,
nllh,
objectiveFunctionGradient,
Expand Down