-
Notifications
You must be signed in to change notification settings - Fork 244
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
[Core] Add Process for Flushing Streams #12154
Open
matekelemen
wants to merge
1
commit into
master
Choose a base branch
from
core/flush-stream-process
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+103
−0
Open
Changes from all commits
Commits
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 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 |
---|---|---|
@@ -0,0 +1,103 @@ | ||
# --- Kratos Imports --- | ||
import KratosMultiphysics | ||
|
||
# --- STD Imports --- | ||
import sys | ||
import time | ||
|
||
|
||
class FlushStreamsProcess(KratosMultiphysics.Process): | ||
""" @brief Flush output streams at a given frequency. | ||
@param interval interval between forced flushes in seconds. | ||
@param stdout controls whether stdout is flushed. | ||
@param stderr controls whether stderr is flushed. | ||
@param stages list of process stages to flush at. Options are | ||
- Initialize | ||
- BeforeSolutionLoop | ||
- InitializeSolutionStep | ||
- FinalizeSolutionStep | ||
- BeforeOutputStep | ||
- AfterOutputStep | ||
- Finalize | ||
@ingroup KratosCore | ||
""" | ||
|
||
def __init__(self, | ||
_: KratosMultiphysics.Model, | ||
parameters: KratosMultiphysics.Parameters): | ||
super().__init__() | ||
parameters.ValidateAndAssignDefaults(self.GetDefaultParameters()) | ||
self.__buffer_interval: float = parameters["interval"].GetDouble() | ||
self.__flush_stdout: bool = parameters["stdout"].GetBool() | ||
self.__flush_stderr: bool = parameters["stderr"].GetBool() | ||
self.__stages: "set[str]" = set() | ||
self.__last_flush: float = time.time() | ||
|
||
# Parse trigger stages | ||
admissible_stages: "set[str]" = set(["Initialize", | ||
"BeforeSolutionLoop", | ||
"InitializeSolutionStep", | ||
"FinalizeSolutionStep", | ||
"BeforeOutputStep", | ||
"AfterOutputStep", | ||
"Finalize"]) | ||
|
||
for stage_parameter in parameters["stages"].values(): | ||
stage_name = stage_parameter.GetString() | ||
if stage_name not in admissible_stages: | ||
raise ValueError(f"invalid stage {stage_name}. Options: {' '.join(admissible_stages)}") | ||
self.__stages.add(stage_name) | ||
|
||
def Execute(self) -> None: | ||
current_time = time.time() | ||
if self.__buffer_interval <= current_time - self.__last_flush: | ||
self.__Flush() | ||
self.__last_flush = current_time | ||
|
||
def ExecuteInitialize(self) -> None: | ||
if "Initialize" in self.__stages: | ||
self.Execute() | ||
|
||
def ExecuteBeforeSolutionLoop(self) -> None: | ||
if "BeforeSolutionLoop" in self.__stages: | ||
self.Execute() | ||
|
||
def ExecuteInitializeSolutionStep(self) -> None: | ||
if "InitializeSolutionStep" in self.__stages: | ||
self.Execute() | ||
|
||
def ExecuteFinalizeSolutionStep(self) -> None: | ||
if "FinalizeSolutionStep" in self.__stages: | ||
self.Execute() | ||
|
||
def ExecuteBeforeOutputStep(self) -> None: | ||
if "BeforeOutputStep" in self.__stages: | ||
self.Execute() | ||
|
||
def ExecuteAfterOutputStep(self) -> None: | ||
if "AfterOutputStep" in self.__stages: | ||
self.Execute() | ||
|
||
def ExecuteFinalize(self) -> None: | ||
if "Finalize" in self.__stages: | ||
self.Execute() | ||
|
||
@classmethod | ||
def GetDefaultParameters(cls) -> KratosMultiphysics.Parameters: | ||
return KratosMultiphysics.Parameters("""{ | ||
"interval" : 0.0, | ||
"stdout" : true, | ||
"stderr" : true, | ||
"stages" : ["FinalizeSolutionStep"] | ||
}""") | ||
|
||
def __Flush(self) -> None: | ||
if self.__flush_stdout: | ||
sys.stdout.flush() | ||
if self.__flush_stderr: | ||
sys.stderr.flush() | ||
|
||
|
||
def Factory(parameters: KratosMultiphysics.Parameters, | ||
model: KratosMultiphysics.Model) -> FlushStreamsProcess: | ||
return FlushStreamsProcess(model, parameters["Parameters"]) |
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.
In my experience also a lot of stuff gets stuck in the logger:
Kratos/applications/CoSimulationApplication/python_scripts/co_simulation_analysis.py
Lines 99 to 102 in 245da2c
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.
wait, which stream is the logger using? I thought it was writing to stdout.
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.
It depends on how the Logger is constructed. It can use to have
std::out
, orstd::fstream
or other stream types as well. So using as @philbucher mentioned makes sense.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.
Kratos/applications/RANSApplication/python_scripts/formulations/utilities.py
Lines 335 to 346 in aacb6c1