- 
                Notifications
    You must be signed in to change notification settings 
- Fork 211
Allow multiple callbacks on same component #6334
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
          
     Merged
      
      
            nilsdeppe
  merged 3 commits into
  sxs-collaboration:develop
from
knelli2:cache_callback_fix
  
      
      
   
  Oct 23, 2024 
      
    
  
     Merged
                    Changes from all commits
      Commits
    
    
            Show all changes
          
          
            3 commits
          
        
        Select commit
          Hold shift + click to select a range
      
      
    File filter
Filter by extension
Conversations
          Failed to load comments.   
        
        
          
      Loading
        
  Jump to
        
          Jump to file
        
      
      
          Failed to load files.   
        
        
          
      Loading
        
  Diff view
Diff view
There are no files selected for viewing
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
|  | @@ -28,6 +28,7 @@ | |
| #include "Parallel/ParallelComponentHelpers.hpp" | ||
| #include "Parallel/ResourceInfo.hpp" | ||
| #include "Parallel/Tags/ResourceInfo.hpp" | ||
| #include "Utilities/Algorithm.hpp" | ||
| #include "Utilities/ErrorHandling/Assert.hpp" | ||
| #include "Utilities/ErrorHandling/Error.hpp" | ||
| #include "Utilities/Gsl.hpp" | ||
|  | @@ -77,10 +78,10 @@ CREATE_GET_TYPE_ALIAS_OR_DEFAULT(component_being_mocked) | |
|  | ||
| template <typename... Tags> | ||
| auto make_mutable_cache_tag_storage(tuples::TaggedTuple<Tags...>&& input) { | ||
| return tuples::TaggedTuple<MutableCacheTag<Tags>...>( | ||
| std::make_tuple(std::move(tuples::get<Tags>(input)), | ||
| std::unordered_map<Parallel::ArrayComponentId, | ||
| std::unique_ptr<Callback>>{})...); | ||
| return tuples::TaggedTuple<MutableCacheTag<Tags>...>(std::make_tuple( | ||
| std::move(tuples::get<Tags>(input)), | ||
| std::unordered_map<Parallel::ArrayComponentId, | ||
| std::vector<std::unique_ptr<Callback>>>{})...); | ||
| } | ||
|  | ||
| template <typename ParallelComponent, typename ComponentList> | ||
|  | @@ -487,14 +488,34 @@ bool GlobalCache<Metavariables>::mutable_cache_item_is_ready( | |
| optional_callback->register_with_charm(); | ||
| // Second mutex is for vector of callbacks | ||
| std::mutex& mutex = tuples::get<MutexTag<tag>>(mutexes_).second; | ||
| const std::unique_ptr<Callback> clone_of_optional_callback = | ||
| optional_callback->get_clone(); | ||
| { | ||
| // Scoped for lock guard | ||
| const std::lock_guard<std::mutex> lock(mutex); | ||
| std::unordered_map<Parallel::ArrayComponentId, std::unique_ptr<Callback>>& | ||
| callbacks = std::get<1>(tuples::get<tag>(mutable_global_cache_)); | ||
|  | ||
| if (callbacks.count(array_component_id) != 1) { | ||
| callbacks[array_component_id] = std::move(optional_callback); | ||
| std::unordered_map<Parallel::ArrayComponentId, | ||
| std::vector<std::unique_ptr<Callback>>>& callbacks = | ||
| std::get<1>(tuples::get<tag>(mutable_global_cache_)); | ||
|  | ||
| if (callbacks.contains(array_component_id)) { | ||
| // If this array component id already exists, we don't want to add | ||
| // multiple of the same callback, so we loop over the existing callbacks | ||
| // and only if none of the existing callbacks are equal to the optional | ||
| // callback do we move the optional callback into the vector | ||
| auto& vec_callbacks = callbacks.at(array_component_id); | ||
| if (alg::none_of(vec_callbacks, | ||
| [&](const std::unique_ptr<Callback>& local_callback) { | ||
| return local_callback->is_equal_to( | ||
| *optional_callback); | ||
| })) { | ||
| vec_callbacks.emplace_back(std::move(optional_callback)); | ||
| } | ||
| } else { | ||
| // If we don't have this array component id, then we create the vector | ||
| // and move the optional callback into the vector | ||
| callbacks[array_component_id] = | ||
| std::vector<std::unique_ptr<Callback>>(1); | ||
| callbacks.at(array_component_id)[0] = std::move(optional_callback); | ||
| } | ||
| } | ||
|  | ||
|  | @@ -531,10 +552,26 @@ bool GlobalCache<Metavariables>::mutable_cache_item_is_ready( | |
| const bool cache_item_is_ready = not callback_was_registered(); | ||
| if (cache_item_is_ready) { | ||
| const std::lock_guard<std::mutex> lock(mutex); | ||
| std::unordered_map<Parallel::ArrayComponentId, std::unique_ptr<Callback>>& | ||
| callbacks = std::get<1>(tuples::get<tag>(mutable_global_cache_)); | ||
|  | ||
| callbacks.erase(array_component_id); | ||
| std::unordered_map<Parallel::ArrayComponentId, | ||
| std::vector<std::unique_ptr<Callback>>>& callbacks = | ||
| std::get<1>(tuples::get<tag>(mutable_global_cache_)); | ||
|  | ||
| // It's possible that no new callbacks were registered, so make sure this | ||
| // array component id still has callbacks before trying to remove them. | ||
| if (callbacks.contains(array_component_id)) { | ||
| // If this callback was a duplicate, we'll have to search through all | ||
| // callbacks to determine which to remove. If it wasn't a duplicate, | ||
| // then it'll just be the last callback in the vector. | ||
| auto& vec_callbacks = callbacks.at(array_component_id); | ||
| std::erase_if(vec_callbacks, | ||
| [&clone_of_optional_callback](const auto& t) { | ||
| return t->is_equal_to(*clone_of_optional_callback); | ||
| }); | ||
|  | ||
| if (callbacks.at(array_component_id).empty()) { | ||
| callbacks.erase(array_component_id); | ||
| 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. Check if the vector is empty after removing the element, instead of before. | ||
| } | ||
| } | ||
| } | ||
|  | ||
| return cache_item_is_ready; | ||
|  | @@ -573,7 +610,8 @@ void GlobalCache<Metavariables>::mutate(const std::tuple<Args...>& args) { | |
| // Therefore, after locking it, we std::move the map of callbacks into a | ||
| // temporary map, clear the original map, and invoke the callbacks in the | ||
| // temporary map. | ||
| std::unordered_map<Parallel::ArrayComponentId, std::unique_ptr<Callback>> | ||
| std::unordered_map<Parallel::ArrayComponentId, | ||
| std::vector<std::unique_ptr<Callback>>> | ||
| callbacks{}; | ||
| // Second mutex is for map of callbacks | ||
| std::mutex& mutex = tuples::get<MutexTag<tag>>(mutexes_).second; | ||
|  | @@ -587,9 +625,10 @@ void GlobalCache<Metavariables>::mutate(const std::tuple<Args...>& args) { | |
| // Invoke the callbacks. Any new callbacks that are added to the | ||
| // list (if a callback calls mutable_cache_item_is_ready) will be | ||
| // saved and will not be invoked here. | ||
| for (auto& [array_component_id, callback] : callbacks) { | ||
| (void)array_component_id; | ||
| callback->invoke(); | ||
| for (auto& [array_component_id, vec_callbacks] : callbacks) { | ||
| for (auto& callback : vec_callbacks) { | ||
| callback->invoke(); | ||
| } | ||
| } | ||
| } | ||
|  | ||
|  | ||
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
      
      Oops, something went wrong.
        
    
  
      
      Oops, something went wrong.
        
    
  
  Add this suggestion to a batch that can be applied as a single commit.
  This suggestion is invalid because no changes were made to the code.
  Suggestions cannot be applied while the pull request is closed.
  Suggestions cannot be applied while viewing a subset of changes.
  Only one suggestion per line can be applied in a batch.
  Add this suggestion to a batch that can be applied as a single commit.
  Applying suggestions on deleted lines is not supported.
  You must change the existing code in this line in order to create a valid suggestion.
  Outdated suggestions cannot be applied.
  This suggestion has been applied or marked resolved.
  Suggestions cannot be applied from pending reviews.
  Suggestions cannot be applied on multi-line comments.
  Suggestions cannot be applied while the pull request is queued to merge.
  Suggestion cannot be applied right now. Please check back later.
  
    
  
    
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.
I'm a bit worried about silently ignoring entries. What types typically occur that cause problems?
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.
The problematic types were proxies and I didn't want to deal with how to evaluate equivalence of those
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.
OK. charmplusplus/charm#3848 is probably the real fix, for what it's worth, but we can't wait for that.