Multithreading library in matlab

7 vues (au cours des 30 derniers jours)
Yessine KAMMOUN
Yessine KAMMOUN le 4 Mai 2021
Commenté : Walter Roberson le 13 Oct 2021
Hi,
My question is simple, is there a way to run threads in matlab like in cpp using <thread> or <pthread> standard library.
Thx in advance !

Réponses (1)

Jan
Jan le 4 Mai 2021
No. You can use the command parfor and spmd, but this does not match the C++ threads exactly.
  4 commentaires
Raymond Norris
Raymond Norris le 13 Oct 2021
Modifié(e) : Raymond Norris le 13 Oct 2021
At the time of Walter's writing, he's right. With that said, in R2021b we've now released backgroundPool, which can execute on a separate thread if you don't have Parallel Computing Toolbox or up to 8 threads with Parallel Computing Toolbox.
Walter Roberson
Walter Roberson le 13 Oct 2021
Communicating to/from background threads is still weak. Background threads are invoked through the parfeval() mechanism, which involves passing in a function handle, and passing in a list of parameters.
Background evaluation does not read from the environment; you cannot access the base workspace, and globals are not copied into the background.
Notice that the global I set in one thread is not always available on the next run, but is sometimes. This is likely due to the pool generated here having 4 workers: if setting globals works inside an individual worker until the worker is destroyed, but the globals are not copied between workers, then you would expect this behaviour, that the change does not become visible until the worker ends up getting re-used (you cannot choose the worker.)
From past experience, the behaviour of globals tells me that it is quite likely that device objects are not being copied into the background space, and so need to be created in the background space.
To send back data from a background thread, maybe it would work to create a dataqueue and pass it into the thread as a parameter. But that would require the Parallel Computing Toolbox if it were going to work at all.
global AAA
AAA = 'this is a global';
BBB = 'this is a local in the base workspace'
BBB = 'this is a local in the base workspace'
P = backgroundPool
P =
BackgroundPool with properties: NumWorkers: 2
tests = {@testbackground1, @testbackground2, @testbackground3, @testbackground3, @testbackground3, @testbackground3, @testbackground3};
for K = 1 : length(tests)
F = parfeval(P, tests{K}, 1)
try
fetchOutputs(F)
catch ME
fprintf('well, test #%d did not work', K)
end
end
F =
FevalFuture with properties: ID: 7 Function: @testbackground1 CreateDateTime: 13-Oct-2021 18:55:22 StartDateTime: 13-Oct-2021 18:55:22 RunningDuration: 0 days 0h 0m 0s State: finished (unread) Error: Unrecognized function or variable 'BBB'. LiveEditorEvaluationHelperEeditorId>testbackground1 (line 19)
well, test #1 did not work
F =
FevalFuture with properties: ID: 8 Function: @testbackground2 CreateDateTime: 13-Oct-2021 18:55:23 StartDateTime: 13-Oct-2021 18:55:23 RunningDuration: 0 days 0h 0m 0s State: finished (unread) Error: Use of function evalin is not supported on a thread-based worker. Use alternatives supported on the background pool. LiveEditorEvaluationHelperEeditorId>testbackground2 (line 23)
well, test #2 did not work
F =
FevalFuture with properties: ID: 9 Function: @testbackground3 CreateDateTime: 13-Oct-2021 18:55:23 StartDateTime: 13-Oct-2021 18:55:23 RunningDuration: 0 days 0h 0m 0s State: finished (read) Error: none
ans = []
F =
FevalFuture with properties: ID: 10 Function: @testbackground3 CreateDateTime: 13-Oct-2021 18:55:23 StartDateTime: 13-Oct-2021 18:55:23 RunningDuration: 0 days 0h 0m 0s State: finished (read) Error: none
ans = []
F =
FevalFuture with properties: ID: 11 Function: @testbackground3 CreateDateTime: 13-Oct-2021 18:55:23 StartDateTime: 13-Oct-2021 18:55:23 RunningDuration: 0 days 0h 0m 0s State: finished (read) Error: none
ans = 'new value set in background'
F =
FevalFuture with properties: ID: 12 Function: @testbackground3 CreateDateTime: 13-Oct-2021 18:55:23 StartDateTime: 13-Oct-2021 18:55:23 RunningDuration: 0 days 0h 0m 0s State: finished (read) Error: none
ans = 'new value set in background'
F =
FevalFuture with properties: ID: 13 Function: @testbackground3 CreateDateTime: 13-Oct-2021 18:55:23 StartDateTime: 13-Oct-2021 18:55:23 RunningDuration: 0 days 0h 0m 0s State: finished (read) Error: none
ans = 'new value set in background'
delete(P)
function out = testbackground1
out = BBB; %refering to base workspace
end
function out = testbackground2
out = evalin('base', 'BBB'); %refering to base workspace explicitly
end
function out = testbackground3
global AAA;
out = AAA; %refering to explicit global
AAA = 'new value set in background'
end

Connectez-vous pour commenter.

Catégories

En savoir plus sur Asynchronous Parallel Programming dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by