0_Simple__simplePrintf

时间:2023-03-10 00:01:20
0_Simple__simplePrintf

在设备代码中使用函数 printf(),没有新的认识。

▶ 源代码

 #include <stdio.h>
#include <cuda_runtime.h>
#include "device_launch_parameters.h"
#include <helper_functions.h>
#include <helper_cuda.h> __global__ void testKernel(int val)
{
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);
} int main(int argc, char **argv)
{
int devID;
cudaDeviceProp props;
devID = findCudaDevice(argc, (const char **)argv);
cudaGetDevice(&devID);
cudaGetDeviceProperties(&props, devID);
printf("Device %d: \"%s\" with Compute %d.%d capability\n",devID, props.name, props.major, props.minor); dim3 dimGrid(, );
dim3 dimBlock(, , );
testKernel<<<dimGrid, dimBlock>>>();
cudaDeviceSynchronize(); getchar();
return ;
}

▶ 输出结果

GPU Device : "GeForce GTX 1070" with compute capability 6.1

Device  : "GeForce GTX 1070" with Compute 6.1 capability
[, ] : Value is :
[, ] : Value is :
[, ] : Value is :
[, ] : Value is :
[, ] : Value is :
[, ] : Value is :
[, ] : Value is :
[, ] : Value is :
[, ] : Value is :
[, ] : Value is :
[, ] : Value is :
[, ] : Value is :
[, ] : Value is :
[, ] : Value is :
[, ] : Value is :
[, ] : Value is :
[, ] : Value is :
[, ] : Value is :
[, ] : Value is :
[, ] : Value is :
[, ] : Value is :
[, ] : Value is :
[, ] : Value is :
[, ] : Value is :
[, ] : Value is :
[, ] : Value is :
[, ] : Value is :
[, ] : Value is :
[, ] : Value is :
[, ] : Value is :
[, ] : Value is :
[, ] : Value is :