void ptx_bswap(int* data, int n) {
int i = block_idx * block_dim + thread_idx;
if (i >= n) return;
int x = data[i];
int result;
asm("prmt.b32 %0, %1, 0, 0x0123;" : "=r"(result) : "r"(x));
data[i] = result;
}
void ptx_membar(int* data, int n) {
int i = block_idx * block_dim + thread_idx;
if (i >= n) return;
asm volatile("membar.gl;");
data[i] += 1;
}
void ptx_clock(unsigned long long* cycles) {
unsigned long long c;
asm volatile("mov.u64 %0, %%clock64;" : "=l"(c));
if (block_idx * block_dim + thread_idx == 0) {
*cycles = c;
}
}
void ptx_lanemask(unsigned int* mask) {
unsigned int m;
asm volatile("activemask.b32 %0;" : "=r"(m));
if (block_idx * block_dim + thread_idx == 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;
cudaMalloc((void**)&d_data, N * sizeof(int));
cudaMalloc((void**)&d_cycles, sizeof(unsigned long long));
cudaMalloc((void**)&d_mask, sizeof(unsigned int));
dim3 grid(N / 256);
dim3 block(256);
{ let _kernel = modules.get_function("ptx_bswap"); unsafe { let _ = launch!( _kernel<<<grid as grid_size, block as block_size, 0 as usize, default>>>(d_data, N) ); } };
{ let _kernel = modules.get_function("ptx_membar"); unsafe { let _ = launch!( _kernel<<<grid as grid_size, block as block_size, 0 as usize, default>>>(d_data, N) ); } };
{ let _kernel = modules.get_function("ptx_clock"); unsafe { let _ = launch!( _kernel<<<1 as grid_size, 1 as block_size, 0 as usize, default>>>(d_cycles) ); } };
{ let _kernel = modules.get_function("ptx_lanemask"); unsafe { let _ = launch!( _kernel<<<1 as grid_size, 1 as block_size, 0 as usize, default>>>(d_mask) ); } };
cudaDeviceSynchronize();
cudaFree(d_data);
cudaFree(d_cycles);
cudaFree(d_mask);
return 0;
}