Design Apps to Use the Cores

You may have come across an announcement that Intel will soon offer desktop cpu's with 8 cores. The news item also suggested that 6 cores may become the default minimum! The reason is simple. Increasing the speed of a cpu is now very difficult. Hence, the capacity of a system can be increased by increasing the number of cores.

However, will increasing the number of cores give you a better performance which you can notice? You may have a lot of applications open. There may be a lot of open windows. How likely are the tasks in various windows cpu bound and using the cpu? Most likely, most of the applications are waiting for you to activate their window and do something in it. Some applications or applets may occasionally wake up and check for some status or query a host.

Unless you are playing an action oriented 3-d game, chances are that it will be rare to find more than a couple of cores to be active. Hence, replacing a cpu may make little difference to your desktop experience.

Just till a few years ago, the clock rate kept increasing and each application was noticeably snappier. Today, the best option for you is to replace the hard disk by a solid state disk to notice a difference, in case you haven't already done that.

There is, however, an opportunity for developers. Various applications can be re-designed to exploit the parallel processing capabilities in the current desktops and notebooks. A great example of this approach is the Google Chrome browser. Each tab starts a separate process and, thus, actively using the hardware to a fuller extent, provided the bandwidth and internet connection speed are not the bottleneck. Firefox also had to follow Chrome though limiting itself to starting a plugin as a separate process. You may notice that if you start playing a flash video, Firefox will start an additional process. Since the desktops typically have a generous amount of RAM, it is becoming advantageous to consider moving from a multi-threaded application to a multi-processing application. Electrolysis project of Mozilla to convert Firefox to a muti-process architecture is active again.

Even solitaire can be made better! Many of the solitaire games offer the hint that whether the game is winnable. At times, the computer has to do a lot of work before it can decide between yes, no and don't know. If only it would use all the cores!

Multiprocessing in Python

Creating mutiple processes is fairly simple with Python. The syntax used by mutiprocessing module follows that of the threading module. The presence of the global interpreter lock in Python makes threading a harder option to increase performance in a multi-core environment. Multiprocessing module makes using the the cores trivial but at the expense of data sharing as each process has its own data.

You can get create multiple processes fairly trivially as the following minimal example illustrates.

import multiprocessing

class ExampleProcess(multiprocessing.Process):

  def run(self):

    print("Do something in " +

a = ExampleProcess()

b = ExampleProcess()



All you need to do is to create one or more classes which inherit from “multiprocessing.Process” and include the method “run”. The harder task is to determine :

  • what can be parallelised

  • what data needs to be shared

  • what data needs to be communicated between processes

As you would expect, Python multi-processing module makes it easy to achieve these goals after you have the design.

  • You may write multiple classes which can be run in parallel.

  • You may create and start mutliple process objects of the same class if the same algorithm can be run in parallel with different data.

  • You may share data between processes using a Queue class or a Pipe class.

  • If you must, you can share the state between processes by using shared memory though this should be avoided.

The Python module provides a lot more functionality to manage an application designed to use the multi-process architecture. There is more than enough documentation on the Python web-site to explore it in detail.

A major advantage of muti-process architecture over muti-threaded architecture is reliability. A failure in a thread brings down the application while a failure is a process only halts that particular process. Hence, if you are designing a new desktop or even a mobile device application or redesigning an existing one, designing it to use multiple cores may give it the extra edge over competition.

Oct 2013