{"id":4870,"date":"2013-03-10T10:24:50","date_gmt":"2013-03-10T09:24:50","guid":{"rendered":"http:\/\/www.walkingrandomly.com\/?p=4870"},"modified":"2013-04-03T11:46:30","modified_gmt":"2013-04-03T10:46:30","slug":"faster-gpu-random-number-generators-in-matlab-2012b","status":"publish","type":"post","link":"https:\/\/walkingrandomly.com\/?p=4870","title":{"rendered":"Faster GPU Random Number Generators in MATLAB 2012b"},"content":{"rendered":"<p>Ever since I took a look at <a href=\"https:\/\/www.walkingrandomly.com\/?p=3978\">GPU accelerating simple Monte Carlo Simulations<\/a> using MATLAB, I&#8217;ve been disappointed with the performance of its GPU random number generator. In MATLAB 2012a, for example, it&#8217;s not much faster than the CPU implementation on my GPU hardware.\u00a0 Consider the following code<\/p>\n<pre>function gpuRandTest2012a(n)\r\n\r\nmydev=gpuDevice();\r\ndisp('CPU - Mersenne Twister');\r\ntic\r\nCPU = rand(n);\r\ntoc\r\n\r\nsg = parallel.gpu.RandStream('mrg32k3a','Seed',1);\r\nparallel.gpu.RandStream.setGlobalStream(sg);\r\ndisp('GPU - mrg32k3a');\r\ntic\r\nRg = parallel.gpu.GPUArray.rand(n);\r\nwait(mydev);\r\ntoc<\/pre>\n<p>Running this on MATLAB 2012a on my laptop gives me the following typical times (If you try this out yourself, the first run will always be slower for various reasons I&#8217;ll not go into here)<\/p>\n<pre>&gt;&gt; gpuRandTest2012a(10000)\r\nCPU - Mersenne Twister\r\nElapsed time is 1.330505 seconds.\r\nGPU - mrg32k3a\r\nElapsed time is 1.006842 seconds.<\/pre>\n<p>Running the same code on MATLAB 2012b, however, gives a very pleasant surprise with typical run times looking like this<\/p>\n<pre>CPU - Mersenne Twister\r\nElapsed time is 1.590764 seconds.\r\nGPU - mrg32k3a\r\nElapsed time is 0.185686 seconds.<\/pre>\n<p>So, generation of random numbers using the GPU is now over 7 times faster than CPU generation on my laptop hardware&#8211;a significant improvment on the previous implementation.<\/p>\n<p><strong>New generators in 2012b<\/strong><\/p>\n<p>The MATLAB developers went a little further in 2012b though.\u00a0 Not only have they significantly improved performance of the mrg32k3a combined multiple recursive generator, they have also implemented two new GPU random number generators based on the <a href=\"http:\/\/www.deshawresearch.com\/resources_random123.html\">Random123 library<\/a>.\u00a0 Here are the timings for the generation of 100 million random numbers in MATLAB 2012b<\/p>\n<ul>\n<li>Get the code &#8211; <a href=\"https:\/\/www.walkingrandomly.com\/images\/matlab\/gpu\/gpuRandTest2012b.m\">gpuRandTest2012b.m<\/a><\/li>\n<\/ul>\n<pre>CPU - Mersenne Twister\r\nElapsed time is 1.370252 seconds.\r\nGPU - mrg32k3a\r\nElapsed time is 0.186152 seconds.\r\nGPU - Threefry4x64-20\r\nElapsed time is 0.145144 seconds.\r\nGPU - Philox4x32-10\r\nElapsed time is 0.129030 seconds.<\/pre>\n<p>Bear in mind that I am running this on the relatively weak GPU of my laptop!\u00a0 If anyone runs it on something stronger, I&#8217;d love to hear of your results.<\/p>\n<ul>\n<li>Laptop model: Dell XPS L702X<\/li>\n<li>CPU:<a href=\"http:\/\/www.notebookcheck.net\/Intel-Core-i7-2630QM-Notebook-Processor.41483.0.html\"> Intel Core i7-2630QM<\/a> @2Ghz software overclockable to 2.9Ghz. 4 physical cores but total 8 virtual cores due to Hyperthreading.<\/li>\n<li>GPU: <a href=\"http:\/\/www.notebookcheck.net\/NVIDIA-GeForce-GT-555M.41933.0.html\">GeForce GT 555M<\/a> with 144 CUDA Cores.\u00a0 Graphics clock: 590Mhz.\u00a0 Processor Clock:1180 Mhz. 3072 Mb DDR3 Memeory<\/li>\n<li>RAM: 8 Gb<\/li>\n<li>OS: Windows 7 Home Premium 64 bit.<\/li>\n<li>MATLAB: 2012a\/2012b<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Ever since I took a look at GPU accelerating simple Monte Carlo Simulations using MATLAB, I&#8217;ve been disappointed with the performance of its GPU random number generator. In MATLAB 2012a, for example, it&#8217;s not much faster than the CPU implementation on my GPU hardware.\u00a0 Consider the following code function gpuRandTest2012a(n) mydev=gpuDevice(); disp(&#8216;CPU &#8211; Mersenne Twister&#8217;); [&hellip;]<\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[44,51,53,11,63],"tags":[],"class_list":["post-4870","post","type-post","status-publish","format-standard","hentry","category-cuda","category-gpu","category-making-matlab-faster","category-matlab","category-random-numbers"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p3swhs-1gy","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=\/wp\/v2\/posts\/4870","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=4870"}],"version-history":[{"count":7,"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=\/wp\/v2\/posts\/4870\/revisions"}],"predecessor-version":[{"id":4894,"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=\/wp\/v2\/posts\/4870\/revisions\/4894"}],"wp:attachment":[{"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=4870"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=4870"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=4870"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}