-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
06d94a5
commit 8b52602
Showing
11 changed files
with
146 additions
and
16 deletions.
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
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
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
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
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
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
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
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,43 @@ | ||
import Logging, LoggingExtras | ||
|
||
export MPILogger, MPIFileLogger | ||
|
||
""" | ||
MPILogger(context::AbstractCommsContext) | ||
MPILogger(iostream, context) | ||
Add a rank prefix before log messages. | ||
Outputs to `stdout` if no IOStream is given. | ||
""" | ||
MPILogger(ctx::AbstractCommsContext) = MPILogger(stdout, ctx) | ||
|
||
function MPILogger(iostream, ctx::AbstractCommsContext) | ||
pid = mypid(ctx) | ||
|
||
function format_log(io, log) | ||
print(io, "[P$pid] ") | ||
println(io, " $(log.level): $(log.message)") | ||
end | ||
|
||
return LoggingExtras.FormatLogger(format_log, iostream) | ||
end | ||
|
||
""" | ||
MPIFileLogger(context, log_dir) | ||
Log MPI ranks to different files within the `log_dir`. | ||
""" | ||
function MPIFileLogger( | ||
ctx::AbstractCommsContext, | ||
log_dir::AbstractString; | ||
min_level::Logging.LogLevel = Logging.Info, | ||
) | ||
rank = mypid(ctx) | ||
!isdir(log_dir) && mkdir(log_dir) | ||
return LoggingExtras.FileLogger( | ||
joinpath(log_dir, "rank_$rank.log"); | ||
append = true, | ||
always_flush = true, | ||
) | ||
end |
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 |
---|---|---|
@@ -1,5 +1,6 @@ | ||
[deps] | ||
ClimaComms = "3a4d1b5c-c61d-41fd-a00a-5873ba7a1b0d" | ||
Logging = "56ddb016-857b-54e1-b83d-db4d58db5568" | ||
MPI = "da04e1cc-30fd-572f-bb4f-1f8673147195" | ||
SafeTestsets = "1bc83da4-3b8d-516f-aca4-4fe02f6d838f" | ||
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40" |
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,40 @@ | ||
import ClimaComms, MPI | ||
using Logging, Test | ||
|
||
ctx = ClimaComms.context() | ||
(mypid, nprocs) = ClimaComms.init(ctx) | ||
|
||
@testset "MPIFileLogger" begin | ||
log_dir = mktempdir() | ||
logger = ClimaComms.MPIFileLogger(ctx, log_dir) | ||
with_logger(logger) do | ||
test_str = "Test message from rank $mypid" | ||
@info test_str | ||
log_content = read(joinpath(log_dir, "rank_$mypid.log"), String) | ||
@test occursin(test_str, log_content) | ||
end | ||
end | ||
|
||
@testset "MPILogger" begin | ||
io = IOBuffer() | ||
logger = ClimaComms.MPILogger(io, ctx) | ||
with_logger(logger) do | ||
@info "smoke test" | ||
end | ||
str = String(take!(io)) | ||
@test contains(str, "[P$mypid] Info: smoke test\n") | ||
|
||
# Test with file IOStream | ||
test_filename, io = mktemp() | ||
logger = ClimaComms.MPILogger(io, ctx) | ||
with_logger(logger) do | ||
test_str = "Test message from rank $mypid" | ||
@info test_str | ||
flush(io) | ||
close(io) | ||
|
||
log_content = read(test_filename, String) | ||
@test occursin(test_str, log_content) | ||
@test occursin(test_str, log_content) | ||
end | ||
end |
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