Threading Perl with TBB - a white paper and CPAN module (threads::tbb)
Sam Vilain
sam at vilain.net
Mon May 9 23:11:58 UTC 2011
On 10/05/11 07:13, Leon Timmermans wrote:
> On Mon, May 9, 2011 at 6:30 PM, Sam Vilain <sam at vilain.net> wrote:
>> There is also an Erlang-style threading library on CPAN,
>> threads::lite,
>> more efficient and scalable but specific in functionality.
> I'm wondering what makes you say that it's specific in functionality?
It is not intended to be a negative criticism, and your module certainly
helped pave the way for my work and is elegantly simple. What I mean is
that it supports a particular type of parallelism - that where you have
a bunch of servers communicating by passing messages in queues. As
Erlang demonstrates this is a useful primitive to have and can be used
as a building block for a rich set of functionality.
However if the task at hand looks more like a data parallelism task,
then the server approach is not a natural fit. You would need to
structure the problem in terms of servers and message passing.
Achieving a real speed-up for this case would likely be challenging -
especially given the performance problems identified with more than 4
running interpreters/threads in the same program (caused by a typical
mutex on malloc/free).
By comparison, TBB provides a collection of algorithms aimed at the
problems which parallelise well, and each can be expected to be tunable
to give high performance.
>> along with a system of lazy cloning for state
> It would be pretty cool if this works out, but I must admit I'm a
> rather skeptical here. I avoided going that same path because it can
> blow up in your face far to easily. It sounds a little too much like 5005threads for my comfort.
It's certainly a very risky trade-off to make, and I did outline some
mitigation approaches. What it has in common with 5005threads is that
data structures written by one thread are read directly by another
thread; however the large difference is that the only function permitted
to do this is marked 'const'. When I did that, I found out just how
many macros I'd assumed were read-only, would actually modify the data
structure passed in under certain circumstances, and instead switched to
the read-only versions (eg, SvIVX instead of SvIV).
The trend towards read-only objects is another facet of Erlang and
functional programming in general which is slowly being assimilated by
at least parts of Perl culture. Arguably Erlang's scalability is due to
the fact that all data structures, once constructed, are "const" - as
well as the decoupling from operating system threads it uses.
While Perl cannot afford to completely redesign its entire state model
to match this, the module allows the user to code in this style. TBB
provides the thread decoupling in the form of tasks. So, it puts high
scalability within reach for Perl programs. If by some new feature, it
would be possible to mark SVs, AVs etc as (recursively) 'const' (or
thread-safe) then the approach would even be sound. As I recommend in
the paper, it would ideally be rationalised with the existing code which
implements Perl_clone(), the sv_dup magic vtable slot API, and the
CLONE_SKIP class API.
>> Enjoy and comments welcome,
> It looks pretty interesting. I'll be watching it.
Thanks for your feedback, and especially thanks for writing
threads::lite - I probably wouldn't have been able to pull this effort
off on time without it :-).
Cheers,
Sam
More information about the parrot-dev
mailing list