필터 지우기
필터 지우기

Optimizing lattice search using parfor

조회 수: 2 (최근 30일)
Gün Süer
Gün Süer 2021년 4월 7일
답변: Anil 2022년 9월 28일
clear all;
K=7; % (K+1)x(K+1) matrix
g = 1; % g(x^2 + y^2)^2 coupling
N = 50; % Lattice site number
xrange = [0,2]; % E range
yrange = [0,1]; % <r^2> range
zrange = [0,1]; % l range
xsol = [];
ysol = [];
zsol = [];
parfor i=1:N
for j=1:N
for k=1:N
a = xrange(1)+(xrange(2)-xrange(1))*i/N;
b = yrange(1)+(yrange(2)-yrange(1))*j/N;
c = zrange(1)+(zrange(2)-zrange(1))*k/N;
if isPosDef(Matrix(K,a,b,c,g))==1
xsol(end+1) = a;
ysol(end+1) = b;
zsol(end+1) = c;
end
end
end
end
Let me tell you my objective. I take a region in 3D space. I scan over the points in that space and if a point satisfies my condition isPosDef, I wan to append that point into my solution set. To speed up the process I want to run the first for loop using parfor. But I get the following errror:
Error: Unable to classify the variable 'xsol' in
the body of the parfor-loop. For more
information, see Parallel for Loops in MATLAB,
"Solve Variable Classification Issues in
parfor-Loops".
How can I optimize this search procedure in matlab?

답변 (1개)

Anil
Anil 2022년 9월 28일
Hi Gün Süer,
As I understand, you would like to improve the performance of the code by using parfor. As suggested by the error, parfor is unable to work with variables that change size inside the parallel region. Hence, pre-allocating xsol, ysol and zsol should sort the issue. In general, pre-allocating is a great idea to achieve performance and especially useful in parallelization. I was unable to test the original code as the definitions for isPosDef and Matrix were missing. However, the following example should work.
K=7; % (K+1)x(K+1) matrix
g = 1; % g(x^2 + y^2)^2 coupling
N = 50; % Lattice site number
xrange = [0,2]; % E range
yrange = [0,1]; % <r^2> range
zrange = [0,1]; % l range
xsol = zeros(N, N, N);
ysol = zeros(N, N, N);
zsol = zeros(N, N, N);
indexPD = zeros(N, N, N, 'logical');
parfor i=1:N
for j=1:N
for k=1:N
a = xrange(1)+(xrange(2)-xrange(1))*i/N;
b = yrange(1)+(yrange(2)-yrange(1))*j/N;
c = zrange(1)+(zrange(2)-zrange(1))*k/N;
if isPosDef(Matrix(K,a,b,c,g))==1
xsol(k, j, i) = a;
ysol(k, j, i) = b;
zsol(k, j, i) = c;
indexPD(k, j, i) = 1;
end
end
end
end
xsol = xsol(indexPD).';
ysol = ysol(indexPD).';
zsol = zsol(indexPD).';

카테고리

Help CenterFile Exchange에서 Parallel for-Loops (parfor)에 대해 자세히 알아보기

제품


릴리스

R2021a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by