asc_sync
产品支持情况
- 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"。
用于同一核内所有流水之间的全部流水同步,功能等价于asc_sync_pipe(PIPE_ALL)。阻塞调用点后所有硬件流水的后序指令,直到调用点之前所有硬件流水的前序指令全部完成。
本接口适用于需要等待核内所有并行执行单元同时到达一个一致状态后再继续执行的场景。
函数原型
__aicore__ inline void asc_sync()
参数说明
无
返回值说明
无
流水类型
PIPE_S
约束说明
asc_sync会等待所有流水中所有先前提交的接口完成,这会对性能产生影响。若仅需阻塞单条流水,应避免随意调用asc_sync,可使用asc_sync_pipe接口指定某个流水的阻塞。
调用示例
将代码保存为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_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();
add_vf(z, x, y);
asc_sync();
asc_copy_ub2gm_align(output, z, BYTES);
asc_sync();
}
} // 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_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 passed." : "[Failed] asc_sync failed.") << std::endl;
aclrtFree(src0_device);
aclrtFree(src1_device);
aclrtFree(output_device);
aclrtResetDevice(0);
aclFinalize();
return passed ? 0 : 1;
}