← 返回 nvidia 的题目列表Transpose a 2D Matrix (C++), Discuss Memory/Cache Trade-offs
类型:online_judge
Given a 2D matrix A of size R x C, implement matrix transpose and return B of size C x R such that B[j][i] = A[i][j].
Requirements
Provide the core transpose implementation (C++ or pseudocode).
Discuss engineering considerations:
Impact of row-major vs column-major on access patterns
Cache friendliness (how to reduce cache misses)
Performance bottlenecks for large matrices
Whether to use blocking/tiling and how tile size affects performance
Constraints
1 ≤ R, C ≤ 10^4 (matrix can be large; do not assume it fits in cache).
Sample tests
[[1,2,3],[4,5,6]] → [[1,4],[2,5],[3,6]]
[[7]] → [[7]]
[[1,2],[3,4]] → [[1,3],[2,4]]
[[1,2,3]] → [[1],[2],[3]]
[[1],[2],[3]] → [[1,2,3]]
Example
Input
2 3
1 2 3
4 5 6
Output
3 2
1 4
2 5
3 6