parfor and for loops - different results
Afficher commentaires plus anciens
Hello all, I have a strange issue that I can't quite figure out. I tried to speed up my code by introducing parrallel processing, I was successful in making my code run faster but the final output of my code changed.
The purpose of my code is to evaluate the auROC curve based on two inputs. The part of my code where I introduced parallel processing was a step where I shuffled my data randomly 1000 times, and calculated 1000 auROC curves. I tried to run this through a parfor loop:
p = randperm(size(binned_raw,1),1000);
parfor ii = 1:1000
binned_raw_shift = circshift(binned_raw,p(ii),1);
[AUROC, TPR, FPR] = get_ROC(binned_raw_shift, binned_behavior);
shuffled_raw(ii,:) = AUROC;
end
As you can see in my code I first generate 1000 random numbers that are less than or equal to the length of my input, and with each loop in the parfor loop I perform a circular shift on my input (binned_raw), and run it through a function I wrote. I collect all 1000 outputs in my matrix shuffled_raw, and I use that as a threshold to determine if any of my original auROC curves were significantly greater than my randomly shuffled data.
The issue I am having is that introducing the parfor loop at this step changes the results compared to when I only use a for loop. I do not exactly see why it should change anything because any indexing and saving of outputs should be controlled by the value of ii. I should note, the changes I observed were that when I used the parfor loop, the results we much more liberal than when I used a normal for loop, stating that a much higher percent of my original data was significantly greater than the shuffled data. Please if anyone can help me figure out why this problem is occurring I would be very greatful. Thanks a bunch.
8 commentaires
Matt J
le 26 Août 2020
Did you seed the random number generator the same for both thte parfor-loop test and the for-loop test?
Connor Johnson
le 26 Août 2020
Rik
le 26 Août 2020
Did you use the rng function before calling randperm? Do any of the functions called call a random function?
Connor Johnson
le 26 Août 2020
Rik
le 26 Août 2020
If you don't use rng to fix the random seed, the change of randperm returning the same shuffle are very small for large sizes of binned_raw, so the input isn't the same if you switch the code from for to parfor.
Connor Johnson
le 26 Août 2020
Dana
le 26 Août 2020
The point is, if you run this script twice even just using the for loop both times (no parfor), you'll likely get different answers. So it has nothing to do with the parfor, it's that the initial random draw will be different every time you run the script (unless you use rng to seed the random number generator).
Connor Johnson
le 26 Août 2020
Réponses (1)
Matt J
le 26 Août 2020
Because randperm returns a random result, the sequence p(ii) will be different in two consecutive runs,e.g.,
>> p = randperm(8,4)
p =
6 4 7 3
>> p = randperm(8,4)
p =
8 7 5 4
That would explain why the parfor and the for-loop versions don't give the same output.
2 commentaires
Matt J
le 26 Août 2020
The code for the comparison should look like this:
p = randperm(size(binned_raw,1),1000); %generate only once
for ii = 1:1000
binned_raw_shift = circshift(binned_raw,p(ii),1);
[AUROC, TPR, FPR] = get_ROC(binned_raw_shift, binned_behavior);
shuffled_rawFOR(ii,:) = AUROC;
end
parfor ii = 1:1000
binned_raw_shift = circshift(binned_raw,p(ii),1);
[AUROC, TPR, FPR] = get_ROC(binned_raw_shift, binned_behavior);
shuffled_rawPARFOR(ii,:) = AUROC;
end
Difference = max(abs(shuffled_rawFOR - shuffled_rawPARFOR),'all')
Connor Johnson
le 26 Août 2020
Catégories
En savoir plus sur Matrix Indexing dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!