{"id":669,"date":"2018-10-30T11:39:04","date_gmt":"2018-10-30T11:39:04","guid":{"rendered":"http:\/\/ri.itservices.manchester.ac.uk\/csf3\/?page_id=669"},"modified":"2025-10-22T10:10:53","modified_gmt":"2025-10-22T09:10:53","slug":"matlab-compilation-advice","status":"publish","type":"page","link":"https:\/\/ri.itservices.manchester.ac.uk\/csf3\/software\/applications\/matlab\/matlab-compilation-advice\/","title":{"rendered":"More MATLAB Compilation Hints &#038; Tips"},"content":{"rendered":"<div class=\"hint\">October 2023: It is NO LONGER necessary to compile your MATLAB code on the CSF.<\/strong> You may compile it if you wish to do so. Hence our compilation instructions are still provided below. Alternatively, see our instructions on <a href=\"..\/uncompiled-matlab\">running un-compiled MATLAB<\/a>.<\/div>\n<h2>Introduction<\/h2>\n<p>Please first read the <a href=\"\/csf3\/software\/applications\/matlab\/compiling-matlab\/\">Basics of Compiling MATLAB Code<\/a> if you haven&#8217;t already done so.<\/p>\n<h2>Recap: Single and multi-threaded compilation<\/h2>\n<p>The <a href=\"\/csf3\/software\/applications\/matlab\/compiling-matlab\/\">Basics of Compiling MATLAB Code<\/a> page describes in details how to compile MATLAB code and the differences between a serial (1-core) app and a multi-core MATLAB app.<\/p>\n<p>In summary, by default, MATLAB jobs are multithreaded so if you <em>submit<\/em> serial (1-core) jobs to the batch system you must also explicitly force MATLAB to perform serial (1-core) computations by compiling your MATLAB code with the\u00a0<code>-R -singleCompThread<\/code>\u00a0option \u00a0at compile time:<\/p>\n<pre>mcc -R -singleCompThread -m mycode.m<\/pre>\n<p>If, for any reason, you choose not to compile with this option, your program may utilize multiple threads and hence multiple cores on the CSF compute node where your batch job runs.\u00a0 If you run a multi-threaded compiled MATLAB program using a serial (1-core) batch job, which is clearly incorrect, you can end up with situations where, for example, your MATLAB jobs are each trying to use 12 cores on a 12 core compute-node. But other jobs could be using those cores.\u00a0 This results in slower run-times and hence reduced throughput.<\/p>\n<p>So the correct method is:<\/p>\n<ul>\n<li>If you want to submit a 1-core MATLAB job, you must compile the MATLAB code to use only 1 core.<\/li>\n<li>If you want to submit a multi-core MATLAB job, you can compile the MATLAB code without forcing it to use 1 core (by default MATLAB will compile to use multi-cores). You should then submit the job to use the whole compute node in the <code>smp.pe<\/code> <a href=\"\/csf3\/batch\/parallel-jobs\/\">parallel environment<\/a>.<\/li>\n<\/ul>\n<p>Note: We have discovered certain cases where the compiler appears to ignore the -R -singleCompThread option leading to over subscription of processor cores. \u00a0This will slow down both your and other people&#8217;s jobs. \u00a0If your code is affected by this bug, you will need to use the\u00a0<code>smp.pe<\/code> <a href=\"\/csf3\/batch\/parallel-jobs\/\">parallel environment<\/a>.<\/p>\n<h2>Dealing with simple dependencies<\/h2>\n<p>Most MATLAB programs consist of several files.\u00a0 For example, you may have main.m that uses a function defined in myfunc.m:<\/p>\n<pre>%contents of main.m\r\nresult = myfunc(1)<\/pre>\n<p>along with<\/p>\n<pre>%Contents of myfunc.m\r\nfunction [out] = myfunc(in)\r\n  out=in + sin(in);\r\nend<\/pre>\n<p>In situations such as this, all you need to do is ensure that main.m and myfunc.m are in the same directory.\u00a0\u00a0<strong>Only main.m need be compiled<\/strong>:<\/p>\n<pre>mcc -R -singleCompThread -m main.m<\/pre>\n<p>The compiler will automatically determine that main.m needs myfunc.m and include it in the resulting executable.<\/p>\n<h2>Dealing with a directory full of dependencies<\/h2>\n<p>More complicated code may include one or more directories containing functions that are required by your main program. You can tell the compiler to include these directories in its search path with the -I flag. For example, if your functions are in the directory myfuns, which is a sub-directory of the directory containing myprogram.m you can do<\/p>\n<pre>mcc -m myprogram.m -R -singleCompThread -I .\/myfuns<\/pre>\n<h2>Faster Compilation<\/h2>\n<p>It can often take a long time to compile large MATLAB programs, as much as 10 minutes or more.\u00a0 It is sometimes possible to speed up this process using by using the -N flag which tells MATLAB not to search for functions from any of the built in toolboxes.\u00a0 That is, the search path is limited to only these locations<\/p>\n<ul>\n<li>matlabroot\\toolbox\\matlab<\/li>\n<li>matlabroot\\toolbox\\local<\/li>\n<li>matlabroot\\toolbox\\compiler\\deploy<\/li>\n<\/ul>\n<p>So, if your program does not use any toolbox functions at all, using the -N flag can improve compilation times without loss of functionality.<\/p>\n<pre>mcc -R -singleCompThread -N -m myprogram.m<\/pre>\n<p>If, however, your program does make use of toolbox functions, using the -N flag can result in runtime errors. For example, consider mystats.m<\/p>\n<pre>%contents of mystats.m\r\nx=normpdf(1.0)\r\ndisp(x)<\/pre>\n<p>This will compile without error<\/p>\n<pre>mcc -R -singleCompThread -N -m mystats.m<\/pre>\n<p>It will, however, give an error message when run<\/p>\n<pre>Undefined function 'normpdf' for input arguments of type 'double'.\r\nError in mystats (line 1)\r\n\r\nMATLAB:UndefinedFunction<\/pre>\n<p>This happens because normpdf is part of the statistics toolbox and we explicitly told MATLAB not to look there at compile time. To include the statistics toolbox we need to use the -p flag as follows<\/p>\n<pre>mcc -R -singleCompThread -N -p stats -m mystats.m<\/pre>\n<p>The resulting executable will now run without error.\u00a0 You can add in as many toolboxes as you like using this mechanism.\u00a0 For example, you could add optimisation as well as statistics with<\/p>\n<pre>mcc -R -singleCompThread -N -p stats -p optim -m mystats.m<\/pre>\n<p>The argument to -p is determined by the toolbox folder name within the MATLAB installation.<\/p>\n<h2>Producing image files<\/h2>\n<p>If your job runs and produces the expected output, but not the images\/plots you are trying to create make sure that you have closed\/saved them in your code. This is not strictly necessary for normal MATLAB usage, but it is essential for compiled code.<\/p>\n<h2>Not everything can be compiled<\/h2>\n<p>Unfortunately, not every MATLAB feature can be included in compiled code.\u00a0 The following are the most commonly used features that cannot be compiled<\/p>\n<ul>\n<li>Anything that uses the symbolic toolbox.<\/li>\n<li>Most MATLAB \u2018apps\u2019 and user interfaces (e.g. optimtool from the optimisation toolbox)<\/li>\n<\/ul>\n<p>Further details of supported MATLAB toolboxes (and exclusions) is available at<\/p>\n<ul>\n<li><a href=\"http:\/\/www.mathworks.co.uk\/products\/compiler\/supported\/compiler_support.html\">http:\/\/www.mathworks.co.uk\/products\/compiler\/supported\/compiler_support.html<\/a><\/li>\n<li><a href=\"https:\/\/uk.mathworks.com\/products\/ineligible_programs.html\">https:\/\/uk.mathworks.com\/products\/ineligible_programs.html<\/a><\/li>\n<\/ul>\n<p>If your code relies on any non-compilable functionality, you will not be able to run many concurrent jobs and will need to contact us for advice.<\/p>\n<h2>The MATLAB Compiler will not make your code go any faster<\/h2>\n<p>A common misconception is that the MATLAB Compiler produces code that runs more quickly than in MATLAB itself.\u00a0 This is not the case. \u00a0We use the MATLAB Compiler in order to ensure that large numbers of HPC MATLAB jobs do not use up all of the campus network licenses (remember: compiled MATLAB code only uses a license during compilation, not when the job runs).<\/p>\n","protected":false},"excerpt":{"rendered":"<p>October 2023: It is NO LONGER necessary to compile your MATLAB code on the CSF. You may compile it if you wish to do so. Hence our compilation instructions are still provided below. Alternatively, see our instructions on running un-compiled MATLAB. Introduction Please first read the Basics of Compiling MATLAB Code if you haven&#8217;t already done so. Recap: Single and multi-threaded compilation The Basics of Compiling MATLAB Code page describes in details how to compile.. <a href=\"https:\/\/ri.itservices.manchester.ac.uk\/csf3\/software\/applications\/matlab\/matlab-compilation-advice\/\">Read more &raquo;<\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"parent":503,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-669","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/ri.itservices.manchester.ac.uk\/csf3\/wp-json\/wp\/v2\/pages\/669","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ri.itservices.manchester.ac.uk\/csf3\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/ri.itservices.manchester.ac.uk\/csf3\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/ri.itservices.manchester.ac.uk\/csf3\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/ri.itservices.manchester.ac.uk\/csf3\/wp-json\/wp\/v2\/comments?post=669"}],"version-history":[{"count":19,"href":"https:\/\/ri.itservices.manchester.ac.uk\/csf3\/wp-json\/wp\/v2\/pages\/669\/revisions"}],"predecessor-version":[{"id":11233,"href":"https:\/\/ri.itservices.manchester.ac.uk\/csf3\/wp-json\/wp\/v2\/pages\/669\/revisions\/11233"}],"up":[{"embeddable":true,"href":"https:\/\/ri.itservices.manchester.ac.uk\/csf3\/wp-json\/wp\/v2\/pages\/503"}],"wp:attachment":[{"href":"https:\/\/ri.itservices.manchester.ac.uk\/csf3\/wp-json\/wp\/v2\/media?parent=669"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}