1 / 40

Lecture 10 Vector codes

Lecture 10 Vector codes. Matlab compiler Vector codes Hyper-plane fitting Distances between points Product of two matrices. Hanoi Tower Play Panel. PlayHanoiTower.m PlayHanoiTower.fig. mcc. Use mcc instruction to compile a matlab function. mcc –m PlayHanoiTower.m. HANOI TOWER.

keira
Download Presentation

Lecture 10 Vector codes

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Lecture 10 Vectorcodes • Matlab compiler • Vector codes • Hyper-plane fitting • Distances between points • Product of two matrices 軟體實作與計算實驗

  2. Hanoi Tower Play Panel PlayHanoiTower.m PlayHanoiTower.fig 軟體實作與計算實驗

  3. mcc Use mcc instruction to compile a matlab function mcc –m PlayHanoiTower.m 軟體實作與計算實驗

  4. HANOI TOWER HANOI.m 軟體實作與計算實驗

  5. Flow chart: move n disks from a to c function HANOI(n,a,b,c) n==1 %Move n-1disks from a to b HANOI(n-1,a,c,b); fprintf('%d -> %d\n',a,c); return %Move 1 disk from a to c HANOI(1,a,b,c); %Move n-1 disks from b to c HANOI(n-1,b,a,c); return 軟體實作與計算實驗

  6. Hyper-plane fitting 軟體實作與計算實驗

  7. d=5; N=1000; x=rand(N,d)*2-1; a= rand(d,1);b=rand(1,1); y=x*a+rand(N,1)*0.2-0.1; Sampling 軟體實作與計算實驗

  8. Mesh plot_2D.m 軟體實作與計算實驗

  9. Minimization 軟體實作與計算實驗

  10. Derivation of normal equations 軟體實作與計算實驗

  11. Derivation 軟體實作與計算實驗

  12. Derivation 軟體實作與計算實驗

  13. Derivation 軟體實作與計算實驗

  14. Derivation 軟體實作與計算實驗

  15. Normal equations 軟體實作與計算實驗

  16. d=3 軟體實作與計算實驗

  17. Function Ce(x,y) • Head: function C=Ce(x,y) • Body: • [N,d]=size(x); • Set C to zeros(d+1,d+1) • for m=1:d for n=1:d for i=1:N C(m,n) = C(m,n) + x(i,m)*x(i,n) • for m=1:d for i=1:N Add C(m,d+1) = C(m,d+1) + x(i,m)*y(i) • For m=1:d for i=1:N C(d+1,n) = C(d+1,n) + x(i,n) • C(d+1,d+1) = N 軟體實作與計算實驗

  18. Scalar code Ce.m 軟體實作與計算實驗

  19. Vector code I A=[x ones(N,1)]; C=A'*A;e=A'*y; u=inv(C)*e; 軟體實作與計算實驗

  20. Vector code I A=[x ones(N,1)]; C=A'*A;e=A'*y; u=C/e; 軟體實作與計算實驗

  21. Demo_hp demo_hp.m 軟體實作與計算實驗

  22. d=100; N=1000 absolute difference = 0.000 cpu time: 0.234 0.047 in sec time ratio- scalar to vector : 5.000 軟體實作與計算實驗

  23. demo_hp(500,2000) absolute difference = 0.000 cpu time: 12.391 0.656 in sec time ratio- scalar to vector : 18.881 軟體實作與計算實驗

  24. Vector code vs. sequential code • For-loop • Inefficient • Time consuming • Speed up • Vector code 軟體實作與計算實驗

  25. Distances between high dimensional data points 軟體實作與計算實驗

  26. MyDistance • Head: D=MyDisrtance(x) • Body: • [N,d]=size(x); D=zeros(N,N); • For i=1:N For j=1:N for k=1:d D(i,j)=D(i,j)+(x(i,k)-x(j,k))^2; D(i,j)=sqrt(D(i,j)); 軟體實作與計算實驗

  27. Scalar code Mydistance.m 軟體實作與計算實驗

  28. A=sum(X.^2,2)*ones(1,N); C=A'; B=X*X'; DD=A-2*B+C; 軟體實作與計算實驗

  29. Execution file demo_distance.m demo_distance.exe 軟體實作與計算實驗

  30. Example >> demo_distance Data size:1000 Dimension:20 cpu time: 14.922 0.125 in sec time ratio- scalar to vector : 119.375 Absolute difference:0.000 Press a key to return 軟體實作與計算實驗

  31. Matrix multiplication A, B denote matrices respectively with size pxq and qxr 軟體實作與計算實驗

  32. Scalar rule 軟體實作與計算實驗

  33. Matrix multiplication Head: C=myMM(A,B) Body [p,q]=size(A);[q,r]=size(B);C=zeros(p,r); for i=1:p for j=1:r for k=1:q C(i,j)=C(i,j)+ A(i,k)*B(k,j) 軟體實作與計算實驗

  34. Example: matrix multiplication p=500;q=1000;r=500; A=rand(p,q); B=rand(q,r); for i=1:p for j=1:r sv=0; for k=1:q sv=sv+A(i,k)*B(k,j); end C(i,j)=sv; end end 軟體實作與計算實驗

  35. Vector code • Matlab built-in instruction C=A*B 軟體實作與計算實驗

  36. Demo_product demo_product.m 軟體實作與計算實驗

  37. cputime • Instruction: cputime demo_cputime.m 軟體實作與計算實驗

  38. P=300 cpu time: 1.766 0.125 in sec time ratio- scalar to vector : 14.125 absolute difference:0.000000 軟體實作與計算實驗

  39. P=500 cpu time: 8.750 0.531 in sec time ratio- scalar to vector : 16.471 absolute difference:0.000000 軟體實作與計算實驗

  40. P=800 cpu time: 39.016 2.203 in sec time ratio- scalar to vector : 17.709 absolute difference:0.000000 軟體實作與計算實驗

More Related