2 * Copyright 1993-2013 NVIDIA Corporation. All rights reserved.
4 * Please refer to the NVIDIA end user license agreement (EULA) associated
5 * with this source code for terms and conditions that govern your use of
6 * this software. Any use, reproduction, disclosure, or distribution of
7 * this software and related documentation outside the terms of the EULA
8 * is strictly prohibited.
13 * Demonstration of inline PTX (assembly language) usage in CUDA kernels
21 #include <cuda_runtime.h>
23 // helper functions and utilities to work with CUDA
24 #include <helper_functions.h>
25 #include <helper_cuda.h>
27 __global__ void sequence_gpu(int *d_ptr, int length)
29 int elemID = blockIdx.x * blockDim.x + threadIdx.x;
34 //This command gets the lane ID within the current warp
35 asm("mov.u32 %0, %%laneid;" : "=r"(laneid));
36 d_ptr[elemID] = laneid;
41 void sequence_cpu(int *h_ptr, int length)
43 for (int elemID=0; elemID<length; elemID++)
45 h_ptr[elemID] = elemID % 32;
49 int main(int argc, char **argv)
51 printf("CUDA inline PTX assembler sample\n");
55 int dev = findCudaDevice(argc, (const char **) argv);
63 checkCudaErrors(cudaMalloc(&d_ptr, N * sizeof(int)));
66 checkCudaErrors(cudaMallocHost(&h_ptr, N * sizeof(int)));
68 dim3 cudaBlockSize(256,1,1);
69 dim3 cudaGridSize((N + cudaBlockSize.x - 1) / cudaBlockSize.x, 1, 1);
70 sequence_gpu<<<cudaGridSize, cudaBlockSize>>>(d_ptr, N);
71 checkCudaErrors(cudaGetLastError());
72 checkCudaErrors(cudaDeviceSynchronize());
74 sequence_cpu(h_ptr, N);
77 checkCudaErrors(cudaMallocHost(&h_d_ptr, N *sizeof(int)));
78 checkCudaErrors(cudaMemcpy(h_d_ptr, d_ptr, N *sizeof(int), cudaMemcpyDeviceToHost));
82 for (int i=0; i<N && bValid; i++)
84 if (h_ptr[i] != h_d_ptr[i])
90 printf("Test %s.\n", bValid ? "Successful" : "Failed");
92 checkCudaErrors(cudaFree(d_ptr));
93 checkCudaErrors(cudaFreeHost(h_ptr));
94 checkCudaErrors(cudaFreeHost(h_d_ptr));
96 checkCudaErrors(cudaDeviceReset());
98 return bValid ? EXIT_SUCCESS: EXIT_FAILURE;