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
답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!