AMD CPU performance test on MATLAB

AMD Ryzen 7 3700X

MATLAB R2019a(9.6.0.1072779)

 

Test items

Matrix calculation

First, let's write a performance test program. The main test items include matrix multiplication, sparse matrix, inverse matrix, FFT, LU, QR, singular value decomposition, eigenvalues ​​and eigenvectors. Each test is performed three times to get the average.

%%%%%%%%%%%%   MATLAB纯CPU性能测试   %%%%%%%%%%%
%%%%%%%%%%%%   Haotian_W  SEP 2020  %%%%%%%%%%%
clear, clc
A = rand(3e3);
B = rand(3e3);
num = 3;
T = zeros(8,num);
for i = 1:num
    % Test1 乘法
    clc
    disp('^-------')
    disp(['第' sprintf('%4i',i) ' 轮乘法测试中...'])
    tic, X1 = A*B; T(1,i) = toc;
    % Test2 稀疏矩阵
    clc
    disp('-^------')
    disp(['第' sprintf('%4i',i) ' 轮稀疏矩阵测试中...'])
    tic, X2 = sparse(A); T(2,i) = toc;
    % Test3 逆矩阵
    clc
    disp('--^-----')
    disp(['第' sprintf('%4i',i) ' 轮逆矩阵测试中...'])
    tic, X3 = inv(A); T(3,i) = toc;
    % Test4 快速傅里叶
    clc
    disp('---^----')
    disp(['第' sprintf('%4i',i) ' 轮快速傅里叶测试中...'])
    tic, X4 = fft(A); T(4,i) = toc;
    % Test5 LU分解
    clc
    disp('----^---')
    disp(['第' sprintf('%4i',i) ' 轮LU分解测试中...'])
    tic, [L5,U5,P5] = lu(A); T(5,i) = toc;
    % Test6 QR分解
    clc
    disp('-----^--')
    disp(['第' sprintf('%4i',i) ' 轮QR分解测试中...'])
    tic, X6 = qr(A); T(6,i) = toc;
    % Test7 奇异值分解
    clc
    disp('------^-')
    disp(['第' sprintf('%4i',i) ' 轮奇异值分解测试中...'])
    tic, [U7,S7,V7] = svd(A); T(7,i) = toc;
    % Test8 特征值与特征向量
    clc
    disp('-------^')
    disp(['第' sprintf('%4i',i) ' 轮特征值与特征向量测试中...'])
    tic, [V8,D8] = eig(A); T(8,i) = toc;
end
clc
% 各项测试平均时间
t = sum(T,2)./num;
disp(['Multiplication :   ' sprintf('%6f',t(1))])
disp(['Sparse         :   ' sprintf('%6f',t(2))])
disp(['Inverse        :   ' sprintf('%6f',t(3))])
disp(['FFT            :   ' sprintf('%6f',t(4))])
disp(['LU             :   ' sprintf('%6f',t(5))])
disp(['QR             :   ' sprintf('%6f',t(6))])
disp(['SVD            :   ' sprintf('%6f',t(7))])
disp(['Eigen          :   ' sprintf('%6f',t(8))])
% Total
total = sum(t);
disp('----------------------------')
disp(['Total          :   ' sprintf('%6f',total)])

% https://blog.csdn.net/BAR_WORKSHOP/article/details/108224394
% i5-7400 R2017a

Multiplication :   0.412246
Sparse         :   0.083916
Inverse        :   0.779796
FFT            :   0.080855
LU             :   0.379976
QR             :   0.416257
SVD            :   13.316459
Eigen          :   20.372446
----------------------------
Total          :   35.841952
% AMD 3700X  R2019a 优化前

Multiplication :   0.758483
Sparse         :   0.047772
Inverse        :   1.036525
FFT            :   0.030290
LU             :   0.336546
QR             :   0.501219
SVD            :   6.163582
Eigen          :   12.077816
----------------------------
Total          :   20.952232

% 在没有优化的R2019a中,3700X部分项目甚至还没有i5-7400快
% AMD 3700X  R2019a  优化后

Multiplication :   0.185732
Sparse         :   0.047977
Inverse        :   0.257886
FFT            :   0.022052
LU             :   0.117283
QR             :   0.172766
SVD            :   3.998370
Eigen          :   7.863378
----------------------------
Total          :   12.665444

Visualization

%%%%%%%%%%%%%%%%%%%%%%%   MATLAB纯CPU性能测试   可视化计算   %%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%   Haotian_W,           SEPT 2020   %%%%%%%%%%%%%%%%%%%%%%
clear
clc
t = zeros(1,5);
num = 5;
for ii = 1:num
    clearvars -except t num
    % 3-D Graphic
    clc
    disp('3-D Graphic Computing')
    f = figure('position',[100,100,500,500],'color','w');
    tic,
    ax = axes;
    ax.XLim = [1 201];
    ax.YLim = [1 201];
    ax.ZLim = [-53.4 160];
    view(3);
    s = surface(160*membrane(1,100));
    s.EdgeColor = 'none';
    camproj('perspective');
    l1 = light;
    l1.Position = [160 400 80];
    l1.Style = 'local';
    l1.Color = [0 0.8 0.8];
    l2 = light;
    l2.Position = [.5 -1 .4];
    l2.Color = [0.8 0.8 0];
    s.FaceColor = [0.9 0.2 0.2];
    s.FaceLighting = 'gouraud';
    s.AmbientStrength = 0.3;
    s.DiffuseStrength = 0.6; 
    s.BackFaceLighting = 'lit';
    s.SpecularStrength = 1;
    s.SpecularColorReflectance = 1;
    s.SpecularExponent = 7;
    axis off
    f.Color = 'none';
    t(1) = toc + t(1);
    % Flow Visualization
    clearvars -except t num
    clc
    disp('Flow Visualization Computing')
    tic,
    figure('position',[100,100,500,500],'color','w')
    cla                                            
    load wind u v w x y z                           
    [m,n,p] = size(u);
    [Cx, Cy, Cz] = meshgrid(1:4:m,1:4:n,1:4:p);    
    h = coneplot(u,v,w,Cx,Cy,Cz,y,4);              
    set(h,'EdgeColor', 'none')
    axis tight equal
    view(37,32)
    box on
    colormap(hsv)
    light
    figure('position',[100,100,500,500],'color','w')
    cla
    [m,n,p] = size(u); 
    [Sx, Sy, Sz] = meshgrid(1,1:5:n,1:5:p);    
    streamline(u,v,w,Sx,Sy,Sz)                 
    axis tight equal
    view(37,32)
    box on
    figure('position',[100,100,500,500],'color','w')
    cla
    [m,n,p] = size(u);
    [Sx, Sy, Sz] = meshgrid(1,1:5:n,1:5:p);    
    h = streamtube(u,v,w,Sx,Sy,Sz);            
    set(h, 'FaceColor', 'cyan')                
    set(h, 'EdgeColor', 'none')
    axis tight equal
    view(37,32)
    box on
    light
    figure('position',[100,100,500,500],'color','w')
    cla
    spd = sqrt(u.*u + v.*v + w.*w);                       
    [fo,vo] = isosurface(x,y,z,spd,40);                      
    [fe,ve,ce] = isocaps(x,y,z,spd,40);   
    p1 = patch('Faces', fo, 'Vertices', vo);                
    p1.FaceColor = 'red';
    p1.EdgeColor = 'none';
    p2 = patch('Faces', fe, 'Vertices', ve, ...              
       'FaceVertexCData', ce);
    p2.FaceColor = 'interp';
    p2.EdgeColor = 'none' ;
    [fc, vc] = isosurface(x, y, z, spd, 30);                
    [fc, vc] = reducepatch(fc, vc, 0.2);                     
    h1 = coneplot(x,y,z,u,v,w,vc(:,1),vc(:,2),vc(:,3),3);    
    h1.FaceColor = 'cyan';
    h1.EdgeColor = 'none';
    [sx, sy, sz] = meshgrid(80, 20:10:50, 0:5:15);         
    h2 = streamline(x,y,z,u,v,w,sx,sy,sz);                   
    set(h2, 'Color', [.4 1 .4])
    axis tight equal
    view(37,32)
    box on
    light
    t(2) = toc + t(2);
    % 4-D Data
    clearvars -except t num
    clc
    load patients Smoker Age Weight Systolic                           
    nsIdx = Smoker == 0;
    smIdx = Smoker == 1;
    figure('position',[100,100,500,500],'color','w')
    tic,
    stem3(Age(nsIdx), Weight(nsIdx), Systolic(nsIdx), 'Color', 'b')    
    hold on
    stem3(Age(smIdx), Weight(smIdx), Systolic(smIdx), 'Color', 'r')   
    hold off
    view(-60,15)
    zlim([100 140])
    xlabel('Age')                                                   
    ylabel('Weight') 
    zlabel('Systolic Blood Pressure') 
    legend('Non-Smoker', 'Smoker', 'Location', 'NorthWest')
    t(3) = toc + t(3);
    % Complex Function
    clearvars -except t num
    clc
    figure('position',[100,100,500,500],'color','w')
    tic,
    r = (0:0.025:1)';                        
    theta = pi*(-1:0.05:1);
    z = r*exp(1i*theta);
    w = z.^3;                                
    surf(real(z),imag(z),real(w),imag(w))    
    xlabel('Real(z)')
    ylabel('Imag(z)')
    zlabel('Real(w)')
    cb = colorbar;
    cb.Label.String = 'Imag(w)';
    t(4) = toc + t(4);
    % Plotmatrix
    clearvars -except t num
    clc
    load patients Height Weight Diastolic Systolic
    figure('position',[100,100,500,500],'color','w')
    tic,
    labels = {'Height' 'Weight' 'Diastolic' 'Systolic'};
    data = [Height Weight Systolic Diastolic];
    [h,ax] = plotmatrix(data);                        
    for i = 1:4                                       
      xlabel(ax(4,i), labels{i})
      ylabel(ax(i,1), labels{i})
    end
    t(5) = toc + t(5);
    close all;
end
t = t./num;
disp(['3-D Graphic        :    ' sprintf('%6f',t(1))])
disp(['Flow Visualization :    ' sprintf('%6f',t(2))])
disp(['4-D Data           :    ' sprintf('%6f',t(3))])
disp(['Complex Function   :    ' sprintf('%6f',t(4))])
disp(['Plotmatrix         :    ' sprintf('%6f',t(5))])
% Total
total = sum(t);
disp('----------------------------')
disp(['Total              :    ' sprintf('%6f',total)])

% https://blog.csdn.net/BAR_WORKSHOP/article/details/108224394
% i5 7400 R2017a

3-D Graphic        :    0.023028
Flow Visualization :    0.434371
4-D Data           :    0.088623
Complex Function   :    0.081808
Plotmatrix         :    0.234556
----------------------------
Total              :    0.862387
% AMD 3700X  R2019a 优化前

3-D Graphic        :    0.013684
Flow Visualization :    0.347523
4-D Data           :    0.050936
Complex Function   :    0.066544
Plotmatrix         :    1.192004
----------------------------
Total              :    1.670691
% AMD 3700X  R2019a  优化后

3-D Graphic        :    0.013359
Flow Visualization :    0.344377
4-D Data           :    0.049520
Complex Function   :    0.063826
Plotmatrix         :    1.161523
----------------------------
Total              :    1.632606

Finite element calculation

clear
clc
tic,
nelx = 90;
nely = 30;
volfrac = 0.5;
penal = 3;
rmin = 3.5;
ft = 1;
E0 = 1;
Emin = 1e-9;
nu = 0.3;
A11 = [12  3 -6 -3;  3 12  3  0; -6  3 12 -3; -3  0 -3 12];
A12 = [-6 -3  0  3; -3 -6 -3 -6;  0 -3 -6  3;  3 -6  3 -6];
B11 = [-4  3 -2  9;  3 -4 -9  4; -2 -9 -4 -3;  9  4 -3 -4];
B12 = [ 2 -3  4 -9; -3  2  9 -2;  4  9  2  3; -9 -2  3  2];
KE = 1/(1-nu^2)/24*([A11 A12;A12' A11]+nu*[B11 B12;B12' B11]);
nodenrs = reshape(1:(1+nelx)*(1+nely),1+nely,1+nelx);
edofVec = reshape(2*nodenrs(1:end-1,1:end-1)+1,nelx*nely,1);
edofMat = repmat(edofVec,1,8)+repmat([0 1 2*nely+[2 3 0 1] -2 -1],nelx*nely,1);
iK = reshape(kron(edofMat,ones(8,1))',64*nelx*nely,1);
jK = reshape(kron(edofMat,ones(1,8))',64*nelx*nely,1);
F = sparse(2,1,-1,2*(nely+1)*(nelx+1),1);
U = zeros(2*(nely+1)*(nelx+1),1);
fixeddofs = union([1:2:2*(nely+1)],[2*(nelx+1)*(nely+1)]);
alldofs = [1:2*(nely+1)*(nelx+1)];
freedofs = setdiff(alldofs,fixeddofs);
iH = ones(nelx*nely*(2*(ceil(rmin)-1)+1)^2,1);
jH = ones(size(iH));
sH = zeros(size(iH));
k = 0;
for i1 = 1:nelx
  for j1 = 1:nely
    e1 = (i1-1)*nely+j1;
    for i2 = max(i1-(ceil(rmin)-1),1):min(i1+(ceil(rmin)-1),nelx)
      for j2 = max(j1-(ceil(rmin)-1),1):min(j1+(ceil(rmin)-1),nely)
        e2 = (i2-1)*nely+j2;
        k = k+1;
        iH(k) = e1;
        jH(k) = e2;
        sH(k) = max(0,rmin-sqrt((i1-i2)^2+(j1-j2)^2));
      end
    end
  end
end
H = sparse(iH,jH,sH);
Hs = sum(H,2);
x = repmat(volfrac,nely,nelx);   
xPhys = x;                       
loop = 0;                        
change = 1; 
while change > 0.01
  clc
  loop = loop + 1;
  disp(['Iteration Times :  ' sprintf('%4i',loop)])
  sK = reshape(KE(:)*(Emin+xPhys(:)'.^penal*(E0-Emin)),64*nelx*nely,1);
  K = sparse(iK,jK,sK); K = (K+K')/2;
  U(freedofs) = K(freedofs,freedofs)\F(freedofs);
  ce = reshape(sum((U(edofMat)*KE).*U(edofMat),2),nely,nelx);
  c = sum(sum((Emin+xPhys.^penal*(E0-Emin)).*ce));
  dc = -penal*(E0-Emin)*xPhys.^(penal-1).*ce;
  dv = ones(nely,nelx);
  if ft == 1
    dc(:) = H*(x(:).*dc(:))./Hs./max(1e-3,x(:));
  elseif ft == 2
    dc(:) = H*(dc(:)./Hs);
    dv(:) = H*(dv(:)./Hs);
  end
  l1 = 0; l2 = 1e9; move = 0.2;
  while (l2-l1)/(l1+l2) > 1e-3
    lmid = 0.5*(l2+l1);
    xnew = max(0,max(x-move,min(1,min(x+move,x.*sqrt(-dc./dv/lmid)))));
    if ft == 1
      xPhys = xnew;
    elseif ft == 2
      xPhys(:) = (H*xnew(:))./Hs;
    end
    if sum(xPhys(:)) > volfrac*nelx*nely, l1 = lmid; else l2 = lmid; end
  end
  change = max(abs(xnew(:)-x(:)));
  x = xnew;
  colormap(gray); imagesc(1-xPhys); caxis([0 1]); axis equal; axis off; drawnow;
end
close all;
t = toc;
clc
disp(['90*30-Mesh FEA :   ' sprintf('%6f',t)])


% https://blog.csdn.net/BAR_WORKSHOP/article/details/108224394
90*30-Mesh FEA :   5.449822

 

Simulink

bench

bench

ans =

    0.0463    0.0808    0.0135    0.0673    0.3977    0.4141

to sum up

After so many tests, to be honest, AMD's performance did not give me any surprises, or even a little disappointed. Intel’s U has only one i5 7400 before the upgrade. I originally didn’t plan to do a horizontal comparison because the gap was too big, but after testing these items, I think it’s better to put the comparison results out.

I know "MKL's negative optimization" and "MATLAB's multi-core weakness". I don't expect the 3700X to be able to beat the 9700K or even higher-end Intel U, but I never thought that the 3700X would lose to the 7400 in some projects. . I heard that the 2020 version is better optimized, so let's test it another day. I won’t say much about the rest. The purpose of writing this article is not to tout who is stepping on whom. I don’t have any brand beliefs. I am a "convenience party". I will use whichever product is suitable. The result is similar to me. A reference for those who need it. Personal opinion: In the field of scientific computing, AMD could not shake Intel's position until ZEN 2, perhaps because of software optimization problems, but since they are bought and used, they must be viewed as a whole. After all, you cannot write software yourself. Regardless of the various media blowing up, the fact is the fact, and I look forward to the performance of the ZEN 3 and the 11th generation Core that will be released soon.

Method batch start

Enter the following command in cmd to run matlab.

    @echo off	
    set MKL_DEBUG_CPU_TYPE=5	
    call "%MKLROOT%\bin\mklvars.bat" MKL_DEBUG_CPU_TYPE=5	
    matlab.exe

 

Method two modify environment variables

Find "Edit System Environment Variables" in the control panel, select "Advanced-Environment Variables", find or create a new MKL_DEBUG_CPU_TYPE, the value is 5. The difference from the above is that after modifying the environment variables, it will affect all programs that use MKL.

Guess you like

Origin blog.csdn.net/BAR_WORKSHOP/article/details/108224394