Kernel Function

Kernel Function Definition

Ascend C allows you to customize kernel functions to extend C++. When a kernel function is executed on the AI processor, several threads are executed in parallel. Each thread has an independent register and stack to complete data compute tasks.

The following is an example of kernel function definition:

1
 __global__ void kernel_name(argument list)

Comply with the following rules when defining kernel functions:

Kernel Function Call

Functions in an operator program can be classified into three types: host functions, kernel functions (executed on the device), and device functions (except kernel functions). The following figure uses operator development based on the kernel launch project as an example to describe the calling relationships between the three types of functions.

Figure 1 Calling relationships between the three types of functions in an operator program

The host calls a kernel function using the syntax of the kernel launch symbol <<<...>>>, as shown below:

1
kernel_name<<<numBlocks, threadsPerBlock, dynUBufSize, stream>>>(args...)
The execution configuration is determined by the following parameters:
  • numBlocks: number of thread blocks configured for the kernel function, that is, the number of enabled cores. The int32_t and dim3 types are supported.
  • threadsPerBlock: number of threads concurrently executed in each thread block. The int32_t and dim3 types are supported.
  • dynUBufSize: total size of the dynamically allocated memory. Generally, this parameter is set to 0.
  • stream: used for stream synchronization between the host and device.