<?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>Juozas devBlog &#187; university</title>
	<atom:link href="http://dev.juokaz.com/tag/university/feed" rel="self" type="application/rss+xml" />
	<link>http://dev.juokaz.com</link>
	<description>Random ideas, scripts and facts</description>
	<lastBuildDate>Mon, 22 Mar 2010 10:48:42 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Parallel processes in PHP</title>
		<link>http://dev.juokaz.com/php/parallel-processes-in-php</link>
		<comments>http://dev.juokaz.com/php/parallel-processes-in-php#comments</comments>
		<pubDate>Thu, 12 Feb 2009 23:36:18 +0000</pubDate>
		<dc:creator>Juozas</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[apache]]></category>
		<category><![CDATA[computing]]></category>
		<category><![CDATA[cron]]></category>
		<category><![CDATA[graphics]]></category>
		<category><![CDATA[Haskell]]></category>
		<category><![CDATA[http]]></category>
		<category><![CDATA[parallel]]></category>
		<category><![CDATA[rayracer]]></category>
		<category><![CDATA[university]]></category>

		<guid isPermaLink="false">http://dev.juokaz.com/?p=108</guid>
		<description><![CDATA[When I was coding Ray-Tracer project for my Computer Science studies in university, I ran into using Haskell parallel map function (map calls function for all list elements). Ray-Tracer runs reflections, shadows, ray-casts, etc. detection for every single pixel in scene and since everything is mathematical calculations, it&#8217;s paralleling is almost trivial.
Parallel map functions does [...]]]></description>
			<content:encoded><![CDATA[<p>When I was coding Ray-Tracer project for my Computer Science studies in university, I ran into using <a href="http://www.haskell.org/ghc/docs/latest/html/libraries/parallel/Control-Parallel-Strategies.html">Haskell parallel map function</a> (map calls function for all list elements). <a href="http://en.wikipedia.org/wiki/Ray_tracing_(graphics)">Ray-Tracer</a> runs reflections, shadows, ray-casts, etc. detection for every single pixel in scene and since everything is mathematical calculations, it&#8217;s paralleling is almost trivial.</p>
<p>Parallel map functions does all work for you &#8211; you don&#8217;t even need to know when it&#8217;s right time to fork another thread. Implementing parallel computation in Haskell was very easy and it almost gave theoretical decrease in processing time by N times (where N is processor cores count). Today I will talk a little bit about possible parallelism with PHP&#8217;s internal <a href="http://uk2.php.net/manual/en/ref.posix.php">POSIX</a> functions.</p>
<p>PHP is neither a functional language, nor is made with threading support (correct me if I&#8217;m wrong). Still, it&#8217;s possible to create parallel processes by using <a href="http://uk2.php.net/manual/en/ref.pcntl.php">pcntl_*</a> functions family. Some days ago I tried just to get something working, what uses parallel processes and can possible be extended to deal with time-consuming mathematical algorithms.</p>
<p>Download my sample code <a href="http://dev.juokaz.com/examples/parallel/run.phps" target="_blank">here</a> (rename extension to <em>php</em>) and run it in terminal (it probably wont run with Apache). This code will try to compute x^3 for 0-1M integers two times, parallel runs both in one call, normal behaviour runs one after another. Output should look similar to this:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">juokaz<span style="color: #000000; font-weight: bold;">@</span>thinkpad:~<span style="color: #000000; font-weight: bold;">/</span>Desktop<span style="color: #000000; font-weight: bold;">/</span>php<span style="color: #000000; font-weight: bold;">/</span>parallel$ php run.php
We start
I am the child, pid = <span style="color: #000000;">0</span>
I am the parent, pid = <span style="color: #000000;">2004</span>
yep, finished, I have <span style="color: #000000;">2004</span> ID
yep, finished, I have <span style="color: #000000;">0</span> ID
Ran parallel <span style="color: #000000;">5.81009602547</span> seconds
yep, finished, I have <span style="color: #000000;">1</span> ID
yep, finished, I have <span style="color: #000000;">2</span> ID
Ran normal <span style="color: #000000;">9.76671719551</span> seconds</pre></div></div>

<p>As you can see, parallel computation on dual core processor ran almost 2x faster. And yes, it does use both cores simultaneously.</p>
<p>If you want to look more deeply, there is great article called <a href="http://www.van-steenbeek.net/?q=php_pcntl_fork">Thorough look at PHP&#8217;s pcntl_fork()</a>. You will quite quickly find that there are problems when using pcntl_fork, because it basically clones running process and then continues. Everything you define before forking child process will be accessible inside child &#8211; it&#8217;s not always what you really want.<a href="http://www.van-steenbeek.net/?q=php_pcntl_fork"><br />
</a></p>
<p>Parallel processes can be easily simulated by using asynchronous calls (over HTTP, for example) from one script to others, but for something more calculations-based, using pcntl_fork() can be much more practical. But I will probably still chose asyncronous calls over parallelizing because they are much more flexible.</p>
]]></content:encoded>
			<wfw:commentRss>http://dev.juokaz.com/php/parallel-processes-in-php/feed</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
	</channel>
</rss>
