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

Compilation message with MPI extension #5

Merged
merged 12 commits into from
Jan 31, 2024
13 changes: 13 additions & 0 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,16 @@ name = "TrixiBase"
uuid = "9a0f1c46-06d5-4909-a5a3-ce25d3fa3284"
authors = ["Michael Schlottke-Lakemper <[email protected]>"]
version = "0.1.0"

[weakdeps]
MPI = "da04e1cc-30fd-572f-bb4f-1f8673147195"

[extensions]
TrixiBaseMPIExt = "MPI"

[compat]
MPI = "0.20"
julia = "1.8"

[extras]
MPI = "da04e1cc-30fd-572f-bb4f-1f8673147195"
20 changes: 20 additions & 0 deletions ext/TrixiBaseMPIExt.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Package extension for adding MPI-based features to TrixiBase.jl
module TrixiBaseMPIExt
sloede marked this conversation as resolved.
Show resolved Hide resolved

# Load package extension code on Julia v1.9 and newer
if isdefined(Base, :get_extension)
using MPI: MPI
end
import TrixiBase

# This is a really working version - assuming the same
# communication pattern etc. used in Trixi.jl.
function TrixiBase.mpi_isparallel(::Val{:MPIExt})
if MPI.Initialized()
return MPI.Comm_size(MPI.COMM_WORLD) > 1
else
return false
end
end

end
9 changes: 9 additions & 0 deletions src/trixi_include.jl
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ function trixi_include(mod::Module, elixir::AbstractString; kwargs...)
find_assignment(expr, key)
end

# Print information on potential wait time only in non-parallel case
if !mpi_isparallel(Val{:MPIExt}())
@info "You just called `trixi_include`. Julia may now compile the code, please be patient."
end
Base.include(ex -> replace_assignments(insert_maxiters(ex); kwargs...), mod, elixir)
end

Expand Down Expand Up @@ -143,3 +147,8 @@ function find_assignment(expr, destination)

result
end

# This is just a dummy function. We only implement a real
# version if MPI.jl is loaded to avoid letting TrixiBase.jl
# depend explicitly on MPI.jl.
mpi_isparallel(x) = false
48 changes: 48 additions & 0 deletions test/test_util.jl
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,57 @@ macro trixi_testset(name, expr)
using Test
using TrixiBase

# We also include this file again to provide the definition of
# the other testing macros. This allows to use `@trixi_testset`
# in a nested fashion and also call `@test_nowarn_mod` from
# there.
include(@__FILE__)

@testset verbose=true $name $expr
end

nothing
end
end

"""
@test_nowarn_mod expr

Modified version of `@test_nowarn expr` that prints the content of `stderr` when
it is not empty and ignores some common info statements printed in Trixi.jl
uses.
"""
macro test_nowarn_mod(expr, additional_ignore_content = String[])
quote
let fname = tempname()
try
ret = open(fname, "w") do f
redirect_stderr(f) do
$(esc(expr))
end
end
stderr_content = read(fname, String)
if !isempty(stderr_content)
println("Content of `stderr`:\n", stderr_content)
end

# Patterns matching the following ones will be ignored. Additional patterns
# passed as arguments can also be regular expressions, so we just use the
# type `Any` for `ignore_content`.
ignore_content = Any["[ Info: You just called `trixi_include`. Julia may now compile the code, please be patient.\n"]
append!(ignore_content, $additional_ignore_content)
for pattern in ignore_content
stderr_content = replace(stderr_content, pattern => "")
end

# We also ignore simple module redefinitions for convenience. Thus, we
# check whether every line of `stderr_content` is of the form of a
# module replacement warning.
@test occursin(r"^(WARNING: replacing module .+\.\n)*$", stderr_content)
ret
finally
rm(fname, force = true)
end
end
end
end
17 changes: 9 additions & 8 deletions test/trixi_include.jl
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,16 @@

# Use `@trixi_testset`, which wraps code in a temporary module, and call
# `trixi_include` with `@__MODULE__` in order to isolate this test.
@test_nowarn trixi_include(@__MODULE__, filename)
@test_nowarn_mod trixi_include(@__MODULE__, filename)
@test @isdefined x
@test x == 4

@test_nowarn trixi_include(@__MODULE__, filename, x = 7)
@test_nowarn_mod trixi_include(@__MODULE__, filename, x = 7)

@test x == 7

# Verify default version (that includes in `Main`)
@test_nowarn trixi_include(filename, x = 11)
@test_nowarn_mod trixi_include(filename, x = 11)
@test Main.x == 11

@test_throws "assignment `y` not found in expression" trixi_include(@__MODULE__,
Expand Down Expand Up @@ -101,22 +102,22 @@
# Use `@trixi_testset`, which wraps code in a temporary module, and call
# `Base.include` and `trixi_include` with `@__MODULE__` in order to isolate this test.
Base.include(@__MODULE__, filename1)
@test_nowarn trixi_include(@__MODULE__, filename2)
@test_nowarn_mod trixi_include(@__MODULE__, filename2)
@test @isdefined x
# This is the default `maxiters` inserted by `trixi_include`
@test x == 10^5

@test_nowarn trixi_include(@__MODULE__, filename2,
maxiters = 7)
@test_nowarn_mod trixi_include(@__MODULE__, filename2,
maxiters = 7)
# Test that `maxiters` got overwritten
@test x == 7

# Verify that adding `maxiters` to `maxiters` results in exactly one of them
# case 1) `maxiters` is *before* semicolon in included file
@test_nowarn trixi_include(@__MODULE__, filename3, maxiters = 11)
@test_nowarn_mod trixi_include(@__MODULE__, filename3, maxiters = 11)
@test y == 11
# case 2) `maxiters` is *after* semicolon in included file
@test_nowarn trixi_include(@__MODULE__, filename3, maxiters = 14)
@test_nowarn_mod trixi_include(@__MODULE__, filename3, maxiters = 14)
@test y == 14
finally
rm(filename1, force = true)
Expand Down
Loading