# Matrix multiplication time explodes after 20k steps

3 views (last 30 days)
Arne on 3 Jul 2012
Hey all,
I am having some trouble with MATLAB: I wrote this tiny function in which for some reason the time required to do the multiplication w*neuro_act increases by a factor of ~10 after 20k steps.Even saving all data from RAM to disk, clearing all memory and loading it again from the disk doesn`t solve the problem... Can anybody help ? (w is a 4096x4096 Matrix, n is about 30k, run_factor around 40.)
function[electrodes,t_multi]=run_rat_run(pattern,w,sampling,lattice_size,dt,tau,vel_x_1,vel_y_1,alpha)
run_factor=sampling/(dt/1000);
n=size(vel_x_1,1);
neuro_act=reshape(pattern, lattice_size^2, 1);
[r, c]=max(neuro_act);
clear r
electrodes=zeros(n,3);
t_multi=zeros(n,1);
global_time_constant=dt/tau;
for x=1:n
velocity_input=1+alpha*repmat([repmat([vel_x_1(x);vel_y_1(x)],lattice_size/2,1);...
repmat([-vel_y_1(x);-vel_x_1(x)],lattice_size/2,1)],lattice_size/2,1);
for y_n=1:run_factor
arg_f=w*neuro_act+velocity_input;
arg_f(arg_f<0)=0;
neuro_act=neuro_act+global_time_constant*(arg_f-neuro_act);
end
tic
test_=w*neuro_act;
t_multi(x)=toc;
test=[];
t_multi(x)
x
electrodes(x,1)=neuro_act(c);
electrodes(x,2)=neuro_act(c+100);
electrodes(x,3)=neuro_act(c-100);
if mod(x,500) == 0
save temp w neuro_act t_multi electrodes x run_factor alpha global_time_constant sampling lattice_size c
clear all
end
end

Jan on 3 Jul 2012
The "save, clear all, load" is voodoo. It decreases the readability of your example code only. It would be helpful, if you post a program we can run. Then some tests with the PROFILEr and inspecting the memory usage might reveal the problem.
Kye Taylor on 3 Jul 2012
You shouldn't want to read from disc each iteration anyways. doing so severly hinders the execution time. Can you just load it once?
Greg on 3 Jul 2012
Well, there are instances where the saving and clearing makes sense... But if you're worried about memory fragmentation, the proper thing to use would be the "pack" function. If you're trying to clear all variables except for those ones that you're storing, try a "clearvars -except" command.