
| Current Path : /var/www/web-klick.de/dsh/50_dev2017/1310__algorithms/Julia/ |
Linux ift1.ift-informatik.de 5.4.0-216-generic #236-Ubuntu SMP Fri Apr 11 19:53:21 UTC 2025 x86_64 |
| Current File : /var/www/web-klick.de/dsh/50_dev2017/1310__algorithms/Julia/wsum.jl |
macro code_wsumfuns(AN, fname)
h = codegen_helper(AN)
t1 = h.term(1)
ti = h.term(:i)
tidx = h.term(:idx)
fname! = symbol("$(fname)!")
_fname! = symbol("_$(fname)!")
quote
global $(fname)
function ($fname)(w::ContiguousRealArray, $(h.aparams...))
n::Int = $(h.inputlen)
n == length(w) || error("Inconsistent argument dimensions.")
if n == 0
0.0
else
@inbounds s = $(t1) * w[1]
for i = 2 : n
@inbounds s += ($ti) * w[i]
end
s
end
end
global $(_fname!)
function $(_fname!)(dst::ContiguousArray, m::Int, n::Int, k::Int, w::ContiguousRealArray, $(h.aparams...))
if n == 1 # each page has a single row (simply evaluate)
@inbounds w1 = w[1]
for i = 1:m*k
@inbounds dst[i] = w1 * $(ti)
end
elseif m == 1 # each page has a single column
idx = 0
for l = 1:k
idx += 1
@inbounds s = w[1] * ($tidx)
for j = 2:n
idx += 1
@inbounds s += w[j] * ($tidx)
end
@inbounds dst[l] = s
end
elseif k == 1 # only one page
@inbounds w1 = w[1]
for i = 1:m
@inbounds dst[i] = w1 * $(ti)
end
idx = m
for j = 2:n
@inbounds wj = w[j]
for i = 1:m
idx += 1
@inbounds dst[i] += wj * $(tidx)
end
end
else # multiple generic pages
idx = 0
od = 0
@inbounds w1 = w[1]
for l = 1:k
for i = 1:m
idx += 1
@inbounds dst[od+i] = w1 * $(tidx)
end
for j = 2:n
@inbounds wj = w[j]
for i = 1:m
idx += 1
odi = od + i
@inbounds dst[odi] += wj * $(tidx)
end
end
od += m
end
end
return dst
end
global $(fname!)
function $(fname!)(dst::ContiguousArray, w::ContiguousRealArray, $(h.aparams...), dim::Int)
shp = $(h.inputsize)
1 <= dim <= length(shp) || error("Invalid value of dim.")
length(w) == shp[dim] || error("Inconsistent argument dimensions.")
m = prec_length(shp, dim)
n = shp[dim]
k = succ_length(shp, dim)
$(_fname!)(dst, m, n, k, w, $(h.args...))
end
global $(fname)
function ($fname)(w::ContiguousRealArray, $(h.aparams...), dim::Int)
tt = $(h.termtype)
shp = $(h.inputsize)
r = Array(promote_type(tt, eltype(w)), reduced_shape(shp, dim))
($fname!)(r, w, $(h.args...), dim)
end
end
end
@code_wsumfuns 0 wsum
@code_wsumfuns 1 wsum
@code_wsumfuns 2 wsum
@code_wsumfuns 3 wsum
@code_wsumfuns (-2) wsumfdiff
wsumsqdiff(w::ContiguousRealArray, x::ContiguousArray, y::ContiguousArrOrNum) =
wsumfdiff(w, Abs2Fun(), x, y)
wsumsqdiff!(dst::ContiguousRealArray, w::ContiguousRealArray,
x::ContiguousArray, y::ContiguousArrOrNum, dim::Int) =
wsumfdiff!(dst, w, Abs2Fun(), x, y, dim)
wsumsqdiff(w::ContiguousRealArray, x::ContiguousArray, y::ContiguousArrOrNum,
dim::Int) = wsumfdiff(w, Abs2Fun(), x, y, dim)