Basis Lab

Memory Is the Limit on a GPU

Store Each Field in Its Own Array and the Group Reads Next-Door Bytes

Last timeNeighbouring Workers, Neighbouring Bytes

Records kept whole put a worker's field 32 bytes from its neighbour's. Keeping each field in its own array puts them 4 bytes apart, and the fetches saved can be counted in advance.

The last lesson ended with a rule: worker ww reads item ww, so that the group reads next-door bytes. Whether a job can follow it is decided before the function runs, by the order in which the data was written down.

Two ways to lay out records

Suppose the data is a million particles, each with eight properties of four bytes: three for position, three for velocity, a mass and a charge. A record is 32 bytes.

FIG 1The same million records, in two orders
plaintext
records kept whole (the vendor's term: array of structures)

    x0 y0 z0 u0 v0 w0 m0 q0   x1 y1 z1 u1 v1 w1 m1 q1   x2 y2 ...

each field in its own array (the vendor's term: structure of arrays)

    x0 x1 x2 x3 ...   y0 y1 y2 y3 ...   ...   m0 m1 m2 m3 ...   q0 q1 ...
Both take 32 MB and hold identical values. In the first, the mass of particle 1 is 32 bytes after the mass of particle 0. In the second it is 4 bytes after.

Now run a function in which worker ww reads the mass of particle ww, to add up the total mass, say.

The lesson stops here

4 more paragraphs to go

You have read the opening. The rest of the argument, the problems that check whether it landed, and the lines worth keeping at the end all come with a plan.

The first lesson of every course in the library reads the whole way through, free, so you can see exactly what the rest of them are.

See the planThe contents

This is the reading half

Starting the course gives you your own copy of it. Every idea on every page has problems standing under it, marked with a reason rather than a tick, and any sentence you do not believe can be opened and argued with. None of that can happen on a page nobody owns.

The contents

The rest of this course

  1. 01Add Up the Largest Moment, Not the Average
  2. 02The Fastest a Job Can Run Is Its Bytes Divided by the Bandwidthopening only
  3. 03One Division Says Whether Faster Arithmetic Would Helpopening only
  4. 04Where a Byte Is Kept Decides What It Costs to Readopening only
  5. 05A Group That Reads Next-Door Addresses Wastes Nothingopening only
  6. 06Store Each Field in Its Own Array and the Group Reads Next-Door Bytesyou are here
  7. 07Use a Value Many Times While It Is Near and the Memory Stops Being the Limitopening only
  8. 08Four Ways to Fit, and What Each One Costsopening only

Read alongside