#define TILE 16
void transpose(const float* in, float* out, int width, int height) {
float tile[TILE][TILE];
int x = block_idx * block_dim + thread_idx;
int y = block_idx * block_dim + thread_idx;
if (x < width && y < height) {
tile[thread_idx][thread_idx] = in[y * width + x];
}
group.sync();
int ox = block_idx * block_dim + thread_idx;
int oy = block_idx * block_dim + thread_idx;
if (ox < height && oy < width) {
out[oy * height + ox] = tile[thread_idx][thread_idx];
}
}
int main(void) {
const int W = 1024;
const int H = 1024;
float* d_in = nullptr;
float* d_out = nullptr;
cudaMalloc((void**)&d_in, W * H * sizeof(float));
cudaMalloc((void**)&d_out, W * H * sizeof(float));
cudaMemcpy(d_in, d_in, W * H * sizeof(float), cudaMemcpyDeviceToDevice);
dim3 grid(W / TILE, H / TILE);
dim3 block(TILE, TILE);
{ let _kernel = modules.get_function("transpose"); unsafe { let _ = launch!( _kernel<<<grid as grid_size, block as block_size, 0 as usize, default>>>(d_in, d_out, W, H) ); } };
cudaDeviceSynchronize();
cudaFree(d_in);
cudaFree(d_out);
return 0;
}