call python function in parfor loop

I have managed to call python function in matlab. However, I would like to know if it is possible to call python function within a parfor loop? If it is not, is there a method to perform parallel computation in my python function?

댓글 수: 5

Raymond Norris
Raymond Norris 2022년 2월 19일
Can't see a reason why you couldn't call python from within parfor. I would suggest trying it and reporting back any issue you run into.
Here is what I get from the command window:
Warning: Unable to load Python object. Saving (serializing) Python objects into a MAT-file is not supported.
> In parallel.internal.pool.deserialize (line 33)
In parallel.internal.pool.deserializeFunction (line 17)
In remoteParallelFunction (line 29)
Ive J
Ive J 2022년 2월 19일
Can you share the snippet as well?
Kevin Zhu
Kevin Zhu 2022년 2월 20일
what do you mean snippet? I have pasted all the warning message from command window here.
Hi.
I am facing the same problem.
My environment is MATLAB R2023a on Windows 10 with Python 3.8.
I would appreciate it if you could give me some guidance.
My MATLAB script is as follows:
clear all
np = py.importlib.import_module('numpy');
nr=10;
alpha=0.2;
q_sbf = np.loadtxt("xxx.txt");
[v_ic_org,~] = pyrunfile('F_extract_initial_state.py',["v_ic_org","u"],q_sbf=q_sbf);
Z=zeros(size(double(v_ic_org)))';
parfor r=1:nr
v_ic = alpha*randn(size(double(v_ic_org)));
tmp = pyrunfile('F_sim_linear_fwd.py',"sol_lin",q_sbf=q_sbf,v_ic=v_ic);
z = double(tmp);
Z(:,r) = z(:,end);
end
Thank you!

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

답변 (1개)

Al Danial
Al Danial 2023년 7월 8일

0 개 추천

An alternative to parfor is to run the loop in parallel with dask. The function running in parallel, sol_lin(), in your case, can either be sent to the cores on your own computer, or to multiple computers on your network.

카테고리

도움말 센터File Exchange에서 Call Python from MATLAB에 대해 자세히 알아보기

제품

릴리스

R2021a

질문:

2022년 2월 19일

답변:

2023년 7월 8일

Community Treasure Hunt

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

Start Hunting!

Translated by