<?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>Planetmarshall &#187; Silverlight</title>
	<atom:link href="http://www.planetmarshall.co.uk/tag/silverlight/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.planetmarshall.co.uk</link>
	<description>Andrew Marshall&#039;s blog</description>
	<lastBuildDate>Mon, 20 Feb 2012 06:57:25 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<item>
		<title>Drawing geodesic curves using the Bing maps Silverlight control</title>
		<link>http://www.planetmarshall.co.uk/2010/06/drawing-geodesic-curves-using-the-bing-maps-silverlight-control/</link>
		<comments>http://www.planetmarshall.co.uk/2010/06/drawing-geodesic-curves-using-the-bing-maps-silverlight-control/#comments</comments>
		<pubDate>Wed, 30 Jun 2010 14:52:05 +0000</pubDate>
		<dc:creator>Andrew</dc:creator>
				<category><![CDATA[Mathematics]]></category>
		<category><![CDATA[Software]]></category>
		<category><![CDATA[bing maps]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Silverlight]]></category>

		<guid isPermaLink="false">http://www.planetmarshall.co.uk/?p=624</guid>
		<description><![CDATA[For an upcoming post I wanted to be able to plot the shortest routes between various positions on the Earth using the Bing Maps Silverlight control. Although since I started working on the problem Bing have provided a similar feature<span class="ellipsis">&#8230;</span> <a href="http://www.planetmarshall.co.uk/2010/06/drawing-geodesic-curves-using-the-bing-maps-silverlight-control/"><div class="read-more">Read more &#8250;</div><!-- end of .read-more --></a>]]></description>
			<content:encoded><![CDATA[<p class="pm_first">For an upcoming post I wanted to be able to plot the shortest routes between various positions on the Earth using the <a title="Bing Maps Silverlight Control" href="http://msdn.microsoft.com/en-us/library/ee681884.aspx" target="_blank">Bing Maps Silverlight control</a>. Although since I started working on the problem Bing have provided a similar feature with their <a title="New Bing Map Apps: Gas Prices, Distance Calculator and Parking Finder" href="http://www.bing.com/community/blogs/maps/archive/2010/05/19/new-bing-map-apps-gas-prices-distance-calculator-and-parking-finder.aspx" target="_blank">Distance Calculator App</a>, the functions are not available for reuse via the public API. Interested developers may just want to skip the maths and just download the code.</p>
<p><a title="Source code in zip file for Silverlight 4.0" href="http://www.planetmarshall.co.uk/code/geodesic.zip">Geodesic source code for Silverlight 4.0</a></p>
<h3>Geodesics</h3>

<a href="http://www.planetmarshall.co.uk/wp-content/gallery/geodesic/geo.png" title="Geodesic showing the shortest distance between Cambridge and New York" class="shutterset_singlepic118" >
	<img class="ngg-singlepic ngg-right" src="http://www.planetmarshall.co.uk/wp-content/gallery/cache/118__150x_geo.png" alt="Geodesic " title="Geodesic " />
</a>

<p>The shortest path between two points on an arbitrary surface is called a <a title="Geodesic from MathWorld" href="http://mathworld.wolfram.com/Geodesic.html" target="_blank">Geodesic</a>, and on a sphere, it is a <a title="Great Circle from MathWorld" href="http://mathworld.wolfram.com/GreatCircle.html" target="_blank">Great Circle</a>. Modelling the surface of the earth as a perfect sphere, the shortest distance between any two locations on the surface is then described by a section of a Great Circle, ie an arc that lies on the plane that is described by the vectors between its start and end points and the Earth&#8217;s centre ( see figure 1 ).</p>
<p>With this information, one way ( and the way I have adopted ) to plot such a curve is as follows:</p>
<ol>
<li>Generate the points of the curve in two dimensions using the parametric equation of a circle.</li>
<li>Transform the plane of the 2d curve into 3D space such that it intersects the end points on the sphere, and the sphere&#8217;s centre.</li>
<li>Project the transformed points back into 2D space using the Mercator projection equations.</li>
</ol>
<p><span id="more-624"></span><br />
[silverlight: Geodesic.xap,520,384,false]</p>
<h3>A Parametric Representation of a Great Circle</h3>
<p>The <a title="Mercator Projection from MathWorld" href="http://mathworld.wolfram.com/MercatorProjection.html">Mercator projection</a> gives the 2D rectilinear coordinates (x,y) as a function of the latitude and longitude of a point on a sphere.  However, it is easier to draw the point using a typical drawing API, if we have a representation that gives each point of the curve in terms of a single parameter. To derive such a function, we observe that the parametric equation for a circle is given by</p>
<p><img src="http://www.planetmarshall.co.uk/wp-content/plugins/latex/latexrender/pictures/e5b8848371277d58a9dc8952b7cf5711.png"title="&#10;\begin{pmatrix} x \\ y \end{pmatrix} = \begin{pmatrix} r \cos t \\ r \sin t \end{pmatrix}&#10;" alt="&#10;\begin{pmatrix} x \\ y \end{pmatrix} = \begin{pmatrix} r \cos t \\ r \sin t \end{pmatrix}&#10;" style="vertical-align:top ;"/></p>
<p>Since a great circle is a rigid transformation of a circle in 3D space, it can also be represented as a function of a single parameter</p>
<p><img src="http://www.planetmarshall.co.uk/wp-content/plugins/latex/latexrender/pictures/8534bc6d8f61891522e2cab9e29a4bcd.png"title="&#10;\begin{pmatrix} x \\ y \\ z \end{pmatrix} = \mathbf{R}\cdot\begin{pmatrix} r \cos t \\ r \sin t \end{pmatrix}&#10;" alt="&#10;\begin{pmatrix} x \\ y \\ z \end{pmatrix} = \mathbf{R}\cdot\begin{pmatrix} r \cos t \\ r \sin t \end{pmatrix}&#10;" style="vertical-align:top ;"/></p>
<p>where <img src="http://www.planetmarshall.co.uk/wp-content/plugins/latex/latexrender/pictures/e1fd601dbae82a538d518550acb1af19.png"title="\mathbf{R}" alt="\mathbf{R}" style="vertical-align:top ;"/> is a 2&#215;3 matrix that transforms the plane circle to a location on a sphere. Using <a title="Spherical Coordinates from MathWorld" href="http://mathworld.wolfram.com/SphericalCoordinates.html">spherical coordinates</a>, the Mercator projection of the curve specified above is then<br />
<img src="http://www.planetmarshall.co.uk/wp-content/plugins/latex/latexrender/pictures/d28b323111eafa9b1752172618ef461d.png"title="&#10;\mathbf{C}(t) =\begin{pmatrix} \lambda \\ \tanh^{-1} \left(\sin \phi \right)\end{pmatrix} = \begin{pmatrix} \tan^{-1}\left(y/x\right)\\ \tanh^{-1}z \end{pmatrix}&#10;" alt="&#10;\mathbf{C}(t) =\begin{pmatrix} \lambda \\ \tanh^{-1} \left(\sin \phi \right)\end{pmatrix} = \begin{pmatrix} \tan^{-1}\left(y/x\right)\\ \tanh^{-1}z \end{pmatrix}&#10;" style="vertical-align:top ;"/><br />
Where <img src="http://www.planetmarshall.co.uk/wp-content/plugins/latex/latexrender/pictures/c6a6eb61fd9c6c913da73b3642ca147d.png"title="\lambda" alt="\lambda" style="vertical-align:top ;"/> and <img src="http://www.planetmarshall.co.uk/wp-content/plugins/latex/latexrender/pictures/1ed346930917426bc46d41e22cc525ec.png"title="\phi" alt="\phi" style="vertical-align:top ;"/> are the longitude and latitude of the point to be projected, respectively. Writing the equation out in full gives,</p>
<p><img src="http://www.planetmarshall.co.uk/wp-content/plugins/latex/latexrender/pictures/d7fa48691fe4625fecde98aac52f7127.png"title="&#10;\mathbf{C}(t) = \begin{pmatrix}&#10;\tan^{-1}\left(\frac{R_{2,1}\cos t + R_{2,2}\sin t}{R_{1,1}\cos t + R_{1,2}\sin t}\right)\\&#10;\tanh^{-1}(R_{3,1}\cos t +R_{3,2}\sin t)&#10;\end{pmatrix}&#10;" alt="&#10;\mathbf{C}(t) = \begin{pmatrix}&#10;\tan^{-1}\left(\frac{R_{2,1}\cos t + R_{2,2}\sin t}{R_{1,1}\cos t + R_{1,2}\sin t}\right)\\&#10;\tanh^{-1}(R_{3,1}\cos t +R_{3,2}\sin t)&#10;\end{pmatrix}&#10;" style="vertical-align:top ;"/></p>
<p>Now that we have a suitable parametric equation, we can draw the geodesic with a series of connected line segments by varying the parameter, t.</p>
<blockquote><p>Mathematical note &#8211; the parameterization given by this expression is highly non-uniform, meaning that there are many more points generated in some parts of the curve than in others. The mathematics of generating uniform ( or natural ) parameterizations belongs to the field of differential curve geometry and is beyond the scope of this article ( and my brain ).</p></blockquote>
<h3>Implementation notes</h3>
<p>
<a href="http://www.planetmarshall.co.uk/wp-content/gallery/geodesic/maps.jpg" title="The upper image shows what happens when aa curve wraps around the map boundary. The lower image shows the effect of splitting the curve at the boundary." class="shutterset_singlepic141" >
	<img class="ngg-singlepic ngg-left" src="http://www.planetmarshall.co.uk/index.php?callback=image&amp;pid=141&amp;width=150&amp;height=&amp;mode=" alt="Geodesic with Split" title="Geodesic with Split" />
</a>
 Inevitably, the mathematics alone is not sufficient to produce an implementation of a reuseable class for the Bing Silverlight control. There are two main issues to resolve; firstly, it is not immediately obvious how to derive from the provided <a title="Documentation on MapShapeBase from MSDN" href="http://msdn.microsoft.com/en-us/library/microsoft.maps.mapcontrol.core.mapshapebase.aspx" target="_blank"><code>MapShapeBase</code></a> class to create new shape overlays and secondly, how to handle drawing the curves when they &#8216;wrap&#8217; beyond the map&#8217;s viewable area ( this is easier to illustrate than to describe &#8211; see the figure opposite ).</p>
<h4>Inheriting from <code>MapShapeBase</code></h4>
<p>I must confess that I cheated slightly in implementing the <code>MapGeodesicPath</code> class, in that I used <a title="Reflector from Red-Gate. Stop sending them hate mail!" href="http://www.red-gate.com/products/reflector/" target="_blank">Reflector</a> to peer into the implementation of the base class. The existing derivations of this class simply defer to <code>MapShapeBase</code> for most of the work, which they can do since for a <code>MapPolygon</code> and <code>MapPolyline</code> there is a one-to-one relationship between <code>Locations</code>, latitude and longitude points on the map and <code>Points</code>, the actual 2D cartesian coordinates used to draw the shape. For the Geodesic, this is not the case, because we only want to specify the start and ending points of the curve, not every point in between. A solution is to delegate the point generation code to a secondary class, one that can be independently tested.</p>
<h4>Splitting the curves at the map boundary</h4>
<p>When the curves wrap around the map projection, they need to be split at the boundary. This is done by finding the parameter t for the longitude value of the boundary. Where the longitude value is +/- 180, this is straightforward as the equation above reduces to,<br />
<img src="http://www.planetmarshall.co.uk/wp-content/plugins/latex/latexrender/pictures/ea9d5ccee06c0b25676cfd84e84b33be.png"title="&#10;t = -\tan^{-1}\frac{R_{1,2}}{R_{2,2}}&#10;" alt="&#10;t = -\tan^{-1}\frac{R_{1,2}}{R_{2,2}}&#10;" style="vertical-align:top ;"/><br />
For other longitude values, we simply offset the longitude values by the required amount, and calculate the value of t for the new matrix.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.planetmarshall.co.uk/2010/06/drawing-geodesic-curves-using-the-bing-maps-silverlight-control/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Winter Camping</title>
		<link>http://www.planetmarshall.co.uk/2010/01/winter-camping/</link>
		<comments>http://www.planetmarshall.co.uk/2010/01/winter-camping/#comments</comments>
		<pubDate>Thu, 21 Jan 2010 00:53:58 +0000</pubDate>
		<dc:creator>Andrew</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[bing maps]]></category>
		<category><![CDATA[Hiking]]></category>
		<category><![CDATA[Silverlight]]></category>
		<category><![CDATA[Swimming]]></category>

		<guid isPermaLink="false">http://www.planetmarshall.co.uk/?p=513</guid>
		<description><![CDATA[Last week myself and a group of friends took a winter camping trip up to the Lake District. An odd decision, it could be argued, seen as the region has recently seen both some of its worst flooding and coldest<span class="ellipsis">&#8230;</span> <a href="http://www.planetmarshall.co.uk/2010/01/winter-camping/"><div class="read-more">Read more &#8250;</div><!-- end of .read-more --></a>]]></description>
			<content:encoded><![CDATA[
<a href="http://www.planetmarshall.co.uk/wp-content/gallery/winter_camp/IMG_0310.JPG" title="Figured out the self timer" class="shutterset_singlepic102" >
	<img class="ngg-singlepic ngg-left" src="http://www.planetmarshall.co.uk/index.php?callback=image&amp;pid=102&amp;width=120&amp;height=&amp;mode=" alt="Figured out the self timer" title="Figured out the self timer" />
</a>

<p class="pm_first">Last week myself and a group of friends took a winter camping trip up to the <a title="The Lake District National Park" href="http://www.lakedistrict.gov.uk" target="_blank">Lake District</a>. An odd decision, it could be argued, seen as the region has recently seen both some of its worst flooding and coldest winters in recent years.  Nonethless, equipped with ice axe, crampons and a generous supply of Kendal Mint Cake we set out to climb Scafell.</p>
<p><span id="more-513"></span></p>
<h3 class="pm_first">Some of the things I learned</h3>
<ol>
<a href="http://www.planetmarshall.co.uk/wp-content/gallery/winter_camp/IMG_0299.JPG" title="Not the north face of the Eiger" class="shutterset_singlepic98" >
	<img class="ngg-singlepic ngg-right" src="http://www.planetmarshall.co.uk/index.php?callback=image&amp;pid=98&amp;width=120&amp;height=&amp;mode=" alt="" title="" />
</a>
</p>
<li>Elephants, though clearly signposted, are not common in the lake district. I didn&#8217;t see a single one all weekend. Sheep, however, are plentiful.</li>
<li>It may be effective as a deterrent against the <a title="Ravenous Bugblatter Beast of Traal" href="http://www.bbc.co.uk/dna/h2g2/A387029" target="_blank">Ravenous Bugblatter Beast of Traal</a>, but a towel is no replacement for an insulating mattress.</li>
<li>Pop up tents, though useful on the beach, are less effective in torrential rain and gale force winds.</li>
<li>Retracing your steps back through the snow sounds easy, and it probably is for other people.</li>
<li>The <a title="The Wasdale Head Inn" href="http://www.wasdaleheadinn.co.uk/home.html">Wasdale Head Inn</a> serves an excellent selection of whiskeys, of which <a title="Wikipedia's entry on Lagavulin." href="http://en.wikipedia.org/wiki/Lagavulin_Single_Malt" target="_blank">Lagavulin </a>is the best. I should know, I tried all of them.</li>
<li>Swimming in Wastwater in January is almost exactly as cold as you&#8217;d expect.</li>
</ol>
<h3 class="pm_first">The Route</h3>
<p>The map below, which requires <a title="Microsoft Silverlight" href="http://www.microsoft.com/SILVERLIGHT/" target="_blank">Microsoft Silverlight</a>, was exported from my <a title="Garmin Forerunner 305" href="https://buy.garmin.com/shop/shop.do?cID=142&amp;pID=349" target="_blank">GPS wristwatch</a> and shows the route we took up Scafell before visibility made further progress a bit risky, and so we retraced our steps back through the snow. Or at least tried to before our steps mysteriously disappeared. There is also a <a title="Photosynth of Scafell" href="http://photosynth.net/view.aspx?cid=4b895bbc-3e41-4957-a35f-b3fab7155bfc" target="_blank">Photosynth </a>from part of our route available.</p>
<p>[silverlight: osmap.xap,520,350,show]</p>
<h3>Swimming in Wastwater</h3>
<p>Although as an amateur triathlete I&#8217;ve done many open water swims, I&#8217;ve never done one in winter and never without a wetsuit. So, in an attempt to kill both birds with one stone I decided to go for a swim in Wastwater in January. I lasted about two minutes. I&#8217;m no <a title="Ice Swimmer Lewis Gordon Pugh" href="http://www.lewispugh.com/" target="_blank">Lewis Gordon Pugh</a>. It was cold, but it really wasn&#8217;t as bad as you might think.  The water was beautifully clear but unfortunately sharp rocks do not a beach make.</p>
<h3>Gallery</h3>

<div class="ngg-galleryoverview" id="ngg-gallery-9-513">

	<!-- Slideshow link -->
	<div class="slideshowlink">
		<a class="slideshowlink" href="http://www.planetmarshall.co.uk/2010/01/winter-camping/?show=slide">
			[Show as slideshow]		</a>
	</div>

	
	<!-- Thumbnails -->
		
	<div id="ngg-image-83" class="ngg-gallery-thumbnail-box"  >
		<div class="ngg-gallery-thumbnail" >
			<a href="http://www.planetmarshall.co.uk/wp-content/gallery/winter_camp/IMG_0247.JPG" title="Dave and Lizzy on the way to the campsite
" class="shutterset_set_9" >
								<img title="Dave and Lizzy on the way to the campsite" alt="Dave and Lizzy on the way to the campsite" src="http://www.planetmarshall.co.uk/wp-content/gallery/winter_camp/thumbs/thumbs_IMG_0247.JPG" width="64" height="48" />
							</a>
		</div>
	</div>
	
		
 		
	<div id="ngg-image-84" class="ngg-gallery-thumbnail-box"  >
		<div class="ngg-gallery-thumbnail" >
			<a href="http://www.planetmarshall.co.uk/wp-content/gallery/winter_camp/IMG_0248.JPG" title="Jamie by Wastwater" class="shutterset_set_9" >
								<img title="Jamie by Wastwater" alt="Jamie by Wastwater" src="http://www.planetmarshall.co.uk/wp-content/gallery/winter_camp/thumbs/thumbs_IMG_0248.JPG" width="64" height="48" />
							</a>
		</div>
	</div>
	
		
 		
	<div id="ngg-image-85" class="ngg-gallery-thumbnail-box"  >
		<div class="ngg-gallery-thumbnail" >
			<a href="http://www.planetmarshall.co.uk/wp-content/gallery/winter_camp/IMG_0253.JPG" title="View from the campsite" class="shutterset_set_9" >
								<img title="View from the campsite" alt="View from the campsite" src="http://www.planetmarshall.co.uk/wp-content/gallery/winter_camp/thumbs/thumbs_IMG_0253.JPG" width="64" height="48" />
							</a>
		</div>
	</div>
	
		
 		
	<div id="ngg-image-109" class="ngg-gallery-thumbnail-box"  >
		<div class="ngg-gallery-thumbnail" >
			<a href="http://www.planetmarshall.co.uk/wp-content/gallery/winter_camp/IMG_0333.JPG" title="The campsite" class="shutterset_set_9" >
								<img title="The campsite" alt="The campsite" src="http://www.planetmarshall.co.uk/wp-content/gallery/winter_camp/thumbs/thumbs_IMG_0333.JPG" width="64" height="48" />
							</a>
		</div>
	</div>
	
		
 		
	<div id="ngg-image-86" class="ngg-gallery-thumbnail-box"  >
		<div class="ngg-gallery-thumbnail" >
			<a href="http://www.planetmarshall.co.uk/wp-content/gallery/winter_camp/IMG_0260.JPG" title="The Wasdale Head Inn" class="shutterset_set_9" >
								<img title="The Wasdale Head Inn" alt="The Wasdale Head Inn" src="http://www.planetmarshall.co.uk/wp-content/gallery/winter_camp/thumbs/thumbs_IMG_0260.JPG" width="64" height="48" />
							</a>
		</div>
	</div>
	
		
 		
	<div id="ngg-image-87" class="ngg-gallery-thumbnail-box"  >
		<div class="ngg-gallery-thumbnail" >
			<a href="http://www.planetmarshall.co.uk/wp-content/gallery/winter_camp/IMG_0261.JPG" title="Before the morning's walk" class="shutterset_set_9" >
								<img title="Before the morning's walk" alt="Before the morning's walk" src="http://www.planetmarshall.co.uk/wp-content/gallery/winter_camp/thumbs/thumbs_IMG_0261.JPG" width="64" height="48" />
							</a>
		</div>
	</div>
	
		
 		
	<div id="ngg-image-88" class="ngg-gallery-thumbnail-box"  >
		<div class="ngg-gallery-thumbnail" >
			<a href="http://www.planetmarshall.co.uk/wp-content/gallery/winter_camp/IMG_0264.JPG" title="Some water" class="shutterset_set_9" >
								<img title="Some water" alt="Some water" src="http://www.planetmarshall.co.uk/wp-content/gallery/winter_camp/thumbs/thumbs_IMG_0264.JPG" width="64" height="48" />
							</a>
		</div>
	</div>
	
		
 		
	<div id="ngg-image-89" class="ngg-gallery-thumbnail-box"  >
		<div class="ngg-gallery-thumbnail" >
			<a href="http://www.planetmarshall.co.uk/wp-content/gallery/winter_camp/IMG_0271.JPG" title="On the way up Scafell..." class="shutterset_set_9" >
								<img title="On the way up Scafell..." alt="On the way up Scafell..." src="http://www.planetmarshall.co.uk/wp-content/gallery/winter_camp/thumbs/thumbs_IMG_0271.JPG" width="64" height="48" />
							</a>
		</div>
	</div>
	
		
 		
	<div id="ngg-image-90" class="ngg-gallery-thumbnail-box"  >
		<div class="ngg-gallery-thumbnail" >
			<a href="http://www.planetmarshall.co.uk/wp-content/gallery/winter_camp/IMG_0274.JPG" title="...still on the way up Scafell" class="shutterset_set_9" >
								<img title="...still on the way up Scafell" alt="...still on the way up Scafell" src="http://www.planetmarshall.co.uk/wp-content/gallery/winter_camp/thumbs/thumbs_IMG_0274.JPG" width="64" height="48" />
							</a>
		</div>
	</div>
	
		
 		
	<div id="ngg-image-91" class="ngg-gallery-thumbnail-box"  >
		<div class="ngg-gallery-thumbnail" >
			<a href="http://www.planetmarshall.co.uk/wp-content/gallery/winter_camp/IMG_0275.JPG" title="A look back over Wastwater" class="shutterset_set_9" >
								<img title="A look back over Wastwater" alt="A look back over Wastwater" src="http://www.planetmarshall.co.uk/wp-content/gallery/winter_camp/thumbs/thumbs_IMG_0275.JPG" width="64" height="48" />
							</a>
		</div>
	</div>
	
		
 		
	<div id="ngg-image-92" class="ngg-gallery-thumbnail-box"  >
		<div class="ngg-gallery-thumbnail" >
			<a href="http://www.planetmarshall.co.uk/wp-content/gallery/winter_camp/IMG_0278.JPG" title="First glimpses of snow up Scafell" class="shutterset_set_9" >
								<img title="First glimpses of snow up Scafell" alt="First glimpses of snow up Scafell" src="http://www.planetmarshall.co.uk/wp-content/gallery/winter_camp/thumbs/thumbs_IMG_0278.JPG" width="64" height="48" />
							</a>
		</div>
	</div>
	
		
 		
	<div id="ngg-image-93" class="ngg-gallery-thumbnail-box"  >
		<div class="ngg-gallery-thumbnail" >
			<a href="http://www.planetmarshall.co.uk/wp-content/gallery/winter_camp/IMG_0288.JPG" title="Starting to get serious" class="shutterset_set_9" >
								<img title="Starting to get serious" alt="Starting to get serious" src="http://www.planetmarshall.co.uk/wp-content/gallery/winter_camp/thumbs/thumbs_IMG_0288.JPG" width="64" height="48" />
							</a>
		</div>
	</div>
	
		
 		
	<div id="ngg-image-94" class="ngg-gallery-thumbnail-box"  >
		<div class="ngg-gallery-thumbnail" >
			<a href="http://www.planetmarshall.co.uk/wp-content/gallery/winter_camp/IMG_0293.JPG" title="Touching the void" class="shutterset_set_9" >
								<img title="Touching the void" alt="Touching the void" src="http://www.planetmarshall.co.uk/wp-content/gallery/winter_camp/thumbs/thumbs_IMG_0293.JPG" width="64" height="48" />
							</a>
		</div>
	</div>
	
		
 		
	<div id="ngg-image-95" class="ngg-gallery-thumbnail-box"  >
		<div class="ngg-gallery-thumbnail" >
			<a href="http://www.planetmarshall.co.uk/wp-content/gallery/winter_camp/IMG_0294.JPG" title=" " class="shutterset_set_9" >
								<img title=" " alt=" " src="http://www.planetmarshall.co.uk/wp-content/gallery/winter_camp/thumbs/thumbs_IMG_0294.JPG" width="64" height="48" />
							</a>
		</div>
	</div>
	
		
 		
	<div id="ngg-image-96" class="ngg-gallery-thumbnail-box"  >
		<div class="ngg-gallery-thumbnail" >
			<a href="http://www.planetmarshall.co.uk/wp-content/gallery/winter_camp/IMG_0295.JPG" title=" " class="shutterset_set_9" >
								<img title=" " alt=" " src="http://www.planetmarshall.co.uk/wp-content/gallery/winter_camp/thumbs/thumbs_IMG_0295.JPG" width="64" height="48" />
							</a>
		</div>
	</div>
	
		
 		
	<div id="ngg-image-97" class="ngg-gallery-thumbnail-box"  >
		<div class="ngg-gallery-thumbnail" >
			<a href="http://www.planetmarshall.co.uk/wp-content/gallery/winter_camp/IMG_0298.JPG" title="England, believe it or not." class="shutterset_set_9" >
								<img title="England, believe it or not." alt="England, believe it or not." src="http://www.planetmarshall.co.uk/wp-content/gallery/winter_camp/thumbs/thumbs_IMG_0298.JPG" width="64" height="48" />
							</a>
		</div>
	</div>
	
		
 		
	<div id="ngg-image-98" class="ngg-gallery-thumbnail-box"  >
		<div class="ngg-gallery-thumbnail" >
			<a href="http://www.planetmarshall.co.uk/wp-content/gallery/winter_camp/IMG_0299.JPG" title="Not the north face of the Eiger" class="shutterset_set_9" >
								<img title=" " alt=" " src="http://www.planetmarshall.co.uk/wp-content/gallery/winter_camp/thumbs/thumbs_IMG_0299.JPG" width="64" height="48" />
							</a>
		</div>
	</div>
	
		
 		
	<div id="ngg-image-99" class="ngg-gallery-thumbnail-box"  >
		<div class="ngg-gallery-thumbnail" >
			<a href="http://www.planetmarshall.co.uk/wp-content/gallery/winter_camp/IMG_0300.JPG" title=" " class="shutterset_set_9" >
								<img title=" " alt=" " src="http://www.planetmarshall.co.uk/wp-content/gallery/winter_camp/thumbs/thumbs_IMG_0300.JPG" width="64" height="48" />
							</a>
		</div>
	</div>
	
		
 		
	<div id="ngg-image-100" class="ngg-gallery-thumbnail-box"  >
		<div class="ngg-gallery-thumbnail" >
			<a href="http://www.planetmarshall.co.uk/wp-content/gallery/winter_camp/IMG_0303.JPG" title=" " class="shutterset_set_9" >
								<img title=" " alt=" " src="http://www.planetmarshall.co.uk/wp-content/gallery/winter_camp/thumbs/thumbs_IMG_0303.JPG" width="64" height="48" />
							</a>
		</div>
	</div>
	
		
 		
	<div id="ngg-image-101" class="ngg-gallery-thumbnail-box"  >
		<div class="ngg-gallery-thumbnail" >
			<a href="http://www.planetmarshall.co.uk/wp-content/gallery/winter_camp/IMG_0305.JPG" title="Getting to grips with the self timer." class="shutterset_set_9" >
								<img title="Getting to grips with the self timer." alt="Getting to grips with the self timer." src="http://www.planetmarshall.co.uk/wp-content/gallery/winter_camp/thumbs/thumbs_IMG_0305.JPG" width="64" height="48" />
							</a>
		</div>
	</div>
	
		
 		
	<div id="ngg-image-102" class="ngg-gallery-thumbnail-box"  >
		<div class="ngg-gallery-thumbnail" >
			<a href="http://www.planetmarshall.co.uk/wp-content/gallery/winter_camp/IMG_0310.JPG" title="Figured out the self timer" class="shutterset_set_9" >
								<img title="Figured out the self timer" alt="Figured out the self timer" src="http://www.planetmarshall.co.uk/wp-content/gallery/winter_camp/thumbs/thumbs_IMG_0310.JPG" width="64" height="48" />
							</a>
		</div>
	</div>
	
		
 		
	<div id="ngg-image-103" class="ngg-gallery-thumbnail-box"  >
		<div class="ngg-gallery-thumbnail" >
			<a href="http://www.planetmarshall.co.uk/wp-content/gallery/winter_camp/IMG_0314.JPG" title="Stopping, briefly, for lunch." class="shutterset_set_9" >
								<img title="Stopping, briefly, for lunch." alt="Stopping, briefly, for lunch." src="http://www.planetmarshall.co.uk/wp-content/gallery/winter_camp/thumbs/thumbs_IMG_0314.JPG" width="64" height="48" />
							</a>
		</div>
	</div>
	
		
 		
	<div id="ngg-image-104" class="ngg-gallery-thumbnail-box"  >
		<div class="ngg-gallery-thumbnail" >
			<a href="http://www.planetmarshall.co.uk/wp-content/gallery/winter_camp/IMG_0316.JPG" title="Axe and cake" class="shutterset_set_9" >
								<img title="Axe and cake" alt="Axe and cake" src="http://www.planetmarshall.co.uk/wp-content/gallery/winter_camp/thumbs/thumbs_IMG_0316.JPG" width="64" height="48" />
							</a>
		</div>
	</div>
	
		
 		
	<div id="ngg-image-106" class="ngg-gallery-thumbnail-box"  >
		<div class="ngg-gallery-thumbnail" >
			<a href="http://www.planetmarshall.co.uk/wp-content/gallery/winter_camp/IMG_0322.JPG" title="On the way down." class="shutterset_set_9" >
								<img title="On the way down." alt="On the way down." src="http://www.planetmarshall.co.uk/wp-content/gallery/winter_camp/thumbs/thumbs_IMG_0322.JPG" width="64" height="48" />
							</a>
		</div>
	</div>
	
		
 		
	<div id="ngg-image-105" class="ngg-gallery-thumbnail-box"  >
		<div class="ngg-gallery-thumbnail" >
			<a href="http://www.planetmarshall.co.uk/wp-content/gallery/winter_camp/IMG_0318.JPG" title="Andy looking down over Wasdale" class="shutterset_set_9" >
								<img title="Andy looking down over Wasdale" alt="Andy looking down over Wasdale" src="http://www.planetmarshall.co.uk/wp-content/gallery/winter_camp/thumbs/thumbs_IMG_0318.JPG" width="64" height="48" />
							</a>
		</div>
	</div>
	
		
 		
	<div id="ngg-image-115" class="ngg-gallery-thumbnail-box"  >
		<div class="ngg-gallery-thumbnail" >
			<a href="http://www.planetmarshall.co.uk/wp-content/gallery/winter_camp/img_0320.jpg" title="Dave surveys his domain" class="shutterset_set_9" >
								<img title="Dave surveys his domain" alt="Dave surveys his domain" src="http://www.planetmarshall.co.uk/wp-content/gallery/winter_camp/thumbs/thumbs_img_0320.jpg" width="64" height="48" />
							</a>
		</div>
	</div>
	
		
 		
	<div id="ngg-image-107" class="ngg-gallery-thumbnail-box"  >
		<div class="ngg-gallery-thumbnail" >
			<a href="http://www.planetmarshall.co.uk/wp-content/gallery/winter_camp/IMG_0327.JPG" title="A night heron." class="shutterset_set_9" >
								<img title="A night heron." alt="A night heron." src="http://www.planetmarshall.co.uk/wp-content/gallery/winter_camp/thumbs/thumbs_IMG_0327.JPG" width="64" height="48" />
							</a>
		</div>
	</div>
	
		
 		
	<div id="ngg-image-108" class="ngg-gallery-thumbnail-box"  >
		<div class="ngg-gallery-thumbnail" >
			<a href="http://www.planetmarshall.co.uk/wp-content/gallery/winter_camp/IMG_0329.JPG" title="Lakes wildlife" class="shutterset_set_9" >
								<img title="Lakes wildlife" alt="Lakes wildlife" src="http://www.planetmarshall.co.uk/wp-content/gallery/winter_camp/thumbs/thumbs_IMG_0329.JPG" width="64" height="48" />
							</a>
		</div>
	</div>
	
		
 		
	<div id="ngg-image-111" class="ngg-gallery-thumbnail-box"  >
		<div class="ngg-gallery-thumbnail" >
			<a href="http://www.planetmarshall.co.uk/wp-content/gallery/winter_camp/IMG_0342.JPG" title="Wastwater" class="shutterset_set_9" >
								<img title="Wastwater" alt="Wastwater" src="http://www.planetmarshall.co.uk/wp-content/gallery/winter_camp/thumbs/thumbs_IMG_0342.JPG" width="64" height="48" />
							</a>
		</div>
	</div>
	
		
 		
	<div id="ngg-image-113" class="ngg-gallery-thumbnail-box"  >
		<div class="ngg-gallery-thumbnail" >
			<a href="http://www.planetmarshall.co.uk/wp-content/gallery/winter_camp/IMG_0345.JPG" title="Walking by Wastwater." class="shutterset_set_9" >
								<img title="Walking by Wastwater." alt="Walking by Wastwater." src="http://www.planetmarshall.co.uk/wp-content/gallery/winter_camp/thumbs/thumbs_IMG_0345.JPG" width="64" height="48" />
							</a>
		</div>
	</div>
	
		
 		
	<div id="ngg-image-79" class="ngg-gallery-thumbnail-box"  >
		<div class="ngg-gallery-thumbnail" >
			<a href="http://www.planetmarshall.co.uk/wp-content/gallery/winter_camp/DSC_0278.JPG" title="Preparing for a swim in Wastwater" class="shutterset_set_9" >
								<img title="Preparing for a swim in Wastwater" alt="Preparing for a swim in Wastwater" src="http://www.planetmarshall.co.uk/wp-content/gallery/winter_camp/thumbs/thumbs_DSC_0278.JPG" width="64" height="42" />
							</a>
		</div>
	</div>
	
		
 		
	<div id="ngg-image-80" class="ngg-gallery-thumbnail-box"  >
		<div class="ngg-gallery-thumbnail" >
			<a href="http://www.planetmarshall.co.uk/wp-content/gallery/winter_camp/DSC_0279_1.JPG" title="Wastwater in winter" class="shutterset_set_9" >
								<img title="Wastwater in winter" alt="Wastwater in winter" src="http://www.planetmarshall.co.uk/wp-content/gallery/winter_camp/thumbs/thumbs_DSC_0279_1.JPG" width="64" height="52" />
							</a>
		</div>
	</div>
	
		
 		
	<div id="ngg-image-81" class="ngg-gallery-thumbnail-box"  >
		<div class="ngg-gallery-thumbnail" >
			<a href="http://www.planetmarshall.co.uk/wp-content/gallery/winter_camp/DSC_0283.JPG" title="Wading in Wastwater" class="shutterset_set_9" >
								<img title="Wading in Wastwater" alt="Wading in Wastwater" src="http://www.planetmarshall.co.uk/wp-content/gallery/winter_camp/thumbs/thumbs_DSC_0283.JPG" width="64" height="42" />
							</a>
		</div>
	</div>
	
		
 		
	<div id="ngg-image-82" class="ngg-gallery-thumbnail-box"  >
		<div class="ngg-gallery-thumbnail" >
			<a href="http://www.planetmarshall.co.uk/wp-content/gallery/winter_camp/DSC_0284.JPG" title="Swimming in Wastwater" class="shutterset_set_9" >
								<img title="Swimming in Wastwater" alt="Swimming in Wastwater" src="http://www.planetmarshall.co.uk/wp-content/gallery/winter_camp/thumbs/thumbs_DSC_0284.JPG" width="64" height="42" />
							</a>
		</div>
	</div>
	
		
 	 	
	<!-- Pagination -->
 	<div class="ngg-clear"></div> 	
</div>


]]></content:encoded>
			<wfw:commentRss>http://www.planetmarshall.co.uk/2010/01/winter-camping/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Silverlight and CUDA interop</title>
		<link>http://www.planetmarshall.co.uk/2010/01/silverlight-and-cuda-interop/</link>
		<comments>http://www.planetmarshall.co.uk/2010/01/silverlight-and-cuda-interop/#comments</comments>
		<pubDate>Fri, 15 Jan 2010 02:11:58 +0000</pubDate>
		<dc:creator>Andrew</dc:creator>
				<category><![CDATA[Software]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[CUDA]]></category>
		<category><![CDATA[Silverlight]]></category>

		<guid isPermaLink="false">http://www.planetmarshall.co.uk/?p=389</guid>
		<description><![CDATA[Update &#8211; source code now available Microsoft have recently released a beta of Silverlight 4, which has limited support for native interoperation using COM. Potentially, this example could be applied to any number of native interop scenarios, however for this<span class="ellipsis">&#8230;</span> <a href="http://www.planetmarshall.co.uk/2010/01/silverlight-and-cuda-interop/"><div class="read-more">Read more &#8250;</div><!-- end of .read-more --></a>]]></description>
			<content:encoded><![CDATA[<p>
<a href="http://www.planetmarshall.co.uk/wp-content/gallery/cuda-interop/mandrill.jpg" title="" class="shutterset_singlepic78" >
	<img class="ngg-singlepic ngg-right" src="http://www.planetmarshall.co.uk/wp-content/gallery/cache/78__x96_mandrill.jpg" alt="mandrill" title="mandrill" />
</a>
<br />
<em>Update &#8211; <a href="#source">source code</a> now available</em></p>
<p class="pm_first">Microsoft have recently released a beta of Silverlight 4, which has limited support for native interoperation using COM. Potentially, this example could be applied to any number of native interop scenarios, however for this example I have chosen to use Nvidia&#8217;s CUDA technology.</p>
<blockquote><p>Disclaimer : This is an example of what can be done, not necessarily, and in all likelihood, an example of how it should be done.</p></blockquote>
<h3>About CUDA</h3>
<p>Up until around 2001 PC graphics cards, though powerful, implemented a fixed function pipeline that limited use to whatever was exposed by the APIs, usually Direct3D or OpenGL. The addition of a programmable pixel pipeline led to the use of graphics cards for more general computation tasks; at first using shaders directly, followed by higher level GPU specific programming languages, such as Brook, SH, and later NVidia&#8217;s CUDA. Most of this work was, and is, documented by the <a title="GPGPU" href="http://gpgpu.org/" target="_blank">GPGPU</a> group. <a href="http://www.nvidia.com/object/cuda_home.html#" target="_blank">NVIDIA&#8217;s website</a> shows CUDA being used in a wide variety of applications but in practice it is best employed in so called &#8220;<a title="Wikipedia : Embarrassingly Parallel" href="http://en.wikipedia.org/wiki/Embarrassingly_parallel" target="_blank">embarassingly parallel</a>&#8221; problems.<br />
<span id="more-389"></span></p>
<h3>The demonstration application</h3>
<p>The demo below shows a Silverlight 4 beta application, which implements a recursive gaussian filter. Note that this is not the same algorithm provided by the sample in the CUDA SDK, but a more efficient method, which is described in detail in <a href="#young">[1]</a> for those interested. The main advantage of a filter implemented in this way is that the computation time is independent of the width of the filter.<br />
To enable CUDA interop, you&#8217;ll need a CUDA compatible graphics card. Then do the following,</p>
<ol>
<li>Install the MFC COM application (link below). The installer should register the application with COM automatically.</li>
<li>Right click on the Silverlight App and install it for running outside of the browser. The CUDA option should now be available from the Combo box.</li>
</ol>
<p>Source code : <a title="Download source code" name="source" href="http://planetmarshall.co.uk/silverlight/cuda_interop/SilverlightCudaInteropDemo.zip">SilverlightCudaInteropDemo.zip</a><br />
<a title="Install CUDA Server application" href="http://planetmarshall.co.uk/silverlight/cuda_interop/CudaServer.msi">Install MFC COM Application (5.5 Mb)</a><br />
[silverlight: cuda_interop/SlCudaInteropDemo.xap,520,580,false]</p>
<h3>The native component</h3>
<p>The native component takes the form of a COM Automation server, implemented as a client side MFC application.</p>
<blockquote><p>Note: Make sure you run Visual Studio with Administrator privileges, otherwise registering the automation server with COM will fail.</p></blockquote>
<p>MFC and Automation are beyond the scope of this article, but the basic process I followed was thus</p>
<ol>
<li>Create an MFC Dialog application using the Wizard. Make sure to enable Automation support</li>
<li>Add a method to the autmation interface using the add Method wizard from the Class View</li>
<li>Add a dual interface using this <a title="TN065: Dual-Interface Support for OLE Automation Servers" href="http://msdn.microsoft.com/en-us/library/4h56szat%28VS.100%29.aspx" target="_blank">Technical Note</a> from MSDN.</li>
<li>If you get link errors, make sure to include the output of MIDL in the application class ( the one that contains OnInitInstance). I couldn&#8217;t find any reference to this step, but it&#8217;s how the samples work.</li>
<li>Make sure that the run time library options passed to nvcc and msvc match, ie they should all use a DLL or Static linking, not a mixture of both</li>
<li>If you get stuck, take a look at the <a title="MFC Samples" href="http://msdn.microsoft.com/en-us/library/482ck6x8%28VS.100%29.aspx" target="_blank">MFC Samples</a>, particularly <a title="ACDUAL Sample: Adds Dual Interfaces to an Automation Application" href="http://msdn.microsoft.com/en-us/library/xfx55tf8%28VS.100%29.aspx" target="_blank">acdual</a>.</li>
</ol>
<p>when you pass a native array through COM Automation, it is converted to a <a title="Array Manipulation Functions from MSDN" href="http://msdn.microsoft.com/en-us/library/ms221145%28VS.100%29.aspx" target="_blank"><code>SAFEARRAY</code></a> on the native side. Note that I couldn&#8217;t find any documentation on this, I discovered it through experience. The code snippets below show sending and receiving array data between Silverlight and the MFC application.</p>
<pre title="Calling COM from Silverlight" lang="csharp">
// note that ComAutomationFactory has become AutomationFactory
// in Silverlight 4 RC
dynamic cuda = AutomationFactory.CreateObject("CudaServer.Application");
float[] data = new [] {1.0f, 3.14f };
dynamic retData = cuda.Process( data );
// retData is a managed float array</pre>
<pre title="Returning data to Silverlight from MFC via COM" lang="cpp">VARIANT CCudaServer::Process(VARIANT &amp;data)
{
  SAFEARRAY *pSrcData =  data.parray;

  // this will copy the safe array into the variant
  CComVariant var(pSrcData);

  // when we return the VARIANT containing the SAFEARRAY
  // it will be marshaled to Silverlight as a managed array
  VARIANT retVal;
  VariantInit( &amp;retVal );
  var.Detach( &amp;retVal );
  retVal.vt = VT_ARRAY | VT_R4;
  return retVal;
}</pre>
<h3>Using MEF to implement the application</h3>
<p>The <a title="Managed Extensibility Framework at Codeplex" href="http://www.codeplex.com/MEF" target="_blank">Managed Extensibility Framework</a> is an extensible plugin framework for .NET applications and Silverlight. I have used it to dynamically discover implementations of <code>IProcessorProvider</code> based on the permissions available to the Silverlight application. The figure below shows the component structure of the application.</p>

<a href="http://www.planetmarshall.co.uk/wp-content/gallery/cuda-interop/slcuda_component.png" title="Component diagram for demo application" class="shutterset_singlepic77" >
	<img class="ngg-singlepic" src="http://www.planetmarshall.co.uk/wp-content/gallery/cache/77__475x_slcuda_component.png" alt="slcuda_component" title="slcuda_component" />
</a>

<h3>Performance notes</h3>
<h4>Silverlight</h4>
<p>Unlike the <a title="My Reaction-Diffusion simulator" href="http://www.planetmarshall.co.uk/index.php/2009/03/reaction-diffusion-models/">Reaction Diffusion simulation</a>, for this application I have chosen to use Silverlight&#8217;s <a title="WirteableBitmap in Silverlight 3, from MSDN" href="http://msdn.microsoft.com/en-us/library/system.windows.media.imaging.writeablebitmap%28VS.95%29.aspx" target="_blank"><code>WriteableBitmap</code></a>, introduced in Silverlight 3, rather than <a title="Joe Stegman's PNG Encoder for Silverlight" href="http://blogs.msdn.com/jstegman/archive/2008/04/21/dynamic-image-generation-in-silverlight.aspx" target="_blank">dynamic PNG encoding</a>. This revealed an interesting performance issue when using a typical double loop to iterate over the pixels. Initial timings revealed that the vast majority of the time was spent in updating the <code>WriteableBitmap</code> rather than actually performing the image processing. The initial update loop used the <code>PixelWidth</code> and <code>PixelHeight</code> properties to bound the loop counters, taking about 200ms to iterate over the loop.</p>
<pre title="Updating bitmap using property accessors" lang="csharp">for (int j = 0; j &lt; bmp.PixelHeight; ++j)
{
  for (int i = 0; i &lt; bmp.PixelWidth; ++i)
  {
     // update pixels
   }
}</pre>
<p>By caching the bitmap properties in local variables, the timing was reduced to ~5ms. Needless to say I was shocked by how much of a difference such a seemingly trivial change made.</p>
<pre title="Updating bitmap with cached variables" lang="csharp">int pxWidth =  bmp.PixelWidth;
int pxHeight = bmp.PixelHeight;
for (int j = 0; j &lt; pxHeight; ++j)
{
  for (int i = 0; i &lt; pxWidth; ++i)
  {
     // update pixels
   }
}</pre>
<h4>OLE Automation</h4>
<p>The guidelines for building performant automation code is much the same as that for other unmanaged interop scenarios in .NET : avoid chatty interfaces. Note that this is exactly what I have not done here. In fact, the time it takes CUDA to perform the image processing is dwarfed by the time it takes to marshal the data between Silverlight and COM. This can be mitigated somewhat by splitting the blur call into two operations, one to load the image, which is called only upon initialization, and one to perform the blur.</p>
<h4>CUDA</h4>
<p>CUDA operations are extremely sensitive to data alignment and the order in which threads access data. Kernels should be written in such a way that threads access adjacent data elements, meaning that the row major access pattern familiar to C and C# developers would produce suboptimal performance ( sometimes by as much as an order of magnitude ). Instead, array accesses should be performed in a manner more reminiscent of FORTRAN. In addition, 2D arrays should be padded out so that threads access data elements that are correctly aligned ( see the CUDA documentation for the correct alignment values ). A full exposition of performance optimization for CUDA is really beyond the scope of this article, there are many examples in the <a title="Learn More about CUDA - NVIDIA" href="http://www.nvidia.com/object/cuda_education.html" target="_blank">NVIDIA documentation</a> although the terminology can be somewhat opaque. One of the clearest explanations I have found is this <a title="Supercomputing 2007 CUDA Tutorial" href="http://gpgpu.org/sc2007" target="_blank">presentation </a>from Mark Harris at Supercomputing 2007.</p>
<pre title="Row major access pattern" lang="cuda">__global__ void kernel( float *destData, float *srcData, int stride, int height )
{
  // suboptimal access. Each thread accesses elements
 // in a striding pattern
  int rowStart = (blockDim.x*blockIdx.x+threadIdx.x)*stride;
  for ( int i = rowStart; i < rowStart+stride; ++i ) {
    destData[i] = srcData[i];
  }
}</pre>
<pre title="Column major access patern" lang="cuda">__global__ void kernel( float *destData, float *srcData, int stride, int height )
{
  // optimal access pattern, each thread accesses adjacent elements
  int colStart = blockDim.x*blockIdx.x+threadIdx.x;
  // this case, 16*sizeof(float)= 64 bytes
  for ( int i = colStart ; i < colStart+(stride*height); i+=stride ) {
    destData[i] = srcData[i];
  }
}</pre>
<h3>References</h3>
<ol>
<li><span class="p1"><a name="young"></a>Young, I.T. &amp; van Vliet,L.J, 1995. <a title="Recursive Implementation of the Gaussian Filter" href="http://www.sciencedirect.com/science?_ob=ArticleURL&amp;_udi=B6V18-3YS90HC-D&amp;_user=10&amp;_coverDate=06%2F30%2F1995&amp;_rdoc=2&amp;_fmt=high&amp;_orig=browse&amp;_srch=doc-info%28%23toc%235668%231995%23999559997%23172292%23FLP%23display%23Volume%29&amp;_cdi=5668&amp;_sort=d&amp;_docanchor=&amp;_ct=11&amp;_acct=C000050221&amp;_version=1&amp;_urlVersion=0&amp;_userid=10&amp;md5=cdfad44c178fc20739d26562c5f26e04" target="_blank">Recursive implementation of the Gaussian filter</a>. <em>Signal Processing</em>, 44, pp.139-151. </span></li>
</ol>
]]></content:encoded>
			<wfw:commentRss>http://www.planetmarshall.co.uk/2010/01/silverlight-and-cuda-interop/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>Using Merged ResourceDictionaries in Silverlight Themes</title>
		<link>http://www.planetmarshall.co.uk/2009/07/using-merged-resourcedictionaries-in-silverlight-themes/</link>
		<comments>http://www.planetmarshall.co.uk/2009/07/using-merged-resourcedictionaries-in-silverlight-themes/#comments</comments>
		<pubDate>Wed, 29 Jul 2009 02:16:59 +0000</pubDate>
		<dc:creator>Andrew</dc:creator>
				<category><![CDATA[Software]]></category>
		<category><![CDATA[Silverlight]]></category>

		<guid isPermaLink="false">http://www.planetmarshall.co.uk/?p=303</guid>
		<description><![CDATA[Silverlight 3.0 moved its featureset closer to that of WPF by adding support for merged Resource Dictionaries and Style Inheritance. Without these features, developing custom templates and styles for Silverlight controls can become a bit of a copy-and-paste nightmare. Since<span class="ellipsis">&#8230;</span> <a href="http://www.planetmarshall.co.uk/2009/07/using-merged-resourcedictionaries-in-silverlight-themes/"><div class="read-more">Read more &#8250;</div><!-- end of .read-more --></a>]]></description>
			<content:encoded><![CDATA[<p class="pm_first"><span>S</span>ilverlight 3.0 moved its featureset closer to that of WPF by adding support for merged <a href="http://msdn.microsoft.com/en-us/library/cc903952%28VS.95%29.aspx" target="_new">Resource Dictionaries</a> and <a href="http://msdn.microsoft.com/en-us/library/system.windows.style.basedon%28VS.95%29.aspx" target="_new">Style Inheritance</a>. Without these features, developing custom templates and styles for Silverlight controls can become a bit of a copy-and-paste nightmare. Since I have used the implicit theming feature built into the <a href="http://www.codeplex.com/Silverlight" target="_new">Silverlight Toolkit</a> to make my Silverlight controls fit into the overall look and feel of this site, I was hoping that these features would enable me to refactor my themes developed for Silverlight 2.0 to be a bit less unwieldy.</p>
<p><span id="more-303"></span></p>
<h3>Silverlight toolkit themes do not support merged dictionaries</h3>
<p>Unfortunately, merged dictionaries are not supported by the Silverlight Toolkit&#8217;s Themes feature &#8211; at least not as it stands. For example, the following XAML and accompanying class causes a <code>XamlParseException</code> if you try to use it.</p>
<pre lang="xml">
<ResourceDictionary>
 <ResourceDictionary.MergedDictionaries>
  <ResourceDictionary Source="/MergedTheme;component/Styles/Button.xaml"/>
 <ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</pre>
<pre lang="csharp">
public class MergedTheme : Theme {
 public MergedTheme() :
  base(typeof(MergedTheme).Assembly, "MergedTheme.Theme.xaml") {
  DefaultStyleKey = typeof(MergedTheme);
 }
}
</pre>
<p>All is not lost however, since full source code is provided for the Silverlight Toolkit, so it becomes a straightforward matter to track down the problem and supply a patch. </p>
<h3>Writing a test to reproduce the problem</h3>
<p>Since the toolkit comes with an accompanying test suite, we proceed in textbook <a href="http://en.wikipedia.org/wiki/Test-driven_development" target="_new">TDD</a> fashion by first writing a test that exercises the issue. We can reuse some of the existing code to do this, adding a resource dictionary containing a merged dictionary reference and adding the following test to the <code>ImplicitStyleManagerTest</code> class</p>
<pre lang="csharp">
///
<summary>
/// Test that styles contained in a  merged dictionary can be successfully applied
/// </summary>

[TestMethod]
[Asynchronous]
[Description("Test that a dictionary contining merged dictionaries can be successfully loaded.")]
public void TestResourceDictionaryWithMergedDictionaries() {
 Uri uri = new Uri("System.Windows.Controls.Testing.Theming;
  component/ImplicitStyleManager/MergedResourceDictionary.xaml", UriKind.Relative);
 TestAuto( (stackPanel) => {
  SetResourceDictionaryUri(stackPanel, uri);
  ImplicitStyleManager.SetApplyMode(stackPanel, ImplicitStylesApplyMode.Auto);
  }, Colors.Blue);
}
</pre>
<p>The test fails as we would expect.</p>

<a href="http://www.planetmarshall.co.uk/wp-content/gallery/sltoolkit/testfail.png" title="The test written to exercise the bug fails as expected." class="shutterset_singlepic70" >
	<img class="ngg-singlepic" src="http://www.planetmarshall.co.uk/wp-content/gallery/cache/70__x_testfail.png" alt="Failing unit test" title="Failing unit test" />
</a>

<h3>Fix the code to make the test pass</h3>
<p>The Theme class works by transforming the<code> ResourceDictionary</code> xaml into a <code>ContentControl</code>. A quick glance at the xaml generated by this process in the Visual Studio debugger shows why an exception is being raised.</p>
<pre lang="xml">
<ContentControl>
 <ContentControl.Resources>
  <ResourceDictionary.MergedDictionaries>
   <ResourceDictionary Source="[...]/ResourceDictionaryForMerge.xaml" />
  </ResourceDictionary.MergedDictionaries>
 </ContentControl.Resources>
</ContentControl>
</pre>
<p>The <code>ResourceDictionary.MergedDictionaries</code> element is missing its parent <code>ResourceDictionary</code> element, so we add a couple of lines of code to ResourceParser.ParseElement to detect the element and wrap it in a <code>ResourceDictionary</code>, using LINQ to XML.</p>
<pre lang="csharp">
private static void ParseElement(XmlReader reader, XmlWriter writer, bool checkTypes) {
 if (reader.Depth == 0) {...}
 else {
  // handle merged dictionaries by wrapping them in a ResourceDictionary element
  if (reader.Name == "ResourceDictionary.MergedDictionaries") {
   XElement parent = new XElement("ResourceDictionary");
   parent.Add(XElement.ReadFrom(reader));
   writer.WriteRaw(parent.ToString());
  }
  else { ... }
 }
}
</pre>
<p>Having made the change, we rerun the test.</p>

<a href="http://www.planetmarshall.co.uk/wp-content/gallery/sltoolkit/testpass.png" title="How I love the green bar" class="shutterset_singlepic71" >
	<img class="ngg-singlepic" src="http://www.planetmarshall.co.uk/wp-content/gallery/cache/71__x_testpass.png" alt="How I love the green bar" title="How I love the green bar" />
</a>

<p>As we would hope, our custom theme now works as expected and we also have a unit test that ensures the bug does not reoccur. Note the Xaml code above, in that the Resource Dictionary is referenced using <a href="http://msdn.microsoft.com/en-us/library/aa970069.aspx" target="_new">Pack Uri</a> syntax. This is because <code>Button.xaml</code> is compiled as a Resource, rather than Content, since the latter doesn&#8217;t appear to work reliably and can result in obfuscated error messages such as <i>XamlParseException: attribute &#8220;Button.xaml&#8221; value out of range.</i></p>

<a href="http://www.planetmarshall.co.uk/wp-content/gallery/sltoolkit/themed.png" title="The button on the right gets its theme from a merged ResourceDictionary" class="shutterset_singlepic72" >
	<img class="ngg-singlepic" src="http://www.planetmarshall.co.uk/wp-content/gallery/cache/72__x_themed.png" alt="Themed controls" title="Themed controls" />
</a>

<p>I have submitted the patch for this fix to the toolkit site on Codeplex.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.planetmarshall.co.uk/2009/07/using-merged-resourcedictionaries-in-silverlight-themes/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Implementation of the Reaction Diffusion Simulation</title>
		<link>http://www.planetmarshall.co.uk/2009/04/implentation-of-the-reaction-diffusion-simulation/</link>
		<comments>http://www.planetmarshall.co.uk/2009/04/implentation-of-the-reaction-diffusion-simulation/#comments</comments>
		<pubDate>Thu, 02 Apr 2009 09:34:08 +0000</pubDate>
		<dc:creator>Andrew</dc:creator>
				<category><![CDATA[Software]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Silverlight]]></category>

		<guid isPermaLink="false">http://www.planetmarshall.co.uk/?p=111</guid>
		<description><![CDATA[Update &#8211; This post was written before the release of Silverlight 3.0b, which provides a number of enhancements relevant to this implementation, such as a WriteableBitmap and Pixel Shaders Rendering The first obstacle to implementing the RD simulation is that<span class="ellipsis">&#8230;</span> <a href="http://www.planetmarshall.co.uk/2009/04/implentation-of-the-reaction-diffusion-simulation/"><div class="read-more">Read more &#8250;</div><!-- end of .read-more --></a>]]></description>
			<content:encoded><![CDATA[<p>
<a href="http://www.planetmarshall.co.uk/wp-content/gallery/reaction_diffusion/blood.png" title="" class="shutterset_singlepic65" >
	<img class="ngg-singlepic ngg-left" src="http://www.planetmarshall.co.uk/wp-content/gallery/cache/65__x96_blood.png" alt="blood.png" title="blood.png" />
</a>
<em>Update &#8211; This post was written before the release of <a href="http://silverlight.net/getstarted/silverlight3/default.aspx" target="_new">Silverlight 3.0b</a>, which provides a number of enhancements relevant to this implementation, such as a WriteableBitmap and Pixel Shaders</em></p>
<h3>Rendering</h3>
<p>The first obstacle to implementing the <a href="http://www.planetmarshall.co.uk/index.php/2009/03/reaction-diffusion-models/">RD simulation</a> is that Silverlight 2.0 does not by default provide a means of generating dynamic images. WPF has a WriteableBitmap, but no equivalent exists in Silverlight. However, it does support PNG streams so we can dynamically update a bitmap by encoding it to PNG on the fly. For this I have used <a href="http://blogs.msdn.com/jstegman/default.aspx">Joe Stegman&#8217;s</a> <a href="http://blogs.msdn.com/jstegman/archive/2008/10/27/silverlight-2-sample-updates.aspx">PNGEncoder </a>class, which I have modified slightly to deal with RGB data and to reduce memory usage.<br />
<span id="more-111"></span></p>
<h3>Performance notes</h3>
<p>
<a href="http://www.planetmarshall.co.uk/wp-content/gallery/reaction_diffusion/waves.png" title="" class="shutterset_singlepic69" >
	<img class="ngg-singlepic ngg-right" src="http://www.planetmarshall.co.uk/wp-content/gallery/cache/69__x96_waves.png" alt="waves.png" title="waves.png" />
</a>
Performance of the simulator is dominated by the time it takes to update the arrays containing the concentration values of the chemicals. Actual rendering time is negligible, even though the image has to be dynamically encoded to a PNG file in order for Silverlight to display it. There are two ways of implementing 2D arrays in Silverlight with C#, rectangular arrays or C style jagged arrays. If this were a desktop application, the most performant method would be to use rectangular arrays and unsafe access, however that option is not available to us in Silverlight so it turns out that, for safe access, jagged arrays are faster.</p>
<p>For example, originally the simulation step looked like this</p>
<pre title="Rectangular array implementation" lang="csharp">for (int j = 0; j &lt; H; j++)
{
 int jm = j != 0 ? j - 1 : H - 1;
 int jp = j != H - 1 ? j + 1 : 0;
 for (int i = 0; i &lt; W; i++)
 {
  int im = i != 0 ? i - 1 : W - 1;
  int ip = i != W - 1 ? i + 1 : 0;
  double d2u = U[j,im]+U[j,ip]+U[jm,i]+U[jp,i]-4*U[j,i];
  double d2v = V[j,im]+V[j,ip]+V[jm,i]+V[jp,i]-4*V[j,i];
  double uv2 = U[j,i]*V[j,i]*V[j,i];
  Uu[j,i] = du*d2u-uv2+F+U[j,i]*one_minus_F;
  Vv[j,i] = dv*d2v+uv2+V[j,i]*one_minus_F_minus_k;
 }
}</pre>
<p>On my machine this took on the order of 600ms to perform 100 iterations. Using jagged arrays instead, we have the following</p>
<pre title="Jagged array implementation" lang="csharp">for (int j = 0; j &lt; H; j++)
{
 int jm = j != 0 ? j - 1 : H - 1;
 int jp = j != H - 1 ? j + 1 : 0;
 for (int i = 0; i &lt; W; i++)
 {
  int im = i != 0 ? i - 1 : W - 1;
  int ip = i != W - 1 ? i + 1 : 0;
  double d2u = U[j][im]+U[j][ip]+U[jm][i]+U[jp][i]-4*U[j][i];
  double d2v = V[j][im]+V[j][ip]+V[jm][i]+V[jp][i]-4*V[j][i];
  double uv2 = U[j][i]*V[j][i]*V[j][i];
  Uu[j][i] = du*d2u-uv2+ F+U[j][i]*one_minus_F;
  Vv[j][i] = dv*d2v+uv2+V[j][i]*one_minus_F_minus_k;
 }
}</pre>
<p>Just replacing the array type and making no other changes this now takes about 500ms, an improvement of about 13%. However, now that we are using jagged arrays we can do better and move the row accesses out of the inner loop.</p>
<pre title="Jagged array with outer loop caching" lang="csharp">for (int j = 0; j &lt; H; j++)
{
 int jm = j != 0 ? j - 1 : H - 1;
 int jp = j != H - 1 ? j + 1 : 0;
 var u_m = U[jm];
 var u = U[j];
 var u_p = U[jp];
 var v_m = V[jm];
 var v = V[j];
 var v_p = V[jp];
 var uu = Uu[j];
 var vv = Vv[j];
 for (int i = 0; i &lt; W; i++)
 {
  int im = i != 0 ? i - 1 : W - 1;
  int ip = i != W - 1 ? i + 1 : 0;
  double d2u = u[im]+u[ip]+u_m[i]+u_p[i]-4*u[i];
  double d2v = v[im]+v[ip]+v_m[i]+v_p[i]-4*v[i];
  double uv2 = u[i]*v[i] *v[i];
  uu[i] = du*d2u-uv2+F+u[i]*one_minus_F;
  vv[i] = dv*d2v+uv2+v[i]*one_minus_F_minus_k;
 }
}</pre>
<p>This brings the count down to about 330ms, an improvement of about 40% over the original implementation.</p>
<p>We&#8217;ve taken the sequential code about as far as we can go. The next step is to try to take advantage of muliple cores that may be available on the user&#8217;s machine.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.planetmarshall.co.uk/2009/04/implentation-of-the-reaction-diffusion-simulation/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Reaction-Diffusion Models</title>
		<link>http://www.planetmarshall.co.uk/2009/03/reaction-diffusion-models/</link>
		<comments>http://www.planetmarshall.co.uk/2009/03/reaction-diffusion-models/#comments</comments>
		<pubDate>Mon, 23 Mar 2009 00:27:03 +0000</pubDate>
		<dc:creator>Andrew</dc:creator>
				<category><![CDATA[Mathematics]]></category>
		<category><![CDATA[Software]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Differential Equations]]></category>
		<category><![CDATA[Silverlight]]></category>

		<guid isPermaLink="false">http://www.planetmarshall.co.uk/?p=124</guid>
		<description><![CDATA[One of Alan Turing&#8216;s many contributions to mathematics and science during the 20th century was his 1952 paper on &#8220;The Chemical Basis of Morphogenesis&#8221; in which he suggested that a simple model of coupled differential equations could account for pattern<span class="ellipsis">&#8230;</span> <a href="http://www.planetmarshall.co.uk/2009/03/reaction-diffusion-models/"><div class="read-more">Read more &#8250;</div><!-- end of .read-more --></a>]]></description>
			<content:encoded><![CDATA[<p>
<a href="http://www.planetmarshall.co.uk/wp-content/gallery/reaction_diffusion/maze.png" title="" class="shutterset_singlepic68" >
	<img class="ngg-singlepic ngg-right" src="http://www.planetmarshall.co.uk/wp-content/gallery/cache/68__x96_maze.png" alt="maze.png" title="maze.png" />
</a>

<p class="pm_first"><span>O</span>ne of <a target="_new" href="http://en.wikipedia.org/wiki/Alan_Turing">Alan Turing</a>&#8216;s many contributions to mathematics and science during the 20th century was his <a href="#1_turing">1952 paper</a> on &#8220;The Chemical Basis of Morphogenesis&#8221; in which he suggested that a simple model of coupled differential equations could account for pattern formation in natural processes such as those found on animal coats. Such models are known as Reaction-Diffusion models, and take the following general form<br />
<img src="http://www.planetmarshall.co.uk/wp-content/plugins/latex/latexrender/pictures/a7d70e02c93cb96e6b0012ecebb75b57.png"title="&#10;\frac{\partial}{\partial t}\mathbf{q}=\mathbf{D}\nabla^2\mathbf{q}+\mathbf{R}(\mathbf{q})&#10;" alt="&#10;\frac{\partial}{\partial t}\mathbf{q}=\mathbf{D}\nabla^2\mathbf{q}+\mathbf{R}(\mathbf{q})&#10;" style="vertical-align:top ;"/></p>
<p><span id="more-124"></span></p>
<p>The equation describes how the concentration of each chemical components in q evolves as a function of the other components. I have chosen to illustrate the Gray-Scott model; the physical derivation of the reactant term is described in detail in <a href="#2_grayscott">[2]</a>, the addition of the diffusion term and the resultant behaviour in <a href="#3_pearson">[3]</a>. The model has two chemical components, U and V and is described as follows.<br />
<img src="http://www.planetmarshall.co.uk/wp-content/plugins/latex/latexrender/pictures/0066159d03a0b2aa45c47b46c63ac571.png"title="&#10;\begin{align*}&#10;\frac{\partial}{\partial t}U&amp;#038;=D_u\nabla^2U-UV^2+F(1-U) \\&#10;\frac{\partial}{\partial t}V&amp;#038;=D_v\nabla^2V+UV^2-(F+k)V&#10;\end{align*}&#10;" alt="&#10;\begin{align*}&#10;\frac{\partial}{\partial t}U&amp;#038;=D_u\nabla^2U-UV^2+F(1-U) \\&#10;\frac{\partial}{\partial t}V&amp;#038;=D_v\nabla^2V+UV^2-(F+k)V&#10;\end{align*}&#10;" style="vertical-align:top ;"/></p>
<p>The model is what is knows as an activator-inhibitor model, in which one chemical acts to inhibit the growth of the other.</p>
<h3>Simulation</h3>
<p>The simulation is implemented with <a href="http://www.silverlight.net" target="_new">Silverlight 2.0</a>. For full details of the implementation see the separate <a href="http://www.planetmarshall.co.uk/index.php/2009/04/implentation-of-the-reaction-diffusion-simulation/">post</a>. For guidance in parameter selection, see <a href="#3_pearson">[3]</a>, or just select a preset and modify it. Note that the Png image generation feature currently requires the <a target="_new" href="http://www.mozilla.com/firefox/">Firefox</a> browser.</p>
<p><object width="450" height="300"<br />
    data="data:application/x-silverlight-2,"<br />
    type="application/x-silverlight-2" ><param name="source" value="http://www.planetmarshall.co.uk/apps/rdsim/ReactionDiffusion.xap"/></object></p>
<h3>Gallery</h3>
<div class="ngg-galleryoverview" id="ngg-gallery-5-124">

	<!-- Slideshow link -->
	<div class="slideshowlink">
		<a class="slideshowlink" href="http://www.planetmarshall.co.uk/2009/03/reaction-diffusion-models/?show=slide">
			[Show as slideshow]		</a>
	</div>

	
	<!-- Thumbnails -->
		
	<div id="ngg-image-65" class="ngg-gallery-thumbnail-box"  >
		<div class="ngg-gallery-thumbnail" >
			<a href="http://www.planetmarshall.co.uk/wp-content/gallery/reaction_diffusion/blood.png" title=" " class="shutterset_set_5" >
								<img title="blood.png" alt="blood.png" src="http://www.planetmarshall.co.uk/wp-content/gallery/reaction_diffusion/thumbs/thumbs_blood.png"  />
							</a>
		</div>
	</div>
	
		
 		
	<div id="ngg-image-66" class="ngg-gallery-thumbnail-box"  >
		<div class="ngg-gallery-thumbnail" >
			<a href="http://www.planetmarshall.co.uk/wp-content/gallery/reaction_diffusion/cells.png" title=" " class="shutterset_set_5" >
								<img title="cells.png" alt="cells.png" src="http://www.planetmarshall.co.uk/wp-content/gallery/reaction_diffusion/thumbs/thumbs_cells.png"  />
							</a>
		</div>
	</div>
	
		
 		
	<div id="ngg-image-67" class="ngg-gallery-thumbnail-box"  >
		<div class="ngg-gallery-thumbnail" >
			<a href="http://www.planetmarshall.co.uk/wp-content/gallery/reaction_diffusion/clouds.png" title=" " class="shutterset_set_5" >
								<img title="clouds.png" alt="clouds.png" src="http://www.planetmarshall.co.uk/wp-content/gallery/reaction_diffusion/thumbs/thumbs_clouds.png"  />
							</a>
		</div>
	</div>
	
		
 		
	<div id="ngg-image-68" class="ngg-gallery-thumbnail-box"  >
		<div class="ngg-gallery-thumbnail" >
			<a href="http://www.planetmarshall.co.uk/wp-content/gallery/reaction_diffusion/maze.png" title=" " class="shutterset_set_5" >
								<img title="maze.png" alt="maze.png" src="http://www.planetmarshall.co.uk/wp-content/gallery/reaction_diffusion/thumbs/thumbs_maze.png"  />
							</a>
		</div>
	</div>
	
		
 		
	<div id="ngg-image-69" class="ngg-gallery-thumbnail-box"  >
		<div class="ngg-gallery-thumbnail" >
			<a href="http://www.planetmarshall.co.uk/wp-content/gallery/reaction_diffusion/waves.png" title=" " class="shutterset_set_5" >
								<img title="waves.png" alt="waves.png" src="http://www.planetmarshall.co.uk/wp-content/gallery/reaction_diffusion/thumbs/thumbs_waves.png"  />
							</a>
		</div>
	</div>
	
		
 	 	
	<!-- Pagination -->
 	<div class="ngg-clear"></div> 	
</div>

<h3>References</h3>
<ol>
<li><a name="1_turing"></a>Turing,A.,1952. <a target="_new" href="http://www.dna.caltech.edu/courses/cs191/paperscs191/turing.pdf">The chemical basis of morphogenesis [pdf]</a>. <i>Philosophical Transactions of the Royal Society</i>, 237 pp.37-72
</li>
<li><a name="2_grayscott"></a>Gray,P. and Scott,S.K. 1985. <a href="http://www.jstor.org/stable/2881810" target="_new">Sustained Oscillations and Other Exotic Patterns of Behavior in Isothermal Reactions.</a> <i>Journal of Physical Chemistry</i>, 89 pp.22-32
</li>
<li><a name="3_pearson"></a>Pearson,J.,1993. <a href="http://www.jstor.org/stable/2881810" target="_new">Complex patterns in a simple system.</a> <i>Science</i>, 261(5118) pp.189-192
</li>
</ol>
]]></content:encoded>
			<wfw:commentRss>http://www.planetmarshall.co.uk/2009/03/reaction-diffusion-models/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>URM Emulator</title>
		<link>http://www.planetmarshall.co.uk/2009/01/urm-emulator/</link>
		<comments>http://www.planetmarshall.co.uk/2009/01/urm-emulator/#comments</comments>
		<pubDate>Sun, 25 Jan 2009 23:22:34 +0000</pubDate>
		<dc:creator>Andrew</dc:creator>
				<category><![CDATA[Mathematics]]></category>
		<category><![CDATA[Software]]></category>
		<category><![CDATA[F#]]></category>
		<category><![CDATA[Logic]]></category>
		<category><![CDATA[Open University]]></category>
		<category><![CDATA[Silverlight]]></category>

		<guid isPermaLink="false">http://www.planetmarshall.co.uk/?p=24</guid>
		<description><![CDATA[The Unlimited Register Machine (URM), designed by Nigel Cutland, is an abstraction of a computer similar to the Turing Machine &#8211; but somewhat easier to get to grips with and more in tune with the operation of a modern computer<span class="ellipsis">&#8230;</span> <a href="http://www.planetmarshall.co.uk/2009/01/urm-emulator/"><div class="read-more">Read more &#8250;</div><!-- end of .read-more --></a>]]></description>
			<content:encoded><![CDATA[<p class="pm_first"><span>T</span>he Unlimited Register Machine (URM), designed by <a href="http://www.cambridge.org/us/catalogue/catalogue.asp?isbn=9780521294652" target="_new">Nigel Cutland</a>, is an abstraction of a computer similar to the <a href="http://en.wikipedia.org/wiki/Turing_machine" target="_new">Turing Machine</a> &#8211; but somewhat easier to get to grips with and more in tune with the operation of a modern computer processor.</p>
<p>It also happens to be the model used by the Open University&#8217;s course on <a href="http://www3.open.ac.uk/courses/bin/p12.dll?C01M381" target="_new">Mathematical Logic and Number Theory</a>, and is the reason for this article since I studied the course in 2007.</p>
<p><span id="more-24"></span></p>
<h3>Emulator</h3>
<p>The implementation details of the emulator are quite interesting, using a mixture of Microsoft technologies including F# and Silverlight, however I will save that for another article for those interested in such things. To use the Emulator just hit Reset, enter the program and initial registers and click Step to trace through the program.<br />
[silverlight: Urm.App.xap,400,300,false] </p>
]]></content:encoded>
			<wfw:commentRss>http://www.planetmarshall.co.uk/2009/01/urm-emulator/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

