Can I use multiple cores to generate code for multiple top-level models in parallel?
조회 수: 3 (최근 30일)
이전 댓글 표시
MathWorks Support Team
2024년 5월 23일
답변: MathWorks Support Team
2024년 5월 23일
Is there any way to utilize multiple cores to generate code for multiple top-level models?
채택된 답변
MathWorks Support Team
2024년 5월 23일
As of date of publishing (R2024a), there is no explicit feature for generating code from multiple top-level models at the same time. There is a feature for generating code for multiple model references in the same model in parallel.
Reduce Build Time for Referenced Models by Using Parallel Builds - MATLAB & Simulink (mathworks.com)
It can be possible to use a parfor loop (Parallel Computing Toolbox) with the rtwbuild() or slbuild() commands to generate code for multiple top level models, concurrently, on different cores. To do this you must take precautions to prevent data concurrency issues as the workers generate and read files. To avoid concurrency issues, you can follow the recommendations similar to this documentation page about "sim in parfor".
The following is some pseudocode that shows the basic concepts of how to setup and cleanup workers using the models from the following example. The key is preventing workers from writing to the same files at the same time. We do this by giving each worker its own SLDD cache, cache folder, code gen folder, and working folder.
Reduce Build Time for Referenced Models by Using Parallel Builds - MATLAB & Simulink (mathworks.com)
models = ["ParallelBuildB1", ...
"ParallelBuildB2","ParallelBuildB3"];
spmd %worker setup
curDir = pwd;
addpath(curDir) %add necessary paths
openProject(curDir); %open project if you have one
Simulink.data.dictionary.setupWorkerCache %give each worker its own SLDD cache
%call any custom setup scripts here
end
parfor i = 1:length(models)
%give each worker its own cache and code gen dir to avoid race conditions
%you may need to give each worker its own working dir if other files will be edited
modelCacheFolder = models(i) + "_cache";
modelCodeGenFolder = models(i) + "_code";
mkdir(modelCacheFolder);
mkdir(modelCodeGenFolder);
Simulink.fileGenControl('set', 'CacheFolder', modelCacheFolder, ...
'CodeGenFolder',modelCodeGenFolder);
slbuild(models(i))
end
spmd %worker cleanup
cd(curDir)
bdclose all %close all models
Simulink.data.dictionary.cleanupWorkerCache; %cleanup SLDD cache
end
댓글 수: 0
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Manual Performance Optimization에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!