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 the sample code for initialization and specifying compute devices, which 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"
    }
}

After the default device function is enabled in the aclInit API, the following is the sample code for initialization, which 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
12
13
// Initialization
int32_t deviceId = 0;
aclInit(nullptr);

// 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.