即使编译正常,cuPrintf也不会打印任何内容

cuPrintf does not print anything even if the compilation is ok

本文关键字:打印 任何内 cuPrintf 编译      更新时间:2023-10-16

以下是我的产品:

#include "cuPrintf.cu"
#include "cuPrintf.cuh"

我的内核:

__global__ void testKernel()
{
  cuPrintf("Test");
}

和我的主要:

int main( int argc, char** argv) 
{   
    dim3 threads(1,1);
    dim3 blocks(1, 1);
    testKernel<<<blocks, threads>>>();
}

有人能向我解释为什么cuPrintf没有打印任何东西吗?编译还可以,我正在使用Cuda 5.5

如果您有cc 2.0或更高版本的设备,那么直接从内核使用printf会更容易。只要您的compile命令为实际设备指定-arch=sm_20或类似内容,就可以执行此操作。

如果您真的想使用cuPrintf,那么请研究相关的示例代码。main例程中缺少cuPrintf正常工作所需的几个函数调用。

最后,无论何时从内核打印,都应该确保在程序退出之前具有一些同步功能。如果在内核之后没有其他东西,那么以下操作就可以了:

cudaDeviceSynchronize();

main例程中,在最后一次内核调用之后。

如果您这样修改main例程:

int main( int argc, char** argv) 
{   
    dim3 threads(1,1);
    dim3 blocks(1, 1);
    cudaPrintfInit();
    testKernel<<<blocks, threads>>>();
    cudaDeviceSynchronize();
    cudaPrintfDisplay(stdout,true);
    cudaPrintfEnd();
}

我相信它会起作用的。