Building a multi-threaded program is much harder than building a single threaded one. You can't just spread on a little multi-threading to make it go faster.

Actually, often you can.

As a simplistic example, take the problem in Averaging Elements in Array of Array, and Ikegami's solution as a starting point. If you wrap the first (nested) loops of that solution up into a subroutine that returns the array of sums, then divides them to produce the averages, you have a single threaded solution that will run in time T.

Now assume you have a machine that has 4 cpus/cores. If you call that same subroutine, passing a subpartiton of the total dataset, 4 times--each time in a different thread:

#! perl -slw use strict; use threads; use threads::shared; sub sumEm{ my $tid = threads->tid; my( $AoARef, $first, $n ) = @_; print "$tid: $first - ", $first + $n - 1; my @sums = ( 0 ) x 4; for my $i ( $first .. $first+$n-1 ) { $sums[ $_ ] += $AoARef->[ $i ][ $_ ] for 0 .. 3; } return @sums; } our $N ||= 1e6; our $T ||= 4; my @AoA:shared; $#AoA = $N; for( 0 .. $N -1 ) { $AoA[ $_ ] = &share([]); @{ $AoA[ $_ ] } = map rand( 100 ), 1 .. $T; } my $perThread = int $N / $T; my @threads = map{ threads->create( \&sumEm, \@AoA, $_* $perThread, $perThread ) } 0 .. $T-1; my @aves = ( 0 ) x 4; for my $thread ( @threads ) { my @subtotals = $thread->join; $aves[ $_ ] += $subtotals[ $_ ] for 0 .. 3; } $_ /= $N for @aves; print "@aves";

The chances are that it will arrive at the same answer in something less than T/3 the time. (I don't have a quad cpu machine on which to prove this!).

I've seen many projects collapse from underestimating the work involved.

I'm gonna make three (totally hairy-arsed, unfounded, speculative) guesses about those projects:

Threads needn't be hard. Certainly no harder, and often easier than forking. There are a raft of commonly cited, difficult to solve, problems that bug threading. And there has been much research, many papers, and a raft-squared proposed, usually complex, or (computationally and/or algorithmically) costly solutions to those problems.

But the simplest solution is invariably overlooked: avoid them!

That is, rather than trying to come up with complex and costly solutions to the well-known problems with locking, sharing and synchronisation--just don't use algorithms and techniques that require them.

iThreads are a damn good start down that road.


Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.
"Too many [] have been sedated by an oppressive environment of political correctness and risk aversion."

In reply to Re^2: Multithreading, how to? by BrowserUk
in thread Multithreading, how to? by iSina

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.