Fun with Fractals: Parallel Better than Straight Threads? Share your comment!

A lot of technologies — some image processing applications, scenery generators, and biology simulators, to name a few — use fractals as their core. Since fractal technology has been here since about 1975, it seemed high time to conjure some up within the Intel Parallel universe.

I started this project off to show you how to generate parallel Mandelbrot images and then display them. After digging up the code, I wrote a fairly simple application that used random parameters to generate variations on this well-known pattern. I compared a sequential generation of four images with a parallel generation of four images. Of course the parallel generations won, since they took less than half the time to perform. The following code makes it a snap to do this: 

cilk_for( int i=0; i

It’s easy to forget you can't have user interface code within the method that makes cilk calls. For instance, I was making a call to SetDlgItemText to display elapsed time. Doing this defeats most of the advantages of Parallel Studio. You sometimes have to be clever to take advantage of the technology and make the necessary user interface calls. I’ve experimented with quite a few methodologies. One of the safest is to create a PreTranslateMessage method and send a WM_USER message with a PostMessage call.

But my test wasn't fair. I was pitting Parallel Studio against sequential code, which didn’t stand a chance. Of course, if I could spawn normal operating system threads to create the fractal images, it would be a fair comparison. So that's what I did. I created a thread procedure that kicked off using regular Windows operating system API calls. The following code did essentially the same thing as the cilk code above. (Please note that cilk_sync makes life much easier for developers.)

for( int i=0; i

I used a Windows timer to cycle between all three creation and rendering types. First was sequential creation of four fractal images. Second, creation of four fractal images using Windows API threads. Third, creation of four fractal images using the cilk_for and cilk_sync syntax.  In Figure 1 you will see a very fancy app with four fractal images and three text labels. The labels show elapsed time for creating the four fractal images. The actual drawing code was done in a completely different thread set up to draw all four images.

Figure 1: This application generates four fractal images and displays them, and shows the elapsed time in milliseconds for each methodology.

Figure 1: This application generates four fractal images and displays them, and shows the elapsed time in milliseconds for each methodology.

You also see that parallel processing of the fractal image creation beat both other methods hands down. That's really important because things like fractal creation are CPU hogs. Here, as everywhere, any time you can get better performance, users will be happy.

In addition to the performance gain, the parallel technology is engineered from the ground up to perform parallel tasks such as this. For this reason, it just works much better than garden variety Windows API threads. Advantage, parallel. 

If you want to twiddle the bits and take a look at the app I wrote, you can download it here. Let me know if you have questions, comments, inspirations, etc. 

Posted on May 23, 2013 by Rick Leinecker, Slashdot Media Contributing Editor