printf

Product Support

Product

Supported

Atlas A3 training products / Atlas A3 inference products

Atlas A2 training products / Atlas A2 inference products

Atlas 200I/500 A2 inference products

x

Atlas inference product 's AI Core

x

Atlas inference product 's Vector Core

x

Atlas training products

x

Function

Provides the formatted output function in the AI CPU operator kernel debugging scenario. The output content is parsed and displayed on the screen by default. The header file aicpu_api.h must be included when this API is used.

Prototype

1
void printf(const char* fmt, ...)

Parameters

Parameter

Input/Output

Description

fmt

Input

Format control string, which contains two types of objects: common characters and conversion descriptions.

  • Common characters are printed as they are.
  • Conversion descriptions are not directly output. Instead, they are used to control the conversion and printing of parameters in printf. Each conversion description starts with a percent (%) character and ends with the specific conversion description, indicating the type of the output data.
    The following conversion types are supported:
    • %d / %i: outputs decimal numbers. The data types that can be printed are bool/int8_t/int16_t/int32_t/int64_t.
    • %f: outputs real numbers. The data types that can be printed are float/half.
    • %x: outputs hexadecimal integers. The data types that can be printed are int8_t/int16_t/int32_t/int64_t/uint8_t/uint16_t/uint32_t/uint64_t.
    • %s: outputs character strings.
    • %u: outputs unsigned data. The data types that can be printed are bool/uint8_t/uint16_t/uint32_t/uint64_t.
    • %p: outputs pointer addresses.

...

Input

Additional parameters (a parameter list with variable quantities and types). Depending on the fmt string, the function may require a series of additional parameters. Each parameter contains a value to be inserted and replaces each % tag specified in the fmt parameter. The number of parameters must be the same as the number of % tags.

Returns

None

Restrictions

  • This API can be called only through <<<...>>> and is used in the heterogeneous compilation scenario.
  • This API does not allow the printing of escape characters, except for newline characters.
  • This API uses the dump function. The total amount of dump data of all APIs using the dump function on each core cannot exceed 1 MB. You need to control the amount of data to be printed. If the limit is exceeded, no content will be printed.
  • When using this API, if you use the bisheng command line for compilation, you need to manually link the related static library. However, if you use CMake for compilation, the framework automatically handles the linkage, and you do not need to pay attention to it. The specific compilation commands are as follows: Use --cce-aicpu-laicpu_api to link libaicpu_api.a to the device, and use --cce-aicpu-L to specify the library path of libaicpu_api.a.
    $bisheng -O2 foo.aicpu --cce-aicpu-L${INSTALL_DIR}/lib64/device/lib64 --cce-aicpu-laicpu_api -I${INSTALL_DIR}/include/ascendc/aicpu_api -c -o foo.aicpu.o

    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.

Example

Call the printf API to print required log information at the target position in the operator kernel implementation code. An example is as follows.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
#include "aicpu_api.h"

// Integer printing:
AscendC::printf("fmt string %d\n", 0x123);

// Floating-point printing:
float a = 3.14;
AscendC::printf("fmt string %f\n", a);

// Pointer printing:
int b = 10;
int *c = &b;
AscendC::printf("TEST %p\n", c);

When the program is running, you will see the following print effect:

1
2
3
fmt string 291
fmt string 3.140000
TEST 0xdfffd6fddd1c