I’m trying to figure out why the profiler and @code_warntype
are picking up dynamic dispatch in the following MWE. The basic function applies matrix multiplications to each column of a matrix array and accumulates the result to another matrix array.
u = rand(4,100)
v = rand(4,100)
A = rand(4,4)
U = (u,v)
function init(A)
mxm_accum!(X,x,j) = X[:,j] += 2 * (A*x)
return mxm_accum!
end
mxm_accum! = init(A)
function foo(A,U)
out = (x->A*x).(U)
for j = 1:size(U[1],2)
Uj = getindex.(U,:,j)
mxm_accum!.(out,Uj,j)
end
return out
end
However, when I run profiler on it, it shows that the broadcasted function mxm_accum!
is being dynamically dispatched.
@code_warntype
shows that the broadcast in this step seems to be type unstable, but I can’t figure out why this would be the case.