Let’s try this again…

In my first attempt at writing a parallel program with TBB, my speedup was disappointing (about 4%). I guessed that vector multiplication wasn’t computationally intensive enough to make good use of multiple cores. I tried to remedy that by writing a program to find points in a Julia set. The Julia set is just the set of points that don’t shoot off to infinity when they are iterated by the function:

z(n+1) = z(n) * z(n) + c

So the basic technique is to take a candidate point z and repeatedly square it and add a constant (both z and c are complex numbers). If it exceeds a bound within a set number of iterations, then z is probably not a Julia point (this is an approximate test). By varying the bound and the number of iterations, I could easily vary the computational load.

The C++ code for both the parallel and serial versions of my Julia set generator is shown below:

// example2.cpp : Defines the entry point for the console application.

#include “stdafx.h”
#include // for complex numbers
#include “tbb/task_scheduler_init.h”
#include “tbb/parallel_for.h”
#include “tbb/blocked_range.h”
#include “tbb/tick_count.h”
using namespace tbb;
using namespace std;

typedef complex JPT;

class julia{
JPT *const z; // pointer to points to test for “Julianess”
const complex c; // complex constant in eq. z_i+1 = (z_i)^2 + c
const unsigned n; // # times to iterate equation
const double b; // Julia point if z_n >= b
bool *is_julia; // put the result here
// constructor copies the arguments into local storage
julia(JPT *arg_z, complex arg_c, int arg_n, double arg_b, bool *arg_is_julia)
: z(arg_z), c(arg_c), n(arg_n), b(arg_b), is_julia(arg_is_julia) { }
// overload () so it finds julia points
void operator() (const blocked_range &r) const {
for(size_t i=r.begin(); i!=r.end(); i++) {
complex zi = z[i]; // store the point to test
is_julia[i] = true; // start off assuming it’s a Julia point
for(unsigned j=0; j= b) {
// Not a Julia point if it exceeds the bound within n iterations
is_julia[i] = false;
zi = zi * zi + c; // keep iterating the point and see where it goes

const size_t n_pts = 100000; // number of points to test
const JPT z_lo = 0.0; // find Julia points between here…
const JPT z_hi = 1.0; // and here
const complex c_julia(-0.123,0.745); // complex constant for iterations
const unsigned n_iter = 100; // number of iterations for checking bounds
const double bound = 100000; // bound for testing “Julianess” of a point

int _tmain(int argc, _TCHAR* argv[])
JPT *z = (JPT*)malloc(n_pts*sizeof(JPT)); // storage for points to test
bool *is_julia = (bool*)malloc(n_pts*sizeof(bool)); // storage for test results
complex c = c_julia;
const unsigned n = n_iter;
const double b = bound;

// initialize vector with evenly-spaced points along a line
JPT spacing = (z_hi – z_lo)/JPT(n_pts-1); // spacing between points
z[0] = z_lo; // initial point
for(size_t i=1; isourcecode for this example if you want to play around with it.

About dave_vandenbout
President of XESS Corp, a manufacturer of FPGA development boards.

One Response to Let’s try this again…

  1. Remember what Robert Kiyosaki, Paul Zane Pilzer and even Richard Branson embrace
    network marketing strategy template. Excel in your game as any professional sportsman
    does. Imagine – you have an effective business plan.
    This is closely related to loan officer marketing strategy template plan, you can see
    why financial luminaries including Warren Buffet, Donald Trump, Paul Zane Pilzer and even Richard Branson embrace network marketing strategy template.
    Maureen Morrison at AdAge has written up a great brand and
    how to maximize employee productivity.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: