<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Kerry D. Wong &#187; Multi-threading</title>
	<atom:link href="http://www.kerrywong.com/tag/multi-threading/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.kerrywong.com</link>
	<description></description>
	<lastBuildDate>Fri, 03 Sep 2010 00:51:09 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>A Simple Thread Barrier Implementation</title>
		<link>http://www.kerrywong.com/2009/11/27/a-simple-thread-barrier-implementation/</link>
		<comments>http://www.kerrywong.com/2009/11/27/a-simple-thread-barrier-implementation/#comments</comments>
		<pubDate>Sat, 28 Nov 2009 01:03:38 +0000</pubDate>
		<dc:creator>kwong</dc:creator>
				<category><![CDATA[Coding]]></category>
		<category><![CDATA[Barrier]]></category>
		<category><![CDATA[C Sharp (C#)]]></category>
		<category><![CDATA[Multi-threading]]></category>

		<guid isPermaLink="false">http://www.kerrywong.com/?p=1540</guid>
		<description><![CDATA[Sometimes a group of concurrently running threads may need to rendezvous at a certain point in time before they can further proceed. This situation commonly arises in areas like event simulation, where the events are synchronized via a clock event (see illustration below): In the above example, a number of threads start at different times [...]]]></description>
			<content:encoded><![CDATA[<p>Sometimes a group of concurrently running threads may need to rendezvous at a certain point in time before they can further proceed. This situation commonly arises in areas like event simulation, where the events are synchronized via a clock event (see illustration below):<span id="more-1540"></span><br />
<div id="attachment_1541" class="wp-caption aligncenter" style="width: 620px"><a href="http://www.kerrywong.com/blog/wp-content/uploads/2009/11/eventbarrier.gif"><img src="http://www.kerrywong.com/blog/wp-content/uploads/2009/11/eventbarrier.gif" alt="Event Barrier" title="Event Barrier" width="610" height="176" class="size-full wp-image-1541" /></a><p class="wp-caption-text">Event Barrier</p></div></p>
<p>In the above example, a number of threads start at different times with the arrival of each clock event and wait till all threads finish running before the next clock event arrives.</p>
<p>Such <a href="http://en.wikipedia.org/wiki/Barrier_%28computer_science%29">barrier</a> can be implemented using various techniques with different level of difficulties. <a href="http://www.albahari.com/threading/part3.aspx#_Thread_Pooling">One possible implementation</a> can be found in  <a href="http://www.albahari.com/threading/">Joseph Albahari&#8217;s Threading in C#</a> where a monitor construct is used to wait on a synchronization object in the main thread after the worker threads are queued. </p>
<pre class="brush: csharp;">
    lock (workerLocker)
    {
      while (runningWorkers &gt; 0)
        Monitor.Wait (workerLocker);
    }
</pre>
<p>In each of the worker thread, the number of running workers is decremented when the thread finishes:</p>
<pre class="brush: csharp;">
    lock (workerLocker)
    {
      runningWorkers--;
      Monitor.Pulse (workerLocker);
    }
</pre>
<p>And when the running number of workers reaches zero, the monitor exist the wait condition and the main thread continues.</p>
<p>The wait on the locker object forms a barrier in the example above. The method illustrated here is simple and elegant. It can be used to create the barriers needed to synchronize the clock event mentioned earlier.</p>
<p>Threadpooling might not always be the optimal solution though. While the number of threads in a thread pool is typically bounded to a few dozens, it may incur excessive context switching penalties if the majority of the threads are CPU bound. In a computation intensive situation, a fixed number of threads approximating the number of computational cores would be more appropriate (for instance, four threads on a four core CPU).</p>
<p>Here I will show another relatively simple way to construct a barrier where only a predetermined maximum number of threads can reach at a given time. To achieve such a ceiling, a global semaphore is used (in this example three concurrent threads can enter the semaphore):</p>
<pre class="brush: csharp;">
public class Global
{
    public static Semaphore sp = new Semaphore(3, 3);
}
</pre>
<p>To simplify coding and shield the programmer from the synchronization mechanism details, I created an <strong>IEvent</strong> interface and an abstract <strong>EventBase</strong> as follows:</p>
<pre class="brush: csharp;">
interface IEvent
{
    void OnEvent(int arg);
}
</pre>
<pre class="brush: csharp;">
public abstract class EventBase : IEvent
{
    #region IEvent Members
    public void OnEvent(int arg)
    {
        Global.sp.WaitOne();
        Run(arg);
        Global.sp.Release();
    }
    #endregion

    public virtual void Run(int arg)
    {
    }
}
</pre>
<p>Users can simply implement their code that needs to block at the barrier by inheriting from <strong>EventBase</strong> and overridding the <strong>Run()</strong> method:</p>
<pre class="brush: csharp;">
public class Event1 : EventBase
{
    public override void Run(int arg)
    {
        Console.WriteLine(&quot;Thread {0}, arg {1}&quot;, Thread.CurrentThread.ManagedThreadId, arg);
        Thread.Sleep(1000);
    }
}
</pre>
<p>To illustrate how this barrier implementation can be used considering the following code:</p>
<pre class="brush: csharp;">
class Program
{
    delegate void MyEvent(int arg);

    static void Main(string[] args)
    {
        IEvent e = new Event1();
        MyEvent m = new MyEvent(e.OnEvent);

        List&lt;IAsyncResult&gt; l = new List&lt;IAsyncResult&gt;();

        for (int i = 0 ; i &lt; 6 ; i++)
        {
          IAsyncResult r = m.BeginInvoke(i, null, null);
          l.Add(r);
        }

        foreach (IAsyncResult r in l)
        {
            m.EndInvoke(r);
        }
    }
}
</pre>
<p>In the code above, we simulate six concurrent threads that need to be synchronized. The multi-threading is achieved via asynchronous invocation. Because the capacity of the semaphore is set to be 3 in this example and if this code is run on a quad-core machine, you will see that the six asynchronous threads are run in two batches, three at a time. And all the threads are synchronized at the end. Here is the sample output:</p>
<blockquote><p>
Thread 10, arg 1<br />
Thread 11, arg 2<br />
Thread 6, arg 0<br />
Thread 13, arg 4<br />
Thread 10, arg 5<br />
Thread 12, arg 3
</p></blockquote>
<p>Using this approach, the number of concurrently running threads can be precisely controlled by the maximum threads allowed in the semaphore.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.kerrywong.com/2009/11/27/a-simple-thread-barrier-implementation/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Poor Man&#8217;s Parallel Task Dispatcher</title>
		<link>http://www.kerrywong.com/2009/07/14/poor-mans-parallel-task-dispatcher/</link>
		<comments>http://www.kerrywong.com/2009/07/14/poor-mans-parallel-task-dispatcher/#comments</comments>
		<pubDate>Wed, 15 Jul 2009 01:55:39 +0000</pubDate>
		<dc:creator>kwong</dc:creator>
				<category><![CDATA[Coding]]></category>
		<category><![CDATA[C Sharp (C#)]]></category>
		<category><![CDATA[Mono]]></category>
		<category><![CDATA[Multi-threading]]></category>

		<guid isPermaLink="false">http://www.kerrywong.com/?p=1392</guid>
		<description><![CDATA[Even though multi-core processors are almost ubiquitous nowadays, applications are slow to catch up. Of course, one could always re-write the applications in order to take the full advantages in a multi-core environment. But it is not an easy undertaking. For applications that performs rather repetitive tasks over a well defined set of data (e.g. [...]]]></description>
			<content:encoded><![CDATA[<p>Even though multi-core processors are almost ubiquitous nowadays, applications are slow to catch up. Of course, one could always re-write the applications in order to take the full advantages in a multi-core environment. But it is not an easy undertaking.<span id="more-1392"></span> For applications that performs rather repetitive tasks over a well defined set of data (e.g. image processing) it is relatively easy to utilize multiple cores with some simple modifications to the applications themselves. </p>
<p>For the type of applications that perform the same task over a large dataset for example,</p>
<pre class="brush: csharp;">
for (int i = 0 ; i &lt; num ; i++)
{
    //lengthy tasks
}
</pre>
<p>we could easily break down the <em>for loop</em> evenly into chunks that fit into the number of processors given. And for each of the subsets we could create a new process to perform the desired tasks. For instance, if we were to execute the above <em>for loop</em> in parallel on a duo-core processor, we could re-write each of the loops like the following:</p>
<pre class="brush: csharp;">
//core 1
for (int i = 0 ; i &lt; num ; i+=2)
{
    //lengthy tasks
}
</pre>
<pre class="brush: csharp;">
//core 2
for (int i = 1 ; i &lt; num ; i+=2)
{
    //lengthy tasks
}
</pre>
<p>By executing the application in separate processes, we eliminated certain race conditions that must be properly handled in a multi-threaded process and thus it is far easier to execute multiple processes concurrently when there is no inter-process communications involved than to handle multi-processing within a process via multi-threading. In a multi-process scenario, the operating system handles the concurrency issue behind the scene whereas in the multi-threading case the application developer must ensure the code regions are thread safe.</p>
<p>The following C# code shows how to dispatch tasks among different processes using asynchronous method invocations. Each process being invoked can identify its order by the extra integer parameter (line 36) passed to it. Thus we only needed to modify the target application to take this extra parameter into account (for instance, in a quad-core system, the first process would process items 1,5,9&#8230; and the second process would run items 2,6,10, and so on)</p>
<pre class="brush: csharp;">
using System;
using System.Diagnostics;
using System.Threading;

namespace MPDispatcher
{
    class Program
    {
        public delegate bool CmdDlg(string fn, string args);

        private bool RunCommand(string fn, string args)
        {
            bool r = true;
            Process p = new Process();

            p.StartInfo.FileName = fn;
            p.StartInfo.Arguments = args;

			Console.WriteLine(String.Format(&quot;Starting {0} with arguments {1}. Thread ID: {2}&quot;, fn, args, Thread.CurrentThread.ManagedThreadId));
            r = p.Start();
            p.WaitForExit();

            return r;
        }

		public bool DispatchCommand(int numOfThreads, string args)
        {
            bool r = true;

            IAsyncResult[] ars = new IAsyncResult[numOfThreads];
            CmdDlg[] dlgs = new CmdDlg[numOfThreads];

            for (int i = 0; i &lt; numOfThreads; i++)
            {
                dlgs[i] = new CmdDlg(RunCommand);
                ars[i] = dlgs[i].BeginInvoke(args, i.ToString(), null, null);
				Thread.Sleep(100);
            }

            bool finished = false;

			//wait till all processes are finished.
            while (!finished)
            {
                bool t = true;

                for (int i = 0; i &lt; numOfThreads; i++)
                    t = t &amp;&amp; ars[i].IsCompleted;   

                if (t) finished = true;

                Thread.Sleep(100);
            }

            for (int i = 0; i &lt; numOfThreads; i++)
            {
                r = r &amp;&amp; dlgs[i].EndInvoke(ars[i]);
            }

            return r;
        }

        static void Main(string[] args)
        {
            Program p = new Program();

			if (args.Length &lt; 1 || args[0].ToUpper() == &quot;-H&quot;) {
				Console.WriteLine(&quot;Usage: MPDispatcher {app name} {app args}&quot;);
			} else {
				int numThreads = 4; //use 4 threads
				string argStr = string.Empty;

				for (int i = 1 ; i &lt; args.Length; i++) {
					argStr = String.Format(&quot;{0} {1}&quot;, argStr, args[i]);
				}

            	p.DispatchCommand(numThreads, argStr);
			}
        }
    }
}
</pre>
<p>The above code works under Windows and Linux (Mono). </p>
]]></content:encoded>
			<wfw:commentRss>http://www.kerrywong.com/2009/07/14/poor-mans-parallel-task-dispatcher/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Interfacing IPP with Magick++</title>
		<link>http://www.kerrywong.com/2009/03/17/interfacing-ipp-with-magick/</link>
		<comments>http://www.kerrywong.com/2009/03/17/interfacing-ipp-with-magick/#comments</comments>
		<pubDate>Wed, 18 Mar 2009 01:01:24 +0000</pubDate>
		<dc:creator>kwong</dc:creator>
				<category><![CDATA[Coding]]></category>
		<category><![CDATA[Algorithm]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[Image Processing]]></category>
		<category><![CDATA[IPP]]></category>
		<category><![CDATA[Multi-threading]]></category>

		<guid isPermaLink="false">http://www.kerrywong.com/?p=864</guid>
		<description><![CDATA[Intel&#8217;s Integrated Performance Primitives (IPP) is a low level C++ library. It provides routines that are highly optimized on Intel processors. I recently started using it because its vast speed advantage in signal and image processing applications. Since the implementation of many of the functions are threaded, it makes the task of writing high performance [...]]]></description>
			<content:encoded><![CDATA[<p>Intel&#8217;s <a href="http://www.intel.com/cd/software/products/asmo-na/eng/302910.htm">Integrated Performance Primitives (IPP)</a> is a low level C++ library. It provides routines that are highly optimized on Intel processors. I recently started using it because its vast speed advantage in signal and image processing applications.<span id="more-864"></span></p>
<p>Since the implementation of many of the functions are threaded, it makes the task of writing high performance applications much easier. Since it is a set of &#8220;performance primitives&#8221; as the name suggests, it uses its own data structures (e.g. Ipp8u) and does not provide functions to directly inter-operate with data coming from other sources (e.g. image files).</p>
<p>Fortunately, such data conversion is pretty straight forward. In this post, I will illustrate how to convert an image file (e.g. .jpg, .png, .gif) to the data IPP uses, and how to save the result into a standard image file once the processing is done.</p>
<p><a href="http://www.imagemagick.org/Magick%2B%2B/">Magick++</a> is a very comprehensive image-processing C++ library and the Image class it provides handles image files quite well. So I chose to use Magic++&#8217;s API to convert image files to the data structure IPP uses. In this particular example, I will use a gray level image. But in practice, color images can be easily handled in a similar fashion.</p>
<p>For the code mentioned below, the following headers and namespaces are used:</p>
<pre class="brush: cpp;">
#include &lt;Magick++/Image.h&gt;
#include &lt;Magick++.h&gt;
#include &lt;ipp.h&gt;

using namespace std;
using namespace Magick;
</pre>
<p>And the following code shows how to convert a standard image file data into format that is suitable for IPP.</p>
<pre class="brush: cpp;">
    IppStatus sts;
    Image img(&quot;{Image File Name}&quot;);
    Geometry g = img.size();

    unsigned int width = g.width();
    unsigned int height= g.height();

    Pixels view(img);
    PixelPacket *pixels = view.get(0,0,width,height);

    int stepByte = 0;
    //allocating a buffer of unsigned char (Ipp8u) for the image.
    Ipp8u *imgCache = ippiMalloc_8u_C1(width,height, &amp;stepByte);

    for (unsigned int row = 0; row &lt; height ; row++)
    {
        for (unsigned int column = 0; column &lt; width ; column++)
        {
            PixelPacket *p = &amp;pixels[column + row * width];
            Color c = Color(p-&gt;red, p-&gt;green, p-&gt;blue);
            double i = c.scaleQuantumToDouble(c.intensity()) * 255;
            imgCache[column + row * width] = (char) i;
        }
    }
</pre>
<p>The above code snippet first reads the image data into *PixelPacket and the pixel buffer is then converted into a one channel buffer of chars (color value 0-255). Note that the range of the image data Magick++ reads in is between 0 and <strong><em>QuantumRange</em></strong>, which needs to be converted back to the range 0-255 accepted by the Ipp8u buffer. If other types of <strong><em>IPP</em></strong> image buffers are used (e.g. Ipp32f), this <strong><em>scaleQuantumToDouble()</em></strong> conversion may not be necessary. At the end, the image data is converted into the one dimensional array <strong><em>imgCache</em></strong> which can be used by <em>IPP</em> procedures.</p>
<p>Once we are in the <strong><em>IPP</em></strong> data domain, we can proceed with whatever processing we had in mind. Here I will show the code for edge detection using <a href="http://en.wikipedia.org/wiki/Canny_edge_detector">Canny algorithm</a>.</p>
<pre class="brush: cpp;">
    IppiSize orgImgSize = {width, height};
    IppiSize newImgSize = {width + 2, height + 2};

    Ipp8u *imgCache1 = ippiMalloc_8u_C1(width + 2,height + 2, &amp;stepByte);
    sts = ippiCopyReplicateBorder_8u_C1R(imgCache, width, orgImgSize,  imgCache1,
            width + 2 , newImgSize, 2, 2);
    IppiSize roi = {width, height};

    Ipp32f low=30.0f, high=100.0f;
    int size, size1, srcStep, dxStep, dyStep, dstStep;

    Ipp8u *src, *dst, *buffer;
    Ipp16s *dx, *dy;

    sts = ippiFilterSobelNegVertGetBufferSize_8u16s_C1R(
            roi, ippMskSize3x3, &amp;size);
    sts = ippiFilterSobelHorizGetBufferSize_8u16s_C1R(
            roi, ippMskSize3x3, &amp;size1);

    if (size&lt;size1) size=size1;
    ippiCannyGetSize(roi, &amp;size1);
    if (size&lt;size1) size=size1;

    buffer = ippsMalloc_8u(size);
    dx = ippsMalloc_16s(size);
    dy = ippsMalloc_16s(size);
    dst = ippsMalloc_8u(size);

    sts = ippiFilterSobelNegVertBorder_8u16s_C1R (
            imgCache1, width + 2 , dx, (width + 2) * 2 ,
            roi, ippMskSize3x3, ippBorderRepl, 0, buffer);
    sts = ippiFilterSobelHorizBorder_8u16s_C1R(
            imgCache1, width + 2, dy, (width + 2) *2 ,
            roi, ippMskSize3x3, ippBorderRepl, 0, buffer);
    sts = ippiCanny_16s8u_C1R(dx,
            (width + 2) * 2, dy, (width + 2) * 2,
            dst, width, roi, low, high, buffer);
</pre>
<p>The code shown above is adopted from Intel&#8217;s IPP manual for image and video processing (by default it is located at /opt/intel/ipp/<em>{version number}</em>/em64t/doc/ippiman.pdf).</p>
<p>Please pay special attention to how the original image is extended via <strong><em>ippiCopyReplicateBorder_8u_C1R</em></strong>. The image is extended by 2 pixels in each direction because the 3&#215;3 mask used for the filtering operation. I omitted error checking code for simplicity, but in general you need to check the return status of each ippi function call. When the call is successful, the status is <strong><em>ippStsNoErr</em></strong>. If you receive a value other than <strong><em>ippStsNoErr</em></strong> (e.g. <strong><em>ippStsStepErr</em></strong>) you will need to check your buffer size to make sure that they are adjusted according to the data type. For instance, in the code above <strong><em>dx</em> </strong>and <strong><em>dy</em></strong> are both 16bit signed integers and thus they both occupy two bytes each. </p>
<p>To save the result image, we convert the pixel buffer back to <strong><em>PixelPacket</em></strong> type. Again we need to convert the data in the buffer (<strong><em>Ipp8u</em></strong>) to the range accepted by Magick++ API.</p>
<pre class="brush: cpp;">
    for (unsigned int y = 0; y&lt; height ; y++)
    {
        for (unsigned int x = 0; x &lt; width; x++)
        {
            Color c;
            float clr = (float) dst[x + y * width] /255;
            float q = c.scaleDoubleToQuantum(clr);
            pixels[x+ y * width] = Color(q, q, q);
        }
    }

    view.sync();
    img.syncPixels();

    img.write(&quot;{Image File Name}&quot;);
</pre>
<p>We can also save the result data into a new image (instead of syncing the data back to the image object holding the original image data) using the code below:</p>
<pre class="brush: cpp;">
    Image img1(Geometry(width, height),&quot;white&quot;);

    for (unsigned int y = 0; y&lt; height ; y++)
    {
        for (unsigned int x = 0; x &lt; width; x++)
        {
            Color c;
            float clr = (float) dst[x + y * width] /255;
            float q = c.scaleDoubleToQuantum(clr);
            img1.pixelColor(x,y, Color(q, q, q));
        }
    }

    img1.write(&quot;{Image File Name}&quot;);
</pre>
<p>And finally the buffers used are freed.</p>
<pre class="brush: cpp;">
    ippiFree(imgCache);
    ippsFree(buffer);
</pre>
<p>The following images show Canny edge detector in action using the code in this article:</p>
<div id="attachment_876" class="wp-caption aligncenter" style="width: 573px"><a href="http://www.kerrywong.com/blog/wp-content/uploads/2009/03/test.jpg"><img src="http://www.kerrywong.com/blog/wp-content/uploads/2009/03/test.jpg" alt="Original" title="Original" width="563" height="422" class="size-full wp-image-876" /></a><p class="wp-caption-text">Original</p></div>
<div id="attachment_879" class="wp-caption aligncenter" style="width: 573px"><a href="http://www.kerrywong.com/blog/wp-content/uploads/2009/03/test_canny.png"><img src="http://www.kerrywong.com/blog/wp-content/uploads/2009/03/test_canny.png" alt="Canny Edge Detector applied" title="Canny Edge Detector applied" width="563" height="422" class="size-full wp-image-879" /></a><p class="wp-caption-text">Canny Edge Detector applied</p></div>
]]></content:encoded>
			<wfw:commentRss>http://www.kerrywong.com/2009/03/17/interfacing-ipp-with-magick/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>TBB Mandelbrot Set</title>
		<link>http://www.kerrywong.com/2008/09/13/tbb-mandelbrot-set/</link>
		<comments>http://www.kerrywong.com/2008/09/13/tbb-mandelbrot-set/#comments</comments>
		<pubDate>Sun, 14 Sep 2008 02:35:48 +0000</pubDate>
		<dc:creator>kwong</dc:creator>
				<category><![CDATA[Coding]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[Mandelbrot]]></category>
		<category><![CDATA[Multi-threading]]></category>
		<category><![CDATA[TBB]]></category>

		<guid isPermaLink="false">http://www.kerrywong.com/?p=352</guid>
		<description><![CDATA[In an earlier post, I created a simple prime finding program using Intel&#8217;s TBB (Thread Building Block). The main benefit of using TBB is that threading and thread synchronization mechanism are abstracted away within the TBB library so we do not need to deal with threads explicitly. Also, TBB is optimized for performance and scales [...]]]></description>
			<content:encoded><![CDATA[<p>In an <a href="/2008/06/22/a-simple-tbb-program-tbb-prime/">earlier post</a>, I created a simple prime finding program using Intel&#8217;s TBB (<a href="http://www.threadingbuildingblocks.org/">Thread Building Block</a>). The main benefit of using TBB is that threading and thread synchronization mechanism are abstracted away within the TBB library so we do not need to deal with threads explicitly. Also, TBB is optimized for performance and scales nicely as the number of processing unit increases.<span id="more-352"></span> In this post, I will show you how to create a <a href="http://en.wikipedia.org/wiki/Mandelbrot_set">Mandelbrot Set</a> generator using TBB and how to optimize the algorithm using loop unrolling.</p>
<p>The standard algorithm for generating Mandelbrot Set is extremely easy to adapt to using TBB. In fact the loops look almost identical to those in the single-threaded approach, except that the iterations are calculated within a 2D range block (<strong>blocked_range2d</strong>) instead of the entire two dimensional space.</p>
<div style="background: white none repeat scroll 0% 0%; font-family: Courier New; font-size: 10pt; color: black; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">void</span> <span style="color: blue; font-weight: bold;">operator</span><span style="color: rgb(128, 128, 192); font-weight: bold;">()</span> <span style="color: rgb(128, 128, 192); font-weight: bold;">(</span><span style="color: blue; font-weight: bold;">const</span> blocked_range2d<span style="color: rgb(128, 128, 192); font-weight: bold;">&lt;</span>size_t<span style="color: rgb(128, 128, 192); font-weight: bold;">&gt;</span> <span style="color: rgb(128, 128, 192); font-weight: bold;">&amp;</span>r<span style="color: rgb(128, 128, 192); font-weight: bold;">)</span> <span style="color: blue; font-weight: bold;">const</span></div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; <span style="color: rgb(128, 128, 192); font-weight: bold;">{</span></div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; drawing_area drawing<span style="color: rgb(128, 128, 192); font-weight: bold;">(</span><span style="color: teal;">0</span><span style="color: rgb(128, 128, 192); font-weight: bold;">,</span> <span style="color: teal;">0</span><span style="color: rgb(128, 128, 192); font-weight: bold;">,</span> screen_size<span style="color: rgb(128, 128, 192); font-weight: bold;">,</span>screen_size<span style="color: rgb(128, 128, 192); font-weight: bold;">);</span></div>
<div style="margin: 0px;">&nbsp;</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">for</span> <span style="color: rgb(128, 128, 192); font-weight: bold;">(</span>size_t x <span style="color: rgb(128, 128, 192); font-weight: bold;">=</span> r<span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>rows<span style="color: rgb(128, 128, 192); font-weight: bold;">().</span>begin<span style="color: rgb(128, 128, 192); font-weight: bold;">();</span> x <span style="color: rgb(128, 128, 192); font-weight: bold;">&lt;</span> r<span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>rows<span style="color: rgb(128, 128, 192); font-weight: bold;">().</span>end<span style="color: rgb(128, 128, 192); font-weight: bold;">();</span> x<span style="color: rgb(128, 128, 192); font-weight: bold;">++)</span></div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: rgb(128, 128, 192); font-weight: bold;">{</span></div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">for</span> <span style="color: rgb(128, 128, 192); font-weight: bold;">(</span>size_t y <span style="color: rgb(128, 128, 192); font-weight: bold;">=</span> r<span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>cols<span style="color: rgb(128, 128, 192); font-weight: bold;">().</span>begin<span style="color: rgb(128, 128, 192); font-weight: bold;">();</span> y <span style="color: rgb(128, 128, 192); font-weight: bold;">&lt;</span> r<span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>cols<span style="color: rgb(128, 128, 192); font-weight: bold;">().</span>end<span style="color: rgb(128, 128, 192); font-weight: bold;">();</span> y<span style="color: rgb(128, 128, 192); font-weight: bold;">++)</span></div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: rgb(128, 128, 192); font-weight: bold;">{</span></div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">float</span> zx <span style="color: rgb(128, 128, 192); font-weight: bold;">=</span> <span style="color: teal;">0</span><span style="color: rgb(128, 128, 192); font-weight: bold;">;</span></div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">float</span> zy <span style="color: rgb(128, 128, 192); font-weight: bold;">=</span> <span style="color: teal;">0</span><span style="color: rgb(128, 128, 192); font-weight: bold;">;</span></div>
<div style="margin: 0px;">&nbsp;</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">float</span> cx <span style="color: rgb(128, 128, 192); font-weight: bold;">=</span> <span style="color: rgb(128, 128, 192); font-weight: bold;">(</span><span style="color: blue; font-weight: bold;">float</span><span style="color: rgb(128, 128, 192); font-weight: bold;">)</span>x <span style="color: rgb(128, 128, 192); font-weight: bold;">/</span> <span style="color: rgb(128, 128, 192); font-weight: bold;">(</span><span style="color: blue; font-weight: bold;">float</span><span style="color: rgb(128, 128, 192); font-weight: bold;">)</span>screen_size <span style="color: rgb(128, 128, 192); font-weight: bold;">*</span> x_range <span style="color: rgb(128, 128, 192); font-weight: bold;">+</span> x_min<span style="color: rgb(128, 128, 192); font-weight: bold;">;</span></div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">float</span> cy <span style="color: rgb(128, 128, 192); font-weight: bold;">=</span> <span style="color: rgb(128, 128, 192); font-weight: bold;">(</span><span style="color: blue; font-weight: bold;">float</span><span style="color: rgb(128, 128, 192); font-weight: bold;">)</span>y <span style="color: rgb(128, 128, 192); font-weight: bold;">/</span> <span style="color: rgb(128, 128, 192); font-weight: bold;">(</span><span style="color: blue; font-weight: bold;">float</span><span style="color: rgb(128, 128, 192); font-weight: bold;">)</span>screen_size <span style="color: rgb(128, 128, 192); font-weight: bold;">*</span> y_range <span style="color: rgb(128, 128, 192); font-weight: bold;">+</span> y_min<span style="color: rgb(128, 128, 192); font-weight: bold;">;</span></div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">int</span> i <span style="color: rgb(128, 128, 192); font-weight: bold;">=</span> <span style="color: teal;">0</span><span style="color: rgb(128, 128, 192); font-weight: bold;">;</span></div>
<div style="margin: 0px;">&nbsp;</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">while</span> <span style="color: rgb(128, 128, 192); font-weight: bold;">(</span>zx <span style="color: rgb(128, 128, 192); font-weight: bold;">*</span> zx <span style="color: rgb(128, 128, 192); font-weight: bold;">+</span> zy <span style="color: rgb(128, 128, 192); font-weight: bold;">*</span> zy <span style="color: rgb(128, 128, 192); font-weight: bold;">&lt;=</span> <span style="color: teal;">4</span> <span style="color: rgb(128, 128, 192); font-weight: bold;">&amp;&amp;</span> i <span style="color: rgb(128, 128, 192); font-weight: bold;">&lt;</span> max_iteration<span style="color: rgb(128, 128, 192); font-weight: bold;">)</span></div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: rgb(128, 128, 192); font-weight: bold;">{</span></div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">float</span> xtemp <span style="color: rgb(128, 128, 192); font-weight: bold;">=</span> zx <span style="color: rgb(128, 128, 192); font-weight: bold;">*</span> zx <span style="color: rgb(128, 128, 192); font-weight: bold;">-</span> zy <span style="color: rgb(128, 128, 192); font-weight: bold;">*</span> zy <span style="color: rgb(128, 128, 192); font-weight: bold;">+</span> cx<span style="color: rgb(128, 128, 192); font-weight: bold;">;</span></div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; zy <span style="color: rgb(128, 128, 192); font-weight: bold;">=</span> <span style="color: teal;">2</span> <span style="color: rgb(128, 128, 192); font-weight: bold;">*</span> zx <span style="color: rgb(128, 128, 192); font-weight: bold;">*</span> zy <span style="color: rgb(128, 128, 192); font-weight: bold;">+</span> cy<span style="color: rgb(128, 128, 192); font-weight: bold;">;</span></div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; zx <span style="color: rgb(128, 128, 192); font-weight: bold;">=</span> xtemp<span style="color: rgb(128, 128, 192); font-weight: bold;">;</span></div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; i<span style="color: rgb(128, 128, 192); font-weight: bold;">++;</span></div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: rgb(128, 128, 192); font-weight: bold;">}</span></div>
<div style="margin: 0px;">&nbsp;</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">int</span> itr <span style="color: rgb(128, 128, 192); font-weight: bold;">=</span> i <span style="color: rgb(128, 128, 192); font-weight: bold;">%</span> <span style="color: teal;">255</span><span style="color: rgb(128, 128, 192); font-weight: bold;">;</span></div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; color_t c <span style="color: rgb(128, 128, 192); font-weight: bold;">=</span> itr <span style="color: rgb(128, 128, 192); font-weight: bold;">&lt;&lt;</span> <span style="color: teal;">16</span> <span style="color: rgb(128, 128, 192); font-weight: bold;">|</span> itr <span style="color: rgb(128, 128, 192); font-weight: bold;">&lt;&lt;</span> <span style="color: teal;">8</span> <span style="color: rgb(128, 128, 192); font-weight: bold;">|</span> itr<span style="color: rgb(128, 128, 192); font-weight: bold;">;</span></div>
<div style="margin: 0px;">&nbsp;</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; drawing<span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>set_pixel<span style="color: rgb(128, 128, 192); font-weight: bold;">(</span>x<span style="color: rgb(128, 128, 192); font-weight: bold;">,</span>y<span style="color: rgb(128, 128, 192); font-weight: bold;">,</span>c<span style="color: rgb(128, 128, 192); font-weight: bold;">);</span></div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: rgb(128, 128, 192); font-weight: bold;">}</span></div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: rgb(128, 128, 192); font-weight: bold;">}</span></div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; <span style="color: rgb(128, 128, 192); font-weight: bold;">}</span></div>
</div>
<p>Because xlib by itself is not thread-safe, special attention must be made when trying to update the display concurrently. One way to address this issue is to employee a shared memory region (<strong>X11/extensions/XShm.h</strong> and <strong>sys/shm.h</strong>), the display is first built in memory and then the shared memory is attached to the display. In my examples above I used code (<strong>video.h</strong>, <strong>xvideo.cpp</strong>) from the sample code that come with the TBB library, which uses the shared memory method I mentioned earlier to make the X11 calls thread-safe.</p>
<p>Many optimization methods can be used to further enhance the performance of the algorithm. One of the most efficient methods is to utilize SSE instructions found on all modern Intel processors (examples can be found here: <a href="http://softwarecommunity.intel.com/articles/eng/3426.htm">Using SSE3 Technology in Algorithms with Complex Arithmetic</a>). This approach however might be difficult to implement and debug since parallel data structures must be used in order to benefit from SSE instructions. Also, explicit assembly level coding makes porting code to other machine architectures a daunting task. Modern compilers can already take full advantage of the underlying machine architecture. For example, the gcc compiler (4.2.3) already generates SSE instructions for the code snippet above. While hand tweaking using SSE instructions might further improve the performance, we would certainly sacrifice code simplicity and portability.</p>
<p>The approach I am going to take to further optimize the code is to use loop unrolling. Since the inner loop of the standard algorithm is pretty short, unrolling the inner loop should lessen the burden of loop overhead and decrease the chances of stalling the pipeline (when branching must be predicted). So a high-level loop unrolling should be able to improve the performance.</p>
<p>Here is the code after the inner loop is unrolled:</p>
<div style="background: white none repeat scroll 0% 0%; font-family: Courier New; font-size: 10pt; color: black; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">void</span> <span style="color: blue; font-weight: bold;">operator</span><span style="color: rgb(128, 128, 192); font-weight: bold;">()</span> <span style="color: rgb(128, 128, 192); font-weight: bold;">(</span><span style="color: blue; font-weight: bold;">const</span> blocked_range2d<span style="color: rgb(128, 128, 192); font-weight: bold;">&lt;</span>size_t<span style="color: rgb(128, 128, 192); font-weight: bold;">&gt;</span> <span style="color: rgb(128, 128, 192); font-weight: bold;">&amp;</span>r<span style="color: rgb(128, 128, 192); font-weight: bold;">)</span> <span style="color: blue; font-weight: bold;">const</span></div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; <span style="color: rgb(128, 128, 192); font-weight: bold;">{</span></div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; drawing_area drawing<span style="color: rgb(128, 128, 192); font-weight: bold;">(</span><span style="color: teal;">0</span><span style="color: rgb(128, 128, 192); font-weight: bold;">,</span> <span style="color: teal;">0</span><span style="color: rgb(128, 128, 192); font-weight: bold;">,</span> screen_size<span style="color: rgb(128, 128, 192); font-weight: bold;">,</span>screen_size<span style="color: rgb(128, 128, 192); font-weight: bold;">);</span></div>
<div style="margin: 0px;">&nbsp;</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">for</span> <span style="color: rgb(128, 128, 192); font-weight: bold;">(</span>size_t x <span style="color: rgb(128, 128, 192); font-weight: bold;">=</span> r<span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>rows<span style="color: rgb(128, 128, 192); font-weight: bold;">().</span>begin<span style="color: rgb(128, 128, 192); font-weight: bold;">();</span> x <span style="color: rgb(128, 128, 192); font-weight: bold;">&lt;</span> r<span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>rows<span style="color: rgb(128, 128, 192); font-weight: bold;">().</span>end<span style="color: rgb(128, 128, 192); font-weight: bold;">();</span> x<span style="color: rgb(128, 128, 192); font-weight: bold;">+=</span><span style="color: teal;">2</span><span style="color: rgb(128, 128, 192); font-weight: bold;">)</span></div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: rgb(128, 128, 192); font-weight: bold;">{</span></div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">for</span> <span style="color: rgb(128, 128, 192); font-weight: bold;">(</span>size_t y <span style="color: rgb(128, 128, 192); font-weight: bold;">=</span> r<span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>cols<span style="color: rgb(128, 128, 192); font-weight: bold;">().</span>begin<span style="color: rgb(128, 128, 192); font-weight: bold;">();</span> y <span style="color: rgb(128, 128, 192); font-weight: bold;">&lt;</span> r<span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>cols<span style="color: rgb(128, 128, 192); font-weight: bold;">().</span>end<span style="color: rgb(128, 128, 192); font-weight: bold;">();</span> y<span style="color: rgb(128, 128, 192); font-weight: bold;">++)</span></div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: rgb(128, 128, 192); font-weight: bold;">{</span></div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">float</span> zx1 <span style="color: rgb(128, 128, 192); font-weight: bold;">=</span> <span style="color: teal;">0</span><span style="color: rgb(128, 128, 192); font-weight: bold;">;</span></div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">float</span> zx2 <span style="color: rgb(128, 128, 192); font-weight: bold;">=</span> <span style="color: teal;">0</span><span style="color: rgb(128, 128, 192); font-weight: bold;">;</span></div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">float</span> zy1 <span style="color: rgb(128, 128, 192); font-weight: bold;">=</span> <span style="color: teal;">0</span><span style="color: rgb(128, 128, 192); font-weight: bold;">;</span></div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">float</span> zy2 <span style="color: rgb(128, 128, 192); font-weight: bold;">=</span> <span style="color: teal;">0</span><span style="color: rgb(128, 128, 192); font-weight: bold;">;</span></div>
<div style="margin: 0px;">&nbsp;</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">float</span> cx1 <span style="color: rgb(128, 128, 192); font-weight: bold;">=</span> <span style="color: rgb(128, 128, 192); font-weight: bold;">(</span><span style="color: blue; font-weight: bold;">float</span><span style="color: rgb(128, 128, 192); font-weight: bold;">)</span>x <span style="color: rgb(128, 128, 192); font-weight: bold;">/</span> <span style="color: rgb(128, 128, 192); font-weight: bold;">(</span><span style="color: blue; font-weight: bold;">float</span><span style="color: rgb(128, 128, 192); font-weight: bold;">)</span>screen_size <span style="color: rgb(128, 128, 192); font-weight: bold;">*</span> x_range <span style="color: rgb(128, 128, 192); font-weight: bold;">+</span> x_min<span style="color: rgb(128, 128, 192); font-weight: bold;">;</span></div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">float</span> cx2 <span style="color: rgb(128, 128, 192); font-weight: bold;">=</span> <span style="color: rgb(128, 128, 192); font-weight: bold;">(</span><span style="color: blue; font-weight: bold;">float</span><span style="color: rgb(128, 128, 192); font-weight: bold;">)(</span>x <span style="color: rgb(128, 128, 192); font-weight: bold;">+</span> <span style="color: teal;">1</span><span style="color: rgb(128, 128, 192); font-weight: bold;">)</span> <span style="color: rgb(128, 128, 192); font-weight: bold;">/</span> <span style="color: rgb(128, 128, 192); font-weight: bold;">(</span><span style="color: blue; font-weight: bold;">float</span><span style="color: rgb(128, 128, 192); font-weight: bold;">)</span>screen_size <span style="color: rgb(128, 128, 192); font-weight: bold;">*</span> x_range <span style="color: rgb(128, 128, 192); font-weight: bold;">+</span> x_min<span style="color: rgb(128, 128, 192); font-weight: bold;">;</span></div>
<div style="margin: 0px;">&nbsp;</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">float</span> cy <span style="color: rgb(128, 128, 192); font-weight: bold;">=</span> <span style="color: rgb(128, 128, 192); font-weight: bold;">(</span><span style="color: blue; font-weight: bold;">float</span><span style="color: rgb(128, 128, 192); font-weight: bold;">)</span>y <span style="color: rgb(128, 128, 192); font-weight: bold;">/</span> <span style="color: rgb(128, 128, 192); font-weight: bold;">(</span><span style="color: blue; font-weight: bold;">float</span><span style="color: rgb(128, 128, 192); font-weight: bold;">)</span>screen_size <span style="color: rgb(128, 128, 192); font-weight: bold;">*</span> y_range <span style="color: rgb(128, 128, 192); font-weight: bold;">+</span> y_min<span style="color: rgb(128, 128, 192); font-weight: bold;">;</span></div>
<div style="margin: 0px;">&nbsp;</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">int</span> i1 <span style="color: rgb(128, 128, 192); font-weight: bold;">=</span> <span style="color: teal;">0</span><span style="color: rgb(128, 128, 192); font-weight: bold;">;</span></div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">int</span> i2 <span style="color: rgb(128, 128, 192); font-weight: bold;">=</span> <span style="color: teal;">0</span><span style="color: rgb(128, 128, 192); font-weight: bold;">;</span></div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">bool</span> loop_stop1 <span style="color: rgb(128, 128, 192); font-weight: bold;">=</span> <span style="color: blue; font-weight: bold;">false</span><span style="color: rgb(128, 128, 192); font-weight: bold;">;</span></div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">bool</span> loop_stop2 <span style="color: rgb(128, 128, 192); font-weight: bold;">=</span> <span style="color: blue; font-weight: bold;">false</span><span style="color: rgb(128, 128, 192); font-weight: bold;">;</span></div>
<div style="margin: 0px;">&nbsp;</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">while</span> <span style="color: rgb(128, 128, 192); font-weight: bold;">(</span> <span style="color: rgb(128, 128, 192); font-weight: bold;">!(</span>loop_stop1 <span style="color: rgb(128, 128, 192); font-weight: bold;">&amp;&amp;</span> loop_stop2<span style="color: rgb(128, 128, 192); font-weight: bold;">))</span></div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: rgb(128, 128, 192); font-weight: bold;">{</span></div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">float</span> xtemp1<span style="color: rgb(128, 128, 192); font-weight: bold;">;</span></div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">float</span> xtemp2<span style="color: rgb(128, 128, 192); font-weight: bold;">;</span></div>
<div style="margin: 0px;">&nbsp;</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">if</span> <span style="color: rgb(128, 128, 192); font-weight: bold;">((</span>zx1 <span style="color: rgb(128, 128, 192); font-weight: bold;">*</span> zx1 <span style="color: rgb(128, 128, 192); font-weight: bold;">+</span> zy1 <span style="color: rgb(128, 128, 192); font-weight: bold;">*</span> zy1 <span style="color: rgb(128, 128, 192); font-weight: bold;">&lt;=</span> <span style="color: teal;">4</span><span style="color: rgb(128, 128, 192); font-weight: bold;">)</span> <span style="color: rgb(128, 128, 192); font-weight: bold;">&amp;&amp;</span> <span style="color: rgb(128, 128, 192); font-weight: bold;">(</span>i1 <span style="color: rgb(128, 128, 192); font-weight: bold;">&lt;</span> max_iteration<span style="color: rgb(128, 128, 192); font-weight: bold;">))</span></div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: rgb(128, 128, 192); font-weight: bold;">{</span></div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; xtemp1 <span style="color: rgb(128, 128, 192); font-weight: bold;">=</span> zx1 <span style="color: rgb(128, 128, 192); font-weight: bold;">*</span> zx1 <span style="color: rgb(128, 128, 192); font-weight: bold;">-</span> zy1 <span style="color: rgb(128, 128, 192); font-weight: bold;">*</span> zy1 <span style="color: rgb(128, 128, 192); font-weight: bold;">+</span> cx1<span style="color: rgb(128, 128, 192); font-weight: bold;">;</span></div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; zy1 <span style="color: rgb(128, 128, 192); font-weight: bold;">=</span> <span style="color: teal;">2</span> <span style="color: rgb(128, 128, 192); font-weight: bold;">*</span> zx1 <span style="color: rgb(128, 128, 192); font-weight: bold;">*</span> zy1 <span style="color: rgb(128, 128, 192); font-weight: bold;">+</span> cy<span style="color: rgb(128, 128, 192); font-weight: bold;">;</span></div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; zx1 <span style="color: rgb(128, 128, 192); font-weight: bold;">=</span> xtemp1<span style="color: rgb(128, 128, 192); font-weight: bold;">;</span></div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; i1<span style="color: rgb(128, 128, 192); font-weight: bold;">++;</span></div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: rgb(128, 128, 192); font-weight: bold;">}</span></div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">else</span></div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: rgb(128, 128, 192); font-weight: bold;">{</span></div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; loop_stop1 <span style="color: rgb(128, 128, 192); font-weight: bold;">=</span> <span style="color: blue; font-weight: bold;">true</span><span style="color: rgb(128, 128, 192); font-weight: bold;">;</span></div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: rgb(128, 128, 192); font-weight: bold;">}</span></div>
<div style="margin: 0px;">&nbsp;</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">if</span> <span style="color: rgb(128, 128, 192); font-weight: bold;">((</span>zx2 <span style="color: rgb(128, 128, 192); font-weight: bold;">*</span> zx2 <span style="color: rgb(128, 128, 192); font-weight: bold;">+</span> zy2 <span style="color: rgb(128, 128, 192); font-weight: bold;">*</span> zy2 <span style="color: rgb(128, 128, 192); font-weight: bold;">&lt;=</span> <span style="color: teal;">4</span><span style="color: rgb(128, 128, 192); font-weight: bold;">)</span> <span style="color: rgb(128, 128, 192); font-weight: bold;">&amp;&amp;</span> <span style="color: rgb(128, 128, 192); font-weight: bold;">(</span>i2<span style="color: rgb(128, 128, 192); font-weight: bold;">&lt;</span> max_iteration<span style="color: rgb(128, 128, 192); font-weight: bold;">))</span></div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: rgb(128, 128, 192); font-weight: bold;">{</span></div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; xtemp2 <span style="color: rgb(128, 128, 192); font-weight: bold;">=</span> zx2 <span style="color: rgb(128, 128, 192); font-weight: bold;">*</span> zx2 <span style="color: rgb(128, 128, 192); font-weight: bold;">-</span> zy2 <span style="color: rgb(128, 128, 192); font-weight: bold;">*</span> zy2 <span style="color: rgb(128, 128, 192); font-weight: bold;">+</span> cx2<span style="color: rgb(128, 128, 192); font-weight: bold;">;</span></div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; zy2 <span style="color: rgb(128, 128, 192); font-weight: bold;">=</span> <span style="color: teal;">2</span> <span style="color: rgb(128, 128, 192); font-weight: bold;">*</span> zx2 <span style="color: rgb(128, 128, 192); font-weight: bold;">*</span> zy2 <span style="color: rgb(128, 128, 192); font-weight: bold;">+</span> cy<span style="color: rgb(128, 128, 192); font-weight: bold;">;</span></div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; zx2 <span style="color: rgb(128, 128, 192); font-weight: bold;">=</span> xtemp2<span style="color: rgb(128, 128, 192); font-weight: bold;">;</span></div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; i2<span style="color: rgb(128, 128, 192); font-weight: bold;">++;</span></div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: rgb(128, 128, 192); font-weight: bold;">}</span></div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">else</span></div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: rgb(128, 128, 192); font-weight: bold;">{</span></div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; loop_stop2 <span style="color: rgb(128, 128, 192); font-weight: bold;">=</span> <span style="color: blue; font-weight: bold;">true</span><span style="color: rgb(128, 128, 192); font-weight: bold;">;</span></div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: rgb(128, 128, 192); font-weight: bold;">}</span></div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: rgb(128, 128, 192); font-weight: bold;">}</span></div>
<div style="margin: 0px;">&nbsp;</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">int</span> itr <span style="color: rgb(128, 128, 192); font-weight: bold;">=</span> <span style="color: teal;">0</span><span style="color: rgb(128, 128, 192); font-weight: bold;">;</span></div>
<div style="margin: 0px;">&nbsp;</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; itr <span style="color: rgb(128, 128, 192); font-weight: bold;">=</span> <span style="color: rgb(128, 128, 192); font-weight: bold;">(</span>i1<span style="color: rgb(128, 128, 192); font-weight: bold;">)</span> <span style="color: rgb(128, 128, 192); font-weight: bold;">%</span> <span style="color: teal;">255</span><span style="color: rgb(128, 128, 192); font-weight: bold;">;</span></div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; color_t c <span style="color: rgb(128, 128, 192); font-weight: bold;">=</span> itr <span style="color: rgb(128, 128, 192); font-weight: bold;">&lt;&lt;</span> <span style="color: teal;">16</span> <span style="color: rgb(128, 128, 192); font-weight: bold;">|</span> itr <span style="color: rgb(128, 128, 192); font-weight: bold;">&lt;&lt;</span> <span style="color: teal;">8</span> <span style="color: rgb(128, 128, 192); font-weight: bold;">|</span> itr<span style="color: rgb(128, 128, 192); font-weight: bold;">;</span></div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; drawing<span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>set_pixel<span style="color: rgb(128, 128, 192); font-weight: bold;">(</span>x<span style="color: rgb(128, 128, 192); font-weight: bold;">,</span>y<span style="color: rgb(128, 128, 192); font-weight: bold;">,</span>c<span style="color: rgb(128, 128, 192); font-weight: bold;">);</span></div>
<div style="margin: 0px;">&nbsp;</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; itr <span style="color: rgb(128, 128, 192); font-weight: bold;">=</span> i2&nbsp; <span style="color: rgb(128, 128, 192); font-weight: bold;">%</span> <span style="color: teal;">255</span><span style="color: rgb(128, 128, 192); font-weight: bold;">;</span></div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; c <span style="color: rgb(128, 128, 192); font-weight: bold;">=</span> itr <span style="color: rgb(128, 128, 192); font-weight: bold;">&lt;&lt;</span> <span style="color: teal;">16</span> <span style="color: rgb(128, 128, 192); font-weight: bold;">|</span> itr <span style="color: rgb(128, 128, 192); font-weight: bold;">&lt;&lt;</span> <span style="color: teal;">8</span> <span style="color: rgb(128, 128, 192); font-weight: bold;">|</span> itr<span style="color: rgb(128, 128, 192); font-weight: bold;">;</span></div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; drawing<span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>set_pixel<span style="color: rgb(128, 128, 192); font-weight: bold;">(</span>x<span style="color: rgb(128, 128, 192); font-weight: bold;">+</span><span style="color: teal;">1</span><span style="color: rgb(128, 128, 192); font-weight: bold;">,</span>y<span style="color: rgb(128, 128, 192); font-weight: bold;">,</span>c<span style="color: rgb(128, 128, 192); font-weight: bold;">);</span></div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: rgb(128, 128, 192); font-weight: bold;">}</span></div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: rgb(128, 128, 192); font-weight: bold;">}</span></div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; <span style="color: rgb(128, 128, 192); font-weight: bold;">}</span></div>
</div>
<p>This code generates identical results as the code mentioned previously. As you can see, the inner loop is not unrolled to handle two data points at a time.</p>
<p>As it turned out, this algorithm runs almost twice as fast as the code mentioned earlier(280ms versus 510ms on Intel Q9450 @ 3.4GHz).</p>
<p align="center"><img alt="Mandelbrot Set" src="/blog/wp-content/uploads/2008/09/mandelbrot_tbb.jpg" /></p>
<p><strong>Source code</strong> for this article can be downloaded <a href="/blog/wp-content/uploads/2008/09/mandelbrot_tbb.zip">here</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.kerrywong.com/2008/09/13/tbb-mandelbrot-set/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>A Simple TBB Program: TBB Prime</title>
		<link>http://www.kerrywong.com/2008/06/22/a-simple-tbb-program-tbb-prime/</link>
		<comments>http://www.kerrywong.com/2008/06/22/a-simple-tbb-program-tbb-prime/#comments</comments>
		<pubDate>Sun, 22 Jun 2008 16:12:08 +0000</pubDate>
		<dc:creator>kwong</dc:creator>
				<category><![CDATA[Coding]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[Multi-threading]]></category>
		<category><![CDATA[TBB]]></category>

		<guid isPermaLink="false">http://www.kerrywong.com/2008/06/22/a-simple-tbb-program-tbb-prime/</guid>
		<description><![CDATA[I have been playing around with Intel&#8217;s Threading Building Block for a while and have started to really appreciate its simplicity and elegance: Instead of thinking in threads and thread synchronizations, one can just simply concentrate on the problem on the hand. Take finding prime numbers for example, while the problem itself (using the most [...]]]></description>
			<content:encoded><![CDATA[<p>I have been playing around with Intel&#8217;s <a href="http://www.threadingbuildingblocks.org/">Threading Building Block</a> for a while and have started to really appreciate its simplicity and elegance: Instead of thinking in threads and thread synchronizations, one can just simply concentrate on the problem on the hand.<span id="more-310"></span>  Take finding prime numbers for example, while the problem itself (using the most rudimentary algorithm) is quite simple, getting it to work in a multi-threaded fashion does take a little bit of work. In this particular example, the prime finding algorithm can be easily paralleled by utilizing threads and thread synchronization is almost a non-issue since the problem domain can be divided into totally disjoint regions, but in general dividing the problem domain into multiple sub-domains and performing load balancing among them could take significant work.  In the following example, I created two C++ classes that both find prime numbers for a given interval (since all prime numbers are odd numbers except 2, 2 is omitted in the calculates below), one sequential and the other parallel. In the main function, both methods are timed and the results are outputted.(download <a title="tbbprime.cpp" href="/blog/wp-content/uploads/2008/06/tbbprime.zip"> tbbprime.cpp</a>)</p>
<div style="background: white none repeat scroll 0% 0%; font-family: Courier New; font-size: 10pt; color: black; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">
<div style="margin: 0px;"><span style="color: rgb(0, 128, 192); font-weight: bold;">#include</span> <span style="color: rgb(163, 21, 21); font-weight: bold;">&lt;stdio.h&gt;</span></div>
<div style="margin: 0px;"><span style="color: rgb(0, 128, 192); font-weight: bold;">#include</span> <span style="color: rgb(163, 21, 21); font-weight: bold;">&lt;stdlib.h&gt;</span></div>
<div style="margin: 0px;">&nbsp;</div>
<div style="margin: 0px;"><span style="color: rgb(0, 128, 192); font-weight: bold;">#include</span> <span style="color: rgb(163, 21, 21); font-weight: bold;">&lt;iostream&gt;</span></div>
<div style="margin: 0px;"><span style="color: rgb(0, 128, 192); font-weight: bold;">#include</span> <span style="color: rgb(163, 21, 21); font-weight: bold;">&lt;iomanip&gt;</span></div>
<div style="margin: 0px;"><span style="color: rgb(0, 128, 192); font-weight: bold;">#include</span> <span style="color: rgb(163, 21, 21); font-weight: bold;">&lt;math.h&gt;</span></div>
<div style="margin: 0px;">&nbsp;</div>
<div style="margin: 0px;"><span style="color: rgb(0, 128, 192); font-weight: bold;">#include</span> <span style="color: rgb(163, 21, 21); font-weight: bold;">&quot;tbb/task_scheduler_init.h&quot;</span></div>
<div style="margin: 0px;"><span style="color: rgb(0, 128, 192); font-weight: bold;">#include</span> <span style="color: rgb(163, 21, 21); font-weight: bold;">&quot;tbb/tick_count.h&quot;</span></div>
<div style="margin: 0px;"><span style="color: rgb(0, 128, 192); font-weight: bold;">#include</span> <span style="color: rgb(163, 21, 21); font-weight: bold;">&quot;tbb/blocked_range.h&quot;</span></div>
<div style="margin: 0px;"><span style="color: rgb(0, 128, 192); font-weight: bold;">#include</span> <span style="color: rgb(163, 21, 21); font-weight: bold;">&quot;tbb/parallel_for.h&quot;</span></div>
<div style="margin: 0px;"><span style="color: rgb(0, 128, 192); font-weight: bold;">#include</span> <span style="color: rgb(163, 21, 21); font-weight: bold;">&quot;tbb/partitioner.h&quot;</span></div>
<div style="margin: 0px;">&nbsp;</div>
<div style="margin: 0px;"><span style="color: blue; font-weight: bold;">using</span> <span style="color: blue; font-weight: bold;">namespace</span> std<span style="color: rgb(128, 128, 192); font-weight: bold;">;</span></div>
<div style="margin: 0px;"><span style="color: blue; font-weight: bold;">using</span> <span style="color: blue; font-weight: bold;">namespace</span> tbb<span style="color: rgb(128, 128, 192); font-weight: bold;">;</span></div>
<div style="margin: 0px;">&nbsp;</div>
<div style="margin: 0px;"><span style="color: blue; font-weight: bold;">static</span> <span style="color: blue; font-weight: bold;">const</span> <span style="color: blue; font-weight: bold;">int</span> MAX_SIZE <span style="color: rgb(128, 128, 192); font-weight: bold;">=</span> <span style="color: teal;">1000000</span><span style="color: rgb(128, 128, 192); font-weight: bold;">;</span></div>
<div style="margin: 0px;">&nbsp;</div>
<div style="margin: 0px;"><span style="color: blue; font-weight: bold;">class</span> prime_single_thread</div>
<div style="margin: 0px;"><span style="color: rgb(128, 128, 192); font-weight: bold;">{</span></div>
<div style="margin: 0px;"><span style="color: blue; font-weight: bold;">public</span><span style="color: rgb(128, 128, 192); font-weight: bold;">:</span></div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">void</span> run<span style="color: rgb(128, 128, 192); font-weight: bold;">(</span><span style="color: blue; font-weight: bold;">const</span> blocked_range<span style="color: rgb(128, 128, 192); font-weight: bold;">&lt;</span>size_t<span style="color: rgb(128, 128, 192); font-weight: bold;">&gt;</span> <span style="color: rgb(128, 128, 192); font-weight: bold;">&amp;</span>r<span style="color: rgb(128, 128, 192); font-weight: bold;">)</span></div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; <span style="color: rgb(128, 128, 192); font-weight: bold;">{</span></div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">bool</span> is_prime <span style="color: rgb(128, 128, 192); font-weight: bold;">=</span> <span style="color: blue; font-weight: bold;">false</span><span style="color: rgb(128, 128, 192); font-weight: bold;">;</span></div>
<div style="margin: 0px;">&nbsp;</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">for</span> <span style="color: rgb(128, 128, 192); font-weight: bold;">(</span>size_t x <span style="color: rgb(128, 128, 192); font-weight: bold;">=</span> r<span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>begin<span style="color: rgb(128, 128, 192); font-weight: bold;">();</span> x <span style="color: rgb(128, 128, 192); font-weight: bold;">&lt;</span> r<span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>end<span style="color: rgb(128, 128, 192); font-weight: bold;">();</span> x<span style="color: rgb(128, 128, 192); font-weight: bold;">+=</span><span style="color: teal;">2</span><span style="color: rgb(128, 128, 192); font-weight: bold;">)</span></div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: rgb(128, 128, 192); font-weight: bold;">{</span></div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; is_prime <span style="color: rgb(128, 128, 192); font-weight: bold;">=</span> <span style="color: blue; font-weight: bold;">true</span><span style="color: rgb(128, 128, 192); font-weight: bold;">;</span></div>
<div style="margin: 0px;">&nbsp;</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">for</span> <span style="color: rgb(128, 128, 192); font-weight: bold;">(</span><span style="color: blue; font-weight: bold;">int</span> i <span style="color: rgb(128, 128, 192); font-weight: bold;">=</span> <span style="color: teal;">3</span><span style="color: rgb(128, 128, 192); font-weight: bold;">;</span> i <span style="color: rgb(128, 128, 192); font-weight: bold;">&lt;=</span> sqrt<span style="color: rgb(128, 128, 192); font-weight: bold;">((</span><span style="color: blue; font-weight: bold;">double</span><span style="color: rgb(128, 128, 192); font-weight: bold;">)</span> x<span style="color: rgb(128, 128, 192); font-weight: bold;">);</span> i<span style="color: rgb(128, 128, 192); font-weight: bold;">+=</span><span style="color: teal;">2</span><span style="color: rgb(128, 128, 192); font-weight: bold;">)</span></div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: rgb(128, 128, 192); font-weight: bold;">{</span></div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">if</span> <span style="color: rgb(128, 128, 192); font-weight: bold;">(</span>x <span style="color: rgb(128, 128, 192); font-weight: bold;">%</span> i <span style="color: rgb(128, 128, 192); font-weight: bold;">==</span> <span style="color: teal;">0</span><span style="color: rgb(128, 128, 192); font-weight: bold;">)</span></div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: rgb(128, 128, 192); font-weight: bold;">{</span></div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; is_prime <span style="color: rgb(128, 128, 192); font-weight: bold;">=</span> <span style="color: blue; font-weight: bold;">false</span><span style="color: rgb(128, 128, 192); font-weight: bold;">;</span></div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">continue</span><span style="color: rgb(128, 128, 192); font-weight: bold;">;</span></div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: rgb(128, 128, 192); font-weight: bold;">}</span></div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: rgb(128, 128, 192); font-weight: bold;">}</span></div>
<div style="margin: 0px;">&nbsp;</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: green;">// Output prime numbers:</span></div>
<div style="margin: 0px;"><span style="color: green;">//&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; if (is_prime)</span></div>
<div style="margin: 0px;"><span style="color: green;">//&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</span></div>
<div style="margin: 0px;"><span style="color: green;">//&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; cout &lt;&lt; x &lt;&lt; endl;</span></div>
<div style="margin: 0px;"><span style="color: green;">//&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</span></div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: rgb(128, 128, 192); font-weight: bold;">}</span></div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; <span style="color: rgb(128, 128, 192); font-weight: bold;">}</span></div>
<div style="margin: 0px;"><span style="color: rgb(128, 128, 192); font-weight: bold;">};</span></div>
<div style="margin: 0px;">&nbsp;</div>
<div style="margin: 0px;"><span style="color: blue; font-weight: bold;">class</span> prime_tbb</div>
<div style="margin: 0px;"><span style="color: rgb(128, 128, 192); font-weight: bold;">{</span></div>
<div style="margin: 0px;"><span style="color: blue; font-weight: bold;">public</span><span style="color: rgb(128, 128, 192); font-weight: bold;">:</span></div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">void</span> test_prime<span style="color: rgb(128, 128, 192); font-weight: bold;">(</span><span style="color: blue; font-weight: bold;">int</span> num<span style="color: rgb(128, 128, 192); font-weight: bold;">)</span> <span style="color: blue; font-weight: bold;">const</span></div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; <span style="color: rgb(128, 128, 192); font-weight: bold;">{</span></div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">bool</span> is_prime <span style="color: rgb(128, 128, 192); font-weight: bold;">=</span> <span style="color: blue; font-weight: bold;">true</span><span style="color: rgb(128, 128, 192); font-weight: bold;">;</span></div>
<div style="margin: 0px;">&nbsp;</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">for</span> <span style="color: rgb(128, 128, 192); font-weight: bold;">(</span><span style="color: blue; font-weight: bold;">int</span> i <span style="color: rgb(128, 128, 192); font-weight: bold;">=</span> <span style="color: teal;">3</span><span style="color: rgb(128, 128, 192); font-weight: bold;">;</span> i <span style="color: rgb(128, 128, 192); font-weight: bold;">&lt;=</span> sqrt<span style="color: rgb(128, 128, 192); font-weight: bold;">((</span><span style="color: blue; font-weight: bold;">double</span><span style="color: rgb(128, 128, 192); font-weight: bold;">)</span> num<span style="color: rgb(128, 128, 192); font-weight: bold;">);</span> i<span style="color: rgb(128, 128, 192); font-weight: bold;">+=</span><span style="color: teal;">2</span><span style="color: rgb(128, 128, 192); font-weight: bold;">)</span></div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: rgb(128, 128, 192); font-weight: bold;">{</span></div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">if</span> <span style="color: rgb(128, 128, 192); font-weight: bold;">(</span>num <span style="color: rgb(128, 128, 192); font-weight: bold;">%</span> i <span style="color: rgb(128, 128, 192); font-weight: bold;">==</span> <span style="color: teal;">0</span><span style="color: rgb(128, 128, 192); font-weight: bold;">)</span></div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: rgb(128, 128, 192); font-weight: bold;">{</span></div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; is_prime <span style="color: rgb(128, 128, 192); font-weight: bold;">=</span> <span style="color: blue; font-weight: bold;">false</span><span style="color: rgb(128, 128, 192); font-weight: bold;">;</span></div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">continue</span><span style="color: rgb(128, 128, 192); font-weight: bold;">;</span></div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: rgb(128, 128, 192); font-weight: bold;">}</span></div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: rgb(128, 128, 192); font-weight: bold;">}</span></div>
<div style="margin: 0px;">&nbsp;</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: green;">// Output prime numbers:</span></div>
<div style="margin: 0px;"><span style="color: green;">//&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; if (is_prime)</span></div>
<div style="margin: 0px;"><span style="color: green;">//&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</span></div>
<div style="margin: 0px;"><span style="color: green;">//&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; cout &lt;&lt; num &lt;&lt; endl;</span></div>
<div style="margin: 0px;"><span style="color: green;">//&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</span></div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; <span style="color: rgb(128, 128, 192); font-weight: bold;">}</span></div>
<div style="margin: 0px;">&nbsp;</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">void</span> <span style="color: blue; font-weight: bold;">operator</span><span style="color: rgb(128, 128, 192); font-weight: bold;">()</span> <span style="color: rgb(128, 128, 192); font-weight: bold;">(</span><span style="color: blue; font-weight: bold;">const</span> blocked_range<span style="color: rgb(128, 128, 192); font-weight: bold;">&lt;</span>size_t<span style="color: rgb(128, 128, 192); font-weight: bold;">&gt;</span> <span style="color: rgb(128, 128, 192); font-weight: bold;">&amp;</span>r<span style="color: rgb(128, 128, 192); font-weight: bold;">)</span> <span style="color: blue; font-weight: bold;">const</span></div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; <span style="color: rgb(128, 128, 192); font-weight: bold;">{</span></div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">for</span> <span style="color: rgb(128, 128, 192); font-weight: bold;">(</span>size_t i <span style="color: rgb(128, 128, 192); font-weight: bold;">=</span> r<span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>begin<span style="color: rgb(128, 128, 192); font-weight: bold;">();</span> i <span style="color: rgb(128, 128, 192); font-weight: bold;">&lt;</span> r<span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>end<span style="color: rgb(128, 128, 192); font-weight: bold;">();</span> i<span style="color: rgb(128, 128, 192); font-weight: bold;">+=</span><span style="color: teal;">2</span><span style="color: rgb(128, 128, 192); font-weight: bold;">)</span></div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: rgb(128, 128, 192); font-weight: bold;">{</span></div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; test_prime<span style="color: rgb(128, 128, 192); font-weight: bold;">(</span>i<span style="color: rgb(128, 128, 192); font-weight: bold;">);</span></div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: rgb(128, 128, 192); font-weight: bold;">}</span></div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; <span style="color: rgb(128, 128, 192); font-weight: bold;">}</span></div>
<div style="margin: 0px;">&nbsp;</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">void</span> run<span style="color: rgb(128, 128, 192); font-weight: bold;">(</span>blocked_range<span style="color: rgb(128, 128, 192); font-weight: bold;">&lt;</span>size_t<span style="color: rgb(128, 128, 192); font-weight: bold;">&gt;</span> r<span style="color: rgb(128, 128, 192); font-weight: bold;">)</span></div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; <span style="color: rgb(128, 128, 192); font-weight: bold;">{</span></div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; prime_tbb prime_tbb<span style="color: rgb(128, 128, 192); font-weight: bold;">;</span></div>
<div style="margin: 0px;">&nbsp;</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; parallel_for<span style="color: rgb(128, 128, 192); font-weight: bold;">(</span>r<span style="color: rgb(128, 128, 192); font-weight: bold;">,</span> prime_tbb<span style="color: rgb(128, 128, 192); font-weight: bold;">);</span></div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; <span style="color: rgb(128, 128, 192); font-weight: bold;">}</span></div>
<div style="margin: 0px;">&nbsp;</div>
<div style="margin: 0px;"><span style="color: rgb(128, 128, 192); font-weight: bold;">};</span></div>
<div style="margin: 0px;">&nbsp;</div>
<div style="margin: 0px;"><span style="color: blue; font-weight: bold;">int</span> main<span style="color: rgb(128, 128, 192); font-weight: bold;">()</span></div>
<div style="margin: 0px;"><span style="color: rgb(128, 128, 192); font-weight: bold;">{</span></div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; task_scheduler_init init<span style="color: rgb(128, 128, 192); font-weight: bold;">;</span></div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">static</span> tick_count t_start<span style="color: rgb(128, 128, 192); font-weight: bold;">,</span> t_end<span style="color: rgb(128, 128, 192); font-weight: bold;">;</span></div>
<div style="margin: 0px;">&nbsp;</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; prime_single_thread p1<span style="color: rgb(128, 128, 192); font-weight: bold;">;</span></div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; prime_tbb p2<span style="color: rgb(128, 128, 192); font-weight: bold;">;</span></div>
<div style="margin: 0px;">&nbsp;</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; cout<span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>setf<span style="color: rgb(128, 128, 192); font-weight: bold;">(</span>ios<span style="color: rgb(128, 128, 192); font-weight: bold;">::</span>fixed<span style="color: rgb(128, 128, 192); font-weight: bold;">);</span></div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; cout<span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>setf<span style="color: rgb(128, 128, 192); font-weight: bold;">(</span>ios<span style="color: rgb(128, 128, 192); font-weight: bold;">::</span>showpoint<span style="color: rgb(128, 128, 192); font-weight: bold;">);</span></div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; cout<span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>precision<span style="color: rgb(128, 128, 192); font-weight: bold;">(</span><span style="color: teal;">2</span><span style="color: rgb(128, 128, 192); font-weight: bold;">);</span></div>
<div style="margin: 0px;">&nbsp;</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; <span style="color: green;">//starting from 3, with a granularity of 100.</span></div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; blocked_range<span style="color: rgb(128, 128, 192); font-weight: bold;">&lt;</span>size_t<span style="color: rgb(128, 128, 192); font-weight: bold;">&gt;</span> r<span style="color: rgb(128, 128, 192); font-weight: bold;">(</span><span style="color: teal;">3</span><span style="color: rgb(128, 128, 192); font-weight: bold;">,</span> MAX_SIZE<span style="color: rgb(128, 128, 192); font-weight: bold;">,</span> <span style="color: teal;">100</span><span style="color: rgb(128, 128, 192); font-weight: bold;">);</span></div>
<div style="margin: 0px;">&nbsp;</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; t_start <span style="color: rgb(128, 128, 192); font-weight: bold;">=</span> tick_count<span style="color: rgb(128, 128, 192); font-weight: bold;">::</span>now<span style="color: rgb(128, 128, 192); font-weight: bold;">();</span></div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; p1<span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>run<span style="color: rgb(128, 128, 192); font-weight: bold;">(</span>r<span style="color: rgb(128, 128, 192); font-weight: bold;">);</span></div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; t_end <span style="color: rgb(128, 128, 192); font-weight: bold;">=</span> tick_count<span style="color: rgb(128, 128, 192); font-weight: bold;">::</span>now<span style="color: rgb(128, 128, 192); font-weight: bold;">();</span></div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; cout <span style="color: rgb(128, 128, 192); font-weight: bold;">&lt;&lt;</span> <span style="color: rgb(128, 128, 192); font-weight: bold;">(</span>t_end <span style="color: rgb(128, 128, 192); font-weight: bold;">-</span> t_start<span style="color: rgb(128, 128, 192); font-weight: bold;">).</span>seconds<span style="color: rgb(128, 128, 192); font-weight: bold;">()</span> <span style="color: rgb(128, 128, 192); font-weight: bold;">*</span> <span style="color: teal;">1000</span> <span style="color: rgb(128, 128, 192); font-weight: bold;">&lt;&lt;</span> <span style="color: rgb(163, 21, 21); font-weight: bold;">&quot; ms&quot;</span> <span style="color: rgb(128, 128, 192); font-weight: bold;">&lt;&lt;</span> endl<span style="color: rgb(128, 128, 192); font-weight: bold;">;</span></div>
<div style="margin: 0px;">&nbsp;</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; t_start <span style="color: rgb(128, 128, 192); font-weight: bold;">=</span> tick_count<span style="color: rgb(128, 128, 192); font-weight: bold;">::</span>now<span style="color: rgb(128, 128, 192); font-weight: bold;">();</span></div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; p2<span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>run<span style="color: rgb(128, 128, 192); font-weight: bold;">(</span>r<span style="color: rgb(128, 128, 192); font-weight: bold;">);</span></div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; t_end <span style="color: rgb(128, 128, 192); font-weight: bold;">=</span> tick_count<span style="color: rgb(128, 128, 192); font-weight: bold;">::</span>now<span style="color: rgb(128, 128, 192); font-weight: bold;">();</span></div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; cout <span style="color: rgb(128, 128, 192); font-weight: bold;">&lt;&lt;</span> <span style="color: rgb(128, 128, 192); font-weight: bold;">(</span>t_end <span style="color: rgb(128, 128, 192); font-weight: bold;">-</span> t_start<span style="color: rgb(128, 128, 192); font-weight: bold;">).</span>seconds<span style="color: rgb(128, 128, 192); font-weight: bold;">()</span> <span style="color: rgb(128, 128, 192); font-weight: bold;">*</span> <span style="color: teal;">1000</span> <span style="color: rgb(128, 128, 192); font-weight: bold;">&lt;&lt;</span> <span style="color: rgb(163, 21, 21); font-weight: bold;">&quot; ms&quot;</span> <span style="color: rgb(128, 128, 192); font-weight: bold;">&lt;&lt;</span> endl<span style="color: rgb(128, 128, 192); font-weight: bold;">;</span></div>
<div style="margin: 0px;">&nbsp;</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">return</span> <span style="color: teal;">0</span><span style="color: rgb(128, 128, 192); font-weight: bold;">;</span></div>
<div style="margin: 0px;"><span style="color: rgb(128, 128, 192); font-weight: bold;">}</span></div>
</div>
<div style="background: white none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial; font-family: Courier New; font-size: 10pt; color: black;">&nbsp;</div>
<p>For a very large interval (e.g. 3~1,000,000), the TBB version of the prime program achieved a 4x speed up given a reasonably large grain size (e.g. 100). Smaller grain size resulted in slightly more overhead.  On a quad-core machine (Q9450 @ 3.2GHz), it took 217.83 ms for the single threaded routine to find all the prime numbers within 1,000,000, whereas it only took 58.32 ms for the TBB version, which runs roughly four times as fast. The TBB framework took care of dividing the task according to the number of processors automatically.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.kerrywong.com/2008/06/22/a-simple-tbb-program-tbb-prime/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Hyper-Threading and Dual Core Performance Comparison for Computational Intensive Applications &#8211; Update</title>
		<link>http://www.kerrywong.com/2007/05/22/hyper-threading-and-dual-core-performance-comparison-for-computational-intensive-applications-update/</link>
		<comments>http://www.kerrywong.com/2007/05/22/hyper-threading-and-dual-core-performance-comparison-for-computational-intensive-applications-update/#comments</comments>
		<pubDate>Wed, 23 May 2007 00:29:36 +0000</pubDate>
		<dc:creator>kwong</dc:creator>
				<category><![CDATA[Coding]]></category>
		<category><![CDATA[Technology]]></category>
		<category><![CDATA[C Sharp (C#)]]></category>
		<category><![CDATA[Multi-threading]]></category>
		<category><![CDATA[Performance]]></category>

		<guid isPermaLink="false">http://dimension/2007/05/22/hyper-threading-and-dual-core-performance-comparison-for-computational-intensive-applications-update/</guid>
		<description><![CDATA[In a post (Hyper-Threading and Dual Core Performance Comparison for Computational Intensive Applications) I wrote at the end of last year, I compared multi-threaded scientific application performance of a Pentium 4 processor with hyper-threading enabled and a Pentium D processor, and concluded that for multi-threaded scientific applications, hyper-threaded processor helped little in terms of application [...]]]></description>
			<content:encoded><![CDATA[<p class="MsoNormal"><span style="font-size: 10pt; font-family: Verdana;">In a post (<a href="/2006/12/26/hyper-threading-and-dual-core-performance-comparison-for-computational-intensive-applications/">Hyper-Threading and Dual Core Performance Comparison for Computational Intensive Applications</a>) I wrote at the end of last year, I compared multi-threaded scientific application performance of a Pentium 4 processor with hyper-threading enabled and a Pentium D processor, and concluded that for multi-threaded scientific applications, hyper-threaded processor helped little in terms of application performance.</span><span id="more-167"></span></p>
<p class="MsoNormal"><span style="font-size: 10pt; font-family: Verdana;"><o:p></o:p>As new processors roll out, new benchmarks tend to only capture the performance of that specific processor family and rarely compare the performance between different generations of processors. From many sources and the readily available data, we know that in terms of multi-media and gaming application which heavily rely on SSE* instructions, the new Core family processors out performs the old Pentium processors by leaps and bounds (The new Core architecture has made significant improvement over its Pentium predecessors). But there is very little information on how scientific applications might have benefited from the enhanced architecture.<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size: 10pt; font-family: Verdana;"><o:p></o:p>I recently just got a new ThinkPad T60 laptop at work (it has a Core Duo T2400 1.83 GHz processor), so I decided to run the same benchmark program I created in <a href="/2006/12/26/hyper-threading-and-dual-core-performance-comparison-for-computational-intensive-applications/">my earlier post</a> and see how the numbers turn out.<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size: 10pt; font-family: Verdana;"><o:p></o:p>And here are the results for the total run time when running 1 to 4 threads:<o:p></o:p></span></p>
<p style="margin-left: 0.5in;" class="MsoNormal"><span style="font-size: 10pt; font-family: Verdana;"><o:p></o:p>Time (1 thread) = 1548.6 ms<o:p></o:p> Time (2 threads)= 784.8 ms<o:p></o:p> Time (3 threads)= 785.3 ms<o:p></o:p> Time (4 threads)= 788.1 ms<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size: 10pt; font-family: Verdana;">In comparison, here are the results for the same benchmark program when running on Pentium 4 3GHz processor:<o:p></o:p></span></p>
<p style="margin-left: 0.5in;" class="MsoNormal"><span style="font-size: 10pt; font-family: Verdana;"><o:p></o:p>Time (1 thread) = 756.2 ms<o:p></o:p> Time (2 threads)= 754.7 ms<o:p></o:p> Time (3 threads)= 769.3 ms<o:p></o:p> Time (4 threads)= 772.5 ms<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size: 10pt; font-family: Verdana;"><o:p></o:p>And Pentium D 2.8GHz processor:<o:p></o:p></span></p>
<p style="margin-left: 0.5in;" class="MsoNormal"><span style="font-size: 10pt; font-family: Verdana;"><o:p></o:p>Time (1 thread) = 931.6 ms<o:p></o:p> Time (2 threads)= 469.4 ms<o:p></o:p> Time (3 threads)= 466.9 ms<o:p></o:p> Time (4 threads)= 485.8 ms<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size: 10pt; font-family: Verdana;"><o:p></o:p>As you can see, the performance of Core Duo is not that impressive for scientific calculations even if the clock speeds were the same. And the Core Duo T2400 mobile processor is probably a little slower than the Pentium D processor with the same clock speed. <o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size: 10pt; font-family: Verdana;"><o:p></o:p>According to an article <a href="http://www.anandtech.com/cpuchipsets/showdoc.aspx?i=2808">Mobile CPU Wars: Core 2 Duo vs. Core Duo</a> on AnandTech, the new Core 2 Duo processor is roughly 10% faster than Core Duo processor under the same clock speed. Of course the mobile version of Core Duo or Core 2 Duo are running at a lower bus speed (667MHz) compared to their desktop counterparts (1066MHz) so from what we&rsquo;ve seen the performance for scientific calculations on Core 2 Duo processors are probably only marginally better then the current Pentium D (in multi-threaded scenario) or Pentium (in single-threaded scenario) processors.<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size: 10pt; font-family: Verdana;"><o:p></o:p>This is not all that surprising as the simple benchmark application I created is so small in size that the working set is likely to fit in Pentium&rsquo;s much smaller on-die cache, so the extra L1/L2 cache memory found in Core series does not provide any significant advantage here. Also, the calculation is somewhat predictable in our simple program so the inefficiencies in the deep pipe-lined Pentium processors do not signify. Further, we did not utilize the new SSE* instructions either.<o:p></o:p></span></p>
<p><span style="font-size: 10pt; font-family: Verdana;"><o:p></o:p>Nevertheless, it is clear that unless we consciously utilize the new instruction sets found in Core serious processors and have a large working set to work with, the speed advantage of the new processors are not significant compared to Pentium processors.</span></p>
]]></content:encoded>
			<wfw:commentRss>http://www.kerrywong.com/2007/05/22/hyper-threading-and-dual-core-performance-comparison-for-computational-intensive-applications-update/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Hyper-Threading and Dual Core Performance Comparison for Computational Intensive Applications</title>
		<link>http://www.kerrywong.com/2006/12/26/hyper-threading-and-dual-core-performance-comparison-for-computational-intensive-applications/</link>
		<comments>http://www.kerrywong.com/2006/12/26/hyper-threading-and-dual-core-performance-comparison-for-computational-intensive-applications/#comments</comments>
		<pubDate>Tue, 26 Dec 2006 20:36:22 +0000</pubDate>
		<dc:creator>kwong</dc:creator>
				<category><![CDATA[Coding]]></category>
		<category><![CDATA[Technology]]></category>
		<category><![CDATA[Multi-threading]]></category>
		<category><![CDATA[Performance]]></category>

		<guid isPermaLink="false">http://dimension/2006/12/26/hyper-threading-and-dual-core-performance-comparison-for-computational-intensive-applications/</guid>
		<description><![CDATA[Download MPThreadBench.zip There is no doubt that Hyper-Threading can, under certain circumstances, boost application performance. The performance gain is highly dependent on application type, and according to Intel, this performance gain is at an average of 15-30%. But for computational intensive multi-threaded applications, Hyper-Threading does not provide much benefit however. Here I will show some [...]]]></description>
			<content:encoded><![CDATA[<p><span style="font-size: 10pt; font-family: Verdana;">Download </span><a title="MPThreadBench.zip" href="/blog/wp-content/uploads/2007/09/mpthreadbench.zip">MPThreadBench.zip</a></p>
<p><span style="font-size: 10pt; font-family: Verdana;">There is no doubt that Hyper-Threading can, under certain circumstances, boost application performance. <span id="more-128"></span>The performance gain is highly dependent on application type, and according to Intel, this performance gain is at an average of 15-30%. <o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size: 10pt; font-family: Verdana;"><o:p></o:p>But for computational intensive multi-threaded applications, Hyper-Threading does not provide much benefit however. Here I will show some benchmarks that support this assertion.</span></p>
<p class="MsoNormal"><strong>The Benchmark Program</strong></p>
<p><span style="font-size: 10pt; font-family: Verdana;">I created a very simple benchmark program using C#</span><span style="font-size: 10pt; font-family: Verdana;"> which generates prime numbers for a given range and for a user specified number of threads. </span><span style="font-size: 10pt; font-family: Verdana;"> The main program creates as many threads as the user specified and each threads computes prime numbers within the given range. The calculation for each thread is interleaved so that each thread generates a unique subset of the prime numbers within the given interval.<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size: 10pt; font-family: Verdana;"><o:p></o:p>The main function is listed here:</span></p>
<p class="MsoNormal"><span style="font-size: 10pt; font-family: 'Courier New';"><span>        </span><strong><span style="color: blue;">public</span></strong> <strong><span style="color: blue;">double</span></strong> Run(<strong><span style="color: blue;">int</span></strong> numOfThreads, <strong><span style="color: blue;">int</span></strong> ubound){ </span></p>
<p class="MsoNormal"><span style="font-size: 10pt; font-family: 'Courier New';"><span>            </span><strong><span style="color: teal;">MPThreadingTest</span></strong>[] threads <span style="color: navy;">=</span> <strong><span style="color: blue;">new</span></strong> <strong><span style="color: teal;">MPThreadingTest</span></strong>[numOfThreads];<o:p></o:p> </span></p>
<p class="MsoNormal"><span style="font-size: 10pt; font-family: 'Courier New';"><span>            </span><strong><span style="color: blue;">int</span></strong> startNum <span style="color: navy;">=</span> <span style="color: teal;">3</span>;<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size: 10pt; font-family: 'Courier New';"><o:p></o:p><span>            </span><strong><span style="color: blue;">for</span></strong> (<strong><span style="color: blue;">int</span></strong> i <span style="color: navy;">=</span> <span style="color: teal;">0</span>; i <span style="color: navy;">&lt;</span> numOfThreads; i<span style="color: navy;">++</span>) <span>               </span></span></p>
<p class="MsoNormal"><span style="font-size: 10pt; font-family: 'Courier New';">threads[i] <span style="color: navy;">=</span> <strong><span style="color: blue;">new</span></strong> <strong><span style="color: teal;">MPThreadingTest</span></strong>(startNum <span style="color: navy;">+</span> i <span style="color: navy;">*</span> <span style="color: teal;">2</span>, numOfThreads <span style="color: navy;">*</span> <span style="color: teal;">2</span>, ubound <span style="color: navy;">-</span> (numOfThreads <span style="color: navy;">-</span> i) <span style="color: navy;">*</span> <span style="color: teal;">2</span>);<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size: 10pt; font-family: 'Courier New';"><span>            </span>perfCounter<span style="color: navy;">.</span>Start();<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size: 10pt; font-family: 'Courier New';"><span>            </span><strong><span style="color: blue;">for</span></strong> (<strong><span style="color: blue;">int</span></strong> i <span style="color: navy;">=</span> <span style="color: teal;">0</span>; i <span style="color: navy;">&lt;</span> numOfThreads; i<span style="color: navy;">++</span>)<o:p></o:p> <span>                </span>threads[i]<span style="color: navy;">.</span>Start();<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size: 10pt; font-family: 'Courier New';"><o:p></o:p><span>            </span><strong><span style="color: blue;">for</span></strong> (<strong><span style="color: blue;">int</span></strong> i <span style="color: navy;">=</span> <span style="color: teal;">0</span>; i <span style="color: navy;">&lt;</span> numOfThreads; i<span style="color: navy;">++</span>)<o:p></o:p> <span>                </span>threads[i]<span style="color: navy;">.</span>Wait();<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size: 10pt; font-family: 'Courier New';"><o:p></o:p><span>            </span>perfCounter<span style="color: navy;">.</span>End();<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size: 10pt; font-family: 'Courier New';"><span>            </span><strong><span style="color: blue;">return</span></strong> perfCounter<span style="color: navy;">.</span>TimeElapsed(<span style="color: maroon;">&quot;MS&quot;</span>);<o:p></o:p> <span>        </span>}</span></p>
<p class="MsoNormal"><span style="font-size: 10pt; font-family: Verdana;">The high precision timing unit was described in one of my earlier <a href="/2005/10/09/a-high-resolution-timing-utility-in-c/">post.</a> </span><span style="font-size: 10pt; font-family: Verdana;"> </span></p>
<h3>Results</h3>
<p><span style="font-size: 10pt; font-family: Verdana;">The following are the results from two CPUs, one is a Pentium 4 3.0 GHz HT (Northwood), the other is a Pentium D 2.8 GHz (<st1:city w:st="on"><st1:place w:st="on">Smithfield</st1:place></st1:city>). All the calculations are repeated for 10 times and the results are averaged. The time obtained is the time it takes to enumerate the prime numbers within 1,000,000.<o:p></o:p></span></p>
<table width="495" cellspacing="0" cellpadding="0" border="0">
<tbody>
<tr valign="bottom">
<td width="83">
<p style="border: medium none ; padding: 0in;"><font face="Arial"><font size="2">Pentium 					4</font></font></p>
</td>
<td width="43">
<p style="border: medium none ; padding: 0in;"><font face="Arial"><font size="2">3 					G</font></font></p>
</td>
<td width="43">
<p style="border: medium none ; padding: 0in;">&nbsp;</p>
</td>
<td width="40">
<p style="border: medium none ; padding: 0in;">&nbsp;</p>
</td>
<td width="41">
<p style="border: medium none ; padding: 0in;">&nbsp;</p>
</td>
<td width="41">
<p style="border: medium none ; padding: 0in;">&nbsp;</p>
</td>
<td width="40">
<p style="border: medium none ; padding: 0in;">&nbsp;</p>
</td>
<td width="43">
<p style="border: medium none ; padding: 0in;">&nbsp;</p>
</td>
<td width="41">
<p style="border: medium none ; padding: 0in;">&nbsp;</p>
</td>
<td width="40">
<p style="border: medium none ; padding: 0in;">&nbsp;</p>
</td>
<td width="40">
<p style="border: medium none ; padding: 0in;">&nbsp;</p>
</td>
</tr>
<tr valign="bottom">
<td width="83">
<p style="border: medium none ; padding: 0in;">&nbsp;</p>
</td>
<td width="125" colspan="3">
<p style="border: medium none ; padding: 0in;"><font face="Arial"><font size="2">Number 					of Threads</font></font></p>
</td>
<td width="41">
<p style="border: medium none ; padding: 0in;">&nbsp;</p>
</td>
<td width="41">
<p style="border: medium none ; padding: 0in;">&nbsp;</p>
</td>
<td width="40">
<p style="border: medium none ; padding: 0in;">&nbsp;</p>
</td>
<td width="43">
<p style="border: medium none ; padding: 0in;">&nbsp;</p>
</td>
<td width="41">
<p style="border: medium none ; padding: 0in;">&nbsp;</p>
</td>
<td width="40">
<p style="border: medium none ; padding: 0in;">&nbsp;</p>
</td>
<td width="40">
<p style="border: medium none ; padding: 0in;">&nbsp;</p>
</td>
</tr>
<tr valign="bottom">
<td width="83">
<p style="border: medium none ; padding: 0in;"><font face="Arial"><font size="2">Runs</font></font></p>
</td>
<td width="43" bgcolor="#00ff00">
<p align="right" style="border: medium none ; padding: 0in;"><font face="Arial"><font size="2">1</font></font></p>
</td>
<td width="43" bgcolor="#00ff00">
<p align="right" style="border: medium none ; padding: 0in;"><font face="Arial"><font size="2">2</font></font></p>
</td>
<td width="40" bgcolor="#00ff00">
<p align="right" style="border: medium none ; padding: 0in;"><font face="Arial"><font size="2">3</font></font></p>
</td>
<td width="41" bgcolor="#00ff00">
<p align="right" style="border: medium none ; padding: 0in;"><font face="Arial"><font size="2">4</font></font></p>
</td>
<td width="41" bgcolor="#00ff00">
<p align="right" style="border: medium none ; padding: 0in;"><font face="Arial"><font size="2">5</font></font></p>
</td>
<td width="40" bgcolor="#00ff00">
<p align="right" style="border: medium none ; padding: 0in;"><font face="Arial"><font size="2">6</font></font></p>
</td>
<td width="43" bgcolor="#00ff00">
<p align="right" style="border: medium none ; padding: 0in;"><font face="Arial"><font size="2">7</font></font></p>
</td>
<td width="41" bgcolor="#00ff00">
<p align="right" style="border: medium none ; padding: 0in;"><font face="Arial"><font size="2">8</font></font></p>
</td>
<td width="40" bgcolor="#00ff00">
<p align="right" style="border: medium none ; padding: 0in;"><font face="Arial"><font size="2">9</font></font></p>
</td>
<td width="40" bgcolor="#00ff00">
<p align="right" style="border: medium none ; padding: 0in;"><font face="Arial"><font size="2">10</font></font></p>
</td>
</tr>
<tr valign="bottom">
<td width="83" bgcolor="#ffcc99">
<p align="right" style="border: medium none ; padding: 0in;"><font face="Arial"><font size="2">1</font></font></p>
</td>
<td width="43" bgcolor="#ffff00">
<p align="right" style="border: medium none ; padding: 0in;"><font face="Arial"><font size="2">761.6</font></font></p>
</td>
<td width="43" bgcolor="#ffff00">
<p align="right" style="border: medium none ; padding: 0in;"><font face="Arial"><font size="2">748.8</font></font></p>
</td>
<td width="40">
<p align="right" style="border: medium none ; padding: 0in;"><font face="Arial"><font size="2">748.5</font></font></p>
</td>
<td width="41">
<p align="right" style="border: medium none ; padding: 0in;"><font face="Arial"><font size="2">748.8</font></font></p>
</td>
<td width="41">
<p align="right" style="border: medium none ; padding: 0in;"><font face="Arial"><font size="2">796.9</font></font></p>
</td>
<td width="40">
<p align="right" style="border: medium none ; padding: 0in;"><font face="Arial"><font size="2">811.6</font></font></p>
</td>
<td width="43">
<p align="right" style="border: medium none ; padding: 0in;"><font face="Arial"><font size="2">790.1</font></font></p>
</td>
<td width="41">
<p align="right" style="border: medium none ; padding: 0in;"><font face="Arial"><font size="2">778.5</font></font></p>
</td>
<td width="40">
<p align="right" style="border: medium none ; padding: 0in;"><font face="Arial"><font size="2">864.8</font></font></p>
</td>
<td width="40" bgcolor="#ffff00">
<p align="right" style="border: medium none ; padding: 0in;"><font face="Arial"><font size="2">832</font></font></p>
</td>
</tr>
<tr valign="bottom">
<td width="83" bgcolor="#ffcc99">
<p align="right" style="border: medium none ; padding: 0in;"><font face="Arial"><font size="2">2</font></font></p>
</td>
<td width="43" bgcolor="#ffff00">
<p align="right" style="border: medium none ; padding: 0in;"><font face="Arial"><font size="2">755.5</font></font></p>
</td>
<td width="43" bgcolor="#ffff00">
<p align="right" style="border: medium none ; padding: 0in;"><font face="Arial"><font size="2">754.7</font></font></p>
</td>
<td width="40">
<p align="right" style="border: medium none ; padding: 0in;"><font face="Arial"><font size="2">749.5</font></font></p>
</td>
<td width="41">
<p align="right" style="border: medium none ; padding: 0in;"><font face="Arial"><font size="2">790.4</font></font></p>
</td>
<td width="41">
<p align="right" style="border: medium none ; padding: 0in;"><font face="Arial"><font size="2">791.1</font></font></p>
</td>
<td width="40">
<p align="right" style="border: medium none ; padding: 0in;"><font face="Arial"><font size="2">759.2</font></font></p>
</td>
<td width="43">
<p align="right" style="border: medium none ; padding: 0in;"><font face="Arial"><font size="2">860.1</font></font></p>
</td>
<td width="41">
<p align="right" style="border: medium none ; padding: 0in;"><font face="Arial"><font size="2">748</font></font></p>
</td>
<td width="40">
<p align="right" style="border: medium none ; padding: 0in;"><font face="Arial"><font size="2">829.8</font></font></p>
</td>
<td width="40" bgcolor="#ffff00">
<p align="right" style="border: medium none ; padding: 0in;"><font face="Arial"><font size="2">808</font></font></p>
</td>
</tr>
<tr valign="bottom">
<td width="83" bgcolor="#ffcc99">
<p align="right" style="border: medium none ; padding: 0in;"><font face="Arial"><font size="2">3</font></font></p>
</td>
<td width="43" bgcolor="#ffff00">
<p align="right" style="border: medium none ; padding: 0in;"><font face="Arial"><font size="2">755.9</font></font></p>
</td>
<td width="43" bgcolor="#ffff00">
<p align="right" style="border: medium none ; padding: 0in;"><font face="Arial"><font size="2">748.7</font></font></p>
</td>
<td width="40">
<p align="right" style="border: medium none ; padding: 0in;"><font face="Arial"><font size="2">790.4</font></font></p>
</td>
<td width="41">
<p align="right" style="border: medium none ; padding: 0in;"><font face="Arial"><font size="2">751.4</font></font></p>
</td>
<td width="41">
<p align="right" style="border: medium none ; padding: 0in;"><font face="Arial"><font size="2">786</font></font></p>
</td>
<td width="40">
<p align="right" style="border: medium none ; padding: 0in;"><font face="Arial"><font size="2">799.6</font></font></p>
</td>
<td width="43">
<p align="right" style="border: medium none ; padding: 0in;"><font face="Arial"><font size="2">797.9</font></font></p>
</td>
<td width="41">
<p align="right" style="border: medium none ; padding: 0in;"><font face="Arial"><font size="2">775</font></font></p>
</td>
<td width="40">
<p align="right" style="border: medium none ; padding: 0in;"><font face="Arial"><font size="2">869.7</font></font></p>
</td>
<td width="40" bgcolor="#ffff00">
<p align="right" style="border: medium none ; padding: 0in;"><font face="Arial"><font size="2">810.2</font></font></p>
</td>
</tr>
<tr valign="bottom">
<td width="83" bgcolor="#ffcc99">
<p align="right" style="border: medium none ; padding: 0in;"><font face="Arial"><font size="2">4</font></font></p>
</td>
<td width="43" bgcolor="#ffff00">
<p align="right" style="border: medium none ; padding: 0in;"><font face="Arial"><font size="2">753.8</font></font></p>
</td>
<td width="43" bgcolor="#ffff00">
<p align="right" style="border: medium none ; padding: 0in;"><font face="Arial"><font size="2">749.5</font></font></p>
</td>
<td width="40">
<p align="right" style="border: medium none ; padding: 0in;"><font face="Arial"><font size="2">791.2</font></font></p>
</td>
<td width="41">
<p align="right" style="border: medium none ; padding: 0in;"><font face="Arial"><font size="2">787.9</font></font></p>
</td>
<td width="41">
<p align="right" style="border: medium none ; padding: 0in;"><font face="Arial"><font size="2">801.7</font></font></p>
</td>
<td width="40">
<p align="right" style="border: medium none ; padding: 0in;"><font face="Arial"><font size="2">800.6</font></font></p>
</td>
<td width="43">
<p align="right" style="border: medium none ; padding: 0in;"><font face="Arial"><font size="2">861.1</font></font></p>
</td>
<td width="41">
<p align="right" style="border: medium none ; padding: 0in;"><font face="Arial"><font size="2">782.7</font></font></p>
</td>
<td width="40">
<p align="right" style="border: medium none ; padding: 0in;"><font face="Arial"><font size="2">835.6</font></font></p>
</td>
<td width="40" bgcolor="#ffff00">
<p align="right" style="border: medium none ; padding: 0in;"><font face="Arial"><font size="2">800.5</font></font></p>
</td>
</tr>
<tr valign="bottom">
<td width="83" bgcolor="#ffcc99">
<p align="right" style="border: medium none ; padding: 0in;"><font face="Arial"><font size="2">5</font></font></p>
</td>
<td width="43" bgcolor="#ffff00">
<p align="right" style="border: medium none ; padding: 0in;"><font face="Arial"><font size="2">755.9</font></font></p>
</td>
<td width="43" bgcolor="#ffff00">
<p align="right" style="border: medium none ; padding: 0in;"><font face="Arial"><font size="2">749.8</font></font></p>
</td>
<td width="40">
<p align="right" style="border: medium none ; padding: 0in;"><font face="Arial"><font size="2">750.2</font></font></p>
</td>
<td width="41">
<p align="right" style="border: medium none ; padding: 0in;"><font face="Arial"><font size="2">757.4</font></font></p>
</td>
<td width="41">
<p align="right" style="border: medium none ; padding: 0in;"><font face="Arial"><font size="2">805.4</font></font></p>
</td>
<td width="40">
<p align="right" style="border: medium none ; padding: 0in;"><font face="Arial"><font size="2">808.5</font></font></p>
</td>
<td width="43">
<p align="right" style="border: medium none ; padding: 0in;"><font face="Arial"><font size="2">784.9</font></font></p>
</td>
<td width="41">
<p align="right" style="border: medium none ; padding: 0in;"><font face="Arial"><font size="2">776.2</font></font></p>
</td>
<td width="40">
<p align="right" style="border: medium none ; padding: 0in;"><font face="Arial"><font size="2">869.5</font></font></p>
</td>
<td width="40" bgcolor="#ffff00">
<p align="right" style="border: medium none ; padding: 0in;"><font face="Arial"><font size="2">835.8</font></font></p>
</td>
</tr>
<tr valign="bottom">
<td width="83" bgcolor="#ffcc99">
<p align="right" style="border: medium none ; padding: 0in;"><font face="Arial"><font size="2">6</font></font></p>
</td>
<td width="43" bgcolor="#ffff00">
<p align="right" style="border: medium none ; padding: 0in;"><font face="Arial"><font size="2">754.6</font></font></p>
</td>
<td width="43" bgcolor="#ffff00">
<p align="right" style="border: medium none ; padding: 0in;"><font face="Arial"><font size="2">750.5</font></font></p>
</td>
<td width="40">
<p align="right" style="border: medium none ; padding: 0in;"><font face="Arial"><font size="2">787.6</font></font></p>
</td>
<td width="41">
<p align="right" style="border: medium none ; padding: 0in;"><font face="Arial"><font size="2">786.4</font></font></p>
</td>
<td width="41">
<p align="right" style="border: medium none ; padding: 0in;"><font face="Arial"><font size="2">787.5</font></font></p>
</td>
<td width="40">
<p align="right" style="border: medium none ; padding: 0in;"><font face="Arial"><font size="2">745.9</font></font></p>
</td>
<td width="43">
<p align="right" style="border: medium none ; padding: 0in;"><font face="Arial"><font size="2">747.3</font></font></p>
</td>
<td width="41">
<p align="right" style="border: medium none ; padding: 0in;"><font face="Arial"><font size="2">747.3</font></font></p>
</td>
<td width="40">
<p align="right" style="border: medium none ; padding: 0in;"><font face="Arial"><font size="2">831.7</font></font></p>
</td>
<td width="40" bgcolor="#ffff00">
<p align="right" style="border: medium none ; padding: 0in;"><font face="Arial"><font size="2">812.1</font></font></p>
</td>
</tr>
<tr valign="bottom">
<td width="83" bgcolor="#ffcc99">
<p align="right" style="border: medium none ; padding: 0in;"><font face="Arial"><font size="2">7</font></font></p>
</td>
<td width="43" bgcolor="#ffff00">
<p align="right" style="border: medium none ; padding: 0in;"><font face="Arial"><font size="2">760.8</font></font></p>
</td>
<td width="43" bgcolor="#ffff00">
<p align="right" style="border: medium none ; padding: 0in;"><font face="Arial"><font size="2">749.5</font></font></p>
</td>
<td width="40">
<p align="right" style="border: medium none ; padding: 0in;"><font face="Arial"><font size="2">785.7</font></font></p>
</td>
<td width="41">
<p align="right" style="border: medium none ; padding: 0in;"><font face="Arial"><font size="2">748.7</font></font></p>
</td>
<td width="41">
<p align="right" style="border: medium none ; padding: 0in;"><font face="Arial"><font size="2">809.1</font></font></p>
</td>
<td width="40">
<p align="right" style="border: medium none ; padding: 0in;"><font face="Arial"><font size="2">790.7</font></font></p>
</td>
<td width="43">
<p align="right" style="border: medium none ; padding: 0in;"><font face="Arial"><font size="2">831</font></font></p>
</td>
<td width="41">
<p align="right" style="border: medium none ; padding: 0in;"><font face="Arial"><font size="2">807.4</font></font></p>
</td>
<td width="40">
<p align="right" style="border: medium none ; padding: 0in;"><font face="Arial"><font size="2">824.3</font></font></p>
</td>
<td width="40" bgcolor="#ffff00">
<p align="right" style="border: medium none ; padding: 0in;"><font face="Arial"><font size="2">781.6</font></font></p>
</td>
</tr>
<tr valign="bottom">
<td width="83" bgcolor="#ffcc99">
<p align="right" style="border: medium none ; padding: 0in;"><font face="Arial"><font size="2">8</font></font></p>
</td>
<td width="43" bgcolor="#ffff00">
<p align="right" style="border: medium none ; padding: 0in;"><font face="Arial"><font size="2">753.9</font></font></p>
</td>
<td width="43" bgcolor="#ffff00">
<p align="right" style="border: medium none ; padding: 0in;"><font face="Arial"><font size="2">755.9</font></font></p>
</td>
<td width="40">
<p align="right" style="border: medium none ; padding: 0in;"><font face="Arial"><font size="2">783.2</font></font></p>
</td>
<td width="41">
<p align="right" style="border: medium none ; padding: 0in;"><font face="Arial"><font size="2">804.5</font></font></p>
</td>
<td width="41">
<p align="right" style="border: medium none ; padding: 0in;"><font face="Arial"><font size="2">788</font></font></p>
</td>
<td width="40">
<p align="right" style="border: medium none ; padding: 0in;"><font face="Arial"><font size="2">758</font></font></p>
</td>
<td width="43">
<p align="right" style="border: medium none ; padding: 0in;"><font face="Arial"><font size="2">784.6</font></font></p>
</td>
<td width="41">
<p align="right" style="border: medium none ; padding: 0in;"><font face="Arial"><font size="2">785.4</font></font></p>
</td>
<td width="40">
<p align="right" style="border: medium none ; padding: 0in;"><font face="Arial"><font size="2">851.9</font></font></p>
</td>
<td width="40" bgcolor="#ffff00">
<p align="right" style="border: medium none ; padding: 0in;"><font face="Arial"><font size="2">840.5</font></font></p>
</td>
</tr>
<tr valign="bottom">
<td width="83" bgcolor="#ffcc99">
<p align="right" style="border: medium none ; padding: 0in;"><font face="Arial"><font size="2">9</font></font></p>
</td>
<td width="43" bgcolor="#ffff00">
<p align="right" style="border: medium none ; padding: 0in;"><font face="Arial"><font size="2">756.2</font></font></p>
</td>
<td width="43" bgcolor="#ffff00">
<p align="right" style="border: medium none ; padding: 0in;"><font face="Arial"><font size="2">747</font></font></p>
</td>
<td width="40">
<p align="right" style="border: medium none ; padding: 0in;"><font face="Arial"><font size="2">749.8</font></font></p>
</td>
<td width="41">
<p align="right" style="border: medium none ; padding: 0in;"><font face="Arial"><font size="2">747.5</font></font></p>
</td>
<td width="41">
<p align="right" style="border: medium none ; padding: 0in;"><font face="Arial"><font size="2">791.5</font></font></p>
</td>
<td width="40">
<p align="right" style="border: medium none ; padding: 0in;"><font face="Arial"><font size="2">756.8</font></font></p>
</td>
<td width="43">
<p align="right" style="border: medium none ; padding: 0in;"><font face="Arial"><font size="2">759.6</font></font></p>
</td>
<td width="41">
<p align="right" style="border: medium none ; padding: 0in;"><font face="Arial"><font size="2">777.8</font></font></p>
</td>
<td width="40">
<p align="right" style="border: medium none ; padding: 0in;"><font face="Arial"><font size="2">869.1</font></font></p>
</td>
<td width="40" bgcolor="#ffff00">
<p align="right" style="border: medium none ; padding: 0in;"><font face="Arial"><font size="2">811.9</font></font></p>
</td>
</tr>
<tr valign="bottom">
<td width="83" bgcolor="#ffcc99">
<p align="right" style="border: medium none ; padding: 0in;"><font face="Arial"><font size="2">10</font></font></p>
</td>
<td width="43" bgcolor="#ffff00">
<p align="right" style="border: medium none ; padding: 0in;"><font face="Arial"><font size="2">754.3</font></font></p>
</td>
<td width="43" bgcolor="#ffff00">
<p align="right" style="border: medium none ; padding: 0in;"><font face="Arial"><font size="2">792.8</font></font></p>
</td>
<td width="40">
<p align="right" style="border: medium none ; padding: 0in;"><font face="Arial"><font size="2">756.8</font></font></p>
</td>
<td width="41">
<p align="right" style="border: medium none ; padding: 0in;"><font face="Arial"><font size="2">802.3</font></font></p>
</td>
<td width="41">
<p align="right" style="border: medium none ; padding: 0in;"><font face="Arial"><font size="2">786.8</font></font></p>
</td>
<td width="40">
<p align="right" style="border: medium none ; padding: 0in;"><font face="Arial"><font size="2">749.6</font></font></p>
</td>
<td width="43">
<p align="right" style="border: medium none ; padding: 0in;"><font face="Arial"><font size="2">785.9</font></font></p>
</td>
<td width="41">
<p align="right" style="border: medium none ; padding: 0in;"><font face="Arial"><font size="2">793.3</font></font></p>
</td>
<td width="40">
<p align="right" style="border: medium none ; padding: 0in;"><font face="Arial"><font size="2">826.1</font></font></p>
</td>
<td width="40" bgcolor="#ffff00">
<p align="right" style="border: medium none ; padding: 0in;"><font face="Arial"><font size="2">839.7</font></font></p>
</td>
</tr>
<tr valign="bottom">
<td width="83">
<p style="border: medium none ; padding: 0in;">&nbsp;</p>
</td>
<td width="43" bgcolor="#ffff00">
<p style="border: medium none ; padding: 0in;">&nbsp;</p>
</td>
<td width="43" bgcolor="#ffff00">
<p style="border: medium none ; padding: 0in;">&nbsp;</p>
</td>
<td width="40">
<p style="border: medium none ; padding: 0in;">&nbsp;</p>
</td>
<td width="41">
<p style="border: medium none ; padding: 0in;">&nbsp;</p>
</td>
<td width="41">
<p style="border: medium none ; padding: 0in;">&nbsp;</p>
</td>
<td width="40">
<p style="border: medium none ; padding: 0in;">&nbsp;</p>
</td>
<td width="43">
<p style="border: medium none ; padding: 0in;">&nbsp;</p>
</td>
<td width="41">
<p style="border: medium none ; padding: 0in;">&nbsp;</p>
</td>
<td width="40">
<p style="border: medium none ; padding: 0in;">&nbsp;</p>
</td>
<td width="40" bgcolor="#ffff00">
<p style="border: medium none ; padding: 0in;">&nbsp;</p>
</td>
</tr>
<tr valign="bottom">
<td width="83">
<p style="border: medium none ; padding: 0in;"><font face="Arial"><font size="2">Avg. 					(ms)</font></font></p>
</td>
<td width="43" bgcolor="#ffff00">
<p align="right" style="border: medium none ; padding: 0in;"><font face="Arial"><font size="2">756.2</font></font></p>
</td>
<td width="43" bgcolor="#ffff00">
<p align="right" style="border: medium none ; padding: 0in;"><font face="Arial"><font size="2">754.7</font></font></p>
</td>
<td width="40">
<p align="right" style="border: medium none ; padding: 0in;"><font face="Arial"><font size="2">769.3</font></font></p>
</td>
<td width="41">
<p align="right" style="border: medium none ; padding: 0in;"><font face="Arial"><font size="2">772.5</font></font></p>
</td>
<td width="41">
<p align="right" style="border: medium none ; padding: 0in;"><font face="Arial"><font size="2">794.4</font></font></p>
</td>
<td width="40">
<p align="right" style="border: medium none ; padding: 0in;"><font face="Arial"><font size="2">778.1</font></font></p>
</td>
<td width="43">
<p align="right" style="border: medium none ; padding: 0in;"><font face="Arial"><font size="2">800.3</font></font></p>
</td>
<td width="41">
<p align="right" style="border: medium none ; padding: 0in;"><font face="Arial"><font size="2">777.2</font></font></p>
</td>
<td width="40">
<p align="right" style="border: medium none ; padding: 0in;"><font face="Arial"><font size="2">847.2</font></font></p>
</td>
<td width="40" bgcolor="#ffff00">
<p align="right" style="border: medium none ; padding: 0in;"><font face="Arial"><font size="2">817.2</font></font></p>
</td>
</tr>
</tbody>
</table>
<p>&nbsp;</p>
<table width="491" cellspacing="0" cellpadding="0" border="0">
<tbody>
<tr valign="bottom">
<td width="79">
<p style="border: medium none ; padding: 0in;"><font face="Arial"><font size="2">Pentium 					D </font></font></p>
</td>
<td width="43">
<p style="border: medium none ; padding: 0in;"><font face="Arial"><font size="2">2.8 					G</font></font></p>
</td>
<td width="41">
<p style="border: medium none ; padding: 0in;">&nbsp;</p>
</td>
<td width="43">
<p style="border: medium none ; padding: 0in;">&nbsp;</p>
</td>
<td width="41">
<p style="border: medium none ; padding: 0in;">&nbsp;</p>
</td>
<td width="40">
<p style="border: medium none ; padding: 0in;">&nbsp;</p>
</td>
<td width="40">
<p style="border: medium none ; padding: 0in;">&nbsp;</p>
</td>
<td width="43">
<p style="border: medium none ; padding: 0in;">&nbsp;</p>
</td>
<td width="41">
<p style="border: medium none ; padding: 0in;">&nbsp;</p>
</td>
<td width="40">
<p style="border: medium none ; padding: 0in;">&nbsp;</p>
</td>
<td width="40">
<p style="border: medium none ; padding: 0in;">&nbsp;</p>
</td>
</tr>
<tr valign="bottom">
<td width="79">
<p style="border: medium none ; padding: 0in;">&nbsp;</p>
</td>
<td width="127" colspan="3">
<p style="border: medium none ; padding: 0in;"><font face="Arial"><font size="2">Number 					of Threads</font></font></p>
</td>
<td width="41">
<p style="border: medium none ; padding: 0in;">&nbsp;</p>
</td>
<td width="40">
<p style="border: medium none ; padding: 0in;">&nbsp;</p>
</td>
<td width="40">
<p style="border: medium none ; padding: 0in;">&nbsp;</p>
</td>
<td width="43">
<p style="border: medium none ; padding: 0in;">&nbsp;</p>
</td>
<td width="41">
<p style="border: medium none ; padding: 0in;">&nbsp;</p>
</td>
<td width="40">
<p style="border: medium none ; padding: 0in;">&nbsp;</p>
</td>
<td width="40">
<p style="border: medium none ; padding: 0in;">&nbsp;</p>
</td>
</tr>
<tr valign="bottom">
<td width="79">
<p style="border: medium none ; padding: 0in;"><font face="Arial"><font size="2">Runs</font></font></p>
</td>
<td width="43" bgcolor="#00ff00">
<p align="right" style="border: medium none ; padding: 0in;"><font face="Arial"><font size="2">1</font></font></p>
</td>
<td width="41" bgcolor="#00ff00">
<p align="right" style="border: medium none ; padding: 0in;"><font face="Arial"><font size="2">2</font></font></p>
</td>
<td width="43" bgcolor="#00ff00">
<p align="right" style="border: medium none ; padding: 0in;"><font face="Arial"><font size="2">3</font></font></p>
</td>
<td width="41" bgcolor="#00ff00">
<p align="right" style="border: medium none ; padding: 0in;"><font face="Arial"><font size="2">4</font></font></p>
</td>
<td width="40" bgcolor="#00ff00">
<p align="right" style="border: medium none ; padding: 0in;"><font face="Arial"><font size="2">5</font></font></p>
</td>
<td width="40" bgcolor="#00ff00">
<p align="right" style="border: medium none ; padding: 0in;"><font face="Arial"><font size="2">6</font></font></p>
</td>
<td width="43" bgcolor="#00ff00">
<p align="right" style="border: medium none ; padding: 0in;"><font face="Arial"><font size="2">7</font></font></p>
</td>
<td width="41" bgcolor="#00ff00">
<p align="right" style="border: medium none ; padding: 0in;"><font face="Arial"><font size="2">8</font></font></p>
</td>
<td width="40" bgcolor="#00ff00">
<p align="right" style="border: medium none ; padding: 0in;"><font face="Arial"><font size="2">9</font></font></p>
</td>
<td width="40" bgcolor="#00ff00">
<p align="right" style="border: medium none ; padding: 0in;"><font face="Arial"><font size="2">10</font></font></p>
</td>
</tr>
<tr valign="bottom">
<td width="79" bgcolor="#ffcc99">
<p align="right" style="border: medium none ; padding: 0in;"><font face="Arial"><font size="2">1</font></font></p>
</td>
<td width="43" bgcolor="#ffff00">
<p align="right" style="border: medium none ; padding: 0in;"><font face="Arial"><font size="2">930.7</font></font></p>
</td>
<td width="41" bgcolor="#ffff00">
<p align="right" style="border: medium none ; padding: 0in;"><font face="Arial"><font size="2">484.1</font></font></p>
</td>
<td width="43">
<p align="right" style="border: medium none ; padding: 0in;"><font face="Arial"><font size="2">466.9</font></font></p>
</td>
<td width="41">
<p align="right" style="border: medium none ; padding: 0in;"><font face="Arial"><font size="2">467.6</font></font></p>
</td>
<td width="40">
<p align="right" style="border: medium none ; padding: 0in;"><font face="Arial"><font size="2">494.5</font></font></p>
</td>
<td width="40">
<p align="right" style="border: medium none ; padding: 0in;"><font face="Arial"><font size="2">464.8</font></font></p>
</td>
<td width="43">
<p align="right" style="border: medium none ; padding: 0in;"><font face="Arial"><font size="2">468.5</font></font></p>
</td>
<td width="41">
<p align="right" style="border: medium none ; padding: 0in;"><font face="Arial"><font size="2">467.9</font></font></p>
</td>
<td width="40">
<p align="right" style="border: medium none ; padding: 0in;"><font face="Arial"><font size="2">465.1</font></font></p>
</td>
<td width="40" bgcolor="#ffff00">
<p align="right" style="border: medium none ; padding: 0in;"><font face="Arial"><font size="2">524.4</font></font></p>
</td>
</tr>
<tr valign="bottom">
<td width="79" bgcolor="#ffcc99">
<p align="right" style="border: medium none ; padding: 0in;"><font face="Arial"><font size="2">2</font></font></p>
</td>
<td width="43" bgcolor="#ffff00">
<p align="right" style="border: medium none ; padding: 0in;"><font face="Arial"><font size="2">931.8</font></font></p>
</td>
<td width="41" bgcolor="#ffff00">
<p align="right" style="border: medium none ; padding: 0in;"><font face="Arial"><font size="2">467.6</font></font></p>
</td>
<td width="43">
<p align="right" style="border: medium none ; padding: 0in;"><font face="Arial"><font size="2">467.1</font></font></p>
</td>
<td width="41">
<p align="right" style="border: medium none ; padding: 0in;"><font face="Arial"><font size="2">467.3</font></font></p>
</td>
<td width="40">
<p align="right" style="border: medium none ; padding: 0in;"><font face="Arial"><font size="2">465.7</font></font></p>
</td>
<td width="40">
<p align="right" style="border: medium none ; padding: 0in;"><font face="Arial"><font size="2">465.1</font></font></p>
</td>
<td width="43">
<p align="right" style="border: medium none ; padding: 0in;"><font face="Arial"><font size="2">468.3</font></font></p>
</td>
<td width="41">
<p align="right" style="border: medium none ; padding: 0in;"><font face="Arial"><font size="2">467.9</font></font></p>
</td>
<td width="40">
<p align="right" style="border: medium none ; padding: 0in;"><font face="Arial"><font size="2">465.1</font></font></p>
</td>
<td width="40" bgcolor="#ffff00">
<p align="right" style="border: medium none ; padding: 0in;"><font face="Arial"><font size="2">522.4</font></font></p>
</td>
</tr>
<tr valign="bottom">
<td width="79" bgcolor="#ffcc99">
<p align="right" style="border: medium none ; padding: 0in;"><font face="Arial"><font size="2">3</font></font></p>
</td>
<td width="43" bgcolor="#ffff00">
<p align="right" style="border: medium none ; padding: 0in;"><font face="Arial"><font size="2">931.2</font></font></p>
</td>
<td width="41" bgcolor="#ffff00">
<p align="right" style="border: medium none ; padding: 0in;"><font face="Arial"><font size="2">468.1</font></font></p>
</td>
<td width="43">
<p align="right" style="border: medium none ; padding: 0in;"><font face="Arial"><font size="2">467.7</font></font></p>
</td>
<td width="41">
<p align="right" style="border: medium none ; padding: 0in;"><font face="Arial"><font size="2">513.8</font></font></p>
</td>
<td width="40">
<p align="right" style="border: medium none ; padding: 0in;"><font face="Arial"><font size="2">525.5</font></font></p>
</td>
<td width="40">
<p align="right" style="border: medium none ; padding: 0in;"><font face="Arial"><font size="2">506.6</font></font></p>
</td>
<td width="43">
<p align="right" style="border: medium none ; padding: 0in;"><font face="Arial"><font size="2">469.8</font></font></p>
</td>
<td width="41">
<p align="right" style="border: medium none ; padding: 0in;"><font face="Arial"><font size="2">468.6</font></font></p>
</td>
<td width="40">
<p align="right" style="border: medium none ; padding: 0in;"><font face="Arial"><font size="2">465.4</font></font></p>
</td>
<td width="40" bgcolor="#ffff00">
<p align="right" style="border: medium none ; padding: 0in;"><font face="Arial"><font size="2">523.7</font></font></p>
</td>
</tr>
<tr valign="bottom">
<td width="79" bgcolor="#ffcc99">
<p align="right" style="border: medium none ; padding: 0in;"><font face="Arial"><font size="2">4</font></font></p>
</td>
<td width="43" bgcolor="#ffff00">
<p align="right" style="border: medium none ; padding: 0in;"><font face="Arial"><font size="2">931.5</font></font></p>
</td>
<td width="41" bgcolor="#ffff00">
<p align="right" style="border: medium none ; padding: 0in;"><font face="Arial"><font size="2">468.2</font></font></p>
</td>
<td width="43">
<p align="right" style="border: medium none ; padding: 0in;"><font face="Arial"><font size="2">466.1</font></font></p>
</td>
<td width="41">
<p align="right" style="border: medium none ; padding: 0in;"><font face="Arial"><font size="2">506.5</font></font></p>
</td>
<td width="40">
<p align="right" style="border: medium none ; padding: 0in;"><font face="Arial"><font size="2">485.2</font></font></p>
</td>
<td width="40">
<p align="right" style="border: medium none ; padding: 0in;"><font face="Arial"><font size="2">465</font></font></p>
</td>
<td width="43">
<p align="right" style="border: medium none ; padding: 0in;"><font face="Arial"><font size="2">468.8</font></font></p>
</td>
<td width="41">
<p align="right" style="border: medium none ; padding: 0in;"><font face="Arial"><font size="2">468.1</font></font></p>
</td>
<td width="40">
<p align="right" style="border: medium none ; padding: 0in;"><font face="Arial"><font size="2">465.1</font></font></p>
</td>
<td width="40" bgcolor="#ffff00">
<p align="right" style="border: medium none ; padding: 0in;"><font face="Arial"><font size="2">518.4</font></font></p>
</td>
</tr>
<tr valign="bottom">
<td width="79" bgcolor="#ffcc99">
<p align="right" style="border: medium none ; padding: 0in;"><font face="Arial"><font size="2">5</font></font></p>
</td>
<td width="43" bgcolor="#ffff00">
<p align="right" style="border: medium none ; padding: 0in;"><font face="Arial"><font size="2">932.3</font></font></p>
</td>
<td width="41" bgcolor="#ffff00">
<p align="right" style="border: medium none ; padding: 0in;"><font face="Arial"><font size="2">467.8</font></font></p>
</td>
<td width="43">
<p align="right" style="border: medium none ; padding: 0in;"><font face="Arial"><font size="2">467.3</font></font></p>
</td>
<td width="41">
<p align="right" style="border: medium none ; padding: 0in;"><font face="Arial"><font size="2">475.7</font></font></p>
</td>
<td width="40">
<p align="right" style="border: medium none ; padding: 0in;"><font face="Arial"><font size="2">466.4</font></font></p>
</td>
<td width="40">
<p align="right" style="border: medium none ; padding: 0in;"><font face="Arial"><font size="2">465.8</font></font></p>
</td>
<td width="43">
<p align="right" style="border: medium none ; padding: 0in;"><font face="Arial"><font size="2">469.4</font></font></p>
</td>
<td width="41">
<p align="right" style="border: medium none ; padding: 0in;"><font face="Arial"><font size="2">468.1</font></font></p>
</td>
<td width="40">
<p align="right" style="border: medium none ; padding: 0in;"><font face="Arial"><font size="2">468</font></font></p>
</td>
<td width="40" bgcolor="#ffff00">
<p align="right" style="border: medium none ; padding: 0in;"><font face="Arial"><font size="2">521.9</font></font></p>
</td>
</tr>
<tr valign="bottom">
<td width="79" bgcolor="#ffcc99">
<p align="right" style="border: medium none ; padding: 0in;"><font face="Arial"><font size="2">6</font></font></p>
</td>
<td width="43" bgcolor="#ffff00">
<p align="right" style="border: medium none ; padding: 0in;"><font face="Arial"><font size="2">934</font></font></p>
</td>
<td width="41" bgcolor="#ffff00">
<p align="right" style="border: medium none ; padding: 0in;"><font face="Arial"><font size="2">467.3</font></font></p>
</td>
<td width="43">
<p align="right" style="border: medium none ; padding: 0in;"><font face="Arial"><font size="2">466.7</font></font></p>
</td>
<td width="41">
<p align="right" style="border: medium none ; padding: 0in;"><font face="Arial"><font size="2">482.9</font></font></p>
</td>
<td width="40">
<p align="right" style="border: medium none ; padding: 0in;"><font face="Arial"><font size="2">497.4</font></font></p>
</td>
<td width="40">
<p align="right" style="border: medium none ; padding: 0in;"><font face="Arial"><font size="2">464.1</font></font></p>
</td>
<td width="43">
<p align="right" style="border: medium none ; padding: 0in;"><font face="Arial"><font size="2">469.8</font></font></p>
</td>
<td width="41">
<p align="right" style="border: medium none ; padding: 0in;"><font face="Arial"><font size="2">468.8</font></font></p>
</td>
<td width="40">
<p align="right" style="border: medium none ; padding: 0in;"><font face="Arial"><font size="2">465.6</font></font></p>
</td>
<td width="40" bgcolor="#ffff00">
<p align="right" style="border: medium none ; padding: 0in;"><font face="Arial"><font size="2">514.1</font></font></p>
</td>
</tr>
<tr valign="bottom">
<td width="79" bgcolor="#ffcc99">
<p align="right" style="border: medium none ; padding: 0in;"><font face="Arial"><font size="2">7</font></font></p>
</td>
<td width="43" bgcolor="#ffff00">
<p align="right" style="border: medium none ; padding: 0in;"><font face="Arial"><font size="2">930.2</font></font></p>
</td>
<td width="41" bgcolor="#ffff00">
<p align="right" style="border: medium none ; padding: 0in;"><font face="Arial"><font size="2">467.2</font></font></p>
</td>
<td width="43">
<p align="right" style="border: medium none ; padding: 0in;"><font face="Arial"><font size="2">467.2</font></font></p>
</td>
<td width="41">
<p align="right" style="border: medium none ; padding: 0in;"><font face="Arial"><font size="2">479.1</font></font></p>
</td>
<td width="40">
<p align="right" style="border: medium none ; padding: 0in;"><font face="Arial"><font size="2">498.9</font></font></p>
</td>
<td width="40">
<p align="right" style="border: medium none ; padding: 0in;"><font face="Arial"><font size="2">464.2</font></font></p>
</td>
<td width="43">
<p align="right" style="border: medium none ; padding: 0in;"><font face="Arial"><font size="2">468.8</font></font></p>
</td>
<td width="41">
<p align="right" style="border: medium none ; padding: 0in;"><font face="Arial"><font size="2">468</font></font></p>
</td>
<td width="40">
<p align="right" style="border: medium none ; padding: 0in;"><font face="Arial"><font size="2">465.5</font></font></p>
</td>
<td width="40" bgcolor="#ffff00">
<p align="right" style="border: medium none ; padding: 0in;"><font face="Arial"><font size="2">510.3</font></font></p>
</td>
</tr>
<tr valign="bottom">
<td width="79" bgcolor="#ffcc99">
<p align="right" style="border: medium none ; padding: 0in;"><font face="Arial"><font size="2">8</font></font></p>
</td>
<td width="43" bgcolor="#ffff00">
<p align="right" style="border: medium none ; padding: 0in;"><font face="Arial"><font size="2">931.6</font></font></p>
</td>
<td width="41" bgcolor="#ffff00">
<p align="right" style="border: medium none ; padding: 0in;"><font face="Arial"><font size="2">467.5</font></font></p>
</td>
<td width="43">
<p align="right" style="border: medium none ; padding: 0in;"><font face="Arial"><font size="2">466.8</font></font></p>
</td>
<td width="41">
<p align="right" style="border: medium none ; padding: 0in;"><font face="Arial"><font size="2">480.9</font></font></p>
</td>
<td width="40">
<p align="right" style="border: medium none ; padding: 0in;"><font face="Arial"><font size="2">480.8</font></font></p>
</td>
<td width="40">
<p align="right" style="border: medium none ; padding: 0in;"><font face="Arial"><font size="2">507.9</font></font></p>
</td>
<td width="43">
<p align="right" style="border: medium none ; padding: 0in;"><font face="Arial"><font size="2">496</font></font></p>
</td>
<td width="41">
<p align="right" style="border: medium none ; padding: 0in;"><font face="Arial"><font size="2">468.5</font></font></p>
</td>
<td width="40">
<p align="right" style="border: medium none ; padding: 0in;"><font face="Arial"><font size="2">465.2</font></font></p>
</td>
<td width="40" bgcolor="#ffff00">
<p align="right" style="border: medium none ; padding: 0in;"><font face="Arial"><font size="2">519</font></font></p>
</td>
</tr>
<tr valign="bottom">
<td width="79" bgcolor="#ffcc99">
<p align="right" style="border: medium none ; padding: 0in;"><font face="Arial"><font size="2">9</font></font></p>
</td>
<td width="43" bgcolor="#ffff00">
<p align="right" style="border: medium none ; padding: 0in;"><font face="Arial"><font size="2">931.3</font></font></p>
</td>
<td width="41" bgcolor="#ffff00">
<p align="right" style="border: medium none ; padding: 0in;"><font face="Arial"><font size="2">467.4</font></font></p>
</td>
<td width="43">
<p align="right" style="border: medium none ; padding: 0in;"><font face="Arial"><font size="2">466.6</font></font></p>
</td>
<td width="41">
<p align="right" style="border: medium none ; padding: 0in;"><font face="Arial"><font size="2">509.1</font></font></p>
</td>
<td width="40">
<p align="right" style="border: medium none ; padding: 0in;"><font face="Arial"><font size="2">494.4</font></font></p>
</td>
<td width="40">
<p align="right" style="border: medium none ; padding: 0in;"><font face="Arial"><font size="2">464.6</font></font></p>
</td>
<td width="43">
<p align="right" style="border: medium none ; padding: 0in;"><font face="Arial"><font size="2">468.9</font></font></p>
</td>
<td width="41">
<p align="right" style="border: medium none ; padding: 0in;"><font face="Arial"><font size="2">468</font></font></p>
</td>
<td width="40">
<p align="right" style="border: medium none ; padding: 0in;"><font face="Arial"><font size="2">465.8</font></font></p>
</td>
<td width="40" bgcolor="#ffff00">
<p align="right" style="border: medium none ; padding: 0in;"><font face="Arial"><font size="2">518.2</font></font></p>
</td>
</tr>
<tr valign="bottom">
<td width="79" bgcolor="#ffcc99">
<p align="right" style="border: medium none ; padding: 0in;"><font face="Arial"><font size="2">10</font></font></p>
</td>
<td width="43" bgcolor="#ffff00">
<p align="right" style="border: medium none ; padding: 0in;"><font face="Arial"><font size="2">931.1</font></font></p>
</td>
<td width="41" bgcolor="#ffff00">
<p align="right" style="border: medium none ; padding: 0in;"><font face="Arial"><font size="2">468.8</font></font></p>
</td>
<td width="43">
<p align="right" style="border: medium none ; padding: 0in;"><font face="Arial"><font size="2">466.2</font></font></p>
</td>
<td width="41">
<p align="right" style="border: medium none ; padding: 0in;"><font face="Arial"><font size="2">474.7</font></font></p>
</td>
<td width="40">
<p align="right" style="border: medium none ; padding: 0in;"><font face="Arial"><font size="2">553.9</font></font></p>
</td>
<td width="40">
<p align="right" style="border: medium none ; padding: 0in;"><font face="Arial"><font size="2">465.9</font></font></p>
</td>
<td width="43">
<p align="right" style="border: medium none ; padding: 0in;"><font face="Arial"><font size="2">469.6</font></font></p>
</td>
<td width="41">
<p align="right" style="border: medium none ; padding: 0in;"><font face="Arial"><font size="2">467.8</font></font></p>
</td>
<td width="40">
<p align="right" style="border: medium none ; padding: 0in;"><font face="Arial"><font size="2">464.8</font></font></p>
</td>
<td width="40" bgcolor="#ffff00">
<p align="right" style="border: medium none ; padding: 0in;"><font face="Arial"><font size="2">522.2</font></font></p>
</td>
</tr>
<tr valign="bottom">
<td width="79" bgcolor="#ffcc99">
<p style="border: medium none ; padding: 0in;">&nbsp;</p>
</td>
<td width="43" bgcolor="#ffff00">
<p style="border: medium none ; padding: 0in;">&nbsp;</p>
</td>
<td width="41" bgcolor="#ffff00">
<p style="border: medium none ; padding: 0in;">&nbsp;</p>
</td>
<td width="43">
<p style="border: medium none ; padding: 0in;">&nbsp;</p>
</td>
<td width="41">
<p style="border: medium none ; padding: 0in;">&nbsp;</p>
</td>
<td width="40">
<p style="border: medium none ; padding: 0in;">&nbsp;</p>
</td>
<td width="40">
<p style="border: medium none ; padding: 0in;">&nbsp;</p>
</td>
<td width="43">
<p style="border: medium none ; padding: 0in;">&nbsp;</p>
</td>
<td width="41">
<p style="border: medium none ; padding: 0in;">&nbsp;</p>
</td>
<td width="40">
<p style="border: medium none ; padding: 0in;">&nbsp;</p>
</td>
<td width="40" bgcolor="#ffff00">
<p style="border: medium none ; padding: 0in;">&nbsp;</p>
</td>
</tr>
<tr valign="bottom">
<td width="79">
<p style="border: medium none ; padding: 0in;"><font face="Arial"><font size="2">Avg. 					(ms)</font></font></p>
</td>
<td width="43" bgcolor="#ffff00">
<p align="right" style="border: medium none ; padding: 0in;"><font face="Arial"><font size="2">931.6</font></font></p>
</td>
<td width="41" bgcolor="#ffff00">
<p align="right" style="border: medium none ; padding: 0in;"><font face="Arial"><font size="2">469.4</font></font></p>
</td>
<td width="43">
<p align="right" style="border: medium none ; padding: 0in;"><font face="Arial"><font size="2">466.9</font></font></p>
</td>
<td width="41">
<p align="right" style="border: medium none ; padding: 0in;"><font face="Arial"><font size="2">485.8</font></font></p>
</td>
<td width="40">
<p align="right" style="border: medium none ; padding: 0in;"><font face="Arial"><font size="2">496.3</font></font></p>
</td>
<td width="40">
<p align="right" style="border: medium none ; padding: 0in;"><font face="Arial"><font size="2">473.4</font></font></p>
</td>
<td width="43">
<p align="right" style="border: medium none ; padding: 0in;"><font face="Arial"><font size="2">471.8</font></font></p>
</td>
<td width="41">
<p align="right" style="border: medium none ; padding: 0in;"><font face="Arial"><font size="2">468.2</font></font></p>
</td>
<td width="40">
<p align="right" style="border: medium none ; padding: 0in;"><font face="Arial"><font size="2">465.6</font></font></p>
</td>
<td width="40" bgcolor="#ffff00">
<p align="right" style="border: medium none ; padding: 0in;"><font face="Arial"><font size="2">519.5</font></font></p>
</td>
</tr>
</tbody>
</table>
<table width="491" cellspacing="0" cellpadding="0" border="0" style="width: 368pt; margin-left: 4.65pt; border-collapse: collapse;" class="MsoNormalTable">
<tbody>
<tr style="height: 12.75pt;">
<td width="79" valign="bottom" nowrap="nowrap" style="padding: 0in 5.4pt; width: 59pt; height: 12.75pt;">
<p class="MsoNormal"><span style="font-size: 10pt; font-family: Arial;">Pentium D   <o:p></o:p></span></p>
</td>
<td width="43" valign="bottom" nowrap="nowrap" style="padding: 0in 5.4pt; width: 32pt; height: 12.75pt;">
<p class="MsoNormal"><span style="font-size: 10pt; font-family: Arial;">2.8 G<o:p></o:p></span></p>
</td>
<td width="41" valign="bottom" nowrap="nowrap" style="padding: 0in 5.4pt; width: 31pt; height: 12.75pt;">
<p class="MsoNormal"><span style="font-size: 10pt; font-family: Arial;"><o:p> </o:p></span></p>
</td>
<td width="43" valign="bottom" nowrap="nowrap" style="padding: 0in 5.4pt; width: 32pt; height: 12.75pt;">
<p class="MsoNormal"><span style="font-size: 10pt; font-family: Arial;"><o:p> </o:p></span></p>
</td>
<td width="41" valign="bottom" nowrap="nowrap" style="padding: 0in 5.4pt; width: 31pt; height: 12.75pt;">
<p class="MsoNormal"><span style="font-size: 10pt; font-family: Arial;"><o:p> </o:p></span></p>
</td>
<td width="40" valign="bottom" nowrap="nowrap" style="padding: 0in 5.4pt; width: 30pt; height: 12.75pt;">
<p class="MsoNormal"><span style="font-size: 10pt; font-family: Arial;"><o:p> </o:p></span></p>
</td>
<td width="40" valign="bottom" nowrap="nowrap" style="padding: 0in 5.4pt; width: 30pt; height: 12.75pt;">
<p class="MsoNormal"><span style="font-size: 10pt; font-family: Arial;"><o:p> </o:p></span></p>
</td>
<td width="43" valign="bottom" nowrap="nowrap" style="padding: 0in 5.4pt; width: 32pt; height: 12.75pt;">
<p class="MsoNormal"><span style="font-size: 10pt; font-family: Arial;"><o:p> </o:p></span></p>
</td>
<td width="41" valign="bottom" nowrap="nowrap" style="padding: 0in 5.4pt; width: 31pt; height: 12.75pt;">
<p class="MsoNormal"><span style="font-size: 10pt; font-family: Arial;"><o:p> </o:p></span></p>
</td>
<td width="40" valign="bottom" nowrap="nowrap" style="padding: 0in 5.4pt; width: 30pt; height: 12.75pt;">
<p class="MsoNormal"><span style="font-size: 10pt; font-family: Arial;"><o:p> </o:p></span></p>
</td>
<td width="40" valign="bottom" nowrap="nowrap" style="padding: 0in 5.4pt; width: 30pt; height: 12.75pt;">
<p class="MsoNormal"><span style="font-size: 10pt; font-family: Arial;"><o:p> </o:p></span></p>
</td>
</tr>
</tbody>
</table>
<p class="MsoNormal"><o:p> </o:p> <span style="font-size: 10pt; font-family: Verdana;">As can be seen, using 2 threads instead of 1 on the Hyper-Threaded CPU, we only achieved roughly 0.2%. While on Pentium D we are able to achieve 98.5% speed gain.<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size: 10pt; font-family: Verdana;"><o:p></o:p>Intuitively, adding more threads than the actual core slows down the operation. But as the benchmarks suggested, the slowdown might not be as dramatic as you might have imagined. Scaling to 10 threads from 2 only slowed down the calculation speed by approximately 10% for both CPUs.</span></p>
]]></content:encoded>
			<wfw:commentRss>http://www.kerrywong.com/2006/12/26/hyper-threading-and-dual-core-performance-comparison-for-computational-intensive-applications/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Thread Safety Made Easier Under Visual Studio 2005</title>
		<link>http://www.kerrywong.com/2006/05/26/thread-safety-made-easier-under-visual-studio-2005/</link>
		<comments>http://www.kerrywong.com/2006/05/26/thread-safety-made-easier-under-visual-studio-2005/#comments</comments>
		<pubDate>Fri, 26 May 2006 09:49:32 +0000</pubDate>
		<dc:creator>kwong</dc:creator>
				<category><![CDATA[Coding]]></category>
		<category><![CDATA[Multi-threading]]></category>
		<category><![CDATA[Visual Studio .Net]]></category>

		<guid isPermaLink="false">http://dimension/2007/05/26/thread-safety-made-easier-under-visual-studio-2005/</guid>
		<description><![CDATA[Most of us know that controls in Windows forms are bound to a specific thread and are not thread safe, and thus UI controls created or referenced on non-UI thread must be marshaled back to the UI thread by using one of the Invoke methods. In the article WinForms UI Thread Invokes: An In-Depth Review [...]]]></description>
			<content:encoded><![CDATA[<p>Most of us know that controls in Windows forms are bound to a specific thread and are not thread safe, and thus UI controls created or referenced on non-UI thread must be marshaled back to the UI thread by using one of the Invoke methods.<span id="more-84"></span></p>
<p>In the article <span style="font-size: 10pt; font-family: Verdana"><a href="http://weblogs.asp.net/justin_rogers/articles/126345.aspx">WinForms UI Thread Invokes: An In-Depth Review of Invoke/BeginInvoke/InvokeRequred on asp.net</a> , Justin Rogers described in detail how to make cross thread usage of UI code in a thread-safe manner. </p>
<p>Threading problems are typically very hard to debug without the clear understanding of the problem domain. And the thread safety of UI components is vastly ignored by many developers. If you want to have an in-depth look of how the UI thread should be used please take a look at Justin’s article. What I wanted to show you is that the enhanced debugger in Visual Studio 2005 actually can detect some invalid cross-thread operations.</p>
<p class="MsoNormal" style="margin: 0in 0in 0pt"><span style="font-size: 10pt; font-family: Verdana"><o:p><font color="#000000"> </font></o:p></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt"><span style="font-size: 10pt; font-family: Verdana"><font color="#000000">Consider the following code snippet for a windows form application:</font></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt"><span style="font-size: 10pt; font-family: Verdana"><o:p></o:p></span><font color="#000000"> </font></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt"><span style="font-size: 10pt; font-family: 'Courier New'"><span><font color="#000000">        </font></span><font color="#000000"><strong><span style="color: blue">private</span></strong> <strong><span style="color: blue">void</span></strong> BtnTest_Click(<strong><span style="color: blue">object</span></strong> sender, <span style="color: teal">EventArgs</span> e) {<o:p></o:p></font></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt"><span style="font-size: 10pt; font-family: 'Courier New'"><span><font color="#000000">            </font></span><span style="color: teal"><font color="#000000">TimerCallback</font></span><font color="#000000"> timerCallBackDelegate <span style="color: gray">=</span> </font></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt"><span style="font-size: 10pt; font-family: 'Courier New'"><font color="#000000"><strong><span style="color: blue">               new</span></strong> <span style="color: teal">TimerCallback</span>(TimerCallBackHandler);<o:p></o:p></font></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt"><span style="font-size: 10pt; font-family: 'Courier New'"><span><font color="#000000">            </font></span><font color="#000000">System<span style="color: gray">.</span>Threading<span style="color: gray">.</span><span style="color: teal">Timer</span> timer <span style="color: gray">=</span> </font></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt"><span style="font-size: 10pt; font-family: 'Courier New'"><font color="#000000"><strong><span style="color: blue">               new</span></strong> System<span style="color: gray">.</span>Threading<span style="color: gray">.</span><span style="color: teal">Timer</span>(timerCallBackDelegate, <strong><span style="color: blue">null</span></strong>, <span style="color: fuchsia">100</span>, <span style="color: fuchsia">100</span>);<o:p></o:p></font></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt"><span style="font-size: 10pt; font-family: 'Courier New'"><span><font color="#000000">        </font></span><font color="#000000">}<o:p></o:p></font></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt"><span style="font-size: 10pt; font-family: 'Courier New'"><o:p><font color="#000000"> </font></o:p></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt"><span style="font-size: 10pt; font-family: 'Courier New'"><span><font color="#000000">        </font></span><font color="#000000"><strong><span style="color: blue">private</span></strong> <strong><span style="color: blue">void</span></strong> TimerCallBackHandler(<strong><span style="color: blue">object</span></strong> state) {<o:p></o:p></font></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt"><span style="font-size: 10pt; font-family: 'Courier New'"><span><font color="#000000">            </font></span><font color="#000000">i <span style="color: gray">+=</span> <span style="color: fuchsia">1</span>;<o:p></o:p></font></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt"><span style="font-size: 10pt; font-family: 'Courier New'"><o:p><font color="#000000"> </font></o:p></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt"><span style="font-size: 10pt; font-family: 'Courier New'"><span><font color="#000000">            </font></span><font color="#000000">Label1<span style="color: gray">.</span>Text <span style="color: gray">=</span> i<span style="color: gray">.</span>ToString();<o:p></o:p></font></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt"><span style="font-size: 10pt; font-family: 'Courier New'"><span><font color="#000000">        </font></span><font color="#000000">}</font></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt"><font color="#000000"> </font></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt"><span style="font-size: 10pt; font-family: Verdana"><font color="#000000">The button click event creates a timer and on the timer callback a label is updated. If you run this code in Visual Studio 2003, it would appear that everything runs fine. But there is a potential bug in TimerCallBackHandler, namely updating the Label control on a non-UI thread (spawned by the timer). </font></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt"><span style="font-size: 10pt; font-family: Verdana"></span><font color="#000000"> </font></p>
<p class="MsoNormal" dir="ltr" style="margin: 0in 0in 0pt"><span style="font-size: 10pt; font-family: Verdana"><font color="#000000">If you run the above code in Vistual Studio 2005, you will get the following message:<o:p></o:p></font></span></p>
<blockquote dir="ltr" style="margin-right: 0px">
<p class="MsoNormal" style="margin: 0in 0in 0pt"><span style="font-size: 10pt; font-family: Verdana"><font color="#000000"><em>InvalidOperationexception was unhandled<o:p></o:p></em></font></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt"><span style="font-size: 10pt; font-family: Verdana"><o:p></o:p></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt"><span style="font-size: 10pt; font-family: Verdana"><font color="#000000"><em>Cross-thread operation not valid: Control ‘Label1’ accessed from a thread other than the tread it was created on.<o:p></o:p></em></font></span></p>
</blockquote>
<p><o:p> </o:p></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt"><span style="font-size: 10pt; font-family: Verdana"><font color="#000000">To fix this problem we need to replace the TimerCallBackHandler with the code below:</font></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt"><span style="font-size: 10pt; font-family: Verdana"><o:p></o:p></span><font color="#000000"> </font></p>
<p><o:p> </o:p></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt"><span style="font-size: 10pt; font-family: 'Courier New'"><span><font color="#000000">        </font></span><font color="#000000"><strong><span style="color: blue">private</span></strong> <strong><span style="color: blue">void</span></strong> TimerCallBackHandler(<strong><span style="color: blue">object</span></strong> state) {<o:p></o:p></font><o:p><font color="#000000"> </font></o:p></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt"><span style="font-size: 10pt; font-family: 'Courier New'"><span><font color="#000000">            </font></span><font color="#000000"><strong><span style="color: blue">if</span></strong> (Label1<span style="color: gray">.</span>InvokeRequired) {<o:p></o:p></font></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt"><span style="font-size: 10pt; font-family: 'Courier New'"><span><font color="#000000">                </font></span><span style="color: teal"><font color="#000000">TimerCallback</font></span><font color="#000000"> callBackDelegage <span style="color: gray">=</span> </font></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt"><span style="font-size: 10pt; font-family: 'Courier New'"><font color="#000000"><strong><span style="color: blue">                  new</span></strong> <span style="color: teal">TimerCallback</span>(TimerCallBackHandler);<o:p></o:p></font></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt"><span style="font-size: 10pt; font-family: 'Courier New'"><span><font color="#000000">                </font></span><font color="#000000">BeginInvoke(callBackDelegage, <strong><span style="color: blue">new</span></strong> <strong><span style="color: blue">object</span></strong>[] { state });<o:p></o:p></font></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt"><span style="font-size: 10pt; font-family: 'Courier New'"><span><font color="#000000">            </font></span><font color="#000000">} <strong><span style="color: blue">else</span></strong> {</font></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt"><span style="font-size: 10pt; font-family: 'Courier New'"><span><font color="#000000">                </font></span><font color="#000000">i <span style="color: gray">+=</span> <span style="color: fuchsia">1</span>;</font></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt"><span style="font-size: 10pt; font-family: 'Courier New'"><span><font color="#000000">                </font></span><font color="#000000">Label1<span style="color: gray">.</span>Text <span style="color: gray">=</span> i<span style="color: gray">.</span>ToString();<o:p></o:p></font></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt"><span style="font-size: 10pt; font-family: 'Courier New'"><span><font color="#000000">            </font></span><font color="#000000">}<o:p></o:p></font></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt"><span style="font-size: 10pt; font-family: 'Courier New'"><span><font color="#000000">        </font></span><font color="#000000">}</font></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt"><span style="font-size: 10pt; font-family: 'Courier New'"><o:p></o:p></span><font color="#000000"> </font></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt"><span style="font-size: 10pt; font-family: Verdana"><font color="#000000">Where the call to update Label1 is marshalled back to the UI thread if Invoke is required. </font></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt"><span style="font-size: 10pt; font-family: Verdana"></span><font color="#000000"> </font></p>
<p><span style="font-size: 10pt; font-family: Verdana"></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt"><span style="font-size: 10pt; font-family: Verdana"><font color="#000000">Clearly, such coding errors will be caught more easily with the new Visual Studio 2005 IDE. Note that this is a feature of the new development environment not the new version of the CLR. If you compile and run the first code snippet using 2.0 framework and run it without starting from the IDE, you will not get the exception message either. <o:p></o:p></font></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt"><span style="font-size: 10pt; font-family: Verdana"><o:p><font color="#000000"> </font></o:p></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt"><span style="font-size: 10pt; font-family: Verdana"><font color="#000000">As a side note, it seems that the internal error reporting mechanism has changed between framework 1.1 and 2.0. If we change the TimerCallBackHandler to the snippet below (note, here we create the label control on a thread other than the UI thread):<o:p></o:p></font></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt"><o:p></o:p><font color="#000000"> </font></p>
<p><o:p> </o:p></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt"><span style="font-size: 10pt; font-family: 'Courier New'"><span><font color="#000000">        </font></span><font color="#000000"><strong><span style="color: blue">private</span></strong> <strong><span style="color: blue">void</span></strong> TimerCallBackHandler(<strong><span style="color: blue">object</span></strong> state) {<o:p></o:p></font></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt"><span style="font-size: 10pt; font-family: 'Courier New'"><span><font color="#000000">            </font></span><span style="color: teal"><font color="#000000">Label</font></span><font color="#000000"> l <span style="color: gray">=</span> <strong><span style="color: blue">new</span></strong> <span style="color: teal">Label</span>();<o:p></o:p></font></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt"><span style="font-size: 10pt; font-family: 'Courier New'"><span><font color="#000000">            </font></span><font color="#000000">l<span style="color: gray">.</span>Left <span style="color: gray">=</span> <span style="color: fuchsia">10</span>;<o:p></o:p></font></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt"><span style="font-size: 10pt; font-family: 'Courier New'"><span><font color="#000000">            </font></span><font color="#000000">l<span style="color: gray">.</span>Top <span style="color: gray">=</span> <span style="color: fuchsia">10</span>;<o:p></o:p></font></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt"><span style="font-size: 10pt; font-family: 'Courier New'"><span><font color="#000000">         </font></span><span><font color="#000000">   </font></span><font color="#000000">l<span style="color: gray">.</span>Text <span style="color: gray">=</span> <span style="color: red">&#8220;Test &#8220;</span>;<o:p></o:p></font></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt"><span style="font-size: 10pt; font-family: 'Courier New'"><span><font color="#000000">            </font></span><font color="#000000"><strong><span style="color: blue">this</span></strong><span style="color: gray">.</span>Controls<span style="color: gray">.</span>Add(l);<o:p></o:p></font></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt"><span style="font-size: 10pt; font-family: 'Courier New'"><span><font color="#000000">        </font></span><font color="#000000">}<o:p></o:p></font></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt"><font color="#000000"> </font></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt"><span style="font-size: 10pt; font-family: Verdana"><font color="#000000">We would get the following exception message in Visual Studio 2003:</font></span></p>
<p><span style="font-size: 10pt; font-family: Verdana"></span></p>
<blockquote dir="ltr" style="margin-right: 0px">
<p class="MsoNormal" style="margin: 0in 0in 0pt"><span style="font-size: 10pt; font-family: Verdana"><font color="#000000"><em>An unhandled exception of type &#8216;System.ArgumentException&#8217; occurred in system.windows.forms.dll<o:p></o:p></em></font></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt"><span style="font-size: 10pt; font-family: Verdana"><o:p></o:p></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt"><span style="font-size: 10pt; font-family: Verdana"><font color="#000000"><em>Additional information: Controls created on one thread cannot be parented to a control on a different thread.<o:p></o:p></em></font></span></p>
</blockquote>
<p><o:p> </o:p></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt"><span style="font-size: 10pt; font-family: Verdana"><font color="#000000">But if we run the same code in Visual Studio 2005: the following exception message appears:<o:p></o:p></font></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt"><span style="font-size: 10pt; font-family: Verdana"><o:p></o:p></span></p>
<blockquote dir="ltr" style="margin-right: 0px">
<p class="MsoNormal" style="margin: 0in 0in 0pt"><span style="font-size: 10pt; font-family: Verdana"><font color="#000000"><em>InvalidOperationexception was unhandled</em></font></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt"><span style="font-size: 10pt; font-family: Verdana"></span><span style="font-size: 10pt; font-family: Verdana"><font color="#000000"><em>Cross-thread operation not valid: Control ‘Label1’ accessed from a thread other than the tread it was created on.</em></font></span></p>
</blockquote>
<p class="MsoNormal" dir="ltr" style="margin: 0in 0in 0pt"><span style="font-size: 10pt; font-family: Verdana"><o:p><font color="#000000">Which essentially is the same as the message from the first exception.</font></o:p></span></p>
]]></content:encoded>
			<wfw:commentRss>http://www.kerrywong.com/2006/05/26/thread-safety-made-easier-under-visual-studio-2005/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>
