Using parsim with SIMULINK model containing c-mex s-functions

조회 수: 10 (최근 30일)
Fawad Farooq Ashraf
Fawad Farooq Ashraf 2022년 2월 3일
댓글: Fawad Farooq Ashraf 2022년 2월 15일
I have a simulation with multiple S-functions (level 2 written in c++). I want to run a Monte-Carlo analysis for my problem and to speed up the process, I want to run the simulations in parallel using the parsim command. There's a parameter for the S-functions "DX" which would be different for every simulation.
model = 'my_model';
load_system(model);
numSims = 10;
for i = 1:numSims
in(i) = Simulink.SimulationInput(model);
in(i) = in(i).setVariable('DX',my_data(:,i));
end
simOut = parsim(in);
According to my understanding, this should work but it gives me errors.
My c++ S-functions is supposed to load and read some DAT files in order to run and they are in a different folder. The path to access the files is written inside the code. I have added an exception in the code that it should give error "Data could not be loaded" in case the code fails to load and read the files. I get this error when I try to run the simulation using "parsim" command. However, it works fine using the "sim" command.
What could I be doing wrong here? Am I missing something? Any help would be highly appreciated.
  댓글 수: 7
Fawad Farooq Ashraf
Fawad Farooq Ashraf 2022년 2월 14일
편집: Fawad Farooq Ashraf 2022년 2월 15일
Actually my simulation consists of a lot of c++ s-functions with other .cpp files as well which contain a lot of functions. I've defined a class in one of the c++ files which reads the data using fscanf function and stores it into a variable and then I use it over different files using the extern command as well or like you said using PWork or RWork variables. I've also initialized the fp variable (and all similar ones) to NULL at the start of my simulation and in mdlTerminate I delete them as well (as they're pointers).
My problem is that it doesn't read the data file when I try to use parsim command. It works with the sim command.
Fawad Farooq Ashraf
Fawad Farooq Ashraf 2022년 2월 15일
I found a link
After reading this, I think that parsim creates temporary directories for each of its workers and sends them the files listed in the model dependency analyzer. In the model dependency analyzer, my data files aren't listed there. Maybe that's the reason it's unable to read/load them.
I've tried to use SetupFcn and setPreSimFcn to copy files to the current working directory of each model but I can't seem to do it correctly. Do you know anything about it?

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

답변 (1개)

Benjamin Thompson
Benjamin Thompson 2022년 2월 14일
Alternatively, you can read data from a binary file using fopen and fread in an M file in MATLAB. Read each dataset into the MATLAB workspace and use a Simulink foreach subsystem to pass data to the sin function in Simulink.
  댓글 수: 1
Fawad Farooq Ashraf
Fawad Farooq Ashraf 2022년 2월 14일
Like I mentioned that my actual simulation consists of a lot of files and doing this would be cumbersome. I do not want to change my validated code which is working perfectly with the sim command.

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

카테고리

Help CenterFile Exchange에서 Introduction to Installation and Licensing에 대해 자세히 알아보기

제품


릴리스

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by