Skip to content

Commit 83d8c3e

Browse files
authored
Compatibility with Adapt.jl (#452)
This adds compatibility of BlockArrays.jl with Adapt.jl through a package extension, so that `Adapt.adapt` works on block arrays and blocked unit ranges. Fixes #451.
1 parent a80fcf7 commit 83d8c3e

File tree

4 files changed

+59
-1
lines changed

4 files changed

+59
-1
lines changed

Project.toml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name = "BlockArrays"
22
uuid = "8e7c35d0-a365-5155-bbbb-fb81a777f24e"
3-
version = "1.4.0"
3+
version = "1.5.0"
44

55
[deps]
66
ArrayLayouts = "4c555306-a7a7-4459-81d9-ec55ddd5c99a"
@@ -9,12 +9,15 @@ FillArrays = "1a297f60-69ca-5386-bcde-b61e274b549b"
99
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
1010

1111
[weakdeps]
12+
Adapt = "79e6a3ab-5dfb-504d-930d-738a2a938a0e"
1213
BandedMatrices = "aae01518-5342-5314-be14-df237901396f"
1314

1415
[extensions]
16+
BlockArraysAdaptExt = "Adapt"
1517
BlockArraysBandedMatricesExt = "BandedMatrices"
1618

1719
[compat]
20+
Adapt = "4.3"
1821
Aqua = "0.8"
1922
ArrayLayouts = "1.0.8"
2023
BandedMatrices = "1.0"
@@ -30,6 +33,7 @@ Test = "1"
3033
julia = "1.10"
3134

3235
[extras]
36+
Adapt = "79e6a3ab-5dfb-504d-930d-738a2a938a0e"
3337
Aqua = "4c88cf16-eb10-579e-8560-4a9242c79595"
3438
BandedMatrices = "aae01518-5342-5314-be14-df237901396f"
3539
Documenter = "e30172f5-a6a5-5a46-863b-614d45cd2de4"
@@ -42,6 +46,7 @@ Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
4246

4347
[targets]
4448
test = [
49+
"Adapt",
4550
"Aqua",
4651
"BandedMatrices",
4752
"Documenter",

ext/BlockArraysAdaptExt.jl

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
module BlockArraysAdaptExt
2+
3+
using Adapt
4+
using BlockArrays
5+
using BlockArrays: _BlockArray, _BlockedUnitRange
6+
import Adapt: adapt_structure
7+
8+
adapt_structure(to, r::BlockedUnitRange) = _BlockedUnitRange(adapt(to, r.first), map(adapt(to), r.lasts))
9+
adapt_structure(to, r::BlockedOneTo) = BlockedOneTo(map(adapt(to), r.lasts))
10+
11+
adapt_structure(to, A::BlockArray) = _BlockArray(map(adapt(to), blocks(A)), map(adapt(to), axes(A)))
12+
adapt_structure(to, A::BlockedArray) = BlockedArray(adapt(to, A.blocks), map(adapt(to), axes(A)))
13+
14+
end

test/runtests.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,4 @@ include("test_blockreduce.jl")
2929
include("test_blockdeque.jl")
3030
include("test_blockcholesky.jl")
3131
include("test_blockbanded.jl")
32+
include("test_adapt.jl")

test/test_adapt.jl

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
module TestBlockArraysAdapt
2+
3+
using BlockArrays, Adapt, Test
4+
5+
@testset "Adapt" begin
6+
@testset "Adapt Ranges" begin
7+
@test blockisequal(adapt(Array, blockedrange([2, 3])), blockedrange([2, 3]))
8+
@test blockisequal(adapt(Array, blockedrange(2, [2, 3])), blockedrange(2, [2, 3]))
9+
end
10+
11+
@testset "Adapt Block Arrays" begin
12+
A = BlockArray(randn(4, 4), [2, 2], [2, 2])
13+
= adapt(Array, A)
14+
@test== A
15+
@testisa BlockArray{Float64,2}
16+
@test blockisequal(axes(Ã), axes(A))
17+
V = view(A, :, :)
18+
= adapt(Array, V)
19+
@test== V
20+
@testisa SubArray{Float64,2}
21+
@test parent(Ṽ) isa BlockArray{Float64,2}
22+
@test blockisequal(axes(parent(Ṽ)), axes(A))
23+
24+
A = BlockedArray(randn(4, 4), [2, 2], [2, 2])
25+
= adapt(Array, A)
26+
@test== A
27+
@testisa BlockedArray{Float64,2}
28+
@test blockisequal(axes(Ã), axes(A))
29+
V = view(A, :, :)
30+
= adapt(Array, V)
31+
@test== V
32+
@testisa SubArray{Float64,2}
33+
@test parent(Ṽ) isa BlockedArray{Float64,2}
34+
@test blockisequal(axes(parent(Ṽ)), axes(A))
35+
end
36+
end
37+
38+
end

0 commit comments

Comments
 (0)