{"id":10356,"date":"2018-05-08T21:23:24","date_gmt":"2018-05-08T19:23:24","guid":{"rendered":"http:\/\/djalil.chafai.net\/blog\/?p=10356"},"modified":"2018-06-26T06:51:34","modified_gmt":"2018-06-26T04:51:34","slug":"parallel-computing-with-julia","status":"publish","type":"post","link":"https:\/\/djalil.chafai.net\/blog\/2018\/05\/08\/parallel-computing-with-julia\/","title":{"rendered":"Parallel computing with Julia"},"content":{"rendered":"<p><img loading=\"lazy\" class=\"aligncenter wp-image-7004 size-medium\" src=\"http:\/\/djalil.chafai.net\/blog\/wp-content\/uploads\/2014\/04\/julia-300x202.png\" alt=\"The Julia Language\" width=\"300\" height=\"202\" srcset=\"https:\/\/djalil.chafai.net\/blog\/wp-content\/uploads\/2014\/04\/julia-300x202.png 300w, https:\/\/djalil.chafai.net\/blog\/wp-content\/uploads\/2014\/04\/julia-1024x692.png 1024w, https:\/\/djalil.chafai.net\/blog\/wp-content\/uploads\/2014\/04\/julia.png 1260w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/p>\n<p>Julia is particularly attractive for its ability to run in parallel, not only on clusters, but also on the multiple cores of your laptop processor. Here is a minimalistic example using <strong>pmap()<\/strong>.<\/p>\n<pre class=\"prettyprint\"># For parallel computing with julia -p NumberOfAdditionalCPU\r\n\r\n@everywhere begin # this part will be available on all CPUs\r\n\r\nfunction computation(runid_is_useless)\r\n  result = rand()\r\n  return result\r\nend # function computation()\r\n\r\nend # @everywhere\r\n\r\n### Main part - runs only on main instance of Julia.\r\n\r\nNprocs = nprocs()\r\nprint(\"Number of processors \",Nprocs,\".\\n\")\r\n\r\n## Launching computations on Nprocs parallel processors\r\nresults = @time pmap(computation,1:Nprocs)\r\n\r\n## Post-processing\r\nfor i in 1:Nprocs\r\n  print(@sprintf(\"Result of CPU %i is %f\\n\",i,results[i]))\r\nend # for i\r\n<\/pre>\n<p>On my laptop, my processor has 4 cores, and I run this code with <strong>julia -p 3 code.jl<\/strong> which gives<\/p>\n<pre>Number of processors 4.\r\n0.548238 seconds (214.75 k allocations: 9.788 MB, 5.76% gc time)\r\nResult of CPU 1 is 0.725449\r\nResult of CPU 2 is 0.377958\r\nResult of CPU 3 is 0.443606\r\nResult of CPU 4 is 0.016641\r\n<\/pre>\n<p>The same code with <strong>-p 39<\/strong> can also be run on the 40 CPUs cluster of my department! For more material on parallel computing with Julia, you may take a look at <a href=\"Julia documentation\">Julia documentation<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Julia is particularly attractive for its ability to run in parallel, not only on clusters, but also on the multiple cores of your laptop processor.&#8230;<\/p>\n<div class=\"more-link-wrapper\"><a class=\"more-link\" href=\"https:\/\/djalil.chafai.net\/blog\/2018\/05\/08\/parallel-computing-with-julia\/\">Continue reading<span class=\"screen-reader-text\">Parallel computing with Julia<\/span><\/a><\/div>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":94},"categories":[1],"tags":[],"_links":{"self":[{"href":"https:\/\/djalil.chafai.net\/blog\/wp-json\/wp\/v2\/posts\/10356"}],"collection":[{"href":"https:\/\/djalil.chafai.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/djalil.chafai.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/djalil.chafai.net\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/djalil.chafai.net\/blog\/wp-json\/wp\/v2\/comments?post=10356"}],"version-history":[{"count":6,"href":"https:\/\/djalil.chafai.net\/blog\/wp-json\/wp\/v2\/posts\/10356\/revisions"}],"predecessor-version":[{"id":10378,"href":"https:\/\/djalil.chafai.net\/blog\/wp-json\/wp\/v2\/posts\/10356\/revisions\/10378"}],"wp:attachment":[{"href":"https:\/\/djalil.chafai.net\/blog\/wp-json\/wp\/v2\/media?parent=10356"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/djalil.chafai.net\/blog\/wp-json\/wp\/v2\/categories?post=10356"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/djalil.chafai.net\/blog\/wp-json\/wp\/v2\/tags?post=10356"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}