shfl
产品支持情况
产品 |
是否支持 |
|---|---|
Atlas 350 加速卡 |
√ |
x |
|
x |
|
x |
|
x |
|
x |
|
x |
功能说明
coalesced_group组内线程的数据交换接口,不通过共享内存实现直接读取组内指定线程的寄存器中的值。
函数原型
1 2 | template <typename T> T shfl(T var, int src_rank) const |
参数说明
参数名 |
输入/输出 |
描述 |
|---|---|---|
T |
输入 |
var的数据类型,支持half、int32_t、uint32_t、float、half2、int64_t、uint64_t。 |
参数名 |
输入/输出 |
描述 |
|---|---|---|
var |
输入 |
线程用于交换的数据。 |
src_rank |
输入 |
期望获取的var值所在的线程在组内的排名。当src_rank大于等于组内线程数时,获取src_rank% num_threads()对应rank线程的var值。 |
返回值说明
coalesced_group组内指定线程输入的var值。
约束说明
无
调用示例
示例代码中的条件分支将一个warp中所有线程id是偶数的线程组成coalesced_group协作组,组内各线程shfl接口返回结果如下图所示。
图1 shfl结果示意图


- SIMT编程场景:
1 2 3 4 5 6 7 8 9 10
using namespace cooperative_groups; __global__ void simt_kernel(...) { ... if (threadIdx.x % 2 == 0) { coalesced_group active = coalesced_threads(); uint32_t result = active.shfl(threadIdx.x + 100, 3); } ... }
- SIMD与SIMT混合编程场景:
1 2 3 4 5 6 7 8 9 10
using namespace cooperative_groups; __simt_vf__ inline void simt_kernel(...) { ... if (threadIdx.x % 2 == 0) { coalesced_group active = coalesced_threads(); uint32_t result = active.shfl(threadIdx.x + 100, 3); } ... }
父主题: coalesced_group