<?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>Dave&#039;s Programming Blog &#187; Go</title>
	<atom:link href="http://dev.mcleish.id.au/category/go/feed/" rel="self" type="application/rss+xml" />
	<link>http://dev.mcleish.id.au</link>
	<description>The Church of Alan Turing of Latter-Day Sorcerers</description>
	<lastBuildDate>Wed, 09 Feb 2011 12:09:59 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Priority-queued channel in Go</title>
		<link>http://dev.mcleish.id.au/2011/01/priority-queued-channel-in-go/</link>
		<comments>http://dev.mcleish.id.au/2011/01/priority-queued-channel-in-go/#comments</comments>
		<pubDate>Sun, 23 Jan 2011 04:16:58 +0000</pubDate>
		<dc:creator>David</dc:creator>
				<category><![CDATA[Go]]></category>

		<guid isPermaLink="false">http://dev.mcleish.id.au/?p=289</guid>
		<description><![CDATA[Right. So. I&#8217;ve been writing my standard hello-world Sudoku solver in Go. It reached a plateau about a week ago when some changes that I thought were going to make it faster actually made it slower. I think I did something to a function that stopped the compiler inlining it. Or something. The point is [...]]]></description>
			<content:encoded><![CDATA[<p>Right. So. I&#8217;ve been writing my standard hello-world <a href="http://hg.mcleish.id.au/sudoku-go">Sudoku solver in Go</a>. It reached a plateau about a week ago when some changes that I thought were going to make it faster actually made it slower. I think I did something to a function that stopped the compiler inlining it. Or something. The point is that I was getting diminishing returns from incremental improvements to the current code.</p>
<p>The next step is to add some concurrency. At the moment it does some steps in parallel in the puzzle-generation stage, but it&#8217;s still solving a single grid serially. (The general algorithm is: solve an empty grid; then visit each square, and clear it if the resulting puzzle still has a unique solution. Right now, the check for a unique solution (or rather, the search for other solutions, which is expected to fail) is happening in several goroutines.)</p>
<p>The approach I used with the <a href="http://dev.mcleish.id.au/2010/11/4x4-sudoku/">Java version</a> was to keep partial solutions in a priority queue, ordered by the number of squares filled (so that the next one out of the queue is the most complete). These were farmed out to a thread pool. The result was a more-or-less depth-first search of the solution space. For reference, the thread manager code is <a href="http://hg.mcleish.id.au/sudoku-java/file/54ad63e1537c/src/au/id/mcleish/sudoku/SudokuThreadManager.java">here</a>. (IIRC, there&#8217;s a bug in it somewhere &#8211; it doesn&#8217;t terminate properly.)</p>
<p>Concurrency is one of the things that&#8217;s <a href="http://golang.org/doc/effective_go.html#concurrency">supposed to be nice in Go</a>. The basic approach I&#8217;m going to take for the priority queue is to have two channels, tied together by a goroutine that reads items from the &#8220;in&#8221; channel, puts them into a heap, then pops from the heap and writes to the &#8220;out&#8221; channel.</p>
<p>This morning I knocked together the <a href="http://hg.mcleish.id.au/pqueue">code</a> to do this in abstract. (Well, with <code>int</code>s, anyway. I&#8217;ll try to abstract out the type before using it for the Sudoku stuff. Generics would be nice here &#8211; generics are one of the <a href="http://golang.org/doc/go_faq.html#generics">longest-standing items on the wishlist for Go</a> &#8211; but I think I can see a Go-like way of doing it regardless&#8230;) There are some funny corner cases with blocking &#8211; it needs to read eagerly enough from the &#8220;in&#8221; channel that things don&#8217;t just pass through, but not so much that it starves the &#8220;out&#8221; channel &#8211; and I&#8217;m not sure I&#8217;ve got the most parsimonious version yet, but it seems to work. And <a href="http://hg.mcleish.id.au/pqueue/file/2c5c676d74a4/pqueue.go">compared</a> to the <a href="http://hg.mcleish.id.au/sudoku-java/file/54ad63e1537c/src/au/id/mcleish/sudoku/SudokuThreadManager.java">Java version</a>, it really is quite pretty. Okay, okay, it might be a bit messier by the time I add a couple of other things needed by the Sudoku solver.</p>
<p>So, yeah. Progress.</p>
]]></content:encoded>
			<wfw:commentRss>http://dev.mcleish.id.au/2011/01/priority-queued-channel-in-go/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
