Call Example
Examples in API description are code snippets only. The following uses dsmi_get_device_health as an example to illustrate the header file to be included (dsmi_common_interface.h), API calling logic, and the printing of returns.
Set up the development environment by referring to CANN Software Installation Guide and obtain the dsmi_common_interface.h file from the /usr/local/Ascend/driver/include directory in the development environment.
/usr/local/Ascend is the default component installation directory. Replace it as required.
Sample Code File (get_device_health.c)
#include <stdio.h> #include <stdlib.h> #include <getopt.h> #include <unistd.h> #include "dsmi_common_interface.h" int main(int argc, char *argv[]) { int ret = 0; int opt = 0; int test_case_num = 0; char optstring[20] = "p:s:gr"; static struct option long_options[] = { {"process", 1, NULL, 'p'}, {0, 0, 0, 0} }; while ((opt = getopt_long(argc, argv, optstring, long_options, NULL)) != -1) { switch (opt) { case 'p': { if (argc < 3) { printf("process test the para num: %d test_error. Input num should not be smaller than 3.test_fail \n",argc); return –1; } test_case_num = strtol(argv[2], NULL, 10); switch (test_case_num) { case 1: { printf("begin query health\n"); unsigned int phealth = 0; ret = dsmi_get_device_health(0, &phealth); if (ret) { printf("call dsmi_get_device_health fail, ret = %d\n", ret); return –1; } else { /*phealth type is unsigned int, printf value should use %u*/ printf("dsmi_get_device_health success,phealth:%u.\n", phealth); } break; } default: break; } } } } return 0; }
Call Example (Ascend EP)
- Upload the get_device_health.c and dsmi_common_interface.h files to the same directory on the host server as the HwHiAiUser user.
- Log in to the host server as the HwHiAiUser user.
- Run the following command to build get_device_health.c into an executable file get_device_health:
gcc get_device_health.c /usr/local/Ascend/driver/lib64/libdrvdsmi_host.so -L. -o get_device_health
- /usr/local/Ascend is the default installation path of the Driver component. Replace it as required.
- For details about how to install Driver, see CANN Software Installation Guide.
- Execute the get_device_health file.
./get_device_health -p 1