problem with parfor and struct

7 views (last 30 days)
Daniel Ermer
Daniel Ermer on 6 May 2019
Reopened: Walter Roberson on 13 May 2019
Hey guys,
i have some problems with my parfor-loop. To explain my programm, i have written down an less complex version of my programm, wich is shown below.
I have two structures with some fields in it, wich i need for calcutations. So the idear is to calculate, struct1(1) with strucht2(1 to n), strucht1(2) with struct2(1 to n), ..., struct(m) with struct2(1 to n). My problem is, that the programm is slower with parfor then with for. I found out, that the problem is the line with "output(:, k) = output_z; ", but i dont know how to solve it different, because otherwise i dont get the output out of the parfor-loop.
If you have an idear what i could do, even if it something totaly different, im happy for your aswer.
Kind regards,
Daniel
struct1(200)=struct();
for k = 1:200
struct1(k).constant.A = round(5 + rand(1)*10);
struct1(k).constant.B = round(2 + rand(1)*10);
end
struct2(500)=struct();
for a = 1 : 500
struct2(a).d = 1+rand(1);
end
tic
parfor k = 1 : 200
[output_z] = loop(struct1(k), struct2)
output(:, k) = output_z;
end
toc
function [output] = loop(struct1, struct2)
for a = 1 : 500
[output(a)] = calculation(struct1, struct2(a));
end
end
function [struct1] = calculation(struct1, struct2)
struct1.pv = struct1.constant.A + struct1.constant.B * struct2.d;
end
  9 Comments
Walter Roberson
Walter Roberson on 7 May 2019
It is quite common for parallel programs to be slower than serial.
  • overhead of sending data back and forth, especially if it decides that a variable has to be a broadcast variable instead of sliced
  • Parallel programs by default only get one thread per worker. If the work being done happens to be match one of the patterns common in linear algebra type operations (including just adding two matrices), then for large enough matrices, MATLAB normally calls one of the high performance multithreaded libraries. With workers only getting one thread, those end up being done in serial, with the difference being that several of those might be happening at the same time. It is common that you end up getting higher performance leaving the routines non-parallel
  • parallel typically wins for cases where not much memory needs to be shared and where the operations are quite independent and where the operations do not paralellize well. That happens often enough to be useful, but parfor taking 1.5 to 5 times longer is pretty common.
Daniel Ermer
Daniel Ermer on 8 May 2019
Thanks to you guys for all the answers. @Walter Roberson, now im going for an matrix as an output. It speeds even my simple version from 4 s (parfor), 1,5 s (for) to 0,6 s. I will have to tipe some more rows of code, but it works.
clear all
struct1(200)=struct();
for k = 1:200
struct1(k).constant.A = round(5 + rand(1)*10);
struct1(k).constant.B = round(2 + rand(1)*10);
end
struct2(500)=struct();
for a = 1 : 500
struct2(a).d = 1+rand(1);
end
tic
parfor k = 1 : 200
[out(k,:)] = loop(struct1(k), struct2);
end
for a = 1 : 1 : 200
evaluation(1+200*(a-1):200*a, :) = out(:,1+3*(a-1):3+3*(a-1));
end
toc
function [out] = loop(struct1, struct2)
for a = 500 : -1 : 1
[struct1] = calculation(struct1, struct2(a));
b = 3;
out(1,1+b*(a-1)) = struct1.pv;
out(1,2+b*(a-1)) = struct1.constant.A;
out(1,3+b*(a-1)) = struct1.constant.B;
end
end
function [struct1] = calculation(struct1, struct2)
struct1.pv = struct1.constant.A + struct1.constant.B * struct2.d;
end

Sign in to comment.

Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!