Skip to content

Commit 6e22e6e

Browse files
authored
More generic allocate (#11)
1 parent 94f0a5e commit 6e22e6e

4 files changed

Lines changed: 50 additions & 44 deletions

File tree

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "UnallocatedArrays"
22
uuid = "43c9e47c-e622-40fb-bf18-a09fc8c466b6"
33
authors = ["ITensor developers <support@itensor.org> and contributors"]
4-
version = "0.1.1"
4+
version = "0.1.2"
55

66
[deps]
77
Adapt = "79e6a3ab-5dfb-504d-930d-738a2a938a0e"

src/abstractunallocatedarray.jl

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,10 @@ for STYPE in (:AbstractArray, :AbstractFill)
4949
end
5050
end
5151

52-
function allocate(f::UnallocatedArray)
52+
# Assume allocated.
53+
allocate(a::AbstractArray) = a
54+
55+
function allocate(f::AbstractFill)
5356
a = similar(f)
5457
fill!(a, getindex_value(f))
5558
return a

test/Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@ Aqua = "4c88cf16-eb10-579e-8560-4a9242c79595"
33
FillArrays = "1a297f60-69ca-5386-bcde-b61e274b549b"
44
JLArrays = "27aeb0d3-9eb9-45fb-866b-73c2ecf80fcb"
55
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
6-
NDTensors = "23ae76d9-e61a-49c4-8f12-3f1a16adf9cf"
76
Pkg = "44cfe95a-1eb2-52ea-b672-e2afdf69b78f"
87
SafeTestsets = "1bc83da4-3b8d-516f-aca4-4fe02f6d838f"
98
Suppressor = "fd094767-a336-5f1f-9728-57cf17d0bbfb"
109
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
1110
TypeParameterAccessors = "7e5a90cf-f82e-492e-a09b-e3e26432c138"
11+
UnallocatedArrays = "43c9e47c-e622-40fb-bf18-a09fc8c466b6"
1212

1313
[compat]
1414
Aqua = "0.8.9"

test/test_basics.jl

Lines changed: 44 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,39 @@
1-
@eval module $(gensym())
21
using FillArrays: FillArrays, AbstractFill, Fill, Zeros
3-
using UnallocatedArrays:
4-
UnallocatedFill, UnallocatedZeros, allocate, alloctype, set_alloctype
2+
using JLArrays: @allowscalar, JLArray
53
using LinearAlgebra: norm
64
using Test: @test, @test_broken, @testset
5+
using UnallocatedArrays:
6+
UnallocatedFill, UnallocatedZeros, allocate, alloctype, set_alloctype
77

8-
# TODO: Factorize this out into a seperate package so we can remove the NDTensors
9-
# test dependency. That also requires splitting of `NDTensors.CUDAExtensions`,
10-
# `NDTensors.AMDGPUExtensions`, `NDTensors.MetalExtensions`, etc.
11-
using NDTensors: NDTensors
12-
include(joinpath(pkgdir(NDTensors), "test", "NDTensorsTestUtils", "NDTensorsTestUtils.jl"))
13-
using .NDTensorsTestUtils: devices_list
8+
const arrayts = (Array, JLArray)
9+
const elts = (Float64, Float32, Complex{Float64}, Complex{Float32})
10+
@testset "Testing UnallocatedArrays on $arrayt with eltype $elt" for arrayt in arrayts,
11+
elt in elts
1412

15-
@testset "Testing UnallocatedArrays on $dev with eltype $elt" for dev in devices_list(ARGS),
16-
elt in (Float64, Float32, ComplexF64, ComplexF32)
13+
(Float64, Float32, ComplexF64, ComplexF32)
1714

1815
@testset "Basic funcitonality" begin
1916
z = Zeros{elt}((2, 3))
20-
Z = UnallocatedZeros(z, dev(Matrix{elt}))
17+
Z = UnallocatedZeros(z, arrayt{elt,2})
2118

2219
@test Z isa AbstractFill
2320
@test size(Z) == (2, 3)
2421
@test length(Z) == 6
2522
@test iszero(sum(Z))
2623
@test iszero(norm(Z))
2724
@test iszero(Z[2, 3])
28-
@test allocate(Z) isa dev(Matrix{elt})
29-
Zp = UnallocatedZeros{elt}(Zeros(2, 3), dev(Matrix{elt}))
25+
x = randn(elt, 2, 3)
26+
x′ = allocate(x)
27+
@test x === x′
28+
a = allocate(z)
29+
@test iszero(a)
30+
@test a isa Matrix{elt}
31+
a = allocate(Z)
32+
@test iszero(a)
33+
@test a isa arrayt{elt,2}
34+
Zp = UnallocatedZeros{elt}(Zeros(2, 3), arrayt{elt,2})
3035
@test Zp == Z
31-
Zp = set_alloctype(z, dev(Matrix{elt}))
36+
Zp = set_alloctype(z, arrayt{elt,2})
3237
@test Zp == Z
3338
Zc = copy(Z)
3439
@test Zc == Z
@@ -41,10 +46,10 @@ using .NDTensorsTestUtils: devices_list
4146
Zs = similar(Z)
4247
@test Zs isa alloctype(Z)
4348

44-
Z = UnallocatedZeros(z, dev(Array))
49+
Z = UnallocatedZeros(z, arrayt)
4550
Za = allocate(Z)
46-
@test Za isa dev(Array{elt,2})
47-
@test Za[1, 3] == zero(elt)
51+
@test Za isa arrayt{elt,2}
52+
@test @allowscalar(Za[1, 3]) == zero(elt)
4853

4954
#########################################
5055
# UnallocatedFill
@@ -68,8 +73,8 @@ using .NDTensorsTestUtils: devices_list
6873
Fs[1, 1, 1] = elt(10)
6974
@test Fs[1, 1, 1] == elt(10)
7075

71-
Fp = set_alloctype(f, dev(Array{elt,ndims(f)}))
72-
@test allocate(Fp) isa dev(Array{elt,ndims(f)})
76+
Fp = set_alloctype(f, arrayt{elt,ndims(f)})
77+
@test allocate(Fp) isa arrayt{elt,ndims(f)}
7378
@test Fp == F
7479
Fc = copy(F)
7580
@test Fc == F
@@ -82,20 +87,20 @@ using .NDTensorsTestUtils: devices_list
8287
Fc[2, 3, 4] = elt(0)
8388
@test iszero(Fc[2, 3, 4])
8489

85-
F = UnallocatedFill(f, dev(Array))
90+
F = UnallocatedFill(f, arrayt)
8691
Fa = allocate(F)
87-
@test Fa isa dev(Array{elt,3})
88-
@test Fa[2, 1, 4] == elt(3)
92+
@test Fa isa arrayt{elt,3}
93+
@test @allowscalar(Fa[2, 1, 4]) == elt(3)
8994

90-
F = UnallocatedFill(f, dev(Vector))
95+
F = UnallocatedFill(f, arrayt{<:Any,1})
9196
Fa = allocate(F)
9297
@test ndims(Fa) == 3
93-
@test Fa isa dev(Array)
98+
@test Fa isa arrayt
9499
end
95100

96101
@testset "Multiplication" begin
97102
z = Zeros{elt}((2, 3))
98-
Z = UnallocatedZeros(z, dev(Matrix{elt}))
103+
Z = UnallocatedZeros(z, arrayt{elt,2})
99104

100105
R = Z * Z'
101106
@test R isa UnallocatedZeros
@@ -118,9 +123,9 @@ using .NDTensorsTestUtils: devices_list
118123
###################################
119124
## UnallocatedFill
120125
f = Fill{elt}(3, (2, 12))
121-
F = UnallocatedFill(f, dev(Matrix{elt}))
126+
F = UnallocatedFill(f, arrayt{elt,2})
122127
p = Fill{elt}(4, (12, 5))
123-
P = UnallocatedFill(p, dev(Array{elt,ndims(p)}))
128+
P = UnallocatedFill(p, arrayt{elt,ndims(p)})
124129
R = F * P
125130
@test F isa UnallocatedFill
126131
@test R[1, 1] == elt(144)
@@ -147,7 +152,7 @@ using .NDTensorsTestUtils: devices_list
147152

148153
@testset "Broadcast" begin
149154
z = Zeros{elt}((2, 3))
150-
Z = UnallocatedZeros(z, dev(Matrix{elt}))
155+
Z = UnallocatedZeros(z, arrayt{elt,2})
151156
R = elt(2) .* Z
152157
@test R isa UnallocatedZeros
153158
@test alloctype(R) == alloctype(Z)
@@ -159,7 +164,7 @@ using .NDTensorsTestUtils: devices_list
159164
@test R isa UnallocatedZeros
160165
@test alloctype(R) == alloctype(Z)
161166

162-
Z = UnallocatedZeros(Zeros{elt}((2, 3)), dev(Matrix{elt}))
167+
Z = UnallocatedZeros(Zeros{elt}((2, 3)), arrayt{elt,2})
163168
R = Z + Z
164169
@test R isa UnallocatedZeros
165170
@test alloctype(R) == alloctype(Z)
@@ -175,7 +180,7 @@ using .NDTensorsTestUtils: devices_list
175180

176181
Z .*= 1.0
177182
@test Z isa UnallocatedZeros
178-
@test alloctype(R) == dev(Matrix{elt})
183+
@test alloctype(R) == arrayt{elt,2}
179184
@test Z[2, 1] == zero(elt)
180185
########################
181186
# UnallocatedFill
@@ -191,7 +196,7 @@ using .NDTensorsTestUtils: devices_list
191196
@test F2[1, 1, 1] == elt(8)
192197
@test alloctype(F2) == alloctype(F)
193198

194-
F = UnallocatedFill(Fill(elt(2), (2, 3)), dev(Matrix{elt}))
199+
F = UnallocatedFill(Fill(elt(2), (2, 3)), arrayt{elt,2})
195200
R = Z + F
196201
@test R isa UnallocatedFill
197202
@test alloctype(R) == alloctype(Z)
@@ -200,20 +205,20 @@ using .NDTensorsTestUtils: devices_list
200205
@test R isa UnallocatedFill
201206
@test alloctype(R) == alloctype(Z)
202207

203-
F = UnallocatedFill(Fill(elt(3), (2, 12)), dev(Matrix{elt}))
208+
F = UnallocatedFill(Fill(elt(3), (2, 12)), arrayt{elt,2})
204209
R = F .* F
205210
@test R isa UnallocatedFill
206211
@test R[2, 9] == elt(9)
207212
@test alloctype(R) == alloctype(F)
208213
@test size(R) == (2, 12)
209214

210-
P = UnallocatedFill(Fill(elt(4), (2, 3)), dev(Matrix{elt}))
215+
P = UnallocatedFill(Fill(elt(4), (2, 3)), arrayt{elt,2})
211216
R = Z .* P
212217
@test R isa UnallocatedZeros
213218
@test alloctype(R) == alloctype(P)
214219
@test size(R) == (2, 3)
215220

216-
F = UnallocatedFill(Fill(elt(2), (2, 3)), dev(Matrix{elt}))
221+
F = UnallocatedFill(Fill(elt(2), (2, 3)), arrayt{elt,2})
217222
R = F + F
218223
@test R isa UnallocatedFill
219224
@test R[1, 3] == elt(4)
@@ -226,13 +231,13 @@ using .NDTensorsTestUtils: devices_list
226231

227232
## TODO make other kron tests
228233
@testset "Kron" begin
229-
A = UnallocatedZeros(Zeros{elt}(2), dev(Vector{elt}))
230-
B = UnallocatedZeros(Zeros{elt}(2), dev(Vector{elt}))
234+
A = UnallocatedZeros(Zeros{elt}(2), arrayt{elt,1})
235+
B = UnallocatedZeros(Zeros{elt}(2), arrayt{elt,1})
231236
C = kron(A, B)
232237
@test C isa UnallocatedZeros
233238
@test alloctype(C) == alloctype(B)
234239

235-
B = UnallocatedFill(Fill(elt(2), (2)), dev(Vector{elt}))
240+
B = UnallocatedFill(Fill(elt(2), (2)), arrayt{elt,1})
236241
C = kron(A, B)
237242
@test C isa UnallocatedZeros
238243
@test alloctype(C) == alloctype(B)
@@ -241,12 +246,10 @@ using .NDTensorsTestUtils: devices_list
241246
@test C isa UnallocatedZeros
242247
@test alloctype(C) == alloctype(B)
243248

244-
A = UnallocatedFill(Fill(elt(3), (2)), dev(Vector{elt}))
249+
A = UnallocatedFill(Fill(elt(3), (2)), arrayt{elt,1})
245250
C = kron(A, B)
246251
@test C isa UnallocatedFill
247252
@test alloctype(C) == alloctype(B)
248253
@test C[1] == elt(6)
249254
end
250255
end
251-
252-
end

0 commit comments

Comments
 (0)