11import Base: cat
2- import Random: MersenneTwister
2+ import Random: MersenneTwister, rand!, randn!
33export partition
44
55mutable struct AllocateArray{T,N} <: ArrayOp{T,N}
@@ -79,53 +79,64 @@ function allocate_array(f, T, sz)
7979 return new_f (T, sz)
8080end
8181allocate_array_func (:: Processor , f) = f
82- function stage (ctx, a:: AllocateArray )
83- chunks = map (CartesianIndices (a. domainchunks)) do I
84- x = a. domainchunks[I]
85- i = LinearIndices (a. domainchunks)[I]
86- args = a. want_index ? (i, size (x)) : (size (x),)
87-
88- if isnothing (a. procgrid)
89- scope = get_compute_scope ()
90- else
91- scope = ExactScope (a. procgrid[CartesianIndex (mod1 .(Tuple (I), size (a. procgrid))... )])
92- end
93- if a. want_index
94- Dagger. @spawn compute_scope= scope allocate_array (a. f, a. eltype, i, args... )
95- else
96- Dagger. @spawn compute_scope= scope allocate_array (a. f, a. eltype, args... )
82+ function stage (ctx, A:: AllocateArray )
83+ tasks = Array {DTask,ndims(A.domainchunks)} (undef, size (A. domainchunks)... )
84+ Dagger. spawn_datadeps () do
85+ default_scope = get_compute_scope ()
86+ for I in CartesianIndices (A. domainchunks)
87+ x = A. domainchunks[I]
88+ i = LinearIndices (A. domainchunks)[I]
89+
90+ if isnothing (A. procgrid)
91+ scope = default_scope
92+ else
93+ scope = ExactScope (A. procgrid[CartesianIndex (mod1 .(Tuple (I), size (A. procgrid))... )])
94+ end
95+
96+ if A. want_index
97+ task = Dagger. @spawn compute_scope= scope allocate_array (A. f, A. eltype, i, size (x))
98+ else
99+ task = Dagger. @spawn compute_scope= scope allocate_array (A. f, A. eltype, size (x))
100+ end
101+ tasks[i] = task
97102 end
98103 end
99- return DArray (a . eltype, a . domain, a . domainchunks, chunks, a . partitioning)
104+ return DArray (A . eltype, A . domain, A . domainchunks, tasks, A . partitioning)
100105end
101106
102107const BlocksOrAuto = Union{Blocks{N} where N, AutoBlocks}
103108
104- function Base. rand (p:: Blocks , eltype :: Type , dims:: Dims ; assignment:: AssignmentType = :arbitrary )
109+ function Base. rand (p:: Blocks , T :: Type , dims:: Dims ; assignment:: AssignmentType = :arbitrary )
105110 d = ArrayDomain (map (x-> 1 : x, dims))
106- a = AllocateArray (eltype , rand, false , d, partition (p, d), p, assignment)
111+ a = AllocateArray (T , rand, false , d, partition (p, d), p, assignment)
107112 return _to_darray (a)
108113end
109- Base. rand (p:: BlocksOrAuto , T:: Type , dims:: Integer... ; assignment:: AssignmentType = :arbitrary ) = rand (p, T, dims; assignment)
110- Base. rand (p:: BlocksOrAuto , dims:: Integer... ; assignment:: AssignmentType = :arbitrary ) = rand (p, Float64, dims; assignment)
111- Base. rand (p:: BlocksOrAuto , dims:: Dims ; assignment:: AssignmentType = :arbitrary ) = rand (p, Float64, dims; assignment)
112- Base. rand (:: AutoBlocks , eltype:: Type , dims:: Dims ; assignment:: AssignmentType = :arbitrary ) =
113- rand (auto_blocks (dims), eltype, dims; assignment)
114-
115- function Base. randn (p:: Blocks , eltype:: Type , dims:: Dims ; assignment:: AssignmentType = :arbitrary )
114+ Base. rand (p:: BlocksOrAuto , T:: Type , dims:: Integer... ; assignment:: AssignmentType = :arbitrary ) =
115+ rand (p, T, dims; assignment)
116+ Base. rand (p:: BlocksOrAuto , dims:: Integer... ; assignment:: AssignmentType = :arbitrary ) =
117+ rand (p, Float64, dims; assignment)
118+ Base. rand (p:: BlocksOrAuto , dims:: Dims ; assignment:: AssignmentType = :arbitrary ) =
119+ rand (p, Float64, dims; assignment)
120+ Base. rand (:: AutoBlocks , T:: Type , dims:: Dims ; assignment:: AssignmentType = :arbitrary ) =
121+ rand (auto_blocks (dims), T, dims; assignment)
122+
123+ function Base. randn (p:: Blocks , T:: Type , dims:: Dims ; assignment:: AssignmentType = :arbitrary )
116124 d = ArrayDomain (map (x-> 1 : x, dims))
117- a = AllocateArray (eltype , randn, false , d, partition (p, d), p, assignment)
125+ a = AllocateArray (T , randn, false , d, partition (p, d), p, assignment)
118126 return _to_darray (a)
119127end
120- Base. randn (p:: BlocksOrAuto , T:: Type , dims:: Integer... ; assignment:: AssignmentType = :arbitrary ) = randn (p, T, dims; assignment)
121- Base. randn (p:: BlocksOrAuto , dims:: Integer... ; assignment:: AssignmentType = :arbitrary ) = randn (p, Float64, dims; assignment)
122- Base. randn (p:: BlocksOrAuto , dims:: Dims ; assignment:: AssignmentType = :arbitrary ) = randn (p, Float64, dims; assignment)
123- Base. randn (:: AutoBlocks , eltype:: Type , dims:: Dims ; assignment:: AssignmentType = :arbitrary ) =
124- randn (auto_blocks (dims), eltype, dims; assignment)
125-
126- function sprand (p:: Blocks , eltype:: Type , dims:: Dims , sparsity:: AbstractFloat ; assignment:: AssignmentType = :arbitrary )
128+ Base. randn (p:: BlocksOrAuto , T:: Type , dims:: Integer... ; assignment:: AssignmentType = :arbitrary ) =
129+ randn (p, T, dims; assignment)
130+ Base. randn (p:: BlocksOrAuto , dims:: Integer... ; assignment:: AssignmentType = :arbitrary ) =
131+ randn (p, Float64, dims; assignment)
132+ Base. randn (p:: BlocksOrAuto , dims:: Dims ; assignment:: AssignmentType = :arbitrary ) =
133+ randn (p, Float64, dims; assignment)
134+ Base. randn (:: AutoBlocks , T:: Type , dims:: Dims ; assignment:: AssignmentType = :arbitrary ) =
135+ randn (auto_blocks (dims), T, dims; assignment)
136+
137+ function sprand (p:: Blocks , T:: Type , dims:: Dims , sparsity:: AbstractFloat ; assignment:: AssignmentType = :arbitrary )
127138 d = ArrayDomain (map (x-> 1 : x, dims))
128- a = AllocateArray (eltype , (T, _dims) -> sprand (T, _dims... , sparsity), false , d, partition (p, d), p, assignment)
139+ a = AllocateArray (T , (T, _dims) -> sprand (T, _dims... , sparsity), false , d, partition (p, d), p, assignment)
129140 return _to_darray (a)
130141end
131142sprand (p:: BlocksOrAuto , T:: Type , dims_and_sparsity:: Real... ; assignment:: AssignmentType = :arbitrary ) =
@@ -134,30 +145,36 @@ sprand(p::BlocksOrAuto, dims_and_sparsity::Real...; assignment::AssignmentType =
134145 sprand (p, Float64, dims_and_sparsity[1 : end - 1 ], dims_and_sparsity[end ]; assignment)
135146sprand (p:: BlocksOrAuto , dims:: Dims , sparsity:: AbstractFloat ; assignment:: AssignmentType = :arbitrary ) =
136147 sprand (p, Float64, dims, sparsity; assignment)
137- sprand (:: AutoBlocks , eltype :: Type , dims:: Dims , sparsity:: AbstractFloat ; assignment:: AssignmentType = :arbitrary ) =
138- sprand (auto_blocks (dims), eltype , dims, sparsity; assignment)
148+ sprand (:: AutoBlocks , T :: Type , dims:: Dims , sparsity:: AbstractFloat ; assignment:: AssignmentType = :arbitrary ) =
149+ sprand (auto_blocks (dims), T , dims, sparsity; assignment)
139150
140- function Base. ones (p:: Blocks , eltype :: Type , dims:: Dims ; assignment:: AssignmentType = :arbitrary )
151+ function Base. ones (p:: Blocks , T :: Type , dims:: Dims ; assignment:: AssignmentType = :arbitrary )
141152 d = ArrayDomain (map (x-> 1 : x, dims))
142- a = AllocateArray (eltype , ones, false , d, partition (p, d), p, assignment)
153+ a = AllocateArray (T , ones, false , d, partition (p, d), p, assignment)
143154 return _to_darray (a)
144155end
145- Base. ones (p:: BlocksOrAuto , T:: Type , dims:: Integer... ; assignment:: AssignmentType = :arbitrary ) = ones (p, T, dims; assignment)
146- Base. ones (p:: BlocksOrAuto , dims:: Integer... ; assignment:: AssignmentType = :arbitrary ) = ones (p, Float64, dims; assignment)
147- Base. ones (p:: BlocksOrAuto , dims:: Dims ; assignment:: AssignmentType = :arbitrary ) = ones (p, Float64, dims; assignment)
148- Base. ones (:: AutoBlocks , eltype:: Type , dims:: Dims ; assignment:: AssignmentType = :arbitrary ) =
149- ones (auto_blocks (dims), eltype, dims; assignment)
150-
151- function Base. zeros (p:: Blocks , eltype:: Type , dims:: Dims ; assignment:: AssignmentType = :arbitrary )
156+ Base. ones (p:: BlocksOrAuto , T:: Type , dims:: Integer... ; assignment:: AssignmentType = :arbitrary ) =
157+ ones (p, T, dims; assignment)
158+ Base. ones (p:: BlocksOrAuto , dims:: Integer... ; assignment:: AssignmentType = :arbitrary ) =
159+ ones (p, Float64, dims; assignment)
160+ Base. ones (p:: BlocksOrAuto , dims:: Dims ; assignment:: AssignmentType = :arbitrary ) =
161+ ones (p, Float64, dims; assignment)
162+ Base. ones (:: AutoBlocks , T:: Type , dims:: Dims ; assignment:: AssignmentType = :arbitrary ) =
163+ ones (auto_blocks (dims), T, dims; assignment)
164+
165+ function Base. zeros (p:: Blocks , T:: Type , dims:: Dims ; assignment:: AssignmentType = :arbitrary )
152166 d = ArrayDomain (map (x-> 1 : x, dims))
153- a = AllocateArray (eltype , zeros, false , d, partition (p, d), p, assignment)
167+ a = AllocateArray (T , zeros, false , d, partition (p, d), p, assignment)
154168 return _to_darray (a)
155169end
156- Base. zeros (p:: BlocksOrAuto , T:: Type , dims:: Integer... ; assignment:: AssignmentType = :arbitrary ) = zeros (p, T, dims; assignment)
157- Base. zeros (p:: BlocksOrAuto , dims:: Integer... ; assignment:: AssignmentType = :arbitrary ) = zeros (p, Float64, dims; assignment)
158- Base. zeros (p:: BlocksOrAuto , dims:: Dims ; assignment:: AssignmentType = :arbitrary ) = zeros (p, Float64, dims; assignment)
159- Base. zeros (:: AutoBlocks , eltype:: Type , dims:: Dims ; assignment:: AssignmentType = :arbitrary ) =
160- zeros (auto_blocks (dims), eltype, dims; assignment)
170+ Base. zeros (p:: BlocksOrAuto , T:: Type , dims:: Integer... ; assignment:: AssignmentType = :arbitrary ) =
171+ zeros (p, T, dims; assignment)
172+ Base. zeros (p:: BlocksOrAuto , dims:: Integer... ; assignment:: AssignmentType = :arbitrary ) =
173+ zeros (p, Float64, dims; assignment)
174+ Base. zeros (p:: BlocksOrAuto , dims:: Dims ; assignment:: AssignmentType = :arbitrary ) =
175+ zeros (p, Float64, dims; assignment)
176+ Base. zeros (:: AutoBlocks , T:: Type , dims:: Dims ; assignment:: AssignmentType = :arbitrary ) =
177+ zeros (auto_blocks (dims), T, dims; assignment)
161178
162179function Base. zero (x:: DArray{T,N} ) where {T,N}
163180 dims = ntuple (i-> x. domain. indexes[i]. stop, N)
0 commit comments