-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
/
multidimensional.jl
590 lines (500 loc) · 17.8 KB
/
multidimensional.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
### From array.jl
@ngenerate N Nothing function checksize(A::AbstractArray, I::NTuple{N, Any}...)
@nexprs N d->(size(A, d) == length(I_d) || throw(DimensionMismatch("index $d has length $(length(I_d)), but size(A, $d) = $(size(A,d))")))
nothing
end
unsafe_getindex(v::Real, ind::Int) = v
unsafe_getindex(v::Range, ind::Int) = first(v) + (ind-1)*step(v)
unsafe_getindex(v::BitArray, ind::Int) = Base.unsafe_bitgetindex(v.chunks, ind)
unsafe_getindex(v::AbstractArray, ind::Int) = v[ind]
unsafe_getindex(v, ind::Real) = unsafe_getindex(v, to_index(ind))
unsafe_setindex!{T}(v::AbstractArray{T}, x::T, ind::Int) = (v[ind] = x; v)
unsafe_setindex!(v::BitArray, x::Bool, ind::Int) = (Base.unsafe_bitsetindex!(v.chunks, x, ind); v)
unsafe_setindex!{T}(v::AbstractArray{T}, x::T, ind::Real) = unsafe_setindex!(v, x, to_index(ind))
# Version that uses cartesian indexing for src
@ngenerate N typeof(dest) function _getindex!(dest::Array, src::AbstractArray, I::NTuple{N,Union(Int,AbstractVector)}...)
checksize(dest, I...)
k = 1
@nloops N i dest d->(@inbounds j_d = unsafe_getindex(I_d, i_d)) begin
@inbounds dest[k] = (@nref N src j)
k += 1
end
dest
end
# Version that uses linear indexing for src
@ngenerate N typeof(dest) function _getindex!(dest::Array, src::Array, I::NTuple{N,Union(Int,AbstractVector)}...)
checksize(dest, I...)
stride_1 = 1
@nexprs N d->(stride_{d+1} = stride_d*size(src,d))
@nexprs N d->(offset_d = 1) # only really need offset_$N = 1
k = 1
@nloops N i dest d->(@inbounds offset_{d-1} = offset_d + (unsafe_getindex(I_d, i_d)-1)*stride_d) begin
@inbounds dest[k] = src[offset_0]
k += 1
end
dest
end
# It's most efficient to call checkbounds first, then to_index, and finally
# allocate the output. Hence the different variants.
_getindex(A, I::(Union(Int,AbstractVector)...)) =
_getindex!(similar(A, index_shape(I...)), A, I...)
@nsplat N function getindex(A::Array, I::NTuple{N,Union(Real,AbstractVector)}...)
checkbounds(A, I...)
_getindex(A, to_index(I...))
end
# Also a safe version of getindex!
@nsplat N function getindex!(dest, src, I::NTuple{N,Union(Real,AbstractVector)}...)
checkbounds(src, I...)
_getindex!(dest, src, to_index(I...)...)
end
@ngenerate N typeof(A) function setindex!(A::Array, x, J::NTuple{N,Union(Real,AbstractArray)}...)
@ncall N checkbounds A J
@nexprs N d->(I_d = to_index(J_d))
stride_1 = 1
@nexprs N d->(stride_{d+1} = stride_d*size(A,d))
@nexprs N d->(offset_d = 1) # really only need offset_$N = 1
if !isa(x, AbstractArray)
@nloops N i d->(1:length(I_d)) d->(@inbounds offset_{d-1} = offset_d + (unsafe_getindex(I_d, i_d)-1)*stride_d) begin
@inbounds A[offset_0] = x
end
else
X = x
@ncall N setindex_shape_check X I
# TODO? A variant that can use cartesian indexing for RHS
k = 1
@nloops N i d->(1:length(I_d)) d->(@inbounds offset_{d-1} = offset_d + (unsafe_getindex(I_d, i_d)-1)*stride_d) begin
@inbounds A[offset_0] = X[k]
k += 1
end
end
A
end
@ngenerate N NTuple{N,Vector{Int}} function findn{T,N}(A::AbstractArray{T,N})
nnzA = countnz(A)
@nexprs N d->(I_d = Array(Int, nnzA))
k = 1
@nloops N i A begin
@inbounds if (@nref N A i) != zero(T)
@nexprs N d->(I_d[k] = i_d)
k += 1
end
end
@ntuple N I
end
### subarray.jl
# Here we want to skip creating the dict-based cached version,
# so use the ngenerate function
function gen_getindex_body(N::Int)
quote
strd_1 = 1
@nexprs $N d->(@inbounds strd_{d+1} = strd_d*s.dims[d])
ind -= 1
indp = s.first_index
@nexprs $N d->begin
i = div(ind, strd_{$N-d+1})
@inbounds indp += i*s.strides[$N-d+1]
ind -= i*strd_{$N-d+1}
end
s.parent[indp]
end
end
eval(ngenerate(:N, nothing, :(getindex{T}(s::SubArray{T,N}, ind::Integer)), gen_getindex_body, 2:5, false))
function gen_setindex!_body(N::Int)
quote
strd_1 = 1
@nexprs $N d->(@inbounds strd_{d+1} = strd_d*s.dims[d])
ind -= 1
indp = s.first_index
@nexprs $N d->begin
i = div(ind, strd_{$N-d+1})
@inbounds indp += i*s.strides[$N-d+1]
ind -= i*strd_{$N-d+1}
end
s.parent[indp] = v
end
end
eval(ngenerate(:N, nothing, :(setindex!{T}(s::SubArray{T,N}, v, ind::Integer)), gen_setindex!_body, 2:5, false))
cumsum(A::AbstractArray, axis::Integer=1) = cumsum!(similar(A, Base._cumsum_type(A)), A, axis)
cumprod(A::AbstractArray, axis::Integer=1) = cumprod!(similar(A), A, axis)
for (f, op) in ((:cumsum!, :+),
(:cumprod!, :*))
@eval begin
@ngenerate N typeof(B) function ($f){T,N}(B, A::AbstractArray{T,N}, axis::Integer=1)
if size(B, axis) < 1
return B
end
size(B) == size(A) || throw(DimensionMismatch("Size of B must match A"))
if axis == 1
# We can accumulate to a temporary variable, which allows register usage and will be slightly faster
@inbounds @nloops N i d->(d > 1 ? (1:size(A,d)) : (1:1)) begin
tmp = convert(eltype(B), @nref(N, A, i))
@nref(N, B, i) = tmp
for i_1 = 2:size(A,1)
tmp = ($op)(tmp, @nref(N, A, i))
@nref(N, B, i) = tmp
end
end
else
@nexprs N d->(isaxis_d = axis == d)
# Copy the initial element in each 1d vector along dimension `axis`
@inbounds @nloops N i d->(d == axis ? (1:1) : (1:size(A,d))) @nref(N, B, i) = @nref(N, A, i)
# Accumulate
@inbounds @nloops N i d->((1+isaxis_d):size(A, d)) d->(j_d = i_d - isaxis_d) begin
@nref(N, B, i) = ($op)(@nref(N, B, j), @nref(N, A, i))
end
end
B
end
end
end
### from abstractarray.jl
@ngenerate N typeof(A) function fill!{T,N}(A::AbstractArray{T,N}, x)
@nloops N i A begin
@inbounds (@nref N A i) = x
end
A
end
@ngenerate N typeof(dest) function copy!{T,N}(dest::AbstractArray{T,N}, src::AbstractArray{T,N})
if @nall N d->(size(dest,d) == size(src,d))
@nloops N i dest begin
@inbounds (@nref N dest i) = (@nref N src i)
end
else
invoke(copy!, (typeof(dest), Any), dest, src)
end
dest
end
### BitArrays
## getindex
# general scalar indexing with two or more indices
# (uses linear indexing, which - in the safe version - performs the final
# bounds check and is defined in bitarray.jl)
# (code is duplicated for safe and unsafe versions for performance reasons)
@ngenerate N Bool function unsafe_getindex(B::BitArray, I_0::Int, I::NTuple{N,Int}...)
stride = 1
index = I_0
@nexprs N d->begin
stride *= size(B,d)
index += (I_d - 1) * stride
end
return unsafe_getindex(B, index)
end
@ngenerate N Bool function getindex(B::BitArray, I_0::Int, I::NTuple{N,Int}...)
stride = 1
index = I_0
@nexprs N d->begin
l = size(B,d)
stride *= l
1 <= I_{d-1} <= l || throw(BoundsError())
index += (I_d - 1) * stride
end
return B[index]
end
# contiguous multidimensional indexing: if the first dimension is a range,
# we can get some performance from using copy_chunks!
function unsafe_getindex(B::BitArray, I0::UnitRange{Int})
X = BitArray(length(I0))
copy_chunks!(X.chunks, 1, B.chunks, first(I0), length(I0))
return X
end
function getindex(B::BitArray, I0::UnitRange{Int})
checkbounds(B, I0)
return unsafe_getindex(B, I0)
end
getindex{T<:Real}(B::BitArray, I0::UnitRange{T}) = getindex(B, to_index(I0))
@ngenerate N BitArray{length(index_shape(I0, I...))} function unsafe_getindex(B::BitArray, I0::UnitRange{Int}, I::NTuple{N,Union(Int,UnitRange{Int})}...)
X = BitArray(index_shape(I0, I...))
f0 = first(I0)
l0 = length(I0)
gap_lst_1 = 0
@nexprs N d->(gap_lst_{d+1} = length(I_d))
stride = 1
ind = f0
@nexprs N d->begin
stride *= size(B, d)
stride_lst_d = stride
ind += stride * (first(I_d) - 1)
gap_lst_{d+1} *= stride
end
storeind = 1
@nloops(N, i, d->I_d,
d->nothing, # PRE
d->(ind += stride_lst_d - gap_lst_d), # POST
begin # BODY
copy_chunks!(X.chunks, storeind, B.chunks, ind, l0)
storeind += l0
end)
return X
end
# general multidimensional non-scalar indexing
@ngenerate N BitArray{length(index_shape(I...))} function unsafe_getindex(B::BitArray, I::NTuple{N,Union(Int,AbstractVector{Int})}...)
X = BitArray(index_shape(I...))
Xc = X.chunks
ind = 1
@nloops N i d->I_d begin
unsafe_bitsetindex!(Xc, (@ncall N unsafe_getindex B i), ind)
ind += 1
end
return X
end
# general version with Real (or logical) indexing which dispatches on the appropriate method
@ngenerate N BitArray{length(index_shape(I...))} function getindex(B::BitArray, I::NTuple{N,Union(Real,AbstractVector)}...)
checkbounds(B, I...)
return unsafe_getindex(B, to_index(I...)...)
end
## setindex!
# general scalar indexing with two or more indices
# (uses linear indexing, which - in the safe version - performs the final
# bounds check and is defined in bitarray.jl)
# (code is duplicated for safe and unsafe versions for performance reasons)
@ngenerate N typeof(B) function unsafe_setindex!(B::BitArray, x::Bool, I_0::Int, I::NTuple{N,Int}...)
stride = 1
index = I_0
@nexprs N d->begin
stride *= size(B,d)
index += (I_d - 1) * stride
end
unsafe_setindex!(B, x, index)
return B
end
@ngenerate N typeof(B) function setindex!(B::BitArray, x::Bool, I_0::Int, I::NTuple{N,Int}...)
stride = 1
index = I_0
@nexprs N d->begin
l = size(B,d)
stride *= l
1 <= I_{d-1} <= l || throw(BoundsError())
index += (I_d - 1) * stride
end
B[index] = x
return B
end
# contiguous multidimensional indexing: if the first dimension is a range,
# we can get some performance from using copy_chunks!
function unsafe_setindex!(B::BitArray, X::BitArray, I0::UnitRange{Int})
l0 = length(I0)
l0 == 0 && return B
f0 = first(I0)
copy_chunks!(B.chunks, f0, X.chunks, 1, l0)
return B
end
function unsafe_setindex!(B::BitArray, x::Bool, I0::UnitRange{Int})
l0 = length(I0)
l0 == 0 && return B
f0 = first(I0)
fill_chunks!(B.chunks, x, f0, l0)
return B
end
@ngenerate N typeof(B) function unsafe_setindex!(B::BitArray, X::BitArray, I0::UnitRange{Int}, I::NTuple{N,Union(Int,UnitRange{Int})}...)
length(X) == 0 && return B
f0 = first(I0)
l0 = length(I0)
gap_lst_1 = 0
@nexprs N d->(gap_lst_{d+1} = length(I_d))
stride = 1
ind = f0
@nexprs N d->begin
stride *= size(B, d)
stride_lst_d = stride
ind += stride * (first(I_d) - 1)
gap_lst_{d+1} *= stride
end
refind = 1
@nloops(N, i, d->I_d,
d->nothing, # PRE
d->(ind += stride_lst_d - gap_lst_d), # POST
begin # BODY
copy_chunks!(B.chunks, ind, X.chunks, refind, l0)
refind += l0
end)
return B
end
@ngenerate N typeof(B) function unsafe_setindex!(B::BitArray, x::Bool, I0::UnitRange{Int}, I::NTuple{N,Union(Int,UnitRange{Int})}...)
f0 = first(I0)
l0 = length(I0)
l0 == 0 && return B
@nexprs N d->(length(I_d) == 0 && return B)
gap_lst_1 = 0
@nexprs N d->(gap_lst_{d+1} = length(I_d))
stride = 1
ind = f0
@nexprs N d->begin
stride *= size(B, d)
stride_lst_d = stride
ind += stride * (first(I_d) - 1)
gap_lst_{d+1} *= stride
end
@nloops(N, i, d->I_d,
d->nothing, # PRE
d->(ind += stride_lst_d - gap_lst_d), # POST
begin # BODY
fill_chunks!(B.chunks, x, ind, l0)
end)
return B
end
# general multidimensional non-scalar indexing
@ngenerate N typeof(B) function unsafe_setindex!(B::BitArray, X::AbstractArray, I::NTuple{N,Union(Int,AbstractArray{Int})}...)
refind = 1
@nloops N i d->I_d @inbounds begin
@ncall N unsafe_setindex! B convert(Bool,X[refind]) i
refind += 1
end
return B
end
@ngenerate N typeof(B) function unsafe_setindex!(B::BitArray, x::Bool, I::NTuple{N,Union(Int,AbstractArray{Int})}...)
@nloops N i d->I_d begin
@ncall N unsafe_setindex! B x i
end
return B
end
# general versions with Real (or logical) indexing which dispatch on the appropriate method
# this one is for disambiguation only
function setindex!(B::BitArray, x, i::Real)
checkbounds(B, i)
return unsafe_setindex!(B, convert(Bool,x), to_index(i))
end
@ngenerate N typeof(B) function setindex!(B::BitArray, x, I::NTuple{N,Union(Real,AbstractArray)}...)
checkbounds(B, I...)
#return unsafe_setindex!(B, convert(Bool,x), to_index(I...)...) # segfaults! (???)
@nexprs N d->(J_d = to_index(I_d))
return @ncall N unsafe_setindex! B convert(Bool,x) J
end
# this one is for disambiguation only
function setindex!(B::BitArray, X::AbstractArray, i::Real)
checkbounds(B, i)
j = to_index(i)
setindex_shape_check(X, j)
return unsafe_setindex!(B, X, j)
end
@ngenerate N typeof(B) function setindex!(B::BitArray, X::AbstractArray, I::NTuple{N,Union(Real,AbstractArray)}...)
checkbounds(B, I...)
@nexprs N d->(J_d = to_index(I_d))
@ncall N setindex_shape_check X J
return @ncall N unsafe_setindex! B X J
end
## findn
@ngenerate N NTuple{N,Vector{Int}} function findn{N}(B::BitArray{N})
nnzB = countnz(B)
I = ntuple(N, x->Array(Int, nnzB))
if nnzB > 0
count = 1
@nloops N i B begin
if (@nref N B i) # TODO: should avoid bounds checking
@nexprs N d->(I[d][count] = i_d)
count += 1
end
end
end
return I
end
## isassigned
@ngenerate N Bool function isassigned(B::BitArray, I_0::Int, I::NTuple{N,Int}...)
stride = 1
index = I_0
@nexprs N d->begin
l = size(B,d)
stride *= l
1 <= I_{d-1} <= l || return false
index += (I_d - 1) * stride
end
return isassigned(B, index)
end
## permutedims
for (V, PT, BT) in {((:N,), BitArray, BitArray), ((:T,:N), Array, StridedArray)}
@eval @ngenerate N typeof(P) function permutedims!{$(V...)}(P::$PT{$(V...)}, B::$BT{$(V...)}, perm)
dimsB = size(B)
length(perm) == N || error("expected permutation of size $N, but length(perm)=$(length(perm))")
isperm(perm) || error("input is not a permutation")
dimsP = size(P)
for i = 1:length(perm)
dimsP[i] == dimsB[perm[i]] || throw(DimensionMismatch("destination tensor of incorrect size"))
end
#calculates all the strides
strides_1 = 0
@nexprs N d->(strides_{d+1} = stride(B, perm[d]))
#Creates offset, because indexing starts at 1
offset = 1 - sum(@ntuple N d->strides_{d+1})
if isa(B, SubArray)
offset += B.first_index - 1
B = B.parent
end
ind = 1
@nexprs 1 d->(counts_{N+1} = strides_{N+1}) # a trick to set counts_($N+1)
@nloops(N, i, P,
d->(counts_d = strides_d), # PRE
d->(counts_{d+1} += strides_{d+1}), # POST
begin # BODY
sumc = sum(@ntuple N d->counts_{d+1})
@inbounds P[ind] = B[sumc+offset]
ind += 1
end)
return P
end
end
## unique across dim
# TODO: this doesn't fit into the new hashing scheme in any obvious way
immutable Prehashed
hash::Uint
end
hash(x::Prehashed) = x.hash
@ngenerate N typeof(A) function unique{T,N}(A::AbstractArray{T,N}, dim::Int)
1 <= dim <= N || return copy(A)
hashes = zeros(Uint, size(A, dim))
# Compute hash for each row
k = 0
@nloops N i A d->(if d == dim; k = i_d; end) begin
@inbounds hashes[k] = hash(hashes[k], hash((@nref N A i)))
end
# Collect index of first row for each hash
uniquerow = Array(Int, size(A, dim))
firstrow = Dict{Prehashed,Int}()
for k = 1:size(A, dim)
uniquerow[k] = get!(firstrow, Prehashed(hashes[k]), k)
end
uniquerows = collect(values(firstrow))
# Check for collisions
collided = falses(size(A, dim))
@inbounds begin
@nloops N i A d->(if d == dim
k = i_d
j_d = uniquerow[k]
else
j_d = i_d
end) begin
if (@nref N A j) != (@nref N A i)
collided[k] = true
end
end
end
if any(collided)
nowcollided = BitArray(size(A, dim))
while any(collided)
# Collect index of first row for each collided hash
empty!(firstrow)
for j = 1:size(A, dim)
collided[j] || continue
uniquerow[j] = get!(firstrow, Prehashed(hashes[j]), j)
end
for v in values(firstrow)
push!(uniquerows, v)
end
# Check for collisions
fill!(nowcollided, false)
@nloops N i A d->begin
if d == dim
k = i_d
j_d = uniquerow[k]
(!collided[k] || j_d == k) && continue
else
j_d = i_d
end
end begin
if (@nref N A j) != (@nref N A i)
nowcollided[k] = true
end
end
(collided, nowcollided) = (nowcollided, collided)
end
end
@nref N A d->d == dim ? sort!(uniquerows) : (1:size(A, d))
end