Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
885 views
in Technique[技术] by (71.8m points)

not able to use printf in cuda kernel function

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
", 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];
}

when compiling, it says:

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

The cuda version is 4

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.

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

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 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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...