How can I use multithreading in Matlab?

142 ビュー (過去 30 日間)
Adriano Gendy
Adriano Gendy 2020 年 3 月 18 日
回答済み: Ed Mitchell 2023 年 3 月 14 日
Hi everyone
I have this sample code here:
a=0;
x = 0;
m = 0;
while (a==0)
m = m+1;
end
for i = 1:60
pause (1)
x = m;
if (i == 60)
a = 1;
end
Is there a way to make the two loops "communicate" with each other at the same time? So that I can update the x value with the m value continuosly?
Thanks in advance
  2 件のコメント
Walter Roberson
Walter Roberson 2020 年 3 月 23 日
spmd with labSend and labReceive
Adriano Gendy
Adriano Gendy 2020 年 3 月 25 日
Thank you for your answer, I tried to used the functions you advised but I am having a little bit of trouble.
clear all
close all
clc
mpiInit
delete(gcp('nocreate'))
labs = parpool(2);
a=0;
x = 0;
m = 0;
spmd
m = m+1;
labSend(m,2);
end
for i = 1:60
pause (1)
x = labReceive(1)
if (i == 60)
a = 1;
end
end
So now I have two probelms:
  1. the variable m does not update in the spmd and it is not considered and integere anymore but a Composite variable
  2. labSend and labReceive do not work because I am not able to define which Lab is the receiver and which the one that sends the value (I tred also the function labSendReceive which does not give me any error and I am abke to pass with it the value m, but m is a Composite and I don't know hot to make it an integer)
Thank you again

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

採用された回答

Ed Mitchell
Ed Mitchell 2023 年 3 月 14 日
For people that are finding this MATLAB answer while searching more generally for how MATLAB manages the number of cores and hyperthreading, please refer to the following MATLAB Answer for more details.

その他の回答 (1 件)

Dinesh Yadav
Dinesh Yadav 2020 年 3 月 23 日
Hi, there are two for loops in the above code. The way MATLAB multi-threading works in case of for loops is it checks for loops and dependent variables between for loops. If there is no dependency they will be parallized otherwise they remain in serial execution. In case of above code as variable x is dependent on m these for loops cannot be parallized.
Also go through similar questions links below asked in community .

カテゴリ

Help Center および File ExchangeStartup and Shutdown についてさらに検索

Translated by