AMD Ryzen 7 3700X
MATLAB R2019a(9.6.0.1072779)
测试项目
矩阵计算
首先,来编写一段性能测试程序,主要的测试项目有矩阵乘法、稀疏矩阵、逆矩阵、FFT、LU、QR、奇异值分解、特征值与特征向量,每项测试进行三遍取均值。
%%%%%%%%%%%% 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 乘法clcdisp('^-------')disp(['第' sprintf('%4i',i) ' 轮乘法测试中...'])tic, X1 = A*B; T(1,i) = toc;% Test2 稀疏矩阵clcdisp('-^------')disp(['第' sprintf('%4i',i) ' 轮稀疏矩阵测试中...'])tic, X2 = sparse(A); T(2,i) = toc;% Test3 逆矩阵clcdisp('--^-----')disp(['第' sprintf('%4i',i) ' 轮逆矩阵测试中...'])tic, X3 = inv(A); T(3,i) = toc;% Test4 快速傅里叶clcdisp('---^----')disp(['第' sprintf('%4i',i) ' 轮快速傅里叶测试中...'])tic, X4 = fft(A); T(4,i) = toc;% Test5 LU分解clcdisp('----^---')disp(['第' sprintf('%4i',i) ' 轮LU分解测试中...'])tic, [L5,U5,P5] = lu(A); T(5,i) = toc;% Test6 QR分解clcdisp('-----^--')disp(['第' sprintf('%4i',i) ' 轮QR分解测试中...'])tic, X6 = qr(A); T(6,i) = toc;% Test7 奇异值分解clcdisp('------^-')disp(['第' sprintf('%4i',i) ' 轮奇异值分解测试中...'])tic, [U7,S7,V7] = svd(A); T(7,i) = toc;% Test8 特征值与特征向量clcdisp('-------^')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 R2017aMultiplication : 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
可视化
%%%%%%%%%%%%%%%%%%%%%%% MATLAB纯CPU性能测试 可视化计算 %%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%% Haotian_W, SEPT 2020 %%%%%%%%%%%%%%%%%%%%%%
clear
clc
t = zeros(1,5);
num = 5;
for ii = 1:numclearvars -except t num% 3-D Graphicclcdisp('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 offf.Color = 'none';t(1) = toc + t(1);% Flow Visualizationclearvars -except t numclcdisp('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 equalview(37,32)box oncolormap(hsv)lightfigure('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 equalview(37,32)box onfigure('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 equalview(37,32)box onlightfigure('position',[100,100,500,500],'color','w')claspd = 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 equalview(37,32)box onlightt(2) = toc + t(2);% 4-D Dataclearvars -except t numclcload 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 onstem3(Age(smIdx), Weight(smIdx), Systolic(smIdx), 'Color', 'r') hold offview(-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 Functionclearvars -except t numclcfigure('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);% Plotmatrixclearvars -except t numclcload patients Height Weight Diastolic Systolicfigure('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})endt(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 R2017a3-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
有限元计算
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:nelxfor j1 = 1:nelye1 = (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));endendend
end
H = sparse(iH,jH,sH);
Hs = sum(H,2);
x = repmat(volfrac,nely,nelx);
xPhys = x;
loop = 0;
change = 1;
while change > 0.01clcloop = 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 == 1dc(:) = H*(x(:).*dc(:))./Hs./max(1e-3,x(:));elseif ft == 2dc(:) = H*(dc(:)./Hs);dv(:) = H*(dv(:)./Hs);endl1 = 0; l2 = 1e9; move = 0.2;while (l2-l1)/(l1+l2) > 1e-3lmid = 0.5*(l2+l1);xnew = max(0,max(x-move,min(1,min(x+move,x.*sqrt(-dc./dv/lmid)))));if ft == 1xPhys = xnew;elseif ft == 2xPhys(:) = (H*xnew(:))./Hs;endif sum(xPhys(:)) > volfrac*nelx*nely, l1 = lmid; else l2 = lmid; endendchange = 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
benchans =0.0463 0.0808 0.0135 0.0673 0.3977 0.4141
总结
测试了这么多,说实话,AMD的表现没有给我任何惊喜,甚至有点失望。Intel的U手里只有一块升级前的i5 7400,我原本是不打算做横向对比的因为差距太大了,可是测完这些项目之后我觉得还是把对比结果放出来比较好。
“MKL的负优化”以及“MATLAB多核的薄弱”我心里有数,我也不指望3700X能打平9700K甚至更高档次的Intel U,但我真的从没想过3700X会在部分项目输给7400。听说2020版本优化比较好,改天再测试下吧。其余的不多说了,写这篇文章的目的不是想吹捧谁踩谁,我没有任何品牌信仰,我是“方便党”,哪个产品合适我就用哪个,结果放在这儿给跟我有差不多需求的人一个参考。个人观点:科学计算领域,AMD到ZEN 2为止不可能撼动Intel的地位,或许是因为软件优化的问题,但既然买来用就必须把它们当成一个整体来看,毕竟你不可能自己写软件。不管各种媒体吹上天,事实就是事实,期待马上要发布的ZEN 3和11代酷睿的表现。
方法一 批处理启动
在cmd中输入以下命令运行matlab。
@echo off set MKL_DEBUG_CPU_TYPE=5 call "%MKLROOT%\bin\mklvars.bat" MKL_DEBUG_CPU_TYPE=5 matlab.exe
方法二 修改环境变量
在控制面板里找到“编辑系统环境变量”,选择“高级-环境变量”,找到或者新建 MKL_DEBUG_CPU_TYPE,值为 5。和上面的不同是,修改完环境变量后会影响所有使用MKL的程序。