I'm running a simulation which keeps track of displacements of individual points (~1000-100,000 depending on geometry). When calculating the next time step acceleration, I need to access the current displacement. The data is kept in a very large 2d array (1000x2) and is accessed with the following line:
eta = U2(connect(2:neib+1,i),:) - onearray*U2(i,:);
U2 is the displacement array, connect is a 2d array (61x1000) filled with integers. This line is run for each point and is the bottleneck, taking ~33% of the total compute time.
So my question is how can I speed up this line? I'm guessing the slowness has to do with accessing so many points in the array, so is there a way to decrease access time?
Edit: neib is an integer from 9 to 61 which keeps track of non-zero rows in connect. i is the iteration number, from 1 to number of points. Onearray = ones(neib,1) pre-allocated to reduce time.
Here's the entire loop around it:
for ii=1:nnodes % #for each node (nodes are points)
neib = connect(1,ii);
onearray = ones(neib,1);
eta= U2(connect(2:neib+1,ii),:) - onearray*U2(ii,:); % #calculates stretch of bonds and subtracts stretch of node
if eta == 0
continue
end
xi=xiall(2:neib+1,:,ii); % #gets vector distance for all horpts for node 'i' (2:total # of horpts+1,all,node)
od=odall(2:neib+1,ii); % #gets magnitude for all horpts for node 'i' (2:total # of horpts+1,all,node)
cp=xi+eta; % #adds stretch to (x-x') (cp is the current relative position vector from node to each bond)
cd = sqrt(cp(:,1).^2 + cp(:,2).^2); % #cd (vector) is the new radii from node to horpts
s=cd./od-1; % #strain (by definition)
f=C*s./cd*A; % #calculates unit force density function
UD(ii,:)=UD(ii,:)+sum([f f].*cp); % #adding force density function to equation of motion
end