asc_scatter
产品支持情况
- Ascend 950PR&950DT系列产品:支持
- Atlas A3系列产品:不支持
- Atlas A2系列产品:不支持
- Atlas 200I/500 A2推理产品:不支持
- Atlas推理系列产品AI Core:不支持
- Atlas推理系列产品Vector Core:不支持
- Atlas训练系列产品:不支持
功能说明
头文件路径为:"c_api/reg_compute/scatter/ub_scatter.h"。
根据索引寄存器中的索引值,将源矢量数据寄存器中的元素分散搬出到Unified Buffer(UB)。mask用于指示参与搬出的元素,掩码为1的源元素写入dst中对应索引位置,掩码为0的源元素不写入目的地址,保留目的地址原数据。搬运过程中数据格式与内容保持不变。
分散搬出过程如下图所示。
图1 分散搬出数据

本接口为Reg矢量搬运接口,仅在AIV上生效。
函数原型
C
// 占位符形式
__simd_callee__ inline void asc_scatter(__ubuf__ <dtype>* dst,
vector_<dtype> src,
vector_<index_dtype> index,
vector_bool mask)
dtype支持数据类型
表1 dtype和index_dtype对应关系
| dtype | index_dtype |
|---|---|
int8_t | uint16_t |
uint8_t | uint16_t |
int16_t | uint16_t |
uint16_t | uint16_t |
half | uint16_t |
bfloat16_t | uint16_t |
int32_t | uint32_t |
uint32_t | uint32_t |
float | uint32_t |
函数原型典型示例
C
__simd_callee__ inline void asc_scatter(__ubuf__ int8_t* dst,
vector_int8_t src,
vector_uint16_t index,
vector_bool mask)
参数说明
表2 参数说明
| 参数名 | 输入/输出 | 描述 |
|---|---|---|
| dst | 输出 | 目的操作数在UB中的起始地址,类型为__ubuf__ <dtype>*,起始地址需32字节对齐。 |
| src | 输入 | 源操作数(矢量数据寄存器),数据类型须与dst指向的数据类型一致。 |
| index | 输入 | 数据索引(矢量数据寄存器),单位为元素。 |
| mask | 输入 | 源操作数掩码(掩码寄存器),用于指示在计算过程中哪些元素参与搬运。对应位置为1时参与计算,为0时不参与计算,保留目的地址原数据。 |
矢量数据寄存器和掩码寄存器的详细说明请参见reg数据类型定义。
返回值说明
无
约束说明
通用约束
- Reg矢量计算C API通用约束请参见通用约束。
- 源操作数和索引为矢量数据寄存器,目的操作数为UB地址。
dst起始地址需32字节对齐。- UB容量上限为256KB,用户可用容量随编译选项与编程场景变化(默认预留6KB SIMD VF栈和2KB Ascend C预留空间,可用248KB;SIMD与SIMT混合编程时再划分32KB~128KB作为Data Cache,可用容量进一步减少)。
index索引后的实际写入地址不可超过实际可用容量。 - 如果本指令与其他指令存在数据依赖,需要插入同步指令asc_mem_bar,保证多个指令串行化,防止出现异常数据。
分散搬出约束
mask比特位为0的源元素不参与搬运,其对应目的地址中的原数据保持不变。- 当
dst为int8_t或uint8_t数据类型时,源操作数中仅偶数位置的元素有效,即src中位置为0、2、4、...、252、254的元素会被分散搬出到目的操作数中。 index中的有效索引值必须唯一。若存在重复的有效索引值,系统仅保留其中一个索引值对应的数据,其余数据将被忽略,且无法确定具体保留的数据。index索引后的实际写入地址需落在UB有效地址范围内。
调用示例
将代码保存为example.asc后,可通过bisheng命令编译运行,其中--npu-arch参数需根据实际产品型号指定对应的NPU架构,具体产品与NPU架构的映射关系请参考__NPU_ARCH__。
以Ascend 950PR&950DT系列产品(对应NPU架构为dav-3510)为例,编译运行命令如下:
Bash
bisheng example.asc -o main --npu-arch=dav-3510 && ./main
C
#include <cstdint>
#include <iostream>
#include <vector>
#include "c_api/asc_simd.h"
#include "utils/std/cmath.h"
#include "acl/acl.h"
namespace {
constexpr uint8_t MUTEX_ID = 0;
constexpr uint32_t ELEMENTS_PER_REGISTER = 128;
constexpr uint32_t ELEMENT_COUNT = 133;
constexpr uint32_t REPEAT_TIMES = 3;
constexpr uint32_t OUTPUT_COUNT = REPEAT_TIMES * ELEMENT_COUNT;
constexpr uint16_t SENTINEL = 0xffff;
constexpr uint32_t INPUT_BYTES = ELEMENT_COUNT * sizeof(uint16_t);
constexpr uint32_t OUTPUT_BYTES = OUTPUT_COUNT * sizeof(uint16_t);
constexpr uint32_t DATA_BYTES = INPUT_BYTES;
// 统一记录实际处理长度,用于计算迭代次数并限制尾轮的有效范围。
constexpr uint32_t TOTAL_LENGTH = ELEMENT_COUNT;
__simd_vf__ inline void scatter_vf(__ubuf__ uint16_t* output,
__ubuf__ uint16_t* input,
__ubuf__ uint16_t* indices, uint32_t one_repeat_size, uint16_t repeat_time)
{
vector_uint16_t src;
vector_uint16_t index;
asc_loadalign(src, input);
asc_loadalign(index, indices);
for (uint32_t repeat = 0; repeat < REPEAT_TIMES; ++repeat) {
// 每次重新遍历输入时重置计数,内层循环由asc_update_mask_b16自动更新。
uint32_t remaining_length = ELEMENT_COUNT;
for (uint16_t i = 0; i < repeat_time; ++i) {
const uint32_t offset = i * one_repeat_size;
vector_bool mask = asc_update_mask_b16(remaining_length);
asc_loadalign(src, input + offset);
asc_loadalign(index, indices + offset);
asc_scatter(output + repeat * ELEMENT_COUNT + offset, src, index, mask);
}
}
}
__global__ __vector__ void asc_scatter_kernel(__gm__ uint16_t* output,
__gm__ uint16_t* input,
__gm__ uint16_t* indices)
{
asc_init();
__ubuf__ uint16_t output_local[OUTPUT_COUNT];
__ubuf__ uint16_t input_local[ELEMENT_COUNT];
__ubuf__ uint16_t index_local[ELEMENT_COUNT];
asc_lock(PIPE_MTE2, MUTEX_ID);
asc_copy_gm2ub_align(output_local, output, OUTPUT_BYTES);
asc_copy_gm2ub_align(input_local, input, INPUT_BYTES);
asc_copy_gm2ub_align(index_local, indices, INPUT_BYTES);
asc_unlock(PIPE_MTE2, MUTEX_ID);
uint32_t one_repeat_size = static_cast<uint32_t>(asc_get_vf_len() / sizeof(uint16_t));
uint16_t repeat_time = static_cast<uint16_t>(AscendC::Std::ceil_div(TOTAL_LENGTH, one_repeat_size));
asc_lock(PIPE_V, MUTEX_ID);
asc_vf_call<scatter_vf>(output_local, input_local, index_local, one_repeat_size, repeat_time);
asc_unlock(PIPE_V, MUTEX_ID);
asc_lock(PIPE_MTE3, MUTEX_ID);
asc_copy_ub2gm_align(output, output_local, OUTPUT_BYTES);
asc_unlock(PIPE_MTE3, MUTEX_ID);
asc_sync();
}
} // namespace
int main()
{
std::vector<uint16_t> input(ELEMENT_COUNT);
std::vector<uint16_t> indices(ELEMENT_COUNT);
std::vector<uint16_t> output(OUTPUT_COUNT, SENTINEL);
for (uint32_t i = 0; i < ELEMENT_COUNT; ++i) {
input[i] = static_cast<uint16_t>(i + 1);
const uint32_t block_count = (i < ELEMENTS_PER_REGISTER) ? ELEMENTS_PER_REGISTER : ELEMENT_COUNT - ELEMENTS_PER_REGISTER;
indices[i] = static_cast<uint16_t>((i * 3) % block_count);
}
if (aclInit(nullptr) != ACL_SUCCESS || aclrtSetDevice(0) != ACL_SUCCESS) return 1;
uint16_t* input_device = nullptr;
uint16_t* index_device = nullptr;
uint16_t* output_device = nullptr;
if (aclrtMalloc(reinterpret_cast<void**>(&input_device), INPUT_BYTES, ACL_MEM_MALLOC_HUGE_FIRST) != ACL_SUCCESS ||
aclrtMalloc(reinterpret_cast<void**>(&index_device), INPUT_BYTES, ACL_MEM_MALLOC_HUGE_FIRST) != ACL_SUCCESS ||
aclrtMalloc(reinterpret_cast<void**>(&output_device), OUTPUT_BYTES, ACL_MEM_MALLOC_HUGE_FIRST) != ACL_SUCCESS) {
return 1;
}
if (aclrtMemcpy(input_device, INPUT_BYTES, input.data(), INPUT_BYTES, ACL_MEMCPY_HOST_TO_DEVICE) != ACL_SUCCESS ||
aclrtMemcpy(index_device, INPUT_BYTES, indices.data(), INPUT_BYTES, ACL_MEMCPY_HOST_TO_DEVICE) != ACL_SUCCESS ||
aclrtMemcpy(output_device, OUTPUT_BYTES, output.data(), OUTPUT_BYTES, ACL_MEMCPY_HOST_TO_DEVICE) != ACL_SUCCESS) {
return 1;
}
asc_scatter_kernel<<<1, 0>>>(output_device, input_device, index_device);
if (aclrtSynchronizeDevice() != ACL_SUCCESS ||
aclrtMemcpy(output.data(), OUTPUT_BYTES, output_device, OUTPUT_BYTES, ACL_MEMCPY_DEVICE_TO_HOST) != ACL_SUCCESS) {
return 1;
}
bool passed = true;
for (uint32_t repeat = 0; repeat < REPEAT_TIMES && passed; ++repeat) {
for (uint32_t i = 0; i < ELEMENT_COUNT; ++i) {
const uint16_t expected = input[i];
const uint32_t block_offset = (i / ELEMENTS_PER_REGISTER) * ELEMENTS_PER_REGISTER;
const uint32_t position = repeat * ELEMENT_COUNT + block_offset + indices[i];
if (output[position] != expected) {
std::cerr << "Mismatch at output[" << position << "]: expected " << expected
<< ", actual " << output[position] << std::endl;
passed = false;
break;
}
}
}
aclrtFree(input_device);
aclrtFree(index_device);
aclrtFree(output_device);
aclrtResetDevice(0);
aclFinalize();
std::cout << (passed ? "[Success] asc_scatter passed." : "[Failed] asc_scatter failed.") << std::endl;
return passed ? 0 : 1;
}