The code provided in this section is only an example. To obtain the complete sample code, take the following steps:
For details, see CANN Software Installation.
Replace ${INSTALL_DIR} with the CANN component directory. For example, if the installation is performed by the root user, the default file storage path is /usr/local/Ascend/cann.
For more information about mstx APIs, see MindStudio mstx API Reference.
The sample code for using the mstx APIs to profile data is as follows:
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 tracing 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. |