bit-tech.net

Editing Memory and Multi-core Programming

Editing Memory and Multi-core Programming

We're in something of a transitional phase in computing at the moment, with multi-core processors threatening to take over the world. This is great news, because what could be better than having more (and mooaaar!) CPU cores? Well, maybe a few things but none of them would be legal.

In spite of this revolution, there is an inherent problem with these increasingly core-tastic CPUs: writing programs that use all of the cores to their full potential is a headache of monumental proportions for software developers.

Rather than writing instructions that are processed one-by-one (or block-by-block) as just one thread', a programmer has to write to multiple threads in order to keep all the CPU's cores busy. This requires further steps need to be taken to ensure that all the commands are executed individually and then the outputs are put back together correctly, so that everything makes sense at the other end. If two different threads require the same piece of information at the same time, then conflicts and slowdown occur in the pipeline as one core will have to wait for another core to finish calculating the data before it can carry on.

In order to progress, obviously there need to be methods and technology that will make the process of writing multi-threaded applications easier, so that programmers more readily take full advantage of all the cores that manufacturers such as Intel and AMD are stuffing under the heatspreaders of their CPUs.

Editing Memory and Multi-core Programming
Sure, you can fit a pair of dual-core CPUs under one heatspreader and make a quad-core - but not everything we run on our computers will use all cores effectively

A few weeks ago we wrote an article on Microsoft's new search engine, Bing which has high aims to knock Google off the top spot. It turns out that the guys at MS quite liked it and so asked us if we were interested in learning about its research on a programming technique called 'Transactional Memory'. They put us in touch with Tim Harris from Microsoft Research in Cambridge to get the low down about Microsoft's efforts to work with many cores and threads.

How Does Transactional Memory Work?

So how does this new programming tool work? In order to explain to those without a Masters in Computer Science, Harris gave us a basic example:

"Suppose that a dual-core CPU is keeping track of the amount of money in two bank accounts X and Y. Then, one core tries to move £10 between the two accounts, and a second core is trying to find their total. Using Transactional Memory a programmer could write the “move” operation as something like this.." Tim scribbles down a bit of code for us:

atomic {

	X.Balance = X.Balance - 10;
	Y.Balance = Y.Balance + 10;
}

"The two middle lines are describing the actual operations to execute removing £10 from one account (account X), and crediting it to the balance in the other account (account Y). The “atomic {... }” around the outside says that the program should perform these operations using TM, rather than performing the removal first and then the credit," explains Harris.

"This is important because a second core might be trying to calculate the total sum of money in the two bank accounts while the first attempts to make the transfer."

Editing Memory and Multi-core Programming
At Microsoft's Research Facility in Cambridge, work is under way that may play a key part in unlocking the full potential of multi-core CPUs

This TM implementation ensures that the second core should also use TM when accessing these two accounts. The TM implementation is then responsible for ensuring that the “atomic” steps in the first core don’t get interleaved with the “atomic” steps in the second core, and so the total is either done first before the transfer, or alternatively the transfer is done first before the total.

atomic {

	total = x.Balance;
	total = total + y.Balance;
}

If the programmer didn’t use TM, then they would need to design their own way of making sure that the operations didn’t get mixed up, thus causing conflict in the cores and slowing things down, Harris informed bit-tech.

"For example, the programmer would need to prevent the second core from computing the total when the first core is in the middle of doing the transfer, says Harris. "That’s something that programmers often get wrong, so we hope that TM will help make their job easier."