Tuesday, February 16, 2021

Glimmer DSL for SWT Mandelbrot Fractal and Hello, Cursor! Samples

UPDATED WITH CODE: 2021-02-19


It includes two new samples:
  • Mandelbrot Fractal: this is a very well known Computer Science/Math algorithm that renders a fractal at various zoom levels, demonstrating the repetitive nature of fractals. During my bachelor of Computer Science at McGill University (Montreal, QC, Canada), I remember spending very long and tiring nights at the computer lab to implement an Assembly language renderer of the Mandelbrot Fractal. Much has changed since then. We have multi-core processors today, let alone the wonderful Ruby programming language, so I wrote this with Glimmer DSL for SWT by taking advantage of the multi-threaded JRuby and saturating all CPU cores to finish calculating Mandelbrot points in less than 10 seconds on 4 CPU cores. It takes advantage of the idea of Thread Pools, resulting in very terse multi-threaded code, which is implemented using concurrent-ruby, a multi-threading library included in Glimmer. The sample supports unlimited zooming, pre-calculated in the background with the window title bar notifying you once higher zooms are available. It also allows panning with scrollbars or mouse dragging. Enjoy!
  • Hello, Cursor!: introduces the various mouse cursors available out of the box in SWT.


Mandelbrot Fractal Sample



Hello, Cursor! Sample

Below is the multi-threaded model code and Glimmer GUI DSL code of the Mandelbrot Fractal initial version (without zooming). The way I parallelized processing is by realizing that the calculation of each Mandelbrot Fractal point (happening in a loop) is independent of all other points, and thus I could distribute and process in separate threads. Of course, I don't want more than 4 threads (or 8 hyperthreads), which depends on the number of CPU cores (I have 4) to avoid thread competition for CPUs slowing down the calculation. As such, I relied on a Thread Pool constrained by as many processors as available. See below how simple the code to achieve that is with Ruby and the concurrent-ruby gem. Otherwise, the Glimmer DSL for SWT GUI simply paints an image as a form of image buffering (to avoid Mandelbrot pixel repaints on GUI repaints) and then puts it inside a canvas to display.


One final note is that obviously from a Software Engineering point of view, Ruby wouldn't be the fastest language at calculating the Mandelbrot Fractal points given it is dynamically typed and interpreted. So, think of this as just a Glimmer GUI learning exercise taking advantage of the fact that modern computers ship with so many cores that even Ruby has a fighting a chance, albeit not the fastest, for this type of problem. That said, for serious applications, if you offload the calculation to another statically typed language like Java while integrating with JRuby for the Glimmer GUI, this sample should run much faster.

Happy Glimmering!

You may learn more about how the Mandelbrot Fractal sample got a progress bar and menus in this following blog post.


No comments: