<?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>jalf.dk &#187; stm</title>
	<atom:link href="http://jalf.dk/blog/tag/stm/feed/" rel="self" type="application/rss+xml" />
	<link>http://jalf.dk/blog</link>
	<description>Musings and thoughts on programming and other geeky stuff</description>
	<lastBuildDate>Sun, 25 Mar 2012 09:51:53 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>STM Status Page</title>
		<link>http://jalf.dk/blog/2011/06/stm-status-page/</link>
		<comments>http://jalf.dk/blog/2011/06/stm-status-page/#comments</comments>
		<pubDate>Sun, 05 Jun 2011 14:50:13 +0000</pubDate>
		<dc:creator>jalf</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[c++]]></category>
		<category><![CDATA[stm]]></category>

		<guid isPermaLink="false">http://jalf.dk/blog/?p=907</guid>
		<description><![CDATA[As I mentioned not too long ago, I’ve recently resumed work on the STM library I created for my Masters Thesis. It’s still not quite where I want it to be, but I felt that at the very least, it deserved a proper status page. So here it is. A brand new page on my [...]]]></description>
			<content:encoded><![CDATA[<p>As I mentioned <a href="http://jalf.dk/blog/2011/04/empty-statement-of-intent/">not too long ago</a>, I’ve recently resumed work on the STM library I created for my Masters Thesis.</p>

<p>It’s still not <em>quite</em> where I want it to be, but I felt that at the very least, it deserved a proper status page. So <a href="http://jalf.dk/blog/stm/">here it is</a>. A brand new page on my blog. As I’m still rather busy, I can’t promise that much will happen with the library over the summer, but when something significant happens, that page will be the first to know.</p>
]]></content:encoded>
			<wfw:commentRss>http://jalf.dk/blog/2011/06/stm-status-page/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Empty statement of intent</title>
		<link>http://jalf.dk/blog/2011/04/empty-statement-of-intent/</link>
		<comments>http://jalf.dk/blog/2011/04/empty-statement-of-intent/#comments</comments>
		<pubDate>Fri, 15 Apr 2011 19:09:06 +0000</pubDate>
		<dc:creator>jalf</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[c++]]></category>
		<category><![CDATA[dikustm]]></category>
		<category><![CDATA[stm]]></category>
		<category><![CDATA[thesis]]></category>

		<guid isPermaLink="false">http://jalf.dk/blog/?p=854</guid>
		<description><![CDATA[I just want it on record that I intend to actually bring my STM library into a feature-complete, robust and well-documented state, and then release it to the world. I have been working on it on and off for the last couple of months (although the pace slowed down a lot when I started at [...]]]></description>
			<content:encoded><![CDATA[<p>I just want it on record that I intend to actually bring my <a href="http://jalf.dk/blog/2009/09/software-transactional-memory/">STM</a> library into a feature-complete, robust and well-documented state, and then release it to the world.
<span id="more-854"></span></p>

<p>I have been working on it on and off for the last couple of months (although the pace slowed down a lot when I started at my new job), and it is actually in pretty good shape. To the best of my knowledge (it is difficult to be sure when it comes to multithreaded code), there are no race conditions or other nasty bugs. It is basically feature complete, although I’m still working on improving the performance of some of them.</p>

<p>And the documentation is, of course, totally missing. I’ve got everything I wrote for <a href="http://jalf.dk/thesis/thesis.pdf">my masters thesis</a>, but a major rewrite is in order since the focus is now very different. Another starting point is <a href="http://jalf.dk/blog/2009/11/using-my-stm-library/">this</a>, but obviously, there’s quite a bit of work left on the documentation as well.</p>

<p>But it came up in a discussion with a bunch of quite clever programmers who expressed interest in it, and since it’s always been my intention to release it <em>some day</em>, and since I’m actually getting fairly close to being satisfied with its state, well, I might as well put this up here for the world to see:</p>

<h1>DikuSTM lives!</h1>
]]></content:encoded>
			<wfw:commentRss>http://jalf.dk/blog/2011/04/empty-statement-of-intent/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Houston, we have a (performance) problem</title>
		<link>http://jalf.dk/blog/2009/12/houston-we-have-a-performance-problem/</link>
		<comments>http://jalf.dk/blog/2009/12/houston-we-have-a-performance-problem/#comments</comments>
		<pubDate>Tue, 15 Dec 2009 13:49:43 +0000</pubDate>
		<dc:creator>jalf</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[c++]]></category>
		<category><![CDATA[performance]]></category>
		<category><![CDATA[stm]]></category>
		<category><![CDATA[thesis]]></category>
		<category><![CDATA[transactional-memory]]></category>

		<guid isPermaLink="false">http://jalf.dk/blog/?p=403</guid>
		<description><![CDATA[Ouch. These last few days, I’ve been fixing a few lingering bugs in my STM system, and last night, I finally nailed them. Specifically, it is now possible to open variables within a transaction as read-only. An obvious optimization, right? At least that’s the idea. Less work is required by the STM system if we [...]]]></description>
			<content:encoded><![CDATA[<p>Ouch. These last few days, I’ve been fixing a few lingering bugs in my STM system, and last night, I finally nailed them. Specifically, it is now possible to open variables within a transaction as <em>read-only</em>. An obvious optimization, right? At least that’s the idea. Less work is required by the STM system if we can trust that the variable isn’t modified by this transaction.
<span id="more-403"></span></p>

<p>Well, my test case for this feature now takes <em>ages</em> to run. As I mentioned previously, a simple transaction modifying two integer variables under heavy contention can pull off almost two million transactions per second on my laptop.</p>

<p>My new test, in which each thread takes four variables and alternates between modifying two of them and reading the other two, runs perhaps ten thousand (!) times slower.</p>

<p>Of course I have several leads on how to fix this. The problem is largely all the performance-related “extras” I’ve been leaving out. For example, if a transaction fails to acquire a variable it needs, it simply aborts and immediately retries. In many cases, a  better approach would be to block the thread, waiting for that variable to actually become available.</p>

<p>There are several other cases where I have a similar problem: I have to choose between delaying the thread for a moment with <code>sleep()</code> before attempting to continue, blocking it until some condition is true, or aborting the transaction entirely and starting over from scratch. At the moment, I generally just pick the easiest solutions (typically abort, and <em>occasionally</em> call <code>sleep()</code> a few times before we resort to that. Again, implementing some actual meaningful policies here would make a big difference. And tweaking these policies should help still more.</p>

<p>Another problem is that currently, I do not enforce a consistent global order when acquiring objects during a commit. This means I risk livelocks, again causing excessive rollbacks when multiple threads are competing over access to the same variables.</p>

<p>So I’m still optimistic. It should be possible to get performance back on track. But man, it’s depressing watching performance plummet like this.</p>

<p><strong>Edit</strong><br />
And an update. After poking around a bit, it turned out that most of the time was being spent sleeping. When a transaction attempts to commit, if it can not acquire all the all the variables it needs, it retries a few times with a short delay (a couple of milliseconds) in between. If it doesn’t succeed after a few tries, it rolls back the entire transaction and starts over.</p>

<p>It turned out that these few, short <code>sleep()</code> calls brought CPU utilization down to something like 0.01%, and totally destroyed performance. Simply turning the <code>sleep()</code> call into a <em>no-op</em> brought me back to something more or less reasonable. I still need to improve on the above shortcomings, but now at least I can run my tests in less than an hour.</p>
]]></content:encoded>
			<wfw:commentRss>http://jalf.dk/blog/2009/12/houston-we-have-a-performance-problem/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Using My STM Library</title>
		<link>http://jalf.dk/blog/2009/11/using-my-stm-library/</link>
		<comments>http://jalf.dk/blog/2009/11/using-my-stm-library/#comments</comments>
		<pubDate>Mon, 30 Nov 2009 12:58:22 +0000</pubDate>
		<dc:creator>jalf</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[c++]]></category>
		<category><![CDATA[stm]]></category>
		<category><![CDATA[thesis]]></category>
		<category><![CDATA[transactional-memory]]></category>

		<guid isPermaLink="false">http://jalf.dk/blog/?p=362</guid>
		<description><![CDATA[As promised yesterday, I’d like to show off a few bits of my STM library. Of course it’s far from done, and is still missing several key features, but the core library is in pretty good shape. So as they say on the internets, “my STM library, let me show you it” In the following, [...]]]></description>
			<content:encoded><![CDATA[<p>As promised yesterday, I’d like to show off a few bits of my STM library. Of course it’s far from done, and is still missing several key features, but the core library is in pretty good shape. So as they say on the internets, <em>“my STM library, let me show you it”</em><span id="more-362"></span></p>

<p>In the following, I’ll show a slightly modified version of one of my test cases. It shows how to call and use my library, from the user’s point of view. The test spawns a number of threads, which each wait on a barrier (because if one thread was allowed to run while another was being constructed, I’d get fewer concurrent transactions, and my test would be less likely to uncover race conditions), and then perform a fixed number of transactions. In each transaction, two transactional variables are opened for writing, one is decremented and the other incremented. If the sum of these variables is nonzero in any iteration, the thread registers a failure.</p>

<p>And if, after all the threads have terminated, the value of each of these variables is not what was expected, a failure is registered as well.</p>

<p>So from a testing point of view, there should be plenty of opportunity for things to go wrong. Just a single small race condition somewhere, and <em>one</em> of the many millions of reads would be inconsistent and the test would fail.</p>

<pre><code>#include &lt;stm.hpp&gt; // my STM library

#include &lt;boost/test/unit_test.hpp&gt; // Boost.Test is used to supply a unit-testing framework
#include &lt;boost/thread.hpp&gt; // Boost.Thread is used as a threading API

// define the number of threads to run, and the number of iterations for each
enum { thread_count = 8, iterations = 200000 }; 

// The following are transactional variables. The shared template ensures that the contained value
// can only be accessed as part of a transaction, and provides the necessary metadata
// for checking validity and consistency
// Two such integers are created, both initialized to zero
stm::shared&lt;int&gt; val1(0);
stm::shared&lt;int&gt; val2(0); 

BOOST_AUTO_TEST_SUITE( threads ) // define a test suite named "threads"

// this function defines the body of our transaction. 
// We're passed a transaction, which can be used to open any "shared" variables
bool tx_func(stm::transaction&amp; tx){
    // open both variables for writing
    int&amp; a = val1.open_rw(tx);
    int&amp; b = val2.open_rw(tx);

    // modify the variables freely
    --a;
    ++b;

    return a + b == 0; // Our transaction returns a bool. Other return types (or void) are also supported
}

// this class defines a thread. operator() is called as the thread's entry point
struct thread_functor{
    // In the constructor, the thread object is given a barrier it can synchronize on,
    // and a reference where it can write the its result (success/failure)
    thread_functor(boost::barrier&amp; bar, int&amp; res) : bar(bar), res(res) {}

    void operator()(){
        // when the thread is first created, we wait for the barrier
        // This ensures that no transactions are running until all threads have been constructed
        bar.wait(); 
        for (int i = 0; i &lt; iterations; ++i){
            // for each iteration, pass our transaction function to the "atomic" function, which executes it atomically.
            // To get a non-void return type, we have to specify the template parameter explicitly 
            // (this can be avoided in C++0x using the return_of template to deduce the return type implicitly)

            // depending on the return value of the transaction, write success or failure back
            res = (res != 0) &amp;&amp; stm::atomic&lt;bool&gt;(tx_func) ? 1 : 0;
        }
    }

    boost::barrier&amp; bar;
    int&amp; res;
};

// another transaction, to be executed after our helper threads terminate, 
// to verify that the right number of modifications have occurred
// note that here variables are opened for reading only
void verify(stm::transaction&amp; tx) {
    const int&amp; a = val1.open_r(tx);
    const int&amp; b = val2.open_r(tx);

    BOOST_CHECK_EQUAL(-a, thread_count * iterations);
    BOOST_CHECK_EQUAL(b, thread_count * iterations);
}

// finally, we get to our test case itself
BOOST_AUTO_TEST_CASE ( short_concurrent_transactions )
{
    boost::barrier bar(thread_count);
    boost::thread_group gr;
    int res[thread_count]; // array of results
    // set all the results to an initial true/1 value (since each iteration "and"'s it together with the current result
    std::fill(res, res+thread_count, 1); 

    for (int i = 0; i &lt; thread_count; ++i){
        gr.create_thread(thread_functor(bar, res[i])); // create the threads, passing the necessary parameters to each
    }

    gr.join_all(); // wait for all threads to terminate

    // verify that each thread return success
    for (int i = 0; i &lt; thread_count; ++i){
        BOOST_CHECK_EQUAL(res[i], 1);
    }

    // run a final transaction to access both variables and check their final values
    stm::atomic(verify);
}

BOOST_AUTO_TEST_SUITE_END()
</code></pre>

<p>In the above, I used a function object to represent threads, and a regular function to represent transactions. Of course in both cases, either would work — a function object would potentially be more efficient as it is easier for the compiler to inline, but I used a function for brevity.</p>

<p>In C++0x, of course, lambdas could also have been used in both cases.</p>

<p>One of my design goals has been to make basic usage as simple and intuitive as possible, and I think I’ve succeeded so far. Any C++ programmer who is familiar with the STL algorithms or the Boost libraries, should find my library’s interface very straightforward. Note especially that all the transaction “magic”, of verifying validity and retrying transactions as needed, is completely invisible to the user. You simply define a function expressing what your transaction should do, and pass it to the <code>atomic</code> function.</p>

<p>In its current version, this test is able to execute around 1,800,000 transactions per second on my Core Duo 2GHz laptop. (Of course, with transactions as small as these, opening only two variables each, performance is a lot better than it would be in real-world transactions.</p>

<p>So that’s it for now. Of course I’ve got a few more user-facing features in the pipeline<sup id="fnref:1"><a href="#fn:1" rel="footnote">1</a></sup>, and a <em>lot</em> of backend changes, but the basic functionality is there, and I’m pretty happy with it so far.</p>

<div class="footnotes">
<hr />
<ol>

<li id="fn:1">
<p>It should probably be possible to specify that the transaction should <em>not</em> automatically retry if it fails to commit, and instead abort with an exception. It should also be possible to define nested transactions, and use operations such as <em>OrElse</em> and <em>Retry</em> primitives introduced in <a href="http://www.haskell.org/haskellwiki/Software_transactional_memory">Haskell STM</a> <a href="#fnref:1" rev="footnote">↩</a></p>
</li>

</ol>
</div>
]]></content:encoded>
			<wfw:commentRss>http://jalf.dk/blog/2009/11/using-my-stm-library/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Getting back on track</title>
		<link>http://jalf.dk/blog/2009/11/getting-back-on-track/</link>
		<comments>http://jalf.dk/blog/2009/11/getting-back-on-track/#comments</comments>
		<pubDate>Sun, 29 Nov 2009 14:13:42 +0000</pubDate>
		<dc:creator>jalf</dc:creator>
				<category><![CDATA[Meanwhile]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[stm]]></category>
		<category><![CDATA[thesis]]></category>
		<category><![CDATA[transactional-memory]]></category>

		<guid isPermaLink="false">http://jalf.dk/blog/?p=357</guid>
		<description><![CDATA[We’ve more or less settled in our new apartment, and we’ve got internet… sort of! The connection works, but our router and the ethernet cables we ordered were apparently lost in the mail… So at the moment, we can have exactly one computer online at a time, and I pretty much have to sit right [...]]]></description>
			<content:encoded><![CDATA[<p>We’ve more or less settled in our new apartment, and we’ve got internet… sort of!<span id="more-357"></span></p>

<p>The connection works, but our router and the ethernet cables we ordered were apparently lost in the mail… So at the moment, we can have exactly one computer online at a time, and I pretty much have to sit right next to the adsl modem for the cable wehave to reach. Silly and annoying.</p>

<p>But overall, things are getting back to normal, and I’ve even had time to work on my thesis some. I’m going to put up a post soon with some code examples and such, but for now, the short version is that <em>it works</em>. I now have a prototype of the core functionality — a lot of importants features aren’t implemented yet (such as support for nested transactions), but the core works. I can start transactions, read and modify variables, and the system then attempts to commit, validates the modifications and if it fails, the transaction is rolled back and restarted.</p>

<p>Since the entire project is meant to take 6 months, and I have just over 3 months left, I’m pretty happy with this. Now that I’ve got a stable and functioning base to build on, future upgrades should go a lot faster.</p>
]]></content:encoded>
			<wfw:commentRss>http://jalf.dk/blog/2009/11/getting-back-on-track/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Being functional in an imperative language</title>
		<link>http://jalf.dk/blog/2009/10/being-functional-in-an-imperative-language/</link>
		<comments>http://jalf.dk/blog/2009/10/being-functional-in-an-imperative-language/#comments</comments>
		<pubDate>Sat, 03 Oct 2009 12:00:25 +0000</pubDate>
		<dc:creator>jalf</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[c++]]></category>
		<category><![CDATA[functional]]></category>
		<category><![CDATA[imperative]]></category>
		<category><![CDATA[stm]]></category>
		<category><![CDATA[thesis]]></category>
		<category><![CDATA[transactional-memory]]></category>

		<guid isPermaLink="false">http://jalf.dk/blog/?p=334</guid>
		<description><![CDATA[By now, I’ve read an awful lot of papers about STM systems, and certain trends are really starting to stand out, not so much in terms of the algorithms used or the clever schemes invented to make transactions appear atomic, but in how they interface with the actual language. It has really underlined to me [...]]]></description>
			<content:encoded><![CDATA[<p>By now, I’ve read an awful lot of papers about STM systems, and certain trends are really starting to stand out, not so much in terms of the algorithms used or the clever schemes invented to make transactions appear atomic, but in how they interface with the actual language.</p>

<p>It has really underlined to me just how deeply entrenched most Java, C and C++ programmers are in the imperative mindset.
<span id="more-334"></span>
When we want to perform a transaction, we’ll need to execute something like the following pseudocode:</p>

<pre><code>for (;;) {
  tx = new Transaction();
  try {
    // ... fill in body of the transaction ...
    tx.Validate();
    tx.Commit()
  }
  catch (InvalidTxException){
    tx.Rollback();
  }
}
</code></pre>

<p>And several STM systems actually require the user to write the above whenever a transaction is to be executed.</p>

<p>Some implementers have realized that this is both verbose, messy and error-prone.
So they try to hide it behind a macro.</p>

<pre><code>#define BEGIN_ATOMIC \
  for (;;) { \
    tx = new Transaction();\
    try { \

#define END_ATOMIC \
      tx.Validate();
      tx.Commit()
    }
    catch (InvalidTxException){
      tx.Rollback();
    }
  }
</code></pre>

<p>and now a transaction looks like this instead, from the user’s point of view:</p>

<pre><code>BEGIN_ATOMIC
// ... fill in body of the transaction ...
END_ATOMIC
</code></pre>

<p>Which is obviously shorter and requires less duplicate code. On the other hand, it relies on macros. Eeeew, yuck!</p>

<p>In Java, most implementers have tried to extend the language, to add a language-level <code>atomic</code> statement, allowing code like this:</p>

<pre><code>atomic {
  // ... fill in body of the transaction ...
}
</code></pre>

<p>And yes, this obviously works too, and has the potential to integrate much more closely with the compiler and type-checker. But  it’s also a <em>very</em> imperative kind of thinking. “add another type of scope inside whichever function wants to perform the transaction”.</p>

<p>What astonishes me is that no one (apart from Peyton Jones, who implemented Haskell STM) have realized that what we really want is nothing more than a higher-order function. A function which implements the transaction infrastructure, and then calls a user-defined function.</p>

<p>What we’d like to do, ideally, is to define a function <code>atomic</code>, as in the following pseudocode:</p>

<pre><code>T atomic(f : Tx -&gt; T){
  for (;;) {
    tx = new Transaction();
    try {
      T ret = f(tx);
      tx.Validate();
      tx.Commit();
      return ret;
    }
    catch (InvalidTxException){
      tx.Rollback();
    }
  }
}
</code></pre>

<p>Of course, if we wanted to be <em>really</em> functional, we could use recursion instead of the loop, but let’s not get carried away. Ultimately, what we want is just to hide all the messy infrastructure of a transaction from the user.</p>

<p>The above declares a function <code>atomic</code> which takes as its parameter a function which, given a transaction (type <code>Tx</code>), returns type <code>T</code>.</p>

<p><code>atomic</code> implements the messy transaction loop, and simply calls the user-supplied function inside it.</p>

<p>And presto, the user can now create a transaction like this:</p>

<pre><code>int myTx(Tx tx) {
  // do whatever
}
int result = atomic(myTx);
</code></pre>

<p>Or, in a language which supports lambda expressions,</p>

<pre><code>int result = atomic((tx) =&gt; {/* do whatever */ });
</code></pre>

<p>Bit nicer than messing around with user-implemented loops <em>or</em> macros, isn’t it?
Of course, I’ve so far sought refuge within the peaceful realms of pseudocode, where anything is possible. We obviously can’t do this in Java or C++, can we?</p>

<p>No, and yes, in that order. In Java, I agree, we’re just screwed. Of course, we could just pass in an object instead of a function, and if it implements some ICallableTransactionBody interface, <code>atomic</code> could call its <code>RunTx()</code> method. That would work, but it’s not quite as elegant.</p>

<p>In C++, though, the situation is different. A large part of the standard library (all the STL algorithms) relies on higher-order functions. .</p>

<p>In C++, we can define <code>atomic</code> as follows:</p>

<pre><code>template &lt;typename Func&gt;
void atomic(Func f){
  for (;;) {
    tx = new Transaction();
    try {
      f(tx);
      tx.Validate();
      tx.Commit();
    }
    catch (InvalidTxException){
      tx.Rollback();
    }
  }
}
</code></pre>

<p>First, I should mention that I cheated. I removed the return value. <code>atomic</code> now returns <code>void</code>. The reason is that it’s not quite trivial to infer the return type of a function in C++. <a href="http://en.wikipedia.org/wiki/C++_Technical_Report_1">TR1</a> does introduce a function <code>std::result_of</code> but it doesn’t work in every case. C++0x will fix this once and for all, by providing the necessary language features to implement it. But until then, we could hack around it, either by requiring the user to explicitly specify the return type (<code>atomic&lt;int&gt;(myTx)</code>), or by using <code>result_of</code> and simply telling the user to avoid the cases where it doesn’t work. But to keep the above code example simple, I simply removed the return type instead. But it <em>can</em> be solved, and my prototype does it.</p>

<p>Now, on with the show. We have now defined our <code>atomic</code> function. How do we call it?</p>

<p>In one of three ways, in decreasing order of familiarity:</p>

<p>First, with a function pointer. Hopefully every C++ programmer knows about these:</p>

<pre><code>void myTx(Tx tx) {... }
atomic(myTx);
</code></pre>

<p>Fairly clean and simple. One downside is that because the <code>atomic</code> function is just passed a function pointer, the compiler may not be able to inline the call to <code>myTx</code>. It is doubtful that it’d make much difference here, but it is nevertheless a valid concern.</p>

<p>The second approach fixes this, as well as enabling some more flexibility: Functors, or function objects, which every C++ programmer <em>should</em> know about, but unfortunately, not everyone does:</p>

<pre><code>struct myTx {
  void operator()(Tx tx) {...}
};
atomic(myTx());
</code></pre>

<p>Now we’re passing in an object of type <code>myTx</code>, which means that the compiler knows exactly which function to call: <code>myTx::operator()(Tx)</code>. So it can inline this trivially, eliminating the above performance concern.</p>

<p>Perhaps more importantly, our function can now hold state. It is an object, so it can be given a constructor and destructor. It can be given other member functions to allow the user to interact with it. It has become a lot more versatile.</p>

<p>Finally, we can wait for C++0x, which introduces lambda expressions:</p>

<pre><code>atomic([](Tx tx) { ...});
</code></pre>

<p>And what’s noteworthy is that the <em>same</em> <code>atomic</code> definition works in all three cases! So the user is actually given the choice of which method to use. Our STM library supports them all.</p>

<p>Isn’t this cleaner than the original approach, of requiring the user to implement the whole loop himself?
So why have no one used it before?</p>

<p>Ultimately, I can think of two possible reasons:</p>

<ul>
<li>the writers, usually very clever researchers and academics, were not familiar with higher-order functions, or with functional programming in general, or</li>
<li>the writers did not know that this was possible in C++</li>
</ul>

<p>I suppose the second option is the most forgiveable. C++ is a big messy language, and not everyone are familiar with every corner of it. That’s fair enough, but even then, most of this could have been done in C too, by using function pointers. It’s not <em>that</em> esoteric, is it? But I sincerely hope it is not the first case. I could understand if Joe Coder at Insignificant Software Inc. had never heard of functional programming, but most of these are computer science professors!</p>

<p>Incidentally, this should also provide a nice example for when an imperative programmer asks “how would it benefit me to learn a functional language?”</p>

<p>Here we have a clear example of how using a very simple functional technique can significantly clean up the code and make it much more robust<sup id="fnref:1"><a href="#fn:1" rel="footnote">1</a></sup>, even in an imperative language like C++.</p>

<div class="footnotes">
<hr />
<ol>

<li id="fn:1">
<p>Imagine what would have happened in the original loop– or macro-based versions, if the user had dared to put a <code>break</code> or <code>return</code> in their transaction… But both are safe in the higher-order function version. <a href="#fnref:1" rev="footnote">↩</a></p>
</li>

</ol>
</div>
]]></content:encoded>
			<wfw:commentRss>http://jalf.dk/blog/2009/10/being-functional-in-an-imperative-language/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Software Transactional Memory</title>
		<link>http://jalf.dk/blog/2009/09/software-transactional-memory/</link>
		<comments>http://jalf.dk/blog/2009/09/software-transactional-memory/#comments</comments>
		<pubDate>Sat, 26 Sep 2009 13:59:27 +0000</pubDate>
		<dc:creator>jalf</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[diku]]></category>
		<category><![CDATA[stm]]></category>
		<category><![CDATA[thesis]]></category>
		<category><![CDATA[transactional-memory]]></category>

		<guid isPermaLink="false">http://jalf.dk/blog/?p=323</guid>
		<description><![CDATA[As I mentioned a few weeks ago, I’m writing my master’s thesis on software transactional memory (STM). Of course it is still early in the process, and while I’ve got some ideas for my implementation, and have started prototyping parts of it, most of the time has been spent catching up on all the existing [...]]]></description>
			<content:encoded><![CDATA[<p>As I mentioned a<a href="http://jalf.dk/blog/2009/08/thesis-yay/"> few weeks ago</a>, I’m writing my master’s thesis on <a href="http://en.wikipedia.org/wiki/Software_transactional_memory">software transactional memory (STM)</a>.</p>

<p>Of course it is still early in the process, and while I’ve got some ideas for my implementation, and have started prototyping parts of it, most of the time has been spent catching up on all the existing work in the field.</p>

<p>Little did I know when I accepted the subject that there’d been written <em>that</em> many papers about it over the last decade. Phew… A commenter asked me for the references I found useful, so here’s an incomplete list:<span id="more-323"></span></p>

<p><a href="http://www.cs.wisc.edu/trans-memory/biblio/index.html"><strong>Tranactional Memory Bibliography</strong></a>: This site contains an up-to-date and extremely comprehensive list of references to everything written aobut STM. Just the sheer number of links out from that site is depressing, but it’s obviously a wonderful place to get an overview of what’s available.</p>

<p><a href="http://www.amazon.com/Transactional-Synthesis-Lectures-Computer-Architecture/dp/1598291246"><strong>Transactional Memory</strong></a>: I bought this book after noticing that almost every paper on STM referenced it. And it was money well spent. The book is well written, explains the subject clearly, and covers pretty much everything I wanted to know. It is split into three main chapters (not counting the introduction):</p>

<p>The first chapter discusses TM purely from a user’s perspective. What should the API look like, what do we, as programmers want or need from a TM implementation, what should the semantics be, and what extensions might be useful? It explains a number of important concepts, such as weak/strong isolation, serializability and linearizability, direct and deferred update and many others. But mostly, this chapter is fantastic as a discussion of what TM research should aim towards. Without getting bogged down in implementation details, it focuses simply on how we’d <em>like</em> it to work (and on the many open questions where we still don’t know which semantics would make sense or be most useful).</p>

<p>The second chapter is useful as a shortcut, allowing newcomers such as me to catch up on the last decade’s research. It summarizes all the important papers on STM, from the earliest precursors dating back to the 70’s, up to 2006 when the book was published. The various implementations and results are briefly described, in enough detail for me to understand the main ideas presented, and determine whether to look up the paper in question and read it in full.</p>

<p>The third chapter gives a similar treatment to Hardware Transactional Memory, and since that is outside the scope of my project, I’ve skipped lightly over this for now, although I may just read it out of curiosity later on. In short, it’s a great book for anyone trying to find their way in the jungle of TM papers.</p>

<p><a href="http://research.microsoft.com/en-us/um/people/simonpj/papers/stm/"><strong>Haskell STM</strong></a>: Simon Peyton Jones and others at Microsoft Research have done some truly impressive work on a STM implementation for Concurrent Haskell. While those of us working in a messy language like C++ can’t aspire for such a clean and elegant implementation, there are still many good ideas we can borrow.</p>

<p>And finally, a few individual papers I’ve gotten a lot out of:</p>

<p><a href="http://www.acc.ncku.edu.tw/chinese/faculty/shulc/courses/adb/transactional-memory/notlockfree.pdf"><strong>Software Transactional Memory Should Not be Obstruction-Free</strong></a>: As STM grew out of research into lock-free datastructures and distributed systems, the early work was downright obsessed about making transactions nonblocking. It took this paper to take a step back and ask whether this requirement actually makes sense <em>for the rest of us</em>. And guess what? It doesn’t. It complicates the STM implementations, and prevents some very useful optimizations.</p>

<p><a href="http://www.cs.tau.ac.il/~shanir/nir-pubs-web/Papers/TRANSACT06.pdf"><strong>What Really Makes Transactions Faster</strong></a> followed up on the above, discussing a number of other ways in which the performance of STM systems could be improved.</p>

<p>And finally, the earlier thesis that drew me into the subject:</p>

<p><a href="http://www.diku.dk/forskning/performance-engineering/Kasper/"><strong>A Software Transactional Memory Library for C++</strong></a>: This is the first implementation I’ve seen which seriously targets C++. Of course many prior C++ implementations exist, but they’ve generally relied on either C or Java idioms, resulting in an uglier, more error-prone interface, and the inability to perform a number of optimizations. This paper describes a proper modern C++ implementation, relying on generic programming, template metaprogramming and all the other tricks in a C++ programmer’s toolbox.</p>
]]></content:encoded>
			<wfw:commentRss>http://jalf.dk/blog/2009/09/software-transactional-memory/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Thesis, yay!</title>
		<link>http://jalf.dk/blog/2009/08/thesis-yay/</link>
		<comments>http://jalf.dk/blog/2009/08/thesis-yay/#comments</comments>
		<pubDate>Sat, 29 Aug 2009 17:59:25 +0000</pubDate>
		<dc:creator>jalf</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[diku]]></category>
		<category><![CDATA[stm]]></category>
		<category><![CDATA[thesis]]></category>

		<guid isPermaLink="false">http://jalf.dk/blog/?p=301</guid>
		<description><![CDATA[Been a bit quiet on the blog front lately. I’ve been busy getting my master’s thesis off the ground. But now it is official. I handed in all the paperwork (in six copies, what do they need all that paper for?), got the signature from my advisor and everything else yesterday. I have now officially [...]]]></description>
			<content:encoded><![CDATA[<p>Been a bit quiet on the blog front lately. I’ve been busy getting my master’s thesis off the ground.</p>

<p>But now it is official. <span id="more-301"></span>
I handed in all the paperwork (in six copies, what do they need all that paper for?), got the signature from my advisor and everything else yesterday. I have now <em>officially started on my thesis</em>. The subject?</p>

<p><strong>Implementing a <a href="http://en.wikipedia.org/wiki/Software_transactional_memory">Software Transactional Memory (STM)</a> library in <a href="http://en.wikipedia.org/wiki/C%2B%2B0x">C++0x</a></strong>. (Or rather, in C++, using whatever C++0x features current compilers have support for. In particular, I can see good uses of rvalue references and lambda expressions, which, luckily, the Visual C++ 2010 beta has support for.</p>

<p>And I am super psyched up about it. The subject seems a lot of fun. Of course I’ve got a lot of reading to do, catching up on all the existing research in the area (Two weeks ago, I had barely heard of STM before, so I can’t claim to be an expert on the field yet ;))</p>

<p>However, the C++0x aspect seems to be almost untouched in previous work<sup id="fnref:1"><a href="#fn:1" rel="footnote">1</a></sup>, which is obviously nice. Feels good to be able to contribute something <em>new</em>.</p>

<p>My deadline is March 1st, 2010, almost exactly 6 months from now.
I’m going to push myself to get a prototype up as early as possible. So the first few weeks are going to be busy. Once I get started properly, I’ll be able to vary the pace a bit more, taking more time out for the blog, work and everything else.</p>

<div class="footnotes">
<hr />
<ol>

<li id="fn:1">
<p>Unsurprisingly, as 1) most current research has been done by people who knew a lot about STM, and not so much about modern C++, 2) C++0x has seemed a long way off, and 3) compiler support for 0x features was almost nonexistent. <a href="#fnref:1" rev="footnote">↩</a></p>
</li>

</ol>
</div>
]]></content:encoded>
			<wfw:commentRss>http://jalf.dk/blog/2009/08/thesis-yay/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>

