Problems using parfor due to way variable is used

조회 수: 1 (최근 30일)
Anders Jensen
Anders Jensen 2019년 12월 9일
편집: Adam 2019년 12월 9일
Hello guys!
I have problems using parfor in this loop. It says: "The PARFOR loop cannot run due to the way variable 'minSSD', 'minCRSSSDCentrumCoordinates', 'minCRSSSDPatchMasks' and 'bestCoMasksCRS' are used.
function [minSSD, minCRSSSDCentrumCoordinates,minCRSSSDPatchMasks,bestCoMasksCRS] = OPM(featuresLibrary,patchSize,searchwindowSize,x,y,z,k,j,subjectPatchMask,zeroPatchMask,objectPatchMask,unitTest)
%UNTITLED Summary of this function goes here
% Detailed explanation goes here
% Creates the mask of the subject patch
bestCoMasksCRS = cell(1,k);
minSSD= zeros(1,k);
minCRSSSDCentrumCoordinates = cell(1,k);
minCRSSSDPatchMasks = cell(1,k);
parfor i = 1:k
[centerPatchMask,topPatchMaskNN,topPatchMaskCenter,...
bottomPatchMaskNN,bottomPatchMaskCenter,rightPatchMaskNN,...
rightPatchMaskCenter,leftPatchMaskNN,leftPatchMaskCenter,...
frontPatchMaskNN,frontPatchMaskCenter,backPatchMaskNN,...
backPatchMaskCenter] = MaskInInitializationWindow(featuresLibrary{3,j},patchSize,searchwindowSize,zeroPatchMask,objectPatchMask,x,y,z,unitTest);
[templates, coMasks]=RandomTemplates(featuresLibrary{1,j},featuresLibrary{2,j}, unitTest);
[patchSubject,patchOutput] = PatchesFromMasks(featuresLibrary{3,j},...
templates,coMasks,subjectPatchMask,centerPatchMask,topPatchMaskNN,...
topPatchMaskCenter,bottomPatchMaskNN,bottomPatchMaskCenter,...
rightPatchMaskNN,rightPatchMaskCenter,leftPatchMaskNN,...
leftPatchMaskCenter,frontPatchMaskNN,frontPatchMaskCenter,...
backPatchMaskNN,backPatchMaskCenter, unitTest) ;
[bestPatchMatchTemp,bestCoMasks] = SSDOnPatches(patchSubject,patchOutput, k, unitTest);
[crsPatchMasks,crsPatches,crsPatchMaskCoordinates] = ConstrainRandomSearch(patchSize,searchwindowSize,bestPatchMatchTemp,x,y,z,k,unitTest);
[minSSD(1,i), minCRSSSDCentrumCoordinates{1,i},minCRSSSDPatchMasks{1,i},bestCoMasksCRS{1,i}] = SSDOnCRSPatches(...
bestCoMasksCRS,bestCoMasks,minSSD,minCRSSSDCentrumCoordinates,...
minCRSSSDPatchMasks,crsPatchMasks,patchSubject,crsPatches,crsPatchMaskCoordinates,k,unitTest);
end
end
Do you have a solution for this problem? Cheers!
Anders
  댓글 수: 1
Adam
Adam 2019년 12월 9일
편집: Adam 2019년 12월 9일
I've no idea what the code is doing - there's an alarming number of arguments being passed around in there, but this function call in particular raises eyebrows for a parfor loop (or even a regular for loop):
[minSSD(1,i), minCRSSSDCentrumCoordinates{1,i},minCRSSSDPatchMasks{1,i},bestCoMasksCRS{1,i}] = SSDOnCRSPatches(...
bestCoMasksCRS,bestCoMasks,minSSD,minCRSSSDCentrumCoordinates,...
minCRSSSDPatchMasks,crsPatchMasks,patchSubject,crsPatches,crsPatchMaskCoordinates,k,unitTest);
You are passing your entire arrays for minSSD etc into this function to calculate something, and are then writing an answer to just a single value in those same arrays.
This is confusing in numerous ways, but purely from the perspective of a parallel loop you cannot have calculation based on an array that each thread is writing to. What kind of answer are you expecting given that parallel processing does not maintain order of i from 1 to k like a for loop would (even in a for loop this code looks suspicious)? So each call to that function passing in minSSD is going to have different parts of it written at different times, and in an unpredictable manner, if it were allowed at all, which it isn't.
Hard to know what to advise as I really have no idea what the code is trying to do, but functions taking that many input and output arguments already raise plenty of questions.

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

답변 (1개)

ME
ME 2019년 12월 9일

카테고리

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