Home > Articles > Programming > General Programming/Other Languages

Multicore Application Programming: An Interview with Darryl Gove

  • Print
  • + Share This
  • 💬 Discuss
From the author of
Jim Mauro interviews Darryl Gove, the author of Multicore Application Programming, about the joys of programming with threads and concurrency, the current state of multi-threaded application software, and the effectiveness of automatic parallelization technology.

Jim Mauro: Your expertise, your work and your published work explore application software performance and concurrency/threaded programming in great detail. Before we get into the joys of programming with threads and concurrency, how long have you been looking at application performance and optimizations, and is there anything in particular that moved you in that direction?

Darryl Gove: I joined Sun in 1999. My Ph.D. is in mathematical and simulation modeling of infectious diseases. When I joined Sun, the plan included me both spending time on modeling the performance of computers and improving the performance of applications on them. That never really worked out; I ended up having plenty to do on performance analysis.

Performance analysis and optimization happened to be something that I had always been interested in. Although I’ve done a fair amount of coding in various languages, I tend to be most comfortable looking at assembly language and understanding things at that level of abstraction. So the work I currently do on optimization, parallelization, and performance seems a very good fit for the kind of thing that I’m interested in.

Jim: We both know it’s really hard to write really good code, and very challenging to write scalable, threaded code. What is your view of the current state of multi-threaded application software, both in terms of what you see out there, and the APIs available for programmers to use?

Darryl: There’s an amazing transition going on. Looking back a few years, it was hard to get access to multiprocessor systems, so you had approaches like MPI, which distributed an application over multiple machines. You had POSIX threads, which gave you low level control over the parallelization that your application employed. You had OpenMP, which gave you a high level way of describing parallelism. Together with Windows threading, these still represent the prevalent approaches.

Today, if you look around you can see a wide range of alternatives. I find the CUDA/OpenCL approach of leveraging GPUs to be very interesting. Partly because I suspect it to be indicative of the future direction of the industry—many parallel cores—and partly because, for some situations, it can provide substantial gains over today’s technologies.

However, there’s not a single approach that has cracked the “make it easy to write parallel programs” problem. The two approaches that are the closest to this are automatic parallelization and OpenMP. Most of the other approaches have significant barrier to entry in terms of what can be achieved, or of what source code changes are necessary.

Jim: In my experience (and this likely seems pretty obvious) the biggest impediment to creating scalable, threaded applications is efficient use of synchronization primitives for locking—mutex locks, reader/write locks. What is your view on the state of tools available to help developers identify lock contention issues that impede scalability, and the state of published, available material for developers to reference to improve the lock granularity in their code? Are threaded applications overly cautious with the use of locks today, in your experience?

Darryl: Locks are one of the critical limiters to scaling. As you know, this is recognized in Solaris, and it has the Dtrace-based tools lockstat and plockstat that highlight lock hold times and contention. However, you can relatively easily pick up locking issues just by profiling the code. Of course the profile can be slightly hard to interpret, but the information is normally lurking there somewhere. That said, it is always possible to improve tools to make the problems more apparent.

Adding locks into code is something of a dark art. Scaling is typically improved by increasing the number of locks (i.e., reducing the granularity of each lock). But the cost of acquiring a particular lock typically increases as the number of locks increase. The alternative of using lock-free algorithms also has overheads, sometimes of performance, sometimes of implementation complexity. So there is not necessarily a “magic ratio” for locks to threads.

What you can expect to find is that an application will scale up to the number of cores that are present on the machine used to develop it. So long as using more cores leads to better performance, a developer will probably accept whatever scaling they get. However, as multicore processors with higher thread counts become available, we should expect (a) applications to show scaling issues and (b) developers to fix these scaling issues. So we’ll hit plenty of lock-induced scaling issues, but these will be fixed as machines with high thread count get more readily available.

Looking at the issue more broadly, poor scaling from lock contention is often relatively easy to diagnose. It is not necessarily that easy to fix, but knowing that the problem is there is the first step to solving it. There are other limitations to scaling that are less easy to diagnose and less easy to solve. Good examples of this are memory locality issues due to NUMA architectures or resource contention in shared processor pipelines. It can be hard to identify these problems, and it can be nearly impossible to provide a general solution for them.

Jim: That all aligns with my experience as well. To what extent did the availability of the atomic operations in Solaris (atomic_ops(3C)) help developers in terms of providing an alternative to the use of mutex locks for relatively simple variable operations? I have never actually measured the difference, but it seems using (for example) atomic_inc_[data_type]() would be far less expensive and more efficient than wrapping a mutex_lock()/mutex_unlock() call around a variable that multiple threads need to increment.

Darryl: The atomic operations were a fantastic addition to Solaris 10. As you point out, they can be much more efficient than a mutex lock. Obviously, atomic ops are only useful for manipulation of a single variable, incrementing it, decrementing it, or some other basic operation. They don’t work for more complex situations, for example where there are multiple variables being modified. What is nice, though, is that there are a number of situations where it is only the modification of a single variable that is needed. The obvious example is keeping an event counter.

If you squint at the code for a mutex lock and the code for an atomic operation, they are very similar—essentially try to do “this” until it works. For a mutex lock, the “this” is to acquire the lock; for an atomic operation it is to modify the variable. The performance gain from using atomic operations is because there is only a single memory address being accessed (no separate mutex variable). There’s also a gain because the atomic operations spin until it works, whereas mutexes have lots of features to handle contention—exponential backoff, sending waiting threads to sleep.

The other pleasing thing with the introduction of atomic operations into Solaris was that it brought the OS up to parity with other platforms. It seems a reasonable expectation that developers will have access to this kind of support. This makes sense. We should now expect most applications to be parallel, so we need low-cost intrinsics that can safely perform these kinds of data manipulation in multithreaded codes.

Jim: In your book, Multicore Application Programming, you dedicate a chapter to using automatic parallelization and OpenMP. In your experience, how effective is the automatic parallelization technology? Are compilers able to generate scalable, threaded programs?

Darryl: Well.... yes for some subset of applications, compilers can generate scalable, multithreaded versions. So when it works, it works well. However, it is really tricky for a compiler to do this in general. There are a number of problems. The most common is pointer aliasing, but other characteristics of the code, such as function calls, can also stop the compiler from automatically parallelizing loops.

The upshot is that automatic parallelization is often quite limited in scope. However, the compiler can usually give feedback as to what is stopping automatic parallelization, and this can enable the developer to change the code to make the compiler more effective.

This can build into a useful approach to parallelizing applications. The first pass is to get the compiler to do what it can, then tweak the source to help the compiler do a better job, before finally going in and manually adding OpenMP directives.

Jim: All modern microprocessors these days are multicore/multithread. That is, multiple threads-per-core. For most architectures, we see relatively small thread-per-core counts (2), and some variation in the underlying implementations (e.g., Intel hyperthreads versus UltraSPARC T-series), the latter of course having significantly more threads-per-core. Is writing multithreaded software for a modern multicore/multithread processor very different than it was several years ago, when systems had multiple CPUs, but a given “CPU” was a single-core, single-thread on a chip? Put another way, does the underlying target platform influence the design and implementation of multithreaded code?

Darryl: I think the question highlights the crux of the book. At first glance writing software for a multicore processor is no different from writing software for a multiprocessor system. The fundamental requirement is to identify chunks of work that can safely and efficiently be performed in parallel.

However, there are actually critical differences. The most obvious difference is that multiple threads on the same core end up sharing resources—instruction issue width and cache space being two obvious ones. This resource sharing may cause the throughput of a core running two threads to be only slightly more than the same core running one thread. This can be summarized whether it is better, for performance, to spread multiple threads across multiple cores to avoid sharing of core resources.

However, not all resource sharing is a problem. In the case of communication between threads, it is useful for the threads to share a common cache. This reduces communication costs and helps an application scale to higher numbers of threads. In this case it is better for an application to place its threads on one core, or multiple cores in close proximity.

The other benefit that comes with low communication costs is that some of the traditional scaling inhibitors—contended mutex locks, false sharing—are substantially diminished.

Consider false sharing. This is the situation where two threads need to write to different items of data that happen to reside on the same cache line. On a multiprocessor system this causes the cache line to migrate between processors. On a multicore processor this causes the cache line to remain resident in the shared cache. Cache latency can be, perhaps, ten times lower than memory latency, so the cost of sharing drops by a factor of ten. The consequence of this is that an application which has significant costs due to false sharing will get a significant performance gain on a multicore system.

An observation that goes with this is that the difficulty of writing scalable code also drops. A developer might end up with a data layout that has false sharing, or they might have fixed correctness issues by adding a number of mutexes. On a multicore processor there is little performance impact from these, so the application scales despite the issue.

Multicore processors are a great opportunity for developers. They have placed parallel application development at a much more accessible price point than multiprocessor systems. The are also a more forgiving platform than multiprocessor systems. The exciting thing is that we are very much in the infancy of this opportunity. There’s still a shift going on where people are figuring out how to best take advantage of multiple threads. So I think we’re going to be in for very interesting times.

  • + Share This
  • 🔖 Save To Your Account

Discussions

comments powered by Disqus