-
Notifications
You must be signed in to change notification settings - Fork 238
Directed rounding #2576
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
Draft
orkolorko
wants to merge
16
commits into
JuliaGPU:master
Choose a base branch
from
orkolorko:DirectedRounding
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Directed rounding #2576
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
6ed8086
fma and add working, sub not working
orkolorko fbee09f
Added tests in tutorials
orkolorko 3354104
Different examples
orkolorko 244a39a
Using RoundToNearest etc... in intrinsic call
orkolorko f8ec736
Added to the tutorial
orkolorko 7346a6a
Directed rounding MMA exposed
orkolorko f8872a4
Removed TODO file with test
orkolorko 8e4cffd
Removed debug
orkolorko 1042ebb
Added rounding to WMMA config, need to propagate back!
orkolorko 0e7fa43
Reverted to WMMA.Config without rounding
orkolorko c40489c
Added rounding as a default keyword argument in mma
orkolorko 4de16b1
Removed TODO
orkolorko 48c36d0
Finished reverting Rounding in Config
orkolorko 5f8fff5
Moved tutorial to hacking section
orkolorko 30105ee
Merge branch 'DirectedRounding' of github.com:orkolorko/CUDA.jl into …
orkolorko 3c2d721
Revert wmma.jl
orkolorko 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 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 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,49 @@ | ||
# # Introduction | ||
|
||
# * Adding new GPU intrinsics * | ||
|
||
# In this tutorial we will expose some GPU intrinsics to allow directed rounding in fused-multiply-add (fma) | ||
# floating point operation | ||
# We start by identifying the intrinsic we want to expose; to do so, we read the PTX (Parallel Thread Execution) | ||
# documentation at [PTX - Floating Point Instructions](https://docs.nvidia.com/cuda/parallel-thread-execution/#floating-point-instructions). | ||
# In table 32, it is presented a summary of floating point operations: we can construct the intrinsic string from that. | ||
# The FMA instruction for Float32 is presented as `{mad,fma}.rnd.f32`, where `rnd` can assume the values `.rnd = { .rn, .rz, .rm, .rp }`, | ||
# where `rn` is round to nearest, `rz` round to zero, `rm` round to minus infinity, `rp` round to plus infinity. | ||
# When building the intrinsic for the call, we need to change the type `.f64` with `.d` and `.f32` with `.f` | ||
# Therefore, to call the rounded towards infinity `fma` for `.f64` we need to call the intrinsic `llvm.nvvm.fma.rp.d` | ||
# Please remark that this is only possible if LLVM support the intrinsic; a source for those exposed by LLVM | ||
# may be found by searching the [LLVM repository](https://github.com/llvm/llvm-project). In in other cases you'd need @asmcall and inline PTX assembly. | ||
|
||
fma_rp(x::Float64, y::Float64, z::Float64) = ccall("llvm.nvvm.fma.rp.d", llvmcall, Cdouble, (Cdouble, Cdouble, Cdouble), x, y, z) | ||
fma(x::T, y::T, z::T, ::RoundingMode{:Up}) where {T <: Union{Float32, Float64}} = fma_rp(x, y, z) | ||
|
||
# We inspect the PTX code | ||
CUDA.code_ptx(fma_rp, Tuple{Float64,Float64,Float64}) | ||
|
||
# It is possible to see that the PTX code contains a call to the intrinsic `fma.rp.f64`; we add this function now | ||
# to src/device/intrins/math.jl | ||
|
||
using CUDA | ||
function test_fma!(out, x, y) | ||
I = threadIdx().x | ||
z = (2.0) ^ (-(I+53)) | ||
|
||
out[I] = fma(x, y, z, RoundNearest) | ||
out[I+4] = fma(x, y, z, RoundToZero) | ||
out[I+8] = fma(x, y, z, RoundUp) | ||
out[I+12] = fma(x, y, z, RoundDown) | ||
|
||
return | ||
end | ||
|
||
# The first four entries of the output are Rounded to Nearest, the entries 5 to 8 are rounded towards zero, | ||
# etc... | ||
|
||
out_d = CuArray(zeros(16)) | ||
@cuda threads = 4 test_fma!(out_d, 1.0, 1.0) | ||
out_h = Array(out_d) | ||
|
||
out_d = CuArray(zeros(4)) | ||
@cuda threads = 4 test_fma!(out_d, -1.0, 1.0) | ||
out_h = Array(out_d) | ||
|
This file contains 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 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.
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.
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.
Unrelated change.