-
Notifications
You must be signed in to change notification settings - Fork 20
Use fast divisions in performance-critical code #1128
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
Merged
Merged
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
748f6cd
Use fast divisions in performance-critical code
efaulhaber 57c47b7
Optimize density diffusion
efaulhaber 4698252
Reformat
efaulhaber c758181
Merge branch 'main' into div-fast
efaulhaber d7691b1
Add docs
efaulhaber 380f261
Add tests for div_fast
efaulhaber d190925
Add comment
efaulhaber ba6c348
Add comments
efaulhaber de8cb92
Reformat
efaulhaber 2d222f0
Fix tests
efaulhaber 807072e
Fix docs
efaulhaber 05906e8
Merge branch 'div-fast' of github.com:efaulhaber/TrixiParticles.jl in…
efaulhaber 8d9bbce
Update docs/src/development.md
efaulhaber 1d89c66
Reformat
efaulhaber 4949722
Optimize the other viscosity models
efaulhaber 36764b9
Reformat
efaulhaber a029c3b
Fix typos
efaulhaber File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,34 @@ | ||
| # TODO this might be integrated into CUDA.jl at some point, see | ||
| # https://github.com/JuliaGPU/CUDA.jl/pull/3077 | ||
| module TrixiParticlesCUDAExt | ||
|
|
||
| using CUDA: CUDA | ||
| using TrixiParticles: TrixiParticles | ||
|
|
||
| # Use faster version of `div_fast` for `Float64` on CUDA. | ||
| # By default, `div_fast` translates to `Base.FastMath.div_fast`, but there is | ||
| # no fast division for `Float64` on CUDA, so we need to redefine it here to use the | ||
| # improved fast reciprocal defined below. | ||
| CUDA.@device_override TrixiParticles.div_fast(x, y::Float64) = x * fast_inv_cuda(y) | ||
|
|
||
| # Improved fast reciprocal for `Float64` by @Mikolaj-A-Kowalski, which is significantly | ||
| # more accurate than just calling "llvm.nvvm.rcp.approx.ftz.d" without the cubic iteration, | ||
| # while still being much faster than a full division. | ||
| # This is copied from Oceananigans.jl, see https://github.com/CliMA/Oceananigans.jl/pull/5140. | ||
| @inline function fast_inv_cuda(a::Float64) | ||
| # Get the approximate reciprocal | ||
| # https://docs.nvidia.com/cuda/parallel-thread-execution/#floating-point-instructions-rcp-approx-ftz-f64 | ||
| # This instruction chops off last 32bits of mantissa and computes inverse | ||
| # while treating all subnormal numbers as 0.0 | ||
| # If reciprocal would be subnormal, underflows to 0.0 | ||
| # 32 least significant bits of the result are filled with 0s | ||
| inv_a = ccall("llvm.nvvm.rcp.approx.ftz.d", llvmcall, Float64, (Float64,), a) | ||
|
|
||
| # Approximate the missing 32bits of mantissa with a single cubic iteration | ||
| e = fma(inv_a, -a, 1.0) | ||
| e = fma(e, e, e) | ||
| inv_a = fma(e, inv_a, inv_a) | ||
| return inv_a | ||
| end | ||
|
|
||
| end # module | ||
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.