Hello everyone,
I am having troubles of adjusting FOR loop codes below to PARFOR loop. Hopefully, you can help me out. Thank you so much in advance.
for i = 1:size(t2,1)-1
x = find(t1.v1>t2.v1(i) & t1.v1<=t2.v1(a+1));
t1.v2(x(t1.v2(x)~=t2.v2(a))) = 0;
end
PS: I am not sure if I give out enough information, please let me know if you need me to clear out anything.

댓글 수: 2

Joseph Cheng
Joseph Cheng 2014년 7월 8일
I do not think this is a case where you can use PARFOR. in the t1.v2() line you maybe indexing the same values in x in parallel loops. what is the error it gives you when you try this?
Hai Nguyen
Hai Nguyen 2014년 7월 8일
Thank you for responding. Actually I got 3 warning when I used PARFOR instead of FOR.
  • The PARFOR loop cannot run due to the way variable 't1' used
  • Valid indices for 't1' are restricted in PARFOR loop
  • The entire array or structure 't2' is broadcast variable. This might result in unnecessary communication overhead.

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

 채택된 답변

Edric Ellis
Edric Ellis 2014년 7월 9일
편집: Edric Ellis 2014년 7월 9일

0 개 추천

There are several problems here, but I think the biggest problems you need to overcome is that the result of a PARFOR loop needs to be expressed as a sliced or a reduction variable, and that the iterations of the loop need to be provably order-independent. Simply put, a sliced output is one where MATLAB can see from the text of your program that each loop iteration assigns into a slice of an array, and the slice depends on the loop variable. So, the following is a sliced output:
x = zeros(10);
parfor idx = 1:10
x(:, idx) = rand(10, 1);
end
In this case, each iteration of the loop assigns into a column slice of x. In your case, the indexing expression into t1.v2 is problematic firstly because it is not a simple 'sliced' form, and secondly because the indexing expression depends on previous values in t1.v2.
You might be able to run the first expression in the loop as a PARFOR, and then perform the assignment in an ordinary FOR loop later - but this will probably not get you any improvement in performance.
parfor i = 1:size(t2,1)-1
xc{i} = find(t1.v1>t2.v1(i) & t1.v1<=t2.v1(a+1));
end
for i = 1:size(t2,1)-1
x = xc{i};
t1.v2(x(t1.v2(x)~=t2.v2(a))) = 0;
end

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Parallel for-Loops (parfor)에 대해 자세히 알아보기

태그

질문:

2014년 7월 8일

편집:

2014년 7월 9일

Community Treasure Hunt

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

Start Hunting!

Translated by