@@ -66,7 +66,6 @@ KernelAbstractions primitives can be used in non-kernel functions.
66
66
67
67
!!! warn
68
68
This is an experimental feature.
69
-
70
69
"""
71
70
macro kernel (config, expr)
72
71
if config isa Expr && config. head == :(= ) &&
@@ -97,13 +96,19 @@ macro Const end
97
96
copyto!(::Backend, dest::AbstractArray, src::AbstractArray)
98
97
99
98
Perform a `copyto!` operation that execution ordered with respect to the backend.
99
+
100
+ !!! note
101
+ Backend implementations **must** implement this function.
100
102
"""
101
103
function copyto! end
102
104
103
105
"""
104
106
synchronize(::Backend)
105
107
106
108
Synchronize the current backend.
109
+
110
+ !!! note
111
+ Backend implementations **must** implement this function.
107
112
"""
108
113
function synchronize end
109
114
@@ -114,12 +119,13 @@ Release the memory of an array for reuse by future allocations
114
119
and reduce pressure on the allocator.
115
120
After releasing the memory of an array, it should no longer be accessed.
116
121
117
- This function is optional both to implement and call.
118
- If not implemented for a particular backend, default action is a no-op.
119
- Otherwise, it should be defined for backend's array type.
120
-
121
122
!!! note
122
123
On CPU backend this is always a no-op.
124
+
125
+ !!! note
126
+ Backend implementations **may** implement this function.
127
+ If not implemented for a particular backend, default action is a no-op.
128
+ Otherwise, it should be defined for backend's array type.
123
129
"""
124
130
function unsafe_free! end
125
131
@@ -393,9 +399,17 @@ constify(arg) = adapt(ConstAdaptor(), arg)
393
399
# ##
394
400
395
401
"""
396
- Abstract type for all KernelAbstractions backends.
402
+
403
+ Abstract type for all KernelAbstractions backends.
397
404
"""
398
405
abstract type Backend end
406
+
407
+ """
408
+ Abstract type for all GPU based KernelAbstractions backends.
409
+
410
+ !!! note
411
+ New backend implementations **must** sub-type this abstract type.
412
+ """
399
413
abstract type GPU <: Backend end
400
414
401
415
"""
@@ -412,6 +426,11 @@ struct CPU <: Backend
412
426
CPU (;static:: Bool = false ) = new (static)
413
427
end
414
428
429
+ """
430
+ isgpu(::Backend)::Bool
431
+
432
+ Returns true for all [`GPU`](@ref) backends.
433
+ """
415
434
isgpu (:: GPU ) = true
416
435
isgpu (:: CPU ) = false
417
436
@@ -420,6 +439,10 @@ isgpu(::CPU) = false
420
439
get_backend(A::AbstractArray)::Backend
421
440
422
441
Get a [`Backend`](@ref) instance suitable for array `A`.
442
+
443
+ !!! note
444
+ Backend implementations **must** provide `get_backend` for their custom array type.
445
+ It should be the same as the return type of [`allocate`](@ref)
423
446
"""
424
447
function get_backend end
425
448
@@ -438,39 +461,61 @@ get_backend(::Array) = CPU()
438
461
Adapt. adapt_storage (:: CPU , a:: Array ) = a
439
462
440
463
"""
441
- allocate(::Backend, Type, dims...)
464
+ allocate(::Backend, Type, dims...)::AbstractArray
442
465
443
466
Allocate a storage array appropriate for the computational backend.
467
+
468
+ !!! note
469
+ Backend implementations **must** implement `allocate(::NewBackend, T, dims::Tuple)`
470
+ """
471
+ allocate (backend:: Backend , T, dims... ) = allocate (backend, T, dims)
472
+ allocate (backend:: Backend , T, dims:: Tuple ) = throw (MethodError (allocate, (backend, T, dims)))
473
+
444
474
"""
445
- allocate (backend, T, dims ... ) = return allocate (backend, T , dims)
475
+ zeros(::Backend, Type , dims...)::AbstractArray
446
476
447
- zeros (backend, T, dims... ) = zeros (backend, T, dims)
448
- function zeros (backend, :: Type{T} , dims:: Tuple ) where T
477
+ Allocate a storage array appropriate for the computational backend filled with zeros.
478
+ """
479
+ zeros (backend:: Backend , T, dims... ) = zeros (backend, T, dims)
480
+ function zeros (backend:: Backend , :: Type{T} , dims:: Tuple ) where T
449
481
data = allocate (backend, T, dims... )
450
482
fill! (data, zero (T))
451
483
return data
452
484
end
453
485
454
- ones (backend, T, dims... ) = ones (backend, T, dims)
455
- function ones (backend, :: Type{T} , dims:: Tuple ) where T
486
+ """
487
+ ones(::Backend, Type, dims...)::AbstractArray
488
+
489
+ Allocate a storage array appropriate for the computational backend filled with ones.
490
+ """
491
+ ones (backend:: Backend , T, dims... ) = ones (backend, T, dims)
492
+ function ones (backend:: Backend , :: Type{T} , dims:: Tuple ) where T
456
493
data = allocate (backend, T, dims)
457
494
fill! (data, one (T))
458
495
return data
459
496
end
460
497
461
498
"""
462
- supports_atomics(::Backend)
499
+ supports_atomics(::Backend)::Bool
463
500
464
501
Returns whether `@atomic` operations are supported by the backend.
502
+
503
+ !!! note
504
+ Backend implementations **must** implement this function,
505
+ only if they **do not** support atomic operations with Atomix.
465
506
"""
466
- supports_atomics (backend ) = true
507
+ supports_atomics (:: Backend ) = true
467
508
468
509
"""
469
- supports_float64(::Backend)
510
+ supports_float64(::Backend)::Bool
470
511
471
512
Returns whether `Float64` values are supported by the backend.
513
+
514
+ !!! note
515
+ Backend implementations **must** implement this function,
516
+ only if they **do not** support `Float64`.
472
517
"""
473
- supports_float64 (backend ) = true
518
+ supports_float64 (:: Backend ) = true
474
519
475
520
"""
476
521
priority!(::Backend, prio::Symbol)
@@ -479,6 +524,9 @@ Set the priority for the backend stream/queue. This is an optional
479
524
feature that backends may or may not implement. If a backend shall
480
525
support priorities it must accept `:high`, `:normal`, `:low`.
481
526
Where `:normal` is the default.
527
+
528
+ !!! note
529
+ Backend implementations **may** implement this function.
482
530
"""
483
531
function priority! (:: Backend , prio:: Symbol )
484
532
if ! (prio in (:high , :normal , :low ))
@@ -501,6 +549,13 @@ import .NDIteration: get
501
549
Kernel closure struct that is used to represent the backend
502
550
kernel on the host. `WorkgroupSize` is the number of workitems
503
551
in a workgroup.
552
+
553
+ !!! note
554
+ Backend implementations **must** implement:
555
+ ```
556
+ (kernel::Kernel{<:NewBackend})(args...; ndrange=nothing, workgroupsize=nothing)
557
+ ```
558
+ As well as the on-device functionality.
504
559
"""
505
560
struct Kernel{Backend, WorkgroupSize<: _Size , NDRange<: _Size , Fun}
506
561
backend:: Backend
0 commit comments