asc_sync_pipe
产品支持情况
- 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/sync/sync.h"。
完成同一流水内的同步控制,用于在同一流水内部约束执行顺序:
- 传入
PIPE_ALL时等待所有流水的前序指令全部完成后才会对后序指令解除阻塞,此时功能等价于asc_sync。 - 传入其他合法流水值时仅阻塞该流水的后续指令,直至该流水前序指令全部完成,不影响其他流水线的指令执行。
如图1所示,其作用是保证指定流水线中前序指令的所有数据读写全部完成后,该流水的后序指令才能开始执行。
图1 asc_sync_pipe接口功能示意图

函数原型
__aicore__ inline void asc_sync_pipe(pipe_t pipe)
参数说明
表1 参数说明
| 参数名 | 输入/输出 | 描述 |
|---|---|---|
| pipe | 输入 | 阻塞的流水类别。支持的取值有PIPE_V、PIPE_M、PIPE_MTE1、PIPE_MTE2、PIPE_MTE3、PIPE_FIX、PIPE_ALL,不支持PIPE_S。如果不关注流水类别,希望阻塞所有流水,可以传入PIPE_ALL。参数的类型是 pipe_t枚举,各个枚举取值的含义请参考硬件流水类型。 |
返回值说明
无
流水类型
PIPE_S
约束说明
- Scalar流水之间的同步由硬件自动保证,
asc_sync_pipe接口不支持PIPE_S单流水的同步。
- 针对Ascend 950PR/Ascend 950DT,
PIPE_V之间的同步由硬件自动保证。
asc_sync_pipe(PIPE_ALL)会等待所有流水中所有先前提交的接口完成,这会对性能产生影响。若仅阻塞单条流水即可解决问题,应避免随意调用asc_sync_pipe(PIPE_ALL)。PIPE_MTE2/PIPE_MTE3在搬运的目的地址有重叠的情况下需要开发者插入同步,保证多个搬运指令的串行化,防止出现异常数据。
调用示例
将代码保存为example.asc后,可通过bisheng命令编译运行,其中--npu-arch参数需根据实际产品型号指定对应的NPU架构,具体产品与NPU架构的映射关系请参考__NPU_ARCH__。
以Ascend 950PR/Ascend 950DT产品(对应NPU架构为dav-3510)为例,编译运行命令如下:
bisheng example.asc -o main --npu-arch=dav-3510 && ./main
#include <cstdint>
#include <iostream>
#include <vector>
#include "c_api/asc_simd.h"
#include "acl/acl.h"
namespace {
constexpr uint32_t ELEMENTS = 64;
constexpr uint32_t BYTES = ELEMENTS * sizeof(float);
__simd_vf__ inline void add_vf(__ubuf__ float* dst, __ubuf__ float* src0, __ubuf__ float* src1)
{
vector_float dst_reg;
vector_float src0_reg;
vector_float src1_reg;
uint32_t count = ELEMENTS;
vector_bool mask = asc_update_mask_b32(count);
asc_loadalign(src0_reg, src0);
asc_loadalign(src1_reg, src1);
asc_add(dst_reg, src0_reg, src1_reg, mask);
asc_storealign(dst, dst_reg, mask);
}
void print_data(const char* label, const std::vector<float>& data)
{
std::cout << label << ":";
for (uint32_t i = 0; i < 8; ++i) std::cout << ' ' << data[i];
std::cout << " ..." << std::endl;
}
__global__ __vector__ void asc_sync_pipe_kernel(__gm__ float* output, __gm__ float* src0, __gm__ float* src1)
{
asc_init();
__ubuf__ float x[ELEMENTS], y[ELEMENTS], z[ELEMENTS];
asc_copy_gm2ub_align(x, src0, BYTES);
asc_copy_gm2ub_align(y, src1, BYTES);
asc_sync_pipe(PIPE_ALL);
add_vf(z, x, y);
asc_sync_pipe(PIPE_ALL);
asc_copy_ub2gm_align(output, z, BYTES);
asc_sync_pipe(PIPE_ALL);
}
} // namespace
int main()
{
std::vector<float> src0(ELEMENTS), src1(ELEMENTS), output(ELEMENTS, 0.0f), golden(ELEMENTS);
for (uint32_t i = 0; i < ELEMENTS; ++i) {
src0[i] = static_cast<float>(i) * 0.25f;
src1[i] = static_cast<float>(ELEMENTS - i) * 0.5f;
golden[i] = src0[i] + src1[i];
}
aclInit(nullptr);
aclrtSetDevice(0);
float *src0_device = nullptr, *src1_device = nullptr, *output_device = nullptr;
aclrtMalloc(reinterpret_cast<void**>(&src0_device), BYTES, ACL_MEM_MALLOC_HUGE_FIRST);
aclrtMalloc(reinterpret_cast<void**>(&src1_device), BYTES, ACL_MEM_MALLOC_HUGE_FIRST);
aclrtMalloc(reinterpret_cast<void**>(&output_device), BYTES, ACL_MEM_MALLOC_HUGE_FIRST);
aclrtMemcpy(src0_device, BYTES, src0.data(), BYTES, ACL_MEMCPY_HOST_TO_DEVICE);
aclrtMemcpy(src1_device, BYTES, src1.data(), BYTES, ACL_MEMCPY_HOST_TO_DEVICE);
asc_sync_pipe_kernel<<<1, 0>>>(output_device, src0_device, src1_device);
aclrtSynchronizeDevice();
aclrtMemcpy(output.data(), BYTES, output_device, BYTES, ACL_MEMCPY_DEVICE_TO_HOST);
print_data("Input src0", src0);
print_data("Input src1", src1);
print_data("Output", output);
print_data("Golden", golden);
const bool passed = output == golden;
std::cout << (passed ? "[Success] asc_sync_pipe passed." : "[Failed] asc_sync_pipe failed.") << std::endl;
aclrtFree(src0_device);
aclrtFree(src1_device);
aclrtFree(output_device);
aclrtResetDevice(0);
aclFinalize();
return passed ? 0 : 1;
}