-
Notifications
You must be signed in to change notification settings - Fork 125
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[AlgorithmSelection] Rename BackendSelection, introduce Backend type (#…
- Loading branch information
Showing
18 changed files
with
111 additions
and
63 deletions.
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
3 changes: 0 additions & 3 deletions
3
NDTensors/src/lib/AlgorithmSelection/src/AlgorithmSelection.jl
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
File renamed without changes.
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,9 @@ | ||
module BackendSelection | ||
include("abstractbackend.jl") | ||
include("backend_types.jl") | ||
|
||
# TODO: This is defined for backwards compatibility, | ||
# delete this alias once downstream packages change over | ||
# to using `BackendSelection`. | ||
const AlgorithmSelection = BackendSelection | ||
end |
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,4 @@ | ||
abstract type AbstractBackend end | ||
|
||
backend_string(::AbstractBackend) = error("Not implemented") | ||
parameters(::AbstractBackend) = error("Not implemented") |
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,57 @@ | ||
for type in (:Algorithm, :Backend) | ||
@eval begin | ||
""" | ||
$($type) | ||
A type representing a backend for a function. | ||
For example, a function might have multiple backends | ||
implementations, which internally are selected with a `$($type)` type. | ||
This allows users to extend functionality with a new implementation but | ||
use the same interface. | ||
""" | ||
struct $type{Back,Kwargs<:NamedTuple} <: AbstractBackend | ||
kwargs::Kwargs | ||
end | ||
|
||
$type{Back}(kwargs::NamedTuple) where {Back} = $type{Back,typeof(kwargs)}(kwargs) | ||
$type{Back}(; kwargs...) where {Back} = $type{Back}(NamedTuple(kwargs)) | ||
$type(s; kwargs...) = $type{Symbol(s)}(NamedTuple(kwargs)) | ||
|
||
$type(backend::$type) = backend | ||
|
||
# TODO: Use `SetParameters`. | ||
backend_string(::$type{Back}) where {Back} = string(Back) | ||
parameters(backend::$type) = getfield(backend, :kwargs) | ||
|
||
function Base.show(io::IO, backend::$type) | ||
return print(io, "$type type ", backend_string(backend), ", ", parameters(backend)) | ||
end | ||
Base.print(io::IO, backend::$type) = | ||
print(io, backend_string(backend), ", ", parameters(backend)) | ||
end | ||
end | ||
|
||
# TODO: See if these can be moved inside of `@eval`. | ||
""" | ||
@Algorithm_str | ||
A convenience macro for writing [`Algorithm`](@ref) types, typically used when | ||
adding methods to a function that supports multiple algorithm | ||
backends. | ||
""" | ||
macro Algorithm_str(s) | ||
return :(Algorithm{$(Expr(:quote, Symbol(s)))}) | ||
end | ||
|
||
""" | ||
@Backend_str | ||
A convenience macro for writing [`Backend`](@ref) types, typically used when | ||
adding methods to a function that supports multiple | ||
backends. | ||
""" | ||
macro Backend_str(s) | ||
return :(Backend{$(Expr(:quote, Symbol(s)))}) | ||
end |
File renamed without changes.
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,28 @@ | ||
@eval module $(gensym()) | ||
using Test: @test, @testset | ||
using NDTensors: NDTensors | ||
using NDTensors.BackendSelection: | ||
BackendSelection, Algorithm, Backend, @Algorithm_str, @Backend_str | ||
# TODO: This is defined for backwards compatibility, | ||
# delete this alias once downstream packages change over | ||
# to using `BackendSelection`. | ||
using NDTensors.AlgorithmSelection: AlgorithmSelection | ||
@testset "BackendSelection" begin | ||
# TODO: This is defined for backwards compatibility, | ||
# delete this alias once downstream packages change over | ||
# to using `BackendSelection`. | ||
@test AlgorithmSelection === BackendSelection | ||
for type in (Algorithm, Backend) | ||
@testset "$type" begin | ||
@test type("backend") isa type{:backend} | ||
@test type(:backend) isa type{:backend} | ||
backend = type("backend"; x=2, y=3) | ||
@test backend isa type{:backend} | ||
@test BackendSelection.parameters(backend) === (; x=2, y=3) | ||
end | ||
end | ||
# Macro syntax. | ||
@test Algorithm"backend"(; x=2, y=3) === Algorithm("backend"; x=2, y=3) | ||
@test Backend"backend"(; x=2, y=3) === Backend("backend"; x=2, y=3) | ||
end | ||
end |
2 changes: 1 addition & 1 deletion
2
NDTensors/src/lib/BlockSparseArrays/src/backup/BlockSparseArrays.jl
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
2 changes: 1 addition & 1 deletion
2
NDTensors/src/lib/BlockSparseArrays/src/backup/LinearAlgebraExt/LinearAlgebraExt.jl
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
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
2 changes: 1 addition & 1 deletion
2
NDTensors/src/lib/TensorAlgebra/src/contract/contract_matricize/contract.jl
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
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