不能在cuda内核函数中使用printf [英] not able to use printf in cuda kernel function

查看:3724
本文介绍了不能在cuda内核函数中使用printf的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

It seems that printf doesn't work inside the Kernel of a cuda code

#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];
}

,它说:

 error: calling a host function("printf") from a __device__/__global__ function("Kernel") is not allowed

cuda版本为4

推荐答案

CUDA编程指南格式化输出仅由计算能力2的设备支持。 x和更高。有关其他信息,请参阅节目指南。

Quoting the CUDA Programming Guide "Formatted output is only supported by devices of compute capability 2.x and higher". See the programming guide for additional information.

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

Devices of compute capability < 2.x can use cuPrintf.

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

If you are on a 2.x and above device and you are trying to use printf make sure you have specified arch=sm_20 (or higher). The default is sm_10 which does not have sufficient features to support printf.

NVIDIA为CUDA提供了三个源代码级调试器。你可能会发现这些比printf更有用的检查变量。
- Nsight Visual Studio版CUDA调试器
- Nsight Eclipse版CUDA调试器
- cuda-gdb

NVIDIA offers three source level debuggers for CUDA. You may find these more useful than printf for inspecting variables. - Nsight Visual Studio Edition CUDA Debugger - Nsight Eclipse Edition CUDA Debugger - cuda-gdb

这篇关于不能在cuda内核函数中使用printf的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆