-
Notifications
You must be signed in to change notification settings - Fork 87
Improve public API docs and expand the manual. #708
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 6 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
6d66a23
Improve public API docs and expand the manual.
giordano c6c8dec
Remove requirement of working POCL installation
giordano c77f48f
[CI] Always upload build artifact
giordano ca3dec6
Do not mention `cpu=false` (it's no-op)
giordano 0c13e55
`get_backend` works with all backends
giordano 5c5bde0
Document that only static `@localmem` is currently supported.
giordano a6f1758
Remove misleading "equivalent to" sentences
giordano 226135b
Add note for implementers to `device`/`ndevices`/`device!`
giordano 4501e10
Turn code blocks into doctests/runnable examples
giordano c9eb245
Remove `StaticSize` example
giordano 354cecc
Add `DynamicSize` and `StaticSize` to the manual in the "Internal" se…
giordano 0e505d9
Merge remote-tracking branch 'origin/main' into mg/docs
giordano c738798
Apply suggestion from @christiangnrd
giordano 096b550
Undeprecate at_private and at_uniform and cleanup some docs
vchuravy 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,9 @@ | ||
| [deps] | ||
| Documenter = "e30172f5-a6a5-5a46-863b-614d45cd2de4" | ||
| KernelAbstractions = "63c18a36-062a-441e-b654-da1e3ab1ce7c" | ||
|
|
||
| [compat] | ||
| Documenter = "1" | ||
|
|
||
| [sources] | ||
| KernelAbstractions = {path = ".."} |
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 |
|---|---|---|
| @@ -1,5 +1,3 @@ | ||
| push!(Base.LOAD_PATH, dirname(@__DIR__)) | ||
|
|
||
| using KernelAbstractions | ||
| using Documenter | ||
|
|
||
|
|
||
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 |
|---|---|---|
| @@ -1,23 +1,110 @@ | ||
| # Writing kernels | ||
| # Writing kernels | ||
|
|
||
| These kernel language constructs are intended to be used as part | ||
| of [`@kernel`](@ref) functions and not valid outside that context. | ||
| These kernel language constructs are intended to be used inside [`@kernel`](@ref) functions. | ||
| They are not valid in ordinary Julia code. | ||
|
|
||
| ## Constant arguments | ||
|
|
||
| Kernel functions allow for input arguments to be marked with the | ||
| [`@Const`](@ref) macro. It informs the compiler that the memory | ||
| accessed through that marked input argument, will not be written | ||
| to as part of the kernel. This has the implication that input arguments | ||
| are **not** allowed to alias each other. If you are used to CUDA C this | ||
| is similar to `const restrict`. | ||
| Kernel functions allow input arguments to be marked with the [`@Const`](@ref) macro. It informs | ||
| the compiler that the memory accessed through that argument will not be written to as part of | ||
| the kernel, and that it does not alias any other memory in the kernel. If you are used to CUDA C, | ||
| this is similar to `const restrict`. | ||
|
|
||
| ```julia | ||
| @kernel function saxpy!(a, @Const(X), Y) | ||
| I = @index(Global) | ||
| @inbounds Y[I] = a * X[I] + Y[I] | ||
| end | ||
| ``` | ||
|
|
||
| ## Indexing | ||
|
|
||
| There are several [`@index`](@ref) variants. | ||
| The [`@index`](@ref) macro returns the index of the current work item. Choose a **granularity** | ||
| and an optional **kind**: | ||
|
|
||
| | Granularity | Meaning | | ||
| |-------------|---------| | ||
| | `Global` | Index over the full `ndrange` (use for global memory) | | ||
| | `Group` | Index of the current workgroup | | ||
| | `Local` | Index within the current workgroup | | ||
|
|
||
| | Kind | Result type | | ||
| |------|-------------| | ||
| | `Linear` (default) | `Int` linear index | | ||
| | `Cartesian` | `CartesianIndex` for multi-dimensional `ndrange` | | ||
| | `NTuple` | `NTuple` of `Int` indices | | ||
|
|
||
| ```julia | ||
| @kernel function fill_diagonal!(A, val) | ||
| I = @index(Global, Cartesian) | ||
| if I[1] == I[2] | ||
| @inbounds A[I] = val | ||
| end | ||
| end | ||
|
|
||
| @kernel function linear_example(A) | ||
| I = @index(Global, Linear) # 1, 2, 3, ... | ||
| g = @index(Group, Linear) # workgroup id | ||
| l = @index(Local, Linear) # lane within workgroup | ||
| @inbounds A[I] = g + l | ||
| end | ||
| ``` | ||
|
|
||
| Inside a kernel, [`@groupsize`](@ref) and [`@ndrange`](@ref) query the launch configuration: | ||
|
|
||
| ```julia | ||
| @kernel function scale!(A, factor) | ||
| N = prod(@groupsize()) | ||
| I = @index(Global, Linear) | ||
| lmem = @localmem Float32 (N,) | ||
| i = @index(Local, Linear) | ||
| lmem[i] = factor | ||
| @synchronize() | ||
| @inbounds A[I] = lmem[i] | ||
| end | ||
| ``` | ||
|
|
||
| ## Local memory, synchronization, and private memory | ||
|
|
||
| [`@localmem`](@ref) declares storage shared by all work items in a workgroup. Only **static** | ||
| local memory is supported at the moment: the allocation size must be known at compile time | ||
| (for example `@localmem Int (32,)` or `@localmem Int (N,)` where `N = prod(@groupsize())` and | ||
| the workgroup size is fixed when the kernel is constructed). Reads and writes must be | ||
| separated by [`@synchronize`](@ref) if they are performed by different work items: | ||
|
|
||
| ```julia | ||
| @kernel function reverse_block!(A) | ||
| I = @index(Global, Linear) | ||
| i = @index(Local, Linear) | ||
| N = prod(@groupsize()) | ||
| buf = @localmem Int (N,) | ||
| buf[i] = i | ||
| @synchronize() | ||
| @inbounds A[I] = buf[N - i + 1] | ||
| end | ||
| ``` | ||
|
|
||
|
giordano marked this conversation as resolved.
Outdated
|
||
| [`@private`](@ref) and [`@uniform`](@ref) are deprecated for KernelAbstractions 1.0. Prefer | ||
| `MArray` for per-lane scratch storage that does not need to survive across `@synchronize`. | ||
|
|
||
| ## Launching kernels | ||
|
|
||
| Construct a kernel by calling the kernel function on a backend and optional static sizes, then | ||
| launch it with `ndrange`: | ||
|
|
||
| ```julia | ||
| # dynamic sizes — supply ndrange (and optionally workgroupsize) at launch | ||
| kernel = my_kernel(backend) | ||
| kernel(A, ndrange=size(A)) | ||
|
|
||
| ## Local memory, variable lifetime and private memory | ||
| # static workgroup size | ||
| kernel = my_kernel(backend, 256) | ||
| kernel(A, ndrange=size(A)) | ||
|
|
||
| [`@localmem`](@ref), [`@synchronize`](@ref), [`@private`](@ref) | ||
| # static workgroup size and ndrange — fewer runtime checks, may reduce recompilation | ||
| kernel = my_kernel(backend, 32, size(A)) | ||
| kernel(A) | ||
| ``` | ||
|
|
||
| # Launching kernels | ||
| Obtain the backend from an array with [`get_backend`](@ref) and always call [`synchronize`](@ref) before reading results on the host. | ||
| See the [Quickstart](@ref) for a full walkthrough and the Examples section of the manual for larger patterns. | ||
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.