{"id":1868,"date":"2020-07-06T13:32:12","date_gmt":"2020-07-06T17:32:12","guid":{"rendered":"https:\/\/www.bu.edu\/engit\/?page_id=1868"},"modified":"2025-12-10T08:25:38","modified_gmt":"2025-12-10T13:25:38","slug":"matlab","status":"publish","type":"page","link":"https:\/\/www.bu.edu\/engit\/knowledge-base\/grid\/software\/matlab\/","title":{"rendered":"Matlab on ENG Linux Labs Systems"},"content":{"rendered":"<p style=\"margin-top: 22pt; margin-bottom: 0pt; font-family: Calibri; font-size: 18.0pt;\"><span style=\"font-weight: bold;\">Compiling Matlab executables<\/span><\/p>\n<p style=\"margin: 0in; font-family: Calibri; font-size: 11.0pt;\">As a quick example, create the file mcctest.m , which contains the following:<\/p>\n<div style=\"direction: ltr;\">\n<table border=\"1\" cellpadding=\"0\" cellspacing=\"0\" valign=\"top\" style=\"direction: ltr; border-collapse: collapse; border: 1pt solid #A3A3A3;\" title=\"\" summary=\"\">\n<tbody>\n<tr>\n<td style=\"background-color: #f2f2f2; vertical-align: top; width: 4.4479in; padding: 4pt 4pt 4pt 4pt; border: 1pt solid #A3A3A3;\">\n<p style=\"margin: 0in; font-family: 'Courier New'; font-size: 11.0pt;\"><span style=\"background: #F3F5F7;\">function mcctest<br \/>\nX = [0:0.01:2*3.14];<br \/>\nY = sin(X)<br \/>\nExit<\/span><\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<p style=\"margin: 0in; font-family: Calibri; font-size: 11.0pt;\">Compile it using:<\/p>\n<div style=\"direction: ltr;\">\n<table border=\"1\" cellpadding=\"0\" cellspacing=\"0\" valign=\"top\" style=\"direction: ltr; border-collapse: collapse; border: 1pt solid #A3A3A3;\" title=\"\" summary=\"\">\n<tbody>\n<tr>\n<td style=\"background-color: #f2f2f2; vertical-align: top; width: 1.884in; padding: 4pt 4pt 4pt 4pt; border: 1pt solid #A3A3A3;\">\n<p style=\"margin: 0in; font-family: 'Courier New'; font-size: 11.0pt;\"><span style=\"background: #F3F5F7;\">mcc85 -m mcctest.m<\/span><\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<p style=\"margin: 0in; font-family: Calibri; font-size: 11.0pt;\">You will end up with a number of files in that directory, including an &#8220;mcctest&#8221; binary, as well as a &#8220;run_mcctest.sh&#8221; script.<\/p>\n<p style=\"margin: 0in; font-family: Calibri; font-size: 11.0pt;\">To run the binary, run:<\/p>\n<div style=\"direction: ltr;\">\n<table border=\"1\" cellpadding=\"0\" cellspacing=\"0\" valign=\"top\" style=\"direction: ltr; border-collapse: collapse; border: 1pt solid #A3A3A3;\" title=\"\" summary=\"\">\n<tbody>\n<tr>\n<td style=\"background-color: #f2f2f2; vertical-align: top; width: 3.1673in; padding: 4pt 4pt 4pt 4pt; border: 1pt solid #A3A3A3;\">\n<p style=\"margin: 0in; font-family: 'Courier New'; font-size: 11.0pt;\"><span style=\"background: #F3F5F7;\">.\/run_mcctest.sh \/PATH\/TO\/MATLAB<\/span><\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<p style=\"margin: 0in; font-family: Calibri; font-size: 11.0pt;\">In this case, that would be:<\/p>\n<div style=\"direction: ltr;\">\n<table border=\"1\" cellpadding=\"0\" cellspacing=\"0\" valign=\"top\" style=\"direction: ltr; border-collapse: collapse; border: 1pt solid #A3A3A3;\" title=\"\" summary=\"\">\n<tbody>\n<tr>\n<td style=\"background-color: #f2f2f2; vertical-align: top; width: 3.9006in; padding: 4pt 4pt 4pt 4pt; border: 1pt solid #A3A3A3;\">\n<p style=\"margin: 0in; font-family: 'Courier New'; font-size: 11.0pt;\"><span style=\"background: #F3F5F7;\">.\/run_mcctest.sh \/ad\/eng\/opt\/matlab-8.5\/<\/span><\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<p style=\"margin: 0in; font-family: Calibri; font-size: 11.0pt;\">On other platforms, run &#8220;help mcc&#8221; from within Matlab for more information.<\/p>\n<p style=\"margin: 0in; font-family: Calibri; font-size: 11.0pt;\">Note that you must test-run it once directly before qsub&#8217;bing it, so that the MCR cache can build without causing race conditions. (You don&#8217;t have to run it to completion &#8212; you can always ctrl-c out of it after it&#8217;s finished building the cache and has begun executing.)<\/p>\n<p style=\"margin-top: 22pt; margin-bottom: 0pt; font-family: Calibri; font-size: 18.0pt;\"><span style=\"font-weight: bold;\">Run in Batch<\/span><\/p>\n<p style=\"margin: 0in; font-family: Calibri; font-size: 11.0pt;\">Now, to qsub the compiled executable, you would run:<\/p>\n<div style=\"direction: ltr;\">\n<table border=\"1\" cellpadding=\"0\" cellspacing=\"0\" valign=\"top\" style=\"direction: ltr; border-collapse: collapse; border: 1pt solid #A3A3A3;\" title=\"\" summary=\"\">\n<tbody>\n<tr>\n<td style=\"background-color: #f2f2f2; vertical-align: top; width: 4.4479in; padding: 4pt 4pt 4pt 4pt; border: 1pt solid #A3A3A3;\">\n<p style=\"margin: 0in; font-family: 'Courier New'; font-size: 11.0pt;\"><span style=\"background: #F3F5F7;\">qsub -cwd -b y &#8220;.\/run_mcctest.sh \/ad\/eng\/opt\/matlab-8.5\/&#8221;<\/span><\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<p style=\"margin: 0in; font-family: Calibri; font-size: 11.0pt;\">Note that your mileage may vary with newer versions of MATLAB. In particular, the newest versions want to put the MCR cache in your home directory, where batch qsub jobs can&#8217;t find it. To get around this set the MCR_CACHE_ROOT variable to the directory you wish to have the MCR cache in (such as \/mnt\/nokrb\/yourusername), as described <a href=\"http:\/\/matlab4compbio.blogspot.com\/2010\/04\/how-to-solve-mcr-cache-access-problems.html\">here<\/a> and <a href=\"http:\/\/undocumentedmatlab.com\/blog\/speeding-up-compiled-apps-startup\">here<\/a>. You should also change the MATLAB_PREFDIR variable so that MATLAB doesn&#8217;t try to use a Kerberized directory within qsub.<\/p>\n<p style=\"margin: 0in; font-family: Calibri; font-size: 11.0pt;\">Taken together you could use a wrapper script like the below and qsub that (or just embed the export commands inside run_mcctest.sh itself.)<\/p>\n<div style=\"direction: ltr;\">\n<table border=\"1\" cellpadding=\"0\" cellspacing=\"0\" valign=\"top\" style=\"direction: ltr; border-collapse: collapse; border: 1pt solid #A3A3A3;\" title=\"\" summary=\"\">\n<tbody>\n<tr>\n<td style=\"background-color: #f2f2f2; vertical-align: top; width: 4.4479in; padding: 4pt 4pt 4pt 4pt; border: 1pt solid #A3A3A3;\">\n<p style=\"margin: 0in; font-family: 'Courier New'; font-size: 11.0pt;\"><span style=\"background: #F3F5F7;\">#$ -cwd<br \/>\nexport MATLAB_PREFDIR=\/mnt\/nokrb\/$USER\/MATLAB<br \/>\nexport MCR_CACHE_ROOT=\/tmp\/$USER\/$RANDOM<br \/>\n.\/run_mcctest.sh \/ad\/eng\/opt\/matlab-8.5\/<\/span><\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<p style=\"margin-top: 22pt; margin-bottom: 0pt; font-family: Calibri; font-size: 18.0pt;\"><span style=\"font-weight: bold;\">Run scripts in Matlab without compiling them<\/span><\/p>\n<p style=\"margin: 0in; font-family: Calibri; font-size: 11.0pt;\">This should work equally well in all versions of Matlab, but be warned, it *does* grab a license for each instance that is executing! So if you run a hundred jobs, you could easily eat all available licenses and cause denial of service for people who are just trying to use Matlab on their desktop!<\/p>\n<div style=\"direction: ltr;\">\n<table border=\"1\" cellpadding=\"0\" cellspacing=\"0\" valign=\"top\" style=\"direction: ltr; border-collapse: collapse; border: 1pt solid #A3A3A3;\" title=\"\" summary=\"\">\n<tbody>\n<tr>\n<td style=\"background-color: #f2f2f2; vertical-align: top; width: 4.359in; padding: 4pt 4pt 4pt 4pt; border: 1pt solid #A3A3A3;\">\n<p style=\"margin: 0in; font-family: 'Courier New'; font-size: 11.0pt;\"><span style=\"background: #F3F5F7;\">matlab -r mcctest -nodesktop -nojvm -nosplash<\/span><\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<p style=\"margin: 0in; font-family: Calibri; font-size: 11.0pt;\">This runs your .m file and exits Matlab at the end. Note that if you qsub this, the .o file contains EVERYTHING that Matlab outputs to text, including its little version splash at the beginning.<\/p>\n<p style=\"margin: 0in; font-family: Calibri; font-size: 11.0pt;\">An example qsub script would be:<\/p>\n<div style=\"direction: ltr;\">\n<table border=\"1\" cellpadding=\"0\" cellspacing=\"0\" valign=\"top\" style=\"direction: ltr; border-collapse: collapse; border: 1pt solid #A3A3A3;\" title=\"\" summary=\"\">\n<tbody>\n<tr>\n<td style=\"background-color: #f2f2f2; vertical-align: top; width: 4.477in; padding: 4pt 4pt 4pt 4pt; border: 1pt solid #A3A3A3;\">\n<p style=\"margin: 0in; font-family: 'Courier New'; font-size: 11.0pt;\"><span style=\"background: #F3F5F7;\">#$ -V<br \/>\n#$ -cwd<br \/>\n#$ -N My_Codename<br \/>\n#$ -j y<\/span><\/p>\n<p style=\"margin: 0in; font-family: 'Courier New'; font-size: 11.0pt;\"><span style=\"background: #F3F5F7;\">export MATLAB_PREFDIR=\/mnt\/nokrb\/$USER\/MATLAB<br \/>\n\/afs\/bu.edu\/common\/IT\/matlab-7.14\/bin\/matlab -nodisplay -r &#8220;My_Codename,exit&#8221;<\/span><\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<p style=\"margin: 0in; font-family: Calibri; font-size: 11.0pt;\">Using the script makes it so that you don&#8217;t have to put so many flags into the command line. You&#8217;ll see we put some commonly used flags (&#8220;-j y&#8221; for joining .o and .e files, -N for the name of the output files, -V for passing through all variables from the current running shell) directly into the script.<\/p>\n<p style=\"margin-top: 22pt; margin-bottom: 0pt; font-family: Calibri; font-size: 18.0pt;\"><span style=\"font-weight: bold;\">Using GPUs<\/span><\/p>\n<p style=\"margin: 0in; font-family: Calibri; font-size: 11.0pt;\">If you are using MATLAB&#8217;s GPU features, make sure to specify &#8220;-l gpu=1&#8221; (or a higher number for multi-GPU codes) and use a queue that&#8217;s gpu-enabled. Remember you need to ask enghelp for permission to be added to these queues.<\/p>\n<p style=\"margin-top: 22pt; margin-bottom: 0pt; font-family: Calibri; font-size: 18.0pt;\"><span style=\"font-weight: bold;\">Links<\/span><\/p>\n<ul type=\"disc\" style=\"margin-left: .375in; direction: ltr; unicode-bidi: embed; margin-top: 0in; margin-bottom: 0in;\">\n<li style=\"margin-top: 0; margin-bottom: 0; vertical-align: middle; color: #cc0000;\"><a href=\"http:\/\/www-rohan.sdsu.edu\/doc\/matlab\/toolbox\/compiler\/mcc.html\"><span style=\"font-family: Calibri; font-size: 11.0pt;\">http:\/\/www-rohan.sdsu.edu\/doc\/matlab\/toolbox\/compiler\/mcc.html <\/span><\/a><\/li>\n<li style=\"margin-top: 0; margin-bottom: 0; vertical-align: middle; color: #cc0000;\"><a href=\"http:\/\/undocumentedmatlab.com\/blog\/speeding-up-compiled-apps-startup\"><span style=\"font-family: Calibri; font-size: 11.0pt;\">http:\/\/undocumentedmatlab.com\/blog\/speeding-up-compiled-apps-startup <\/span><\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Compiling Matlab executables As a quick example, create the file mcctest.m , which contains the following: function mcctest X = [0:0.01:2*3.14]; Y = sin(X) Exit Compile it using: mcc85 -m mcctest.m You will end up with a number of files in that directory, including an &#8220;mcctest&#8221; binary, as well as a &#8220;run_mcctest.sh&#8221; script. To run [&hellip;]<\/p>\n","protected":false},"author":1470,"featured_media":0,"parent":138,"menu_order":1,"comment_status":"closed","ping_status":"closed","template":"","meta":[],"_links":{"self":[{"href":"https:\/\/www.bu.edu\/engit\/wp-json\/wp\/v2\/pages\/1868"}],"collection":[{"href":"https:\/\/www.bu.edu\/engit\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.bu.edu\/engit\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.bu.edu\/engit\/wp-json\/wp\/v2\/users\/1470"}],"replies":[{"embeddable":true,"href":"https:\/\/www.bu.edu\/engit\/wp-json\/wp\/v2\/comments?post=1868"}],"version-history":[{"count":3,"href":"https:\/\/www.bu.edu\/engit\/wp-json\/wp\/v2\/pages\/1868\/revisions"}],"predecessor-version":[{"id":2958,"href":"https:\/\/www.bu.edu\/engit\/wp-json\/wp\/v2\/pages\/1868\/revisions\/2958"}],"up":[{"embeddable":true,"href":"https:\/\/www.bu.edu\/engit\/wp-json\/wp\/v2\/pages\/138"}],"wp:attachment":[{"href":"https:\/\/www.bu.edu\/engit\/wp-json\/wp\/v2\/media?parent=1868"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}