Graphics argument validation for function in parfor loop

I have a function that I'd like to run serially but also sometimes inside a parfor loop, with the ability to optionally plot output. Understandably, I can't plot or modify graphics within a parpool thread, but I find even having unused relevant argument validation that draws from matlab.graphics.axis.Axes class properties causes a parallel:threadpool:NonConcurrentLibrary error to be thrown during parallel execution. Attached is a code snippet demonstrating what I mean.
I suppose I have two questions. First, is this expected behaviour, given that args is not even invoked in the body of the function? Second, is there any way to retain automatic argument validation for an axis object that plays nicely with parfor, in cases where these arguments are used conditionally and never during parallel execution?
function ArgValidationParForTest()
pool = gcp();
fprintf('Parallel pool initialised with %d workers\n', pool.NumWorkers)
ntests = 10;
parfor i = 1:ntests
TestFunc()
end
end
function TestFunc(args)
arguments
args.?matlab.graphics.axis.Axes
end
fprintf('Called\n')
end

 採用された回答

Catalytic
Catalytic 約18時間 前
編集済み: Catalytic 約18時間 前

1 投票

If you use a process-based pool, it will work --
parpool('Processes')

その他の回答 (1 件)

Matt J
Matt J 約17時間 前
編集済み: Matt J 約16時間 前

1 投票

Second, is there any way to retain automatic argument validation for an axis object that plays nicely with parfor, in cases where these arguments are used conditionally and never during parallel execution?
You would probably have to hand off to a separate validation routine,
function TestFunc(varargin)
fprintf('Called\n')
args=handOff(varargin{:});
end
function args=handOff(varargin)
p = gcp('nocreate');
args=[];
if isempty(p) || ~isa(p,'parallel.ThreadPool')
args=validateGraphics(varargin{:});
elseif ~isempty(varargin)
error 'Handle graphics aren''t supported in threadpools'
end
end
function args=validateGraphics(args)
arguments
args.?matlab.graphics.axis.Axes
end
end

1 件のコメント

Jake
Jake 約2時間 前
Thanks, I think this level of burying removes a lot of the benefits of argument validation for me (CLI completion, code clarity). But, it's nice to know such a solution is possible

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeParallel Computing Fundamentals についてさらに検索

製品

リリース

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by