필터 지우기
필터 지우기

How to automatically choose to compute for or parfor loops

조회 수: 1 (최근 30일)
Felix Lauwaert
Felix Lauwaert 2015년 8월 21일
댓글: Felix Lauwaert 2015년 8월 21일
Hi,
I wonder if there's a way to specify if I want to compute with a normal for or with a parfor.
What I don't want is to make a switch with two cases (case 1 --> parfor; case2 --> for;) because what's inside the loop is quite long so I don't want to copy/paste so many code lines and it's not 100% necessary to make this choice automatic. But sometimes I have to calculate few inputs and sometimes a lot of them.
EDIT: I guess I can make two functions: 1 for looped and another parfor looped, but is there any other option?

채택된 답변

Sean de Wolski
Sean de Wolski 2015년 8월 21일
You can use this syntax for parfor
parfor (loopvar = initval:endval, M); statements; end
Where M is dynamically determined. If it's zero, then this loop will run serially.
  댓글 수: 4
Sean de Wolski
Sean de Wolski 2015년 8월 21일
편집: Sean de Wolski 2015년 8월 21일
Also, you can turn off the auto start of the pool in preferences so it never starts without you explicitly telling it to.
And, if startup is the cost you don't want to wait for, consider boosting the idle timeout to longer and just keep the pool open.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by