{"id":5458,"date":"2014-08-01T09:00:23","date_gmt":"2014-08-01T13:00:23","guid":{"rendered":"https:\/\/blogs.mathworks.com\/pick\/?p=5458"},"modified":"2014-07-31T20:02:13","modified_gmt":"2014-08-01T00:02:13","slug":"time-parallel-for-parfor-loops","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/pick\/2014\/08\/01\/time-parallel-for-parfor-loops\/","title":{"rendered":"Time Parallel FOR (PARFOR) Loops"},"content":{"rendered":"<div xmlns:mwsh=\"https:\/\/www.mathworks.com\/namespace\/mcode\/v1\/syntaxhighlight.dtd\" class=\"content\">\r\n   <introduction>\r\n      <p><a href=\"https:\/\/www.mathworks.com\/matlabcentral\/answers\/contributors\/3208495\">Sean<\/a>'s pick this week is <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/27472-partictoc\">Par Tic Toc<\/a> by <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/authors\/32376\">Sarah Wait Zaranek<\/a>.\r\n      <\/p>\r\n   <\/introduction>\r\n\r\n   <p>I run a fair number of computationally expensive operations and often use <a href=\"\">parallel for (parfor)<\/a> loops to speed them up.  This utility interested me mainly because it shows how work is distributed across the workers and\r\n      provides worker utilization rates.\r\n   <\/p>\r\n   <p>First, I will make sure a parallel pool is open using <a href=\"\"><tt>gcp()<\/tt><\/a> or \"Get Current Pool\".  This opens the headless MATLAB processes that a parfor loop distributes iterations to.\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">pool = gcp<\/pre><pre style=\"font-style:oblique\">\r\npool = \r\n\r\n Pool with properties: \r\n\r\n            Connected: true\r\n           NumWorkers: 2\r\n              Cluster: local\r\n        AttachedFiles: {}\r\n          IdleTimeout: 90 minute(s) (89 minutes remaining)\r\n          SpmdEnabled: true\r\n\r\n<\/pre><p>We'll do a simple example with a loop that has a random pause.  The pause is a proxy for an operation that takes a varying\r\n      and unpredicable amount of time.\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">n = 50;\r\np = Par(n); <span style=\"color: #228B22\">% Build the Par timer<\/span>\r\n<span style=\"color: #0000FF\">parfor<\/span> ii = 1:n\r\n    Par.tic; <span style=\"color: #228B22\">% start timer<\/span>\r\n    pause(randi(5)) <span style=\"color: #228B22\">% Pause for up to five seconds<\/span>\r\n    p(ii) = Par.toc; <span style=\"color: #228B22\">% measure elapsed time<\/span>\r\n<span style=\"color: #0000FF\">end<\/span>\r\nstop(p); <span style=\"color: #228B22\">% all done<\/span>\r\nplot(p);<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/pick\/Sean\/mainpartictoc\/mainpartictoc_01.png\"> <p>The parfor loop has done a good job distributing the calculations between the two workers.  The more highly utilized worker\r\n      only has one additional iteration that the other.\r\n   <\/p>\r\n   <p>There are a few other neat tricks in here as well that I found from looking through the code.<\/p>\r\n   <div>\r\n      <ol>\r\n         <li>If you right click on the axes with each iteration's time, it gives you context menu to select the scale.<\/li>\r\n         <li>Since the Par class inherits from handle, you get those visible methods.  Sarah hides those from <a href=\"\"><tt>methods()<\/tt><\/a> by making their signature hidden.\r\n         <\/li>\r\n         <li>Sarah has also provided a full set of documentation on the capabilities of this utiltiy as well as examples.<\/li>\r\n      <\/ol>\r\n   <\/div>\r\n   <h3>Comments<a name=\"6\"><\/a><\/h3>\r\n   <p>Give it a try and let us know what you think <a href=\"https:\/\/blogs.mathworks.com\/pick\/?p=5458#respond\">here<\/a> or leave a <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/27472-partictoc#comments\">comment<\/a> for Sarah.\r\n   <\/p><script language=\"JavaScript\">\r\n<!--\r\n\r\n    function grabCode_22fe8fcbb29e40e0bef440a057251af3() {\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='22fe8fcbb29e40e0bef440a057251af3 ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' 22fe8fcbb29e40e0bef440a057251af3';\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 = 'Sean de Wolski';\r\n        copyright = 'Copyright 2014 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_22fe8fcbb29e40e0bef440a057251af3()\"><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; R2014a<br><\/p>\r\n<\/div>\r\n<!--\r\n22fe8fcbb29e40e0bef440a057251af3 ##### SOURCE BEGIN #####\r\n%% Time Parallel FOR (PARFOR) Loops\r\n%\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/answers\/contributors\/3208495 Sean>'s pick this week is\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/27472-partictoc Par Tic Toc> by\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/authors\/32376 Sarah Wait Zaranek>.\r\n% \r\n\r\n%% \r\n% I run a fair number of computationally expensive operations and often use\r\n% < parallel for (parfor)> loops to speed them up.  This utility interested me\r\n% mainly because it shows how work is distributed across the workers and\r\n% provides worker utilization rates.\r\n\r\n%%\r\n% First, I will make sure a parallel pool is open using\r\n% < |gcp()|>\r\n% or \"Get Current Pool\".  This opens the headless MATLAB processes that a\r\n% parfor loop distributes iterations to.\r\npool = gcp\r\n\r\n\r\n%%\r\n% We'll do a simple example with a loop that has a random pause.  The pause\r\n% is a proxy for an operation that takes a varying and unpredicable amount\r\n% of time.\r\n\r\nn = 50;\r\np = Par(n); % Build the Par timer\r\nparfor ii = 1:n\r\n    Par.tic; % start timer\r\n    pause(randi(5)) % Pause for up to five seconds\r\n    p(ii) = Par.toc; % measure elapsed time\r\nend\r\nstop(p); % all done\r\nplot(p);\r\n\r\n%%\r\n% The parfor loop has done a good job distributing the calculations between\r\n% the two workers.  The more highly utilized worker only has one additional\r\n% iteration that the other.\r\n\r\n%% \r\n% There are a few other neat tricks in here as well that I found from\r\n% looking through the code.\r\n%   \r\n% # If you right click on the axes with each iteration's time, it\r\n% gives you context menu to select the scale. \r\n% # Since the Par class inherits from handle, you get those visible\r\n% methods.  Sarah hides those from\r\n% <\r\n% |methods()|> by making their signature hidden.\r\n% # Sarah has also provided a full set of documentation on the capabilities\r\n% of this utiltiy as well as examples.\r\n%\r\n\r\n\r\n%% Comments\r\n% \r\n% Give it a try and let us know what you think\r\n% <https:\/\/blogs.mathworks.com\/pick\/?p=5458#respond here> or leave a\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/27472-partictoc#comments\r\n% comment> for Sarah.\r\n%\r\n \r\n\r\n##### SOURCE END ##### 22fe8fcbb29e40e0bef440a057251af3\r\n-->","protected":false},"excerpt":{"rendered":"<div class=\"overview-image\"><img decoding=\"async\"  class=\"img-responsive\" src=\"https:\/\/blogs.mathworks.com\/images\/pick\/Sean\/mainpartictoc\/mainpartictoc_01.png\" onError=\"this.style.display ='none';\" \/><\/div><p>\r\n   \r\n      Sean's pick this week is Par Tic Toc by Sarah Wait Zaranek.\r\n      \r\n   \r\n\r\n   I run a fair number of computationally expensive operations and often use parallel for (parfor) loops to... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/pick\/2014\/08\/01\/time-parallel-for-parfor-loops\/\">read more >><\/a><\/p>","protected":false},"author":87,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[16],"tags":[],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/5458"}],"collection":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/users\/87"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/comments?post=5458"}],"version-history":[{"count":7,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/5458\/revisions"}],"predecessor-version":[{"id":5466,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/5458\/revisions\/5466"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/media?parent=5458"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/categories?post=5458"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/tags?post=5458"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}