-
Notifications
You must be signed in to change notification settings - Fork 1
/
demat_be_julia.jl
145 lines (116 loc) · 3.66 KB
/
demat_be_julia.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
## Delayed Expressions Julia Backend
# Copyright 2012-2013, Krzysztof Kamieniecki ([email protected])
require("demat_base.jl")
type DeBackEndJulia
end
type DeArrJulia{T,N} <: DeArr{DeBackEndJulia,T,N}
DeArrJulia(a::Array{T,N}) = new(a)
DeArrJulia(a::Number) = new([convert(T,a)])
data::Array{T,N}
end
size(a::DeArrJulia) = size(a.data)
size(a::DeArrJulia,dim) = size(a.data,dim)
typealias DeVecJ{T} DeArrJulia{T,1}
typealias DeMatJ{T} DeArrJulia{T,2}
# de_jl_eval returns a 3-tuple that contains
# symbol that contains the value of the extression
# result type
# the quoted preamble code
# the quoted kernal code
function de_jl_eval(a::DeConst,param,idxSym)
@gensym r
( r
, typeof(a.p1)
, quote ($r) = ($param).p1 end
, quote end
)
end
function de_jl_eval(a::DeReadOp,param,idxSym)
@gensym r src
( r
, eltype(a.p1.data)
, quote ($src) = ($param).p1.data end
, quote ($r) = ($src)[($idxSym)] end
)
end
function de_jl_eval(v::DeUniOp,param,idxSym)
@gensym r
p1 = de_jl_eval(v.p1,quote ($param).p1 end,idxSym)
( r
, promote_type(p1[2])
, quote $(p1[3]) end
, quote $(p1[4]);($r) = de_jl_do_op($v,$(p1[1])) end
)
end
function de_jl_eval(v::DeBinOp,param,idxSym)
@gensym r
p1 = de_jl_eval(v.p1,quote ($param).p1 end,idxSym)
p2 = de_jl_eval(v.p2,quote ($param).p2 end,idxSym)
( r
, promote_type(p1[2],p2[2])
, quote $(p1[3]);$(p2[3]) end
, quote $(p1[4]);$(p2[4]);($r) = de_jl_do_op($v,$(p1[1]),$(p2[1])) end
)
end
for op = keys(deUniOpMap)
opSingle = deUniOpMap[op];
@eval de_jl_do_op{P1}(v::DeUniOp{$(expr(:quote, op)),P1},a) = ($opSingle)(a)
end
for op = keys(deBinOpMap)
opSingle = deBinOpMap[op];
@eval de_jl_do_op{P1,P2}(v::DeBinOp{$(expr(:quote, op)),P1,P2},a,b) = ($opSingle)(a,b)
#@eval de_jl_do_op(v::DeBinOp{$op},a,b) = ($opSingle)(a,b)
#@eval function de_jl_do_op(v::DeBinOp{$opType},a,b) = ($opSingle)(a,b) end # does not work?
end
function assign(lhs::DeVecJ,rhs::DeExpr)
buildTime = @elapsed begin
@gensym i prhs
(rhsResult,rhsResultType,rhsPreamble,rhsKernel) = de_jl_eval(rhs,prhs,i)
rhsType = typeof(rhs);
@eval function assign1(plhs::DeVecJ,($prhs)::($rhsType))
rhsSz = de_check_dims($prhs)
lhsSz = size(plhs)
if rhsSz != lhsSz
error("src & dst size does not match. NOT IMPLEMENTED FOR SCALARS FIX")
end
N = size(plhs,1)
lhsData = plhs.data
$rhsPreamble
for ($i) = 1:N
$rhsKernel
lhsData[($i)] = ($rhsResult)
end
return plhs
end
global assign
@eval assign(lhs::DeVecJ,rhs::($rhsType)) = assign1(lhs,rhs)
end
println("DeMatJulia: Built New Assign (took $buildTime seconds) ... $rhsType");
return assign1(lhs,rhs)
end
assign(lhs::DeArrJulia,rhs::DeEle) = assign(lhs,de_promote(rhs)...)
assign(lhs::DeArrJulia,rhs::Number) = assign(lhs,de_promote(rhs)...)
## TODO quick hack for sum, should be generalized for reduce
function sum(rhs::DeExpr)
buildTime = @elapsed begin
@gensym i prhs
(rhsResult,rhsResultType,rhsPreamble,rhsKernel) = de_jl_eval(rhs,prhs,i)
rhsType = typeof(rhs);
@eval function sum1(($prhs)::($rhsType))
rhsSz = de_check_dims($prhs)
N = rhsSz[1]
sumV = convert($rhsResultType,0)
$rhsPreamble
for ($i) = 1:N
$rhsKernel
sumV += ($rhsResult)
end
return sumV
end
global sum
@eval sum(rhs::($rhsType)) = sum1(rhs)
end
println("DeMatJulia: Built New Sum (took $buildTime seconds) ... $rhsType");
return sum1(rhs)
end
sum(rhs::DeArrJulia) = sum(de_promote(rhs)...)