---
title: GatherB
description: "| 产品 | 是否支持 |"
url: https://www.hiascend.com/document/detail/zh/canncommercial/latest/API/ascendcopapi/atlasascendc_api_07_0402.html
sourcePath: /source/zh/canncommercial/900/API/ascendcopapi/atlasascendc_api_07_0402.html
indexId: ac9c10d60b64b88e5067088ab9cde6a6d903dd35a552c434bd5ef60c0b82b15f76
---
# GatherB

#### 产品支持情况

| 产品 | 是否支持 |
| --- | --- |
| Atlas 350 加速卡 | √ |
| Atlas A3 训练系列产品 / Atlas A3 推理系列产品 | x |
| Atlas A2 训练系列产品 / Atlas A2 推理系列产品 | x |
| Atlas 200I/500 A2 推理产品 | x |
| Atlas 推理系列产品 AI Core | x |
| Atlas 推理系列产品 Vector Core | x |
| Atlas 训练系列产品 | x |


#### 功能说明

给定源操作数在UB中的基地址和索引，GatherB指令根据索引位置将源操作数按DataBlock收集到结果寄存器张量中。每个DataBlock长度为32Byte。收集过程如下图所示：


#### 定义原型

```
template <typename T = DefaultType, typename U, typename S>
__simd_callee__ inline void GatherB(U& dstReg, __ubuf__ T* baseAddr, S& index, MaskReg& mask)
```


#### 参数说明


**表1 模板参数说明**

| 参数名 | 描述 |
| --- | --- |
| T | 目的操作数和源操作数的数据类型 |
| U | 目的操作数的RegTensor类型， 例如RegTensor<half>，由编译器自动推导，用户不需要填写 |
| S | 索引值的RegTensor类型，例如RegTensor<uint32\_t>，由编译器自动推导，用户不需要填写 |


**表2 函数参数说明**

| 参数名 | 输入/输出 | 描述 |
| --- | --- | --- |
| dstReg | 输出 | 目的操作数。 类型为RegTensor Atlas 350 加速卡，dstReg支持的数据类型为：uint8\_t/int8\_t/uint16\_t/int16\_t/uint32\_t/int32\_t/half/float/bfloat16\_t/uint64\_t/int64\_t |
| baseAddr | 输入 | 源操作数在UB中的基地址。需要32B对齐。 类型为UB指针。 Atlas 350 加速卡，支持的数据类型为：uint8\_t/int8\_t/uint16\_t/int16\_t/uint32\_t/int32\_t/half/float/bfloat16\_t/uint64\_t/int64\_t |
| index | 输入 | dstReg中的每个DataBlock在UB中相对于baseAddr的索引位置。索引位置要大于等于0且32B对齐。 类型为RegTensor Atlas 350 加速卡，支持的数据类型为：uint32\_t |
| mask | 输入 | src element操作有效指示，详细说明请参考MaskReg |


#### 约束说明

- 目的操作数与源操作数的数据类型相同。
- 源操作数在UB中的基地址需要32B对齐。
- 索引位置要大于等于0且32B对齐。
- 索引寄存器中可以存在相同的值，即可以多次读取源操作数中同一个DataBlock的数据。


#### 调用示例

```
template<typename T, typename U>
__simd_vf__ inline void GatherBVF(__ubuf__ T* dstAddr, __ubuf__ T* src0Addr, __ubuf__ U* src1Addr, uint32_t count, uint32_t oneRepeatSize, uint16_t repeatTimes)
{
AscendC::Reg::RegTensor<U> srcReg;
AscendC::Reg::RegTensor<T> dstReg;
AscendC::Reg::MaskReg mask;
for (uint16_t i = 0; i < repeatTimes; i++) {
mask = AscendC::Reg::UpdateMask<T>(count);
AscendC::Reg::LoadAlign(srcReg, src1Addr + i * oneRepeatSize);
AscendC::Reg::GatherB(dstReg, src0Addr, srcReg, mask);
AscendC::Reg::StoreAlign(dstAddr + i * oneRepeatSize, dstReg, mask);
}
}
```
