示例代码

使用mstx API执行采集操作示例代码如下:

aclrtContext context_;
aclrtStream stream_;
 
// 1.AscendCL初始化
 
// 2.申请运行管理资源,包括设置用于计算的Device、创建Context、创建Stream
aclError aclRet = ACL_ERROR_NONE;
aclRet = aclprofStart(config);
if (aclRet != ACL_ERROR_NONE) {
    ERROR_LOG("acl prof start failed");
    return FAILED;
}

ret = aclrtSetDevice(0);
if (ret != ACL_ERROR_NONE) {
    ERROR_LOG("aclrtSetDevice faile");
    return FAILED;
}
ret = aclrtCreateContext(&context_, 0);
if (ret != ACL_ERROR_NONE) {
    ERROR_LOG("acl create context failed");
    return FAILED;
}
aclError ret = aclrtCreateStream(&stream_);
if (ret != ACL_ERROR_NONE) {
    ERROR_LOG("acl create stream failed");
    return FAILED;
}
....
 
// 3.Profiling初始化
// 设置数据落盘路径
const char *aclProfPath = "...";
aclprofInit(aclProfPath, strlen(aclProfPath));
 
// 4.进行Profiling配置
uint32_t deviceIdList[1] = {0}; // 须根据实际环境的Device ID配置
// 创建配置结构体
aclprofConfig *config = aclprofCreateConfig(deviceIdList, 1, ACL_AICORE_ARITHMETIC_UTILIZATION,
nullptr,ACL_PROF_ACL_API | ACL_PROF_TASK_TIME | ACL_PROF_MSPROFTX);
 
// 5.开启profiling
ret = aclprofStart(config);
....
 
 
// 6.添加打点,比如在执行模型前后
mstxRangeId id = mstxRangeStartA("model execute", nullptr); // 第二个如此设置nullptr,只记录host侧range耗时(适用于纯host侧代码段);设置有效的stream,同时记录host侧和对应device侧耗时(适用于下发计算或通信的代码段)
ret = aclmdlExecute(modelId, input, output);
mstxRangeEnd(id);
 
// 7.其他操作
 
// 8.停止prof采集
aclprofStop(config);
aclprofDestroyConfig(config);
aclprofFinalize();
 
// 9.释放运行管理资源
 
// 10.AscendCl去初始化