How can I debug parfor loop to find out where an error comes from?

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. In each iteration, a temporary structure variable (DCM) is initialised then sent to a function to return the output to the same variable (DCM). At the end of each parfor iteration, the estimated DCM is saved on the disk as DCM_i and it is worth mentioning that the analysis time for different datasets is different. When I ran the code for 15 datasets, it loaded 8 datasets randomly and I could see (according to the script I have written) when one of the sets was finished in a lab, another one would be loaded. So, after 7 of datasets were analysed and their DCM's were saved on the disk, suddenly I got a similar error to the following:
Error in converting struct to double => It is not possible to change struct to double
Now I have no idea where this error happens. All my datasets are exactly the same and the same code has been applied on all of them using for loop. As my parfor loop is working now and it has given me the results of some of the datasets, I don't get it why I would receive such a basic error. I feel I should have received the same error for all datasets if there was a coding mistake.
Best, Pegah

5 Comments

Hmmm -- could you experiment by putting
clear DCM
inside the parfor, before the assignment to DCM ?
Dear Walter,
I ran the same code again yesterday and it worked for all 15 datasets without any errors. I have no idea why.
But now I have another problem. When the running was finished and I checked the results coming from parfor with the ones coming from for, they are different. My supervisor and I have checked so many things but have not yet found why. 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 variable DCM along with the name of the dataset that each parfor iteration should load which is saved in DCM.xY.Dfile. The two functions spm_dcm_erp_data and spm_dcm_erp_dipfit add some more basic parameters to DCM to prepare if for the last function in the loop which is spm_dcm_erp. When DCM is sent to this last function, the actual analyses start on the dataset whose name is saved in DCM.xY.Dfile until the model converges which may take from 1 to 64 iterations and then some parameters will be added to DCM and at the end of spm_dcm_erp (an 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. Do you think I am making a mistake in defining my variable? 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.
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)];
% Parameters and options used for setting up model.
%-------------------------------------------------------
DCM.options.analysis = 'ERP'; % analyze evoked responses
DCM.options.model = 'ERP'; % ERP model
%----------------------------------------------------------
% data and spatial model
%----------------------------------------------------------
forwardsetspm(DCM.xY.Dfile)
DCM = spm_dcm_erp_data(DCM);
% location priors for dipoles
%----------------------------------------------------------
DCM.Lpos = [[-42; -22; 7] [46; -14; 8] [-61; -32; 8] [59; -25; 8]
%----------------------------------------------------------
% spatial model
%----------------------------------------------------------
DCM = spm_dcm_erp_dipfit(DCM);
%----------------------------------------------------------
% specify connectivity model
%----------------------------------------------------------
cd(Panalysis)
DCM.A{1} = zeros(Nareas,Nareas);
%invert
%----------------------------------------------------------
DCM.name = ['DCM_mansour_trials_' num2str(i) '_10'];
DCM = spm_dcm_erp(DCM);
end
I just ran my parfor code without opening the MATLABPool and now it gives me the same result as my for-loop. Can I conclude that I am not defining anything wrong in parfor? or Now that the pool is closed, MATLAB looks at or even kind of replace parfor with for and that is the reason for the same results?
When the pool is not open, "for" is used instead of "parfor". Except that the iterations might not be done in the same order as if "parfor" had not been specified.
So is it possible that the way I define the parameters in the above code is not suitable for parfor?

Sign in to comment.

Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Products

Asked:

on 28 Aug 2012

Community Treasure Hunt

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

Start Hunting!