<?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"
	>

<channel>
	<title>parallel roads</title>
	<atom:link href="http://www.parallelroads.com/blog/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.parallelroads.com/blog</link>
	<description>thoughts on technology and design by Rick Molloy</description>
	<pubDate>Sat, 08 Nov 2008 18:00:55 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.6.3</generator>
	<language>en</language>
			<item>
		<title>Building a Future in Visual Studio 2010</title>
		<link>http://www.parallelroads.com/blog/2008/11/building-an-asynchronous-future-in-visua/</link>
		<comments>http://www.parallelroads.com/blog/2008/11/building-an-asynchronous-future-in-visua/#comments</comments>
		<pubDate>Wed, 05 Nov 2008 06:57:18 +0000</pubDate>
		<dc:creator>rickmolloy</dc:creator>
		
		<category><![CDATA[C++0x]]></category>

		<category><![CDATA[concurrency]]></category>

		<category><![CDATA[concurrency runtime]]></category>

		<category><![CDATA[parallelism]]></category>

		<category><![CDATA[technology]]></category>

		<guid isPermaLink="false">http://www.parallelroads.com/blog/2008/11/building-an-asynchronous-future-in-visua/</guid>
		<description><![CDATA[A very common use case for concurrency is computing a value asynchronously, this is often called a ‘future’ and frequently folks talk about promises of future values as well.  Section 35 of the C++0x draft has a proposal for a future construct, and the Parallel Extensions to .NET also has a .NET class for a [...]]]></description>
			<content:encoded><![CDATA[<p>A very common use case for concurrency is computing a value asynchronously, this is often called a ‘future’ and frequently folks talk about promises of future values as well.  <a href="http://open-std.org/JTC1/SC22/WG21/docs/papers/2008/n2798.pdf">Section 35 of the C++0x draft</a> has a proposal for a future construct, and the Parallel Extensions to .NET also has a .NET class for a future.</p>
<p>Unfortunately futures aren’t included in the Parallel Pattern Library or the Asynchronous Agents Library in the Visual Studio 2010 CTP. But I’d like to show a couple of ways to build a simple future class with the CTP.</p>
<h3>A synopsis for a simple future</h3>
<p>Here’s a simple template class synopsis for a future that allows you to construct a future with a functor and then retrieve it’s value asynchronously:</p>
<pre class="code"><span style="color: #0000ff;">   template
</span>   &lt;<span style="color: #0000ff;">class </span>T&gt;
<span style="color: #0000ff;">   class </span>future{
<span style="color: #0000ff;">   public</span>:
       <span style="color: #0000ff;">template
       </span>&lt;<span style="color: #0000ff;">class </span>Functor&gt;
       future(Functor fn);
       ~future();
       T GetValue();
   };</pre>
<p><a href="http://11011.net/software/vspaste"></a></p>
<p>Here’s a simple example of creating a future with a lambda and retrieving its value:</p>
<pre class="code">   future&lt;<span style="color: #0000ff;">int</span>&gt; f([](){
       <span style="color: #0000ff;">return </span>2;
   });
<span style="color: #0000ff;">   int </span>result = f.GetValue();
   assert(result == 2);</pre>
<p><a href="http://11011.net/software/vspaste"></a><a href="http://11011.net/software/vspaste"></a></p>
<p>The scenario above isn’t super exciting, and it isn’t really asynchronous but it’s functional and in practice a lot of concurrency can be realized by chaining and combining futures together into expressions and waiting on the final result.</p>
<h3>Implementing it step-by-step</h3>
<p>Let’s take a look at one possible way of implementing this template class.  There are in fact other strategies which can be more efficient if you’re building lots of very fine grained futures, but this is very easy to write and in practice for relatively large expressions is probably quite fine.  I’ll mention the overheads and tradeoffs as I go along.</p>
<p>The first thing we’ll need in our future class is a place to store that value and a way to compute it asynchronously. From the CTP, I’m going to use both the task constructs in &lt;ppl.h&gt; and the message blocks in &lt;agents.h&gt;. To do this let’s create a couple private member variables starting with an overwrite_buffer:</p>
<pre class="code">   overwrite_buffer&lt;T&gt; value_;</pre>
<p>The overwrite_buffer is a template class defined in agents.h, called a “message block” that allows a single value to be “sent” to it or stored, and copies of the most recent value are extracted upon request typically using a helper function called &#8216;”receive”.  We will use this to store our result. It’s also important to note that ideally this would be a single assignment variable, but that isn’t included in the CTP (so I’m not using it here).</p>
<p>The next thing we need is a way to compute the value asynchronously and to do this I’m going to use a task_group from the PPL.</p>
<p>   task_group tg_;</p>
<p>task_group is a class defined in &lt;ppl.h&gt; and is used to schedule tasks, wait for them all to complete or cancel any outstanding tasks assigned to it.  In the CTP the task_group is actually thread-safe so I can add tasks to it, call wait or cancel from any thread and get well defined behavior.</p>
<p>Now let’s implement the constructor which will schedule a task to run the functor passed in and then place it’s result into our overwrite_buffer.  We schedule a task with the task_group::run template member function and assign it to the overwrite_buffer with the template method send defined in agents.h, note that here in the lambda I’m capturing fn &amp; this by value and copying them into the lambda:</p>
<pre class="code"><span style="color: #0000ff;">   template
</span>   &lt;<span style="color: #0000ff;">class </span>Functor&gt;
   future(Functor fn){
       tg_.run([fn,<span style="color: #0000ff;">this</span>](){
           send(<span style="color: #0000ff;">this</span>-&gt;value_,fn());
       });
   }</pre>
<p>I mentioned I would call out overheads and we’ve just created one. The overload for task_group::run that we just used will have a heap allocation because if you notice the ‘fn’ object is created on the stack and would be destructed if once the method exits, so we need to allocate a copy on the heap and delete it after it’s been executed (or canceled, or faulted).  There is an overloaded task_group::run method that takes a reference to a task_handle&lt;T&gt; that can be used which can avoid this particular overhead, but it will rely on the user to manage the lifetime of that task and ensure it is still valid whenever it is executed.</p>
<p>Next we need to implement GetValue to return the result.  To do this we’re going to use the template method receive, which is the analog of send.  When receive is used on a ‘message block’ it will actually block and wait for that value to be computed.  One of the important things about using the agents APIs to do this is that receive will generate a blocking notification to the Concurrency Runtime and the runtime will use this blocking notification to run additional work if it’s been scheduled.  Here’s how GetValue is implemented, it’s incredibly straightforward:</p>
<pre class="code">   T GetValue(){
       <span style="color: #0000ff;">return </span>receive(value_);
   };</pre>
<p>The last thing we need to do is implement the destructor. Here I’m just calling task_group::wait to ensure that our task is cleaned up.  You may also want to call task_group::cancel if you don’t need that task to run to completion.</p>
<pre class="code">   ~future(){
       tg_.wait();
   };</pre>
<h3>Putting it all together</h3>
<p>Here’s the complete program, that was a lot of typing for not very much code:</p>
<pre class="code"><span style="color: #0000ff;">#include </span><span style="color: #a31515;">&lt;agents.h&gt;
</span><span style="color: #0000ff;">template
</span>&lt;<span style="color: #0000ff;">class </span>T&gt;
<span style="color: #0000ff;">class </span>future{
    overwrite_buffer&lt;T&gt; value_;
    task_group tg_;
<span style="color: #0000ff;">public</span>:
    <span style="color: #0000ff;">template
    </span>&lt;<span style="color: #0000ff;">class </span>Functor&gt;
    future(Functor fn){
        tg_.run([fn,<span style="color: #0000ff;">this</span>](){
            send(<span style="color: #0000ff;">this</span>-&gt;value_,fn());
        });
    }
    ~future(){
        tg_.wait();
    };
    T GetValue(){
        <span style="color: #0000ff;">return </span>receive(value_);
    };
};
<span style="color: #0000ff;">void </span>main(){
    future&lt;<span style="color: #0000ff;">int</span>&gt; f([](){
        <span style="color: #0000ff;">return </span>2;
    });
    <span style="color: #0000ff;">int </span>result = f.GetValue();
    assert(result == 2);
}</pre>
<h3>Wrapping it up</h3>
<p>I’m still not doing anything very interesting here and I could always do something like compute the fibonacci sequence the slow way. But in practice there are many more uses for a future, like applying a filter to an image or various forms of i/o where there is a combination of CPU work and perhaps a significant amount of latency.</p>
<p>As I alluded there are other implementation strategies here, for example the transform message block in &lt;agents.h&gt; could be used instead of a task_group and a series of message blocks could be combined to implement richer functionality like that described in the C++0x specification, most notably a timer and choice (wait for one of n inputs to be sent) or join would likely be required to ease implementation.</p>
<p>and here’s that recursive Fibonacci sequence, it’s late and I can’t resist:</p>
<pre class="code"><span style="color: #0000ff;">   int </span>fib(<span style="color: #0000ff;">int </span>n){
       <span style="color: #0000ff;">if</span>(n &lt; 2)
           <span style="color: #0000ff;">return </span>n;
       future&lt;<span style="color: #0000ff;">int</span>&gt; n1([n](){<span style="color: #0000ff;">return </span>fib(n-1);});
       future&lt;<span style="color: #0000ff;">int</span>&gt; n2([n](){<span style="color: #0000ff;">return </span>fib(n-2);});
       <span style="color: #0000ff;">return </span>n1.GetValue() + n2.GetValue();
   }</pre>
<p><a href="http://11011.net/software/vspaste"></a></p>
<p>-Rick</p>
]]></content:encoded>
			<wfw:commentRss>http://www.parallelroads.com/blog/2008/11/building-an-asynchronous-future-in-visua/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Notes from PDC 2008: Day 1</title>
		<link>http://www.parallelroads.com/blog/2008/10/notes-from-pdc-2008-day-1/</link>
		<comments>http://www.parallelroads.com/blog/2008/10/notes-from-pdc-2008-day-1/#comments</comments>
		<pubDate>Tue, 28 Oct 2008 07:18:30 +0000</pubDate>
		<dc:creator>rickmolloy</dc:creator>
		
		<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://www.parallelroads.com/blog/2008/10/notes-from-pdc-2008-day-1/</guid>
		<description><![CDATA[It&#8217;s been a very long but great day today at PDC. I talked earlier in the day on the Parallel Pattern Library, Asynchronous Agents Library and Concurrency Runtime and demonstrating the CTP of Visual Studio 2010.&#160;&#160; Hazim Shafi also spoke on multi-threaded and parallel performance.&#160; I spent the latter portion of the day talking to [...]]]></description>
			<content:encoded><![CDATA[<p>It&#8217;s been a very long but great day today at PDC. I talked earlier in the day on the Parallel Pattern Library, Asynchronous Agents Library and Concurrency Runtime and demonstrating the CTP of Visual Studio 2010.&nbsp;&nbsp; Hazim Shafi also spoke on multi-threaded and parallel performance.&nbsp; I spent the latter portion of the day talking to developers at the booth we&#8217;re sharing with the High Performance Computing team. </p>
<p>There will be several more talks from the Parallel Computing Platform team at PDC this week, our architect Niklas will be doing a deep dive on the Concurrency Runtime and discussing extensibility, Daniel Moth will be talking soon about the Task Parallel Library and PLINQ and there&#8217;s several Symposium sessions lined up.</p>
<p>While I&#8217;m here at the PDC I&#8217;ll post on my personal blog summarizing experiences and I&#8217;ll also try to get a few more examples posted either here or on our team blog which demonstrate the functionality included in the CTP.&nbsp; That&#8217;s it for now though, it&#8217;s been a long day.&nbsp; </p>
<p>-Rick</p>
<div style="padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px; padding-top: 0px; display: inline" id="scid:0767317B-992E-4b12-91E0-4F059A8CECA8:759328d9-2f12-4089-9f0f-8a5c8316ede2" class="wlWriterSmartContent">Technorati Tags: <a href="http://technorati.com/tags/PDC2008" rel="tag">PDC2008</a></div>
]]></content:encoded>
			<wfw:commentRss>http://www.parallelroads.com/blog/2008/10/notes-from-pdc-2008-day-1/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Beauty is in the eye of the beholder</title>
		<link>http://www.parallelroads.com/blog/2008/06/beauty-is-in-the-eye-of-the-beholder/</link>
		<comments>http://www.parallelroads.com/blog/2008/06/beauty-is-in-the-eye-of-the-beholder/#comments</comments>
		<pubDate>Wed, 11 Jun 2008 16:49:48 +0000</pubDate>
		<dc:creator>rickmolloy</dc:creator>
		
		<category><![CDATA[personal]]></category>

		<category><![CDATA[technology]]></category>

		<category><![CDATA[books]]></category>

		<category><![CDATA[c++]]></category>

		<category><![CDATA[code]]></category>

		<guid isPermaLink="false">http://www.parallelroads.com/blog/2008/06/beauty-is-in-the-eye-of-the-beholder/</guid>
		<description><![CDATA[Several folks have blogged recently about the book beautiful code, some like Jeff have blogged multiple times about it.&#160; The reactions are somewhat mixed and not really surprisingly.&#160; I really would expect beauty, which is decidedly aesthetic to vary from person to person.
I&#8217;ll be honest and state that I haven&#8217;t read this book yet, but [...]]]></description>
			<content:encoded><![CDATA[<p>Several folks have blogged recently about the book beautiful code, some like Jeff have <a href="http://www.codinghorror.com/blog/archives/001062.html">blogged</a> <a href="http://www.codinghorror.com/blog/archives/001131.html">multiple times</a> about it.&nbsp; The reactions are <a href="http://alarmingdevelopment.org/?p=79">somewhat mixed</a> and not really surprisingly.&nbsp; I really would expect beauty, which is decidedly aesthetic to vary from person to person.</p>
<p>I&#8217;ll be honest and state that I haven&#8217;t read this book yet, but it is on my to read list.&nbsp; Given that I can&#8217;t comment yet on the book, but I can share some traits I find beautiful in code.</p>
<p><strong>Readable</strong>, it should be very clear what the code is doing from the names of the objects, their instances and any functions or methods.&nbsp; This can be done with or without language support, and in a very personal sense, I will tend to avoid language features that make code unreadable. </p>
<p>I also really admire code that is <strong>decisive </strong>some folks like to call this declarative, but I think it can be more.&nbsp; I <strong>dislike excessive branching </strong>mostly because I am very cautious and tend to get worried about testing a method with a lot of branching in it.</p>
<p>I <strong>admire stateless functions and objects </strong>I tend to find it fascinating when I see code that enables useful scenarios that is built out of stateless objects.&nbsp; This also tends to lead to rather <strong>simple code </strong>which is also quite nice, because when I&#8217;ve long forgotten what code does if it&#8217;s readable and straightforward I can usually pick it back up again soon.</p>
<p>I fully expect your mileage to vary because appreciation of beauty is so personal.&nbsp; I&#8217;ll also chime back in after I&#8217;ve read the book.</p>
<p>~Rick</p>
]]></content:encoded>
			<wfw:commentRss>http://www.parallelroads.com/blog/2008/06/beauty-is-in-the-eye-of-the-beholder/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Keeping things clean with lambdas &#38; the STL</title>
		<link>http://www.parallelroads.com/blog/2008/05/keeping-things-clean-with-lambdas-the-stl/</link>
		<comments>http://www.parallelroads.com/blog/2008/05/keeping-things-clean-with-lambdas-the-stl/#comments</comments>
		<pubDate>Fri, 30 May 2008 16:58:22 +0000</pubDate>
		<dc:creator>rickmolloy</dc:creator>
		
		<category><![CDATA[concurrency]]></category>

		<category><![CDATA[technology]]></category>

		<guid isPermaLink="false">http://www.parallelroads.com/blog/2008/05/keeping-things-clean-with-lambdas-the-stl/</guid>
		<description><![CDATA[I was reading Michael Feather&#8217;s blog the other day and he mentioned how he thinks lambdas may create a tension between clutter &#38; expressiveness.&#160; In the spirit of consensus building, I&#8217;ll tend to agree that like any tool they can be overused or misapplied.&#160; But, there are many extremely useful ways in which they actually [...]]]></description>
			<content:encoded><![CDATA[<p>I was reading Michael Feather&#8217;s blog the other day and he mentioned how he thinks lambdas <a href="http://beautifulcode.oreillynet.com/2008/03/the_lambda_function_tradeoff_c.php">may create a tension between clutter &amp; expressiveness</a>.&#160; In the spirit of consensus building, I&#8217;ll tend to agree that like any tool they can be overused or misapplied.&#160; But, there are many extremely useful ways in which they actually reduce clutter, improve readability and quality, and serve as tools to help us refactor and test our code.</p>
<h3>Thinking about this on the plane</h3>
<p>Last week I was in sunny &amp; warm California talking to some customers and I picked up <a href="http://www.amazon.com/gp/product/B0019OY9JY?ie=UTF8&amp;tag=parallelroads-20&amp;linkCode=as2&amp;camp=1789&amp;creative=9325&amp;creativeASIN=B0019OY9JY">Game Programming Gems 6</a><img style="margin: 0px; border-top-style: none! important; border-right-style: none! important; border-left-style: none! important; border-bottom-style: none! important" height="1" alt="" src="http://www.assoc-amazon.com/e/ir?t=parallelroads-20&amp;l=as2&amp;o=1&amp;a=B0019OY9JY" width="1" border="0" /> from the library.&#160; One of the most interesting articles to me was an article on using a variant of the Levenshtein string matching algorithm to help find close matches in library routines for maps and other developer / designer created content.</p>
<h3>Loved the article, hated the code</h3>
<p>Let&#8217;s be really, really clear about this. I loved this article; I&#8217;m really a geek about this sort of thing.&#160; Unfortunately when I read the code example, like Harrison Ford in now two movies I can only say&#8230; &quot;I have a bad feeling about this&quot;</p>
<p>Here it is:</p>
<p><span style="color: #008000">// This is an example of a template function that can be used to find      <br />// a closest match in a std map, assuming the map uses strings as       <br />// the primary index. Note that we must search through every string       <br />// in the map to find the closest match, so it is a very       <br />// slow function: O(n), which is linear time. It&#8217;s handy for       <br />// debugging during development, but should not be used in       <br /></span><span style="color: #008000">// release builds.      <br /></span><span style="color: #0000ff">template</span>&lt;<span style="color: #0000ff">class </span>_InIt&gt;     <br /><span style="color: #0000ff">inline </span>_InIt closestMatch(_InIt _First, _InIt _Last, <span style="color: #0000ff">char const </span>*val, <span style="color: #0000ff">float </span>limit)     <br />{     <br />&#160;&#160; <span style="color: #0000ff">float </span>maxVal = limit;     <br />&#160;&#160; _InIt _max = _Last;     <br />&#160;&#160; <span style="color: #0000ff">for </span>(; _First != _Last; ++_First){     <br />&#160;&#160;&#160;&#160; <span style="color: #0000ff">float </span>newVal = stringMatch(_First-&gt;first.c_str(), val);     <br />&#160;&#160;&#160;&#160; <span style="color: #0000ff">if </span>(newVal &gt; maxVal)     <br />&#160;&#160;&#160; {     <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; maxVal = newVal; _max = _First;     <br />&#160;&#160;&#160;&#160; }&#160; <br />&#160;&#160; }&#160; <br /><span style="color: #0000ff">&#160;&#160; return </span>(_max);     <br />}</p>
<p><a href="http://11011.net/software/vspaste"></a></p>
<p>The first thing I noticed about this template function is that it the naming convention seemed a bit inconsistent.</p>
<p>Then I noticed something more problematic,&#160; this function looks awfully familiar.</p>
<p>In fact it looks almost identical to std::max_element or std::accumulate depending on the mood your in; even the parameters are the same.&#160; Only rather than reusing the existing STL function it really looks like accumulate was copied and pasted into the sample and then edited.&#160; Here&#8217;s max_element from my VS installation, I hope it&#8217;s clear how similar this code is:</p>
<p><span style="color: #008000">// TEMPLATE FUNCTION max_element WITH PRED      <br /></span><span style="color: #0000ff">template</span>&lt;<span style="color: #0000ff">class </span>_FwdIt, <span style="color: #0000ff">class </span>_Pr&gt;     <br /><span style="color: #0000ff">inline </span>_FwdIt _Max_element(_FwdIt _First, _FwdIt _Last, _Pr _Pred)     <br />{&#160; <br /><span style="color: #008000"><font color="#444444">&#160;&#160; </font>// find largest element, using _Pred       <br /></span>&#160;&#160; _FwdIt _Found = _First;     <br /><span style="color: #0000ff">&#160;&#160; if </span>(_First != _Last)     <br /><span style="color: #0000ff">&#160;&#160;&#160;&#160;&#160; for </span>(; ++_First != _Last; )     <br /><span style="color: #0000ff">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; if </span>(_DEBUG_LT_PRED(_Pred, *_Found, *_First))     <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; _Found = _First;&#160; <br />&#160;&#160; <span style="color: #0000ff">return </span>(_Found);     <br />}</p>
<p><a href="http://11011.net/software/vspaste"></a></p>
<p><span style="color: #008000">     <br /></span></p>
<h3>Let&#8217;s clean this up</h3>
<p>I&#8217;d also like to take a moment to share where I&#8217;d like to take this, but showing my hand there&#8217;s an extraneous function call here and I think that we can get a lot closer to code that looks like this:</p>
<p><span style="color: #0000ff">template</span>&lt;<span style="color: #0000ff">class </span>InputIterator&gt;     <br /><span style="color: #0000ff">inline </span>InputIterator newClosestMatch( InputIterator begin,     <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; InputIterator end,&#160; <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; std::string val, <span style="color: #0000ff">float </span>limit     <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; )     <br />{&#160; <br />&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #0000ff">return </span>std::max_element(begin,end,[&amp;](&#8230;){&#8230;.});     <br />}</p>
<p>But first we have to unravel it a bit.&#160; Perhaps it isn&#8217;t so hard&#8230;&#160; We need to note that we&#8217;re using the stringMatch function as our comparison function.&#160; The key piece to unravel is the call to stringMatch to retrieve the comparison number and that this can be re-expressed in a comparison function as:</p>
<p>&#160;&#160;&#160;&#160;&#160; stringMatch(leftString, val) &lt; stringMatch(rightString,val)</p>
<p><a href="http://11011.net/software/vspaste"></a>The next important change is that we need to refactor slightly to work with the collection element (in this case it&#8217;s a hash_map) directly as follows:</p>
<p><span style="color: #0000ff">typedef </span>std::pair&lt;std::string,<span style="color: #0000ff">int</span>&gt; StrPair;     </p>
<p> <span style="color: #0000ff">template</span>&lt;<span style="color: #0000ff">class </span>InputIterator&gt;   <br /><span style="color: #0000ff">inline </span>InputIterator newClosestMatch( InputIterator begin,   <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; InputIterator end,&#160; <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; std::string val, <span style="color: #0000ff">float </span>limit   <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; ) {&#160;&#160; <br /><span style="color: #0000ff"><font color="#444444">&#160;&#160; </font>return </span>std::max_element( begin, end,&#160; <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; [&amp;](<span style="color: #0000ff">const </span>StrPair&amp; left, <span style="color: #0000ff">const </span>StrPair&amp; right){&#160;&#160; <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #0000ff">return </span>( stringMatch(left.first.c_str(),val.c_str()) &lt;&#160; <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; stringMatch(right.first.c_str(),val.c_str()) );&#160; <br />&#160;&#160;&#160; };   <br />}
<p><a href="http://11011.net/software/vspaste"></a><a href="http://11011.net/software/vspaste"></a></p>
<p>Oh and by the way we&#8217;re done now and we basically have reduced several lines of user written code with declarative code that uses a single lambda to compute the expression.</p>
<p>I dunno about you, but this makes me very happy.</p>
<p><a href="http://11011.net/software/vspaste"></a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.parallelroads.com/blog/2008/05/keeping-things-clean-with-lambdas-the-stl/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Is 4 cores really the end? (Part 3)</title>
		<link>http://www.parallelroads.com/blog/2008/04/is-4-cores-really-the-end-part-3/</link>
		<comments>http://www.parallelroads.com/blog/2008/04/is-4-cores-really-the-end-part-3/#comments</comments>
		<pubDate>Mon, 21 Apr 2008 23:53:03 +0000</pubDate>
		<dc:creator>rickmolloy</dc:creator>
		
		<category><![CDATA[concurrency]]></category>

		<category><![CDATA[technology]]></category>

		<guid isPermaLink="false">http://www.parallelroads.com/blog/2008/04/is-4-cores-really-the-end-part-3/</guid>
		<description><![CDATA[OK, so I promised to add some more traces showing speedups of real user applications, i.e. not developer tools or benchmark suites.&#160; The weekends over, but I thought that I would add an application that I&#8217;m confident a lot of people are using&#8230; Internet Explorer 7.
How does IE use 4-cores? 
Well, if you remember that [...]]]></description>
			<content:encoded><![CDATA[<p>OK, so <a href="http://www.parallelroads.com/blog/2008/04/is-4-cores-really-the-end-part-2/">I promised</a> to add some more traces showing speedups of real user applications, i.e. not developer tools or benchmark suites.&#160; The weekends over, but I thought that I would add an application that I&#8217;m confident a lot of people are using&#8230; Internet Explorer 7.</p>
<h3>How does IE use 4-cores? </h3>
<p>Well, if you remember that IE let&#8217;s you have multiple home pages in multiple tabs.&#160; I use this feature to track the news sites I read and my web-mail, and it&#8217;s definitely using so here&#8217;s what it looks like on a quad core:</p>
<p><a href="http://www.parallelroads.com/blog/wp-content/uploads/2008/04/ie-4way.png"><img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="113" alt="ie_4way" src="http://www.parallelroads.com/blog/wp-content/uploads/2008/04/ie-4way-thumb.png" width="244" border="0" /></a> </p>
<p>You should be able to see that it&#8217;s taking about 7.5 seconds to launch IE then the CPU to settle down.&#160; More importantly, there&#8217;s a period of approximately 2 seconds between the 3.5 &amp;&#160; 5.5 second tickmarks where the CPU utilization is at &gt; 70%.</p>
<h3>What about 8-cores?</h3>
<p>I happen to be fortunate enough to run this same scenario on a dual-socket quad core and so I collected a trace on that as well.&#160; Keep in mind that the clock speeds of these 2 are <u>completely different</u> so comparing benchmarks between them is quite inadvisable, but I&#8217;m going to do it anyways :).</p>
<p>You can see in the capture below that on the 8-way the time has been reduced to 5.5 seconds&#8230; it&#8217;s still using &gt; 70% of the CPU but now it&#8217;s 70% of&#160; 8 cores instead of 4&#8230;</p>
<p><a href="http://www.parallelroads.com/blog/wp-content/uploads/2008/04/ie-8way.png"><img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="110" alt="ie_8way" src="http://www.parallelroads.com/blog/wp-content/uploads/2008/04/ie-8way-thumb.png" width="244" border="0" /></a> </p>
<p>And that period of high CPU utilization (between 8.5 seconds &amp; 9 seconds on this box) is now only about a second long.</p>
<h3></h3>
<h3>Hey it didn&#8217;t get twice as fast, why is this good?</h3>
<p>&#160;</p>
<p>I could very easily have shown a paint.net application getting twice as fast as it did on my quad-core, but that&#8217;s actually not very interesting.&#160; The point here is that we took a look at an existing application that was easy to find use that was already multi-threaded and was designed to take advantage of inherent latencies in I/O and showed that some decent speedups were available to be taken advantage of with a multi-core system.&#160;&#160; This is really just an opportunistic performance win and real users,&#160; using real applications will see some noticeable improvements without <a href="http://www.codinghorror.com/blog/archives/001103.html">installing developer tools or running gadzillions of applications at once</a> (and don&#8217;t knock that &#8211; I think I have about 40 windows open on my desktop right now).</p>
<p>Perhaps in a future post, I&#8217;ll post a couple ideas of how to take even better advantage of multiple cores using something like the Task Parallel Library or PLINQ, but for now that&#8217;s it.</p>
<p>-Rick    </p>
]]></content:encoded>
			<wfw:commentRss>http://www.parallelroads.com/blog/2008/04/is-4-cores-really-the-end-part-3/feed/</wfw:commentRss>
		</item>
	</channel>
</rss>
