我假设您的意思是 CUDA 意义上的“共享内存”(NVIDIA GPU 上的快速、每个 SM 共享内存)。在这种情况下,您有几个选择。
首先,如果您只想知道使用了多少共享内存,可以在编译时通过添加-Mcuda=ptxinfo 来确定。
pgcc -fast -ta=tesla:cc35 laplace2d.c -Mcuda=ptxinfo
ptxas info : 0 bytes gmem
ptxas info : Compiling entry function 'main_61_gpu' for 'sm_35'
ptxas info : Function properties for main_61_gpu
0 bytes stack frame, 0 bytes spill stores, 0 bytes spill loads
ptxas info : Used 26 registers, 368 bytes cmem[0]
ptxas info : Compiling entry function 'main_65_gpu_red' for 'sm_35'
ptxas info : Function properties for main_65_gpu_red
0 bytes stack frame, 0 bytes spill stores, 0 bytes spill loads
ptxas info : Used 18 registers, 368 bytes cmem[0]
ptxas info : Compiling entry function 'main_72_gpu' for 'sm_35'
ptxas info : Function properties for main_72_gpu
0 bytes stack frame, 0 bytes spill stores, 0 bytes spill loads
ptxas info : Used 18 registers, 344 bytes cmem[0]
在上述情况下,我似乎没有使用任何共享内存。 (跟进我与一位 PGI 编译器工程师交谈,得知共享内存是在内核启动时动态调整的,因此不会通过 ptxinfo 显示。)
您还可以使用 NVIDIA Visual Profiler 来获取此信息。如果您收集 GPU 时间线,然后单击特定内核的实例,属性面板应打开并显示共享内存/块。在我的例子中,上面显示使用了 0 字节的共享内存,而 Visual Profiler 显示正在使用一些内存,所以我需要深入研究原因。
您也可以在运行时获取一些信息。如果您对命令行感到满意,可以使用 nvprof:
# Analyze load/store transactions
$ nvprof -m shared_load_transactions,shared_store_transactions ./a.out
# Analyze shared memory efficiency
# This will result in a LOT of kernel replays.
$ nvprof -m shared_efficiency ./a.out
这不会显示使用的数量,但可以让您了解它的使用方式。 Visual Profiler 的引导式分析将让您深入了解这些指标的含义。