{"id":6518,"date":"2016-01-29T09:00:35","date_gmt":"2016-01-29T14:00:35","guid":{"rendered":"https:\/\/blogs.mathworks.com\/pick\/?p=6518"},"modified":"2016-01-07T17:19:39","modified_gmt":"2016-01-07T22:19:39","slug":"2d-wave-equation","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/pick\/2016\/01\/29\/2d-wave-equation\/","title":{"rendered":"2D Wave Equation"},"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\/profile\/authors\/3208495\">Sean<\/a>'s pick this week is <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/41080-2d-wave-equation\">2D Wave Equation<\/a> by <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/profile\/authors\/870624\">Daniel Armyr<\/a>.\r\n      <\/p>\r\n   <\/introduction>   \r\n   <h3>GPUs<a name=\"1\"><\/a><\/h3>\r\n   <p>MATLAB's <a href=\"https:\/\/www.mathworks.com\/products\/parallel-computing\/\">Parallel Computing Toolbox<\/a> has direct support for Graphics Processing Units (GPUs or GPGPUs) for many different computations.  Since most laptops, with\r\n      the exception of high-end gaming ones, don't have powerful enough GPUs for scientific computing, I usually don't get to take\r\n      advantage of this.\r\n   <\/p>\r\n   <p>However, I've been lucky enough over the last month or so to have access to a fancy portable desktop with a <a href=\"http:\/\/www.nvidia.com\/object\/tesla-workstations.html\">Tesla K40 GPU<\/a> in it.  Daniel's app allows me to make waves and then compute the propagation on the cpu or gpu while measuring the difference\r\n      in computational speed.  (I switch to the GPU at 28s)\r\n   <\/p>\r\n   <p>\r\n      <object id=\"csSWF\" classid=\"clsid:d27cdb6e-ae6d-11cf-96b8-444553540000\" width=\"1000\" height=\"520\" codebase=\"http:\/\/download.macromedia.com\/pub\/shockwave\/cabs\/flash\/swflash.cab#version=9,0,115,0\">\r\n      <param name=\"src\" value=\"https:\/\/blogs.mathworks.com\/images\/pick\/Sean\/main2dwave\/Using2dWave.mp4\">\r\n      <param name=\"bgcolor\" value=\"#1a1a1a\">\r\n      <param name=\"quality\" value=\"best\">\r\n      <param name=\"allowScriptAccess\" value=\"always\">\r\n      <param name=\"allowFullScreen\" value=\"true\">\r\n      <param name=\"scale\" value=\"showall\">\r\n      <param name=\"flashVars\" value=\"autostart=false#&amp;thumb=FirstFrame.png&amp;thumbscale=45&amp;color=0x1A1A1A,0x1A1A1A\">\r\n      <embed name=\"csSWF\" src=\"https:\/\/blogs.mathworks.com\/images\/pick\/Sean\/main2dwave\/Using2dWave.mp4\" width=\"638\" height=\"480\" bgcolor=\"#1a1a1a\" quality=\"best\" allowscriptaccess=\"always\"\r\n      allowfullscreen=\"true\" scale=\"showall\" flashvars=\"autostart=false&amp;thumb=FirstFrame.png&amp;thumbscale=45&amp;color=0x1A1A1A,0x1A1A1A\"\r\n      pluginspage=\"http:\/\/www.macromedia.com\/shockwave\/download\/index.cgi?P1_Prod_Version=ShockwaveFlash\">\r\n      <\/object>\r\n      \r\n   <\/p>\r\n   <p>What did it take to switch over to the GPU from the CPU?  Here's the <a href=\"https:\/\/www.mathworks.com\/help\/matlab\/matlab_env\/comparing-files-and-folders.html\">code comparison<\/a>:\r\n   <\/p>\r\n   <p><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/pick\/Sean\/main2dwave\/codecomp.png\"> <\/p>\r\n   <p>Just the commands <tt>gpuArray<\/tt> and <tt>gather<\/tt> to move the arrays back and forth between the CPU and GPU.  There are quite a few commands that run directly on the GPU,\r\n      you can find them <a href=\"https:\/\/www.mathworks.com\/help\/distcomp\/run-built-in-functions-on-a-gpu.html\">here<\/a>.  This list grows bigger with every release of MATLAB.  My personal favorite is <tt>iradon<\/tt> which was a big bottleneck in my previous life working with CT images.\r\n   <\/p>\r\n   <p>One thing I'd like to see Daniel do differently would be to check if a GPU is available.  If you don't have a supported GPU,\r\n      the app errors rather than failing gracefully or just disabling the GPU option.  There's a convenient function, <tt>gpuDeviceCount<\/tt>, that you can use to see how many supported GPUs are on the machine.\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\"><span style=\"color: #0000FF\">if<\/span> gpuDeviceCount == 0\r\n    warndlg(sprintf(<span style=\"color: #A020F0\">'You don''t have a supported GPU!\\nGo talk to your IT department :)'<\/span>),<span style=\"color: #A020F0\">'No GPU Found'<\/span>)\r\n<span style=\"color: #0000FF\">end<\/span><\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/pick\/Sean\/main2dwave\/main2dwave_01.png\"> <h3>Comments<a name=\"4\"><\/a><\/h3>\r\n   <p>Do you use a GPU with MATLAB or have interest in trying one?<\/p>\r\n   <p>Give it a try and let us know what you think <a href=\"https:\/\/blogs.mathworks.com\/pick\/?p=6518#respond\">here<\/a> or leave a <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/41080-2d-wave-equation#comments\">comment<\/a> for Daniel.\r\n   <\/p><script language=\"JavaScript\">\r\n<!--\r\n\r\n    function grabCode_361363c424ea4bdd88df8cb3ca65bd3c() {\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='361363c424ea4bdd88df8cb3ca65bd3c ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' 361363c424ea4bdd88df8cb3ca65bd3c';\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('<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_361363c424ea4bdd88df8cb3ca65bd3c()\"><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; R2015b<br><\/p>\r\n<\/div>\r\n<!--\r\n361363c424ea4bdd88df8cb3ca65bd3c ##### SOURCE BEGIN #####\r\n%% 2D Wave Equation\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\/41080-2d-wave-equation 2D Wave\r\n% Equation> by\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/profile\/authors\/870624 Daniel\r\n% Armyr>.\r\n% \r\n\r\n%% GPUs\r\n%\r\n% MATLAB's <https:\/\/www.mathworks.com\/products\/parallel-computing\/ Parallel\r\n% Computing Toolbox> has direct support for Graphics Processing Units (GPUs\r\n% or GPGPUs) for many different computations.  Since most laptops, with the\r\n% exception of high-end gaming ones, don't have powerful enough GPUs for\r\n% scientific computing, I usually don't get to take advantage of this.\r\n%\r\n% However, I've been lucky enough over the last month or so to have access\r\n% to a fancy portable desktop with a\r\n% <http:\/\/www.nvidia.com\/object\/tesla-workstations.html Tesla K40 GPU> in\r\n% it.  Daniel's app allows me to make waves and then compute the\r\n% propagation on the cpu or gpu while measuring the difference in\r\n% computational speed.  (I switch to the GPU at 28s)\r\n%\r\n% <html>\r\n%     <object id=\"csSWF\" classid=\"clsid:d27cdb6e-ae6d-11cf-96b8-444553540000\" width=\"1000\" height=\"520\" codebase=\"http:\/\/download.macromedia.com\/pub\/shockwave\/cabs\/flash\/swflash.cab#version=9,0,115,0\">\r\n%          <param name=\"src\" value=\"Using2dWave.mp4\">\r\n%          <param name=\"bgcolor\" value=\"#1a1a1a\">\r\n%          <param name=\"quality\" value=\"best\">\r\n%          <param name=\"allowScriptAccess\" value=\"always\">\r\n%          <param name=\"allowFullScreen\" value=\"true\">\r\n%          <param name=\"scale\" value=\"showall\">\r\n%          <param name=\"flashVars\" value=\"autostart=false#&amp;thumb=FirstFrame.png&amp;thumbscale=45&amp;color=0x1A1A1A,0x1A1A1A\">\r\n%          <embed name=\"csSWF\" src=\"Using2dWave.mp4\" width=\"638\" height=\"480\" bgcolor=\"#1a1a1a\" quality=\"best\" allowscriptaccess=\"always\" allowfullscreen=\"true\" scale=\"showall\" flashvars=\"autostart=false&amp;thumb=FirstFrame.png&amp;thumbscale=45&amp;color=0x1A1A1A,0x1A1A1A\" pluginspage=\"http:\/\/www.macromedia.com\/shockwave\/download\/index.cgi?P1_Prod_Version=ShockwaveFlash\">\r\n%      <\/object>\r\n% <\/html>\r\n\r\n%%\r\n% What did it take to switch over to the GPU from the CPU?  Here's the\r\n% <https:\/\/www.mathworks.com\/help\/matlab\/matlab_env\/comparing-files-and-folders.html\r\n% code comparison>:\r\n%\r\n% <<codecomp.png>>\r\n%\r\n% Just the commands |gpuArray| and |gather| to move the arrays back and\r\n% forth between the CPU and GPU.  There are quite a few commands that run\r\n% directly on the GPU, you can find them\r\n% <https:\/\/www.mathworks.com\/help\/distcomp\/run-built-in-functions-on-a-gpu.html\r\n% here>.  This list grows bigger with every release of MATLAB.  My personal\r\n% favorite is |iradon| which was a big bottleneck in my previous life\r\n% working with CT images.\r\n\r\n\r\n%%\r\n% One thing I'd like to see Daniel do differently would be to check if a\r\n% GPU is available.  If you don't have a supported GPU, the app errors\r\n% rather than failing gracefully or just disabling the GPU option.  There's\r\n% a convenient function, |gpuDeviceCount|, that you can use to see how many\r\n% supported GPUs are on the machine.\r\n\r\nif gpuDeviceCount == 0\r\n    warndlg(sprintf('You don''t have a supported GPU!\\nGo talk to your IT department :)'),'No GPU Found')\r\nend\r\n\r\n%% Comments\r\n% \r\n% Do you use a GPU with MATLAB or have interest in trying one?\r\n%\r\n% Give it a try and let us know what you think\r\n% <https:\/\/blogs.mathworks.com\/pick\/?p=6518#respond here> or leave a\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/41080-2d-wave-equation#comments\r\n% comment> for Daniel.\r\n%\r\n \r\n\r\n##### SOURCE END ##### 361363c424ea4bdd88df8cb3ca65bd3c\r\n-->","protected":false},"excerpt":{"rendered":"<div class=\"overview-image\"><img decoding=\"async\"  class=\"img-responsive\" src=\"https:\/\/blogs.mathworks.com\/images\/pick\/Sean\/main2dwave\/codecomp.png\" onError=\"this.style.display ='none';\" \/><\/div><p>\r\n   \r\n      Sean's pick this week is 2D Wave Equation by Daniel Armyr.\r\n      \r\n      \r\n   GPUs\r\n   MATLAB's Parallel Computing Toolbox has direct support for Graphics Processing Units (GPUs... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/pick\/2016\/01\/29\/2d-wave-equation\/\">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\/6518"}],"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=6518"}],"version-history":[{"count":1,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/6518\/revisions"}],"predecessor-version":[{"id":6519,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/6518\/revisions\/6519"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/media?parent=6518"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/categories?post=6518"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/tags?post=6518"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}