I have a code that repeatedly calculates a sparse matrix in a loop (it performs this calculation 13472 times to be precise). Each of these sparse matrices is unique.
After each execution, it adds the newly calculated sparse matrix to what was originally a sparse zero matrix.
When all 13742 matrices have been added, the code exits the loop and the program terminates.
The code bottleneck occurs in adding the sparse matrices. I have made a dummy version of the code that exhibits the same behavior as my real code. It consists of a MATLAB function and a script given below.
(1) Function that generates the sparse matrix:
function out = test_evaluate_stiffness(n)ind = randi([1 n*n],300,1);val = rand(300,1);[I,J] = ind2sub([n,n],ind);out = sparse(I,J,val,n,n);end
(2) Main script (program)
% Calculate the stiffness matrix
n=1000;K=sparse([],[],[],n,n,n^2);ticfor i=1:13472 temp=rand(1)*test_evaluate_stiffness(n); K=K+temp;endfprintf('Stiffness Calculation Complete\nTime taken = %f s\n',toc)
I'm not very familiar with sparse matrix operations so I may be missing a critical point here that may allow my code to be sped up considerably.
Am I handling the updating of my stiffness matrix in a reasonable way in my code? Is there another way that I should be using sparse that will result in a faster assembly?
Note that I have coupling in my system so the the values by which I augment my global stiffness matrix in each loop execution aren't necessarily on the diagonal.
A profiler report is also provided below:
%
Best Answer