Successful parallel programming

 

Summary

Intel's James Reinders presents some recurring themes for developers looking to improve their game when it comes to programming parallel systems...

Events

Echelon 2012
June 11 and 12, 2012

University Cultural Centre, National University of Singapore

Startup Asia Jakarta 2012
June 7 and 8, 2012

12th Floor, Annex Building, Wisma Nusantara Complex, Jl. M.H. Thamrin No. 59 Jakarta 10350, Indonesia

MMA Forum Singapore
April 23-25, 2012

Grand Hyatt Singapore

I’m James Reinders and as I've travelled around talking to programmers about parallel programming I’ve come up with a set of rules of thumb; recurring themes about what makes programmers more successful doing parallel programming.

The firs tone is: Think parallel. By this I mean think about the parallelism in your program first. Don’t think about how to implement your program in the traditional methods first and then try to jam parallelism in. Now the reality is that you probably already have some programs you’re trying to introduce parallelism in. But I’d still encourage you to step back, think about what parallelism really is, and how you’re going to get at it and have a strategy for that before you get busy coding. Doing this really helps, even if you can’t take advantage of all the parallelism right away you can have a strategy for it and introduce a little bit at a time and have a roadmap for taking advantage of it.

The second rule of thumb is don’t try to program to the number of processors there are. Think about tasks, think about pieces of parallelism and program tools, don’t think about threads. So I call this program to tasks, not to threads. A thread to me is something that typically there is one thread per processor. The best parallel programs don’t query the number of processors and have all their algorithms written around that. Even though that may happen a the lower levels, the higher level of the program really should be producing tasks. Lots of them, and the allowing some underlying structure to map those on to the number of processors that happen to be there. So obviously a program that produces thousands of tasks of available parallelism will scale a lot better tan one that only produces two tasks. So focus programming to tasks, not to threads.

The third one which you don’t want to overlook is: take a look at the tool that you’re using. Take a look at the compilers, the libraries, the debugging tools; take a look at them and ask yourself: were these designed with parallelism in mind? If not, take a look at what is available and think very hard about making sure that your not making your life a lot more difficult as you approach parallelism by ignoring getting the proper tools in place. As a professional, writing programs, worrying about parallelism, you really deserve to take a look at the tools you’re using for that. You’re probably used to having tools that support you really well in the programming you’re doing now and as you add parallelism you really should have the same attitude about looking for them.

The fourth rule of thumb is perhaps my favourite one as it turns out to be an incredibly important one that I see people do all the time that do parallelism but is often not spoken about. And that is to make sure that your program can run sequentially. So if you have a multi-threaded program and it can run in four threads and eight threads and so on, make sure it can run in one thread. It doesn’t need to be efficient, but if you have a program that you can kick back into single threaded mode you’ll find yourself doing a lot of debugging in that mode because you can debug the general errors in your program that you do today in your sequential program, you can do that without having to debug any special issues relating to parallelism. And then you kick it into two threads or four threads mode. If your program has a failure running in four threads and you make it run in one thread and the failure goes away it gives you a hint that the error has something to do with the way you specified parallelism. If the issue is still there the good news is that you can use the traditional debugging tools you already have today to debug the program in single threaded mode. It’s quite possible to write programs that can only run in parallelism. It’s a bad idea. Avoid using techniques that require that. In general it is very easy to write a program so that it can run in a single threaded mode and it makes debugging much easier.

Fifth rule of thumb. Really really limit the use of locks. Often when people learn about parallelism they learn about the need to synchronise between multiple threads between pieces of execution of their program. They learn about locks and they start adding locks. Lots and lots of lock. And then they start to learn the problems with locks. One is that they are inefficient – they limit the scalability of a program. Another one is that in a real program with lots of dynamics going on and libraries being called it is very to have a lock set that then interferes with another lock and you get deadlocks and you get all sorts of problems. The less locks you have the better. We can go back to the first rule of thumb thinking parallelism and we can connect these together. If you keep in mind that you don’ want to use a lot of locks then the best way to extract parallelism in your program won’t need explicit locking, or it won’t need a lot of it. If the algorithm you come up with requires a lot of locks and you’re using locks everywhere to protect things, it’s worth taking a bit time to think about whether you can come up with an algorithm that take a lot less locks or perhaps no locks at all using implicit synchronisation.

Another rue of thumb is don’t forget about memory allocation. Memory allocation turns you to be a very common bottleneck for programs as they move to parallelism if you continue to use the memory allocation techniques you’ve used before. It’s much more efficient to have multiple pools of memory than to have a scalable memory allocator. And there are quite a few of them available out the market. The rule of thumb here is don’t forget to take a look at those. Don’t forget to take a look at whether memory allocation is something you do a lot of in your program and if it is when you move to parallelism don’t forget to move the memory allocation to parallelism as well.

The final rule of thumb is going back to thinking about Amdahl’s Law and Gustafson’s observations about it he made in 1988. To get true scalability of program you need to expect the workload to increase so over time as we get more and more processors, more powerful systems, you should have in mind that you’re going to get performance benefits out of that. Maybe another way to look at this rule of thumb is don’t get too hung up on expecting great big speedups on today’s program with today’s dataset. Get yourself lined up for scalability. Get yourself some benefit from the current processors, but be sure to take a look at how is that scalability going to come as your workloads increase. As your programs are used to process more data or to do more tasks in the future. Keep in mind the scaling. So these are seven rules of thumb that I think work very well for people that are doing parallel programming today.

Talkback

Add your opinion

In order to post a comment, you need to be registered. (Sign In or register below)

Post your comment

ZDNet Asia Live

Malaysia organizations don't realize severity of cyberattacks http://t.co/PUCv68Rd

News: Radio Costa Rica by EnjoyIT 1.0: Radio Costa Rica allows you to listen to a great var... http://t.co/BLzVT5As http://t.co/1Dhcy6ki

The key for mobile operators is identifying the applications that are popular with subscribers on their network. They can then work partn...

2 hours ago by camcullen on Experience trumps content in apps monetization

Experience trumps content in apps monetization | ZDNet http://t.co/gBXcjbGd

Experience trumps content in apps monetization - ZDNet Asia News: "What we are doing currently is not to monetiz... http://t.co/S2EZtd8m

Malaysia organizations don't realize severity of cyberattacks: "Minister Maximus Johnity Ongkili said at the Sec... http://t.co/bgVlOBvx

#security Malaysia organizations don't realize severity of cyberattacks: "Minister Maximus Johnity Ongkili said ... http://t.co/hkFb4zrI

Malaysia organizations don't realize severity of cyberattacks http://t.co/EEEmRM3j via @zdnetasia

Malaysia organizations don't realize severity of cyberattacks - ZDNet Asia News http://t.co/YpNMYgb5

Malaysia organizations don't realize severity of cyberattacks http://t.co/FFems54Q

China solar cell makers seek Taiwan partnerships http://t.co/p5Hh7kJD

Big data acquisitions pave way to fast, effective innovation http://t.co/hdiEfBsz via @zdnetasia

Integration, focused investments to propel Windows Phone: By Kevin Kwang , ZDNet Asia on May 23, 2012 (2 hours a... http://t.co/E7tsZbHJ

Integration, focused investments to propel Windows Phone http://t.co/u9TqjQ8C

ZDNet Asia IT Salary Benchmark 2012 http://t.co/rVwYlV7H

AsiaClassifiedToday. Integration, focused investments to propel Windows Phone - ZDNet Asia: S... http://t.co/47tdjZyG #asia #google #biz

So much as we know , MTK6575 extremely integrated frequency1GHz ARM Cortex-A9 processor, the superiority of 3G / HSPA Modem, and help the...

1 day ago by y15822137359 on 5 SaaS adoption speed bumps to avoid

I reckon your view: "CRM is strategy, not software", if a company replicating the approach uses in ERP implementation into CRM, what they...

2 days ago by wykoong on Gartner: Mobile CRM gives better ROI than social

This video will teach you about the Excel fill handle but also provide you with a workook to download... http://www.youtube.com/watch?v=...

3 days ago by TradeBrother on A quick fill handle trick for Microsoft Excel

waiting...

5 days ago by eapete on What should count in a company's market value?

Boy, you've opened a can of worms now.

Wait for the rants & raves.

5 days ago by eapete on What should count in a company's market value?

I was puzzling before this whether to replicate the success formula we executed for a financial institute, and come out with a standard s...

5 days ago by wykoong on Drop the egos, copy ideas, then innovate