Lightweight Concurrency: Threads are on a Diet

Most modern programming languages are adding lightweight concurrency capabilities. Why is this happening? It is a response to the multicore revolution. You need more parallelism in your applications and you need it without adding a great overhead.

"Sugar threads or diet threads? Task-based programming, please."

James Reinders offered eight key rules for multicore programming based on parallel programming experiences of his own and others. I have been successfully following his eight key rules with just a few minor changes. I needed some changes a few times in order to adapt the rules to the limitations imposed by each different framework and programming language.

His rule #3 suggests programming in tasks (chores), not threads (cores). However, many popular high-level programming languages have been offering threads to support concurrency. Therefore, in order to follow this rule in C# 3.0 or Java 6, you needed to create your own abstraction layer, handling the thread/core management for you. This was indeed complex.

Furthermore, there is another big problem. Threads are expensive. Creating a new thread requires processing time and additional memory. For this reason, there is an important overhead when using threads. If the overhead introduced is equal or greater than the benefit, it will encourage you to avoid threads.

You can use a pool of threads, to avoid creating new threads for each new task to be distributed into different cores. However, this technique can transform a simple task-based algorithm into many complex synchronization activities to distribute each task to a different thread. A nice object-oriented design combined with a thread management layer can add exciting task-based capabilities to your existing frameworks. It is a bit complex to achieve this goal in C# 3.0, as shown in an article I had published a few months ago "Simplifying Parallelism Complexity in C#".

In the last two years, most modern programming languages began adding lightweight concurrency capabilities. They are offering different ways of creating tasks (chores) and not threads (cores). They are adding features that simplify developers to follow James Reinders' rule #3.

On the one hand, you can take advantage of multi-core using processes and threads. These are known as heavyweight concurrency. On the other hand, you can work with tasks, fibers and messages between actors, known as lightweight concurrency.

Functional programming is back because it offers an excellent alternative to create task-based algorithms with lightweight concurrency mechanisms.

.Net Framework 4.0 with its Parallel Extensions, still in Beta 1, adds the possibility to work with Tasks. It replaces the old heavyweight treading model with a new lightweight threading model. Tasks add less overhead than threads.

Apple's Grand Central Dispatch, explained by James Reinders a few days ago (http://www.ddj.com/go-parallel/blog/archives/2009/06/apples_grand_ce.html) also takes the lightweight concurrency route.

Ruby 1.9 added Fibers, lightweight concurrency to favor a task-based approach.

GParallelizer adds lightweight concurrency to Groovy, running on the JVM.

The aforementioned moves to lightweight concurrency are just a few examples. There are many other frameworks and languages adding lightweight concurrency capabilities.

The lightweight concurrency era is the next step to evolve programming languages to the multi-core age. This will allow applications to take advantage of more than 8 cores using task-based algorithms and reducing the concurrency overhead. The language architects are working hard to help you follow James Reinders' rule #3.

Real World Parallelism Webinar Series
  • November 17, 2009
    Visual Effects for Animation - presented by DreamWorks Animation
    Speaker: Ron Henderson (Bio)

    Ron Henderson manages the FX Tools group at DreamWorks Animation, where he is responsible for developing physical simulation and procedural modeling tools. These systems have been used for key visual effects in recent films such as Kung Fu Panda and Monsters vs. Aliens (March 2009).

    Prior to joining DreamWorks in 2002 he was a senior scientist at Caltech with a joint appointment to the Applied Math and Aeronautics departments, where he worked on efficient techniques for the direct numerical simulation of fluid turbulence.

    Abstract:
    In this webinar, Ron Henderson will show examples of visual effects, from hair and feathers to smoke and fire, from a variety of DreamWorks Animation feature films. He will discuss in general terms the kinds of techniques used to achieve particular visual effects. Finally, Henderson will show a detailed breakdown of the dam-breaking scene from Madagascar: Escape 2 Africa, demonstrating how different elements of key frame animation, simulation, and rendering are combined in a real production shot.

  • December 1, 2009
    A Quick and Easy Way to Parallelize a Legacy Codebase with Intel® Threading Building Blocks (TBBs)
    Speaker: Bernard Laberge, Avid, Senior Principal Engineer (Bio)

    Bernard Laberge is a senior principal engineer in the video editors division at Avid. During his seven years with the company he has been actively involved in the replacement of the legacy video processing engines used by Avid editors with a common hardware-abstracted, component-based video processing engine currently running on the CPU with SIMD optimized code, GPU, and dedicated hardware.

    Abstract:
    Learn how to overcome the limitations of a thread-based scheduler, including dealing with the absence of recursive parallelism support and the inefficient handling of unbalanced processing load. Bernard Laberge addresses how Avid resolved the expensive refactoring of their thread-based scheduler into a task-based solution by choosing Intel® Threading Building Blocks (TBBs). He explores how Avid was able to easily integrate the Intel TBBs into their video editor applications and more than 5 million lines of code.

  • December 15, 2009
    How to Use Intel® Parallel Studio to Streamline Code Development in a Multicore Environment
    Speaker: Matt Dunbar, Director for Performance Technology, SIMULIA (Bio)

    Matt Dunbar is the director for performance technology at SIMULIA. Since joining the company in 1993, he has worked on parallelization of the Abaqus suite of products, initially for shared memory architectures and more recently for distributed memory architectures. Dunbar has also been intimately involved in selecting both the hardware and software tools used in the development of the Abaqus product line.

    Abstract:
    Resolve elusive, costly multithreading errors quickly and efficiently with Intel® Parallel Studio. While many coding problems that lead to bugs in software applications are typically straightforward logic errors, errors in managing memory and in multithreading code can sometimes take weeks to months to diagnose and fix. Matt Dunbar explores how and why taking advantage of multicore processors through multithreaded code is critical for compute-intensive applications. While spotlighting his work on SIMULIA's Abaqus finite element solver, Dunbar addresses the need for multicore execution and shares his experiences using Intel Parallel Studio to streamline code development in a multicore environment.