Advertisement
Guest User

Untitled

a guest
Jul 22nd, 2019
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.88 KB | None | 0 0
  1. %% Test different sparse summation implementation
  2. % Run the loop until time between outputs becomes unbearable, then cancel execution and plot results
  3.  
  4. % starting parameters
  5. N = 10000;
  6. density = .001;
  7.  
  8. previousNumLoops = 0;
  9. numLoops = 2;
  10.  
  11. runtimes = struct;
  12. ind = 1;
  13.  
  14. while 1
  15. % Setup dummy data matrices
  16. for i = (previousNumLoops+1):numLoops
  17. Rcell(i) = {sprand(N,N,density)};
  18. end
  19.  
  20. runtimes(ind).numLoops = numLoops;
  21.  
  22. % Evaluate implementation performance matrix multiplication
  23. runtimes(ind).ConstructSparse = ConstructSparse(N, Rcell, numLoops);
  24. runtimes(ind).AddSparse = AddSparse(N, Rcell, numLoops);
  25.  
  26. disp(runtimes(ind))
  27.  
  28. ind = ind + 1;
  29. previousNumLoops = numLoops;
  30. numLoops = numLoops + 2;
  31.  
  32. end
  33.  
  34. %% Plot runtime results
  35.  
  36. % Run to create a plot of the results
  37. PlotRuntimes(runtimes, {'ConstructSparse','AddSparse'});
  38.  
  39. %% Functions to time implementation and plot runtimes according to scaling factor
  40.  
  41. % measure accumulation of nonzero value properties and construction of the sparse matrix after loop
  42. function runtime = ConstructSparse(N, Rcell, numLoops)
  43. tic
  44.  
  45. sparseInds = cell(numLoops,3);
  46. for i = 1:numLoops
  47. [rowInds,colInds,nonzeroVals] = find(Rcell{i});
  48. sparseInds(i,:) = {rowInds,colInds,nonzeroVals};
  49. end
  50.  
  51. I = vertcat(sparseInds{:,1});
  52. J = vertcat(sparseInds{:,2});
  53. nonzeroValues = vertcat(sparseInds{:,3});
  54.  
  55. A = sparse(I,J,nonzeroValues,N,N);
  56.  
  57. runtime = toc;
  58.  
  59. end
  60.  
  61. % Measure summing of sparse matrices within loop
  62. function runtime = AddSparse(N, Rcell, numLoops)
  63. tic
  64.  
  65. A = sparse(N,N);
  66. for i = 1:numLoops
  67. A = A + Rcell{i};
  68. end
  69.  
  70. runtime = toc;
  71.  
  72. end
  73.  
  74. function [] = PlotRuntimes(runtimes, testNames)
  75. % Function to plot runtime results
  76. figure
  77. hold on;
  78. for label = testNames
  79. plot([runtimes(1:(end-1)).numLoops],[runtimes(1:(end-1)).(label{1})])
  80. end
  81. hold off
  82.  
  83. xlabel('Loop parameter')
  84. ylabel('Runtime(seconds)')
  85.  
  86. legend(testNames)
  87.  
  88. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement