asc_scatter
产品支持情况
- Ascend 950PR/Ascend 950DT:支持
- Atlas A3 训练系列产品/Atlas A3 推理系列产品:不支持
- Atlas A2 训练系列产品/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的源元素不写入目的地址,保留目的地址原数据。搬运过程中数据格式与内容保持不变。本接口在Vector Function(__simd_vf__标记的函数)内使用。
分散搬出过程如下图所示。
图1 分散搬出数据

本接口仅在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数据类型定义。
返回值说明
无
约束说明
通用约束
- 本接口在非AIV上调用直接返回。
- 本接口在Vector Function(
__simd_vf__标记的函数)内调用,源操作数和索引为矢量数据寄存器,目的操作数为UB地址。 dst起始地址需32字节对齐。- UB容量上限为256KB,用户可用容量随编译选项与编程场景变化(默认预留6KB SIMD VF栈和2KB Ascend C预留空间,可用248KB;SIMD与SIMT混合编程时再划分32KB~128KB作为Data Cache,可用容量进一步减少)。
index索引后的实际写入地址不可超过实际可用容量。 mask需通过掩码设置接口预先赋值后再传入;未赋值的掩码寄存器内容不确定,会导致有效元素位置错误。- 如果本指令与其他指令存在UB地址重叠,需要插入同步指令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/Ascend 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 "acl/acl.h"
namespace {
constexpr uint32_t ELEMENT_COUNT = 128;
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);
__simd_vf__ inline void scatter_vf(__ubuf__ uint16_t* output,
__ubuf__ uint16_t* input,
__ubuf__ uint16_t* indices)
{
vector_uint16_t src;
vector_uint16_t index;
asc_loadalign(src, input);
asc_loadalign(index, indices);
vector_bool mask = asc_create_mask_b16(PAT_M4);
for (uint32_t repeat = 0; repeat < REPEAT_TIMES; ++repeat) {
asc_scatter(output + repeat * ELEMENT_COUNT, 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_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_sync_notify(PIPE_MTE2, PIPE_V, EVENT_ID0);
asc_sync_wait(PIPE_MTE2, PIPE_V, EVENT_ID0);
scatter_vf(output_local, input_local, index_local);
asc_sync_notify(PIPE_V, PIPE_MTE3, EVENT_ID0);
asc_sync_wait(PIPE_V, PIPE_MTE3, EVENT_ID0);
asc_copy_ub2gm_align(output, output_local, OUTPUT_BYTES);
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);
indices[i] = static_cast<uint16_t>((i * 3) % ELEMENT_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 = (i % 4 == 0) ? input[i] : SENTINEL;
const uint32_t position = repeat * ELEMENT_COUNT + 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;
}