Skip to content

Commit

Permalink
Compilation message with MPI extension (#5)
Browse files Browse the repository at this point in the history
* draft MPI extension

* fix extension including precompilation

Co-authored-by: Michael Schlottke-Lakemper <michael@sloede.com>

* precompile extension

* MPI compat

* fix tests

* remove dummy test file

* format

* just use at-test_nowarn_mod

* format

---------

Co-authored-by: Michael Schlottke-Lakemper <michael@sloede.com>
  • Loading branch information
ranocha and sloede committed Jan 31, 2024
1 parent 1dc5a93 commit b73ff96
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 8 deletions.
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 <michael@sloede.com>"]
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

# 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

2 comments on commit b73ff96

@sloede
Copy link
Member

@sloede sloede commented on b73ff96 Jan 31, 2024

Choose a reason for hiding this comment

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

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

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

Registration pull request created: JuliaRegistries/General/99944

Tip: Release Notes

Did you know you can add release notes too? Just add markdown formatted text underneath the comment after the text
"Release notes:" and it will be added to the registry PR, and if TagBot is installed it will also be added to the
release that TagBot creates. i.e.

@JuliaRegistrator register

Release notes:

## Breaking changes

- blah

To add them here just re-invoke and the PR will be updated.

Tagging

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.1.0 -m "<description of version>" b73ff9649fb643d437832dcaad113b1c607db0f1
git push origin v0.1.0

Please sign in to comment.