Master the fundamental concepts of cache optimization through this focused micro-challenge.
You have read the whole brief, and the concepts above stay free on every task. Writing and running the code needs a plan.
Three hints are available for this task, revealed one at a time inside the code workspace so you can struggle productively before seeing them.
Every task includes starter code, theory, and hidden tests so you can implement and verify locally in the browser.
How it worksCache-oblivious algorithms use recursive divide-and-conquer so subproblems eventually fit in L1, L2, or L3 without hard-coded tile sizes. Matrix transpose is the textbook demo: naive a[j][i]=b[i][j] scans one matrix with huge stride.
Split the matrix into quadrants. Transpose each quadrant recursively until the base case fits in cache. Combine with a simple in-register transpose at the leaf.
cLoading…
Keep the relevant documentation open while you implement. When your output disagrees with the reference, trace one failing case by hand before changing random lines.
You will implement a recursive cache-oblivious transpose and compare against naive code. This exercise requires reporting both runtime and cache-miss counters.
Transposing a matrix reads one array row by row and writes the other column by column, so one side always walks against the cache. Blocking fixes this with a tile size tuned to the cache. A cache-oblivious transpose recursively halves the larger dimension until the pieces are small, which works for every cache size without knowing it. Implement all three, check that each produces the correct transpose, and count the misses of each in a simulated cache.
cLoading…
ceil(N*N*ELEM / LINE) * LINE). Every copy step reads A[i][j] (address (i*N + j) * ELEM) and then writes B[j][i]. Each algorithm starts with an empty cache.for i, for j.for i, for j. Edge tiles are clipped.rec(r0, r1, c0, c1). If both extents are <= BASE, copy naively. Otherwise split the rows in half if rows >= cols, else split the columns, and recurse on the first half, then the second.B[j][i] == A[i][j] everywhere (fill A with 0, 1, 2, …).cache: invalid geometry (the line must be a power of two, the number of sets a power of two), matrix: invalid, blocked: block size must be positive, and recursive: base size must be positive.cLoading…
A result that is not a transpose prints WRONG instead of transpose correct.
Input:
cLoading…
Output:
cLoading…
Hidden tests cover a small 4-way cache where power-of-two rows make blocking useless while a small recursion base still helps, a matrix size that is not a power of two (edge tiles), a block larger than the matrix, a recursion base of 1, and invalid settings.