"Error using mean" only when using parallel computing
6 次查看(过去 30 天)
显示 更早的评论
Hi all,
I'm new to using MATLAB's Parallel Computing toolbox. It has dramatically improved my analysis efficiency, but I am encountering an error that I can't wrap my head around. After a certain number of iterations, I will often get the following error:
Error using mean
Dimension argument must be a positive integer scalar, a vector of unique positive integers, or 'all'.
Error in PCApaper_02_coherence_allfreqs_allchans_parallel (line 119)
parfor cmb_idx = 1:ncombs % LOOP OVER CHANNELS
In the above example, ncombs is simply a positive integer constant.
The error is never associated with a line that makes sense (no means are calculated in the line). It has even referenced a line that was commented out in my script with the same error! It appears to happen randomly after an unpredictable number of iterations. If I close and reopen matlab and run again at the same looping index, the code runs fine-- the error does not appear (unless after many more iterations it crops up again).
Is there something I am missing here? Is there a way I can avoid this? My interpretation is that it's a mislabeled error that occurs when parallel computing is drawing too many resources.
I'm using R2022b, and version 7.7 of the parallel computing toolbox.
Thank you! I'll include my loop below:
parfor cmb_idx = 1:ncombs % LOOP OVER CHANNELS
comb2 = nchoosek(order,2);
raw_1 = data_ds.trial{1,1}(comb2(cmb_idx,1),:);
raw_2 = data_ds.trial{1,1}(comb2(cmb_idx,2),:);
data_fft1 = fft(raw_1,n_convolution);
data_fft2 = fft(raw_2,n_convolution);
for fi=1:nfreqs % LOOP OVER FREQUENCIES
% create wavelet and take FFT
freqs2use = [1:1:30,10.^(log10(31):0.04:log10(200))];
num_cycles = logspace(log10(4),log10(8),length(freqs2use));
s = num_cycles(fi)/(2*pi*freqs2use(fi));
wavelet_fft = fft( exp(2*1i*pi*freqs2use(fi).*time) .* exp(-time.^2./(2*(s^2))) ,n_convolution);
% phase angles from channel 1 via convolution
convolution_result_fft = ifft(wavelet_fft.*data_fft1,n_convolution);
convolution_result_fft = convolution_result_fft(half_wavelet+1:end-half_wavelet);
sig1_long = convolution_result_fft; % cut off 10 s of padding
% phase angles from channel 2 via convolution
convolution_result_fft = ifft(wavelet_fft.*data_fft2,n_convolution);
convolution_result_fft = convolution_result_fft(half_wavelet+1:end-half_wavelet);
sig2_long = convolution_result_fft;
% NOW, loop over windows
for win_idx = 1:num_windows % LOOP OVER WINDOWS
begsamp = start_idx(win_idx);
endsamp = begsamp + winlength;
sig1 = sig1_long(:,begsamp:endsamp);
sig2 = sig2_long(:,begsamp:endsamp);
% compute power and cross-spectral power
spec1 = mean(sig1.*conj(sig1),2); % mean across trials = you retain a time series
spec2 = mean(sig2.*conj(sig2),2);
specX = abs(mean(sig1.*conj(sig2),2)).^2;
spectcoher = specX./(spec1.*spec2);
ps_raw(cmb_idx,fi,win_idx) = spectcoher;
pull_tag_sample = start_idx(win_idx)+round(winlength/2);
sz_tags(cmb_idx,win_idx) = pull_tag_sample;
% adjust your seizure_tag to reflect new windowing (based on center value
% of each window)
end % END OVER WINDOWS
end % END OVER FREQUENCIES
end % END OVER COMBS
toc
%tocBytes(gcp)
0 个评论
采纳的回答
Raghav
2023-4-5
Hi,
Based on your question I understand that you are facing difficulty in using parfor loop.
It looks like the error is related to the use of parfor and the fact that you are using the loop variable cmb_idx to index an array in the inner loop (ps_raw(cmb_idx,fi,win_idx)). When using parfor, each iteration of the loop is executed on a separate worker process, and these workers cannot communicate directly with each other. Therefore, they need to be careful about accessing shared variables. In particular, they cannot access shared variables in a way that might lead to race conditions or other synchronization issues.
One possible explanation for the error you are seeing is that two workers are trying to access the same element of ps_raw at the same time, causing a conflict. This could happen if two iterations of the loop happen to have the same value of cmb_idx.
Another issue that could be related to the error is the fact that you are pre-allocating the ps_raw and sz_tags arrays outside of the loop. When using parfor, it is important to make sure that each worker has its own copy of these arrays, otherwise they could conflict with each other. One way to ensure this is to create a temporary array inside the loop and then combine the results afterwards.
Lastly, you may want to consider using the Parallel Computing Toolbox's debugging tools to help diagnose the issue. For example, you can use the dbstop command to set a breakpoint at a specific line of code, which can help you see what is happening when the error occurs.
Hope it helps!
Regards,
Raghav Bansal
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 MATLAB Parallel Server 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!