필터 지우기
필터 지우기

parallel computing sig problem

조회 수: 1 (최근 30일)
Yu Li
Yu Li 2018년 8월 25일
편집: Yu Li 2018년 8월 26일
Hi:
I have a for-loop, which is very long (over 500 line for example). now I'm able to optimize it into parallel computing.
the problem I'm having is that, I want to add a input as parallel sig: par_sig, to control if the code will do parallel computing. if par_sig=1, do parallel computing, if par_sig=0, do normal loop compution.
when this go to the code, it becomes:
if par_sig==0
for ...
500 lines operation...
end
elseif par_sig==1
parfor
500 lines operation...
end
end
in this way the same 500 lines code are written twice in the code. if I revise somewhere I'll have to do twice also, which makes the operation very boring. is there anyway, or any trick, to write it only once?
Thanks!
Yu

답변 (1개)

John D'Errico
John D'Errico 2018년 8월 25일
편집: John D'Errico 2018년 8월 25일
So, you simply cannot put those 500 lines of code inside a function? Or if not as a function, as a script m-file?
There is absolutely no need to copy the code and thus replicate it inline.
Essentially, you can just put it into a separate m-file. Call it my_code.m. Save that as a file. You would then call it essentially like this:
if par_sig==0
for ...
my_code
end
elseif par_sig==1
parfor
my_code
end
end
Now you can make any changes just once, editing the my_code function/script.
  댓글 수: 1
Yu Li
Yu Li 2018년 8월 26일
I have thought about this way, however, my code is looks like this:
1000 lines code.
for ...
my_code
end
there is a lot of shared variables between 'my_code' and '1000 lines' code, it is very complex to put the 500 lines into 'my_code' directly.
however, thanks for your suggestion.
Yu

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

카테고리

Help CenterFile Exchange에서 Manual Performance Optimization에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by