フィルターのクリア

how to use printf inside a CUDA kernel?

77 ビュー (過去 30 日間)
Daniel Castaño Díez
Daniel Castaño Díez 2024 年 6 月 28 日 14:39
コメント済み: Daniel Castaño Díez 2024 年 7 月 11 日 9:44
Hi,
I wonder why I cannot use printf in cuda kernels. The code inside my file test.cu (adapted from the Mathworks help)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <iostream>
__global__ void add2( double * v1, const double * v2)
{
int idx = threadIdx.x;
v1[idx] += v2[idx];
printf("identity: %d \n",idx);
}
compiles nicely with mexcuda with
mexcuda -ptx test.cu
but trying to runt it from the command line as
k = parallel.gpu.CUDAKernel("test.ptx","test.cu");
N = 8;
k.ThreadBlockSize = N;
in1 = ones(N,1,"gpuArray");
in2 = ones(N,1,"gpuArray");
result = feval(k,in1,in2);
gather(result);
does not put any result on screen.
this link suggests some operations with the header, as #undef printf to avoid conflicts with mex.h... but it didn't work for me.
  5 件のコメント
Daniel Castaño Díez
Daniel Castaño Díez 2024 年 7 月 4 日 12:23
Hi Umar,
thanks, but why do you think snprintf is allowed in device code? mexcuda does protest:
/mnt/stor0hdd/castano/workplace/gpu/kernel/test.cu(21): error:
calling a __host__ function("snprintf") from a __global__
function("add2") is not allowed
Should I do something special to specify that it is some special version of a host device?
cheers,
D.
Umar
Umar 2024 年 7 月 4 日 12:38
Hi Daniel,
In CUDA C/C++, snprintf is allowed in device code because it is a host function that can be used in device code without any special modifications and facilitating easier code development. Also, when you use snprintf in device code, CUDA automatically handles the necessary translations and optimizations for device execution. Therefore, you do not need to specify snprintf as a special version for host or device; it can be used directly in device code as you would in host code.

サインインしてコメントする。

採用された回答

Joss Knight
Joss Knight 2024 年 7 月 6 日 22:04
編集済み: Joss Knight 2024 年 7 月 6 日 22:05

Just use it, and launch MATLAB from a terminal. On Linux, the output will appear in the terminal window. On Windows you will need to launch MATLAB with the undocumented options -wait -log.

  3 件のコメント
Joss Knight
Joss Knight 2024 年 7 月 8 日 9:38
If you want to redirect the output into the MATLAB command window you are going to need to use one of the tricks off the internet or mentioned here to redirect the kernel stdout into a buffer so you can invoke mexPrintf with it on the host. For debugging purposes, this is more trouble than it's worth.
Daniel Castaño Díez
Daniel Castaño Díez 2024 年 7 月 11 日 9:44
Thanks, Joss. As you say, for debugging purposes I don't need it... I'm already happy with the output in the terminal :-)

サインインしてコメントする。

その他の回答 (1 件)

Udit06
Udit06 2024 年 7 月 1 日 11:46
Hi Daniel,
One more suggestion that I found in the following discussion is to use "cudaDeviceSynchronize" to ensure that the kernel finishes and the driver flushes the output buffer.
If the issue still persists, you can refer to the solution given in the following discussion:
I hope this helps.

カテゴリ

Help Center および File ExchangeGet Started with GPU Coder についてさらに検索

製品


リリース

R2024a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by