-
Notifications
You must be signed in to change notification settings - Fork 55
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
Check and use only available FFTW libraries #278
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,44 @@ | ||
const valid_fftw_providers = let | ||
# Hardcoded list of supported platforms | ||
# In principle, we could check FFTW_jll.is_available() and MKL_jll.is_available() | ||
# but then we would have to load MKL_jll which we want to avoid (lazy artifacts!) | ||
platforms_providers = Dict( | ||
Base.BinaryPlatforms.Platform("aarch64", "macos") => ("fftw",), | ||
Base.BinaryPlatforms.Platform("aarch64", "linux"; libc = "glibc") => ("fftw",), | ||
Base.BinaryPlatforms.Platform("aarch64", "linux"; libc = "musl") => ("fftw",), | ||
Base.BinaryPlatforms.Platform("armv6l", "linux"; libc = "glibc", call_abi = "eabihf") => ("fftw",), | ||
Base.BinaryPlatforms.Platform("armv6l", "linux"; libc = "musl", call_abi = "eabihf") => ("fftw",), | ||
Base.BinaryPlatforms.Platform("armv7l", "linux"; libc = "glibc", call_abi = "eabihf") => ("fftw",), | ||
Base.BinaryPlatforms.Platform("armv7l", "linux"; libc = "musl", call_abi = "eabihf") => ("fftw",), | ||
Base.BinaryPlatforms.Platform("i686", "linux"; libc = "glibc") => ("fftw", "mkl"), | ||
Base.BinaryPlatforms.Platform("i686", "linux"; libc = "musl") => ("fftw",), | ||
Base.BinaryPlatforms.Platform("i686", "windows") => ("fftw", "mkl"), | ||
Base.BinaryPlatforms.Platform("powerpc64le", "linux"; libc = "glibc") => ("fftw",), | ||
Base.BinaryPlatforms.Platform("x86_64", "macos") => ("fftw", "mkl"), | ||
Base.BinaryPlatforms.Platform("x86_64", "linux"; libc = "glibc") => ("fftw",), | ||
Base.BinaryPlatforms.Platform("x86_64", "linux"; libc = "musl") => ("fftw",), | ||
Base.BinaryPlatforms.Platform("x86_64", "freebsd") => ("fftw",), | ||
Base.BinaryPlatforms.Platform("x86_64", "windows") => ("fftw", "mkl"), | ||
) | ||
Base.BinaryPlatforms.select_platform(platforms_providers, Base.BinaryPlatforms.HostPlatform()) | ||
end | ||
if valid_fftw_providers === nothing | ||
error("no valid FFTW library available") | ||
end | ||
|
||
function get_provider() | ||
# Note: we CANNOT do something like have the `default` value be `get(ENV, "JULIA_FFTW_PROVIDER", "fftw")` here. | ||
# This is because the only way the Julia knows that a default has changed is if the values on-disk change; so | ||
# if your "default" value can be changed from the outside, you quickly run into cache invalidation issues. | ||
# So the default here _must_ be a constant. | ||
default_provider = "fftw" | ||
default_provider = first(valid_fftw_providers) | ||
|
||
# Load the preference | ||
provider = @load_preference("provider", default_provider) | ||
|
||
# Ensure the provider matches one of the ones we support | ||
if provider ∉ ("fftw", "mkl") | ||
@error("Invalid provider setting \"$(provider)\"; valid settings include [\"fftw\", \"mkl\"], defaulting to \"fftw\"") | ||
if provider ∉ valid_fftw_providers | ||
@error("Invalid provider setting \"$(provider)\"; valid settings include [$(join(map(x -> '"' * x * '"', valid_fftw_providers), ", "))]") | ||
provider = default_provider | ||
end | ||
return provider | ||
|
@@ -34,8 +61,8 @@ | |
`Preferences.set_preferences!()` for more information on what these values mean. | ||
""" | ||
function set_provider!(provider; export_prefs::Bool = false) | ||
if provider !== nothing && provider !== missing && provider ∉ ("fftw", "mkl") | ||
throw(ArgumentError("Invalid provider value '$(provider)'")) | ||
if provider !== nothing && provider !== missing && provider ∉ valid_fftw_providers | ||
throw(ArgumentError("Invalid provider value \"$(provider)\"; valid settings include [$(join(map(x -> '"' * x * '"', valid_fftw_providers), ", "))]")) | ||
end | ||
set_preferences!(@__MODULE__, "provider" => provider; export_prefs, force = true) | ||
if provider != fftw_provider | ||
|
@@ -48,6 +75,11 @@ | |
# If we're using fftw_jll, load it in | ||
@static if fftw_provider == "fftw" | ||
import FFTW_jll | ||
if !FFTW_jll.is_available() | ||
# more descriptive error message if FFTW is not available | ||
# (should not be possible to reach this) | ||
@error("FFTW library cannot be loaded: Run `FFTW.set_provider!(\"mkl\")` to switch to MKL") | ||
end | ||
libfftw3[] = FFTW_jll.libfftw3_path | ||
libfftw3f[] = FFTW_jll.libfftw3f_path | ||
|
||
|
@@ -83,6 +115,11 @@ | |
# If we're using MKL, load it in and set library paths appropriately. | ||
@static if fftw_provider == "mkl" | ||
import MKL_jll | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why not doing the test There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I was not aware of this issue. Mainly, I thought it would be more user-friendly to figure out the supported libraries in advance, before trying to set them. Only moving the check here won't help with my use case and the linked issue above. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is there any other way to check whether a library can be loaded apart from There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
No.
That's going to hard-code answers which may potentially change over time. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Why not? Capture the fact that the artifact isn't available and throw a helpful error message to explain what to do, instead of getting a cryptic There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also, if I understand this implementation correctly this is automagically changing the provider if the requested one isn't actually available, which doesn't sound very deterministic. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, switching to the supported default provider was the main point - changing the LocalPreferences.toml file manually on my end creates git conflicts when moving branches and pulling all the time (and if I forget to adjust it, compilation will fail). To some extent that's already done in the current code: If you specify an invalid provider other than There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Displaying a more descriptive error message is an improvement but I think a solution that just defines the (in)valid providers correctly would be much more convenient. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Yes, a bit annoying. But since I didn't see any other alternative to define the valid providers correctly, I switched to hardcoding the supported platforms. I guess the supported platforms could always be updated easily - and probably supported platforms for MKL won't change much at least? If we're more ambitious, probably we could even automatically extract it from the Artifacts.toml files in FFTW_jll and MKL_jll. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
It did change. |
||
if !MKL_jll.is_available() | ||
# more descriptive error message if MKL is not available | ||
# (should not be possible to reach this) | ||
@error("MKL cannot be loaded: Run `FFTW.set_provider!(\"fftw\")` to switch to the FFTW library") | ||
end | ||
libfftw3[] = MKL_jll.libmkl_rt_path | ||
libfftw3f[] = MKL_jll.libmkl_rt_path | ||
const _last_num_threads = Ref(Cint(1)) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
#280 shows that the hardcoded list a bad idea: there's no MKL 2024 build for this platform.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, I always thought that a hardcoded list is suboptimal. Ideally we would just parse the Artifacts.toml file, I think, similar to https://github.com/JuliaPackaging/JLLWrappers.jl/blob/a1025a86bc58ac40e2d1df30f4d6bf6c390d51e1/src/toplevel_generators.jl#L132-L185 - but in the best case such a functionality would be available somewhere else. I would imagine that this would be a more common use case - figuring out whether a user's platform is supported without actually loading the JLL package.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But why not removing the automagic fallback, and only do what the user asks, and loudly and clearly fail if the artifact isn't available? In that case you can use is_available safely, since the user explicitly requested it
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Because the motivation for the PR and the problem I was trying to solve was that it failed. As a user I don't want to run into an error, I just want to use the provider that is available on my platform.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See e.g. also https://julialang.slack.com/archives/C6A044SQH/p1701979919655409
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To clarify a bit more: In my case the MKL provider was chosen as the default for me by a package I was using, but in principle it could also be the other way around (that the currently hardcoded default FFTW_jll provider does not work but MKL_jll does). I think it would be less surprising and more user-friendly if the default provider would not be hard-coded to e.g. FFTW_jll but that it is checked which providers are actually available for the user and the default one is chosen as the first one among those. I don't think one should override explicit user settings but I think the default should work (and the error messages/list of valid providers should match the ones that are actually available).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure packages should do that, preferences should be up to users. Especially if packages get it wrong.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree. But I want to emphasize that the PR tries to address the problem of incorrect lists of valid providers and non-working default providers in FFTW.jl itself, and does aim (anymore) to fix incorrect settings in some downstream package.