mstx API Use Case
The code provided in this section is only an example. To obtain the complete sample code, perform the following steps:
- You have installed the CANN Toolkit package and ops operator package.
- The mstx API sample code is integrated in the CANN Toolkit package and stored in the ${INSTALL_DIR}/tools/mstx/samples directory.
Replace ${INSTALL_DIR} with the actual CANN component directory. If the Ascend-CANN-Toolkit package is installed as the root user, the CANN component directory is /usr/local/Ascend/ascend-toolkit/latest.
- Use the sample code based on the README file in the directory.
For more information about the mstx APIs, see MindStudio mstx API Reference.
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 | aclrtContext context_; aclrtStream stream_; // 1. Initialize AscendCL. aclError ret = ACL_ERROR_NONE; ret = aclInit(nullptr); if (ret != ACL_SUCCESS) { ERROR_LOG("aclInit failed"); return FAILED; } // 2. Allocate runtime resources, including setting the compute device and creating a context and a stream. ret = aclrtSetDevice(0); if (ret != ACL_ERROR_NONE) { ERROR_LOG("aclrtSetDevice failed"); return FAILED; } ret = aclrtCreateContext(&context_, 0); if (ret != ACL_ERROR_NONE) { ERROR_LOG("acl create context failed"); return FAILED; } ret = aclrtCreateStream(&stream_); if (ret != ACL_ERROR_NONE) { ERROR_LOG("acl create stream failed"); return FAILED; } .... // 3. Add markers to the positions where time consumption needs to be profiled. For example, add markers before and after model execution to obtain the model execution time. mstxRangeId rangeId = 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 only). A valid stream is set, and the time consumed by the host and the corresponding device is recorded (applicable to computation or communication task delivery). ret = aclmdlExecute(modelId, input, output); // Execute the model sample code. mstxRangeEnd(rangeId); // 4. The marker data in step 3 belongs to the default domain. You can call the mstx domain API to create a custom domain and specify the domain to insert markers. mstxDomainHandle_t selfDomain = mstxDomainCreateA("self_domain"); mstxRangeId domainRangeId = mstxDomainRangeStartA(selfDomain, "model execute", nullptr); ret = aclmdlExecute(modelId, input, output); // Execute the model sample code. mstxDomainRangeEnd(selfDomain, domainRangeId); // 5. Destroy runtime allocations. // 6. Deinitialize AscendCL. |
Parent topic: Appendixes