The following uses a "Hello World" print example to describe how to compile an AI CPU operator using BiSheng command lines.
The content of the hello_world.aicpu file is as follows:
1 2 3 4 5 6 7 | #include "aicpu_api.h" __global__ __aicpu__ uint32_t hello_world(void *args) { AscendC::printf("Hello World!!!\n"); return 0; } |
On the host, the kernel launch symbol <<<...>>> is used to call the AI CPU operator. The sample code of main.asc 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 | #include "acl/acl.h" struct KernelArgs { int mode; }; extern __global__ __aicpu__ uint32_t hello_world(void *args); int32_t main(int argc, char const *argv[]) { aclInit(nullptr); int32_t deviceId = 0; aclrtSetDevice(deviceId); aclrtStream stream = nullptr; aclrtCreateStream(&stream); struct KernelArgs args = {0}; constexpr uint32_t numBlocks = 1; hello_world<<<numBlocks, nullptr, stream>>>(&args, sizeof(KernelArgs)); aclrtSynchronizeStream(stream); aclrtDestroyStream(stream); aclrtResetDevice(deviceId); aclFinalize(); return 0; } |
You can use BiSheng command lines to compile hello_world.aicpu and main.asc into .o files and then link them into executable files. The compilation commands are as follows:
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.
$bisheng -O2 hello_world.aicpu --cce-aicpu-L${INSTALL_DIR}/lib64/device/lib64 --cce-aicpu-laicpu_api -I${INSTALL_DIR}/include/ascendc/aicpu_api -c -o hello_world.aicpu.o # --npu-arch specifies the NPU architecture version. dav- is followed by the architecture version number. For details about the architecture version number of each product model, see Table 1. $bisheng --npu-arch=dav-2201 main.asc -c -o main.asc.o $bisheng hello_world.aicpu.o main.asc.o -o demo
The preceding example demonstrates how to use BiSheng command lines to compile and generate an executable file. In addition, BiSheng command lines can be used to compile and generate dynamic and static libraries for AI CPU operators. You can use the kernel call symbol <<<...>>> in the ASC code to call the kernel function of an AI CPU operator. When compiling the ASC source file to generate an executable file, link the AI CPU dynamic or static library. Note: If you compile the AI CPU operator code separately to generate a dynamic or static library, you need to manually link default link libraries listed in Table 2.
# Compile test_aicpu.cpp to generate a dynamic operator library. # -lxxx indicates the default link library. # bisheng -shared -x aicpu test_aicpu.cpp -o libtest_aicpu.so -lxxx ...
# Compile test_aicpu.cpp to generate a static operator library. # -lxxx indicates the default link library. # bisheng -lib -x aicpu test_aicpu.cpp -o libtest_aicpu.a -lxxx ...
Common compilation options of AI CPU operators are as follows.
Option |
Required (Yes/No) |
Description |
|---|---|---|
-help |
No |
Displays the help information. |
-x |
No |
Specifies the compilation language. If this option is set to aicpu, the programming language of the AI CPU operator is used. |
-o <file> |
No |
Specifies the name and location of the output file. |
-c |
No |
Compiles and generates the target file. |
-shared, --shared |
No |
Compiles and generates a dynamic link library. |
-lib |
No |
Compiles and generates a static link library. |
-g |
No |
Adds debugging information during compilation. |
-fPIC |
No |
Instructs the compiler to generate location-independent code. |
-O |
No |
Specifies the optimization level of the compiler. Currently, -O3, -O2, and -O0 are supported. |
--cce-aicpu-L |
No |
Specifies the library path on which the AI CPU device depends. |
--cce-aicpu-l |
No |
Specifies the library on which the AI CPU device depends. |
In a project, you can use CMake to more easily use the BiSheng Compiler to compile AI CPU operators and generate executable files, dynamic libraries, static libraries, or binary files.
The "Hello World" print sample described in Compilation Using BiSheng Command Lines is still used as an example. In addition to the code implementation file, you need to prepare a CMakeLists.txt file in the project directory.
├── hello_world.aicpu // AI CPU operator kernel function definition ├── main.asc // AI CPU operator kernel function call └── CMakeLists.txt
The content of CMakeLists.txt is as follows:
cmake_minimum_required(VERSION 3.16)
# 1. find_package() is a command used in CMake to search for and configure the Ascend C compilation toolchain.
find_package(ASC REQUIRED)
find_package(AICPU REQUIRED)
# 2. Specify that the project supports the ASC, AI CPU, and CXX languages. ASC indicates that the BiSheng Compiler can be used to compile the Ascend C programming language. AI CPU indicates that the BiSheng Compiler can be used to compile AI CPU operators.
project(kernel_samples LANGUAGES ASC AICPU CXX)
# 3. Use the CMake API to compile executable files.
add_executable(demo
hello_world.aicpu
main.asc
)
# 4. Specify the linker because the ASC and AI CPU languages are used.
set_target_properties(demo PROPERTIES LINKER_LANGUAGE ASC) # Specify the language used for the link.
target_compile_options(demo PRIVATE
# --npu-arch specifies the NPU architecture version. dav- is followed by the architecture version number. For details about the architecture version number of each product model, see Table 1.
# <COMPILE_LANGUAGE:ASC>: indicates that the compilation option takes effect only for the ASC language.
$<$<COMPILE_LANGUAGE:ASC>:--npu-arch=dav-2201>
)
If you need to use CMake to compile and generate dynamic and static libraries, the following provides more detailed compilation examples:
# Set the .cpp file to the ASC attribute and enable the Ascend C language for compilation.
set_source_files_properties(
add_custom_base.cpp
sub_custom_base.cpp
PROPERTIES LANGUAGE ASC
)
# Set the .cpp file to the AI CPU attribute to support AI CPU operator compilation.
set_source_files_properties(
aicpu_kernel.cpp
PROPERTIES LANGUAGE AICPU
)
add_library(kernel_lib SHARED
add_custom_base.cpp
sub_custom_base.cpp
aicpu_kernel.cpp # AI CPU operators and AI Core operators can be packed together into a dynamic library.
)
target_compile_options(kernel_lib PRIVATE
$<$<COMPILE_LANGUAGE:ASC>:--npu-arch=dav-2201>
)
# During AI CPU operator compilation, you need to manually link the following dependency libraries. (If the specified link language is ASC, you do not need to manually link the following libraries.)
target_link_libraries(kernel_lib PRIVATE
ascendc_runtime
profapi
unified_dlog
ascendcl
runtime
c_sec
mmpa
error_manager
ascend_dump
)
add_executable(demo
main.asc
)
target_link_libraries(demo PRIVATE
kernel_lib
)
# By default, the Ascend C language is used to compile the .asc file, and the AI CPU language is used to compile the .aicpu file. You do not need to use set_source_files_properties to set the language.
add_library(kernel_lib STATIC
add_custom_base.asc
sub_custom_base.asc
aicpu_kernel.aicpu # AI CPU operators and AI Core operators can be packed together into a static library.
)
target_compile_options(kernel_lib PRIVATE
$<$<COMPILE_LANGUAGE:ASC>:--npu-arch=dav-2201>
)
add_executable(demo
main.asc
)
target_link_libraries(demo PRIVATE
kernel_lib
)
The following lists the common variable configurations and link libraries used during CMake compilation.
Variable |
Configuration Description |
|---|---|
CMAKE_BUILD_TYPE |
Compilation mode options:
|
CMAKE_INSTALL_PREFIX |
Specifies the prefix of the installation path when the CMake executes the install command. Compilation results (target specified in ascendc_library and the corresponding header file) are installed in this path. The path is defaulted to the out directory in the current directory. |
CMAKE_CXX_COMPILER_LAUNCHER |
Configures the C++ compiler (such as g++) and BiSheng Compiler launcher as ccache, enabling cache compilation, accelerating repeated compilation, and improving build efficiency. Before using this function, you need to install ccache. The following shows how to perform configuration in the corresponding CMakeLists.txt file: set(CMAKE_CXX_COMPILER_LAUNCHER <launcher_program>) <launcher_program> indicates the ccache installation path, for example, /usr/bin/ccache. set(CMAKE_CXX_COMPILER_LAUNCHER /usr/bin/ccache) |
Name |
Description |
Scenario |
|---|---|---|
libtiling_api.a |
Library related to the tiling function. |
This library needs to be linked when the tiling API related to high-level APIs is used. |
libregister.so |
Library related to tiling registration. |
This library needs to be linked when the tiling API related to high-level APIs is used. |
libgraph_base.so |
Basic data structures and API library. |
This library needs to be linked when basic structures such as ge::Shape and ge::DataType are called. |
libplatform.so |
Hardware platform information library. |
This library needs to be linked when the hardware platform information API related to PlatformAscendC is used. |
Name |
Description |
|---|---|
libascendc_runtime.a |
Assembly library for Ascend C operator parameters. |
libruntime.so |
Runtime library. |
libprofapi.so |
Library for collecting running performance data of Ascend C operators. |
libunified_dlog.so |
CANN log collection library. |
libmmpa.so |
CANN system API library. |
libascend_dump.so |
CANN maintenance and test information library. |
libc_sec.so |
CANN security function library. |
liberror_manager.so |
CANN error information management library. |
libascendcl.so |
ACL-related API library. |