How to make variables in order when using parfor?

1 回表示 (過去 30 日間)
Junseob Kim
Junseob Kim 2020 年 2 月 27 日
コメント済み: Mike Croucher 2021 年 12 月 14 日
Hello. I am now using parfor and have some problems.
For example, When I run this simple code:
k=10;
parfor n = 1:k
disp(['Time Step = ',num2str(n)]);
end
the results are:
Time Step = 1
Time Step = 2
Time Step = 3
Time Step = 8
Time Step = 7
Time Step = 4
Time Step = 5
Time Step = 6
Time Step = 10
Time Step = 9
but I want to get the results in a row like:
Time Step = 1
Time Step = 2
Time Step = 3
Time Step = 4
Time Step = 5
Time Step = 6
Time Step = 7
Time Step = 8
Time Step = 9
Time Step = 10
So, how to make variables in order when using parfor?
Thanks.
  5 件のコメント
Walter Roberson
Walter Roberson 2021 年 12 月 14 日
Use a controlling process that reads frames from the video file. As each one is read, parfeval() the processing function, passing in the content of the frame. As of R2021b this can be done with background threads with basic MATLAB
Mike Croucher
Mike Croucher 2021 年 12 月 14 日
@Arthur Vieira Enforcing order almost guarantees serial execution which also outweighs the benefits of parfor. There would need to be a lot of blocking going on. This isn't an issue particular to parfor, its an issue related to parallel looping in general.
In your case, I would investigate alternative and more advanced ways of exposing the parallelism rather than a simple parfor loop. For example, could you decompress the frames in batches? Maybe at the moment you have a loop that looks like this (in pseudo-code)
loop over all frames % each core does iterations of this independently
decompress a frame
process it
end loop
Instead maybe something do something like
chunksize=32 frames
loop over all chunks
decompress a chunk of frames
loop over frames in a chunk
process frame
endloop
endloop
Depending on the timings of the various operations, parallelising over either loop may be an option. You could also vary the chunk size.

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

回答 (0 件)

カテゴリ

Help Center および 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