{"id":6525,"date":"2016-01-08T10:32:13","date_gmt":"2016-01-08T15:32:13","guid":{"rendered":"https:\/\/blogs.mathworks.com\/pick\/?p=6525"},"modified":"2016-05-17T21:17:46","modified_gmt":"2016-05-18T01:17:46","slug":"matlab-parallel-cloud","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/pick\/2016\/01\/08\/matlab-parallel-cloud\/","title":{"rendered":"MATLAB Parallel Cloud"},"content":{"rendered":"<div class=\"content\">\r\n\r\n<a href=\"https:\/\/www.mathworks.com\/matlabcentral\/profile\/authors\/3208495\">Sean<\/a>'s pick this week is MATLAB Parallel Cloud by <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/profile\/authors\/4855226\">MathWorks Parallel Computing Toolbox Team<\/a>.\r\n\r\n&nbsp;\r\n\r\n<em>Note: With the release of R2016a on March 3rd 2016, MATLAB Parallel Cloud has been removed from the File Exchange and is now part of the Parallel Computing Toolbox product.\u00a0\u00a0For more information, please visit the product page: <a href=\"https:\/\/www.mathworks.com\/products\/parallel-computing\/matlab-parallel-cloud\/\">https:\/\/www.mathworks.com\/products\/parallel-computing\/matlab-parallel-cloud\/<\/a>.<\/em>\r\n<h3>Background<a name=\"1\"><\/a><\/h3>\r\nHave you ever wanted borrow a more powerful computer to finish a certain task or to see how well your code scales? If you\r\nhave, but do not want to deal with moving your files to another computer or don't have access to another computer with MATLAB,\r\nthen MATLAB Parallel Cloud is a good option. For just a few dollars per hour, it lets you have access to 16 MATLAB parallel workers\r\n(headless MATLAB engines) on 16 physical cores on a machine running on Amazon EC2. My laptop, in comparison, has only a\r\nwimpy two cores.\r\n\r\nMATLAB Parallel Cloud only allows for interactive use of the workers, so you can use <tt>parfor<\/tt>, <tt>parfeval<\/tt> and <tt>spmd<\/tt> just like always. It will not work with <tt>batch<\/tt> for totally offloading your work like you could do with the <a href=\"https:\/\/www.mathworks.com\/products\/parallel-computing\/parallel-computing-on-the-cloud\/index.html\">Distributed Computing Server<\/a>.\r\n\r\nIt's also nice that I don't need to be on my corporate network to use it.\r\n\r\nIf this interests you, you need to be running R2015a or R2015b, be located in North America, and have the <a href=\"https:\/\/www.mathworks.com\/products\/parallel-computing\/\">Parallel Computing Toolbox<\/a> installed. Then just download the installer from the File Exchange. Installing requires a credit card which will be billed\r\nfor every hour of usage.\r\n\r\nOnce you download it, run <tt>setupParallelCloud<\/tt> and then test the connection:\r\n\r\n<img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/pick\/Sean\/mainparallelcloud\/parallelcloudsetup.png\" alt=\"\" hspace=\"5\" vspace=\"5\" \/>\r\n\r\nNow to use it, you just have to switch your default parallel cluster to <b>MATLAB Parallel Cloud<\/b>.\r\n\r\n<img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/pick\/Sean\/mainparallelcloud\/parallelprofile.png\" alt=\"\" hspace=\"5\" vspace=\"5\" \/>\r\n\r\n<b>CAUTION<\/b> As soon as you start the pool, you'll be billed for an hour and again for the next hour 60 minutes later. If you don't think\r\nyou'll need it for more than an hour, it would be wise to set a pool idle timeout in parallel preferences to make sure you're\r\nnot paying for unused time. I'd also be sure to always set the default pool back to <b>Local<\/b> when you're done.\r\n<h3>Example<a name=\"2\"><\/a><\/h3>\r\nFor my example, I'm going to run a nonlinear integer optimization to minimize production cost for a semiconductor manufacturing\r\nplant. The model is made in <a href=\"https:\/\/www.mathworks.com\/products\/simevents\/\">SimEvents<\/a>, MathWork's discrete event simulation tool that sits on Simulink.\r\n\r\nHere's a peek at the model:\r\n\r\n<img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/pick\/Sean\/mainparallelcloud\/semimanufacturing.png\" alt=\"\" hspace=\"5\" vspace=\"5\" \/>\r\n\r\nSince I'm working with a Simulink model, I need to load it into each worker's workspace. This is done with <tt>spmd<\/tt>, which runs the same command on each worker:\r\n<pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid #c8c8c8;\"><span style=\"color: #228b22;\">% Open the pool if there is none and supply it with the model<\/span>\r\np = gcp;\r\np.addAttachedFiles(<span style=\"color: #a020f0;\">'SemiconductorManufacturing.slx'<\/span>)\r\n\r\n<span style=\"color: #0000ff;\">spmd<\/span>\r\n    load_system(<span style=\"color: #a020f0;\">'SemiconductorManufacturing.slx'<\/span>);\r\n<span style=\"color: #0000ff;\">end<\/span><\/pre>\r\nNow I need to set up the optimization problem. I'll use <a href=\"https:\/\/www.mathworks.com\/help\/gads\/ga.html\"><tt>ga<\/tt><\/a>, the genetic algorithm optimizer in the Global Optimization Toolbox, since it can handle integer constraints. Here we will\r\nbe optimizing the time between starting new lots, the number of human operators, and how many lots can be held at once.\r\n<pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid #c8c8c8;\"><span style=\"color: #228b22;\">% Options, the important one is  <a href=\"https:\/\/www.mathworks.com\/products\/parallel-computing\/parallel-support.html\">'UseParallel'<\/a> which will evaluate the<\/span>\r\n<span style=\"color: #228b22;\">% individuals in parallel on the available workers.<\/span>\r\nopts = gaoptimset(<span style=\"color: #a020f0;\">'PlotFcns'<\/span>,{@gaplotbestf; @gaplotbestindiv},<span style=\"color: #0000ff;\">...<\/span>\r\n                  <span style=\"color: #a020f0;\">'Generations'<\/span>,20, <span style=\"color: #a020f0;\">'StallGenLimit'<\/span>, 10, <span style=\"color: #0000ff;\">...<\/span>\r\n                  <span style=\"color: #a020f0;\">'PopulationSize'<\/span>,25, <span style=\"color: #a020f0;\">'EliteCount'<\/span>,1, <span style=\"color: #0000ff;\">...<\/span>\r\n                  <span style=\"color: #a020f0;\">'UseParallel'<\/span>, true);\r\n\r\n<span style=\"color: #228b22;\">% Lower and upper bounds on variables.<\/span>\r\n<span style=\"color: #228b22;\">% All variables must be integers<\/span>\r\nlb = [50 1 10];\r\nub = [1000 5 3000];\r\nIntCon = [1 2 3];\r\n\r\n<span style=\"color: #228b22;\">% Execute genetic algorithm solver.  productionCcost calculates the final<\/span>\r\n<span style=\"color: #228b22;\">% production cost after running the model with the given parameters.<\/span>\r\nfinalResult = ga(@productionCost,3,[],[],[],[],lb,ub,[],IntCon,opts);\r\n\r\n<span style=\"color: #228b22;\">% Report results<\/span>\r\ndisp(<span style=\"color: #a020f0;\">'Results'<\/span>)\r\ndisp(<span style=\"color: #a020f0;\">'Time Between Lots'<\/span>)\r\ndisp(finalResult(1))\r\ndisp(<span style=\"color: #a020f0;\">'Number of Operators'<\/span>)\r\ndisp(finalResult(2))\r\ndisp(<span style=\"color: #a020f0;\">'Holding for Lots'<\/span>)\r\ndisp(finalResult(3))<\/pre>\r\n<pre style=\"font-style: oblique;\">Optimization terminated: maximum number of generations exceeded.\r\nResults\r\nTime Between Lots\r\n    84\r\n\r\nNumber of Operators\r\n     1\r\n\r\nHolding for Lots\r\n    13\r\n\r\n<\/pre>\r\n<img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/pick\/Sean\/mainparallelcloud\/mainparallelcloud_01.png\" alt=\"\" hspace=\"5\" vspace=\"5\" \/>\r\n\r\nI'm seeing a 10x speedup for the optimization over running it locally in serial.\r\n<h3>Comments<a name=\"6\"><\/a><\/h3>\r\nDo you need to scale up your analysis? If so, what kind of speed-ups or size scaling are you looking for?\r\n\r\nGive it a try and let us know what you think.\r\n\r\n<script>\/\/ <![CDATA[\r\nfunction grabCode_703c972f82024b179e7c42cc7d13de40() {\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='703c972f82024b179e7c42cc7d13de40 ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' 703c972f82024b179e7c42cc7d13de40';\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 2016 The MathWorks, Inc.';\r\n\r\n        w = window.open();\r\n        d = w.document;\r\n        d.write('\r\n\r\n\r\n\r\n<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>\r\n\r\n\r\n\r\n\r\n\\n');\r\n      \r\n      d.title = title + ' (MATLAB code)';\r\n      d.close();\r\n      }\r\n\/\/ ]]><\/script>\r\n<p style=\"text-align: right; font-size: xx-small; font-weight: lighter; font-style: italic; color: gray;\"><a><span style=\"font-size: x-small; font-style: italic;\">Get\r\nthe MATLAB code\r\n<noscript>(requires JavaScript)<\/noscript><\/span><\/a><\/p>\r\nPublished with MATLAB\u00ae R2015b\r\n\r\n<\/div>\r\n<!--\r\n703c972f82024b179e7c42cc7d13de40 ##### SOURCE BEGIN #####\r\n%% MATLAB Parallel Cloud\r\n%\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/profile\/authors\/3208495 Sean>'s\r\n% pick this week is\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/52403 MATLAB % Parallel Cloud> by\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/profile\/authors\/4855226 MathWorks % Parallel Computing Toolbox Team>.\r\n%\r\n\r\n%% Background\r\n% Have you ever wanted borrow a more powerful computer to finish a certain\r\n% task or to see how well your code scales?  If you have, but do not want\r\n% to deal with moving your files to another computer or don't have access\r\n% to another computer with MATLAB, then Parallel Cloud is a good option.\r\n% For just a few dollars per hour, it lets you have access to 16 MATLAB\r\n% parallel workers (headless MATLAB engines) on 16 physical cores on a\r\n% machine running on Amazon EC2.  My laptop, in comparision, has only a\r\n% wimpy two cores.\r\n%\r\n% The parallel cloud only allows for interactive use of the workers, so you\r\n% can use |parfor|, |parfeval| and |spmd| just like always.  It will not\r\n% work with |batch| for totally offloading your work like you could do with\r\n% the\r\n% <https:\/\/www.mathworks.com\/products\/parallel-computing\/parallel-computing-on-the-cloud\/index.html % Distributed Computing Server>.\r\n%\r\n% It's also nice that I don't need to be on my corporate network to use it.\r\n%\r\n% If this interests you, you need to be running R2015a or R2015b, be\r\n% located in North America, and have the\r\n% <https:\/\/www.mathworks.com\/products\/parallel-computing\/ Parallel Computing % Toolbox> installed.  Then just download the installer from the File\r\n% Exchange.  Installing requires a credit card which will be billed for\r\n% every hour of usage.\r\n%\r\n% Once you download it, run |setupParallelCloud| and then test the\r\n% connection:\r\n%\r\n% <<parallelcloudsetup.png>>\r\n%\r\n% Now to use it, you just have to switch your default parallel cluster to\r\n% *MATLAB Parallel Cloud*.\r\n%\r\n% <<parallelprofile.png>>\r\n%\r\n% *CAUTION* As soon as you start the pool, you'll be billed for an hour and\r\n% again for the next hour 60 minutes later. If you don't think you'll need\r\n% it for more than an hour, it would be wise to set a pool idle timeout in\r\n% parallel preferences to make sure you're not paying for unused time.  I'd\r\n% also always be sure to always set the default pool back to *Local* when\r\n% you're done.\r\n\r\n%% Example\r\n% For my example, I'm going to run a nonlinear integer optimization to\r\n% minimize production cost for a semiconductor manufacturing plant.  The\r\n% model is made in <https:\/\/www.mathworks.com\/products\/simevents\/ % SimEvents>, MathWork's discrete event simulation tool that sits on\r\n% Simulink.\r\n%\r\n% Here's a peek at the model:\r\n%\r\n% <<semimanufacturing.png>>\r\n%\r\n\r\n%%\r\n% Since I'm working with a Simulink model, I need to load it into each\r\n% worker's workspace.  This is done with |spmd|, which runs the same\r\n% command on each worker:\r\n\r\n% Open the pool if there is none and supply it with the model\r\np = gcp;\r\np.addAttachedFiles('SemiconductorManufacturing.slx')\r\n\r\nspmd\r\nload_system('SemiconductorManufacturing.slx');\r\nend\r\n\r\n%%\r\n% Now I need to set up the optimization problem.  I'll use\r\n% <https:\/\/www.mathworks.com\/help\/gads\/ga.html |ga|>, the\r\n% genetic algorithm optimizer in the Global Optimization Toolbox, since it\r\n% can handle integer constraints.  Here we will be optimizing the time\r\n% between starting new lots, the number of human operators, and how many\r\n% lots can be held at once.\r\n\r\n% Options, the important one is 'UseParallel' which will evaluate the\r\n% individuals in parallel on the available workers.\r\nopts = gaoptimset('PlotFcns',{@gaplotbestf; @gaplotbestindiv},...\r\n'Generations',20, 'StallGenLimit', 10, ...\r\n'PopulationSize',25, 'EliteCount',1, ...\r\n'UseParallel', true);\r\n\r\n% Lower and upper bounds on variables.\r\n% All variables must be integers\r\nlb = [50 1 10];\r\nub = [1000 5 3000];\r\nIntCon = [1 2 3];\r\n\r\n% Execute genetic algorithm solver.  productionCcost calculates the final\r\n% production cost after running the model with the given parameters.\r\nfinalResult = ga(@productionCost,3,[],[],[],[],lb,ub,[],IntCon,opts);\r\n\r\n% Report results\r\ndisp('Results')\r\ndisp('Time Between Lots')\r\ndisp(finalResult(1))\r\ndisp('Number of Operators')\r\ndisp(finalResult(2))\r\ndisp('Holding for Lots')\r\ndisp(finalResult(3))\r\n\r\n%%\r\n% I'm seeing an 10x speedup for the optimization over running it locally\r\n% in serial.\r\n\r\n%% Comments\r\n%\r\n% Do you need to scale up your analysis?  If so, what kind of speed-ups or\r\n% size scaling are you looking for?\r\n%\r\n% Give it a try and let us know what you think\r\n% <https:\/\/blogs.mathworks.com\/pick\/?p=52403#respond here> or leave a\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/6525#comments % comment> for the Parallel Computing Team.\r\n%\r\n\r\n##### SOURCE END ##### 703c972f82024b179e7c42cc7d13de40\r\n-->","protected":false},"excerpt":{"rendered":"<div class=\"overview-image\"><img decoding=\"async\"  class=\"img-responsive\" src=\"https:\/\/blogs.mathworks.com\/images\/pick\/Sean\/mainparallelcloud\/parallelcloudsetup.png\" onError=\"this.style.display ='none';\" \/><\/div><p>\r\n\r\nSean's pick this week is MATLAB Parallel Cloud by MathWorks Parallel Computing Toolbox Team.\r\n\r\n&nbsp;\r\n\r\nNote: With the release of R2016a on March 3rd 2016, MATLAB Parallel Cloud has been... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/pick\/2016\/01\/08\/matlab-parallel-cloud\/\">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\/6525"}],"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=6525"}],"version-history":[{"count":21,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/6525\/revisions"}],"predecessor-version":[{"id":7275,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/6525\/revisions\/7275"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/media?parent=6525"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/categories?post=6525"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/tags?post=6525"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}