<?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>Reallywow &#187; parallel</title>
	<atom:link href="http://blog.reallywow.com/archives/category/parallel/feed" rel="self" type="application/rss+xml" />
	<link>http://blog.reallywow.com</link>
	<description>Really? Wow... That's Reallywow</description>
	<lastBuildDate>Thu, 27 May 2010 15:02:55 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=abc</generator>
		<item>
		<title>Basic Block Data Decomposition in Perl</title>
		<link>http://blog.reallywow.com/archives/65</link>
		<comments>http://blog.reallywow.com/archives/65#comments</comments>
		<pubDate>Tue, 03 Mar 2009 21:28:19 +0000</pubDate>
		<dc:creator>lbjay</dc:creator>
				<category><![CDATA[code]]></category>
		<category><![CDATA[hacks]]></category>
		<category><![CDATA[parallel]]></category>
		<category><![CDATA[perl parallel threads]]></category>

		<guid isPermaLink="false">http://blog.reallywow.com/?p=65</guid>
		<description><![CDATA[I was playing around with the idea of parallelizing something the other day to eke out some performance. Unfortunately, I&#8217;ve gotten a bit rusty since writing some MPI code for a parallel computing course a few years back. I got stuck on what should be the simple part of dividing up my input across the [...]]]></description>
			<content:encoded><![CDATA[<abbr class="unapi-id" title="http://blog.reallywow.com/?p=65"><!-- &nbsp; --></abbr>
<p>I was playing around with the idea of parallelizing something the other day to eke out some performance. Unfortunately, I&#8217;ve gotten a bit rusty since writing some MPI code for a parallel computing course a few years back. I got stuck on what should be the simple part of dividing up my input across the threads.</p>
<p><span id="more-65"></span>The goal is to divvy things up into continguous blocks of roughly equal size. i.e., if the size of your input is 38 (<em>n</em>) and you start four threads (<em>p</em>) you don&#8217;t want to give the first three threads chunks of 12 and the last thread gets 2. You want slices of 10, 9, 10 and 9.</p>
<p>So I flailed away with loops and the POSIX::floor for little awhile and came pretty close to what I remembered. I had to finally drag out my <a href="http://books.google.com/books?id=tDxNyGSXg5IC">textbook</a> (and translate from the C Macros) to get it right.</p>
<pre lang="perl">#!/usr/bin/perl

# Block Data Decomposition:
# Divide array n into p contiguous blocks of roughly equal size

use POSIX qw(floor);
use strict;

sub block_start {
    my ($i, $p, $n) = @_;
    return floor(($i * $n) / $p);
}

sub block_end {
    my ($i, $p, $n) = @_;
    return (block_start($i + 1, $p, $n) - 1);
}

my @input = get_input();
my $n = scalar @input;
my $p = 4;

for my $i (0..$p-1)
{
    my $start = block_start($i, $p, $n);
    my $end = block_end($i, $p, $n);
    my @range = @input[$start..$end];
    do_something(\@range);
}</pre>
<p>The idea is that</p>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">do_something(\@range)</div></div>
<p>sends a slice of input off for processing by one of your threads. A pretty useful algorithm when doing this sort of thing. Certainly not rocket science. Which is why we should all be happy I&#8217;m not a rocket scientist.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.reallywow.com/archives/65/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>
