Sample Code
Use the mstx API to profile data:
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. Initialize AscendCL. // 2. Allocate runtime resources, including setting the compute device and creating a context and a 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. Initialize profiling. // Set the data flush path. const char *aclProfPath = "..."; aclprofInit(aclProfPath, strlen(aclProfPath)); // 4. Configure profiling. uint32_t deviceIdList[1] = {0}; // Set this parameter based on the device ID in the actual environment. // Create a configuration struct. aclprofConfig *config = aclprofCreateConfig(deviceIdList, 1, ACL_AICORE_ARITHMETIC_UTILIZATION, nullptr,ACL_PROF_ACL_API | ACL_PROF_TASK_TIME | ACL_PROF_MSPROFTX); // 5. Enable profiling. ret = aclprofStart(config); .... // 6. Add dots, for example, before and after model execution. mstxRangeId id = mstxRangeStartA("model execute", nullptr); // Set the second input parameter to nullptr, which records only the time consumed by the range on the host (applicable to the code snippet on the host). A valid stream is set, and the time consumed by the host and the corresponding device is recorded (applicable to the code snippet for delivering computation or communication). ret = aclmdlExecute(modelId, input, output); mstxRangeEnd(id); // 7. Other operations // 8. Stop profiling. aclprofStop(config); aclprofDestroyConfig(config); aclprofFinalize(); // 9. Destroy runtime allocations. // 10. Deinitialize AscendCL. |
Parent topic: mstx API Reference