Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

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

c++ - Cuda Hello World printf not working even with -arch=sm_20

I didn't think I was a complete newbie with Cuda, but apparently I am.

I recently upgraded my cuda device to one capable capability 1.3 to 2.1 (Geforce GT 630). I thought to do a full upgrade to Cuda toolkit 5.0 as well.

I can compile general cuda kernels, but printf is not working even with -arch=sm_20 set.

Code:

#include <stdio.h>
#include <assert.h>
#include <cuda.h>
#include <cuda_runtime.h>

__global__ void test(){

    printf("Hi Cuda World");
}

int main( int argc, char** argv )
{

    test<<<1,1>>>();
        return 0;
}

Compiler:

Error   2   error MSB3721: The command ""C:Program FilesNVIDIA GPU Computing ToolkitCUDAv5.0in
vcc.exe" -gencode=arch=compute_10,code="sm_20,compute_10" --use-local-env --cl-version 2010 -ccbin "C:Program Files (x86)Microsoft Visual Studio 10.0VCin"  -I"C:Program FilesNVIDIA GPU Computing ToolkitCUDAv5.0include" -I"C:Program FilesNVIDIA GPU Computing ToolkitCUDAv5.0include"  -G   --keep-dir "Debug" -maxrregcount=0  --machine 32 --compile -arch=sm_20  -g   -D_MBCS -Xcompiler "/EHsc /W3 /nologo /Od /Zi /RTC1 /MDd  " -o "Debugmain.cu.obj" "d:userstoredocumentsvisual studio 2010ProjectsestCudaestCudamain.cu"" exited with code 2.  C:Program Files (x86)MSBuildMicrosoft.Cppv4.0BuildCustomizationsCUDA 5.0.targets  592 10  testCuda
Error   1   error : calling a __host__ function("printf") from a __global__ function("test") is not allowed d:userstoredocumentsvisual studio 2010ProjectsestCudaestCudamain.cu    9   1   testCuda

I'm about done with life because of this problem...done done done. Please talk me down from the rooftops with an answer.

See Question&Answers more detail:os

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

1 Answer

0 votes
by (71.8m points)

If you're using printf in kernel, you should use cudaDeviceSynchronize():

#include <stdio.h>
#include <assert.h>
#include <cuda.h>
#include <cuda_runtime.h>

__global__ void test(){
    printf("Hi Cuda World");
}

int main( int argc, char** argv )
{
    test<<<1,1>>>();
    cudaDeviceSynchronize();
    return 0;
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...