-
Notifications
You must be signed in to change notification settings - Fork 196
mrregister: fix race condition in displacement update #3277
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
base: dev
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -23,9 +23,36 @@ | |
| #include "interp/linear.h" | ||
| #include "registration/warp/helpers.h" | ||
| #include "transform.h" | ||
| #include <mutex> | ||
|
|
||
| namespace MR::Registration::Warp { | ||
|
|
||
| class MaxNormSquaredFunctor { | ||
| public: | ||
| explicit MaxNormSquaredFunctor(default_type &overall_max_norm_squared) | ||
| : overall_max_norm_squared(overall_max_norm_squared), local_max_norm_squared(0.0) {} | ||
| MaxNormSquaredFunctor(const MaxNormSquaredFunctor &) = default; | ||
| MaxNormSquaredFunctor(MaxNormSquaredFunctor &&) = default; | ||
| MaxNormSquaredFunctor &operator=(const MaxNormSquaredFunctor &) = delete; | ||
| MaxNormSquaredFunctor &operator=(MaxNormSquaredFunctor &&) = delete; | ||
|
|
||
| ~MaxNormSquaredFunctor() { | ||
| std::lock_guard<std::mutex> lock(mutex); | ||
| overall_max_norm_squared = std::max(overall_max_norm_squared, local_max_norm_squared); | ||
| } | ||
|
|
||
| void operator()(Image<default_type> &update) { | ||
| const default_type norm_squared = Eigen::Vector3d(update.row(3)).squaredNorm(); | ||
| if (norm_squared > local_max_norm_squared) | ||
| local_max_norm_squared = norm_squared; | ||
| } | ||
|
|
||
| private: | ||
| default_type &overall_max_norm_squared; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. warning: member 'overall_max_norm_squared' of type 'default_type &' (aka 'double &') is a reference [cppcoreguidelines-avoid-const-or-ref-data-members] default_type &overall_max_norm_squared;
^
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I know we've used this approach throughout the code base, but I think it may have been criticised at some point. Is there something better, even if only for my own knowledge when I need to do similar in the future? My naive instinct would be |
||
| default_type local_max_norm_squared; | ||
| inline static std::mutex mutex; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. warning: variable 'mutex' is non-const and globally accessible, consider making it const [cppcoreguidelines-avoid-non-const-global-variables] inline static std::mutex mutex;
^ |
||
| }; | ||
|
|
||
| class ComposeLinearDeformKernel { | ||
| public: | ||
| ComposeLinearDeformKernel(const transform_type &transform) : transform(transform) {} | ||
|
|
@@ -161,13 +188,9 @@ FORCE_INLINE void update_displacement_scaling_and_squaring(Image<default_type> & | |
| const default_type step = 1.0) { | ||
| check_dimensions(input, output, 0, 3); | ||
|
|
||
| default_type max_norm = 0.0; | ||
| auto max_norm_func = [&max_norm](Image<default_type> &update) { | ||
| default_type norm = Eigen::Vector3d(update.row(3)).norm(); | ||
| if (norm > max_norm) | ||
| max_norm = norm; | ||
| }; | ||
| ThreadedLoop(update).run(max_norm_func, update); | ||
| default_type max_norm_squared = 0.0; | ||
| ThreadedLoop(update).run(MaxNormSquaredFunctor(max_norm_squared), update); | ||
| const default_type max_norm = std::sqrt(max_norm_squared); | ||
| default_type min_vox_size = | ||
| static_cast<default_type>(std::min(input.spacing(0), std::min(input.spacing(1), input.spacing(2)))); | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
warning: variable 'lock' of type 'std::lock_guardstd::mutex' can be declared 'const' [misc-const-correctness]