<?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>Parallelcoding.com &#187; C#</title>
	<atom:link href="http://www.parallelcoding.com/tag/c/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.parallelcoding.com</link>
	<description></description>
	<lastBuildDate>Mon, 16 Aug 2010 11:55:45 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=</generator>
		<item>
		<title>Intel Threading Challenge #1</title>
		<link>http://www.parallelcoding.com/2009/07/10/intel-threading-challenge-1/</link>
		<comments>http://www.parallelcoding.com/2009/07/10/intel-threading-challenge-1/#comments</comments>
		<pubDate>Fri, 10 Jul 2009 15:16:57 +0000</pubDate>
		<dc:creator>Robert Green</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Parallel / Distributed]]></category>
		<category><![CDATA[.NET]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[concurrent]]></category>
		<category><![CDATA[Intel]]></category>
		<category><![CDATA[Parallel]]></category>
		<category><![CDATA[Sorting]]></category>

		<guid isPermaLink="false">http://www.parallelcoding.com/?p=466</guid>
		<description><![CDATA[While I never participated in the Intel Threading Challenge, I still find the problems really intriguing. Why? Because they are problems designed to test threaded development which is not only cool but is also going to play a large part in the future of computing. I would call all this problem for the most part [...]]]></description>
			<content:encoded><![CDATA[<p>While I never participated in the <a href="http://software.intel.com/en-us/contests/Threading-Challenge-2009/codecontest.php">Intel Threading Challenge</a>, I still find the problems really intriguing. Why? Because they are problems designed to test threaded development which is not only cool but is also going to play a large part in the future of computing. I would call all this problem for the most part concurrent, not parallel. Why? Read <a href="http://www.linux-mag.com/id/7411">this</a> and you&#8217;ll understand completely. Now, on to the challenge!</p>
<p>Problem # 1 states:</p>
<blockquote>
<p style="font-family: verdana, sans-serif; margin-top: 0px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; line-height: 16px; padding: 0px;"><em>Problem description: Given a set of unsorted items with keys that can be considered as a binary representation of an integer, the bits within the key can be used to sort the set of items. This method of sorting is known as Radix Sort.</em></p>
<p style="font-family: verdana, sans-serif; margin-top: 0px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; line-height: 16px; padding: 0px;"><em>Write a program that includes a threaded version of a Radix Sort algorithm that sorts the keys read from an input file, then output the sorted keys to another file. The input and output file names shall be the first and second arguments on the command line of the application execution.</em></p>
<p style="font-family: verdana, sans-serif; margin-top: 0px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; line-height: 16px; padding: 0px;"><em>The first line of the input text file is the total number of keys (N) to be sorted; this is followed by N keys, one per line, in the file.  A key will be a seven-character string made up of printable characters not including the space character (ASCII 0&#215;20). The number of keys within the file is less than 2^31 &#8211; 1.  Sorted output must be stored in a text file, one key per line.</em></p>
<p style="font-family: verdana, sans-serif; margin-top: 0px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; line-height: 16px; padding: 0px;"><em>Timing: If you put timing code into your application to time the sorting process and report the elapsed time, this time will be used for scoring.  If no timing code is added, the entire execution time (including time for input and output) will be used for scoring.</em></p>
<p style="font-family: verdana, sans-serif; margin-top: 0px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; line-height: 16px; padding: 0px;"><em><br style="font-family: verdana, sans-serif; padding: 0px; margin: 0px;" />Example Input file:<br style="font-family: verdana, sans-serif; padding: 0px; margin: 0px;" />8<br style="font-family: verdana, sans-serif; padding: 0px; margin: 0px;" />H@skell<br style="font-family: verdana, sans-serif; padding: 0px; margin: 0px;" />surVEYs<br style="font-family: verdana, sans-serif; padding: 0px; margin: 0px;" />sysTEMS<br style="font-family: verdana, sans-serif; padding: 0px; margin: 0px;" />HASKELL<br style="font-family: verdana, sans-serif; padding: 0px; margin: 0px;" />Surveys<br style="font-family: verdana, sans-serif; padding: 0px; margin: 0px;" />1234567<br style="font-family: verdana, sans-serif; padding: 0px; margin: 0px;" />SURveys<br style="font-family: verdana, sans-serif; padding: 0px; margin: 0px;" />systEMS</em></p>
<p style="font-family: verdana, sans-serif; margin-top: 0px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; line-height: 16px; padding: 0px;"><em>Example Output file:<br style="font-family: verdana, sans-serif; padding: 0px; margin: 0px;" />1234567<br style="font-family: verdana, sans-serif; padding: 0px; margin: 0px;" />H@skell<br style="font-family: verdana, sans-serif; padding: 0px; margin: 0px;" />HASKELL<br style="font-family: verdana, sans-serif; padding: 0px; margin: 0px;" />SURveys<br style="font-family: verdana, sans-serif; padding: 0px; margin: 0px;" />Surveys<br style="font-family: verdana, sans-serif; padding: 0px; margin: 0px;" />surVEYs<br style="font-family: verdana, sans-serif; padding: 0px; margin: 0px;" />sysTEMS<br style="font-family: verdana, sans-serif; padding: 0px; margin: 0px;" />systEMS</em></p>
</blockquote>
<p style="font-family: verdana, sans-serif; margin-top: 0px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; line-height: 16px; padding: 0px;">My solution (both serial and parallel):</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">using</span> <span style="color: #008080;">System</span><span style="color: #008000;">;</span>
<span style="color: #0600FF;">using</span> <span style="color: #008080;">System.Collections.Generic</span><span style="color: #008000;">;</span>
<span style="color: #0600FF;">using</span> <span style="color: #008080;">System.Linq</span><span style="color: #008000;">;</span>
<span style="color: #0600FF;">using</span> <span style="color: #008080;">System.Text</span><span style="color: #008000;">;</span>
<span style="color: #0600FF;">using</span> <span style="color: #008080;">System.Collections</span><span style="color: #008000;">;</span>
<span style="color: #0600FF;">using</span> <span style="color: #008080;">System.Diagnostics</span><span style="color: #008000;">;</span>
<span style="color: #0600FF;">using</span> <span style="color: #008080;">System.Threading</span><span style="color: #008000;">;</span>
<span style="color: #0600FF;">using</span> <span style="color: #008080;">System.IO</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #0600FF;">namespace</span> RadixSort <span style="color: #000000;">&#123;</span>
&nbsp;
    <span style="color: #FF0000;">class</span> Program <span style="color: #000000;">&#123;</span>
        <span style="color: #0600FF;">static</span> <span style="color: #0600FF;">void</span> Main<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">string</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span> args<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
            StreamReader sr<span style="color: #008000;">;</span>
            <span style="color: #FF0000;">int</span> length<span style="color: #008000;">;</span>
            TimeSpan serial, parallel<span style="color: #008000;">;</span>
            sr <span style="color: #008000;">=</span> File.<span style="color: #0000FF;">OpenText</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">@&quot;C:\Documents and Settings\rgreen\Desktop\Threading\Threading\rsTestK100.dat&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
            length <span style="color: #008000;">=</span> Convert.<span style="color: #0000FF;">ToInt32</span><span style="color: #000000;">&#40;</span>sr.<span style="color: #0000FF;">ReadLine</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">Trim</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            <span style="color: #FF0000;">string</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span> values <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> <span style="color: #FF0000;">string</span><span style="color: #000000;">&#91;</span>length<span style="color: #000000;">&#93;</span><span style="color: #008000;">;</span>
            <span style="color: #FF0000;">string</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span> newValues <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> <span style="color: #FF0000;">string</span><span style="color: #000000;">&#91;</span>length<span style="color: #000000;">&#93;</span><span style="color: #008000;">;</span>
            <span style="color: #0600FF;">for</span><span style="color: #000000;">&#40;</span><span style="color: #FF0000;">int</span> x <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span> x <span style="color: #008000;">&lt;</span> length<span style="color: #008000;">;</span> x<span style="color: #008000;">++</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
                values<span style="color: #000000;">&#91;</span>x<span style="color: #000000;">&#93;</span> <span style="color: #008000;">=</span> sr.<span style="color: #0000FF;">ReadLine</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">Trim</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            <span style="color: #000000;">&#125;</span>
&nbsp;
            Stopwatch sw <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> Stopwatch<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
            <span style="color: #008080; font-style: italic;">//</span>
            <span style="color: #008080; font-style: italic;">// Serial</span>
            <span style="color: #008080; font-style: italic;">//</span>
            sw.<span style="color: #0000FF;">Start</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            RadixSort<span style="color: #000000;">&#40;</span>values<span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">CopyTo</span><span style="color: #000000;">&#40;</span>newValues, <span style="color: #FF0000;">0</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            sw.<span style="color: #0000FF;">Stop</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            serial <span style="color: #008000;">=</span> sw.<span style="color: #0000FF;">Elapsed</span><span style="color: #008000;">;</span>
&nbsp;
            <span style="color: #008080; font-style: italic;">//</span>
            <span style="color: #008080; font-style: italic;">// Parallel</span>
            <span style="color: #008080; font-style: italic;">//</span>
            sw.<span style="color: #0000FF;">Reset</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            sw.<span style="color: #0000FF;">Start</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            ParallelRadixSort<span style="color: #000000;">&#40;</span>values<span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">CopyTo</span><span style="color: #000000;">&#40;</span>newValues, <span style="color: #FF0000;">0</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            sw.<span style="color: #0000FF;">Stop</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            parallel <span style="color: #008000;">=</span> sw.<span style="color: #0000FF;">Elapsed</span><span style="color: #008000;">;</span>
&nbsp;
&nbsp;
            Console.<span style="color: #0000FF;">WriteLine</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;Serial Time: &quot;</span> <span style="color: #008000;">+</span> serial<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            Console.<span style="color: #0000FF;">WriteLine</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;Parallel Time: &quot;</span> <span style="color: #008000;">+</span> parallel<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            Console.<span style="color: #0000FF;">ReadLine</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> <span style="color: #FF0000;">string</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span> ParallelRadixSort<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">string</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span> array<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
            <span style="color: #FF0000;">int</span> length <span style="color: #008000;">=</span> array.<span style="color: #0000FF;">Length</span><span style="color: #008000;">;</span>
&nbsp;
            Parallel.<span style="color: #0600FF;">For</span><span style="color: #000000;">&#40;</span><span style="color: #FF0000;">0</span>, array<span style="color: #000000;">&#91;</span><span style="color: #FF0000;">0</span><span style="color: #000000;">&#93;</span>.<span style="color: #0000FF;">Length</span> <span style="color: #008000;">-</span> <span style="color: #FF0000;">1</span>, <span style="color: #FF0000;">delegate</span><span style="color: #000000;">&#40;</span><span style="color: #FF0000;">int</span> curRadix<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
                <span style="color: #FF0000;">int</span> index <span style="color: #008000;">=</span> array<span style="color: #000000;">&#91;</span><span style="color: #FF0000;">0</span><span style="color: #000000;">&#93;</span>.<span style="color: #0000FF;">Length</span> <span style="color: #008000;">-</span> <span style="color: #FF0000;">1</span> <span style="color: #008000;">-</span> curRadix<span style="color: #008000;">;</span>
                array <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> MergeSort<span style="color: #000000;">&#40;</span>array, array<span style="color: #000000;">&#91;</span><span style="color: #FF0000;">0</span><span style="color: #000000;">&#93;</span>.<span style="color: #0000FF;">Length</span> <span style="color: #008000;">-</span> <span style="color: #FF0000;">1</span> <span style="color: #008000;">-</span> index<span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">Results</span><span style="color: #008000;">;</span>
            <span style="color: #000000;">&#125;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
            <span style="color: #0600FF;">return</span> array<span style="color: #008000;">;</span>
        <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> <span style="color: #FF0000;">string</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span> RadixSort<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">string</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span> array<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
&nbsp;
            <span style="color: #FF0000;">int</span> length <span style="color: #008000;">=</span> array.<span style="color: #0000FF;">Length</span><span style="color: #008000;">;</span>
&nbsp;
            <span style="color: #0600FF;">for</span><span style="color: #000000;">&#40;</span><span style="color: #FF0000;">int</span> curRadix <span style="color: #008000;">=</span> array<span style="color: #000000;">&#91;</span><span style="color: #FF0000;">0</span><span style="color: #000000;">&#93;</span>.<span style="color: #0000FF;">Length</span> <span style="color: #008000;">-</span> <span style="color: #FF0000;">1</span><span style="color: #008000;">;</span> curRadix <span style="color: #008000;">&gt;=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span> curRadix<span style="color: #008000;">--</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
                array <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> MergeSort<span style="color: #000000;">&#40;</span>array, array<span style="color: #000000;">&#91;</span><span style="color: #FF0000;">0</span><span style="color: #000000;">&#93;</span>.<span style="color: #0000FF;">Length</span> <span style="color: #008000;">-</span> <span style="color: #FF0000;">1</span> <span style="color: #008000;">-</span> curRadix<span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">Results</span><span style="color: #008000;">;</span>
            <span style="color: #000000;">&#125;</span>
&nbsp;
            <span style="color: #0600FF;">return</span> array<span style="color: #008000;">;</span>
        <span style="color: #000000;">&#125;</span>
    <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<p>And there you have it!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.parallelcoding.com/2009/07/10/intel-threading-challenge-1/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Project Euler #6</title>
		<link>http://www.parallelcoding.com/2009/07/01/project-euler-6/</link>
		<comments>http://www.parallelcoding.com/2009/07/01/project-euler-6/#comments</comments>
		<pubDate>Wed, 01 Jul 2009 13:52:40 +0000</pubDate>
		<dc:creator>Robert Green</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Parallel / Distributed]]></category>
		<category><![CDATA[.NET]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Project Euler]]></category>

		<guid isPermaLink="false">http://www.parallelcoding.com/?p=451</guid>
		<description><![CDATA[Project Euler problem #6 is Find the difference between the sum of the squares of the first one hundred natural numbers and the square of the sum. When I first began looking at this problem I wanted to experiment a bit with the Parallel Extensions for .NET, so I started where any parallel algorithm begins: [...]]]></description>
			<content:encoded><![CDATA[<p>Project Euler problem #6 is</p>
<blockquote><p>Find the difference between the sum of the squares of the first one hundred natural numbers and the square of the sum.</p></blockquote>
<p>When I first began looking at this problem I wanted to experiment a bit with the Parallel Extensions for .NET, so I started where any parallel algorithm begins: with a sequential algorithm. The algorithm here is rather trivial: Loop over the natural numbers from 1 to 100. In order to sum the squares I will sum the square of each number. In order to square the sum I will sum the numbers and then square them. The difference is the answer. So, first, I came up with two functions: SumOfSquares and SquareOfSums.</p>
<p><br/></p>
<p>My first attempt at these functions ended up something like this:</p>
<p><br/></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">static</span> <span style="color: #FF0000;">double</span> SumOfSquares<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">int</span> min, <span style="color: #FF0000;">int</span> max<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
    <span style="color: #FF0000;">double</span> result <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span>
    <span style="color: #0600FF;">for</span><span style="color: #000000;">&#40;</span><span style="color: #FF0000;">int</span> x <span style="color: #008000;">=</span> min<span style="color: #008000;">;</span> x <span style="color: #008000;">&lt;=</span> max<span style="color: #008000;">;</span> x<span style="color: #008000;">++</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
        result <span style="color: #008000;">+=</span> Math.<span style="color: #0000FF;">Pow</span><span style="color: #000000;">&#40;</span>x, <span style="color: #FF0000;">2</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
     <span style="color: #000000;">&#125;</span>
     <span style="color: #0600FF;">return</span> result<span style="color: #008000;">;</span>
<span style="color: #000000;">&#125;</span>
<span style="color: #0600FF;">static</span> <span style="color: #FF0000;">double</span> SquareOfSums<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">int</span> min, <span style="color: #FF0000;">int</span> max<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
    <span style="color: #FF0000;">double</span> result <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span>
    <span style="color: #0600FF;">for</span><span style="color: #000000;">&#40;</span><span style="color: #FF0000;">int</span> x <span style="color: #008000;">=</span> min<span style="color: #008000;">;</span> x <span style="color: #008000;">&lt;=</span> max<span style="color: #008000;">;</span> x<span style="color: #008000;">++</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
        result <span style="color: #008000;">+=</span> x<span style="color: #008000;">;</span>
    <span style="color: #000000;">&#125;</span>
    <span style="color: #0600FF;">return</span> Math.<span style="color: #0000FF;">Pow</span><span style="color: #000000;">&#40;</span>result, <span style="color: #FF0000;">2</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<p> Both of those functions are very straight forward. My first thought after writing these was, &#8220;Hey, why not use some LINQ?&#8221; So I did. Here is how the functions change:</p>
<p><br/></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">static</span> <span style="color: #FF0000;">double</span> LinqSumOfSquares<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">int</span> min, <span style="color: #FF0000;">int</span> max<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
    <span style="color: #0600FF;">return</span> Enumerable.<span style="color: #0000FF;">Range</span><span style="color: #000000;">&#40;</span>min, max<span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">Select</span><span style="color: #000000;">&#40;</span>d <span style="color: #008000;">=&gt;</span> Math.<span style="color: #0000FF;">Pow</span><span style="color: #000000;">&#40;</span>d, <span style="color: #FF0000;">2</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">Sum</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>           
<span style="color: #000000;">&#125;</span>
<span style="color: #0600FF;">static</span> <span style="color: #FF0000;">double</span> LinqSquareOfSums<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">int</span> min, <span style="color: #FF0000;">int</span> max<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
    <span style="color: #0600FF;">return</span> Math.<span style="color: #0000FF;">Pow</span><span style="color: #000000;">&#40;</span>Enumerable.<span style="color: #0000FF;">Range</span><span style="color: #000000;">&#40;</span>min, max<span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">Select</span><span style="color: #000000;">&#40;</span>d <span style="color: #008000;">=&gt;</span> d<span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">Sum</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>, <span style="color: #FF0000;">2</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>    
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<p>Wow! Talk about incredible, shrinking functions! I love finding a way to make code more succinct, readable, and elegant and these changes seem to have hit the nail on the head! Anyways, that is basically all the pieces for the sequential algorithm. All you have to do is call each of those functions and take the difference. Simple, huh? But the real question is how can we parallelize these bad boys? I have a few thoughts.</p>
<ol>
<li>Call each function in parallel. In other words let the sumOfSquares and SquareOfSum function run at the same time in different threads.</li>
<li>Parallelize each function invidvidually. In other words  leverage the loops inside of each function in order to parallelize them.</li>
</ol>
<p>So let&#8217;s take a look at each method using the Parallel Extensions for .NET. The first method is calling each function in parallel. My first thought here was to use Parallel.Invoke in order to call each function at the same time. A little further research quickly revealed that Parallel.Invoke cannot return any values. My initial response to that: &#8220;Well that sucks.&#8221; Luckily there&#8217;s another class in the Parallel library called Futures. What&#8217;s a future? From <a href="http://www.devx.com/dotnet/Article/39204/1763/page/5">DevX</a> &#8211;<br />
<blockquote>&#8220;In TPL terms, a Future is basically a task that returns a value. It&#8217;s like a deferred function. You start it running and then use its value later. If the Future hasn&#8217;t finished calculating its value by the time you need it, it makes you wait while it finishes.&#8221; </p></blockquote>
<p>Sounds good to me. So how do we use futures? Like this:
</p>
<p><br/></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">static</span> <span style="color: #FF0000;">long</span> Problem6Futures<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">int</span> min, <span style="color: #FF0000;">int</span> max<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
&nbsp;
    Future<span style="color: #008000;">&lt;</span><span style="color: #FF0000;">double</span><span style="color: #008000;">&gt;</span> fSumOfSquares <span style="color: #008000;">=</span> Future.<span style="color: #0000FF;">Create</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #008000;">=&gt;</span> SumOfSquares<span style="color: #000000;">&#40;</span>min, max<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
    Future<span style="color: #008000;">&lt;</span><span style="color: #FF0000;">double</span><span style="color: #008000;">&gt;</span> fSquareOfSums <span style="color: #008000;">=</span> Future.<span style="color: #0000FF;">Create</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #008000;">=&gt;</span> SquareOfSums<span style="color: #000000;">&#40;</span>min, max<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #FF0000;">double</span> result <span style="color: #008000;">=</span> fSquareOfSums.<span style="color: #0000FF;">Value</span> <span style="color: #008000;">-</span> fSumOfSquares.<span style="color: #0000FF;">Value</span><span style="color: #008000;">;</span>
&nbsp;
    <span style="color: #0600FF;">return</span> result<span style="color: #008000;">;</span>
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<p> Easy, huh? All you have to remember is that Futures are deferred functions that return values. The result of  Future operation gets stored in an object of type Future<Type> and the value is stored in Object.Value. So how about the second method of parallelizing this algorithm? Well, it&#8217;s even easier because of PLINQ &#8211; or Parallel LINQ. Let&#8217;s see what it looks like.</p>
<p><br/></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">static</span> <span style="color: #FF0000;">double</span> ParallelSumOfSquares<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">int</span> min, <span style="color: #FF0000;">int</span> max<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
    <span style="color: #0600FF;">return</span> Enumerable.<span style="color: #0000FF;">Range</span><span style="color: #000000;">&#40;</span>min, max<span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">AsParallel</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">Select</span><span style="color: #000000;">&#40;</span>d <span style="color: #008000;">=&gt;</span> Math.<span style="color: #0000FF;">Pow</span><span style="color: #000000;">&#40;</span>d, <span style="color: #FF0000;">2</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">Sum</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #000000;">&#125;</span>
<span style="color: #0600FF;">static</span> <span style="color: #FF0000;">double</span> ParallelSquareOfSums<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">int</span> min, <span style="color: #FF0000;">int</span> max<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
    <span style="color: #0600FF;">return</span> Math.<span style="color: #0000FF;">Pow</span><span style="color: #000000;">&#40;</span>Enumerable.<span style="color: #0000FF;">Range</span><span style="color: #000000;">&#40;</span>min, max<span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">AsParallel</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">Sum</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>, <span style="color: #FF0000;">2</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<p> Hah! Even easier! All that I did was add .AsParrallel() to our data. That tells LINQ to do the processing in parallel!</p>
<p>
So, there&#8217;s Project Euler Problem #6 for you. Was it a hard problem? Not really. Are you going to see major performance results through the parallelization of this algorithm? No. But you gotta&#8217; start somewhere when you&#8217;re learning how to parallelize algorithms using a new library. Today I used Futures and PLINQ and that sound&#8217;s like a pretty solid start to me.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.parallelcoding.com/2009/07/01/project-euler-6/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Project Euler #3</title>
		<link>http://www.parallelcoding.com/2009/06/30/project-euler-3/</link>
		<comments>http://www.parallelcoding.com/2009/06/30/project-euler-3/#comments</comments>
		<pubDate>Tue, 30 Jun 2009 12:09:06 +0000</pubDate>
		<dc:creator>Robert Green</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Parallel / Distributed]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Project Euler]]></category>

		<guid isPermaLink="false">http://www.parallelcoding.com/?p=418</guid>
		<description><![CDATA[Project Euler #3 is : Find the largest prime factor of a composite number. My first attempt at this whipped up some typical code that simply brute forced my way to the solution. My code looked like this: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 [...]]]></description>
			<content:encoded><![CDATA[<p>Project Euler #3 is :</p>
<blockquote><p>Find the largest prime factor of a composite number.</p></blockquote>
<p>My first attempt at this whipped up some typical code that simply brute forced my way to the solution. My code looked like this:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">static</span> <span style="color: #0600FF;">void</span> Problem3<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
    <span style="color: #FF0000;">long</span> n <span style="color: #008000;">=</span> <span style="color: #FF0000;">600851475143</span><span style="color: #008000;">;</span>
    <span style="color: #FF0000;">int</span> factor <span style="color: #008000;">=</span> <span style="color: #FF0000;">2</span><span style="color: #008000;">;</span>
    <span style="color: #FF0000;">int</span> lastFactor <span style="color: #008000;">=</span> <span style="color: #FF0000;">1</span><span style="color: #008000;">;</span>
&nbsp;
&nbsp;
    <span style="color: #0600FF;">if</span><span style="color: #000000;">&#40;</span>n <span style="color: #008000;">%</span> <span style="color: #FF0000;">2</span> <span style="color: #008000;">==</span> <span style="color: #FF0000;">0</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
        lastFactor <span style="color: #008000;">=</span> <span style="color: #FF0000;">2</span><span style="color: #008000;">;</span>
        n <span style="color: #008000;">=</span> n <span style="color: #008000;">/</span> <span style="color: #FF0000;">2</span><span style="color: #008000;">;</span>
        <span style="color: #0600FF;">while</span><span style="color: #000000;">&#40;</span>n <span style="color: #008000;">%</span> <span style="color: #FF0000;">2</span> <span style="color: #008000;">==</span> <span style="color: #FF0000;">0</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
            n <span style="color: #008000;">=</span> n <span style="color: #008000;">/</span> <span style="color: #FF0000;">2</span><span style="color: #008000;">;</span>
        <span style="color: #000000;">&#125;</span>
     <span style="color: #000000;">&#125;</span> <span style="color: #0600FF;">else</span> <span style="color: #000000;">&#123;</span>
         lastFactor <span style="color: #008000;">=</span> <span style="color: #FF0000;">1</span><span style="color: #008000;">;</span>
     <span style="color: #000000;">&#125;</span>
     factor <span style="color: #008000;">=</span> <span style="color: #FF0000;">3</span><span style="color: #008000;">;</span>
&nbsp;
     <span style="color: #FF0000;">double</span> maxFactor <span style="color: #008000;">=</span> Math.<span style="color: #0000FF;">Sqrt</span><span style="color: #000000;">&#40;</span>n<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
     <span style="color: #0600FF;">while</span><span style="color: #000000;">&#40;</span>n <span style="color: #008000;">&gt;</span> <span style="color: #FF0000;">1</span> <span style="color: #008000;">&amp;&amp;</span> factor <span style="color: #008000;">&lt;=</span> maxFactor<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
          <span style="color: #0600FF;">if</span><span style="color: #000000;">&#40;</span>n <span style="color: #008000;">%</span> factor <span style="color: #008000;">==</span> <span style="color: #FF0000;">0</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
              n <span style="color: #008000;">=</span> n <span style="color: #008000;">/</span> factor<span style="color: #008000;">;</span>
              lastFactor <span style="color: #008000;">=</span> factor<span style="color: #008000;">;</span>
              <span style="color: #0600FF;">while</span><span style="color: #000000;">&#40;</span>n <span style="color: #008000;">%</span> factor <span style="color: #008000;">==</span> <span style="color: #FF0000;">0</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
                  n <span style="color: #008000;">=</span> n <span style="color: #008000;">/</span> factor<span style="color: #008000;">;</span>
              <span style="color: #000000;">&#125;</span>
              maxFactor <span style="color: #008000;">=</span> Math.<span style="color: #0000FF;">Sqrt</span><span style="color: #000000;">&#40;</span>n<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
          <span style="color: #000000;">&#125;</span>
          factor <span style="color: #008000;">+=</span> <span style="color: #FF0000;">2</span><span style="color: #008000;">;</span>
      <span style="color: #000000;">&#125;</span>
&nbsp;
      <span style="color: #0600FF;">if</span><span style="color: #000000;">&#40;</span>n <span style="color: #008000;">==</span> <span style="color: #FF0000;">1</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
          Console.<span style="color: #0000FF;">Write</span><span style="color: #000000;">&#40;</span>lastFactor.<span style="color: #0000FF;">ToString</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #008000;">+</span> <span style="color: #666666;">&quot; &quot;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
      <span style="color: #000000;">&#125;</span> <span style="color: #0600FF;">else</span> <span style="color: #000000;">&#123;</span>
          Console.<span style="color: #0000FF;">Write</span><span style="color: #000000;">&#40;</span>n.<span style="color: #0000FF;">ToString</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #008000;">+</span> <span style="color: #666666;">&quot; &quot;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
      <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<p><br/><br />
Not very elegant, but it works. So I set out to find something a bit prettier and I came across I LINQ solution <a href="http://srtsolutions.com/blogs/billwagner/archive/2008/03/28/notes-on-euler-problem-3.aspx">here</a>. All I did was tack on the .AsParallel() in order to give it a little speed boost. The code looks like:<br/></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">static</span> <span style="color: #0600FF;">void</span> LinqProblem3<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
    <span style="color: #FF0000;">long</span> largeNumber <span style="color: #008000;">=</span> <span style="color: #FF0000;">600851475143</span><span style="color: #008000;">;</span>
    var allPrimeFactors <span style="color: #008000;">=</span> from p <span style="color: #0600FF;">in</span> Primes.<span style="color: #0000FF;">PrimeFactors</span><span style="color: #000000;">&#40;</span>largeNumber<span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">AsParallel</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
                                 orderby p descending
                                 select p<span style="color: #008000;">;</span>
&nbsp;
    <span style="color: #0600FF;">foreach</span><span style="color: #000000;">&#40;</span>var f <span style="color: #0600FF;">in</span> allPrimeFactors<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span>
        Console.<span style="color: #0000FF;">WriteLine</span><span style="color: #000000;">&#40;</span>f<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span>
&nbsp;
<span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> <span style="color: #FF0000;">class</span> Primes <span style="color: #000000;">&#123;</span>
&nbsp;
    <span style="color: #008080; font-style: italic;">// Find all prime factors.</span>
    <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> IEnumerable<span style="color: #008000;">&lt;</span><span style="color: #FF0000;">int</span><span style="color: #008000;">&gt;</span> PrimeFactors<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">long</span> number<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
        <span style="color: #008080; font-style: italic;">// Start by removing the lowest prime (2)</span>
        <span style="color: #0600FF;">return</span> MorePrimeFactors<span style="color: #000000;">&#40;</span>number, <span style="color: #FF0000;">2</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #000000;">&#125;</span>
&nbsp;
    <span style="color: #008080; font-style: italic;">// This recursive method finds all prime factors.</span>
    <span style="color: #0600FF;">private</span> <span style="color: #0600FF;">static</span> IEnumerable<span style="color: #008000;">&lt;</span><span style="color: #FF0000;">int</span><span style="color: #008000;">&gt;</span> MorePrimeFactors<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">long</span> number, <span style="color: #FF0000;">int</span> factor<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
        <span style="color: #008080; font-style: italic;">// Find the next prime factor</span>
	<span style="color: #0600FF;">while</span><span style="color: #000000;">&#40;</span>number <span style="color: #008000;">%</span> factor <span style="color: #008000;">!=</span> <span style="color: #FF0000;">0</span><span style="color: #000000;">&#41;</span>
	    factor<span style="color: #008000;">++;</span>
	<span style="color: #008080; font-style: italic;">// Return it.</span>
	yield <span style="color: #0600FF;">return</span> factor<span style="color: #008000;">;</span>
&nbsp;
	<span style="color: #008080; font-style: italic;">// look again...</span>
	<span style="color: #0600FF;">if</span><span style="color: #000000;">&#40;</span>number <span style="color: #008000;">&gt;</span> factor<span style="color: #000000;">&#41;</span>
		<span style="color: #008080; font-style: italic;">// recursively look for this factor again, using Num/factor</span>
		<span style="color: #008080; font-style: italic;">// as the new big number</span>
		<span style="color: #0600FF;">foreach</span><span style="color: #000000;">&#40;</span><span style="color: #FF0000;">int</span> factors <span style="color: #0600FF;">in</span> MorePrimeFactors<span style="color: #000000;">&#40;</span>number <span style="color: #008000;">/</span> factor, factor<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
			yield <span style="color: #0600FF;">return</span> factors<span style="color: #008000;">;</span>
	<span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://www.parallelcoding.com/2009/06/30/project-euler-3/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Left Joins in LINQ</title>
		<link>http://www.parallelcoding.com/2009/06/19/left-joins-in-linq/</link>
		<comments>http://www.parallelcoding.com/2009/06/19/left-joins-in-linq/#comments</comments>
		<pubDate>Fri, 19 Jun 2009 14:15:10 +0000</pubDate>
		<dc:creator>Robert Green</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[LINQ]]></category>

		<guid isPermaLink="false">http://www.parallelcoding.com/?p=438</guid>
		<description><![CDATA[For some reason it seems that I often need to perform a left join in LINQ. Every time I need to do this I find myself scouring the web one more time in order to remember how a left join works in LINQ. So how does it work? The best example that I&#8217;ve found is [...]]]></description>
			<content:encoded><![CDATA[<p>For some reason it seems that I often need to perform a left join in LINQ. Every time I need to do this I find myself scouring the web one more time in order to remember how a left join works in LINQ. So how does it work? The best example that I&#8217;ve found is <a href="http://geekswithblogs.net/AzamSharp/archive/2008/04/07/121103.aspx">here</a>. The example looks something like this:</p>
<p><br/></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;">var list <span style="color: #008000;">=</span> from r <span style="color: #0600FF;">in</span> dc.<span style="color: #0000FF;">tblRooms</span>
             join ui <span style="color: #0600FF;">in</span> dc.<span style="color: #0000FF;">tblUserInfos</span>
             on r.<span style="color: #0000FF;">UserName</span> equals ui.<span style="color: #0000FF;">UserName</span> into userrooms
             where r.<span style="color: #0000FF;">CourseID</span> <span style="color: #008000;">==</span> <span style="color: #FF0000;">1848</span>
             from ur <span style="color: #0600FF;">in</span> userrooms.<span style="color: #0000FF;">DefaultIfEmpty</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
             select <span style="color: #008000;">new</span><span style="color: #000000;">&#123;</span>
                 FirstName <span style="color: #008000;">=</span> <span style="color: #000000;">&#40;</span>ur.<span style="color: #0000FF;">FirstName</span> <span style="color: #008000;">==</span> <span style="color: #0600FF;">null</span><span style="color: #000000;">&#41;</span> <span style="color: #008000;">?</span> <span style="color: #666666;">&quot;N/A&quot;</span> <span style="color: #008000;">:</span> ur.<span style="color: #0000FF;">FirstName</span>,
                 LastName <span style="color: #008000;">=</span> <span style="color: #000000;">&#40;</span>ur.<span style="color: #0000FF;">LastName</span> <span style="color: #008000;">==</span> <span style="color: #0600FF;">null</span><span style="color: #000000;">&#41;</span> <span style="color: #008000;">?</span> <span style="color: #666666;">&quot;N/A&quot;</span> <span style="color: #008000;">:</span> ur.<span style="color: #0000FF;">LastName</span>,
                 RoomName <span style="color: #008000;">=</span> r.<span style="color: #0000FF;">Name</span>
              <span style="color: #000000;">&#125;</span><span style="color: #008000;">;</span></pre></td></tr></table></div>

<p>I like this example a lot because it is very straight forward. Personally I would like to make the statement a bit more generic. In order to do that all we need to remember is that every Left Join has a left table A and a right table B. All the results from the A will be returned regardless of the join with B. In my example I will call table A the LEFT_TABLE and table B the RIGHT_TABLE.</p>
<p><br/></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;">var list <span style="color: #008000;">=</span>  from LT <span style="color: #0600FF;">in</span> LEFT_TABLE
	      join RT <span style="color: #0600FF;">in</span> RIGHT_TABLE
	      on LT.<span style="color: #0000FF;">key</span> equals RT.<span style="color: #0000FF;">KEY</span> into NEW_TABLE
              where <span style="color: #008000;">&lt;</span>CONDITIONS<span style="color: #008000;">&gt;</span>
              from NT <span style="color: #0600FF;">in</span> NEW_TABLE.<span style="color: #0000FF;">DefaultIfEmpty</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
              <span style="color: #008000;">&lt;</span>SELECT_STATEMENT<span style="color: #008000;">&gt;;</span></pre></td></tr></table></div>

<p>
The only problem that may occur with this left join occurs with the DefaultIfEmpty() operator. A better practice would be to pass in a default value so that we can know what to expect in return.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.parallelcoding.com/2009/06/19/left-joins-in-linq/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Project Euler #2</title>
		<link>http://www.parallelcoding.com/2009/05/28/project-euler-2/</link>
		<comments>http://www.parallelcoding.com/2009/05/28/project-euler-2/#comments</comments>
		<pubDate>Thu, 28 May 2009 20:07:09 +0000</pubDate>
		<dc:creator>Robert Green</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Project Euler]]></category>

		<guid isPermaLink="false">http://www.parallelcoding.com/?p=414</guid>
		<description><![CDATA[Problem #2 in the Project Euler series is: Each new term in the Fibonacci sequence is generated by adding the previous two terms. By starting with 1 and 2, the first 10 terms will be: 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, &#8230; Find the sum of all the even-valued terms in [...]]]></description>
			<content:encoded><![CDATA[<p>Problem #2 in the Project Euler series is:</p>
<blockquote><p><em>Each new term in the Fibonacci sequence is generated by adding the previous two terms. By starting with 1 and 2, the first 10 terms will be:</em></p>
<p><em>1, 2, 3, 5, 8, 13, 21, 34, 55, 89, &#8230;</em></p>
<p><em>Find the sum of all the even-valued terms in the sequence which do not exceed four million.</em></p></blockquote>
<p>My solution:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">static</span> <span style="color: #0600FF;">void</span> Problem2<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
    <span style="color: #FF0000;">int</span> term1 <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span>
    <span style="color: #FF0000;">int</span> term2 <span style="color: #008000;">=</span> <span style="color: #FF0000;">1</span><span style="color: #008000;">;</span>
    <span style="color: #FF0000;">int</span> term3 <span style="color: #008000;">=</span> <span style="color: #FF0000;">1</span><span style="color: #008000;">;</span>
    <span style="color: #FF0000;">int</span> max <span style="color: #008000;">=</span> <span style="color: #FF0000;">4000000</span><span style="color: #008000;">;</span>
    <span style="color: #FF0000;">double</span> sum <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span>
&nbsp;
    <span style="color: #0600FF;">while</span><span style="color: #000000;">&#40;</span>term1 <span style="color: #008000;">&lt;</span> max <span style="color: #008000;">&amp;&amp;</span> term2 <span style="color: #008000;">&lt;</span> max <span style="color: #008000;">&amp;&amp;</span> term3 <span style="color: #008000;">&lt;</span> max<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
        term1 <span style="color: #008000;">=</span> term2 <span style="color: #008000;">+</span> term3<span style="color: #008000;">;</span>
        term2 <span style="color: #008000;">=</span> term1 <span style="color: #008000;">+</span> term3<span style="color: #008000;">;</span>
        term3 <span style="color: #008000;">=</span> term2 <span style="color: #008000;">+</span> term1<span style="color: #008000;">;</span>
&nbsp;
        <span style="color: #0600FF;">if</span><span style="color: #000000;">&#40;</span>term1 <span style="color: #008000;">%</span> <span style="color: #FF0000;">2</span> <span style="color: #008000;">==</span> <span style="color: #FF0000;">0</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
            sum <span style="color: #008000;">+=</span> term1<span style="color: #008000;">;</span>
        <span style="color: #000000;">&#125;</span>
        <span style="color: #0600FF;">if</span><span style="color: #000000;">&#40;</span>term2 <span style="color: #008000;">%</span> <span style="color: #FF0000;">2</span> <span style="color: #008000;">==</span> <span style="color: #FF0000;">0</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
            sum <span style="color: #008000;">+=</span> term2<span style="color: #008000;">;</span>
         <span style="color: #000000;">&#125;</span>
         <span style="color: #0600FF;">if</span><span style="color: #000000;">&#40;</span>term3 <span style="color: #008000;">%</span> <span style="color: #FF0000;">2</span> <span style="color: #008000;">==</span> <span style="color: #FF0000;">0</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
            sum <span style="color: #008000;">+=</span> term3<span style="color: #008000;">;</span>
         <span style="color: #000000;">&#125;</span>
&nbsp;
    <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://www.parallelcoding.com/2009/05/28/project-euler-2/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Capitalize A String in C#</title>
		<link>http://www.parallelcoding.com/2009/05/27/capitalize-a-string-in-c/</link>
		<comments>http://www.parallelcoding.com/2009/05/27/capitalize-a-string-in-c/#comments</comments>
		<pubDate>Wed, 27 May 2009 17:34:34 +0000</pubDate>
		<dc:creator>Robert Green</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[C#]]></category>

		<guid isPermaLink="false">http://www.parallelcoding.com/?p=395</guid>
		<description><![CDATA[Capitalizing a string is a rather trivial task in C#. There are 2 ways to approach single word capitalization where each method includes 3 steps. Method 1 uses only strings and string methods while method 2 treats the letter to be capitalized as a char. Method 1: Get the first character as a string ( [...]]]></description>
			<content:encoded><![CDATA[<p>Capitalizing a string is a rather trivial task in C#. There are 2 ways to approach single word capitalization where each method includes 3 steps. Method 1 uses only strings and string methods while method 2 treats the letter to be capitalized as a char.</p>
<p><strong>Method 1:</strong></p>
<ol>
<li>Get the first character as a string ( stringToCapitalize.Substring(0,1) )</li>
<li>Transform the first character to uppercase ( stringToCapitalize.Substring(0,1).ToUpper() )</li>
<li>Append the rest of the string ( stringToCapitalize.Substring(0,1).ToUpper() + stringToCapitalize.Substring(1) )</li>
</ol>
<p><br/></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> <span style="color: #FF0000;">string</span> Capitalize<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">string</span> toCapitalize<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
    <span style="color: #0600FF;">try</span> <span style="color: #000000;">&#123;</span>
        <span style="color: #0600FF;">if</span><span style="color: #000000;">&#40;</span>toCapitalize.<span style="color: #0000FF;">Length</span> <span style="color: #008000;">&gt;</span> <span style="color: #FF0000;">1</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
            toCapitalize <span style="color: #008000;">=</span> toCapitalize.<span style="color: #0000FF;">Substring</span><span style="color: #000000;">&#40;</span><span style="color: #FF0000;">0</span>, <span style="color: #FF0000;">1</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">ToUpper</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #008000;">+</span> toCapitalize.<span style="color: #0000FF;">Substring</span><span style="color: #000000;">&#40;</span><span style="color: #FF0000;">1</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #000000;">&#125;</span>
    <span style="color: #000000;">&#125;</span> <span style="color: #0600FF;">catch</span><span style="color: #000000;">&#40;</span>Exception ex<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
        ExceptionHandling.<span style="color: #0000FF;">ExceptionLogging</span><span style="color: #000000;">&#40;</span>ex, <span style="color: #666666;">&quot;Error:Capitalize.&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #000000;">&#125;</span>
    <span style="color: #0600FF;">return</span> toCapitalize<span style="color: #008000;">;</span>
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<p><br/><strong>Method 2:</strong></p>
<ol>
<li>Get the first character as a char ( stringToCapitalize[0] )</li>
<li>Transform the first character to uppercase using the char.toUpper method ( char.toUpper(stringToCapitalize[0]) )</li>
<li>Append the rest of the string ( char.toUpper(stringToCapitalize.Substring[0]) + stringToCapitalize.Substring(1) )</li>
</ol>
<p><br/></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> <span style="color: #FF0000;">string</span> Capitalize<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">string</span> toCapitalize<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
    <span style="color: #0600FF;">try</span> <span style="color: #000000;">&#123;</span>
        <span style="color: #0600FF;">if</span><span style="color: #000000;">&#40;</span>toCapitalize.<span style="color: #0000FF;">Length</span> <span style="color: #008000;">&gt;</span> <span style="color: #FF0000;">1</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
            toCapitalize <span style="color: #008000;">=</span> <span style="color: #FF0000;">char</span>.<span style="color: #0000FF;">ToUpper</span><span style="color: #000000;">&#40;</span>toCapitalize<span style="color: #000000;">&#91;</span><span style="color: #FF0000;">0</span><span style="color: #000000;">&#93;</span><span style="color: #000000;">&#41;</span> <span style="color: #008000;">+</span> toCapitalize.<span style="color: #0000FF;">Substring</span><span style="color: #000000;">&#40;</span><span style="color: #FF0000;">1</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #000000;">&#125;</span>
    <span style="color: #000000;">&#125;</span> <span style="color: #0600FF;">catch</span><span style="color: #000000;">&#40;</span>Exception ex<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
        ExceptionHandling.<span style="color: #0000FF;">ExceptionLogging</span><span style="color: #000000;">&#40;</span>ex, <span style="color: #666666;">&quot;Error:Capitalize.&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #000000;">&#125;</span>
    <span style="color: #0600FF;">return</span> toCapitalize<span style="color: #008000;">;</span>
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<p>Using either of these methods you could create an extension method as well. Something like this:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> <span style="color: #FF0000;">class</span> MyExtensions
<span style="color: #000000;">&#123;</span>
    <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> <span style="color: #FF0000;">string</span> Capitalize<span style="color: #000000;">&#40;</span><span style="color: #0600FF;">this</span> <span style="color: #FF0000;">String</span> toCapitalize<span style="color: #000000;">&#41;</span>
    <span style="color: #000000;">&#123;</span>
        <span style="color: #0600FF;">if</span><span style="color: #000000;">&#40;</span>toCapitalize.<span style="color: #0000FF;">Length</span> <span style="color: #008000;">&gt;</span> <span style="color: #FF0000;">1</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
            toCapitalize <span style="color: #008000;">=</span> toCapitalize.<span style="color: #0000FF;">Substring</span><span style="color: #000000;">&#40;</span><span style="color: #FF0000;">0</span>, <span style="color: #FF0000;">1</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">ToUpper</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #008000;">+</span> toCapitalize.<span style="color: #0000FF;">Substring</span><span style="color: #000000;">&#40;</span><span style="color: #FF0000;">1</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #000000;">&#125;</span>
        <span style="color: #0600FF;">return</span> toCapitalize<span style="color: #008000;">;</span>
    <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://www.parallelcoding.com/2009/05/27/capitalize-a-string-in-c/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Content-Disposition attachment vs inline</title>
		<link>http://www.parallelcoding.com/2008/05/30/content-disposition-attachment-vs-inline/</link>
		<comments>http://www.parallelcoding.com/2008/05/30/content-disposition-attachment-vs-inline/#comments</comments>
		<pubDate>Fri, 30 May 2008 16:46:03 +0000</pubDate>
		<dc:creator>manatarms</dc:creator>
				<category><![CDATA[Web Development]]></category>
		<category><![CDATA[.NET]]></category>
		<category><![CDATA[C#]]></category>

		<guid isPermaLink="false">http://dotanything.wordpress.com/?p=17</guid>
		<description><![CDATA[Today I ran into an interesting issue. We have some legacy code in .NET 1.1 that exports an HTML table to Microsoft Excel. This export occurs by simply rendering the table via Response.Write and setting the header content-disposition to &#8220;attachment; filename=FileName.xls&#8221;. The original code looked something like this: 1 2 3 4 5 6 7 [...]]]></description>
			<content:encoded><![CDATA[<p>Today I ran into an interesting issue. We have some legacy code in .NET 1.1 that exports an HTML table to Microsoft Excel. This export occurs by simply rendering the table via Response.Write and setting the header content-disposition to &#8220;attachment; filename=FileName.xls&#8221;. The original code looked something like this:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;">Response.<span style="color: #0000FF;">Clear</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
Response.<span style="color: #0000FF;">AddHeader</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;content-disposition&quot;</span>, <span style="color: #666666;">&quot;attachment;filename=SalesByProductReport.xls&quot;</span><span style="color: #000000;">&#41;</span>
Response.<span style="color: #0000FF;">Charset</span> <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;utf-8&quot;</span>
Response.<span style="color: #0000FF;">Cache</span>.<span style="color: #0000FF;">SetCacheability</span><span style="color: #000000;">&#40;</span>HttpCacheability.<span style="color: #0000FF;">NoCache</span><span style="color: #000000;">&#41;</span>
Response.<span style="color: #0000FF;">ContentType</span> <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;application/vnd.ms-excel&quot;</span>
&nbsp;
Dim stringWrite <span style="color: #0600FF;">As</span> IO.<span style="color: #0000FF;">StringWriter</span> <span style="color: #008000;">=</span> <span style="color: #008000;">New</span> <span style="color: #000000;">System.<span style="color: #0000FF;">IO</span></span>.<span style="color: #0000FF;">StringWriter</span>
Dim htmlWrite <span style="color: #0600FF;">As</span> HtmlTextWriter <span style="color: #008000;">=</span> <span style="color: #008000;">New</span> HtmlTextWriter<span style="color: #000000;">&#40;</span>stringWrite<span style="color: #000000;">&#41;</span>
&nbsp;
tblTable.<span style="color: #0000FF;">RenderControl</span><span style="color: #000000;">&#40;</span>htmlWrite<span style="color: #000000;">&#41;</span>
&nbsp;
Response.<span style="color: #0000FF;">Write</span><span style="color: #000000;">&#40;</span>stringWrite.<span style="color: #0000FF;">ToString</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
Response.<span style="color: #0000FF;">Flush</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
Response.<span style="color: #0000FF;">End</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span></pre></td></tr></table></div>

<p>The problem that occurred was that any user using Internet Explorer (surprise, surprise!) would get a prompt to download the file but the file would not download! The file worked properly in all other browsers. The solution is to change</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;">Response.<span style="color: #0000FF;">AddHeader</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;content-disposition&quot;</span>, <span style="color: #666666;">&quot;attachment;filename=SalesByProductReport.xls&quot;</span><span style="color: #000000;">&#41;</span></pre></td></tr></table></div>

<p>to</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;">Response.<span style="color: #0000FF;">AddHeader</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;content-disposition&quot;</span>, <span style="color: #666666;">&quot;inline;filename=SalesByProductReport.xls&quot;</span><span style="color: #000000;">&#41;</span></pre></td></tr></table></div>

<p>Now, why exactly does this work? I&#8217;m not sure, so if you know please tell me.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.parallelcoding.com/2008/05/30/content-disposition-attachment-vs-inline/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>
