openaccel-lab
aien-dev/openaccel-lab
Brought by drakestapleton
openaccel-lab / workloads / transpose / kernel.cu
| 1 | // SPDX-License-Identifier: Apache-2.0 |
| 2 | #include <cuda_runtime.h> |
| 3 | |
| 4 | #define TS 16 |
| 5 | |
| 6 | // 16x16 tile through static shared memory (one padding column against bank conflicts). |
| 7 | __global__ void transpose(const float *a, float *b, int R, int C) |
| 8 | { |
| 9 | __shared__ float tile[TS][TS + 1]; |
| 10 | int tx = threadIdx.x, ty = threadIdx.y; |
| 11 | int c0 = blockIdx.x * TS, r0 = blockIdx.y * TS; |
| 12 | int r = r0 + ty, c = c0 + tx; |
| 13 | if (r < R && c < C) |
| 14 | tile[ty][tx] = a[(long long)r * C + c]; |
| 15 | __syncthreads(); |
| 16 | int orow = c0 + ty, ocol = r0 + tx; // output b is C x R |
| 17 | if (orow < C && ocol < R) |
| 18 | b[(long long)orow * R + ocol] = tile[tx][ty]; |
| 19 | } |
| 20 | |
| 21 | void oal_launch(const long long *p, void *const *buf) |
| 22 | { |
| 23 | int R = (int)p[0], C = (int)p[1]; |
| 24 | dim3 grid((unsigned)((C + TS - 1) / TS), (unsigned)((R + TS - 1) / TS)); |
| 25 | dim3 block(TS, TS); |
| 26 | transpose<<<grid, block>>>((const float *)buf[0], (float *)buf[1], R, C); |
| 27 | } |