<?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; Parallel / Distributed</title>
	<atom:link href="http://www.parallelcoding.com/category/development/parallelanddistributed/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.parallelcoding.com</link>
	<description>Intelligent, Efficient, Parallel, and Sustainable Code</description>
	<lastBuildDate>Thu, 02 Feb 2012 15:17:23 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=</generator>
		<item>
		<title>Moore&#039;s vs. May&#039;s</title>
		<link>http://www.parallelcoding.com/2011/03/18/637/</link>
		<comments>http://www.parallelcoding.com/2011/03/18/637/#comments</comments>
		<pubDate>Fri, 18 Mar 2011 14:03:45 +0000</pubDate>
		<dc:creator>Robert Green</dc:creator>
				<category><![CDATA[Parallel / Distributed]]></category>

		<guid isPermaLink="false">http://www.parallelcoding.com/?p=637</guid>
		<description><![CDATA[<a href="http://www.parallelcoding.com/2011/03/18/637/" title="Moore&#039;s vs. May&#039;s"></a>I just read Doublas Eadline's post at  Linux Magazine and found it really fascinating. The best tidbit to chew on is: This is where all the hardware excitement meets the cold reality of parallel programming. We are all familiar with &#8230;<p class="read-more"><a href="http://www.parallelcoding.com/2011/03/18/637/">Read more &#187;</a></p>]]></description>
			<content:encoded><![CDATA[<a href="http://www.parallelcoding.com/2011/03/18/637/" title="Moore&#039;s vs. May&#039;s"></a><p>I just read Doublas Eadline's post at  <a href="http://www.linux-mag.com/id/8422/?hq_e=el&amp;hq_m=1210749&amp;hq_l=12&amp;hq_v=76abbfac82">Linux Magazine</a> and found it really fascinating. The best tidbit to chew on is:</p>
<blockquote><p>This is where all the hardware excitement meets the cold reality of parallel programming. We are all familiar with “Moore’s Law” (the transistor density doubles every two years). Many people have probably not heard of “May’s Law.” (for the purist, you can substitute “trend” for “law”). In any case, <a href="http://en.wikipedia.org/wiki/David_May_%28computer_scientist%29">David May</a> states his law as follows, “Software efficiency halves every 18 months, compensating for Moore’s Law.” Think about it. Every new generation of hardware</p></blockquote>
<p><a href="http://en.wikipedia.org/wiki/Moore's_law">Moore's Law</a> - The number of transistors on a chip doubles roughly every 2 years</p>
<p><a href="http://en.wikipedia.org/wiki/David_May_%28computer_scientist%29">May's Law</a> - Software efficiency halves every 18 months, compensating for Moore’s Law.</p>
<p>Chew on that.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.parallelcoding.com/2011/03/18/637/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Installing the MATLAB plug-in for CUDA in Ubuntu 10.04</title>
		<link>http://www.parallelcoding.com/2010/10/13/installing-the-matlab-plug-in-for-cuda-in-ubuntu-10-04/</link>
		<comments>http://www.parallelcoding.com/2010/10/13/installing-the-matlab-plug-in-for-cuda-in-ubuntu-10-04/#comments</comments>
		<pubDate>Wed, 13 Oct 2010 14:00:21 +0000</pubDate>
		<dc:creator>Robert Green</dc:creator>
				<category><![CDATA[CUDA]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[GPU]]></category>
		<category><![CDATA[Matlab]]></category>
		<category><![CDATA[Parallel / Distributed]]></category>
		<category><![CDATA[Research]]></category>

		<guid isPermaLink="false">http://www.parallelcoding.com/?p=625</guid>
		<description><![CDATA[<a href="http://www.parallelcoding.com/2010/10/13/installing-the-matlab-plug-in-for-cuda-in-ubuntu-10-04/" title="Installing the MATLAB plug-in for CUDA in Ubuntu 10.04"></a>I had previously installed CUDA and CUDA SDK on my computer running Ubuntu 10.04. Matlab is also installed on the computer, and some of the students are currently using libSVM for data classification research. One of the major problems that &#8230;<p class="read-more"><a href="http://www.parallelcoding.com/2010/10/13/installing-the-matlab-plug-in-for-cuda-in-ubuntu-10-04/">Read more &#187;</a></p>]]></description>
			<content:encoded><![CDATA[<a href="http://www.parallelcoding.com/2010/10/13/installing-the-matlab-plug-in-for-cuda-in-ubuntu-10-04/" title="Installing the MATLAB plug-in for CUDA in Ubuntu 10.04"></a><p>I had previously installed CUDA and CUDA SDK on my computer running Ubuntu 10.04. Matlab is also installed on the computer, and some of the students are currently using libSVM for data classification research. One of the major problems that we are having is that the data sets are so large that libSVM is simply too slow to use. The solution - let's add cuSVM to the installation so they can speed up their code easily.</p>
<p>I planned on following the instructions <a href="http://www.google.com/url?sa=t&amp;source=web&amp;cd=1&amp;ved=0CBIQFjAA&amp;url=http%3A%2F%2Fwww.ece.nmsu.edu%2F~pdeleon%2FSoftware%2FcuSVM%2FcuSVM_Linux64_Info.pdf&amp;rct=j&amp;q=cuSVM%20matlab&amp;ei=9LO1TJbsCMnPnAeV0MyADQ&amp;usg=AFQjCNFakY3hGqO2o0gRLwhbvSVmV-Yv4w&amp;sig2=rwGfg7ra_8MkpEekJNb-RQ&amp;cad=rja" target="_blank">here</a> where the first step is "Download Matlab plug-in for Cuda (Version 1.1) from <a href="http://developer.nvidia.com/object/matlab_cuda.html" target="_blank">http://developer.nvidia.com/object/matlab_cuda.html</a>". Along the way I decided that I would not only download the Matlab plug-in for Cuda, but that I would install and test it as well. Though, along the way I ran into a couple of issues. Here are the steps that I followed:</p>
<ol>
<li>Download the Matlab plug-in for CUDA and extract it</li>
<li>Open the Makefile for editing</li>
<li>Add a line at the very beginning of the file that specifies the current directory. In my example this is:
<pre class="brush: bash; title: ; notranslate">CWD=/path/to/my/home/directory/Matlab_Cuda_1.1</pre>
</li>
<li>If you are on a 64-bit system (as I am), make sure that line 6 reads as :
<pre class="brush: bash; title: ; notranslate">INCLUDELIB  = -L$(CUDAHOME)/lib64 -lcufft  -Wl,-rpath,$(CUDAHOME)/lib64</pre>
</li>
<li>Change line 11 (export Matlab=/usr/local/matlab) so that it matches your Matlab installation. In my case this was /opt/Matlab</li>
<li>On lines 38, 45, and 52 you will find references to a script called nvopts.sh. When I first ran this Makefile, I continually got errors telling me that nvopts.sh did not exist. How to fix this? Add the correct path to the nvopts.sh reference. I did this by following the instructions in step 3 and then I adjusted lines 39, 46, and 53 to read as follows:
<pre class="brush: bash; title: ; notranslate">$(NVMEX) -f $(CWD)/nvopts.sh $(INCLUDEDIR) $(INCLUDELIB)</pre>
</li>
<li>Save the Makefile</li>
<li>Open the file called nvmex</li>
<li>Comment out lines 1448-1454. Just in case I have the line numbers slightly wrong, these lines read like this:

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">$compile_only</span>&quot;</span> <span style="color: #000000; font-weight: bold;">!</span>= <span style="color: #ff0000;">&quot;1&quot;</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span>; <span style="color: #000000; font-weight: bold;">then</span>
    <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">$gateway_lang</span>&quot;</span> = <span style="color: #ff0000;">&quot;C&quot;</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span>; <span style="color: #000000; font-weight: bold;">then</span>
    	<span style="color: #007800;">files</span>=<span style="color: #ff0000;">&quot;<span style="color: #007800;">$files</span> <span style="color: #007800;">$MATLAB</span>/extern/src/mexversion.c&quot;</span>
    <span style="color: #000000; font-weight: bold;">else</span>
        <span style="color: #007800;">files</span>=<span style="color: #ff0000;">&quot;<span style="color: #007800;">$files</span> <span style="color: #007800;">$MATLAB</span>/extern/lib/<span style="color: #007800;">$Arch</span>/version4.o&quot;</span>
    <span style="color: #000000; font-weight: bold;">fi</span>
<span style="color: #000000; font-weight: bold;">fi</span></pre></div></div>

<p>and should now look like</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#if [ &quot;$compile_only&quot; != &quot;1&quot; ]; then</span>
<span style="color: #666666; font-style: italic;">#    if [ &quot;$gateway_lang&quot; = &quot;C&quot; ]; then</span>
<span style="color: #666666; font-style: italic;">#    	files=&quot;$files $MATLAB/extern/src/mexversion.c&quot;</span>
<span style="color: #666666; font-style: italic;">#    else</span>
<span style="color: #666666; font-style: italic;">#        files=&quot;$files $MATLAB/extern/lib/$Arch/version4.o&quot;</span>
<span style="color: #666666; font-style: italic;">#    fi</span>
<span style="color: #666666; font-style: italic;">#fi</span></pre></div></div>

</li>
<li>Save nvmex</li>
<li>In the case that your paths are not set properly (mine are not) open up nvopts.sh. Make sure that the call to nvcc on lines 56, 87 and 164 are set correctly. I changed these from
<pre class="brush: bash; title: ; notranslate">CC='nvcc'</pre>
<p>to</p>
<pre class="brush: bash; title: ; notranslate">CC='/usr/local/cuda/bin/nvcc'</pre>
</li>
<li>Save nvmex</li>
<li>Open the terminal in the directory where your Makefile is and type 'make all'. Enjoy!</li>
</ol>
]]></content:encoded>
			<wfw:commentRss>http://www.parallelcoding.com/2010/10/13/installing-the-matlab-plug-in-for-cuda-in-ubuntu-10-04/feed/</wfw:commentRss>
		<slash:comments>2</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[<a href="http://www.parallelcoding.com/2010/03/04/march-madness-multicore-style/" title="March Madness Multicore Style"></a>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 &#8230;<p class="read-more"><a href="http://www.parallelcoding.com/2010/03/04/march-madness-multicore-style/">Read more &#187;</a></p>]]></description>
			<content:encoded><![CDATA[<a href="http://www.parallelcoding.com/2010/03/04/march-madness-multicore-style/" title="March Madness Multicore Style"></a><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[<a href="http://www.parallelcoding.com/2010/02/23/the-parallel-power-law/" title="The Parallel Power Law"></a>This article is very interesting. A very hot topic in parallel computing these days is power consumption and this is the first that I've heard of the parallel power law. I'm intrigued.]]></description>
			<content:encoded><![CDATA[<a href="http://www.parallelcoding.com/2010/02/23/the-parallel-power-law/" title="The Parallel Power Law"></a><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've heard of the parallel power law. I'm intrigued.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.parallelcoding.com/2010/02/23/the-parallel-power-law/feed/</wfw:commentRss>
		<slash:comments>1</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[<a href="http://www.parallelcoding.com/2009/07/10/intel-threading-challenge-1/" title="Intel Threading Challenge #1"></a>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 &#8230;<p class="read-more"><a href="http://www.parallelcoding.com/2009/07/10/intel-threading-challenge-1/">Read more &#187;</a></p>]]></description>
			<content:encoded><![CDATA[<a href="http://www.parallelcoding.com/2009/07/10/intel-threading-challenge-1/" title="Intel Threading Challenge #1"></a><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'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 0x20). The number of keys within the file is less than 2^31 - 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; font-weight: bold;">using</span> <span style="color: #008080;">System</span><span style="color: #008000;">;</span>
<span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008080;">System.Collections.Generic</span><span style="color: #008000;">;</span>
<span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008080;">System.Linq</span><span style="color: #008000;">;</span>
<span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008080;">System.Text</span><span style="color: #008000;">;</span>
<span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008080;">System.Collections</span><span style="color: #008000;">;</span>
<span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008080;">System.Diagnostics</span><span style="color: #008000;">;</span>
<span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008080;">System.Threading</span><span style="color: #008000;">;</span>
<span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008080;">System.IO</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #0600FF; font-weight: bold;">namespace</span> RadixSort <span style="color: #008000;">&#123;</span>
&nbsp;
    <span style="color: #6666cc; font-weight: bold;">class</span> Program <span style="color: #008000;">&#123;</span>
        <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #6666cc; font-weight: bold;">void</span> Main<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">string</span><span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span> args<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
            StreamReader sr<span style="color: #008000;">;</span>
            <span style="color: #6666cc; font-weight: bold;">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: #008000;">.</span><span style="color: #0000FF;">OpenText</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">@&quot;C:\Documents and Settings\rgreen\Desktop\Threading\Threading\rsTestK100.dat&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
            length <span style="color: #008000;">=</span> Convert<span style="color: #008000;">.</span><span style="color: #0000FF;">ToInt32</span><span style="color: #008000;">&#40;</span>sr<span style="color: #008000;">.</span><span style="color: #0000FF;">ReadLine</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Trim</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
            <span style="color: #6666cc; font-weight: bold;">string</span><span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span> values <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> <span style="color: #6666cc; font-weight: bold;">string</span><span style="color: #008000;">&#91;</span>length<span style="color: #008000;">&#93;</span><span style="color: #008000;">;</span>
            <span style="color: #6666cc; font-weight: bold;">string</span><span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span> newValues <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> <span style="color: #6666cc; font-weight: bold;">string</span><span style="color: #008000;">&#91;</span>length<span style="color: #008000;">&#93;</span><span style="color: #008000;">;</span>
            <span style="color: #0600FF; font-weight: bold;">for</span><span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">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: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
                values<span style="color: #008000;">&#91;</span>x<span style="color: #008000;">&#93;</span> <span style="color: #008000;">=</span> sr<span style="color: #008000;">.</span><span style="color: #0000FF;">ReadLine</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Trim</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
            <span style="color: #008000;">&#125;</span>
&nbsp;
            Stopwatch sw <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> Stopwatch<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#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: #008000;">.</span><span style="color: #0000FF;">Start</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
            RadixSort<span style="color: #008000;">&#40;</span>values<span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">CopyTo</span><span style="color: #008000;">&#40;</span>newValues, <span style="color: #FF0000;">0</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
            sw<span style="color: #008000;">.</span><span style="color: #0000FF;">Stop</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
            serial <span style="color: #008000;">=</span> sw<span style="color: #008000;">.</span><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: #008000;">.</span><span style="color: #0000FF;">Reset</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
            sw<span style="color: #008000;">.</span><span style="color: #0000FF;">Start</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
            ParallelRadixSort<span style="color: #008000;">&#40;</span>values<span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">CopyTo</span><span style="color: #008000;">&#40;</span>newValues, <span style="color: #FF0000;">0</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
            sw<span style="color: #008000;">.</span><span style="color: #0000FF;">Stop</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
            parallel <span style="color: #008000;">=</span> sw<span style="color: #008000;">.</span><span style="color: #0000FF;">Elapsed</span><span style="color: #008000;">;</span>
&nbsp;
&nbsp;
            Console<span style="color: #008000;">.</span><span style="color: #0000FF;">WriteLine</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;Serial Time: &quot;</span> <span style="color: #008000;">+</span> serial<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
            Console<span style="color: #008000;">.</span><span style="color: #0000FF;">WriteLine</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;Parallel Time: &quot;</span> <span style="color: #008000;">+</span> parallel<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
            Console<span style="color: #008000;">.</span><span style="color: #0000FF;">ReadLine</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #008000;">&#125;</span>
&nbsp;
        <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #6666cc; font-weight: bold;">string</span><span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span> ParallelRadixSort<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">string</span><span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span> array<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
            <span style="color: #6666cc; font-weight: bold;">int</span> length <span style="color: #008000;">=</span> array<span style="color: #008000;">.</span><span style="color: #0000FF;">Length</span><span style="color: #008000;">;</span>
&nbsp;
            Parallel<span style="color: #008000;">.</span><span style="color: #0600FF; font-weight: bold;">For</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">0</span>, array<span style="color: #008000;">&#91;</span><span style="color: #FF0000;">0</span><span style="color: #008000;">&#93;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Length</span> <span style="color: #008000;">-</span> <span style="color: #FF0000;">1</span>, <span style="color: #6666cc; font-weight: bold;">delegate</span><span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">int</span> curRadix<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
                <span style="color: #6666cc; font-weight: bold;">int</span> index <span style="color: #008000;">=</span> array<span style="color: #008000;">&#91;</span><span style="color: #FF0000;">0</span><span style="color: #008000;">&#93;</span><span style="color: #008000;">.</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: #008000;">&#40;</span>array, array<span style="color: #008000;">&#91;</span><span style="color: #FF0000;">0</span><span style="color: #008000;">&#93;</span><span style="color: #008000;">.</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: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Results</span><span style="color: #008000;">;</span>
            <span style="color: #008000;">&#125;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
            <span style="color: #0600FF; font-weight: bold;">return</span> array<span style="color: #008000;">;</span>
        <span style="color: #008000;">&#125;</span>
&nbsp;
        <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #6666cc; font-weight: bold;">string</span><span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span> RadixSort<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">string</span><span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span> array<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
&nbsp;
            <span style="color: #6666cc; font-weight: bold;">int</span> length <span style="color: #008000;">=</span> array<span style="color: #008000;">.</span><span style="color: #0000FF;">Length</span><span style="color: #008000;">;</span>
&nbsp;
            <span style="color: #0600FF; font-weight: bold;">for</span><span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">int</span> curRadix <span style="color: #008000;">=</span> array<span style="color: #008000;">&#91;</span><span style="color: #FF0000;">0</span><span style="color: #008000;">&#93;</span><span style="color: #008000;">.</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: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
                array <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> MergeSort<span style="color: #008000;">&#40;</span>array, array<span style="color: #008000;">&#91;</span><span style="color: #FF0000;">0</span><span style="color: #008000;">&#93;</span><span style="color: #008000;">.</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;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Results</span><span style="color: #008000;">;</span>
            <span style="color: #008000;">&#125;</span>
&nbsp;
            <span style="color: #0600FF; font-weight: bold;">return</span> array<span style="color: #008000;">;</span>
        <span style="color: #008000;">&#125;</span>
    <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#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[<a href="http://www.parallelcoding.com/2009/07/01/project-euler-6/" title="Project Euler #6"></a>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 &#8230;<p class="read-more"><a href="http://www.parallelcoding.com/2009/07/01/project-euler-6/">Read more &#187;</a></p>]]></description>
			<content:encoded><![CDATA[<a href="http://www.parallelcoding.com/2009/07/01/project-euler-6/" title="Project Euler #6"></a><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; font-weight: bold;">static</span> <span style="color: #6666cc; font-weight: bold;">double</span> SumOfSquares<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">int</span> min, <span style="color: #6666cc; font-weight: bold;">int</span> max<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
    <span style="color: #6666cc; font-weight: bold;">double</span> result <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span>
    <span style="color: #0600FF; font-weight: bold;">for</span><span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">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: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
        result <span style="color: #008000;">+=</span> Math<span style="color: #008000;">.</span><span style="color: #0000FF;">Pow</span><span style="color: #008000;">&#40;</span>x, <span style="color: #FF0000;">2</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
     <span style="color: #008000;">&#125;</span>
     <span style="color: #0600FF; font-weight: bold;">return</span> result<span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span>
<span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #6666cc; font-weight: bold;">double</span> SquareOfSums<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">int</span> min, <span style="color: #6666cc; font-weight: bold;">int</span> max<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
    <span style="color: #6666cc; font-weight: bold;">double</span> result <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span>
    <span style="color: #0600FF; font-weight: bold;">for</span><span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">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: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
        result <span style="color: #008000;">+=</span> x<span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span>
    <span style="color: #0600FF; font-weight: bold;">return</span> Math<span style="color: #008000;">.</span><span style="color: #0000FF;">Pow</span><span style="color: #008000;">&#40;</span>result, <span style="color: #FF0000;">2</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p> Both of those functions are very straight forward. My first thought after writing these was, "Hey, why not use some LINQ?" 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; font-weight: bold;">static</span> <span style="color: #6666cc; font-weight: bold;">double</span> LinqSumOfSquares<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">int</span> min, <span style="color: #6666cc; font-weight: bold;">int</span> max<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
    <span style="color: #0600FF; font-weight: bold;">return</span> Enumerable<span style="color: #008000;">.</span><span style="color: #0000FF;">Range</span><span style="color: #008000;">&#40;</span>min, max<span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0600FF; font-weight: bold;">Select</span><span style="color: #008000;">&#40;</span>d <span style="color: #008000;">=&gt;</span> Math<span style="color: #008000;">.</span><span style="color: #0000FF;">Pow</span><span style="color: #008000;">&#40;</span>d, <span style="color: #FF0000;">2</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Sum</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>           
<span style="color: #008000;">&#125;</span>
<span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #6666cc; font-weight: bold;">double</span> LinqSquareOfSums<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">int</span> min, <span style="color: #6666cc; font-weight: bold;">int</span> max<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
    <span style="color: #0600FF; font-weight: bold;">return</span> Math<span style="color: #008000;">.</span><span style="color: #0000FF;">Pow</span><span style="color: #008000;">&#40;</span>Enumerable<span style="color: #008000;">.</span><span style="color: #0000FF;">Range</span><span style="color: #008000;">&#40;</span>min, max<span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0600FF; font-weight: bold;">Select</span><span style="color: #008000;">&#40;</span>d <span style="color: #008000;">=&gt;</span> d<span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Sum</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>, <span style="color: #FF0000;">2</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>    
<span style="color: #008000;">&#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'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: "Well that sucks." Luckily there's another class in the Parallel library called Futures. What's a future? From <a href="http://www.devx.com/dotnet/Article/39204/1763/page/5">DevX</a> -<br />
<blockquote>"In TPL terms, a Future is basically a task that returns a value. It's like a deferred function. You start it running and then use its value later. If the Future hasn't finished calculating its value by the time you need it, it makes you wait while it finishes." </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; font-weight: bold;">static</span> <span style="color: #6666cc; font-weight: bold;">long</span> Problem6Futures<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">int</span> min, <span style="color: #6666cc; font-weight: bold;">int</span> max<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
&nbsp;
    Future<span style="color: #008000;">&lt;</span><span style="color: #6666cc; font-weight: bold;">double</span><span style="color: #008000;">&gt;</span> fSumOfSquares <span style="color: #008000;">=</span> Future<span style="color: #008000;">.</span><span style="color: #0000FF;">Create</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">=&gt;</span> SumOfSquares<span style="color: #008000;">&#40;</span>min, max<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    Future<span style="color: #008000;">&lt;</span><span style="color: #6666cc; font-weight: bold;">double</span><span style="color: #008000;">&gt;</span> fSquareOfSums <span style="color: #008000;">=</span> Future<span style="color: #008000;">.</span><span style="color: #0000FF;">Create</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">=&gt;</span> SquareOfSums<span style="color: #008000;">&#40;</span>min, max<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #6666cc; font-weight: bold;">double</span> result <span style="color: #008000;">=</span> fSquareOfSums<span style="color: #008000;">.</span><span style="color: #0000FF;">Value</span> <span style="color: #008000;">-</span> fSumOfSquares<span style="color: #008000;">.</span><span style="color: #0000FF;">Value</span><span style="color: #008000;">;</span>
&nbsp;
    <span style="color: #0600FF; font-weight: bold;">return</span> result<span style="color: #008000;">;</span>
<span style="color: #008000;">&#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's even easier because of PLINQ - or Parallel LINQ. Let'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; font-weight: bold;">static</span> <span style="color: #6666cc; font-weight: bold;">double</span> ParallelSumOfSquares<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">int</span> min, <span style="color: #6666cc; font-weight: bold;">int</span> max<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
    <span style="color: #0600FF; font-weight: bold;">return</span> Enumerable<span style="color: #008000;">.</span><span style="color: #0000FF;">Range</span><span style="color: #008000;">&#40;</span>min, max<span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">AsParallel</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0600FF; font-weight: bold;">Select</span><span style="color: #008000;">&#40;</span>d <span style="color: #008000;">=&gt;</span> Math<span style="color: #008000;">.</span><span style="color: #0000FF;">Pow</span><span style="color: #008000;">&#40;</span>d, <span style="color: #FF0000;">2</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Sum</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span>
<span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #6666cc; font-weight: bold;">double</span> ParallelSquareOfSums<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">int</span> min, <span style="color: #6666cc; font-weight: bold;">int</span> max<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
    <span style="color: #0600FF; font-weight: bold;">return</span> Math<span style="color: #008000;">.</span><span style="color: #0000FF;">Pow</span><span style="color: #008000;">&#40;</span>Enumerable<span style="color: #008000;">.</span><span style="color: #0000FF;">Range</span><span style="color: #008000;">&#40;</span>min, max<span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">AsParallel</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Sum</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>, <span style="color: #FF0000;">2</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#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'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' start somewhere when you're learning how to parallelize algorithms using a new library. Today I used Futures and PLINQ and that sound'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[<a href="http://www.parallelcoding.com/2009/06/30/project-euler-3/" title="Project Euler #3"></a>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 &#8230;<p class="read-more"><a href="http://www.parallelcoding.com/2009/06/30/project-euler-3/">Read more &#187;</a></p>]]></description>
			<content:encoded><![CDATA[<a href="http://www.parallelcoding.com/2009/06/30/project-euler-3/" title="Project Euler #3"></a><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; font-weight: bold;">static</span> <span style="color: #6666cc; font-weight: bold;">void</span> Problem3<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
    <span style="color: #6666cc; font-weight: bold;">long</span> n <span style="color: #008000;">=</span> <span style="color: #FF0000;">600851475143</span><span style="color: #008000;">;</span>
    <span style="color: #6666cc; font-weight: bold;">int</span> factor <span style="color: #008000;">=</span> <span style="color: #FF0000;">2</span><span style="color: #008000;">;</span>
    <span style="color: #6666cc; font-weight: bold;">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; font-weight: bold;">if</span><span style="color: #008000;">&#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: #008000;">&#41;</span> <span style="color: #008000;">&#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; font-weight: bold;">while</span><span style="color: #008000;">&#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: #008000;">&#41;</span> <span style="color: #008000;">&#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: #008000;">&#125;</span>
     <span style="color: #008000;">&#125;</span> <span style="color: #0600FF; font-weight: bold;">else</span> <span style="color: #008000;">&#123;</span>
         lastFactor <span style="color: #008000;">=</span> <span style="color: #FF0000;">1</span><span style="color: #008000;">;</span>
     <span style="color: #008000;">&#125;</span>
     factor <span style="color: #008000;">=</span> <span style="color: #FF0000;">3</span><span style="color: #008000;">;</span>
&nbsp;
     <span style="color: #6666cc; font-weight: bold;">double</span> maxFactor <span style="color: #008000;">=</span> Math<span style="color: #008000;">.</span><span style="color: #0000FF;">Sqrt</span><span style="color: #008000;">&#40;</span>n<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
     <span style="color: #0600FF; font-weight: bold;">while</span><span style="color: #008000;">&#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: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
          <span style="color: #0600FF; font-weight: bold;">if</span><span style="color: #008000;">&#40;</span>n <span style="color: #008000;">%</span> factor <span style="color: #008000;">==</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#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; font-weight: bold;">while</span><span style="color: #008000;">&#40;</span>n <span style="color: #008000;">%</span> factor <span style="color: #008000;">==</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
                  n <span style="color: #008000;">=</span> n <span style="color: #008000;">/</span> factor<span style="color: #008000;">;</span>
              <span style="color: #008000;">&#125;</span>
              maxFactor <span style="color: #008000;">=</span> Math<span style="color: #008000;">.</span><span style="color: #0000FF;">Sqrt</span><span style="color: #008000;">&#40;</span>n<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
          <span style="color: #008000;">&#125;</span>
          factor <span style="color: #008000;">+=</span> <span style="color: #FF0000;">2</span><span style="color: #008000;">;</span>
      <span style="color: #008000;">&#125;</span>
&nbsp;
      <span style="color: #0600FF; font-weight: bold;">if</span><span style="color: #008000;">&#40;</span>n <span style="color: #008000;">==</span> <span style="color: #FF0000;">1</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
          Console<span style="color: #008000;">.</span><span style="color: #0000FF;">Write</span><span style="color: #008000;">&#40;</span>lastFactor<span style="color: #008000;">.</span><span style="color: #0000FF;">ToString</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">+</span> <span style="color: #666666;">&quot; &quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
      <span style="color: #008000;">&#125;</span> <span style="color: #0600FF; font-weight: bold;">else</span> <span style="color: #008000;">&#123;</span>
          Console<span style="color: #008000;">.</span><span style="color: #0000FF;">Write</span><span style="color: #008000;">&#40;</span>n<span style="color: #008000;">.</span><span style="color: #0000FF;">ToString</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">+</span> <span style="color: #666666;">&quot; &quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
      <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#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; font-weight: bold;">static</span> <span style="color: #6666cc; font-weight: bold;">void</span> LinqProblem3<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
    <span style="color: #6666cc; font-weight: bold;">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> <span style="color: #0600FF; font-weight: bold;">from</span> p <span style="color: #0600FF; font-weight: bold;">in</span> Primes<span style="color: #008000;">.</span><span style="color: #0000FF;">PrimeFactors</span><span style="color: #008000;">&#40;</span>largeNumber<span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">AsParallel</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
                                 orderby p descending
                                 <span style="color: #0600FF; font-weight: bold;">select</span> p<span style="color: #008000;">;</span>
&nbsp;
    <span style="color: #0600FF; font-weight: bold;">foreach</span><span style="color: #008000;">&#40;</span>var f <span style="color: #0600FF; font-weight: bold;">in</span> allPrimeFactors<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#123;</span>
        Console<span style="color: #008000;">.</span><span style="color: #0000FF;">WriteLine</span><span style="color: #008000;">&#40;</span>f<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #6666cc; font-weight: bold;">class</span> Primes <span style="color: #008000;">&#123;</span>
&nbsp;
    <span style="color: #008080; font-style: italic;">// Find all prime factors.</span>
    <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">static</span> IEnumerable<span style="color: #008000;">&lt;</span><span style="color: #6666cc; font-weight: bold;">int</span><span style="color: #008000;">&gt;</span> PrimeFactors<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">long</span> number<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
        <span style="color: #008080; font-style: italic;">// Start by removing the lowest prime (2)</span>
        <span style="color: #0600FF; font-weight: bold;">return</span> MorePrimeFactors<span style="color: #008000;">&#40;</span>number, <span style="color: #FF0000;">2</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span>
&nbsp;
    <span style="color: #008080; font-style: italic;">// This recursive method finds all prime factors.</span>
    <span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #0600FF; font-weight: bold;">static</span> IEnumerable<span style="color: #008000;">&lt;</span><span style="color: #6666cc; font-weight: bold;">int</span><span style="color: #008000;">&gt;</span> MorePrimeFactors<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">long</span> number, <span style="color: #6666cc; font-weight: bold;">int</span> factor<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
        <span style="color: #008080; font-style: italic;">// Find the next prime factor</span>
	<span style="color: #0600FF; font-weight: bold;">while</span><span style="color: #008000;">&#40;</span>number <span style="color: #008000;">%</span> factor <span style="color: #008000;">!=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">&#41;</span>
	    factor<span style="color: #008000;">++;</span>
	<span style="color: #008080; font-style: italic;">// Return it.</span>
	<span style="color: #0600FF; font-weight: bold;">yield</span> <span style="color: #0600FF; font-weight: bold;">return</span> factor<span style="color: #008000;">;</span>
&nbsp;
	<span style="color: #008080; font-style: italic;">// look again...</span>
	<span style="color: #0600FF; font-weight: bold;">if</span><span style="color: #008000;">&#40;</span>number <span style="color: #008000;">&gt;</span> factor<span style="color: #008000;">&#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; font-weight: bold;">foreach</span><span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">int</span> factors <span style="color: #0600FF; font-weight: bold;">in</span> MorePrimeFactors<span style="color: #008000;">&#40;</span>number <span style="color: #008000;">/</span> factor, factor<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>
			<span style="color: #0600FF; font-weight: bold;">yield</span> <span style="color: #0600FF; font-weight: bold;">return</span> factors<span style="color: #008000;">;</span>
	<span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#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>Multiprocessing with Python</title>
		<link>http://www.parallelcoding.com/2009/03/26/multiprocessing-with-python/</link>
		<comments>http://www.parallelcoding.com/2009/03/26/multiprocessing-with-python/#comments</comments>
		<pubDate>Thu, 26 Mar 2009 12:36:08 +0000</pubDate>
		<dc:creator></dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Parallel / Distributed]]></category>
		<category><![CDATA[Python]]></category>

		<guid isPermaLink="false">http://www.parallelcoding.com/?p=310</guid>
		<description><![CDATA[<a href="http://www.parallelcoding.com/2009/03/26/multiprocessing-with-python/" title="Multiprocessing with Python"></a>Available here]]></description>
			<content:encoded><![CDATA[<a href="http://www.parallelcoding.com/2009/03/26/multiprocessing-with-python/" title="Multiprocessing with Python"></a><p>Available <a href="http://www.ibm.com/developerworks/aix/library/au-multiprocessing/index.html">here</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.parallelcoding.com/2009/03/26/multiprocessing-with-python/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>My First Moments with Haskell</title>
		<link>http://www.parallelcoding.com/2008/12/23/my-first-moments-with-haskell/</link>
		<comments>http://www.parallelcoding.com/2008/12/23/my-first-moments-with-haskell/#comments</comments>
		<pubDate>Tue, 23 Dec 2008 21:11:41 +0000</pubDate>
		<dc:creator>manatarms</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Parallel / Distributed]]></category>

		<guid isPermaLink="false">http://www.parallelcoding.com/?p=197</guid>
		<description><![CDATA[<a href="http://www.parallelcoding.com/2008/12/23/my-first-moments-with-haskell/" title="My First Moments with Haskell"></a>As of this moment, I have worked through section 4.5.2 of the very well written Yet Another Haskell Tutorial. What have I learned so far? How to use HUGS and GHC to play with and compile Haskell. The basics of &#8230;<p class="read-more"><a href="http://www.parallelcoding.com/2008/12/23/my-first-moments-with-haskell/">Read more &#187;</a></p>]]></description>
			<content:encoded><![CDATA[<a href="http://www.parallelcoding.com/2008/12/23/my-first-moments-with-haskell/" title="My First Moments with Haskell"></a><p>As of this moment, I have worked through section 4.5.2 of the very well written <a href="http://darcs.haskell.org/yaht/yaht.pdf"> Yet Another Haskell Tutorial</a>. What have I learned so far?</p>
<ol>
<li>How to use HUGS and GHC to play with and compile Haskell.</li>
<li>The basics of arithmetic, pairs, tuples, and lists.</li>
<li>The basic built in functions (map, foldr, foldl, etc.)</li>
<li>How to create source files</li>
<li>How Haskell treats types</li>
</ol>
<p>What impressions do I have this far? Functional programming seems to be a pretty good deal in my eyes. The most appealing aspect is its base in real mathematics. All computer science algorithms are obviously based in some sort of mathematics, but with functional programming it seems that the good kind of mathematics becomes more apparent. This would be the type of mathematics that is translated almost directly to code with no worry about writing loops and other structures to manage your data.  A quick example of this would be the Fibonacci Sequence. Mathematically this looks like this:</p>
<p><strong>X<sub>1</sub> = 1</strong><br />
<strong>X<sub>2</sub> = 1 + X<sub>1</sub></strong><br />
<strong>X<sub>n</sub> = X<sub>n-1</sub> + X<sub>n-2</sub></strong></p>
<p>In Haskell it looks like:</p>
<pre lang="haskell" lnie="1">fib 1 = 1;
fib 2 = 1 + fib(1);
fib x = fib(x-2) + fib(x-1);</pre>
<p style="margin-bottom:20px;">or</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code"><pre class="haskell" style="font-family:monospace;">fib n <span style="color: #339933; font-weight: bold;">=</span>
  <span style="color: #06c; font-weight: bold;">if</span> n <span style="color: #339933; font-weight: bold;">==</span> <span style="color: red;">1</span> <span style="color: #339933; font-weight: bold;">||</span> n <span style="color: #339933; font-weight: bold;">==</span> <span style="color: red;">2</span>
    <span style="color: #06c; font-weight: bold;">then</span> <span style="color: red;">1</span>
    <span style="color: #06c; font-weight: bold;">else</span> fib <span style="color: green;">&#40;</span>n<span style="color: #339933; font-weight: bold;">-</span><span style="color: red;">1</span><span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">+</span> fib <span style="color: green;">&#40;</span>n<span style="color: #339933; font-weight: bold;">-</span><span style="color: red;">2</span><span style="color: green;">&#41;</span></pre></td></tr></table></div>

<p>As you can see above, we have simply translated a recursive mathematical formula almost directly into code! That is powerful! Another functional programming feature used is pattern matching. Notice the fib 1, fib 2, and fib n from the first example? Whenever the code encounters a call to the fib function, it matches the parameter against 1,2, or n in order to determine which function to call. Sweet!</p>
<p>Maybe it's just me, but the sheer mathiness of Haskell and functional programming makes me giddy! It makes me imagine a world where code is written correctly because it follows mathematics! Now that is exciting.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.parallelcoding.com/2008/12/23/my-first-moments-with-haskell/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>What&#039;s the next big thing in parallel?</title>
		<link>http://www.parallelcoding.com/2008/12/03/whats-the-next-big-thing-in-parallel/</link>
		<comments>http://www.parallelcoding.com/2008/12/03/whats-the-next-big-thing-in-parallel/#comments</comments>
		<pubDate>Wed, 03 Dec 2008 21:45:57 +0000</pubDate>
		<dc:creator></dc:creator>
				<category><![CDATA[Parallel / Distributed]]></category>
		<category><![CDATA[Erlang]]></category>
		<category><![CDATA[F#]]></category>
		<category><![CDATA[Parallel]]></category>

		<guid isPermaLink="false">http://www.parallelcoding.com/?p=83</guid>
		<description><![CDATA[<a href="http://www.parallelcoding.com/2008/12/03/whats-the-next-big-thing-in-parallel/" title="What&#039;s the next big thing in parallel?"></a>I'm telling you it's going to be functional programming. Myself, I am going to begin exploring Haskell, Erlang, and F# and I hope to document all of my little exploits and such here. I'll learn a bit and share a &#8230;<p class="read-more"><a href="http://www.parallelcoding.com/2008/12/03/whats-the-next-big-thing-in-parallel/">Read more &#187;</a></p>]]></description>
			<content:encoded><![CDATA[<a href="http://www.parallelcoding.com/2008/12/03/whats-the-next-big-thing-in-parallel/" title="What&#039;s the next big thing in parallel?"></a><p>I'm telling you it's going to be functional programming. Myself, I am going to begin exploring Haskell, Erlang, and F# and I hope to document all of my little exploits and such here. I'll learn a bit and share a bit. If I get really ambitious you may also see some F# code pop up here as well.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.parallelcoding.com/2008/12/03/whats-the-next-big-thing-in-parallel/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

