#include <hip/hip_runtime.h>
#define TILE 16
__global__ void transpose(const float* in, float* out, int width, int height) {
__shared__ float tile[TILE][TILE];
int x = blockIdx.x * blockDim.x + threadIdx.x;
int y = blockIdx.y * blockDim.y + threadIdx.y;
if (x < width && y < height) {
tile[threadIdx.y][threadIdx.x] = in[y * width + x];
}
__syncthreads();
int ox = blockIdx.y * blockDim.y + threadIdx.x;
int oy = blockIdx.x * blockDim.x + threadIdx.y;
if (ox < height && oy < width) {
out[oy * height + ox] = tile[threadIdx.x][threadIdx.y];
}
}
int main(void) {
const int W = 1024;
const int H = 1024;
float* d_in = nullptr;
float* d_out = nullptr;
hipMalloc((void**)&d_in, W * H * sizeof(float));
hipMalloc((void**)&d_out, W * H * sizeof(float));
hipMemcpy(d_in, d_in, W * H * sizeof(float), cudaMemcpyDeviceToDevice);
dim3 grid(W / TILE, H / TILE);
dim3 block(TILE, TILE);
hipLaunchKernelGGL(transpose, dim3(grid), dim3(block), 0, 0, d_in, d_out, W, H);
hipDeviceSynchronize();
hipFree(d_in);
hipFree(d_out);
return 0;
}