Why do I get different results with parfor and for loops?

7 次查看(过去 30 天)
Hi,
I am trying to run a specific code 15 times, each time for a new set of data so my iterations are not sequential. I use SPM toolbox and am trying to speed up the calculations using matlabpool and parfor. The results of parfor loop and for-loop are different even when I just load one dataset instead of 15 datasets i.e. parfor i=1:1. If I close matlabpool then the results are the same. My code looks something like the following. I have not included all the details of defining my structure. This code is exactly the same for for-loop.
I define some parameters in structure variable DCM. The name of the dataset that each parfor iteration should load is saved in DCM.xY.Dfile. Then, DCM is sent to spm_dcm_erp. Inside this function, the dataset whose name is saved in DCM.xY.Dfile is loaded and some parameters are calculated until the model converges which may take from 1 to 64 iterations. Then, some parameters will be added to DCM and at the end of spm_dcm_erp (and inside the function not in parfor loop) the updated DCM will be saved on the disk with the name defined in DCM.name. It is this saved DCM that I check for the two models (with for and parfor) and is different in the 2 conditions. * I even ran parfor for just one of my datasets (instead of i=1:15 I put i=1) and the result is still different from the one from for-loop. * The funny thing is when I run for-loop the model converges at iteration 62 but when I run parfor it converges at iteration 11. * I tried saving the workspace inside my spm_dcm_erp and the parameters of the two conditions (for and parfor) are exactly the same before the for-loop of spm_dcm_erp function starts to calculate the parameters of the model so I made sure that parfor does not change my variable in any way before sending it to the spm_dcm_erp function.
clc
clear all
DCM=[];
parfor i=1
spm('defaults','EEG');
DCM=[];
% paths to data, etc.
%-------------------------------------------------------
Pbase = pwd; % directory with your data,
Pdata = fullfile(Pbase, '.'); % data directory in Pbase
Panalysis = fullfile(Pbase, '.'); % analysis directory in Pbase
% the data (mismatch negativity ERP SPM file from SPM-webpages)
% -------------------------------------------------------------
DCM.xY.Dfile = ['mansour_trials_' num2str(i)];
DCM.name = ['DCM_mansour_trials_' num2str(i) '_10'];
DCM = spm_dcm_erp(DCM);
end
  7 个评论
Pegah Hosseini
Pegah Hosseini 2012-9-5
I was in contact with SPM coders for a while and it seems that there might be a problem in the interaction between the two toolboxes (PCT and SPM). I will let you know if this is the case.
Pegah Hosseini
Pegah Hosseini 2014-5-30
Although I don't think you are interested in this trend anymore, just so it has a final answer, I am updating it. There really was a problem in the interaction between PCT and SPM and it was solved in the new version. At least that is what I was told by the group. By the time the problem was solved, I had already moved on from SPM so I did not go back to check if it worked for me or it still had any problems.

请先登录,再进行评论。

回答(0 个)

类别

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