{"id":107,"date":"2007-09-12T08:51:11","date_gmt":"2007-09-12T13:51:11","guid":{"rendered":"https:\/\/blogs.mathworks.com\/loren\/2007\/09\/12\/controlling-multithreading\/"},"modified":"2007-09-11T08:51:43","modified_gmt":"2007-09-11T13:51:43","slug":"controlling-multithreading","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/loren\/2007\/09\/12\/controlling-multithreading\/","title":{"rendered":"Controlling Multithreading"},"content":{"rendered":"<div xmlns:mwsh=\"https:\/\/www.mathworks.com\/namespace\/mcode\/v1\/syntaxhighlight.dtd\" class=\"content\">\r\n   <introduction>\r\n      <p>With Release <a href=\"https:\/\/www.mathworks.com\/products\/matlab\/whatsnew.html\">R2007b<\/a>, you now have the ability to set (and get) the maximum number of allowed threads.  What does this mean for you?\r\n      <\/p>\r\n   <\/introduction>\r\n   <h3>Contents<\/h3>\r\n   <div>\r\n      <ul>\r\n         <li><a href=\"#1\">The Skinny on maxNumCompThreads<\/a><\/li>\r\n         <li><a href=\"#2\">When to Use maxNumCompThreads<\/a><\/li>\r\n         <li><a href=\"#3\">If You Find a Performance Issue...<\/a><\/li>\r\n      <\/ul>\r\n   <\/div>\r\n   <h3>The Skinny on maxNumCompThreads<a name=\"1\"><\/a><\/h3>\r\n   <p>Here's some pertinent information about <a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/maxnumcompthreads.html\"><tt>maxNumCompThreads<\/tt><\/a>.\r\n   <\/p>\r\n   <div>\r\n      <ul>\r\n         <li><tt>maxNumCompThreads<\/tt> allows you to programmatically control the maximum number of computational threads that MATLAB uses.\r\n         <\/li>\r\n         <li>Unlike the setting from the preferences panel, this setting does not persist between MATLAB sessions.<\/li>\r\n         <li><tt>maxNumCompThreads<\/tt> controls both the JIT and BLAS, meaning both element-wise and matrix operations.  Since the JIT doesn't operate at the command\r\n            line, <tt>maxNumCompThreads<\/tt> only affects element-wise computations performed from M-files.\r\n         <\/li>\r\n         <li>The <tt>'automatic'<\/tt> setting is designed to take best advantage of the hardware the program is currently running on.\r\n         <\/li>\r\n         <li>Not all operations run with the maximum number of computational threads.  When MATLAB uses fewer than the maximum, it's avoiding\r\n            the overhead of threading which can dominate certain operations, for example, matrix multiplication for 2x2 matrices.\r\n         <\/li>\r\n         <li>Currently, only a subset of functions that could benefit from multithreading are taking advantage of it.  We will expand this\r\n            list with each upcoming release.\r\n         <\/li>\r\n      <\/ul>\r\n   <\/div>\r\n   <h3>When to Use maxNumCompThreads<a name=\"2\"><\/a><\/h3>\r\n   <p>The main message here is that you should not generally need to use this function at all!  Why?  Because we'd like to make\r\n      MATLAB do the best job possible for you.  However, there are some situations where the use of <tt>maxNumCompThreads<\/tt> is merited.\r\n   <\/p>\r\n   <div>\r\n      <ul>\r\n         <li>Benchmarking - This needs to be done with care. For example, as I mentioned earlier, matrix multiplication doesn't use multiple\r\n            threads for 2x2 matrices since the thread overhead dominates the numerical work that's being done.  Be sure to test a variety\r\n            of sizes to characterize your results.\r\n         <\/li>\r\n         <li>If you notice some slowdown in code that was previously running faster, you can programmatically reduce <tt>maxNumCompThreads<\/tt> and then restore the <tt>maxNumCompThreads<\/tt> value when the calculation is finished.\r\n         <\/li>\r\n      <\/ul>\r\n   <\/div>\r\n   <h3>If You Find a Performance Issue...<a name=\"3\"><\/a><\/h3>\r\n   <p>Please let us know if you encounter a situation in which setting <tt>maxNumCompThreads<\/tt> manually is necessary for better performance, by contacting <a href=\"https:\/\/www.mathworks.com\/support\/contact_us\/index.html\">Technical Support<\/a>. It is our intention that you should not need to use <tt>maxNumCompThreads<\/tt> and it will help us to improve the code if you notify us of such situations.\r\n   <\/p><script language=\"JavaScript\">\r\n<!--\r\n\r\n    function grabCode_0e740cd7e04e46b2b00a5346897f936c() {\r\n        \/\/ Remember the title so we can use it in the new page\r\n        title = document.title;\r\n\r\n        \/\/ Break up these strings so that their presence\r\n        \/\/ in the Javascript doesn't mess up the search for\r\n        \/\/ the MATLAB code.\r\n        t1='0e740cd7e04e46b2b00a5346897f936c ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' 0e740cd7e04e46b2b00a5346897f936c';\r\n    \r\n        b=document.getElementsByTagName('body')[0];\r\n        i1=b.innerHTML.indexOf(t1)+t1.length;\r\n        i2=b.innerHTML.indexOf(t2);\r\n \r\n        code_string = b.innerHTML.substring(i1, i2);\r\n        code_string = code_string.replace(\/REPLACE_WITH_DASH_DASH\/g,'--');\r\n\r\n        \/\/ Use \/x3C\/g instead of the less-than character to avoid errors \r\n        \/\/ in the XML parser.\r\n        \/\/ Use '\\x26#60;' instead of '<' so that the XML parser\r\n        \/\/ doesn't go ahead and substitute the less-than character. \r\n        code_string = code_string.replace(\/\\x3C\/g, '\\x26#60;');\r\n\r\n        author = 'Loren Shure';\r\n        copyright = 'Copyright 2007 The MathWorks, Inc.';\r\n\r\n        w = window.open();\r\n        d = w.document;\r\n        d.write('<pre>\\n');\r\n        d.write(code_string);\r\n\r\n        \/\/ Add author and copyright lines at the bottom if specified.\r\n        if ((author.length > 0) || (copyright.length > 0)) {\r\n            d.writeln('');\r\n            d.writeln('%%');\r\n            if (author.length > 0) {\r\n                d.writeln('% _' + author + '_');\r\n            }\r\n            if (copyright.length > 0) {\r\n                d.writeln('% _' + copyright + '_');\r\n            }\r\n        }\r\n\r\n        d.write('<\/pre>\\n');\r\n      \r\n      d.title = title + ' (MATLAB code)';\r\n      d.close();\r\n      }   \r\n      \r\n-->\r\n<\/script><p style=\"text-align: right; font-size: xx-small; font-weight:lighter;   font-style: italic; color: gray\"><br><a href=\"javascript:grabCode_0e740cd7e04e46b2b00a5346897f936c()\"><span style=\"font-size: x-small;        font-style: italic;\">Get \r\n            the MATLAB code \r\n            <noscript>(requires JavaScript)<\/noscript><\/span><\/a><br><br>\r\n      Published with MATLAB&reg; 7.5<br><\/p>\r\n<\/div>\r\n<!--\r\n0e740cd7e04e46b2b00a5346897f936c ##### SOURCE BEGIN #####\r\n%% Controlling Multithreading\r\n% With Release <https:\/\/www.mathworks.com\/products\/matlab\/whatsnew.html R2007b>, \r\n% you now have the ability to set (and get) the maximum number of allowed\r\n% threads.  What does this mean for you?\r\n%% The Skinny on maxNumCompThreads\r\n% Here's some pertinent information about \r\n% <https:\/\/www.mathworks.com\/help\/matlab\/ref\/maxnumcompthreads.html |maxNumCompThreads|>.\r\n%\r\n% * |maxNumCompThreads| allows you to programmatically control the maximum\r\n% number of computational threads that MATLAB uses.\r\n% * Unlike the setting from the preferences panel, this setting does not\r\n% persist between MATLAB sessions.\r\n% * |maxNumCompThreads| controls both the JIT and BLAS, meaning both\r\n% element-wise and matrix operations.  Since the JIT\r\n% doesn't operate at the command line, |maxNumCompThreads| only affects\r\n% element-wise computations performed from M-files.\r\n% * The |'automatic'| setting is designed to take best advantage of the\r\n% hardware the program is currently running on.\r\n% * Not all operations run with the maximum number of computational \r\n% threads.  When MATLAB uses fewer than the maximum, it's avoiding the\r\n% overhead of threading which can dominate certain operations, for example,\r\n% matrix multiplication for 2x2 matrices.\r\n% * Currently, only a subset of functions that could benefit from\r\n% multithreading are taking advantage of it.  We will expand this list with\r\n% each upcoming release.\r\n% \r\n%% When to Use maxNumCompThreads\r\n% The main message here is that you should not generally need to use this\r\n% function at all!  Why?  Because we'd like to make MATLAB do the best job\r\n% possible for you.  However, there are some situations where the use of\r\n% |maxNumCompThreads| is merited.\r\n%\r\n% * Benchmarking - This needs to be done with care. For example, as I \r\n% mentioned earlier, matrix multiplication doesn't use multiple threads for\r\n% 2x2 matrices since the thread overhead dominates the numerical work\r\n% that's being done.  Be sure to test a variety of sizes to characterize\r\n% your results.\r\n% * If you notice some slowdown in code that was previously running faster,\r\n% you can programmatically reduce |maxNumCompThreads| and then\r\n% restore the |maxNumCompThreads| value when the calculation is finished.\r\n%% If You Find a Performance Issue...\r\n% Please let us know if you encounter a situation in which setting\r\n% |maxNumCompThreads| manually is necessary for better performance, by\r\n% contacting\r\n% <https:\/\/www.mathworks.com\/support\/contact_us\/index.html Technical Support>.\r\n% It is our intention that you should not need to use\r\n% |maxNumCompThreads| and it will help us to improve the code if you notify\r\n% us of such situations.\r\n##### SOURCE END ##### 0e740cd7e04e46b2b00a5346897f936c\r\n-->","protected":false},"excerpt":{"rendered":"<p>\r\n   \r\n      With Release R2007b, you now have the ability to set (and get) the maximum number of allowed threads.  What does this mean for you?\r\n      \r\n   \r\n   Contents\r\n   \r\n... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/loren\/2007\/09\/12\/controlling-multithreading\/\">read more >><\/a><\/p>","protected":false},"author":39,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[16,6],"tags":[],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts\/107"}],"collection":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/users\/39"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/comments?post=107"}],"version-history":[{"count":0,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts\/107\/revisions"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/media?parent=107"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/categories?post=107"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/tags?post=107"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}