2
votes

I have a Simulink model that, before starting, execute a script (in the callback) (let's call the script constants.m) to set various constants and parameters in the base workspace. Now, I would like the Simulink model to be executed with various set of constants and parameters (i.e. having multiple files, constants1.m, constants2.m, etc.) That would remove the need to manually change the same script each time before running the Simulink model.

I was able to set a script (batchProcessing.m), that load, execute and close the model multiple times. However, I would like to have all the jobs to run simultaneously on various cores (I have a Xeon CPU with 12 cores). Would it be possible to do this without the Parallel Toolbox? Would it be possible with a batch script (.bat)?

Update:

Here what i tried with the help of macduff answer :

My test function :

function test3(n, ii)
A = rand(n);
B = rand(n);
tic; C=A*B; 
tableTitle = {'Resultat'};
fileID = fopen(strcat('D:\Documents\MATLAB', '\', 'batchResults', num2str(ii), '.txt'),'w');
fprintf(fileID, '%12s\n', tableTitle{1});
fprintf(fileID, '%12.5f\n',C(1:10));
fclose(fileID);
toc
end

The .bat file :

set MATLAB_EXE_PATH = "C:\MATLAB\R2010a\bin\matlab.exe"
start /MIN /LOW "MATLAB" "%MATLAB_EXE_PATH%" -nosplash -nodesktop -r "test3('%100%','%1');"
start /MIN /LOW "MATLAB" "%MATLAB_EXE_PATH%" -nosplash -nodesktop -r "test3('%100%','%2');"

Nothing happens, and the .txt are not created. If I run the test3 function directly in Matlab, it works correctly. So the problem is with the batch file.

Update with correct answer

Again, with the help of macduff answer, here what i did :

set MATLAB_EXE_PATH=C:\MATLAB\R2010a\bin\matlab.exe
set arg1=5000
set arg2=1
start /MIN /LOW %MATLAB_EXE_PATH% -nodesktop -nosplash -r "cd('D:\Documents\MATLAB\'); test3(%arg1%,%arg2%); exit;"
set arg2=2
start /MIN /LOW %MATLAB_EXE_PATH% -nodesktop -nosplash -r "cd('D:\Documents\MATLAB\'); test3(%arg1%,%arg2%); exit;"

Now it would be great if the Matlab windows could stay closed and not popup on the screen.

3

3 Answers

2
votes

This is a need that regularly comes up, the need to run several Matlab sessions without Parallel Toolbox. This indeed is possible. I believe the method you suggest is quite possible, and likely the simplest, though there are many ways to solve this problem.

set MATLAB_EXE_PATH = "C:\MATLAB\2009b\win32\matlab.exe"
start /MIN /LOW "MATLAB" "%MATLAB_EXE_PATH%" -nosplash -nodesktop -r "functionIWantToRun('%stringArgumentsToThisFunction%',...)"

You can just repeat the start command above with different parameters to your initial function call to make your batch job.

EDIT

Guess I wasn't very clear, sorry. Here's an example of a batch file:

rem In a file like, runMe.bat on the Desktop that you double click
set MATLAB_EXE_PATH = "C:\MATLAB\2009b\win32\matlab.exe"
start /MIN /LOW "MATLAB" "%MATLAB_EXE_PATH%" -nosplash -nodesktop -r "functionIWantToRun('%stringArgumentsToThisFunction%',...)"
start /MIN /LOW "MATLAB" "%MATLAB_EXE_PATH%" -nosplash -nodesktop -r "functionIWantToRun('%differentStringArgumentsToThisFunction%',...)"

I've never needed to specify which core on which to run Matlab. When a new process spools up it is distributed to each core, in my experience, so I always just run as many processes as I have cores. This has been very effective, however if you need finer grain control you should consider dbenham advice about PSEXEC and such. HTH!

EDIT

You'll need to put the script in the startup directory, the directory where Matlab starts up, see this link. I tried the following, just as a quick and dirty check, where I've got test3.m in C:\Data.

set V=100
set K=2
start "MATLAB" /LOW /MAX "MATLAB" -nosplash -nodesktop -r "chdir('C:\Data'); test3(%V%,%K%);"

It works! But I don't have the MATLAB version called out, just using the PATH.

Here's my final offering. :-)

m-file:

function test3(n, ii)
  A = rand(n);
  B = rand(n);
  tic; C=A*B; 
  tableTitle = {'Resultat'};
  fileID = fopen(strcat('C:\', 'batchResults', num2str(ii), '.txt'),'w');
  fprintf(fileID, '%12s\n', tableTitle{1});
  fprintf(fileID, '%12.5f\n',C(1:10));
  fclose(fileID);
  toc
  exit;
end

Bat File:

set MATLAB_EXE_PATH=C:\MATLAB\R2009bSP1\bin\win32\matlab.exe
start /MIN /LOW "MATLAB" "%MATLAB_EXE_PATH%" -nosplash -nodesktop -r "test3('100','1');"
start /MIN /LOW "MATLAB" "%MATLAB_EXE_PATH%" -nosplash -nodesktop -r "test3('100','2');"

set V=100
set K=2
start "MATLAB" /LOW /MAX "MATLAB" -nosplash -nodesktop -r "chdir('C:\Data'); test3(%V%,%K%);"
set V=100
set K=3
start "MATLAB" /LOW /MAX "MATLAB" -nosplash -nodesktop -r "chdir('C:\Data'); test3(%V%,%K%);"

Sample Output:

Resultat
26.90660
25.58899
23.75740
23.83745
23.75160
27.37161
23.52786
28.48510
27.30217
22.07751

Enjoy!

1
votes

You can simply write a master batch script that uses multiple START commands, one per desired process. The operating system should balance the use of your processors automatically.

Or, if you want to make sure that a given process is targeted to a specific processor, you download and use Microsoft's PSEXEC

If you have more processes to run than you have processors, and you want to run them in parallel, but wait before running the next process until a processor is free, then have a look at my answer to Parallel execution of shell processes

0
votes

Yes, it's possible with the Parallel Computing Toolbox, see Videos & examples, "Parallel Computing with Simulink" section. You have to be careful though how you set up the data for each instance of model so that it's available for each worked.