Using MATLAB Remotely on Windows
As part of the recent Hudson setup I needed to run MATLAB scripts remotely on Windows machines. Despite a fair amount of searching I could find instructions no where for making this work. There were however a lot of unanswered posts asking how to do it.
The main challenge is that MATLAB on Windows doesn’t put its output in the console. It instead opens another window (even if you use -nodesktop or any other param combo). That combined with the barbaric licensing they employ is the challenge.
Here is how I got it to work. This approach works great for doing remote running of scripts. It would be annoying if you were wanting to do prolonged interactive work with MATLAB given the requirement of using “-logfile”. I had to get more tricky to get MATLAB scripts hitting the GPU remotely but leaving those steps out of this post. If someone does need to accomplish that let me know and I can continue in the comments.
Why can this be helpful? I can think of a few reasons:
- You have 1 machine licensed to run MATLAB that is shared by several people that only need occasional access. Linux would work better, but perhaps you are stuck with Windows by preference or circumstance.
- You need to run bulk jobs, perhaps by cron or similar, on Windows machines automatically.
- You want to write larger scripts of which M code is only a component while sticking with bash.
- You want to do some sort of automated testing on Windows of MATLAB code.
I have tested this approach successfully in the 32bit and 64bit versions of Windows XP and Windows 7. Perform these steps as the user on the machine that is licensed to run MATLAB.
- Go here, download the setup.exe file, and run that on your Windows machine.
- In addition to the defaults install the openssh and cygrunsrv packages.
- In a cygwin prompt, run
ssh-host-config -y. All prompts should get answered automatically and you will get a “Host configuration finished” message.
- In a cygwin prompt, run
cygrunsrv -S sshd.
- Now open Control Panel -> Admin Tools -> Services.
- Right click “CYGWIN sshd” and hit properties.
- On the Log On tab, change the radio button to “This account” and specify the user name and password for the licensed MATLAB user on the Windows machine.
- Hit Apply and OK and close the services dialog.
- Open Control Panel -> Admin Tools -> Local Security Settings -> Local Policies
- Make sure the licensed MATLAB user on the Windows machine is explicitly listed for each of the following permissions:
- Adjust the memory quotas for a process.
- Create a token object.
- Log on as a service.
- Replace a process level token.
- In a cygwin prompt,
chown [licensed user] /var/log/sshd.logand
chown -R [licensed user] /var/empty
- Now open the services dialog back up and restart CYGWIN sshd. Make sure it starts back up cleanly.
That should wrap up the setup.
General idea is we are using sshd to connect to the Windows machine and run our MATLAB scripts. The key piece is changing the user that runs the sshd process to be the same user that is licensed to run MATLAB.
Now you can connect to this machine and run MATLAB scripts like this:
matlab -nodesktop -nosplash -r "rand,quit" -logfile matlab_output.txt
That would run rand and put the output in matlab_output.txt. You can similarly (or more practically) run entire scripts:
matlab -nodesktop -nosplash -r "my_file,quit" -logfile script_output.txt
You can chain as many commands/scripts as you would like as part of the “-r” parameter by separating them with commas or semicolons. Note that there will be a delay between you issuing the command and when the specified log file gets written to. This is because in Windows a launcher/starter process runs, spawns the actual MATLAB process, and then exits while the MATLAB process continues to crank. You can use “-wait” to eliminate this and have the launcher process remain running until MATLAB terminates. This is particularly helpful when writing scripts that should not continue until your MATLAB call has completed.
With respect to Hudson this setup is really nice. You can just write bash scripts and use ssh to connect to the Windows slaves. Only change is the need to cat the output files if you want them to get logged and/or archived in Hudson.