@@ -4,7 +4,8 @@ export partition
4
4
5
5
mutable struct AllocateArray{T,N} <: ArrayOp{T,N}
6
6
eltype:: Type{T}
7
- f:: Function
7
+ f
8
+ want_index:: Bool
8
9
domain:: ArrayDomain{N}
9
10
domainchunks
10
11
partitioning:: AbstractBlocks
@@ -23,17 +24,29 @@ function partition(p::AbstractBlocks, dom::ArrayDomain)
23
24
map (_cumlength, map (length, indexes (dom)), p. blocksize))
24
25
end
25
26
27
+ function allocate_array (f, T, idx, sz)
28
+ new_f = allocate_array_func (thunk_processor (), f)
29
+ return new_f (idx, T, sz)
30
+ end
31
+ function allocate_array (f, T, sz)
32
+ new_f = allocate_array_func (thunk_processor (), f)
33
+ return new_f (T, sz)
34
+ end
35
+ allocate_array_func (:: Processor , f) = f
26
36
function stage (ctx, a:: AllocateArray )
27
- alloc (idx, sz) = a. f (idx, a. eltype, sz)
28
- thunks = [Dagger. @spawn alloc (i, size (x)) for (i, x) in enumerate (a. domainchunks)]
37
+ if a. want_index
38
+ thunks = [Dagger. @spawn allocate_array (a. f, a. eltype, i, size (x)) for (i, x) in enumerate (a. domainchunks)]
39
+ else
40
+ thunks = [Dagger. @spawn allocate_array (a. f, a. eltype, size (x)) for (i, x) in enumerate (a. domainchunks)]
41
+ end
29
42
return DArray (a. eltype, a. domain, a. domainchunks, thunks, a. partitioning)
30
43
end
31
44
32
45
const BlocksOrAuto = Union{Blocks{N} where N, AutoBlocks}
33
46
34
47
function Base. rand (p:: Blocks , eltype:: Type , dims:: Dims )
35
48
d = ArrayDomain (map (x-> 1 : x, dims))
36
- a = AllocateArray (eltype, (_, x ... ) -> rand (x ... ) , d, partition (p, d), p)
49
+ a = AllocateArray (eltype, rand, false , d, partition (p, d), p)
37
50
return _to_darray (a)
38
51
end
39
52
Base. rand (p:: BlocksOrAuto , T:: Type , dims:: Integer... ) = rand (p, T, dims)
@@ -45,7 +58,7 @@ Base.rand(::AutoBlocks, eltype::Type, dims::Dims) =
45
58
46
59
function Base. randn (p:: Blocks , eltype:: Type , dims:: Dims )
47
60
d = ArrayDomain (map (x-> 1 : x, dims))
48
- a = AllocateArray (eltype, (_, x ... ) -> randn (x ... ) , d, partition (p, d), p)
61
+ a = AllocateArray (eltype, randn, false , d, partition (p, d), p)
49
62
return _to_darray (a)
50
63
end
51
64
Base. randn (p:: BlocksOrAuto , T:: Type , dims:: Integer... ) = randn (p, T, dims)
@@ -57,7 +70,7 @@ Base.randn(::AutoBlocks, eltype::Type, dims::Dims) =
57
70
58
71
function sprand (p:: Blocks , eltype:: Type , dims:: Dims , sparsity:: AbstractFloat )
59
72
d = ArrayDomain (map (x-> 1 : x, dims))
60
- a = AllocateArray (eltype, (_, T, _dims) -> sprand (T, _dims... , sparsity), d, partition (p, d), p)
73
+ a = AllocateArray (eltype, (T, _dims) -> sprand (T, _dims... , sparsity), false , d, partition (p, d), p)
61
74
return _to_darray (a)
62
75
end
63
76
sprand (p:: BlocksOrAuto , T:: Type , dims_and_sparsity:: Real... ) =
@@ -73,7 +86,7 @@ sprand(::AutoBlocks, eltype::Type, dims::Dims, sparsity::AbstractFloat) =
73
86
74
87
function Base. ones (p:: Blocks , eltype:: Type , dims:: Dims )
75
88
d = ArrayDomain (map (x-> 1 : x, dims))
76
- a = AllocateArray (eltype, (_, x ... ) -> ones (x ... ) , d, partition (p, d), p)
89
+ a = AllocateArray (eltype, ones, false , d, partition (p, d), p)
77
90
return _to_darray (a)
78
91
end
79
92
Base. ones (p:: BlocksOrAuto , T:: Type , dims:: Integer... ) = ones (p, T, dims)
@@ -85,7 +98,7 @@ Base.ones(::AutoBlocks, eltype::Type, dims::Dims) =
85
98
86
99
function Base. zeros (p:: Blocks , eltype:: Type , dims:: Dims )
87
100
d = ArrayDomain (map (x-> 1 : x, dims))
88
- a = AllocateArray (eltype, (_, x ... ) -> zeros (x ... ) , d, partition (p, d), p)
101
+ a = AllocateArray (eltype, zeros, false , d, partition (p, d), p)
89
102
return _to_darray (a)
90
103
end
91
104
Base. zeros (p:: BlocksOrAuto , T:: Type , dims:: Integer... ) = zeros (p, T, dims)
0 commit comments