CUDA例程:cdpSimplePrint

概念

CUDA:Compute unified device architecture    统一计算架构; 计算统一设备架构; 统一计算设备架构(百科)

CDP:CUDA dynamic parallelism    CUDA动态并行特性

核函数:__global__ func<<<m,n>>>

线程栅格:每个核函数 func 都会有一个线程栅格 <<<m,n>>>

线程块:m,表示栅格中包含m个线程块

线程:n,表示线程块中包含n个线程

例子功能

核函数打印当前线程栅格中所有id为0的线程,并且递归调用,max_depth为递归深度

核函数

__global__ void cdp_kernel(int max_depth, int depth, int thread, int parent_uid)
{
    // We create a unique ID per block. Thread 0 does that and shares the value with the other threads.
    __shared__ int s_uid;

    if (threadIdx.x == 0)
    {
        s_uid = atomicAdd(&g_uids, 1);
    }

    __syncthreads();

    // We print the ID of the block and information about its parent.
    print_info(depth, thread, s_uid, parent_uid);

    // We launch new blocks if we haven't reached the max_depth yet.
    if (++depth >= max_depth)
    {
        return;
    }

    cdp_kernel<<<gridDim.x, blockDim.x>>>(max_depth, depth, threadIdx.x, s_uid);
}

atomicAdd()  :原子++,可以查看cuda c programming guide

 __syncthreads() :同步等待同一线程块中所有的线程


本例中,打印结果会随着迭代深度、核函数m、n的变化而变化,可以试试加深理解


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值