-
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.
Add adapt support to BlockSparseArrays
- Loading branch information
Showing
3 changed files
with
21 additions
and
0 deletions.
There are no files selected for viewing
5 changes: 5 additions & 0 deletions
5
.../src/lib/BlockSparseArrays/ext/BlockSparseArraysAdaptExt/src/BlockSparseArraysAdaptExt.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
module BlockSparseArraysAdaptExt | ||
using Adapt: Adapt, adapt | ||
using ..BlockSparseArrays: AbstractBlockSparseArray, map_stored_blocks | ||
Adapt.adapt_structure(to, x::AbstractBlockSparseArray) = map_stored_blocks(adapt(to), x) | ||
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
14 changes: 14 additions & 0 deletions
14
NDTensors/src/lib/BlockSparseArrays/src/blocksparsearrayinterface/map.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
function map_stored_blocks(f, a::AbstractArray) | ||
# TODO: Implement this as: | ||
# ```julia | ||
# mapped_blocks = SparseArraysInterface.map_stored(f, blocks(a)) | ||
# BlockSparseArray(mapped_blocks, axes(a)) | ||
# ``` | ||
# TODO: `block_stored_indices` should output `Indices` storing | ||
# the stored Blocks, not a `Dictionary` from cartesian indices | ||
# to Blocks. | ||
bs = block_stored_indices(a) | ||
mapped_blocks = Dictionary(bs, map(b -> f(@view(a[b])), bs)) | ||
# TODO: Use `similartype(typeof(a), eltype(eltype(mapped_blocks)))(...)`. | ||
return BlockSparseArray(mapped_blocks, axes(a)) | ||
end |