1

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
Toby
  • 21
  • 4
  • Can you give us more insight on what the parameters in that line are for? I can't suggest anything without knowing what the purpose of each of them are for. Specifically, what is `neib`? Also, what is the purpose of `i`? Is this the iteration number? – rayryeng Jan 08 '15 at 18:31
  • 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. – Toby Jan 08 '15 at 18:33
  • So if I'm understanding your problem properly, do the results from the current iteration rely on any results from the previous iteration? Do you know all of the values of `neib` before hand, or is this dynamically computed at each iteration? – rayryeng Jan 08 '15 at 18:35
  • all values of neib are known beforehand. Everything in this line is static, except for the size of onearray. In fact, the element of connect(1,i) is where neib is stored. – Toby Jan 08 '15 at 18:47
  • In old Matlab versions it seems that using `i` as a variable name can slow things down (see comments to [this answer, or see [this other answer](http://stackoverflow.com/a/17723167/2586922)](http://stackoverflow.com/a/14790765/2586922)). Try changing that name and see if it helps – Luis Mendo Jan 08 '15 at 19:34
  • divide the structure into parts that fit into cache of cpu,gpu. – huseyin tugrul buyukisik Jan 08 '15 at 19:45
  • @huseyintugrulbuyukisik - You probably are unaware with how MATLAB works. If you want access to the GPU, you would need to use the `gpuArray` structure in MATLAB and MATLAB only supports nVidia GPUs for computation. Unless the OP has a nVidia card, this option won't work. Also, judging from the context of the OP's problem, vectorization can surely be achieved which doesn't require any GPU acceleration. I don't have the time to write an answer now as I'm at work, but I do have a couple of ideas that I want to try later tonight. – rayryeng Jan 08 '15 at 19:53
  • 3
    Could you supply more of the loop that surrounds this line? I suspect the answer to speeding up this line is to vectorize the entire loop, or at least this portion of it. – Peter Jan 08 '15 at 21:01
  • surrounding code added to original post – Toby Jan 12 '15 at 20:16

0 Answers0