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

Make ConsoleStream/GeneralRegistry thread-safe #27996

Merged
merged 3 commits into from
Jun 27, 2024
Merged
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion framework/include/interfaces/SolutionInvalidInterface.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class SolutionInvalidInterface
SolutionInvalidInterface(MooseObject * const moose_object);

protected:
void flagInvalidSolutionInternal(InvalidSolutionID _invalid_solution_id) const;
void flagInvalidSolutionInternal(InvalidSolutionID invalid_solution_id) const;

// Register invalid solution with a message
InvalidSolutionID registerInvalidSolutionInternal(const std::string & message) const;
Expand Down
5 changes: 3 additions & 2 deletions framework/include/outputs/ConsoleStream.h
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,10 @@ class ConsoleStream
/// of something in AutomaticMortarGeneration that requires
/// this to be trivially copyable.
mutable std::shared_ptr<std::ostringstream> _oss;
};

extern std::mutex _stream_mutex;
/// Mutex to prevent concurrent read/writes, write/writes
static std::mutex _stream_mutex;
};

template <typename StreamType>
const ConsoleStream &
Expand Down
17 changes: 2 additions & 15 deletions framework/include/utils/GeneralRegistry.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

#include <unordered_map>
#include <mutex>
#include <vector>
#include <deque>

#include "MooseError.h"

Expand Down Expand Up @@ -63,18 +63,13 @@ class GeneralRegistry
template <typename CreateItem>
std::size_t registerItem(const Key & key, CreateItem & create_item);

/**
* Reserves \p size entires in the item vector, _id_to_item
*/
void reserve(const std::size_t size);

/// The name of this registry; used in error handling
const std::string _name;

/// Map of keys to IDs
std::unordered_map<Key, std::size_t, KeyHash> _key_to_id;
/// Vector of IDs to Items
std::vector<Item> _id_to_item;
std::deque<Item> _id_to_item;

/// Mutex for locking access to _key_to_id
/// NOTE: These can be changed to shared_mutexes once we get C++17
Expand Down Expand Up @@ -160,11 +155,3 @@ GeneralRegistry<Key, Item, KeyHash>::registerItem(const Key & key, CreateItem &
_id_to_item.emplace_back(std::move(create_item(id)));
return id;
}

template <class Key, class Item, class KeyHash>
void
GeneralRegistry<Key, Item, KeyHash>::reserve(const std::size_t size)
{
std::lock_guard<std::mutex> lock(_id_to_item_mutex);
_id_to_item.reserve(size);
}
6 changes: 3 additions & 3 deletions framework/src/interfaces/SolutionInvalidInterface.C
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@ SolutionInvalidInterface::SolutionInvalidInterface(MooseObject * const moose_obj

/// Set solution invalid mark for the given solution ID
void
SolutionInvalidInterface::flagInvalidSolutionInternal(InvalidSolutionID _invalid_solution_id) const
SolutionInvalidInterface::flagInvalidSolutionInternal(InvalidSolutionID invalid_solution_id) const
{
auto & solution_invalidity = _si_moose_object.getMooseApp().solutionInvalidity();
if (_si_problem.immediatelyPrintInvalidSolution())
solution_invalidity.printDebug(_invalid_solution_id);
return solution_invalidity.flagInvalidSolutionInternal(_invalid_solution_id);
solution_invalidity.printDebug(invalid_solution_id);
return solution_invalidity.flagInvalidSolutionInternal(invalid_solution_id);
}

InvalidSolutionID
Expand Down
6 changes: 2 additions & 4 deletions framework/src/outputs/ConsoleStream.C
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,17 @@
#include "MooseUtils.h"
#include "OutputWarehouse.h"

std::mutex _stream_mutex;
std::mutex ConsoleStream::_stream_mutex;

ConsoleStream::ConsoleStream(OutputWarehouse & output_warehouse)
: _output_warehouse(output_warehouse), _oss(std::make_shared<std::ostringstream>())
{
}

static std::mutex manip_mutex;

const ConsoleStream &
ConsoleStream::operator<<(const StandardEndLine & manip) const
{
const std::lock_guard<std::mutex> lock(manip_mutex);
const std::lock_guard<std::mutex> lock(_stream_mutex);

if (manip == (std::basic_ostream<char> & (*)(std::basic_ostream<char> &)) & std::endl)
(*_oss) << '\n';
Expand Down
4 changes: 0 additions & 4 deletions framework/src/utils_nonunity/PerfGraphRegistry.C
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,6 @@ getPerfGraphRegistry()
PerfGraphRegistry::PerfGraphRegistry()
: GeneralRegistry<std::string, PerfGraphSectionInfo>("PerfGraphRegistry")
{
// Reserve space so that re-allocation doesn't need to happen much
// This does not take much memory and, for most cases, will keep a single
// reallocation from happening
reserve(5000);
}

unsigned int
Expand Down