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

[Core] Add Process for Flushing Streams #12154

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
103 changes: 103 additions & 0 deletions kratos/processes/flush_streams_process.py
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()
Comment on lines +95 to +98
Copy link
Member

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:

@staticmethod
def Flush():
sys.stdout.flush()
KM.Logger.Flush()

Copy link
Contributor Author

@matekelemen matekelemen Mar 6, 2024

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.

Copy link
Member

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, or std::fstream or other stream types as well. So using as @philbucher mentioned makes sense.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

def AddFileLoggerOutput(log_file_name):
file_logger = Kratos.FileLoggerOutput(log_file_name)
default_severity = Kratos.Logger.GetDefaultOutput().GetSeverity()
Kratos.Logger.GetDefaultOutput().SetSeverity(Kratos.Logger.Severity.WARNING)
Kratos.Logger.AddOutput(file_logger)
return default_severity, file_logger
def RemoveFileLoggerOutput(default_severity, file_logger):
Kratos.Logger.Flush()
Kratos.Logger.RemoveOutput(file_logger)
Kratos.Logger.GetDefaultOutput().SetSeverity(default_severity)



def Factory(parameters: KratosMultiphysics.Parameters,
model: KratosMultiphysics.Model) -> FlushStreamsProcess:
return FlushStreamsProcess(model, parameters["Parameters"])
Loading