Scan to open on your phone
Point a camera at the QR code - it opens this exact paste, no app needed.
https://codepastes.com/zzgsnbyk8j2z9qpguntitledPlain Text
#include <cuda_runtime.h>
#define TILE_DIM 32
#define BLOCK_ROWS 8
__global__ void matrix_transpose_kernel(const float* input, float* output, int rows, int cols) {
__shared__ float tile[TILE_DIM][TILE_DIM+1];
int x = blockIdx.x * TILE_DIM + threadIdx.x;
int y = blockIdx.y * TILE_DIM + threadIdx.y;
for (int j = 0; j < TILE_DIM; j += BLOCK_ROWS) {
if (x < cols && (y+j) < rows)
tile[threadIdx.y+j][threadIdx.x] = input[(y+j) * cols + x];
}
__syncthreads();
x = blockIdx.y * TILE_DIM + threadIdx.x;
y = blockIdx.x * TILE_DIM + threadIdx.y;
for (int j = 0; j < TILE_DIM; j += BLOCK_ROWS) {
if (x < rows && (y+j) < cols)
output[(y+j) * rows + x] = tile[threadIdx.x][threadIdx.y+j];
}
}
// input, output are device pointers (i.e. pointers to memory on the GPU)
extern "C" void solve(const float* input, float* output, int rows, int cols) {
dim3 threadsPerBlock(TILE_DIM, BLOCK_ROWS);
dim3 blocksPerGrid((cols + TILE_DIM - 1) / TILE_DIM,
(rows + TILE_DIM - 1) / TILE_DIM);
matrix_transpose_kernel<<<blocksPerGrid, threadsPerBlock>>>(input, output, rows, cols);
cudaDeviceSynchronize();
}
1,213 chars · codepastes.com