// Generated by decuda.
// OpenCL device code is the bulk of this file. The host-side calls
// (`cudaXxx`) have been rewritten to OpenCL equivalents inline// surrounding host program still needs a cl_context + cl_queue, not
// included here. Look for TODO(decuda) markers for items requiring
// manual attention.
// Advanced fixture: inline PTX assembly constructs.
//
// Exercises:
// - asm("...") with output/input operands
// - asm volatile("...") with no operands
// - asm with memory clobber
// - All PTX constructs are flagged as warnings (NVIDIA-specific, no
// equivalent in HIP/SYCL/OpenCL/Rust — manual rewrite required)
// - __global__ kernel, threadIdx.x, blockIdx.x, blockDim.x
// - cuda_runtime.h header
#include <CL/cl.h> /* was: cuda_runtime.h */
__kernel void ptx_bswap(int* data, int n) {
int i = get_group_id(0) * get_local_size(0) + get_local_id(0) if (i >= n) return
int x = data[i]
// Byte-swap via PTX `prmt` instruction.
int result asm("prmt.b32 %0, %1, 0, 0x0123;" : "=r"(result) : "r"(x)) data[i] = result}
__kernel void ptx_membar(int* data, int n) {
int i = get_group_id(0) * get_local_size(0) + get_local_id(0) if (i >= n) return
// Memory barrier via PTX.
asm volatile("membar.gl;") data[i] += 1}
__kernel void ptx_clock(unsigned long long* cycles) {
// Read the GPU clock counter via PTX.
unsigned long long c asm volatile("mov.u64 %0, %%clock64;" : "=l"(c)) if (get_group_id(0) * get_local_size(0) + get_local_id(0) == 0) {
*cycles = c }
}
__kernel void ptx_lanemask(unsigned int* mask) {
// Get the active lane mask via PTX.
unsigned int m asm volatile("activemask.b32 %0;" : "=r"(m)) if (get_group_id(0) * get_local_size(0) + get_local_id(0) == 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)
clEnqueueNDRangeKernel(queue, ptx_bswap_kernel, 1, NULL, (size_t[1]){grid}, (size_t[1]){block}, 0, NULL, NULL) /* args: d_data, N */ clEnqueueNDRangeKernel(queue, ptx_membar_kernel, 1, NULL, (size_t[1]){grid}, (size_t[1]){block}, 0, NULL, NULL) /* args: d_data, N */ clEnqueueNDRangeKernel(queue, ptx_clock_kernel, 1, NULL, (size_t[1]){1}, (size_t[1]){1}, 0, NULL, NULL) /* args: d_cycles */ clEnqueueNDRangeKernel(queue, ptx_lanemask_kernel, 1, NULL, (size_t[1]){1}, (size_t[1]){1}, 0, NULL, NULL) /* args: d_mask */
cudaDeviceSynchronize() cudaFree(d_data) cudaFree(d_cycles) cudaFree(d_mask) return 0}