-
Notifications
You must be signed in to change notification settings - Fork 53
Multi-threading attempt III #203
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
Draft
lkdvos
wants to merge
18
commits into
main
Choose a base branch
from
ld-multithreading2
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 8 commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
244628a
Add OhMyThreads
lkdvos 4371650
Add `TensorKitBackend`
lkdvos 2da9b7a
Add backend/allocator support in `add_transform!`
lkdvos aa1e6d1
Add backend/allocator support in TensorOperations
lkdvos d526053
Add scheduler support in `mul!`
lkdvos cfc91a9
rework default scheduler settings
lkdvos 22739bb
Add functions for controlling schedulers
lkdvos c501f63
fix typo, remove undefined export
lkdvos 1eca7a8
simplify `mul!`
lkdvos 9a9e708
fix typo
lkdvos 0d042de
move`select_backend`
lkdvos 7d880e3
define function
lkdvos cbc3dee
remove incorrect code patterns
lkdvos 6bfe4cb
Revert "simplify `mul!`"
lkdvos 2e43793
Hack blockiterator for BraidingTensor
lkdvos dd013fd
specialize `mul!` for `BraidingTensor`
lkdvos 30527de
Fix block iterator for `BraidingTensor`
lkdvos 2ce33eb
small bugfixes
lkdvos 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
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 |
---|---|---|
@@ -0,0 +1,107 @@ | ||
# Scheduler implementation | ||
# ------------------------ | ||
""" | ||
const blockscheduler = ScopedValue{Scheduler}(SerialScheduler()) | ||
|
||
The default scheduler used when looping over different blocks in the matrix representation of a | ||
tensor. | ||
|
||
For controlling this value, see also [`set_blockscheduler`](@ref) and [`with_blockscheduler`](@ref). | ||
""" | ||
const blockscheduler = ScopedValue{Scheduler}(SerialScheduler()) | ||
|
||
""" | ||
const subblockscheduler = ScopedValue{Scheduler}(SerialScheduler()) | ||
|
||
The default scheduler used when looping over different subblocks in a tensor. | ||
|
||
For controlling this value, see also [`set_subblockscheduler`](@ref) and [`with_subblockscheduler`](@ref). | ||
""" | ||
const subblockscheduler = ScopedValue{Scheduler}(SerialScheduler()) | ||
|
||
function select_scheduler(scheduler=OhMyThreads.Implementation.NotGiven(); kwargs...) | ||
return if scheduler == OhMyThreads.Implementation.NotGiven() && isempty(kwargs) | ||
Threads.nthreads() == 1 ? SerialScheduler() : DynamicScheduler() | ||
else | ||
OhMyThreads.Implementation._scheduler_from_userinput(scheduler; kwargs...) | ||
end | ||
end | ||
|
||
""" | ||
set_blockscheduler!([scheduler]; kwargs...) -> previuos | ||
|
||
Set the default scheduler used in looping over the different blocks in the matrix representation | ||
of a tensor. | ||
The arguments to this function are either an `OhMyThreads.Scheduler` or a `Symbol` with optional | ||
set of keywords arguments. For a detailed description, consult the | ||
[`OhMyThreads` documentation](https://juliafolds2.github.io/OhMyThreads.jl/stable/refs/api/#Schedulers). | ||
|
||
See also [`with_blockscheduler`](@ref). | ||
""" | ||
function set_blockscheduler!(scheduler=OhMyThreads.Implementation.NotGiven(); kwargs...) | ||
previous = blockscheduler[] | ||
blockscheduler[] = select_scheduler(scheduler; kwargs...) | ||
return previous | ||
end | ||
|
||
""" | ||
with_blockscheduler(f, [scheduler]; kwargs...) | ||
|
||
Run `f` in a scope where the `blockscheduler` is determined by `scheduler` and `kwargs...`. | ||
|
||
See also [`set_blockscheduler!`](@ref). | ||
""" | ||
function with_blockscheduler(f, scheduler=OhMyThreads.Implementation.NotGiven(); kwargs...) | ||
@with blockscheduler => select_scheduler(scheduler; kwargs...) f() | ||
end | ||
|
||
""" | ||
set_subblockscheduler!([scheduler]; kwargs...) -> previous | ||
|
||
Set the default scheduler used in looping over the different subblocks in a tensor. | ||
The arguments to this function are either an `OhMyThreads.Scheduler` or a `Symbol` with optional | ||
set of keywords arguments. For a detailed description, consult the | ||
[`OhMyThreads` documentation](https://juliafolds2.github.io/OhMyThreads.jl/stable/refs/api/#Schedulers). | ||
|
||
See also [`with_subblockscheduler`](@ref). | ||
""" | ||
function set_subblockscheduler!(scheduler=OhMyThreads.Implementation.NotGiven(); kwargs...) | ||
previous = subblockscheduler[] | ||
subblockscheduler[] = select_scheduler(scheduler; kwargs...) | ||
return previous | ||
end | ||
|
||
""" | ||
with_subblockscheduler(f, [scheduler]; kwargs...) | ||
|
||
Run `f` in a scope where the [`subblockscheduler`](@ref) is determined by `scheduler` and `kwargs...`. | ||
|
||
See also [`set_subblockscheduler!`](@ref). | ||
""" | ||
function with_subblockscheduler(f, scheduler=OhMyThreads.Implementation.NotGiven(); | ||
kwargs...) | ||
@with subblockscheduler => select_scheduler(scheduler; kwargs...) f() | ||
end | ||
|
||
# Backend implementation | ||
# ---------------------- | ||
# TODO: figure out a name | ||
# TODO: what should be the default scheduler? | ||
@kwdef struct TensorKitBackend{B<:AbstractBackend,BS,SBS} <: AbstractBackend | ||
arraybackend::B = TO.DefaultBackend() | ||
blockscheduler::BS = blockscheduler[] | ||
subblockscheduler::SBS = subblockscheduler[] | ||
end | ||
|
||
function TO.select_backend(::typeof(TO.tensoradd!), C::AbstractTensorMap, | ||
A::AbstractTensorMap) | ||
return TensorKitBackend() | ||
end | ||
function TO.select_backend(::typeof(TO.tensortrace!), C::AbstractTensorMap, | ||
A::AbstractTensorMap) | ||
return TensorKitBackend() | ||
end | ||
function TO.select_backend(::typeof(TO.tensorcontract!), C::AbstractTensorMap, | ||
A::AbstractTensorMap, B::AbstractTensorMap) | ||
return TensorKitBackend() | ||
end |
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
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.