This case demonstrates how to use the Matmul high-level API for matrix multiplication in the matrix multiplication operator scenario and how to enable K-axis tiling of matrix data in multi-core parallel computation to improve operator performance. To implement parallel execution of operators on multiple cores and improve computing efficiency, matrix data needs to be tiled, and the tiled data blocks are allocated to different cores for processing. Generally, only the M and N axes are tiled, and the K axis is not tiled. If the values of M and N are small, it is difficult to tile the M and N axes. In this case, tiling the K axis needs to be considered. After the K-axis tiling of matrix data in multi-core parallel computation is enabled, the K axis of the matrix can be tiled so that the operator can be executed in parallel on multiple cores. Because the value of the K axis is large, not tiling the K axis in this scenario usually results in an excessively large size of input data for a single core. After K-axis tiling is enabled, the tiling policy can more effectively balance the output bandwidth and input bandwidth.
The operator specifications are as follows.
|
Input |
Shape |
Data Type |
Format |
|---|---|---|---|
|
a |
16, 1024 |
float16 |
ND |
|
b |
1024, 16 |
float16 |
ND |
On the AI processor used in the current case, there are 24 cores in total, and the high-level API Matmul in CUBE_ONLY is enabled for the operator. The tiling parameters are as follows:
Use the msProf tool to obtain the Operator Simulation Pipeline and On-board Profiling data.

After K-axis tiling of matrix data in multi-core parallel computation is enabled, the data in the K direction of the matrix can be tiled. As shown in the following figure, matrix R blocks in matrix C are obtained by accumulating A1 × B1 + A2 × B2 + A3 × B3, where A1 × B1, A2 × B2, and A3 × B3 can be computed in parallel on multiple cores.
Call the EnableMultiCoreSplitK API before the GetTiling API to enable K-axis tiling of matrix data in multi-core parallel computation. In the kernel implementation, clear the global memory address of matrix C and then enable AtomicAdd. For details about the complete example of enabling K-axis tiling of matrix data in multi-core parallel computation, see operator sample for multi-core K splitting. The specific procedure is as follows:
1 2 3 4 5 6 7 8 9 10 |
cubeTiling.SetOrgShape(M, N, K); cubeTiling.SetShape(M, N, K); cubeTiling.EnableBias(isBias); cubeTiling.SetBufferSpace(-1, -1, -1); // tiling enable split K cubeTiling.EnableMultiCoreSplitK(true); if (cubeTiling.GetTiling(tilingData) == -1) { std::cout << "gen tiling failed." << std::endl; return {}; } |
1 2 3 |
cGlobal.SetGlobalBuffer(reinterpret_cast<__gm__ cType*>(c), tiling.M * tiling.N); // clear gm Fill(cGlobal, tiling.M * tiling.N, (cType)0); |
1 2 3 |
// set AtomicAdd uint8_t enAtomic = 1; matmulObj.IterateAll(cGlobal, enAtomic); |

When an operator uses the Matmul API to perform Cube computation, the M and N directions of the original matrix cannot be effectively split and the result is output to the global memory. In this case, you can enable the K-axis tiling of matrix data in multi-core parallel computation to implement multi-core parallel computing and improve computing efficiency.