# Transpose: Swap the Axes

*Task 4 of 5 · [Matrix Multiply](https://gpu.rocks/learn/matrix-multiply-972e080b.md) · GPU.js Learn*

Look back at the matmul loop: `b[k][x]` walks *down a column* —
each step jumps a whole row of memory. GPUs hate that; neighbouring threads reading
neighbouring addresses is where their bandwidth comes from. The standard fix is to
**transpose** B first, turning column walks into row walks.

A transpose kernel is one line of insight: the thread that owns output cell
`[y][x]` reads input cell `[x][y]`. With a rectangular 24×40 input
the flip is visible in the shapes too — the result is 40×24, so
`output: [24, 40]`.

## Goal

**Goal:** transpose the 24×40 matrix `matWide` — output cell
`[y][x]` holds `matWide[x][y]`, giving a 40×24 result.

## Requirements

- Keep `output: [24, 40]` — the transposed width and height
- Each thread reads exactly one input cell: indices *swapped*
- No loops — a transpose moves data, it computes nothing

## Hint 1 — who reads what

The thread writing output cell `[y][x]` must read the input cell
whose row and column are swapped. Both `this.thread.x` and
`this.thread.y` appear — just not in their usual seats.

## Hint 2 — the one-liner

`return m[this.thread.x][this.thread.y];`

## Same idea elsewhere

Memory-coalescing is why cuBLAS and rocBLAS pick a different tiled kernel for
each setting of GEMM's `transA/transB` flags — whichever layout you pass,
threads must still read side by side — and why Metal and WebGPU matmul kernels
pre-stage tiles in threadgroup memory. Reordering data for coalesced access is half of
GPU performance work.

## Starter code

```js
// The thread for output [y][x] reads input... where?
const gpu = new GPU({ mode });

const transpose = gpu.createKernel(function (m) {
  // TODO: return the input cell with row and column swapped.
  return 0;
}, {
  // input is 24 rows × 40 cols → output is 40 rows × 24 cols
  output: [24, 40],
});

const t = await transpose(matWide);
console.log('rows:', t.length, 'cols:', t[0].length);
```

---

Interactive version: https://gpu.rocks/learn/matrix-multiply-972e080b/4

[Previous task](https://gpu.rocks/learn/matrix-multiply-972e080b/3.md) · [Next task](https://gpu.rocks/learn/matrix-multiply-972e080b/5.md)
