Skip to content

Commit a430568

Browse files
author
Andy Ferris
committed
Removed dodgy promote_op from generators
Fixes #79
1 parent 3d8c9be commit a430568

File tree

2 files changed

+21
-24
lines changed

2 files changed

+21
-24
lines changed

src/mapreduce.jl

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@
44

55
# Single input
66
@generated function map{T}(f, a1::StaticArray{T})
7-
newtype = :(similar_type($a1, promote_op(f, T)))
87
exprs = [:(f(a1[$j])) for j = 1:length(a1)]
98
return quote
109
$(Expr(:meta, :inline))
11-
@inbounds return $(Expr(:call, newtype, Expr(:tuple, exprs...)))
10+
newtype = similar_type(typeof(a1), promote_op(f, T))
11+
@inbounds return $(Expr(:call, :newtype, Expr(:tuple, exprs...)))
1212
end
1313
end
1414

@@ -18,17 +18,16 @@ end
1818
error("Dimensions must match. Got sizes $(size(a1)) and $(size(a2))")
1919
end
2020

21-
newtype = :(similar_type($a1, promote_op(f, T1, T2)))
2221
exprs = [:(f(a1[$j], a2[$j])) for j = 1:length(a1)]
2322
return quote
2423
$(Expr(:meta, :inline))
25-
@inbounds return $(Expr(:call, newtype, Expr(:tuple, exprs...)))
24+
newtype = similar_type(typeof(a1), promote_op(f, T1, T2))
25+
@inbounds return $(Expr(:call, :newtype, Expr(:tuple, exprs...)))
2626
end
2727
end
2828

2929
# TODO these assume linear fast...
3030
@generated function map{T1,T2}(f, a1::StaticArray{T1}, a2::AbstractArray{T2})
31-
newtype = :(similar_type($a1, promote_op(f, T1, T2)))
3231
exprs = [:(f(a1[$j], a2[$j])) for j = 1:length(a1)]
3332
return quote
3433
$(Expr(:meta, :inline))
@@ -37,12 +36,12 @@ end
3736
error("Dimensions must match. Got sizes $(size(a1)) and $(size(a2))")
3837
end
3938

40-
@inbounds return $(Expr(:call, newtype, Expr(:tuple, exprs...)))
39+
newtype = similar_type(typeof(a1), promote_op(f, T1, T2))
40+
@inbounds return $(Expr(:call, :newtype, Expr(:tuple, exprs...)))
4141
end
4242
end
4343

4444
@generated function map{T1,T2}(f, a1::AbstractArray{T1}, a2::StaticArray{T2})
45-
newtype = :(similar_type($a2, promote_op(f, T1, T2)))
4645
exprs = [:(f(a1[$j], a2[$j])) for j = 1:length(a2)]
4746
return quote
4847
$(Expr(:meta, :inline))
@@ -51,7 +50,8 @@ end
5150
error("Dimensions must match. Got sizes $(size(a1)) and $(size(a2))")
5251
end
5352

54-
@inbounds return $(Expr(:call, newtype, Expr(:tuple, exprs...)))
53+
newtype = similar_type(typeof(a2), promote_op(f, T1, T2))
54+
@inbounds return $(Expr(:call, :newtype, Expr(:tuple, exprs...)))
5555
end
5656
end
5757

src/matrix_multiply.jl

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,8 @@ import Base: A_mul_B!, Ac_mul_B!, A_mul_Bc!, Ac_mul_Bc!, At_mul_B!, A_mul_Bt!, A
33

44
typealias BlasEltypes Union{Float64, Float32, Complex{Float64}, Complex{Float32}}
55

6-
# Stolen from https://github.com/JuliaLang/julia/pull/18218
7-
matprod(x,y) = x*y + x*y
8-
# I think promote_op has a pure context problem... and we use lots of generated functions
9-
# Attempt to retrofit this and revert it to rely on `zero` (which is a bit annoying!)
10-
@pure promote_op{T1,T2}(::typeof(matprod), ::Type{T1}, ::Type{T2}) = typeof(zero(T1)*zero(T2) + zero(T1)*zero(T2))
6+
# Idea inspired by https://github.com/JuliaLang/julia/pull/18218
7+
promote_matprod{T1,T2}(::Type{T1}, ::Type{T2}) = typeof(zero(T1)*zero(T2) + zero(T1)*zero(T2))
118

129
# TODO Potentially a loop version for rather large arrays? Or try and figure out inference problems?
1310

@@ -96,7 +93,7 @@ end
9693
sb = size(b)
9794

9895
s = (sA[1],)
99-
T = promote_op(matprod, TA, Tb)
96+
T = promote_matprod(TA, Tb)
10097
#println(T)
10198

10299
if sb[1] != sA[2]
@@ -135,7 +132,7 @@ end
135132
sb = size(b)
136133

137134
s = (sA[1],)
138-
T = promote_op(matprod, TA, Tb)
135+
T = promote_matprod(TA, Tb)
139136
#println(T)
140137

141138
if sb[1] != sA[2]
@@ -174,7 +171,7 @@ end
174171
#sb = size(b)
175172

176173
s = (sA[1],)
177-
T = promote_op(matprod, TA, Tb)
174+
T = promote_matprod(TA, Tb)
178175

179176
if T == TA
180177
newtype = similar_type(A, s)
@@ -202,7 +199,7 @@ end
202199
sA = size(A)
203200

204201
s = (sA[1],)
205-
T = promote_op(matprod, TA, Tb)
202+
T = promote_matprod(TA, Tb)
206203

207204
if T == TA
208205
newtype = similar_type(A, s)
@@ -232,7 +229,7 @@ end
232229
sB = size(B)
233230

234231
s = (sa[1],sB[2])
235-
T = promote_op(matprod, Ta, TB)
232+
T = promote_matprod(Ta, TB)
236233

237234
if sB[1] != 1
238235
error("Dimension mismatch")
@@ -266,7 +263,7 @@ end
266263
TA = eltype(A)
267264
TB = eltype(B)
268265

269-
T = promote_op(matprod, TA, TB)
266+
T = promote_matprod(TA, TB)
270267

271268
can_mutate = !isbits(A) || !isbits(B) # !isbits implies can get a persistent pointer (to pass to BLAS). Probably will change to !isimmutable in a future version of Julia.
272269
can_blas = T == TA && T == TB && T <: Union{Float64, Float32, Complex{Float64}, Complex{Float32}}
@@ -328,7 +325,7 @@ end
328325
TB = eltype(B)
329326

330327
s = (sA[1], sB[2])
331-
T = promote_op(matprod, TA, TB)
328+
T = promote_matprod(TA, TB)
332329

333330
if sB[1] != sA[2]
334331
error("Dimension mismatch")
@@ -369,7 +366,7 @@ end
369366
TB = eltype(B)
370367

371368
s = (sA[1], sB[2])
372-
T = promote_op(matprod, TA, TB)
369+
T = promote_matprod(TA, TB)
373370

374371
if sB[1] != sA[2]
375372
error("Dimension mismatch")
@@ -414,7 +411,7 @@ end
414411
TB = eltype(B)
415412

416413
s = (sA[1], sB[2])
417-
T = promote_op(matprod, TA, TB)
414+
T = promote_matprod(TA, TB)
418415

419416
if sB[1] != sA[2]
420417
error("Dimension mismatch")
@@ -458,7 +455,7 @@ end
458455
sb = size(b)
459456

460457
s = (sA[1],)
461-
T = promote_op(matprod, TA, Tb)
458+
T = promote_matprod(TA, Tb)
462459

463460
if sb[1] != sA[2]
464461
error("Dimension mismatch")
@@ -621,7 +618,7 @@ end
621618

622619
TA = eltype(A)
623620
TB = eltype(B)
624-
T = promote_op(matprod, TA, TB)
621+
T = promote_matprod(TA, TB)
625622

626623
can_blas = T == TA && T == TB && T <: Union{Float64, Float32, Complex{Float64}, Complex{Float32}}
627624

0 commit comments

Comments
 (0)