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!

 採用された回答

Walter Roberson
Walter Roberson 2016 年 7 月 7 日

0 投票

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.)

1 件のコメント

Amulya NV
Amulya NV 2016 年 7 月 7 日
Thats great!! Thanks a lot.. It is working :)

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeParallel for-Loops (parfor) についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by