Missing function outputs in some iterations inside parfor

I have a function which is invoked in a parfor loop. The function in turn calls a couple more functions and outputs structure fields. However, I do not get all my results, although my simulation runs correctly.
parfor i = 1:900
for j = 1:3600
[x(i,j),y(i,j).y,y(i,j).z] = func(a,b,c,d);
end
end
Here, x,y,z,a,b,c,d are a mix of structures and matrices. So lets assume x is an array. I get correct results in x which tells me that my function runs fine. But a few entries in y (which is a structure) are missing. The missing entries are different at different execution of the same program. Eg: all the columns from row 29 to 41 in y have no entries, but x has the corresponding entries.
What could be the cause? This is my first experience with parallel programming. So any help is appreciated!

 Accepted Answer

You should be avoiding writing to the same output variable with multiple varying subscripts: it is not permitted at all in older versions and is not as efficient when it is done.
Vectorizing properly is a bit tricky because of your structures.
parfor i = 1:900
xi = zeros(1,3600); yiy = cell(1,3600); yiz = cell(1,3600);
for j = 1:3600
[xi(1,j), yiy{1,j}, yiz{1,j}] = func(a,b,c,d);
end
x(i,:) = xi; y(i,:) = struct('y', yiy, 'z', yiz);
end
Note that the above code is only valid for the case where y does not have any existing content that needs to be left intact (such as if there were additional fields of y that you were not writing to but need preserved.)

More Answers (0)

Categories

Tags

Community Treasure Hunt

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

Start Hunting!