验证4个SMSP是否是串行访问ShareMemory的

验证4个SMSP是否是串行访问ShareMemory的

原以为4个smsp中的warp在没有bank冲突的情况下,是可以并行访问共享内存的

通过下面的测试发现,其实是串行的,share memory每个cycle只能处理一个请求

测试过程

c 复制代码
tee shm_kernel.cu<<-'EOF'
#include <iostream>
#include <cuda_runtime.h>
__global__ void shm_kernel(float *input,float *output) {
    int tid  = threadIdx.x + blockIdx.x * blockDim.x;
    __shared__ float shm_data[0xc000/4];
    float vals;
    clock_t t0=clock64();
    vals=shm_data[tid];
    __syncthreads();
    clock_t t1=clock64();
    vals*=(tid);
    output[tid]=vals;
    if(tid==0)
    {
        printf("ts:%lld\n",t1-t0);
    }
}
EOF

/usr/local/cuda/bin/nvcc -std=c++17  -dc -lineinfo -arch=sm_86 -ptx shm_kernel.cu -o shm_kernel.ptx
/usr/local/cuda/bin/nvcc -arch=sm_86 shm_kernel.ptx -cubin -o shm_kernel.cubin
/usr/local/cuda/bin/nvcc -arch=sm_86 shm_kernel.cubin -fatbin -o shm_kernel.fatbin
/usr/local/cuda/bin/cuobjdump --dump-sass shm_kernel.fatbin
 
tee shm_kernel_main.cpp<<-'EOF'
#include <stdio.h>
#include <string.h>
#include <cuda_runtime.h>
#include <cuda.h>

int main(int argc,char *argv[])
{
    CUresult error;
    CUdevice cuDevice;
    cuInit(0);
    int deviceCount = 0;
    error = cuDeviceGetCount(&deviceCount);
    error = cuDeviceGet(&cuDevice, 0);
    if(error!=CUDA_SUCCESS)
        {
        printf("Error happened in get device!\n");
    }
    CUcontext cuContext;
    error = cuCtxCreate(&cuContext, 0, cuDevice);
    if(error!=CUDA_SUCCESS)
        {
        printf("Error happened in create context!\n");
    }

    CUmodule module;
    CUfunction function;

    const char* module_file = "shm_kernel.fatbin";
    const char* kernel_name = "_Z10shm_kernelPfS_";

    error = cuModuleLoad(&module, module_file);
    if(error!=CUDA_SUCCESS)
        {
        printf("Error happened in load moudle %d!\n",error);
    }

    error = cuModuleGetFunction(&function, module, kernel_name);
    if(error!=CUDA_SUCCESS)
    {
        printf("get function error!\n");
    }
    
    int thread_size_conf[3]={32,32*4,32*4*4};
    
    for(int k=0;k<3;k++)
    {
        int block_size=1;
        int thread_size=thread_size_conf[k];
        int data_size=sizeof(float)*thread_size*block_size;

        float *output_ptr=nullptr;
        float *input_ptr=nullptr;
        int cudaStatus=0;

        cudaStatus = cudaMalloc((void**)&input_ptr, data_size);
        if(cudaStatus)
        {
            printf("cudaMalloc input_ptr Failed\n");
        }
        cudaStatus= cudaMalloc((void**)&output_ptr, data_size);
        if(cudaStatus)
        {
            printf("cudaMalloc output_ptr Failed\n");        
        }
        void *kernelParams[]= {(void*)&output_ptr, (void*)&input_ptr};
        
        auto ret=cuLaunchKernel(function,
                    block_size, 1, 1,
                    thread_size, 1, 1,
                    0,0,kernelParams, 0);
        cudaError_t cudaerr = cudaDeviceSynchronize();
        if (cudaerr != cudaSuccess)
            printf("kernel launch failed with error \"%s\".\n",cudaGetErrorString(cudaerr));  
       
        cudaFree(output_ptr);
        cudaFree(input_ptr);        
    }
    cuModuleUnload(module);
    cuCtxDestroy(cuContext);
    return 0;
}
EOF
g++ shm_kernel_main.cpp -o shm_kernel_main -I /usr/local/cuda/include -L /usr/local/cuda/lib64 -lcudart -lcuda

/usr/local/NVIDIA-Nsight-Compute/ncu --metrics l1tex__data_bank_conflicts_pipe_lsu_mem_shared_op_ld.sum,\
smsp__sass_l1tex_data_bank_conflicts_pipe_lsu_mem_shared_op_ldgsts.sum,\
smsp__sass_inst_executed_op_shared_ld.sum,\
l1tex__data_pipe_lsu_wavefronts_mem_shared_op_ld.sum.peak_sustained,\
l1tex__data_pipe_lsu_wavefronts_mem_shared_op_ld.avg.peak_sustained,\
l1tex__data_pipe_lsu_wavefronts_mem_shared_op_ld.sum ./shm_kernel_main

输出

bash 复制代码
ts:33
ts:1551
0%....50%....100% - 3 passes
==PROF== Profiling "shm_kernel(float *, float *)" - 1: ts:39
ts:39
ts:1622
0%....50%....100% - 3 passes
==PROF== Profiling "shm_kernel(float *, float *)" - 2: ts:64
ts:57
ts:1706
0%....50%....100% - 3 passes
==PROF== Disconnected from process 657443
[657443] shm_kernel_main@127.0.0.1
  shm_kernel(float *, float *) (1, 1, 1)x(32, 1, 1), Context 1, Stream 7, Device 0, CC 8.6
    Section: Command line profiler metrics
    ---------------------------------------------------------------------- ----------- ------------
    Metric Name                                                            Metric Unit Metric Value
    ---------------------------------------------------------------------- ----------- ------------
    l1tex__data_bank_conflicts_pipe_lsu_mem_shared_op_ld.sum                                      0
    l1tex__data_pipe_lsu_wavefronts_mem_shared_op_ld.avg.peak_sustained        1/cycle            1
    l1tex__data_pipe_lsu_wavefronts_mem_shared_op_ld.sum                                          1
    l1tex__data_pipe_lsu_wavefronts_mem_shared_op_ld.sum.peak_sustained        1/cycle           28
    smsp__sass_inst_executed_op_shared_ld.sum                                     inst            1
    smsp__sass_l1tex_data_bank_conflicts_pipe_lsu_mem_shared_op_ldgsts.sum                        0
    ---------------------------------------------------------------------- ----------- ------------

  shm_kernel(float *, float *) (1, 1, 1)x(128, 1, 1), Context 1, Stream 7, Device 0, CC 8.6
    Section: Command line profiler metrics
    ---------------------------------------------------------------------- ----------- ------------
    Metric Name                                                            Metric Unit Metric Value
    ---------------------------------------------------------------------- ----------- ------------
    l1tex__data_bank_conflicts_pipe_lsu_mem_shared_op_ld.sum                                      0
    l1tex__data_pipe_lsu_wavefronts_mem_shared_op_ld.avg.peak_sustained        1/cycle            1
    l1tex__data_pipe_lsu_wavefronts_mem_shared_op_ld.sum                                          4
    l1tex__data_pipe_lsu_wavefronts_mem_shared_op_ld.sum.peak_sustained        1/cycle           28
    smsp__sass_inst_executed_op_shared_ld.sum                                     inst            4
    smsp__sass_l1tex_data_bank_conflicts_pipe_lsu_mem_shared_op_ldgsts.sum                        0
    ---------------------------------------------------------------------- ----------- ------------

  shm_kernel(float *, float *) (1, 1, 1)x(512, 1, 1), Context 1, Stream 7, Device 0, CC 8.6
    Section: Command line profiler metrics
    ---------------------------------------------------------------------- ----------- ------------
    Metric Name                                                            Metric Unit Metric Value
    ---------------------------------------------------------------------- ----------- ------------
    l1tex__data_bank_conflicts_pipe_lsu_mem_shared_op_ld.sum                                      0
    l1tex__data_pipe_lsu_wavefronts_mem_shared_op_ld.avg.peak_sustained        1/cycle            1
    l1tex__data_pipe_lsu_wavefronts_mem_shared_op_ld.sum                                         16
    l1tex__data_pipe_lsu_wavefronts_mem_shared_op_ld.sum.peak_sustained        1/cycle           28
    smsp__sass_inst_executed_op_shared_ld.sum                                     inst           16
    smsp__sass_l1tex_data_bank_conflicts_pipe_lsu_mem_shared_op_ldgsts.sum                        0
    ---------------------------------------------------------------------- ----------- ------------
相关推荐
~奔跑的简默~3 小时前
GO语言性能分析
开发语言·性能优化·golang
Hi202402179 小时前
smsp__inst_executed_pipe_fp64为什么对不上
性能优化·gpu·cuda·性能分析·gpgpu
杰克逊的日记1 天前
nvdia和cuda的区别与联系
cuda·nvdia
杰克逊的日记1 天前
PyTorch----模型运维与实战
人工智能·pytorch·python·gpu
summer@彤妈1 天前
遇到僵尸进程,怎么处理---学习笔记
linux·性能优化
Flying_Fish_roe1 天前
mysql性能优化-云服务与数据库即服务(DBaaS)优化
数据库·mysql·性能优化
盼盼盼1 天前
react Refine 框架在性能优化方面有哪些具体的技术手段?
前端·react.js·性能优化
来一杯龙舌兰1 天前
【JAVA】Undertow的使用及性能优化,以及Undertow与Tomcat的对比
java·性能优化·tomcat·undertow·容器配置
陈 洪 伟2 天前
虚拟内存、内存分段、分页、CUDA编程中的零拷贝
mmu·cuda·虚拟内存
别NULL2 天前
DPDK基础入门(七):网卡性能优化
网络·性能优化