Oct 09 2007

What is DMA?

Published by at 9:54 pm under Code

One of the things which I encounter in my day job is lots of engineers who do not know what DMA is. DMA is “direct memory access.” It is a way to transfer data from one memory location to another. For most PC and Xbox 360 programmers, DMA is handled by the OS and they do not have to care about it. DMA is fundamentally how bytes of data are moved through the hardware. This means that PCI buses do DMA, USB devices do DMA, hard drives do DMA, etc.

For PS3 (and PS2) programmers, DMA is a critical part of how to get the best performance. With the PS3, one needs to understand how the SPUs work, especially since the Cell architecture is based around it. The PS3 has two types of CPUs: the PPU and the SPU. The PPU is a traditional main CPU. It is a PowerPC chip, but it behaves basically like any CPU in your PC, like a Pentium chip. The SPU is a specialized chip which has limited RAM (256k) and is optimized for processing vector data. It is not made for general game code.

The SPUs do not have direct access to main memory. If you have a pointer into main RAM, the SPUs cannot automatically get the data that is stored there. Unlike the XBox360, the Cell chip is an asymmetrical architecture. The SPUs have their own memory (called the Local Store, or LS) which is extremely fast, but is not a part of the main memory.

PS3 Memory Bus

This simplistic view means that the SPUs can trigger a transfer from the main RAM into the LS, but that the SPU cannot directly access the main RAM. On the SPUs, this code would fail miserably:

ClFoobar *pNext = pCur->pNext;
// BOOM! pNext is a pointer to main RAM, not the SPU local store
int myVar = pNext->somevar ;

because the memory is not in the local store.

A brain-dead implementation would be something like:

ClFoobar *pNext = pCur->pNext;
// Start the DMA
ps3CellGetMemory(myBuffer, pNext, sizeof(ClFoobar), dmaChannelNum);
// Wait for the DMA to finish
// Now we can use the memory
(ClFoobar *)pRealNext = (ClFoobar *)myBuffer;
int myVar = pRealNext->somevar;

So why is this an issue for game programmers? Data structures like trees or linked lists are used all over game code. For every pointer on the SPU, the programmer needs to handle the DMA transfer to pull the next data structure to the SPU. The SPUs really like working on large blocks on contiguous data, not randomly jumping through memory. Because the majority of game code assumes both symmetric cores and a unified memory architecture, the concept of DMA and memory transfers is very foreign to most game programmers.

I am working on a C++ template library which handles this transparently. It is basically an auto_ptr template class which hides the PPU vs SPU differences. But that is for the next post…

One response so far

One Response to “What is DMA?”

  1. markon 06 Mar 2008 at 11:03 am

    Great explanation. I’m checking for the c++ template….

    as a fan of ppc and g4 user (still on 10.3.9)- and also a pd and max user – i appreciate this kind of analysis. I would like to work more on ‘fixing’ problems I encounter.

    I would like to hear more of why gaming capacities are not ‘open’ for video artists. Why is gem limited? How can we use more opengl in live work? what are some c++ methods, eg cross platform….

Trackback URI | Comments RSS

Leave a Reply

You must be logged in to post a comment.