-
Notifications
You must be signed in to change notification settings - Fork 38
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
Needs docs on module initialization #167
Comments
@timholy Thanks a lot for opening this issue. Yes, moving the storage into the dependent module is definitely the better way to ensure compatibility with to revise. |
Not just Revise. It's really a package precompilation issue: currently, any "work" that needs to be done to initialize ParallelStencil must be done from within an |
@timholy: similar observations as you made with revise.jl can be made with pluto.jl, as a newly opened issue just showed: #178 . Naturally, I would like to do a fix that resolves both issues. Would you mind having a quick look at what was reported? Do you think storing all the needed settings in the dependent module should also solve the pluto issue?
Thanks a lot for your suggestion. Instead of creating global constants for each setting in the caller module, I would prefer to create a single metadata submodule in the caller module bundling all the settings. Do you see any issues with this solution? |
@timholy This worked out without any issues and we have now full compatibility with Revise.jl. Thanks for the hint! |
Glad you're fixing things successfully. Since you asked, let me explain the precompilation issue a bit more. First, navigate to a temporary directory (here I'll call it using Pkg
Pkg.generate("MyPkg")
Pkg.generate("UserPkg")
Pkg.activate("UserPkg")
Pkg.develop(PackageSpec(path=abspath("MyPkg")))
write("MyPkg/src/MyPkg.jl", """
module MyPkg
const nusers = Ref(0)
end
""")
write("UserPkg/src/UserPkg.jl", """
module UserPkg
using MyPkg
MyPkg.nusers[] += 1
@show MyPkg.nusers[]
end
""") Briefly, this creates two packages, The following might surprise you: tim@diva:/tmp/modinit$ julia -q --startup=no --project=UserPkg
julia> using UserPkg
Precompiling UserPkg
Info Given UserPkg was explicitly requested, output will be shown live
MyPkg.nusers[] = 1
2 dependencies successfully precompiled in 3 seconds
1 dependency had output during precompilation:
┌ UserPkg
│ [Output was shown above]
└
julia> UserPkg.MyPkg.nusers[]
0 Huh? How is tim@diva:/tmp/modinit$ julia --compiled-modules=no -q --startup=no --project=UserPkg
julia> using UserPkg
MyPkg.nusers[] = 1
julia> UserPkg.MyPkg.nusers[]
1 The explanation is the following: precompilation stores a snapshot of the result of executing the package's main
"Fixing" this is what |
The README doesn't describe how to handle ParallelStencil's initialization requirements. It should mention that each dependent package needs a
__init__()
function that calls@init_parallel_stencil
, otherwise folks may try to call it from top-level in their module. xref timholy/Revise.jl#821Alternatively (and better), you could switch to storing all the needed settings in the dependent module, e.g., have
@init_parallel_stencil
create aglobal const __parallel_stencil_initialized__ = true
in the downstream package. It would also have to store any needed settings as well, of course. That would be precompile-friendly. The reason this is better is that (1) users don't need to know anything about__init__
functions, and (2)__init__
functions are awful black-boxes when it comes to the upcoming ability to compile small binaries (we will have to cache them regardless of whether they are needed). So it should be a win-win to move your storage into the dependent package.The text was updated successfully, but these errors were encountered: