|
1 | 1 | # Interface |
2 | 2 |
|
3 | 3 | To extend the above functionality to a new array type, you should use the types and |
4 | | -implement the interfaces listed on this page. GPUArrays is design around having two |
5 | | -different array types to represent a GPU array: one that only ever lives on the host, and |
| 4 | +implement the interfaces listed on this page. GPUArrays is designed around having two |
| 5 | +different array types to represent a GPU array: one that exists only on the host, and |
6 | 6 | one that actually can be instantiated on the device (i.e. in kernels). |
| 7 | +Device functionality is then handled by [KernelAbstractions.jl](https://github.com/JuliaGPU/KernelAbstractions.jl). |
7 | 8 |
|
| 9 | +## Host abstractions |
8 | 10 |
|
9 | | -## Device functionality |
10 | | - |
11 | | -Several types and interfaces are related to the device and execution of code on it. First of |
12 | | -all, you need to provide a type that represents your execution back-end and a way to call |
13 | | -kernels: |
| 11 | +You should provide an array type that builds on the `AbstractGPUArray` supertype, such as: |
14 | 12 |
|
15 | | -```@docs |
16 | | -GPUArrays.AbstractGPUBackend |
17 | | -GPUArrays.AbstractKernelContext |
18 | | -GPUArrays.gpu_call |
19 | | -GPUArrays.thread_block_heuristic |
20 | 13 | ``` |
| 14 | +mutable struct CustomArray{T, N} <: AbstractGPUArray{T, N} |
| 15 | + data::DataRef{Vector{UInt8}} |
| 16 | + offset::Int |
| 17 | + dims::Dims{N} |
| 18 | + ... |
| 19 | +end |
21 | 20 |
|
22 | | -You then need to provide implementations of certain methods that will be executed on the |
23 | | -device itself: |
24 | | - |
25 | | -```@docs |
26 | | -GPUArrays.AbstractDeviceArray |
27 | | -GPUArrays.LocalMemory |
28 | | -GPUArrays.synchronize_threads |
29 | | -GPUArrays.blockidx |
30 | | -GPUArrays.blockdim |
31 | | -GPUArrays.threadidx |
32 | | -GPUArrays.griddim |
33 | 21 | ``` |
34 | 22 |
|
| 23 | +This will allow your defined type (in this case `JLArray`) to use the GPUArrays interface where available. |
| 24 | +To be able to actually use the functionality that is defined for `AbstractGPUArray`s, you need to define the backend, like so: |
35 | 25 |
|
36 | | -## Host abstractions |
37 | | - |
38 | | -You should provide an array type that builds on the `AbstractGPUArray` supertype: |
39 | | - |
40 | | -```@docs |
41 | | -AbstractGPUArray |
42 | 26 | ``` |
43 | | - |
44 | | -First of all, you should implement operations that are expected to be defined for any |
45 | | -`AbstractArray` type. Refer to the Julia manual for more details, or look at the `JLArray` |
46 | | -reference implementation. |
47 | | - |
48 | | -To be able to actually use the functionality that is defined for `AbstractGPUArray`s, you |
49 | | -should provide implementations of the following interfaces: |
50 | | - |
51 | | -```@docs |
52 | | -GPUArrays.backend |
| 27 | +import KernelAbstractions: Backend |
| 28 | +struct CustomBackend <: KernelAbstractions.GPU |
| 29 | +KernelAbstractions.get_backend(a::CA) where CA <: CustomArray = CustomBackend() |
53 | 30 | ``` |
| 31 | + |
| 32 | +There are numerous examples of potential interfaces for GPUArrays, such as with [JLArrays](https://github.com/JuliaGPU/GPUArrays.jl/blob/master/lib/JLArrays/src/JLArrays.jl), [CuArrays](https://github.com/JuliaGPU/CUDA.jl/blob/master/src/gpuarrays.jl), and [ROCArrays](https://github.com/JuliaGPU/AMDGPU.jl/blob/master/src/gpuarrays.jl). |
0 commit comments