<?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>fleurer &#187; parallel</title>
	<atom:link href="http://www.fleurer-lee.com/tag/parallel/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.fleurer-lee.com</link>
	<description>rage and love, story of my life.</description>
	<lastBuildDate>Thu, 02 Sep 2010 11:07:00 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.1</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>并行的quicksort</title>
		<link>http://www.fleurer-lee.com/2009/07/24/%e5%b9%b6%e8%a1%8c%e7%9a%84quicksort/</link>
		<comments>http://www.fleurer-lee.com/2009/07/24/%e5%b9%b6%e8%a1%8c%e7%9a%84quicksort/#comments</comments>
		<pubDate>Fri, 24 Jul 2009 13:01:21 +0000</pubDate>
		<dc:creator>ssword</dc:creator>
				<category><![CDATA[备忘]]></category>
		<category><![CDATA[FP]]></category>
		<category><![CDATA[haskell]]></category>
		<category><![CDATA[parallel]]></category>
		<category><![CDATA[trick]]></category>

		<guid isPermaLink="false">http://swdpress.cn/?p=645315</guid>
		<description><![CDATA[haskell搞并行还是挺方便的。纯函数式没状态嘛，这儿锁啊钥匙啊什么的都不用管了。有个Control.Parallel，里貌似只有两个函数，一个par，表示两个并行计算；一个pseq，表示连续计算。参数都是两个名字。像处理个分治算法啥的，就再合适不过了。于是再次膜拜惰性求值，写起来真的太奇特了。

module Main where
import Control.Parallel
&#160;
--the classic one
qsort :: &#40;Ord a&#41; =&#62; &#91;a&#93; -&#62; &#91;a&#93;
qsort &#91;&#93; = &#91;&#93;
qsort &#40;x:xs&#41; =
 &#40;qsort lt&#41; ++ &#91;x&#93; ++ &#40;qsort gt&#41;
 where
  lt = filter &#40;&#60;x&#41; xs
  gt = filter &#40;&#62;=x&#41; xs
&#160;
&#160;
--the parallel one
psort :: &#40;Ord a&#41; =&#62; &#91;a&#93; -&#62; &#91;a&#93;
psort &#91;&#93; = &#91;&#93;
psort &#40;x:xs&#41; =
 sorted_lt `par` sorted_gt [...]]]></description>
			<content:encoded><![CDATA[<p>haskell搞并行还是挺方便的。纯函数式没状态嘛，这儿锁啊钥匙啊什么的都不用管了。有个Control.Parallel，里貌似只有两个函数，一个par，表示两个并行计算；一个pseq，表示连续计算。参数都是两个名字。像处理个分治算法啥的，就再合适不过了。于是再次膜拜惰性求值，写起来真的太奇特了。</p>

<div class="wp_syntax"><div class="code"><pre class="haskell" style="font-family:monospace;"><span style="color: #06c; font-weight: bold;">module</span> Main <span style="color: #06c; font-weight: bold;">where</span>
<span style="color: #06c; font-weight: bold;">import</span> Control<span style="color: #339933; font-weight: bold;">.</span>Parallel
&nbsp;
<span style="color: #5d478b; font-style: italic;">--the classic one</span>
qsort <span style="color: #339933; font-weight: bold;">::</span> <span style="color: green;">&#40;</span><span style="color: #cccc00; font-weight: bold;">Ord</span> a<span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">=&gt;</span> <span style="color: green;">&#91;</span>a<span style="color: green;">&#93;</span> <span style="color: #339933; font-weight: bold;">-&gt;</span> <span style="color: green;">&#91;</span>a<span style="color: green;">&#93;</span>
qsort <span style="color: green;">&#91;</span><span style="color: green;">&#93;</span> <span style="color: #339933; font-weight: bold;">=</span> <span style="color: green;">&#91;</span><span style="color: green;">&#93;</span>
qsort <span style="color: green;">&#40;</span>x:xs<span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">=</span>
 <span style="color: green;">&#40;</span>qsort lt<span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">++</span> <span style="color: green;">&#91;</span>x<span style="color: green;">&#93;</span> <span style="color: #339933; font-weight: bold;">++</span> <span style="color: green;">&#40;</span>qsort gt<span style="color: green;">&#41;</span>
 <span style="color: #06c; font-weight: bold;">where</span>
  lt <span style="color: #339933; font-weight: bold;">=</span> <span style="font-weight: bold;">filter</span> <span style="color: green;">&#40;</span><span style="color: #339933; font-weight: bold;">&lt;</span>x<span style="color: green;">&#41;</span> xs
  gt <span style="color: #339933; font-weight: bold;">=</span> <span style="font-weight: bold;">filter</span> <span style="color: green;">&#40;</span><span style="color: #339933; font-weight: bold;">&gt;=</span>x<span style="color: green;">&#41;</span> xs
&nbsp;
&nbsp;
<span style="color: #5d478b; font-style: italic;">--the parallel one</span>
psort <span style="color: #339933; font-weight: bold;">::</span> <span style="color: green;">&#40;</span><span style="color: #cccc00; font-weight: bold;">Ord</span> a<span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">=&gt;</span> <span style="color: green;">&#91;</span>a<span style="color: green;">&#93;</span> <span style="color: #339933; font-weight: bold;">-&gt;</span> <span style="color: green;">&#91;</span>a<span style="color: green;">&#93;</span>
psort <span style="color: green;">&#91;</span><span style="color: green;">&#93;</span> <span style="color: #339933; font-weight: bold;">=</span> <span style="color: green;">&#91;</span><span style="color: green;">&#93;</span>
psort <span style="color: green;">&#40;</span>x:xs<span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">=</span>
 sorted<span style="color: #339933; font-weight: bold;">_</span>lt `par` sorted<span style="color: #339933; font-weight: bold;">_</span>gt `pseq` <span style="color: green;">&#40;</span>sorted<span style="color: #339933; font-weight: bold;">_</span>lt <span style="color: #339933; font-weight: bold;">++</span> <span style="color: green;">&#91;</span>x<span style="color: green;">&#93;</span> <span style="color: #339933; font-weight: bold;">++</span> sorted<span style="color: #339933; font-weight: bold;">_</span>gt<span style="color: green;">&#41;</span> <span style="color: #5d478b; font-style: italic;">----unbelieveable,isn't it? :&gt;</span>
 <span style="color: #06c; font-weight: bold;">where</span>
  sorted<span style="color: #339933; font-weight: bold;">_</span>lt <span style="color: #339933; font-weight: bold;">=</span> psort <span style="color: #339933; font-weight: bold;">$</span> <span style="font-weight: bold;">filter</span> <span style="color: green;">&#40;</span><span style="color: #339933; font-weight: bold;">&lt;</span><span style="color: green;">&#41;</span> xs
  sorted<span style="color: #339933; font-weight: bold;">_</span>gt <span style="color: #339933; font-weight: bold;">=</span> psort <span style="color: #339933; font-weight: bold;">$</span> <span style="font-weight: bold;">filter</span> <span style="color: green;">&#40;</span><span style="color: #339933; font-weight: bold;">&gt;=</span>x<span style="color: green;">&#41;</span> xs
&nbsp;
&nbsp;
main <span style="color: #339933; font-weight: bold;">=</span> <span style="color: #06c; font-weight: bold;">do</span> <span style="color: green;">&#123;</span>
 list <span style="color: #339933; font-weight: bold;">&lt;-</span> <span style="font-weight: bold;">return</span> <span style="color: green;">&#91;</span><span style="color: red;">5000</span><span style="color: #339933; font-weight: bold;">,</span>4999<span style="color: #339933; font-weight: bold;">..</span>1<span style="color: green;">&#93;</span>;
 <span style="font-weight: bold;">print</span> <span style="color: #339933; font-weight: bold;">$</span> qsort list;
 <span style="font-weight: bold;">print</span> <span style="color: #339933; font-weight: bold;">$</span> psort list;
<span style="color: green;">&#125;</span></pre></div></div>

<p>profile下，给ghc添加一个编译选项 -prof -O：</p>
<pre>
ghc --make -prof -O -auto-all quicksort.hs
</pre>
<p>执行程序，加一个选项 +RTS -p，它会在本目录下生成一个quicksort.prof文件</p>
<pre>
quicksort +RTS -p
</pre>
<p>quicksort.prof文件的部分内容：</p>
<pre>
 Mon Jul 20 16:40 2009 Time and Allocation Profiling Report  (Final)

    quicksort +RTS -p -RTS

 total time  =        6.86 secs   (343 ticks @ 20 ms)
 total alloc = 1,401,627,416 bytes  (excludes profiling overheads)

COST CENTRE                    MODULE               %time %alloc

qsort                          Main                  69.1   49.9
psort                          Main                  30.9   49.9
</pre>
<p>可见在这台双核的机器上，性能提高了一半多 :)<br />
ps:测试的数据貌似不是很好（按说该用个随机数列），不过知道有这回事就行了~</p>
]]></content:encoded>
			<wfw:commentRss>http://www.fleurer-lee.com/2009/07/24/%e5%b9%b6%e8%a1%8c%e7%9a%84quicksort/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
	</channel>
</rss>
