Skip to content

Commit 91e595d

Browse files
authored
[AlgorithmSelection] Rename BackendSelection, introduce Backend type (#1407)
1 parent fa6c706 commit 91e595d

File tree

18 files changed

+111
-63
lines changed

18 files changed

+111
-63
lines changed

NDTensors/src/blocksparse/contract.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using .AlgorithmSelection: Algorithm, @Algorithm_str
1+
using .BackendSelection: Algorithm, @Algorithm_str
22

33
function contract(
44
tensor1::BlockSparseTensor,

NDTensors/src/imports.jl

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ using TimerOutputs
2222
using TupleTools
2323

2424
for lib in [
25-
:AlgorithmSelection,
2625
:AllocateData,
26+
:BackendSelection,
2727
:BaseExtensions,
2828
:UnspecifiedTypes,
2929
:TypeParameterAccessors,
@@ -51,6 +51,10 @@ for lib in [
5151
include("lib/$(lib)/src/$(lib).jl")
5252
@eval using .$lib: $lib
5353
end
54+
# TODO: This is defined for backwards compatibility,
55+
# delete this alias once downstream packages change over
56+
# to using `BackendSelection`.
57+
const AlgorithmSelection = BackendSelection
5458

5559
using Base: @propagate_inbounds, ReshapedArray, DimOrInd, OneTo
5660

NDTensors/src/lib/AlgorithmSelection/src/AlgorithmSelection.jl

Lines changed: 0 additions & 3 deletions
This file was deleted.

NDTensors/src/lib/AlgorithmSelection/src/algorithm.jl

Lines changed: 0 additions & 39 deletions
This file was deleted.

NDTensors/src/lib/AlgorithmSelection/test/runtests.jl

Lines changed: 0 additions & 12 deletions
This file was deleted.
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
module BackendSelection
2+
include("abstractbackend.jl")
3+
include("backend_types.jl")
4+
5+
# TODO: This is defined for backwards compatibility,
6+
# delete this alias once downstream packages change over
7+
# to using `BackendSelection`.
8+
const AlgorithmSelection = BackendSelection
9+
end
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
abstract type AbstractBackend end
2+
3+
backend_string(::AbstractBackend) = error("Not implemented")
4+
parameters(::AbstractBackend) = error("Not implemented")
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
for type in (:Algorithm, :Backend)
2+
@eval begin
3+
"""
4+
$($type)
5+
6+
A type representing a backend for a function.
7+
8+
For example, a function might have multiple backends
9+
implementations, which internally are selected with a `$($type)` type.
10+
11+
This allows users to extend functionality with a new implementation but
12+
use the same interface.
13+
"""
14+
struct $type{Back,Kwargs<:NamedTuple} <: AbstractBackend
15+
kwargs::Kwargs
16+
end
17+
18+
$type{Back}(kwargs::NamedTuple) where {Back} = $type{Back,typeof(kwargs)}(kwargs)
19+
$type{Back}(; kwargs...) where {Back} = $type{Back}(NamedTuple(kwargs))
20+
$type(s; kwargs...) = $type{Symbol(s)}(NamedTuple(kwargs))
21+
22+
$type(backend::$type) = backend
23+
24+
# TODO: Use `SetParameters`.
25+
backend_string(::$type{Back}) where {Back} = string(Back)
26+
parameters(backend::$type) = getfield(backend, :kwargs)
27+
28+
function Base.show(io::IO, backend::$type)
29+
return print(io, "$type type ", backend_string(backend), ", ", parameters(backend))
30+
end
31+
Base.print(io::IO, backend::$type) =
32+
print(io, backend_string(backend), ", ", parameters(backend))
33+
end
34+
end
35+
36+
# TODO: See if these can be moved inside of `@eval`.
37+
"""
38+
@Algorithm_str
39+
40+
A convenience macro for writing [`Algorithm`](@ref) types, typically used when
41+
adding methods to a function that supports multiple algorithm
42+
backends.
43+
"""
44+
macro Algorithm_str(s)
45+
return :(Algorithm{$(Expr(:quote, Symbol(s)))})
46+
end
47+
48+
"""
49+
@Backend_str
50+
51+
A convenience macro for writing [`Backend`](@ref) types, typically used when
52+
adding methods to a function that supports multiple
53+
backends.
54+
"""
55+
macro Backend_str(s)
56+
return :(Backend{$(Expr(:quote, Symbol(s)))})
57+
end

0 commit comments

Comments
 (0)