<?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; Development</title>
	<atom:link href="http://www.parallelcoding.com/category/development/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>Omnet++ 4.1, MiXiM, and IEEE 802.15.14</title>
		<link>http://www.parallelcoding.com/2010/07/12/omnet-4-1-mixim-and-ieee-802-15-14/</link>
		<comments>http://www.parallelcoding.com/2010/07/12/omnet-4-1-mixim-and-ieee-802-15-14/#comments</comments>
		<pubDate>Mon, 12 Jul 2010 11:37:12 +0000</pubDate>
		<dc:creator>Robert Green</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Research]]></category>
		<category><![CDATA[MiXiM]]></category>
		<category><![CDATA[Omnet++]]></category>
		<category><![CDATA[Zigbee]]></category>

		<guid isPermaLink="false">http://www.parallelcoding.com/?p=600</guid>
		<description><![CDATA[If anyone is having trouble compiling (no makefiles) or finding (not in the downloaded source) the IEEE 802.15.14 examples that go along with MiXiM like I did, I suggest the following steps: Check out the source using the command svn co https://mixim.svn.sourceforge.net/svnroot/mixim mixim Move the contents of the trunk directory into the omnetpp-4.1/samples/MiXiM/ directory Change directories to [...]]]></description>
			<content:encoded><![CDATA[<p>If anyone is having trouble compiling (no makefiles) or finding (not in the downloaded source) the IEEE 802.15.14 examples that go along with <a title="MiXiM" href="http://mixim.sourceforge.net/" target="_blank">MiXiM</a> like I did, I suggest the following steps:</p>
<ol>
<li>Check out the source using the command

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"> <span style="color: #c20cb9; font-weight: bold;">svn</span> <span style="color: #c20cb9; font-weight: bold;">co</span> https:<span style="color: #000000; font-weight: bold;">//</span>mixim.svn.sourceforge.net<span style="color: #000000; font-weight: bold;">/</span>svnroot<span style="color: #000000; font-weight: bold;">/</span>mixim mixim</pre></div></div>

</li>
<li>Move the contents of the trunk directory into the omnetpp-4.1/samples/MiXiM/ directory</li>
<li>Change directories to omnetpp-4.1/samples/MiXiM/examples/ieee802154Narrow</li>
<li>Build the project using the command

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">opp_makemake <span style="color: #660033;">-f</span> <span style="color: #660033;">-O</span> out -L..<span style="color: #000000; font-weight: bold;">/</span>..<span style="color: #000000; font-weight: bold;">/</span>out<span style="color: #000000; font-weight: bold;">/</span>gcc-debug<span style="color: #000000; font-weight: bold;">/</span>base -L..<span style="color: #000000; font-weight: bold;">/</span>..<span style="color: #000000; font-weight: bold;">/</span>out<span style="color: #000000; font-weight: bold;">/</span>gcc-
debug<span style="color: #000000; font-weight: bold;">/</span>modules -L..<span style="color: #000000; font-weight: bold;">/</span>..<span style="color: #000000; font-weight: bold;">/</span>out<span style="color: #000000; font-weight: bold;">/</span>gcc-debug<span style="color: #000000; font-weight: bold;">/</span>tests<span style="color: #000000; font-weight: bold;">/</span>testUtils <span style="color: #660033;">-lmiximbase</span> -
lmiximmodules -I..<span style="color: #000000; font-weight: bold;">/</span>..<span style="color: #000000; font-weight: bold;">/</span>modules<span style="color: #000000; font-weight: bold;">/</span>utility -I..<span style="color: #000000; font-weight: bold;">/</span>..<span style="color: #000000; font-weight: bold;">/</span>base<span style="color: #000000; font-weight: bold;">/</span>messages -I..<span style="color: #000000; font-weight: bold;">/</span>..<span style="color: #000000; font-weight: bold;">/</span>
base<span style="color: #000000; font-weight: bold;">/</span>utils -I..<span style="color: #000000; font-weight: bold;">/</span>..<span style="color: #000000; font-weight: bold;">/</span>base<span style="color: #000000; font-weight: bold;">/</span>modules -I..<span style="color: #000000; font-weight: bold;">/</span>..<span style="color: #000000; font-weight: bold;">/</span>base<span style="color: #000000; font-weight: bold;">/</span>phyLayer <span style="color: #660033;">-o</span>
ieee802154Narrow</pre></div></div>

</li>
<li>Change directories to omnetpp-4.1/samples/MiXiM/examples/ieee802154A</li>
<li>Build the project using the command

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">opp_makemake <span style="color: #660033;">-f</span> <span style="color: #660033;">-O</span> out -L..<span style="color: #000000; font-weight: bold;">/</span>..<span style="color: #000000; font-weight: bold;">/</span>out<span style="color: #000000; font-weight: bold;">/</span>gcc-debug<span style="color: #000000; font-weight: bold;">/</span>base -L..<span style="color: #000000; font-weight: bold;">/</span>..<span style="color: #000000; font-weight: bold;">/</span>out<span style="color: #000000; font-weight: bold;">/</span>gcc-
debug<span style="color: #000000; font-weight: bold;">/</span>modules -L..<span style="color: #000000; font-weight: bold;">/</span>..<span style="color: #000000; font-weight: bold;">/</span>out<span style="color: #000000; font-weight: bold;">/</span>gcc-debug<span style="color: #000000; font-weight: bold;">/</span>tests<span style="color: #000000; font-weight: bold;">/</span>testUtils <span style="color: #660033;">-lmiximbase</span> -
lmiximmodules <span style="color: #660033;">-o</span> ieee8021514a</pre></div></div>

</li>
</ol>
<p>Now everything should work!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.parallelcoding.com/2010/07/12/omnet-4-1-mixim-and-ieee-802-15-14/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Monte Carlo Simulation, State Space Pruning, and Meta-Heuristics</title>
		<link>http://www.parallelcoding.com/2010/07/05/monte-carlo-simulation-state-space-pruning-and-meta-heuristics/</link>
		<comments>http://www.parallelcoding.com/2010/07/05/monte-carlo-simulation-state-space-pruning-and-meta-heuristics/#comments</comments>
		<pubDate>Mon, 05 Jul 2010 14:56:50 +0000</pubDate>
		<dc:creator>Robert Green</dc:creator>
				<category><![CDATA[Ant Colony Optimization]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[Genetic Algorithms]]></category>
		<category><![CDATA[Heuristics]]></category>
		<category><![CDATA[Monte Carlo Simulation]]></category>
		<category><![CDATA[Particle Swarm Optimization]]></category>
		<category><![CDATA[Research]]></category>
		<category><![CDATA[State Space Pruning]]></category>

		<guid isPermaLink="false">http://www.parallelcoding.com/?p=567</guid>
		<description><![CDATA[I haven&#8217;t posted here in quite a while, mainly because I&#8217;ve been so busy and also because I didn&#8217;t have the time to complete a proper post. As this blog is mainly about my professional career and academic interests, let me start by sharing some of my most recent work. I have spent quite a bit of time pursuing [...]]]></description>
			<content:encoded><![CDATA[<p>I haven&#8217;t posted here in quite a while, mainly because I&#8217;ve been so busy and also because I didn&#8217;t have the time to complete a proper post. As this blog is mainly about my professional career and academic interests, let me start by sharing some of my most recent work.</p>
<p>I have spent quite a bit of time pursuing an area of research concerned with State Space Pruning for Monte Carlo  Simulation (MCS) when calculating Reliability Indices for Power Systems. This is a necessity due to the curse of dimensionality. Simply stated, in a power system with 32 generators one finds themselves with a need to examine <img src="http://www.parallelcoding.com/wp-content/plugins/easy-latex/cache/tex_a92ab3d51879fea0fe3cf524189d6487.png" title="2^{32}" style="vertical-align:-20%;" class="tex" alt="2^{32}" /> states. Expand this to a larger system and suddenly the state space size explodes. In this work we&#8217;ve used Genetic Algorithms (GA), Repulsive Binary Particle Swarm Optimization (RBPSO), and Ant Colony Optimization (ACO) in order to reduce MCS time and iterations before convergence. In all cases we have used a binary representation (each bit represents a generator&#8217;s on/off state) with a DC Optimal Power Flow (DCOPF) that has been tailored to minimize load curtailment instead of cost. The algorithms are customized in order to server our particular needs (specifically generating states in which there is no load curtailment quickly). We have had a rather good success rate over two different test systems: IEEE-RTS79 and IEEE-RTS96. For those not familiar with them, these two test systems are designed specifically for reliability testing (RTS = Reliability Testing System). RTS-79 came first and RTS-96 (for the most part) is simply 3 RTS-79 that are connected to each other with minor changes. The table below shows a brief comparison between the two systems.<br/><br />
<center></p>
<table class="tableizer-table">
<tbody>
<tr class="tableizer-firstrow">
<th></th>
<th>Generators</th>
<th>Buses</th>
<th>Lines</th>
<th>Capacity</th>
<th>Load</th>
</tr>
<tr>
<td>RTS-79</td>
<td>32</td>
<td>24</td>
<td>38</td>
<td>3405</td>
<td>2850</td>
</tr>
<tr>
<td>RTS-96</td>
<td>99</td>
<td>73</td>
<td>120</td>
<td>10215</td>
<td>8550</td>
</tr>
</tbody>
</table>
<p><label> Table I: RTS-79 and RTS-96</label><br />
</center><br />
The details of the remainder of the work can be found by contacting me or checking out the papers that we have produced. This work has produced four papers that have been either accepted or submitted so far that include:</p>
<ol>
<li>R. Green, L. Wang, Z. Wang, M. Alam, and C. Singh, “Power System Reliability Assessment Using Intelligent State Space Pruning Techniques: A Comparative Study” Submitted to 2010 Conference on Power System Technology, Hangzou China, October 2010.</li>
<li>R. Green, L. Wang, M. Alam, and C. Singh, “State space pruning for Reliability Evaluation using Binary Particle Swarm Optimization,” Submitted to Hawaii International Conference on System Sciences,University of Hawaii at Manoa, January 2011.</li>
<li>R. Green, L. Wang, and C. Singh, “State space pruning for power system reliability evaluation using genetic algorithms,” IEEE Power &amp; Energy Society General Meeting 2010, Minneapolis, MN, July 2010.</li>
<li>R. Green, Z. Wang, L. Wang, M. Alam, and C. Singh, “Evaluation of loss of load probability for power systems using intelligent search based state space pruning,” The 11th International Conference on Probabilistic Methods Applied to Power Systems, Singapore, June 2010</li>
</ol>
<p>Some further resources for this work that may be helpful to others include:</p>
<ol>
<li><a title="Matpower" href="http://www.pserc.cornell.edu/matpower/" target="_blank">Matpower</a> Formulation of IEEE-RTS79 [<a href="http://www.parallelcoding.com/wp-content/uploads/Research/MCSPruning/case79.m">IEEE-RTS79 MatPower</a>]</li>
<li><a title="Matpower" href="http://www.pserc.cornell.edu/matpower/" target="_blank">Matpower</a> Formulation of IEEE-RTS96 [<a href="http://www.parallelcoding.com/wp-content/uploads/Research/MCSPruning/case96.m">IEEE-RTS96 MatPower</a>]</li>
<li>DCOPF for RTS79 in <a title="LP_Solve" href="http://sourceforge.net/projects/lpsolve/" target="_blank">LP_Solve</a> format [<a href="http://www.parallelcoding.com/wp-content/uploads/Research/MCSPruning/RTS79.lp">RTS-79 DC Optimal Power Flow</a>]</li>
<li>DCOPF for RTS96 in <a title="LP_Solve" href="http://sourceforge.net/projects/lpsolve/" target="_blank">LP_Solve</a> format [<a href="http://www.parallelcoding.com/wp-content/uploads/Research/MCSPruning/RTS96.lp">RTS-96 DC Optimal Power Flow</a>]</li>
</ol>
<p>As well as some references:</p>
<ol>
<li>Joydeep Mitra and Chanan Singh. Incorporating the dc load flow model in the decomposition-simulation method of multi-area reliability evaluation. <em>IEEE Transactions on Power Systems</em>, 11(3):1245-1254, Aug 1996.</li>
<li>Chanan Singh and Joydeep Mitra. Composite system reliability evaluation using state space pruning. <em>IEEE Transactions on Power Systems</em>, 12(1):471-479, 1997.</li>
<li>Joydeep Mitra and Chanan Singh. Pruning and simluation for determination of frequency duration indices of composite power systems. <em>IEEE Transactions on Power Systems</em>, 14(3):899-905, 1999.</li>
<li>M. Wall. GAlib: A C++ library of genetic algorithm components. <em>Mechanical Engineering Department, Massachusetts Institute of Technology</em>, 1996.</li>
<li>Lingfeng Wang and Chanan Singh. Stochastic combined heat and power dispatch based on multi-objective particle swarm optimization. <em>International Journal of Electrical Power &amp; Energy Systems</em>, 30(3), 2007.</li>
<li>Lingfeng Wang and Chanan Singh. Population-based intelligent search in reliability evaluation of generation systems with wind power penetration. <em>IEEE Transactions on Power Systems</em>, 23(3):1336-1345, May 2008.</li>
<li>Lingfeng Wang and Chanan Singh. Reliability-constrained optimum placement of reclosers and distributed generators in distribution networks using an ant colony system<br />
algorithm. <em>IEEE Transactions on Systems, Man, and Cybernetics, Part C: Applications and Reviews</em>, 38(6), 2008.</li>
<li>Chanan Singh and Lingfeng Wang. Role of artificial intelligence in the reliability evaluation of electric power systems. <em>Turkish Journal of Electrical Engineering &amp; Computer Science</em>,<br />
16(3):189-200, 2008.</li>
<li>David E. Goldberg. <em>Genetic Algorithms in Search, Optimization, and Machine Learning</em>. Addison-Wesley Professional, 1 edition, Jan 1989.</li>
<li>Genetic algorithm, 2009. [ <a href="\url{http://en.wikipedia.org/wiki/Genetic\_algorithm}">Wikipedia: Genetic Algorithms</a> ]</li>
<li>Ray D. Zimmerman, E. M.-S. Carlos, and Deqiang Gan. MATPOWER: A MATLAB Power System Simulation Package, Version 3.1b2, User&#8217;s Manual. Technical report, Power Systems Engineering Research Center, 2006. [ <a href="http://www.pserc.cornell.edu/matpower/">Matpower</a> ]</li>
<li>Michel Berkelaar, Kjell Eikland, and Peter Notebaert. lp_solve : Open source (mixed-integer) linear programming system. 2004.</li>
<li>IEEE Committee Report. IEEE reliability test system. <em>IEEE Transactions on Power Apparatus and Systems</em>, PAS-98(6):2047-2054, 1979.</li>
<li>C. Grigg, P. Wong, P. Albrecht, and et al. The IEEE reliability test system-1996. <em>IEEE Transactions on Power Systems</em>, 14(3):1010-1020, 1999.</li>
<li>Final report on research project 2473-10. Technical report, EPRI, 1987.</li>
<li>M.V.F Pereira and N.J. Balu. Composite generation/transmission reliability evaluation. <em>Proceedings of the IEEE</em>, 80(4):470-491, apr 1992.</li>
<li>Roy Billinton and Wenyuan Li. <em>Reliability Assessment of Electric Power Systems using Monte Carlo Methods</em>. Plenum, New York; London, 1994.</li>
<li>J. Kennedy and R. Eberhart. Particle swarm optimization. In <em>Neural Networks, 1995. Proceedings., IEEE International Conference on</em>, volume 4, August 2002.</li>
<li>Del, G. K. Venayagamoorthy, S. Mohagheghi, J. C. Hernandez, and R. G. Harley. Particle swarm optimization: Basic concepts, variants and applications in power systems. <em>Evolutionary Computation, IEEE Transactions on</em>, 12(2):171-195, 2008.</li>
<li>D. K. Agrafiotis and W. Cedeño. Feature selection for structure-activity correlation using binary particle swarms. <em>Journal of Medicinal Chemistry</em>, 45:1098-1107, 2002.</li>
<li>A. Moraglio, C. Di Chio, and R. &amp; Poli. Geometric particle swarm optimization. In <em>Proceedings of the European conference on genetic programming<br />
(EuroGP)</em>, volume 4445, pages 125-136, 2007.</li>
<li>R. Green, L. Wang, and C. Singh. State space pruning for power system reliability evaluation using genetic algorithms. In <em>Proceedings of the IEEE PES General Meeting</em>, Minneapolis,  MN, July 2010.</li>
<li>R. Green, Z. Wang, L. Wang, M. Alam, and C. Singh. Evaluation of loss of load probability for power systems using intelligent search based state space pruning. In <em>Proceedings of the 11th International Conference on Probabilistic Methods Applied to Power Systems</em>, Singapore, June 2010.</li>
<li>R. Green, L. Wang, M. Alam, and C. Singh. State space pruning for reliability evaluation using binary particle swarm optimization. Jan 2011.</li>
<li>J. Kennedy and R.C. Eberhart. A discrete binary version of the particle swarm algorithm.In <em>IEEE International Conference on Systems, Man, and Cybernetics</em>, volume 5, pages 4104-4108, 1997.</li>
<li>Riccardo Poli, James Kennedy, and Time Blackwell. Particle swarm optimization. <em>Swarm Intelligence</em>, 1(1):33-57, June 2007.</li>
<li>C. K. Mohan and B. Al-Kazemi. Discrete particle swarm optimization. Indianapolis, IN, 2001. Purdue School of Engineering and Technology.</li>
<li>R. Eberhart and James Kennedy. A new optimizer using particle swarm theory. In <em>Proceedings of the Sixth International Symposium on Micro Machine and Human Science</em>, pages 39-43, 1995.</li>
<li>M. Dorigo. <em>Optimization, Learning and Natural Algorithms</em>. PhD thesis, Politecnico di Milano, Italy, 1992.</li>
<li>Thomas Stützle and Holger H. Hoos. Max-min ant system. <em>Future Gener. Comput. Syst.</em>, 16(9):889-914, 2000.</li>
<li>Min Kong and Peng Tian. A binary ant colony optimization for the unconstrained function optimization problem. In <em>CIS (1)</em>, pages 682-687, 2005.</li>
<li>Onay Urfalioglu. Robust estimation of camera rotation, translation and focal length at high outlier rates. <em>Computer and Robot Vision, Canadian Conference</em>, 0:464-471,  2004.</li>
<li>T. Krink, J. S. Vesterstrom, and J. Riget. Particle swarm optimisation with spatial particle extension. In <em>CEC &#8217;02: Proceedings of the Evolutionary Computation on 2002.  CEC &#8217;02. Proceedings of the 2002 Congress</em>, pages 1474-1479, Washington, DC,  USA, 2002. IEEE Computer Society.</li>
<li>T. Krink, J. S. Vesterstrom, and J. Riget. Particle swarm optimisation with spatial particle extension. In <em>CEC &#8217;02: Proceedings of the Evolutionary Computation on 2002.  CEC &#8217;02. Proceedings of the 2002 Congress</em>, pages 1474-1479, Washington, DC,  USA, 2002. IEEE Computer Society.</li>
<li>J. Riget and J.S. Vesterstrøm. A diversity-guided particle swarm optimizer &#8211; the arpso. Technical report, 2002.</li>
</ol>
]]></content:encoded>
			<wfw:commentRss>http://www.parallelcoding.com/2010/07/05/monte-carlo-simulation-state-space-pruning-and-meta-heuristics/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>March Madness Multicore Style</title>
		<link>http://www.parallelcoding.com/2010/03/04/march-madness-multicore-style/</link>
		<comments>http://www.parallelcoding.com/2010/03/04/march-madness-multicore-style/#comments</comments>
		<pubDate>Thu, 04 Mar 2010 16:57:34 +0000</pubDate>
		<dc:creator>Robert Green</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[HPC]]></category>
		<category><![CDATA[Parallel / Distributed]]></category>

		<guid isPermaLink="false">http://www.parallelcoding.com/?p=536</guid>
		<description><![CDATA[An interesting article has been posted over at Linux Magazine. The article interests me mainly because it gets to the heart of an issue that I myself ponder over: As hardware overtakes software, how will software catch up? This is a very important question, especially in the HPC world as there is no single language [...]]]></description>
			<content:encoded><![CDATA[<p>An interesting article has been posted over at Linux Magazine. The article interests me mainly because it gets to the heart of an issue that I myself ponder over: As hardware overtakes software, how will software catch up? This is a very important question, especially in the HPC world as there is no single language that elegantly translates across multiple architectures.<br />
<br/><br />
Read the rest of the article <a href="http://www.linux-mag.com/id/7722">here</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.parallelcoding.com/2010/03/04/march-madness-multicore-style/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>The Parallel Power Law</title>
		<link>http://www.parallelcoding.com/2010/02/23/the-parallel-power-law/</link>
		<comments>http://www.parallelcoding.com/2010/02/23/the-parallel-power-law/#comments</comments>
		<pubDate>Tue, 23 Feb 2010 14:14:14 +0000</pubDate>
		<dc:creator>Robert Green</dc:creator>
				<category><![CDATA[Parallel / Distributed]]></category>

		<guid isPermaLink="false">http://www.parallelcoding.com/?p=526</guid>
		<description><![CDATA[This article is very interesting. A very hot topic in parallel computing these days is power consumption and this is the first that I&#8217;ve heard of the parallel power law. I&#8217;m intrigued.]]></description>
			<content:encoded><![CDATA[<p><a href="http://perilsofparallel.blogspot.com/2010/02/parallel-power-law.html">This</a> article is very interesting. A very hot topic in parallel computing these days is power consumption and this is the first that I&#8217;ve heard of the parallel power law. I&#8217;m intrigued.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.parallelcoding.com/2010/02/23/the-parallel-power-law/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Particle Swarm Optimization</title>
		<link>http://www.parallelcoding.com/2010/02/01/particle-swarm-optimization/</link>
		<comments>http://www.parallelcoding.com/2010/02/01/particle-swarm-optimization/#comments</comments>
		<pubDate>Mon, 01 Feb 2010 15:20:54 +0000</pubDate>
		<dc:creator>Robert Green</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Heuristics]]></category>
		<category><![CDATA[Optimization]]></category>
		<category><![CDATA[Research]]></category>

		<guid isPermaLink="false">http://www.parallelcoding.com/?p=518</guid>
		<description><![CDATA[In pursuing some of my research as of late, I needed to use Particle Swarm Optimization. All I wanted was some simple code for both Real and Binary PSO &#8211; but I couldn&#8217;t find anything I like! So, I&#8217;m providing a file containing both along with the RNG that I used. Please be aware that [...]]]></description>
			<content:encoded><![CDATA[<p>In pursuing some of my research as of late, I needed to use Particle Swarm Optimization. All I wanted was some simple code for both Real and Binary PSO &#8211; but I couldn&#8217;t find anything I like! So, I&#8217;m providing a file containing both along with the RNG that I used. Please be aware that this code has not been refined. It&#8217;s clear, but rather ugly.<br/><br/></p>
<p><a href="http://www.parallelcoding.com/wp-content/uploads/2010/02/main.cpp">Particle Swarm Optimization Code</a></p>
<p><a href="http://www.parallelcoding.com/wp-content/uploads/2010/02/MersenneTwister.h">Mersenne Twister</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.parallelcoding.com/2010/02/01/particle-swarm-optimization/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<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>Toughest Developer Puzzle Ever</title>
		<link>http://www.parallelcoding.com/2009/07/02/toughest-developer-puzzle-ever/</link>
		<comments>http://www.parallelcoding.com/2009/07/02/toughest-developer-puzzle-ever/#comments</comments>
		<pubDate>Thu, 02 Jul 2009 18:34:20 +0000</pubDate>
		<dc:creator>Robert Green</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[Web Development]]></category>
		<category><![CDATA[puzzle]]></category>

		<guid isPermaLink="false">http://www.parallelcoding.com/?p=463</guid>
		<description><![CDATA[Jeff Blankenburg has put together an incredible puzzle over at http://www.toughestdeveloperpuzzleever.com/tdpe/. Absolutely a ton of fun. It took me a few hours to get through it myself!]]></description>
			<content:encoded><![CDATA[<p>Jeff Blankenburg has put together an incredible puzzle over at <a href="http://www.toughestdeveloperpuzzleever.com/tdpe/">http://www.toughestdeveloperpuzzleever.com/tdpe/</a>. Absolutely a ton of fun. It took me a few hours to get through it myself!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.parallelcoding.com/2009/07/02/toughest-developer-puzzle-ever/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>
	</channel>
</rss>
