#include <hip/hip_runtime.h>
__global__ void ptx_bswap(int* data, int n) {
int i = blockIdx.x * blockDim.x + threadIdx.x;
if (i >= n) return;
int x = data[i];
int result;
asm("prmt.b32 %0, %1, 0, 0x0123;" : "=r"(result) : "r"(x));
data[i] = result;
}
__global__ void ptx_membar(int* data, int n) {
int i = blockIdx.x * blockDim.x + threadIdx.x;
if (i >= n) return;
asm volatile("membar.gl;");
data[i] += 1;
}
__global__ void ptx_clock(unsigned long long* cycles) {
unsigned long long c;
asm volatile("mov.u64 %0, %%clock64;" : "=l"(c));
if (blockIdx.x * blockDim.x + threadIdx.x == 0) {
*cycles = c;
}
}
__global__ void ptx_lanemask(unsigned int* mask) {
unsigned int m;
asm volatile("activemask.b32 %0;" : "=r"(m));
if (blockIdx.x * blockDim.x + threadIdx.x == 0) {
*mask = m;
}
}
int main(void) {
const int N = 1024;
int* d_data = nullptr;
unsigned long long* d_cycles = nullptr;
unsigned int* d_mask = nullptr;
hipMalloc((void**)&d_data, N * sizeof(int));
hipMalloc((void**)&d_cycles, sizeof(unsigned long long));
hipMalloc((void**)&d_mask, sizeof(unsigned int));
dim3 grid(N / 256);
dim3 block(256);
hipLaunchKernelGGL(ptx_bswap, dim3(grid), dim3(block), 0, 0, d_data, N);
hipLaunchKernelGGL(ptx_membar, dim3(grid), dim3(block), 0, 0, d_data, N);
hipLaunchKernelGGL(ptx_clock, dim3(1), dim3(1), 0, 0, d_cycles);
hipLaunchKernelGGL(ptx_lanemask, dim3(1), dim3(1), 0, 0, d_mask);
hipDeviceSynchronize();
hipFree(d_data);
hipFree(d_cycles);
hipFree(d_mask);
return 0;
}