Use MATLAB Coder to convert matlab code to mex without using multi-threading
조회 수: 2 (최근 30일)
이전 댓글 표시
My code is as follows
function all_Spins = simulate_particles(num_particles, num_steps, all_Spins, SpinLocOri, Gamma, magnetic_field, GdAmp, StepT, spherePositions, sphereRadii, Lx_intra, Ly_intra, Lz_intra, penetration, Lx_extra, Ly_extra, Lz_extra)
parfor mun = 1:num_particles
particle = all_Spins(mun, :);
particle = simulate_particle_motion(num_steps, particle, SpinLocOri, Gamma, magnetic_field, GdAmp, StepT, spherePositions, sphereRadii, mun, Lx_intra, Ly_intra, Lz_intra, penetration, Lx_extra, Ly_extra, Lz_extra);
all_Spins(mun, :) = particle;
end
end
After using MATLAB Coder to convert to mex, I found that multi-threading was not used and my cpu was idle.
댓글 수: 0
답변 (2개)
Infinite_king
2024년 3월 26일
편집: Infinite_king
2024년 3월 26일
Hi 梅花,
By default, MATLAB Coder generates code in which the parfor loop body runs in parallel using OpenMP.
In the code generation configuration object, you can enable 'OpenMP' as follows,
cfg = coder.config('mex');
cfg.EnableOpenMP = true;
% generate code
codegen myFunction -config cfg
For more information, refer the following resources,
Enabling/Disabling OpenMP - https://www.mathworks.com/help/coder/ug/control-compilation-of-parfor-loops.html
MATLAB Answer related to OpenMP support - https://www.mathworks.com/matlabcentral/answers/237411-can-i-make-use-of-openmp-in-my-matlab-mex-files?#answer_190619
Hope this is helpful.
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Execution Speed에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!