{"id":1006,"date":"2013-08-20T11:05:15","date_gmt":"2013-08-20T11:05:15","guid":{"rendered":"http:\/\/ri.itservices.manchester.ac.uk\/csf-apps\/?page_id=1006"},"modified":"2018-10-30T11:32:48","modified_gmt":"2018-10-30T11:32:48","slug":"matlab-compilation-advice","status":"publish","type":"page","link":"https:\/\/ri.itservices.manchester.ac.uk\/csf-apps\/software\/applications\/matlab\/matlab-compilation-advice\/","title":{"rendered":"MATLAB compilation Hints &#038; Tips"},"content":{"rendered":"<p><br \/>\nThe information here is applicable to both the CSF and DPSF.<\/p>\n<h2>Single and multi-threaded compilation<\/h2>\n<p>By default, MATLAB jobs are multithreaded so if you submit serial jobs you should explicitly force single-threaded computation with the\u00a0<strong>-R -singleCompThread<\/strong>\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.\u00a0 If you run such multi-threaded programs serially you can end up with situations where, for example, 12 programs are each trying to use 12 cores on a 12 core machine.\u00a0 This results in slower run-times and hence reduced throughput.\u00a0 In such cases, you should submit to\u00a0the\u00a0<a href=\"\/csf2\/csf-user-documentation\/parallel-jobs\/\">smp.pe\u00a0parallel environment<\/a>.<\/p>\n<p>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<a href=\"\/csf2\/csf-user-documentation\/parallel-jobs\/\">smp.pe\u00a0parallel environmen<\/a>t.<a href=\"http:\/\/condor.eps.manchester.ac.uk\/tutorials\/taking-advantage-of-dynamic-slots\/\"><br \/>\n<\/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>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>\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>The information here is applicable to both the CSF and DPSF. Single and multi-threaded compilation By default, MATLAB jobs are multithreaded so if you submit serial jobs you should explicitly force single-threaded computation with the\u00a0-R -singleCompThread\u00a0option \u00a0at compile time. mcc -R -singleCompThread -m mycode.m If, for any reason, you choose not to compile with this option, your program may utilize multiple threads and hence multiple cores.\u00a0 If you run such multi-threaded programs serially you can.. <a href=\"https:\/\/ri.itservices.manchester.ac.uk\/csf-apps\/software\/applications\/matlab\/matlab-compilation-advice\/\">Read more &raquo;<\/a><\/p>\n","protected":false},"author":15,"featured_media":0,"parent":233,"menu_order":0,"comment_status":"open","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-1006","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/ri.itservices.manchester.ac.uk\/csf-apps\/wp-json\/wp\/v2\/pages\/1006","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ri.itservices.manchester.ac.uk\/csf-apps\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/ri.itservices.manchester.ac.uk\/csf-apps\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/ri.itservices.manchester.ac.uk\/csf-apps\/wp-json\/wp\/v2\/users\/15"}],"replies":[{"embeddable":true,"href":"https:\/\/ri.itservices.manchester.ac.uk\/csf-apps\/wp-json\/wp\/v2\/comments?post=1006"}],"version-history":[{"count":20,"href":"https:\/\/ri.itservices.manchester.ac.uk\/csf-apps\/wp-json\/wp\/v2\/pages\/1006\/revisions"}],"predecessor-version":[{"id":4911,"href":"https:\/\/ri.itservices.manchester.ac.uk\/csf-apps\/wp-json\/wp\/v2\/pages\/1006\/revisions\/4911"}],"up":[{"embeddable":true,"href":"https:\/\/ri.itservices.manchester.ac.uk\/csf-apps\/wp-json\/wp\/v2\/pages\/233"}],"wp:attachment":[{"href":"https:\/\/ri.itservices.manchester.ac.uk\/csf-apps\/wp-json\/wp\/v2\/media?parent=1006"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}