在设备代码中使用函数 printf(),没有新的认识。
▶ 源代码
1 #include <stdio.h> 2 #include <cuda_runtime.h> 3 #include "device_launch_parameters.h" 4 #include <helper_functions.h> 5 #include <helper_cuda.h> 6 7 __global__ void testKernel(int val) 8 { 9 printf("[%d, %d]:\t\tValue is:%d\n", blockIdx.y*gridDim.x + blockIdx.x, threadIdx.z*blockDim.x*blockDim.y + threadIdx.y*blockDim.x + threadIdx.x, val); 10 } 11 12 int main(int argc, char **argv) 13 { 14 int devID; 15 cudaDeviceProp props; 16 devID = findCudaDevice(argc, (const char **)argv); 17 cudaGetDevice(&devID); 18 cudaGetDeviceProperties(&props, devID); 19 printf("Device %d: \"%s\" with Compute %d.%d capability\n",devID, props.name, props.major, props.minor); 20 21 dim3 dimGrid(2, 2); 22 dim3 dimBlock(2, 2, 2); 23 testKernel<<<dimGrid, dimBlock>>>(10); 24 cudaDeviceSynchronize(); 25 26 getchar(); 27 return 0; 28 }
▶ 输出结果
GPU Device 0: "GeForce GTX 1070" with compute capability 6.1 Device 0 : "GeForce GTX 1070" with Compute 6.1 capability [2, 0] : Value is : 10 [2, 1] : Value is : 10 [2, 2] : Value is : 10 [2, 3] : Value is : 10 [2, 4] : Value is : 10 [2, 5] : Value is : 10 [2, 6] : Value is : 10 [2, 7] : Value is : 10 [3, 0] : Value is : 10 [3, 1] : Value is : 10 [3, 2] : Value is : 10 [3, 3] : Value is : 10 [3, 4] : Value is : 10 [3, 5] : Value is : 10 [3, 6] : Value is : 10 [3, 7] : Value is : 10 [1, 0] : Value is : 10 [1, 1] : Value is : 10 [1, 2] : Value is : 10 [1, 3] : Value is : 10 [1, 4] : Value is : 10 [1, 5] : Value is : 10 [1, 6] : Value is : 10 [1, 7] : Value is : 10 [0, 0] : Value is : 10 [0, 1] : Value is : 10 [0, 2] : Value is : 10 [0, 3] : Value is : 10 [0, 4] : Value is : 10 [0, 5] : Value is : 10 [0, 6] : Value is : 10 [0, 7] : Value is : 10