Presentation is loading. Please wait.

Presentation is loading. Please wait.

MATLAB Tips for Simulation Parinya Sanguansat. Version Selection  X86  Faster  Smaller  X64  Slower  Bigger.

Similar presentations


Presentation on theme: "MATLAB Tips for Simulation Parinya Sanguansat. Version Selection  X86  Faster  Smaller  X64  Slower  Bigger."— Presentation transcript:

1 MATLAB Tips for Simulation Parinya Sanguansat

2 Version Selection  X86  Faster  Smaller  X64  Slower  Bigger

3 Section  Syntax: % section  Run Section  Run without save

4 Save the results  Long time processing  Power failure  Computer freeze and crash  TIP  Save and continue next time  Don't save all variables  Too many saves, slow down your work  Display progress (Text / GUI)  Redundant save file

5 Example work for i=1:100 result(i) = i end

6 Save and continue next time if exist('savedata','file') load savedata end if ~exist('startindex','var') startindex = 1; end for i=startindex:100 result(i) = i; startindex = i; save savedata end

7 Don't save all variables if exist('savedata','file') load savedata end if ~exist('startindex','var') startindex = 1; end for i=startindex:100 result(i) = i; startindex = i; save('savedata','startindex','result'); end

8 Too many saves, slow down your work if exist('savedata','file') load savedata end if ~exist('startindex','var') startindex = 1; end for i=startindex:100 result(i) = i; if mod(i,10) == 0 startindex = i; save('savedata','startindex','result'); end

9 Display progress if exist('savedata','file') load savedata end if ~exist('startindex','var') startindex = 1; end endindex = 100; fprintf('\n\n%6.2f%', 0); for i=startindex:endindex result(i) = i; fprintf('\b\b\b\b\b\b\b%6.2f%', i/endindex * 100); pause(1) if mod(i,10) == 0 startindex = i; save('savedata','startindex','result'); end

10 Display progress with GUI if exist('savedata','file') load savedata end if ~exist('startindex','var') startindex = 1; end endindex = 100; waitbar(startindex/endindex,'Processing...'); fprintf('\n\n%6.2f%', 0); for i=startindex:endindex result(i) = i; waitbar(i/endindex); fprintf('\b\b\b\b\b\b\b%6.2f%', i/endindex * 100); pause(1) if mod(i,10) == 0 startindex = i; save('savedata','startindex','result'); end

11 Redundant save file if exist('savedata','file') load savedata end if ~exist('startindex','var') startindex = 1; end endindex = 100; waitbar(startindex/endindex,'Processing...'); fprintf('\n\n%6.2f%', 0); for i=startindex:endindex result(i) = i; waitbar(i/endindex); fprintf('\b\b\b\b\b\b\b%6.2f%', i/endindex * 100); pause(1) if mod(i,10) == 0 startindex = i; save('savedata','startindex','result'); save('savedata2','startindex','result'); % redundant save data end

12 Speed up your work  Long time processing  Using huge memory  TIP  Display too much information will slow down your work  Allocate memory  Use small data type first  Access row (the first dimension) first  Eliminate nested loop  Array processing with GPU

13 Display % display too much information will slow down your work for i=1:100 result(i) = i; if mod(i,10) == 0 result(i) end

14 Allocation memory memsize = 100; result = zeros(1,memsize); for i=1:memsize result(i) = i; end

15 Use small data type first memsize = 100; result = zeros(1,memsize,'uint8'); for i=1:memsize result(i) = i; end

16 Access row (the first index) first A = rand(1000); for j=1:size(A,1) for i=1:size(A,2) A(i,j) = 1; end

17 Eliminate nested loop A = rand(1000); for i=1:numel(A) A(i) = 1; % row index = mod(i-1,size(A,1))+1 % column index = ceil(i/size(A,1)); end Vectorization Linear indexing

18 Array processing with GPU X = rand(1000); G = gpuArray(X); % Send data to GPU G2 = G.* G; % Performed on GPU W = gather(G); % Return data to client


Download ppt "MATLAB Tips for Simulation Parinya Sanguansat. Version Selection  X86  Faster  Smaller  X64  Slower  Bigger."

Similar presentations


Ads by Google