Problems using parfor due to way variable is used

3 views (last 30 days)
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
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},...
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(...
Do you have a solution for this problem? Cheers!
  1 Comment
Adam on 9 Dec 2019
Edited: Adam on 9 Dec 2019
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(...
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.

Sign in to comment.

Answers (1)

ME on 9 Dec 2019

Community Treasure Hunt

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

Start Hunting!

Translated by