无法在cuda内核函数中使用printf

mah*_*ood 3 printf cuda

似乎printf在cuda代码的内核中不起作用

#include "Common.h"
#include<cuda.h>
#include <stdio.h>

__device__ __global__ void Kernel(float *a_d , float *b_d ,int size)
{
    int idx = threadIdx.x ;
    int idy = threadIdx.y ;
    //Allocating memory in the share memory of the device
    __shared__ float temp[16][16];

    //Copying the data to the shared memory
    temp[idy][idx] = a_d[(idy * (size+1)) + idx] ;


    printf("idx=%d, idy=%d, size=%d\n", idx, idy, size);


    for(int i =1 ; i<size ;i++) {
            if((idy + i) < size) { // NO Thread divergence here
                    float var1 =(-1)*( temp[i-1][i-1]/temp[i+idy][i-1]);
                    temp[i+idy][idx] = temp[i-1][idx] +((var1) * (temp[i+idy ][idx]));
            }
            __syncthreads(); //Synchronizing all threads before Next iterat ion
    }
    b_d[idy*(size+1) + idx] = temp[idy][idx];
}
Run Code Online (Sandbox Code Playgroud)

在编译时,它说:

 error: calling a host function("printf") from a __device__/__global__ function("Kernel") is not allowed
Run Code Online (Sandbox Code Playgroud)

cuda版本是4

Gre*_*ith 7

引用CUDA编程指南" 格式化输出仅受计算能力2.x及更高版本的设备支持 ".有关其他信息,请参阅编程指南.

计算能力<2.x的设备可以使用cuPrintf.

如果您使用的是2.x及以上设备并且尝试使用printf,请确保指定了arch = sm_20(或更高版本).默认值为sm_10,它没有足够的功能来支持printf.

NVIDIA为CUDA提供三种源代码级调试器.您可能会发现这些比printf更有用于检查变量. - Nsight Visual Studio Edition CUDA调试器 - Nsight Eclipse版CUDA调试器 - cuda-gdb