Initialization

CANN Runtime provides the aclInit and aclrtSetDevice APIs, which are called when an application is started. These APIs work with the JSON configuration file to implement the following functions:

  • Environment initialization: set the environment parameters required for CANN Runtime to ensure that all runtime resources and configuration items are correctly loaded.
  • Device resource configuration: initialize hardware-related resources (such as Ascend processors and accelerator cards) and allocate resources to them so that subsequent compute tasks can be executed on appropriate devices.
  • Log setting: initialize log recording to ensure that the system running status can be checked and debugged in real time.
  • Resource management initialization: prepare resources for subsequent functions such as memory management, task scheduling, and memory allocation.

The following is a code example for initialization and compute device setting. It is for reference only and cannot be directly copied for compilation or running. For the complete sample code, click here.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
// Initialization
int32_t deviceId = 0;
aclInit(nullptr); // Set the path to nullptr in the JSON file for default initialization.
aclrtSetDevice(deviceId);

// Other aclrt runtime APIs can be called only after SetDevice is called.
......

// Deinitialization
aclrtResetDeviceForce(deviceId);
aclFinalize();

If aclrtSetDevice is not explicitly called, the default device configured below can be used for processing. For example, device 0 is specified as the default device in the JSON file of the aclInit API:

{
    "defaultDevice":{
        "default_device":"0"
    }
}

With the default device function enabled in the aclInit API, refer to the following code example for initialization. Note that it is for reference only and cannot be directly copied for compilation or running.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
// Initialization
// The two dots (..) indicate a path relative to the directory of the executable file.
// For example, if the executable file is stored in the out directory, the two dots (..) point to the parent directory of the out directory.
const char *aclConfigPath = "../src/acl.json";
aclError ret = aclInit(aclConfigPath);

// After the default device is enabled, you can directly call the runtime API without explicitly calling aclrtSetDevice.
// aclrtSetDevice is implicitly called in the API by the device specified in the JSON configuration file.
aclrtMalloc(&devPtr, size, 0); 

......

// Deinitialization
aclrtResetDeviceForce(deviceId);
aclFinalize();

In addition to the default device function, you can configure the JSON file in the aclInit API to implement functions such as performance data collection, model input/output data export, and overflow/underflow operator dump in binary mode without modifying the code. For details about the configuration examples and usage instructions, see the description of the aclInit API.