Book on PPL is now available

For those of you who may be interested in concurrency and parallelism using Microsoft tools, there’s a new book now available on the Visual C++ 2010 Parallel Patterns Library (PPL). I hope you enjoy it.

Normally I don’t write about other people’s platform-specific books, but I happened to be involved in the design of PPL, thought the book was nicely done, and contributed a Foreword. Here’s what I wrote to introduce the book:

This timely book comes as we navigate a major turning point in our industry: parallel hardware + mobile devices = the pocket supercomputer as the mainstream platform for the next 20 years.

Parallel applications are increasingly needed to exploit all kinds of target hardware. As I write this, getting full computational performance out of most machines—nearly all desktops and laptops, most game consoles, and the newest smartphones—already means harnessing local parallel hardware, mainly in the form of multicore CPU processing; this is the commoditization of the supercomputer. Increasingly in the coming years, getting that full performance will also mean using gradually ever-more-heterogeneous processing, from local general-purpose computation on graphics processing units (GPGPU) flavors to harnessing “often-on” remote parallel computing power in the form of elastic compute clouds; this is the generalization of the heterogeneous cluster in all its NUMA glory, with instantiations ranging from on-die to on-machine to on-cloud, with early examples of each kind already available in the wild.

Starting now and for the foreseeable future, for compute-bound applications, “fast” will be synonymous not just with “parallel,” but with “scalably parallel.” Only scalably parallel applications that can be shipped with lots of latent concurrency beyond what can be exploited in this year’s mainstream machines will be able to enjoy the new Free Lunch of getting substantially faster when today’s binaries can be installed and blossom on tomorrow’s hardware that will have more parallelism.

Visual C++ 2010 with its Parallel Patterns Library (PPL), described in this book, helps enable applications to take the first steps down this new path as it continues to unfold. During the design of PPL, many people did a lot of heavy lifting. For my part, I was glad to be able to contribute the heavy emphasis on lambda functions as the key central language extension that enabled the rest of PPL to be built as Standard Template Library (STL)-like algorithms implemented as a normal library. We could instead have built a half-dozen new kinds of special-purpose parallel loops into the language itself (and almost did), but that would have been terribly invasive and non-general. Adding a single general-purpose language feature like lambdas that can be used everywhere, including with PPL but not limited to only that, is vastly superior to baking special cases into the language.

The good news is that, in large parts of the world, we have as an industry already achieved pervasive computing: the vision of putting a computer on every desk, in every living room, and in everyone’s pocket. But now we are in the process of delivering pervasive and even elastic supercomputing: putting a supercomputer on every desk, in every living room, and in everyone’s pocket, with both local and non-local resources. In 1984, when I was just finishing high school, the world’s fastest computer was a Cray X-MP with four processors, 128MB of RAM, and peak performance of 942MFLOPS—or, put another way, a fraction of the parallelism, memory, and computational power of a 2005 vintage Xbox, never mind modern “phones” and Kinect. We’ve come a long way, and the pace of change is not only still strong, but still accelerating.

The industry turn to parallelism that has begun with multicore CPUs (for the reasons I outlined a few years ago in my essay “The Free Lunch Is Over”) will continue to be accelerated by GPGPU computing, elastic cloud computing, and other new and fundamentally parallel trends that deliver vast amounts of new computational power in forms that will become increasingly available to us through our mainstream programming languages. At Microsoft, we’re very happy to be able to be part of delivering this and future generations of tools for mainstream parallel computing across the industry. With PPL in particular, I’m very pleased to see how well the final product has turned out and look forward to seeing its capabilities continue to grow as we re-enable the new Free Lunch applications—scalable parallel applications ready for our next 20 years.

Herb Sutter
Principal Architect, Microsoft
Bellevue, WA, USA

February 2011

 

Interview on Channel 9

Over the holidays, Erik Meijer interviewed me on Channel 9. We covered a wide variety of topics, mostly centered on C++ with some straying into C#/Java/Haskell/Clojure/Erlang, but ranging from auto and closures to why (not?) derive future<T> from T, and from what the two most important problems in parallelism are in 2011 to why and how to taste new programming languages regularly. I think it turned out well. Enjoy!

2010: Cyberpunk World

Speaking as a neutral observer with exactly zero opinion on any political question, and not even a cyberpunk reader given that I’ve read about two such novels in my life: Is it just me, or do the last few months’ global news headlines read like they were ghostwritten by Neal Stephenson?

I wonder if we may look back on 2010 as the year it became widely understood that we now live in a cyberpunk world. Many of 2010’s top stories read like sci-fi:

  • Stuxnet: Sovereign nations (apparently) carry out successful attacks on each other with surgically crafted malware — viruses and worms that target specific nuclear facilities, possibly causing more damage and delay to their targets’ weapons programs than might have been achieved with a conventional military strike.
  • Wikileaks: Stateless ‘Net organizations operating outside national laws fight information battles with major governments, up to and including the strongest industrial and military superpowers. The governments react by applying political pressure to powerful multinational corporations to try to force the stateless organizations off the ‘Net and cut off their support and funding, but these efforts succeed only temporarily as the target keeps moving and reappearing.
  • Anonymous: Small vigilante groups of private cybergunners retaliate by (or just latch onto a handy excuse to go) carrying out global attacks on the websites of multinational corporations, inflicting enough damage on Visa and Mastercard to temporarily take them off the ‘Net, while being repelled by cyberfortresses like Amazon and Paypal that have stronger digital defenses. But before we get too confident about Amazon’s strength, remember that this definitely ain’t the biggest attack they’ll ever see, just a 21st-century-cyberwar hors d’oeuvre: Who were these global attackers? About 100 people, many of them teenagers.
  • Assange: Charismatic cyberpersonalities operating principally on the ‘Net live as permanent residents of no nation, and roam the world (until arrested) wherever they can jack in, amid calls for their arrest and/or assassination.
  • Kinect: Your benevolent (you hope) living room game console can see you. Insert obligatory Minority Report UIs no longer sci-fi line here, with optional reference to Nineteen Eighty-Four.
  • Other: Never mind that organized crime has for years now been well-known to be behind much of the phishing, spam, card skimming, and other electronic and ‘Net crimes. Not new to 2010, but seeing a significant uptick in the continued transition from boutique crime to serious organization and spear-phishing targeting specific high-profile organizations including the U.S. military.

Over the coming months and years, it will be interesting to see how multinational corporations and sovereign governments react to what some of them no doubt view as a new stateless — transnational? extranational? supernational? — and therefore global threat to their normal way of doing business.

Trip Report: November 2010 C++ Standards Meeting

The fall 2010 ISO C++ meeting was held on November 8-13 in Batavia, IL, USA. The post-meeting mailing is now live, including meeting minutes and other information.

I attended this meeting virtually, as I was still recovering from some shoulder surgery. Fermilab’s teleconference facilities are excellent — I think it’s safe to say they’re the best I’ve ever used, and it was very helpful for several of us telecommuters to participate actively in the key design discussions below.

Where are we in the process?

For this and the next meeting (Madrid in March), the committee is doing ballot resolution and dealing with national body comments. Because we issued a Final Committee Draft, ISO C++0x can officially no longer add new features. All that we can do is address national body comments, and make any other bug fixes we find.

Things are going well and we are on track to complete the Final Draft International Standard (FDIS) for the C++0x standard after the Madrid meeting in March. If that happens and that ballot succeeds, the C++0x standard will be published in 2011. If it turns out we need another meeting to be able to handle the last of the comment tail, the fallback will be to target FDIS at the following meeting (Indiana in August).

For about five years now we’ve been having three six-day meetings a year, besides smaller unofficial subgroup meetings in person or by teleconference in between official meetings. Because things are going well, we also decided to scale back to two five-day meetings a year starting in 2011, which is what we had after C++98 shipped until work on C++0x began in earnest.

Key design decisions at this meeting

Note: For the first item, I’ll repeat much of the text from the August meeting trip report to make this a standalone post.

Attributes: alignment, noreturn, and virtual control. As reported in the previous trip report, my personal hot button for these past two meetings was that C++0x syntax for override control in particular not look like the following example:

class [[base_check]] Derived : public Base {
public:
  virtual void f [[override]] ();
  virtual double g [[final]] ( int );
  virtual void h [[hiding]] ();
};

The committee has now decided replace these attributes with keywords. The two main options for keywords were:

  • fully reserved words, which can break existing user code that uses the words as variable or type names and so would mean we need to pick uglier names; and
  • contextual keywords as done in C++/CLI, which does not break existing user code and so lets us pick the ideal nice names, but which would be the first contextual keywords in ISO C++ (and there’s always resistance to being the first).

It was decided to follow the latter — contextual keywords pretty much as used in C++/CLI, down to renaming [[hiding]] as new. Here’s the paper with the wording changes, and here’s how the above example now looks:

class Derived explicit : public Base {
public:
  virtual void f () override;
  virtual double g( int ) final;
  virtual void h() new;
};

The other two kinds of attributes that we considered changing to keywords were [[align]] to specify the alignment of a type, and [[noreturn]] to specify that a function never return. The committee confirmed the decision reported as tentative in the previous trip report, namely to change [[align]] to a keyword and leave [[noreturn]] alone.

With these changes, the only two standard attributes are [[noreturn]] and [[carries_dependency]].

noexcept, part 1: Destructor and delete operators noexcept by default. This tentative resolution from Rapperswil was mostly adopted. Briefly, every destructor will be noexcept by default unless a member or base destructor is noexcept(false); you can of course still explicitly override the default and write noexcept(false) on any destructor. This means that the vast majority of classes should be noexcept. The papers containing the exact wording changes are here (destructors) and here (delete operators). For detailed rationale and ranting about why this is a Good Thing, see the previous trip report.

noexcept, part 2: noexcept now partly applied to the standard library. We also passed the first set of papers to apply noexcept to the standard library, including changing functions marked throw() or specified “Throws: Nothing” to be marked noexcept, and removing non-empty exception specifications from the standard library.

Restricting implicit move generation. A hot topic going into this meeting was that move operations could be generated implicitly for an existing type in ways that would be surprising and incorrect (i.e., break the invariants that should hold on all objects of that type). The previous rule was to generate a move constructor and move assignment operator implicitly if the class had no user-declared copy constructor and the move constructor would not be implicitly defined as deleted. Several options were discussed at length, including not generating move implicitly at all. In the end, the decision was that implicit move was both desirable but needed to be generated less aggressively to ensure correctness, and so now the rule is to generate a move constructor and move assignment operator implicitly if the class had no user-declared copy constructor and the move constructor would not be implicitly defined as deleted (same as before) and the class has no user-declared copy or move assignment operator or user-declared destructor. Here’s the paper with the wording changes.

Looking forward

Finally, here are the scheduled dates and locations for next year’s ISO C++ standards committee meetings:

  • March 21-26, 2011: Madrid, Spain
  • August 15-19, 2011: Bloomington, IN, USA

Herb

PDC Languages Panel and (Shortened) Lambdas Talk

At PDC 2010 this week, I participated in a panel and gave one talk. Both are now online for live on-demand viewing.

Note: The talks should work on any browser. They do not require Silverlight. If you get a message that Silverlight is needed, it just made a mistake in auto-detecting your browser (I’m told this happens with Firefox sometimes), so just click on one of the alternate formats and Bob’s your uncle.

Here they are:

1. Languages Panel (update: new link)

I got to participate again this year on a fun panel on programming languages and software development, together with fellow panelists Anders Hejlsberg (creator of Turbo Pascal, Delphi, and C#), Gilad Bracha (of Java and Newspeak fame), and Mark S. Miller (of Ecmascript and E and general security fame). Our esteemed moderator, Erik Meijer, is one of the original designers of LINQ in C# and has contributed heavily to many other languages.

2. Lambdas, Lambdas Everywhere (update: new link)

This is a shortened version of the full lambdas talk I gave at C&B on Wednesday, and will be giving again at C++ and Beyond Encore this December. To get the full talk, come to C&B Encore… but to get a good chunk of it, check out this version online.

Note: Just before the talk, I went around the room to chat with individual attendees, and discovered that a lot of the audience members were C# programmers who didn’t realize this was a C++ talk. Just before we went live, I spoke up to the room and alerted everywhere about that, and nearly everyone stayed anyway, which was nice. But if you wonder why I mentioned C# lambdas a number of times to this audience, that’s why.

C++0x Current Hot Issues

Anthony Williams has posted an excellent summary of the two major language design questions going into next month’s ISO C++ meeting in Batavia, IL, USA.

As we wind down C++0x, we are still working on an ever-decreasing set of open issues. Unsurprisingly, they’re in the newest features, as we bake them and see how they integrate with the rest of the standard.

 

 

Another New Talk: Elements of Design

Elements of Design

At C++ and Beyond next week (and in December) I’ll also be giving a brand-new half-day talk on Elements of Design.

I’m passionate about design, in part because it requires specific skills and taste, but most off all because it’s so important for every programmer — whether building a new library or extending one, building a new class or maintaining one, and that covers pretty much all of us.

In recent years, I’ve been spending a lot of my time leading design work in diverse areas ranging from general- and special-purpose library design, to systems software architecture, to programming language design and evolution, including participating in the crafting of various C++0x language and library features.

Although those areas are pretty diverse, they also have a lot of commonalities, and the insights and learnings apply directly to mainstream classes-and-libraries production software design. I’ve organized the topics to cover proven design practices at three levels:

  1. Design process: Running a design effort to set it up for repeatable success. This isn’t about heavyweight Process with a capital P, it’s about practices that  apply universally to projects of any size — when the projects are smaller, the elements can be informal.
  2. Design principles: Fundamental truths that guide design choices toward high quality.
  3. Design elements: Key specific design points and best practices to learn and apply.

We’ll build these areas from the bottom up — first key elements as the foundation, then the principles that unify the elements, and finally the process that enables staying true to the principles. Each process, principle, and practice is illustrated using real-world examples drawn from personal experience in many different design areas, but always targets “how you can use this today”: concrete skills and techniques for the development of well-designed production software and applications.

I’m looking forward very much to this topic in particular, not only in this talk itself but also in the informal and unstructured fireside time built into the C++ and Beyond schedule. Together with my other new talk on Lambdas, Lambdas Everywhere, my part of C++ and Beyond will be 100% new material never seen in public before — 270 minutes’ worth (whew). I look forward to seeing many of you there next week, or at the December “Encore” event where registration opened two weeks ago.

Herb

 

 

C++ and Beyond Session: Lambdas, Lambdas Everywhere

Lambdas, Lambdas Everywhere

We’ll be posting abstracts (summaries) of the C++ and Beyond 2010 sessions over the coming days over at the C&B site. Below is the first, for my talk on “Lambdas, Lambdas Everywhere.”

This is a brand new talk. I delivered a ‘sneak peek’ preview of a subset of this material in conjunction with the ISO C++ standards meeting in Switzerland two months ago, but the full talk will be given publicly for the first time at C++ and Beyond.

Historical trivia: This talk exists because Bjarne Stroustrup asked for it. Bjarne knew that I felt lambda functions were a game-changing feature that would have a pervasive impact on C++ coding style across many domains, and he asked me to write up the examples demonstrating why. This talk is the result.

Here’s the abstract:

Why care about C++0x lambda functions? Syntactically, they are nothing but sugar for function objects. However, they are an essential and enabling sugar that will change the way we will write C++ code more often than most people realize.

This talk will cover what lambda functions are and how to use them effectively, including how lambdas touch many wide-ranging kinds of code — from their convenience when writing concurrent and scalably parallel code, to how they stand to revolutionize STL usability and programming, to how they enable such small conveniences as local functions and local variable initialization.

Note that lambda functions, although futuristic, are not a far-future feature. They are available today in several shipping C++ compilers, including Intel C++ 11, Microsoft Visual Studio 2010, and gcc 4.5. The future is now. Come find out how this powerful feature affects you and your code.

About the speaker: Herb Sutter is an architect on the Windows C++ team at Microsoft and the chair of the ISO C++ standards committee. He is the main designer of lambda functions in Visual C++ 2010 and their integration with the VC++ Parallel Patterns Library, and is partly responsible for the design of lambda functions in the C++0x standard, notably their final syntax.

C++ and Beyond Encore: Public Registration Now Open

Public registration is now open for the overflow “Encore” showing of C++ and Beyond. The deadline for early-bird discount registration is November 14, but if you want to make sure you get a place it would probably be good to register sooner (the first showing sold out during the early-bird period).

If you weren’t able to get into the first one before it sold out, here is a second chance to attend the same event with the same speakers; the same location; and the same solid technical program on C++ and directly related technologies, including many new and up-to-the-minute-current talks that have never before been presented publicly anywhere else. The only thing that’s different is the date: C++ and Beyond Encore runs from December 13 (evening) through 16, 2010.

Technical Program

The talk schedule with titles and speakers is available at the Location and Schedule page. The topics are designed to provide a balance of useful and pragmatic material, ranging from broadly applicable design techniques to hard-core deep dives on specific important language features. The program includes substantial coverage of:

  • C++0x features and impact: In-depth discussion of move semantics (Scott) and lambdas (me) take up most of  day 1.
  • High performance: Hardware performance issues and techniques in Scott’s “CPU Caches” talk on day 2, and then a double-barreled hit on day 3 with Andrei’s “CAS” talk on high performance concurrency and his “Super Size Me” talk on scalability-focused issues.
  • Effective design: On day 2 Andrei has lots to say revisiting “Containers and Iterators,”  and I’ll be launching day 3 with “Elements of Design” focused on design lessons learned in many domains but all useful today in your production C++ code.
  • Three panels: The first and last are dedicated to answer your questions, submitted in advance and live at the event. The second promises to be an interesting and informative “cage match” where each of the speakers will choose two or three issues that they feel strongly about, but that are potentially controversial and bound to lead to lots of instructive discussion and debate about ideal designs and pragmatic tradeoffs.

We look forward to seeing many of you in the beautiful Pacific Northwest later this year!

Watch the C++ and Beyond site and RSS feed for further announcements, including detailed talk abstracts to be posted soon.

Updated 10/22 to remove the deferred GotW-0x talk.

Effective Concurrency: Know When to Use an Active Object Instead of a Mutex

This month’s Effective Concurrency column, “Know When to Use an Active Object Instead of a Mutex,” is now live on DDJ’s website.

From the article:

Let’s say that your program has a shared log file object. The log file is likely to be a popular object; lots of different threads must be able to write to the file; and to avoid corruption, we need to ensure that only one thread may be writing to the file at any given time.

Quick: How would you serialize access to the log file?

Before reading on, please think about the question and pencil in some pseudocode to vet your design. More importantly, especially if you think this is an easy question with an easy answer, try to think of at least two completely different ways to satisfy the problem requirements, and jot down a bullet list of the advantages and disadvantages they trade off.

Ready? Then let’s begin.

I hope you enjoy it. Finally, here are links to previous Effective Concurrency columns:

The Pillars of Concurrency (Aug 2007)

How Much Scalability Do You Have or Need? (Sep 2007)

Use Critical Sections (Preferably Locks) to Eliminate Races (Oct 2007)

Apply Critical Sections Consistently (Nov 2007)

Avoid Calling Unknown Code While Inside a Critical Section (Dec 2007)

Use Lock Hierarchies to Avoid Deadlock (Jan 2008)

Break Amdahl’s Law! (Feb 2008)

Going Superlinear (Mar 2008)

Super Linearity and the Bigger Machine (Apr 2008)

10 Interrupt Politely (May 2008)

11 Maximize Locality, Minimize Contention (Jun 2008)

12 Choose Concurrency-Friendly Data Structures (Jul 2008)

13 The Many Faces of Deadlock (Aug 2008)

14 Lock-Free Code: A False Sense of Security (Sep 2008)

15 Writing Lock-Free Code: A Corrected Queue (Oct 2008)

16 Writing a Generalized Concurrent Queue (Nov 2008)

17 Understanding Parallel Performance (Dec 2008)

18 Measuring Parallel Performance: Optimizing a Concurrent Queue(Jan 2009)

19 volatile vs. volatile (Feb 2009)

20 Sharing Is the Root of All Contention (Mar 2009)

21 Use Threads Correctly = Isolation + Asynchronous Messages (Apr 2009)

22 Use Thread Pools Correctly: Keep Tasks Short and Nonblocking(Apr 2009)

23 Eliminate False Sharing (May 2009)

24 Break Up and Interleave Work to Keep Threads Responsive (Jun 2009)

25 The Power of “In Progress” (Jul 2009)

26 Design for Manycore Systems (Aug 2009)

27 Avoid Exposing Concurrency – Hide It Inside Synchronous Methods (Oct 2009)

28 Prefer structured lifetimes – local, nested, bounded, deterministic(Nov 2009)

29 Prefer Futures to Baked-In “Async APIs” (Jan 2010)

30 Associate Mutexes with Data to Prevent Races (May 2010)

31 Prefer Using Active Objects Instead of Naked Threads (June 2010)

32 Prefer Using Futures or Callbacks to Communicate Asynchronous Results (August 2010)

33 Know When to Use an Active Object Instead of a Mutex (September 2010)