Parfor transparency violation error while using anonymous functions

조회 수: 7 (최근 30일)
Calvin Eiber
Calvin Eiber 2021년 7월 17일
댓글: Calvin Eiber 2021년 7월 21일
Hello
I'm using a parallel for-loop to accellerate a slow computation, and I'm getting a transparency violation error in some (but not all) versions of Matlab - specifically, my code works in R2019b (windows) and R2018B (unix) but not R2019a (windows).
I've defined my functions and hid all the messy details as follows:
function run_my_loop
cache_path = [tempdir filesep 'my-cache'];
load('my-variables.mat', 'time', 'sensitivity', 'axon_xy', 'options')
% time is a nT x 1 double
% sensitivity is an nE x nF cell array
% axon_xy is a nG x nF cell array
% options is a structure
nG = size(axon_xy,1)
nF = size(axon_xy,2)
parfor ii = 1:(nG*nF)
V{ii} = parfun_unpack(cache_path, @(g,f) ...
models.spike_to_wave(g+f/10,time, ...
sensitivity(:,ff), ...
axon_xy{g,f},options), ...
[nG nF],ii); %#ok<PFBNS>
end
with
function wave = parfun_unpack(cache_path,fun,nGnF,ii)
tools.cache('set',cache_path)
[gg,ff] = ind2sub(nGnF,ii);
wave = fun(gg,ff);
Since I'm not saving, clearing, eval-in-ing, using scripts, or otherwise doing any of the nasties mentioned in https://au.mathworks.com/help/parallel-computing/transparency.html, I'm unclear as to why I'm having issues with this code in this particular version of matlab. Both models.spike_to_wave and tools.cache('set', ...) both would have file IO calls which might cause issues, were those calls to occuer in the body of the parallel for-loop, but since the code runs on some versions of Matlab I'm unclear as to why it doesn't run on 2019a.
Does anybody have any idea what's going on here?
Version info for the offending machine:
% MATLAB Version: 9.6.0.1150989 (R2019a) Update 4
% MATLAB License Number: **********
% Operating System: Microsoft Windows 10 Pro Version 10.0 (Build 19041)
% Java Version: Java 1.8.0_181-b13 with Oracle Corporation Java HotSpot(TM) 64-Bit Server VM mixed mode
% -----------------------------------------------------------------------------------------------------
% Parallel Computing Toolbox Version 7.0 (R2019a)
  댓글 수: 2
Edric Ellis
Edric Ellis 2021년 7월 21일
Hm, this is strange, and I haven't been able to reproduce the problem. I suspect it's related to your use of load just outside the parfor loop. This creates variables in the enclosing workspace in a way that MATLAB cannot statically determine (in general - in this case, it's obvious to you and I, but not to the parfor analysis). Could you try doing:
% Use the struct-returning form of load:
data = load('my-variables.mat', '...');
% then something like
parfor ii = 1:(nG*nF)
V{ii} = parfun_unpack(cache_path, @(g,f) ...
models.spike_to_wave(g+f/10,data.time, ...
data.sensitivity(:,ff), ...
data.axon_xy{g,f},data.options), ...
[nG nF],ii); %#ok<PFBNS>
end
Calvin Eiber
Calvin Eiber 2021년 7월 21일
I'll give it a go and let you know how it goes.

댓글을 달려면 로그인하십시오.

답변 (0개)

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

제품


릴리스

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by