Skip to content

Commit

Permalink
Add file logging support
Browse files Browse the repository at this point in the history
  • Loading branch information
nefrathenrici committed Jan 28, 2025
1 parent ebf8e24 commit 1118252
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 45 deletions.
7 changes: 7 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@ See the [documentation](https://clima.github.io/ClimaAtmos.jl/dev/contributor_g

### Features

### File Logging

`ClimaAtmos` now supports logging to stdout and file simultaneously using
`ClimaComms.FileLogger`. This option is enabled by default. To disable,
run with `log_to_file = false`. See [ClimaComms documentation](https://clima.github.io/ClimaComms.jl/dev/logging/)
for more background on logging.

### Read CO2 from file

`ClimaAtmos` now support using data from the Mauna Loa CO2 measurements to set
Expand Down
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ ArgParse = "1"
ArtifactWrappers = "0.2"
Artifacts = "1"
AtmosphericProfilesLibrary = "0.1.7"
ClimaComms = "0.6.4"
ClimaComms = "0.6.6"
ClimaCore = "0.14.19"
ClimaDiagnostics = "0.2.4"
ClimaParams = "0.10.17"
Expand Down
3 changes: 3 additions & 0 deletions config/default_configs/default_config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -345,3 +345,6 @@ updraft_number:
radiation_reset_rng_seed:
help: "Reset the RNG seed before calling RRTGMP to a known value (the timestep number). When modeling cloud optics, RRTGMP uses a random number generator. Resetting the seed every time RRTGMP is called to a deterministic value ensures that the simulation is fully reproducible and can be restarted in a reproducible way. Disable this option when running production runs. Please note that this flag is only used for `AllSkyRadiation` and `AllSkyRadiationWithClearSkyDiagnostics` radiation modes."
value: false
log_to_file:
help: "Log to stdout and file simultaneously. The log file is saved within the output directory"
value: true
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ diagnostics:
- short_name: [pfull, wa, va, rv]
period: 1days
writer: h5
log_to_file: false
12 changes: 6 additions & 6 deletions examples/Manifest.toml
Original file line number Diff line number Diff line change
Expand Up @@ -345,9 +345,9 @@ version = "0.28.2"

[[deps.ClimaComms]]
deps = ["Adapt", "Logging", "LoggingExtras"]
git-tree-sha1 = "d29ca07d91e6ca6c0a3649ad6cf37f2634951fd3"
git-tree-sha1 = "40ad44466dfe0092e18c04bc02c849fe8e095d30"
uuid = "3a4d1b5c-c61d-41fd-a00a-5873ba7a1b0d"
version = "0.6.5"
version = "0.6.6"
weakdeps = ["CUDA", "MPI"]

[deps.ClimaComms.extensions]
Expand Down Expand Up @@ -1229,9 +1229,9 @@ version = "1.0.0"

[[deps.JET]]
deps = ["CodeTracking", "InteractiveUtils", "JuliaInterpreter", "LoweredCodeUtils", "MacroTools", "Pkg", "PrecompileTools", "Preferences", "Test"]
git-tree-sha1 = "5c5ac91e775b585864015c5c1703cee283071a47"
git-tree-sha1 = "24bdbf3ef611b69d1f5ef9331e69b6007152989e"
uuid = "c3a54625-cd67-489e-a8e7-0a5a0ff4e31b"
version = "0.9.12"
version = "0.9.14"

[deps.JET.extensions]
JETCthulhuExt = "Cthulhu"
Expand Down Expand Up @@ -2576,9 +2576,9 @@ version = "1.1.42+0"

[[deps.XZ_jll]]
deps = ["Artifacts", "JLLWrappers", "Libdl"]
git-tree-sha1 = "beef98d5aad604d9e7d60b2ece5181f7888e2fd6"
git-tree-sha1 = "56c6604ec8b2d82cc4cfe01aa03b00426aac7e1f"
uuid = "ffd25f8a-64ca-5728-b0f7-c24cf3aae800"
version = "5.6.4+0"
version = "5.6.4+1"

[[deps.Xorg_libX11_jll]]
deps = ["Artifacts", "JLLWrappers", "Libdl", "Xorg_libxcb_jll", "Xorg_xtrans_jll"]
Expand Down
39 changes: 16 additions & 23 deletions src/solver/type_getters.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import ClimaAtmos as CA
import LinearAlgebra
import ClimaCore.Fields
import ClimaTimeSteppers as CTS
import Logging

import ClimaDiagnostics

Expand Down Expand Up @@ -502,7 +503,7 @@ function auto_detect_restart_file(
end

function get_sim_info(config::AtmosConfig)
(; parsed_args) = config
(; comms_ctx, parsed_args) = config
FT = eltype(config)

(; job_id) = config
Expand Down Expand Up @@ -531,9 +532,21 @@ function get_sim_info(config::AtmosConfig)

output_dir = OutputPathGenerator.generate_output_path(
base_output_dir;
context = config.comms_ctx,
context = comms_ctx,
style = output_dir_style,
)
if parsed_args["log_to_file"]
@info "Logging to $output_dir/output.log"
logger = ClimaComms.FileLogger(comms_ctx, output_dir)
Logging.global_logger(logger)
end
@info "Running on $(nameof(typeof(ClimaComms.device(comms_ctx))))"
if comms_ctx isa ClimaComms.SingletonCommsContext
@info "Setting up single-process ClimaAtmos run"
else
@info "Setting up distributed ClimaAtmos run" nprocs =
ClimaComms.nprocs(comms_ctx)
end

isnothing(restart_file) ||
@info "Restarting simulation from file $restart_file"
Expand Down Expand Up @@ -627,30 +640,10 @@ function get_comms_context(parsed_args)
return comms_ctx
end

"""
silence_non_root_processes(comms_ctx)
Set the logging behavior based on the process rank within the given communication context `comms_ctx`.
If the process is the root process, logging is set to display messages to the console with `Info` level.
For all other processes, logging is silenced by setting it to a `NullLogger`.
# Arguments
- `comms_ctx`: The communication context used to determine the rank of the process.
"""
function silence_non_root_processes(comms_ctx)
# Set logging to only display for the root process
if ClimaComms.iamroot(comms_ctx)
Logging.global_logger(Logging.ConsoleLogger(stderr, Logging.Info))
else
Logging.global_logger(Logging.NullLogger())
end
end

function get_simulation(config::AtmosConfig)
sim_info = get_sim_info(config)
params = ClimaAtmosParameters(config)
atmos = get_atmos(config, params)
sim_info = get_sim_info(config)
job_id = sim_info.job_id
output_dir = sim_info.output_dir
@info "Simulation info" job_id output_dir
Expand Down
15 changes: 0 additions & 15 deletions src/solver/types.jl
Original file line number Diff line number Diff line change
Expand Up @@ -724,22 +724,7 @@ function AtmosConfig(
override_file = CP.merge_toml_files(config["toml"]),
)
comms_ctx = isnothing(comms_ctx) ? get_comms_context(config) : comms_ctx
device = ClimaComms.device(comms_ctx)
silence_non_root_processes(comms_ctx)
@info "Running on $(nameof(typeof(device)))"
if comms_ctx isa ClimaComms.SingletonCommsContext
@info "Setting up single-process ClimaAtmos run"
else
@info "Setting up distributed ClimaAtmos run" nprocs =
ClimaComms.nprocs(comms_ctx)
end

config = config_with_resolved_and_acquired_artifacts(config, comms_ctx)
if device isa ClimaComms.CPUMultiThreaded
@info "Running ClimaCore in threaded mode, with $(Threads.nthreads()) threads"
else
@info "Running ClimaCore in unthreaded mode"
end

isempty(job_id) &&
@warn "`job_id` is empty and likely not passed to AtmosConfig"
Expand Down

0 comments on commit 1118252

Please sign in to comment.