Filter löschen
Filter löschen

Optimizing lattice search using parfor

2 Ansichten (letzte 30 Tage)
Gün Süer
Gün Süer am 7 Apr. 2021
Beantwortet: Anil am 28 Sep. 2022
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?

Antworten (1)

Anil
Anil am 28 Sep. 2022
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).';

Kategorien

Mehr zu Parallel for-Loops (parfor) finden Sie in Help Center und File Exchange

Produkte


Version

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by