{"id":617,"date":"2014-08-17T04:27:14","date_gmt":"2014-08-17T04:27:14","guid":{"rendered":"http:\/\/www.dyalog.com\/blog\/?p=617"},"modified":"2016-11-21T14:07:49","modified_gmt":"2016-11-21T14:07:49","slug":"isolated-mandelbrot-set-explorer","status":"publish","type":"post","link":"https:\/\/www.dyalog.com\/blog\/2014\/08\/isolated-mandelbrot-set-explorer\/","title":{"rendered":"Isolated Mandelbrot Set Explorer"},"content":{"rendered":"<p><a href=\"http:\/\/www.dyalog.com\/blog\/wp-content\/uploads\/2014\/08\/mandelbrot-equation.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-656 aligncenter\" src=\"http:\/\/www.dyalog.com\/blog\/wp-content\/uploads\/2014\/08\/mandelbrot-equation.png\" alt=\"mandelbrot-equation\" width=\"114\" height=\"23\" \/><\/a><\/p>\n<p>It amazes me that\u00a0an equation so simple can produce images so beautiful and complex. \u00a0I was first introduced to the <a title=\"Wolfram MathWorld Mandelbrot Set Article\" href=\"http:\/\/mathworld.wolfram.com\/MandelbrotSet.html\" target=\"_blank\">Mandelbrot set<\/a> by the\u00a0<a title=\"Computer Recreations article\" href=\"https:\/\/www.scientificamerican.com\/media\/inline\/blog\/File\/Dewdney_Mandelbrot.pdf\" target=\"_blank\">August 1985 issue of Scientific American<\/a>. <a title=\"Wikipedia Mandelbrot Set Article\" href=\"http:\/\/en.wikipedia.org\/wiki\/Mandelbrot_set\" target=\"_blank\">Wikipedia<\/a> says &#8220;The\u00a0Mandelbrot set is the set of complex numbers &#8216;c&#8217; for which the sequence ( c , c\u00b2 + c , (c\u00b2+c)\u00b2 + c , ((c\u00b2+c)\u00b2+c)\u00b2 + c , (((c\u00b2+c)\u00b2+c)\u00b2+c)\u00b2 + c , &#8230;) does not approach infinity.&#8221; This means that in the images below, the areas in black are points in the Mandelbrot set and the other point colors are determined by how quickly that point &#8220;zooms&#8221; off towards infinity. The links above give\u00a0a deeper discussion on the Mandelbrot set \u2013 for this post\u00a0I&#8217;d like to focus on how I was able to speed my Mandelbrot set explorer\u00a0up using isolates.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-619\" src=\"http:\/\/www.dyalog.com\/blog\/wp-content\/uploads\/2014\/08\/mandelbrot.png\" alt=\"Mandelbrot Set\" width=\"800\" height=\"600\" srcset=\"https:\/\/www.dyalog.com\/blog\/wp-content\/uploads\/2014\/08\/mandelbrot.png 800w, https:\/\/www.dyalog.com\/blog\/wp-content\/uploads\/2014\/08\/mandelbrot-300x225.png 300w, https:\/\/www.dyalog.com\/blog\/wp-content\/uploads\/2014\/08\/mandelbrot-400x300.png 400w\" sizes=\"auto, (max-width: 800px) 100vw, 800px\" \/><br \/>\nI find I learn things more easily through application than abstraction. In 2006 I wanted to learn more about the built-in GUI features of Dyalog for Windows and the application I chose was a Mandelbrot set explorer.\u00a0While the basic algorithm for calculating a point in the Mandelbrot set is fairly simple, a\u00a0640\u00d7480 pixel image could involve over 75 million calculations. As new versions of Dyalog are released, I try to experiment with any applicable new features and possibly incorporate them into the explorer. For instance, when complex numbers were introduced in Dyalog, I modified the explorer to use them.<\/p>\n<p><a title=\"Dyalog v14.0 announcement\" href=\"http:\/\/www.dyalog.com\/news\/91\/420\/Dyalog-Ltd-Announces-Dyalog-Version-14-0.htm\" target=\"_blank\">Dyalog v14.0<\/a> introduced a prototype of a parallel computing feature called <a title=\"Isolates Documentation\" href=\"http:\/\/docs.dyalog.com\/14.0\/Dyalog%20APL%20Experimental%20Functionality%20-%20Parallel%20Language%20Features.pdf\" target=\"_blank\">isolates<\/a> that may one day be integrated into the interpreter. Isolates behave much like namespaces except that they run in\u00a0their own process with their own copy of the interpreter. Each process can run on a separate processor, greatly improving performance. You can even set up isolate servers on other machines.<\/p>\n<p>Mandelbrot set calculations are ideal candidates for parallelization as:<\/p>\n<ul>\n<li>they&#8217;re\u00a0CPU intensive<\/li>\n<li>each pixel can be calculated independently of others<\/li>\n<li>there are no side effects and no state to maintain<\/li>\n<\/ul>\n<p>The core calculation is shown below. It takes the real and imaginary parameters for the top left corner, the height and width of the image and the increment along each dimension and uses those parameters to build a set of co-ordinates, then iterates to see how quickly each co-ordinate &#8220;escapes&#8221; the Mandelbrot set.<\/p>\n<pre><code class=\"language-apl\"> \u2207 r\u2190real buildset_core imaginary;height;i_incr;top;width;r_incr;left;set;coord;inds;cnt;escaped\r\n  \u235d Calculate new Mandelbrot set - remember using origin 0 (\u2395IO\u21900)\r\n  \u235d real and imaginary are the parameters of the space to calculate\r\n  \u235d       real            imaginary\r\n  \u235d [0]   width           height            of the image\r\n  \u235d [1]   increment size  increment size    along each axis\r\n  \u235d [2]   top             left              coordinates\r\n\r\n   (height i_incr top)\u2190imaginary            \u235d split imaginary argument parameters\r\n   (width r_incr left)\u2190real                 \u235d split real argument parameters\r\n   set\u2190coord\u2190,(0J1\u00d7top+i_incr\u00d7\u2373height)\u2218.+(left+r_incr\u00d7\u2373width) \u235d create all points in the image\r\n   inds\u2190\u2373\u2262r\u2190(\u2262coord)\u23740                      \u235d initialize the result and create vector of indices\r\n   :For cnt :In 1\u2193\u2373256                      \u235d loop up to 255 times (the size of our color palette)\r\n       escaped\u21904&lt;coord\u00d7+coord               \u235d mark those that have escaped the Mandelbrot set\r\n       r[escaped\/inds]\u2190cnt                  \u235d set their index in the color palette\r\n       (inds coord)\u2190(~escaped)\u2218\/\u00a8inds coord \u235d keep those that have not yet escaped\r\n       :If 0\u220a\u2374inds \u22c4 :Leave \u22c4 :EndIf        \u235d anything left to do?\r\n       coord\u2190set[inds]+\u00d7\u2368coord              \u235d the core Mandelbrot computation... z\u2190(z*2)+c\r\n   :EndFor\r\n   r\u2190(height,width)\u2374r                       \u235d reshape to image size\r\n \u2207\r\n<\/code><\/pre>\n<p>So, what&#8217;s involved in using isolates to parallelize the explorer? \u00a0As it turns out, not much! First I )COPYed the isolate namespace from the isolate workspace distributed with Dyalog v14.0. I wanted to be able to select the number of processors to use, so I added a line to my init function to query how many processors are available:<\/p>\n<pre><code class=\"language-apl\">\u00a0processors\u21901111\u2336\u236c \u235d query number of processors<\/code><\/pre>\n<p>Next I wrote a small function to initialize isolates for as many processors as were selected (2 or more). Notice that when\u00a0creating the isolates,\u00a0I copy\u00a0buildset_core (from above) into each one:<\/p>\n<pre><code class=\"language-apl\"> \u2207 init_isolates processors\r\n  \u235d called upon initialization, or whenever user changes number of processors to use\r\n   f.isomsg.Visible\u21901    \u235d turn form's \"Isolates initializing\" message on\r\n   {}isolate.Reset''     \u235d reset the isolates\r\n   {}isolate.Config'processors'processors \u235d set the number of processors\r\n   :If processors&gt;1      \u235d if using more than one processor\r\n       \u235d create the isolates, populating each one with buildset_core\r\n       isolates\u2190isolate.New\u00a8processors\u2374\u2395NS'buildset_core'\r\n   :EndIf\r\n   f.isomsg.Visible\u21900    \u235d turn the form message off\r\n \u2207\r\n<\/code><\/pre>\n<p>Finally, I wrote a routine to share the work between the selected number of processors and assemble the results back together.<\/p>\n<pre><code class=\"language-apl\"> \u2207 r\u2190size buildset rect;top;bottom;left;right;width;height;vert;horz;rows;incr;imaginary;real\r\n  \u235d Build new Mandelbrot Set using isolates if specified\r\n  \u235d rect is the top, bottom, left, and right\r\n  \u235d size is the height and width of the image\r\n   (top bottom left right)\u2190rect\r\n   (height width)\u2190size\r\n   vert\u2190bottom-top                             \u235d vertical dimension\r\n   horz\u2190right-left                             \u235d horizontal dimension\r\n   rows\u2190processors{\u00af2-\/\u2308\u2375,\u2368(\u2373\u237a)\u00d7\u2375\u00f7\u237a}height     \u235d divide image among processors (height=+\/rows)\r\n   incr\u2190vert\u00f7height                            \u235d vertical increment\r\n   imaginary\u2190\u2193rows,incr,\u236atop+incr\u00d7\u00af1\u21930,+\\rows  \u235d imaginary arguments to buildset_core\r\n   real\u2190width,(horz\u00f7width),left                \u235d real arguments to buildset_core\r\n   :If processors=1                            \u235d if not using isolates\r\n       r\u2190real buildset_core\u2283imaginary          \u235d build the image here\r\n   :Else\r\n       r\u2190\u2283\u236a\/(\u2282real)isolates.buildset_core imaginary \u235d otherwise let the isolates to it\r\n   :EndIf\r\n \u2207\r\n<\/code><\/pre>\n<p>The images below depicts what happens with 4 processors. Each processor builds a slice of the image and the main task assembles them together.<br \/>\n<a href=\"http:\/\/www.dyalog.com\/blog\/wp-content\/uploads\/2014\/08\/Drawing1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-622\" src=\"http:\/\/www.dyalog.com\/blog\/wp-content\/uploads\/2014\/08\/Drawing1.png\" alt=\"Drawing1\" width=\"660\" height=\"247\" srcset=\"https:\/\/www.dyalog.com\/blog\/wp-content\/uploads\/2014\/08\/Drawing1.png 660w, https:\/\/www.dyalog.com\/blog\/wp-content\/uploads\/2014\/08\/Drawing1-300x112.png 300w, https:\/\/www.dyalog.com\/blog\/wp-content\/uploads\/2014\/08\/Drawing1-500x187.png 500w\" sizes=\"auto, (max-width: 660px) 100vw, 660px\" \/><br \/>\n<\/a><\/p>\n<p>So, how much faster is it? \u00a0Did my 8 processor Intel Core i7 speed things up by a factor of 8? No, but depending on where in the Mandelbrot set you explore, the speed is generally increased by at least a factor of 2 and sometimes as high as 5 or 6. I thought about why this might be and have a couple of ideas&#8230;<\/p>\n<ul>\n<li>There&#8217;s overhead to receive and assemble the results from each isolate.<\/li>\n<li>The overall time will be the maximum of the individual times; if one slice is in an area that requires more iterations, then the main task needs to wait for those results, even if the other slices have already finished. I could possibly show this by updating the individual slices as they become available. Maybe next time \ud83d\ude42<\/li>\n<\/ul>\n<p>What impressed me was that I could speed up my application significantly with about 20 lines of code and maybe an hour&#8217;s effort \u2013 most of which was spent tinkering to learn isolates. You can download the Mandelbrot set explorer from <a title=\"Brian's GitHub\" href=\"https:\/\/github.com\/Dyalog\/bb\/tree\/master\/mandelbrot\" target=\"_blank\">my GitHub repository.<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>It amazes me that\u00a0an equation so simple can produce images so beautiful and complex. \u00a0I was first introduced to the Mandelbrot set by the\u00a0August 1985 issue of Scientific American. Wikipedia says &#8220;The\u00a0Mandelbrot set is the set of complex numbers &#8216;c&#8217; &hellip; <a href=\"https:\/\/www.dyalog.com\/blog\/2014\/08\/isolated-mandelbrot-set-explorer\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":10,"featured_media":619,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8,19,15],"tags":[],"class_list":["post-617","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-apl-language","category-tools","category-windows"],"_links":{"self":[{"href":"https:\/\/www.dyalog.com\/blog\/wp-json\/wp\/v2\/posts\/617","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.dyalog.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.dyalog.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.dyalog.com\/blog\/wp-json\/wp\/v2\/users\/10"}],"replies":[{"embeddable":true,"href":"https:\/\/www.dyalog.com\/blog\/wp-json\/wp\/v2\/comments?post=617"}],"version-history":[{"count":38,"href":"https:\/\/www.dyalog.com\/blog\/wp-json\/wp\/v2\/posts\/617\/revisions"}],"predecessor-version":[{"id":4723,"href":"https:\/\/www.dyalog.com\/blog\/wp-json\/wp\/v2\/posts\/617\/revisions\/4723"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.dyalog.com\/blog\/wp-json\/wp\/v2\/media\/619"}],"wp:attachment":[{"href":"https:\/\/www.dyalog.com\/blog\/wp-json\/wp\/v2\/media?parent=617"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.dyalog.com\/blog\/wp-json\/wp\/v2\/categories?post=617"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.dyalog.com\/blog\/wp-json\/wp\/v2\/tags?post=617"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}