示例代码
使用mstx API执行采集操作示例代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 | 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去初始化
|
父主题: mstx API参考