<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://www.atalasoft.com/cs/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Rick Minerich's Development Wonderland : programming, ideas, concurrency</title><link>http://www.atalasoft.com/cs/blogs/rickm/archive/tags/programming/ideas/concurrency/default.aspx</link><description>Tags: programming, ideas, concurrency</description><dc:language>en</dc:language><generator>CommunityServer 2.1 SP1 (Build: 61025.2)</generator><item><title>How will you parallelize your existing codebase? Try R.A.S.P.</title><link>http://www.atalasoft.com/cs/blogs/rickm/archive/2008/12/23/how-will-you-parallelize-your-existing-codebase-try-r-a-s-p.aspx</link><pubDate>Tue, 23 Dec 2008 21:23:00 GMT</pubDate><guid isPermaLink="false">647108ca-f046-4d8d-9feb-a7fbd2049b37:16964</guid><dc:creator>RickM</dc:creator><slash:comments>6</slash:comments><comments>http://www.atalasoft.com/cs/blogs/rickm/comments/16964.aspx</comments><wfw:commentRss>http://www.atalasoft.com/cs/blogs/rickm/commentrss.aspx?PostID=16964</wfw:commentRss><wfw:comment>http://www.atalasoft.com/cs/blogs/rickm/rsscomments.aspx?PostID=16964</wfw:comment><description>&lt;p&gt;There has been much talk of how we will be writing all of our new code with parallelization in mind.&amp;#160; However, what of our existing code?&amp;#160; It’s unlikely that everyone will just suddenly dump decades of existing code and write everything from scratch.&amp;#160; In this article I’m going to provide a simple methodology for how we might deal with the ever building problem of parallelizing our existing mountains of code.&amp;#160; Comments and contributions are welcome.&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;h3&gt;Methodologies of the Past&lt;/h3&gt;  &lt;p&gt;From the STL to .NET, the frameworks we have constructed our applications around have been heavily dependent on the idea of an application having a single thread.&amp;#160; Given that the foundation of what we have all been using for a very long time was constructed around this preconception, it’s unreasonable to expect that much of our existing code will ever be fully parallel.&amp;#160; Even those that wrote code on top of a thread safe framework may find that years of patches and and poor design decisions make ground up parallelization impossible.&lt;/p&gt;  &lt;p&gt;If we set our expectations reasonably, we see that we should instead focus on leveraging parallelism to improve the performance of the slowest parts of our software.&amp;#160; From this viewpoint, parallelization is an optimization problem.&amp;#160; Like all optimization, the difficulty of parallelizing code will have much to do with the methodologies which were used to write it.&amp;#160;&amp;#160; &lt;/p&gt;  &lt;p&gt;Of course, object-oriented code being written with modern &lt;a href="http://butunclebob.com/ArticleS.UncleBob.PrinciplesOfOod"&gt;S.O.L.I.D. principles&lt;/a&gt; and will be easier to parallelize than older procedural code.&amp;#160; At the same time, a poorly organized codebase or poorly written code will always make change difficult and so also hard to parallelize.&amp;#160; This is why well written code is worth the investment.&amp;#160; We will see the investment paying off in spades for the companies who have bothered to care about code quality.&amp;#160; Others who find they have spaghetti code under the hood will find they will need to deeply segregate and modularize before parallelization is possible.&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;h3&gt;A Methodology for Revisiting the Past&lt;/h3&gt;  &lt;p&gt;In most cases it would be a poor choice to implement your own threading API.&amp;#160; Efficient and easy to use parallelization APIs are coming to (or already part of) every commonly used language and framework.&amp;#160; Most of these APIs are not only built on top of &lt;a href="http://lacl.univ-paris12.fr/gava/PAPP2009/"&gt;years of research&lt;/a&gt;, they also have been written and debugged by a large number of people with specific expertise.&amp;#160; These APIs are a godsend because they will allow most developers to parallelize existing software with a minimum amount of pain.&amp;#160; The parallelization of existing code bases will be much the same as any other kind of performance tuning.&amp;#160; &lt;/p&gt;  &lt;p&gt;The key will be using a profiler to identify places in the code that would be sped up by parallelization and leveraging these new APIs to take advantage of the available hardware.&amp;#160; The exciting part is that this can be done with any existing profiler and many existing APIs.&amp;#160; The unfortunate part is that because memory sharing is such a big issue, parallelization requires a degree of separation beyond other types of optimization and so is likely to require some amount of refactoring.&lt;/p&gt;  &lt;p&gt;Not all types of performance problems are conducive to being solved by parallelization, careful evaluation of the problem at hand is required.&amp;#160; Also, as with anything that requires significant code change, building a solid test fixture is key to introducing as few bugs as possible.&amp;#160; By leveraging the ideas of &lt;a href="http://prematureoptimization.org/blog/archives/26"&gt;avoiding premature optimization&lt;/a&gt;, &lt;a href="http://dotnet.sys-con.com/node/133775"&gt;pragmatic unit testing&lt;/a&gt;, &lt;a href="http://msdn.microsoft.com/en-us/magazine/cc163329.aspx"&gt;using existing APIs&lt;/a&gt;, and &lt;a href="http://www.ryanlowe.ca/blog/archives/001048_be_mindful_of_code_entropy.php"&gt;mindful refactoring&lt;/a&gt; it will be possible to introduce parallelization into many already existing projects with a manageable amount of risk.&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;h3&gt;What is RASP?&lt;/h3&gt;  &lt;p&gt;While not included in the acronym, the first step in any kind of optimization is &lt;strong&gt;&lt;a href="http://en.wikipedia.org/wiki/Performance_analysis"&gt;profiling&lt;/a&gt;&lt;/strong&gt;.&amp;#160; Before you can begin to parallelize your code, you must determine where the bottlenecks might be.&amp;#160; A broadly defined list of parallelizable things to look for would be, to quote &lt;a href="http://loufranco.com/blog/files/20-Days-of-Clojure-Day-12.html"&gt;Rich Hickey&lt;/a&gt;, “independent data/work, moderate-to-course-grained work units and/or complex coordination logic that would be simplified with threads”.&amp;#160; A couple quick examples of low hanging fruit to be on the lookout for would be slow iterative loops and blocking I/O.&amp;#160; It is important to note that as a general guideline it would be wrong to parallelize anything if it would not significantly increase the speed of your software.&lt;/p&gt;  &lt;p&gt;For each of the bottlenecks found while profiling, parallelization is best separated into four steps:&lt;/p&gt;  &lt;table cellspacing="0" cellpadding="2"&gt;     &lt;tr&gt;       &lt;td&gt;&lt;strong&gt;Review&lt;/strong&gt;: &lt;/td&gt;        &lt;td&gt;Review code to determine if it is a good candidate for parallelization.&lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;td&gt;&lt;strong&gt;Anchor:&lt;/strong&gt;&lt;/td&gt;        &lt;td&gt;Create a unit test fixture to ensure that the behavior of the to be parallelized code does not change.&lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;td&gt;&lt;strong&gt;Separate&lt;/strong&gt;: &lt;/td&gt;        &lt;td&gt;Ensure that the to be parallelized code has no shared memory constraints.&lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;td&gt;&lt;strong&gt;Parallelize&lt;/strong&gt;: &lt;/td&gt;        &lt;td&gt;Minimally refactor for parallelization while leveraging an existing API to do the heavy lifting.&lt;/td&gt;     &lt;/tr&gt;   &lt;/table&gt;  &lt;p&gt;As the specifics of what each of these would entail depends greatly on exactly which platform and language is in use, I will not go into them deeply now.&amp;#160; Overall, it’s a simple methodology but I think both sufficient for the task at hand and broadly applicable.&amp;#160; &lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;h3&gt;Conclusion &lt;/h3&gt;  &lt;p&gt;Review, Anchor, Separate, Parallelize.&amp;#160; It’s not intended to be a difficult concept but instead to provide a simple path to parallelization.&amp;#160; I would be very interested in hearing about any opinions on what RASP might be missing or how it may be better clarified.&amp;#160; While I didn’t have time to discuss them deeply in this post, &lt;a href="http://www.cs.uiuc.edu/homes/snir/PPP/"&gt;parallelization patterns&lt;/a&gt; are also a key concept in using RASP as if you can’t easily identify what can be parallelized than it would be impossible to use any parallelization methodology.&amp;#160; In the future I hope flush out RASP further as well as discuss parallelization patterns in depth.&lt;/p&gt;
&lt;div class = "shareblock"&gt;&lt;strong&gt;Share this post:&lt;/strong&gt; &lt;a href = "mailto:?body=Thought you might like this: http://www.atalasoft.com/cs/blogs/rickm/archive/2008/12/23/how-will-you-parallelize-your-existing-codebase-try-r-a-s-p.aspx&amp;amp;;subject=How+will+you+parallelize+your+existing+codebase%3f+Try+R.A.S.P." target="_blank" title = "Post http://www.atalasoft.com/cs/blogs/rickm/archive/2008/12/23/how-will-you-parallelize-your-existing-codebase-try-r-a-s-p.aspx"&gt;email it!&lt;/a&gt; |  &lt;a href = "http://del.icio.us/post?url=http://www.atalasoft.com/cs/blogs/rickm/archive/2008/12/23/how-will-you-parallelize-your-existing-codebase-try-r-a-s-p.aspx&amp;amp;;title=How+will+you+parallelize+your+existing+codebase%3f+Try+R.A.S.P." target="_blank" title = "Post http://www.atalasoft.com/cs/blogs/rickm/archive/2008/12/23/how-will-you-parallelize-your-existing-codebase-try-r-a-s-p.aspx"&gt;bookmark it!&lt;/a&gt; |  &lt;a href = "http://www.digg.com/submit?url=http://www.atalasoft.com/cs/blogs/rickm/archive/2008/12/23/how-will-you-parallelize-your-existing-codebase-try-r-a-s-p.aspx&amp;amp;;phase=2" target="_blank" title = "Post http://www.atalasoft.com/cs/blogs/rickm/archive/2008/12/23/how-will-you-parallelize-your-existing-codebase-try-r-a-s-p.aspx"&gt;digg it!&lt;/a&gt; |  &lt;a href = "http://reddit.com/submit?url=http://www.atalasoft.com/cs/blogs/rickm/archive/2008/12/23/how-will-you-parallelize-your-existing-codebase-try-r-a-s-p.aspx&amp;amp;title=How+will+you+parallelize+your+existing+codebase%3f+Try+R.A.S.P." target="_blank" title = "Post http://www.atalasoft.com/cs/blogs/rickm/archive/2008/12/23/how-will-you-parallelize-your-existing-codebase-try-r-a-s-p.aspx"&gt;reddit!&lt;/a&gt; |  &lt;a href = "http://www.dotnetkicks.com/submit/?url=http://www.atalasoft.com/cs/blogs/rickm/archive/2008/12/23/how-will-you-parallelize-your-existing-codebase-try-r-a-s-p.aspx&amp;amp;;title=How+will+you+parallelize+your+existing+codebase%3f+Try+R.A.S.P." target="_blank" title = "Post http://www.atalasoft.com/cs/blogs/rickm/archive/2008/12/23/how-will-you-parallelize-your-existing-codebase-try-r-a-s-p.aspx"&gt;kick it!&lt;/a&gt; |  &lt;a href = "https://favorites.live.com/quickadd.aspx?marklet=1&amp;amp;;mkt=en-us&amp;amp;;url=http://www.atalasoft.com/cs/blogs/rickm/archive/2008/12/23/how-will-you-parallelize-your-existing-codebase-try-r-a-s-p.aspx&amp;amp;;title=How+will+you+parallelize+your+existing+codebase%3f+Try+R.A.S.P.&amp;amp;;top=1" target="_blank" title = "Post http://www.atalasoft.com/cs/blogs/rickm/archive/2008/12/23/how-will-you-parallelize-your-existing-codebase-try-r-a-s-p.aspx"&gt;live it!&lt;/a&gt;&lt;/div&gt;&lt;img src="http://www.atalasoft.com/cs/aggbug.aspx?PostID=16964" width="1" height="1"&gt;</description><category domain="http://www.atalasoft.com/cs/blogs/rickm/archive/tags/programming/default.aspx">programming</category><category domain="http://www.atalasoft.com/cs/blogs/rickm/archive/tags/concurrency/default.aspx">concurrency</category><category domain="http://www.atalasoft.com/cs/blogs/rickm/archive/tags/object+oriented/default.aspx">object oriented</category><category domain="http://www.atalasoft.com/cs/blogs/rickm/archive/tags/ideas/default.aspx">ideas</category><category domain="http://www.atalasoft.com/cs/blogs/rickm/archive/tags/optimization/default.aspx">optimization</category><category domain="http://www.atalasoft.com/cs/blogs/rickm/archive/tags/profiling/default.aspx">profiling</category><category domain="http://www.atalasoft.com/cs/blogs/rickm/archive/tags/procedural/default.aspx">procedural</category><category domain="http://www.atalasoft.com/cs/blogs/rickm/archive/tags/RASP/default.aspx">RASP</category></item></channel></rss>