Merged
修复完善资料与样例中存在的问题 #7253
UFOunderAdd on 23 days ago
修复完善资料与样例中存在的问题 #7253
Merged
Merged fromUFOunder/asc-devkit_1828masterintocann/asc-devkitmaster
Total: 8 files changed+121-17
| @@ -35,7 +35,7 @@ M方向切分: | |||
| 35 | 35 | ||
| 36 |  | 36 |  |
| 37 | 37 | ||
| 38 | -在NZ2ND模式下启用双目标模式如下图所示,分为按M维度拆分和按N维度拆分,按M维度拆分M必须为2的倍数,按N维度拆分N必须为2的倍数: | 38 | +在NZ2ND模式下启用双目标模式如下图所示,分为按M维度拆分和按N维度拆分,按M维度拆分M必须为2的倍数,按N维度拆分N必须为32的倍数: |
| 39 | 39 | ||
| 40 | - ndNum = 2,表示源NZ矩阵的数目为2。图中红框区域为矩阵1,蓝框区域为矩阵2。 | 40 | - ndNum = 2,表示源NZ矩阵的数目为2。图中红框区域为矩阵1,蓝框区域为矩阵2。 |
| 41 | - nSize = 32,表示源NZ矩阵(图中红框区域或蓝框区域)在N方向上的大小为32个元素。 | 41 | - nSize = 32,表示源NZ矩阵(图中红框区域或蓝框区域)在N方向上的大小为32个元素。 |
| @@ -41,6 +41,19 @@ | |||
| 41 | 41 | ||
| 42 | 搬入前的数据排布参见[关键分形格式详解](../../cube_compute/cube_compute_fractal_intro/key_fractal_format_details.md);地址对齐与搬运粒度要求参见[矩阵计算输入搬运约束](cube_compute_input_move_constraint.md),并结合本接口的参数和约束说明使用。 | 42 | 搬入前的数据排布参见[关键分形格式详解](../../cube_compute/cube_compute_fractal_intro/key_fractal_format_details.md);地址对齐与搬运粒度要求参见[矩阵计算输入搬运约束](cube_compute_input_move_constraint.md),并结合本接口的参数和约束说明使用。 |
| 43 | 43 | ||
| 44 | +### A矩阵输入布局与接口选择 | ||
| 45 | + | ||
| 46 | +本接口的“非转置搬运”和“转置搬运”描述的是L1 Buffer到L0A Buffer的数据搬运方式,不直接代表矩阵乘法表达式中的左矩阵是否转置。 | ||
| 47 | + | ||
| 48 | +以目标计算`C = A × B`为例,其中A的形状为`M×K`,B的形状为`K×N`。当GM中的输入采用ND格式,并通过`asc_copy_gm2l1_nd2nz`转换为L1 Buffer中的Nz格式时,A侧接口选择如下: | ||
| 49 | + | ||
| 50 | +| GM中存储的数据 | GM输入形状 | L1到L0A搬运接口 | 目标计算 | | ||
| 51 | +| --- | --- | --- | --- | | ||
| 52 | +| A | `M×K` | `asc_copy_l12l0a` | `C = A × B` | | ||
| 53 | +| Aᵀ,即预先转置的A | `K×M` | `asc_copy_l12l0a_transpose` | `C = A × B` | | ||
| 54 | + | ||
| 55 | +L0A Buffer使用Nz分形排布,应结合源矩阵的实际布局选择接口,不能仅根据接口名中的`transpose`判断最终乘法方向。B侧也需根据实际输入布局选择对应的L1 Buffer到L0B Buffer搬运接口。转置搬运仍需满足本页参数说明和约束说明中的分形、对齐及空间要求。 | ||
| 56 | + | ||
| 44 | ## 函数原型 | 57 | ## 函数原型 |
| 45 | 58 | ||
| 46 | ### 2D搬运模式 | 59 | ### 2D搬运模式 |
| @@ -215,7 +228,11 @@ $$ | |||
| 215 | 228 | ||
| 216 | ## 调用示例 | 229 | ## 调用示例 |
| 217 | 230 | ||
| 218 | -仓库中的完整工程样例请参考[mmad样例](../../../../../../../examples/02_simd_c_api/03_c_api/03_matrix_compute/mmad)。 | 231 | +以下示例演示`asc_copy_l12l0a`的非转置搬运。示例输入矩阵A的形状为`M×K`,输入矩阵B的形状为`N×K`,A/B侧分别使用`asc_copy_l12l0a`和`asc_copy_l12l0b`进行非转置搬运,实际计算为`C = A × Bᵀ`,输出形状为`M×N`。Host侧Golden按相同公式计算。 |
| 232 | + | ||
| 233 | +本例中的B指代码变量`b`所存储的输入矩阵;A侧非转置搬运并不意味着整个示例计算`C = A × B`。 | ||
| 234 | + | ||
| 235 | +涵盖A/B矩阵转置与非转置输入、int8_t/half/float数据类型的完整工程,请参考[load_data_l12l0_950样例](../../../../../../../examples/02_simd_c_api/03_c_api/03_matrix_compute/load_data_l12l0_950/README.md)。 | ||
| 219 | 236 | ||
| 220 | 将代码保存为`examples.asc`后,可通过`bisheng`命令编译运行,其中`--npu-arch`参数需根据实际产品型号指定对应的NPU架构,具体产品与NPU架构的映射关系请参考[`__NPU_ARCH__`](../../../../../guide/programming_guide/language_extension/simd_builtin_keywords.md#npu-arch)。 | 237 | 将代码保存为`examples.asc`后,可通过`bisheng`命令编译运行,其中`--npu-arch`参数需根据实际产品型号指定对应的NPU架构,具体产品与NPU架构的映射关系请参考[`__NPU_ARCH__`](../../../../../guide/programming_guide/language_extension/simd_builtin_keywords.md#npu-arch)。 |
| 221 | 238 | ||
| @@ -251,7 +268,7 @@ __global__ __cube__ void AscCopyL12l0aKernel(__gm__ int8_t* a, __gm__ int8_t* b, | |||
| 251 | __cb__ int8_t b_l0[K * N]; | 268 | __cb__ int8_t b_l0[K * N]; |
| 252 | __cc__ int32_t c_l0[ELEMENTS]; | 269 | __cc__ int32_t c_l0[ELEMENTS]; |
| 253 | 270 | ||
| 254 | - // 配置Nz格式,并将128 x 128矩阵从GM搬入L1 Buffer。 | 271 | + // 将形状为M×K的A矩阵和形状为N×K的B矩阵从GM搬入L1 Buffer,并转换为Nz格式。 |
| 255 | asc_set_gm2l1_nz_para(1, 1, 128, 0); | 272 | asc_set_gm2l1_nz_para(1, 1, 128, 0); |
| 256 | asc_copy_gm2l1_nd2nz(a_l1, a, K, asc_load_l2_cache_mode::NORMAL_FIRST_VICTIM, M, K, 0, false); | 273 | asc_copy_gm2l1_nd2nz(a_l1, a, K, asc_load_l2_cache_mode::NORMAL_FIRST_VICTIM, M, K, 0, false); |
| 257 | asc_set_gm2l1_nz_para(1, 1, 128, 0); | 274 | asc_set_gm2l1_nz_para(1, 1, 128, 0); |
| @@ -260,13 +277,13 @@ __global__ __cube__ void AscCopyL12l0aKernel(__gm__ int8_t* a, __gm__ int8_t* b, | |||
| 260 | asc_sync_notify(PIPE_MTE2, PIPE_MTE1, EVENT_ID0); | 277 | asc_sync_notify(PIPE_MTE2, PIPE_MTE1, EVENT_ID0); |
| 261 | asc_sync_wait(PIPE_MTE2, PIPE_MTE1, EVENT_ID0); | 278 | asc_sync_wait(PIPE_MTE2, PIPE_MTE1, EVENT_ID0); |
| 262 | 279 | ||
| 263 | - // Move all A and B fractals to L0A Buffer and L0B Buffer for the 128x128x128 matrix multiplication. | 280 | + // 将A矩阵和B矩阵从L1 Buffer分别非转置搬入L0A Buffer和L0B Buffer。 |
| 264 | asc_copy_l12l0a(a_l0, a_l1, 0, 0, M / CUBE_M, K / CUBE_K, M / CUBE_M, M / CUBE_M); | 281 | asc_copy_l12l0a(a_l0, a_l1, 0, 0, M / CUBE_M, K / CUBE_K, M / CUBE_M, M / CUBE_M); |
| 265 | asc_copy_l12l0b(b_l0, b_l1, 0, 0, N / CUBE_M, K / CUBE_K, N / CUBE_M, N / CUBE_M); | 282 | asc_copy_l12l0b(b_l0, b_l1, 0, 0, N / CUBE_M, K / CUBE_K, N / CUBE_M, N / CUBE_M); |
| 266 | asc_sync_notify(PIPE_MTE1, PIPE_M, EVENT_ID0); | 283 | asc_sync_notify(PIPE_MTE1, PIPE_M, EVENT_ID0); |
| 267 | asc_sync_wait(PIPE_MTE1, PIPE_M, EVENT_ID0); | 284 | asc_sync_wait(PIPE_MTE1, PIPE_M, EVENT_ID0); |
| 268 | 285 | ||
| 269 | - // Multiply A and B after PIPE_MTE1 has produced both operands. | 286 | + // 等待PIPE_MTE1完成搬运后,执行C = A × Bᵀ的矩阵乘法,其中M=N=K=128。 |
| 270 | asc_mmad(c_l0, a_l0, b_l0, M, K, N, asc_unit_flag_mode::DISABLE, true, false, true); | 287 | asc_mmad(c_l0, a_l0, b_l0, M, K, N, asc_unit_flag_mode::DISABLE, true, false, true); |
| 271 | asc_sync_notify(PIPE_M, PIPE_FIX, EVENT_ID0); | 288 | asc_sync_notify(PIPE_M, PIPE_FIX, EVENT_ID0); |
| 272 | asc_sync_wait(PIPE_M, PIPE_FIX, EVENT_ID0); | 289 | asc_sync_wait(PIPE_M, PIPE_FIX, EVENT_ID0); |
| @@ -291,7 +308,7 @@ void PrintRow(const char* label, const std::vector<T>& data) | |||
| 291 | 308 | ||
| 292 | int main() | 309 | int main() |
| 293 | { | 310 | { |
| 294 | - // Generate deterministic dense inputs and the corresponding host-side golden result. | 311 | + // 构造确定性输入矩阵,并在Host侧按C = A × Bᵀ计算Golden结果。 |
| 295 | std::vector<int8_t> a(M * K), b(N * K); | 312 | std::vector<int8_t> a(M * K), b(N * K); |
| 296 | std::vector<int32_t> output(ELEMENTS), golden(ELEMENTS); | 313 | std::vector<int32_t> output(ELEMENTS), golden(ELEMENTS); |
| 297 | for (uint32_t row = 0; row < M; ++row) { | 314 | for (uint32_t row = 0; row < M; ++row) { |
| @@ -41,6 +41,19 @@ | |||
| 41 | 41 | ||
| 42 | 搬入前的数据排布参见[关键分形格式详解](../../cube_compute/cube_compute_fractal_intro/key_fractal_format_details.md);地址对齐与搬运粒度要求参见[矩阵计算输入搬运约束](cube_compute_input_move_constraint.md),并结合本接口的参数和约束说明使用。 | 42 | 搬入前的数据排布参见[关键分形格式详解](../../cube_compute/cube_compute_fractal_intro/key_fractal_format_details.md);地址对齐与搬运粒度要求参见[矩阵计算输入搬运约束](cube_compute_input_move_constraint.md),并结合本接口的参数和约束说明使用。 |
| 43 | 43 | ||
| 44 | +### B矩阵输入布局与接口选择 | ||
| 45 | + | ||
| 46 | +本接口的“非转置搬运”和“转置搬运”描述的是L1 Buffer到L0B Buffer的数据搬运方式,不直接代表矩阵乘法表达式中的右矩阵是否转置。 | ||
| 47 | + | ||
| 48 | +以目标计算`C = A × B`为例,其中A的形状为`M×K`,B的形状为`K×N`。当GM中的输入采用ND格式,并通过`asc_copy_gm2l1_nd2nz`转换为L1 Buffer中的Nz格式时,B侧接口选择如下: | ||
| 49 | + | ||
| 50 | +| GM中存储的数据 | GM输入形状 | L1到L0B搬运接口 | 目标计算 | | ||
| 51 | +| --- | --- | --- | --- | | ||
| 52 | +| B | `K×N` | `asc_copy_l12l0b_transpose` | `C = A × B` | | ||
| 53 | +| Bᵀ,即预先转置的B | `N×K` | `asc_copy_l12l0b` | `C = A × B` | | ||
| 54 | + | ||
| 55 | +L0B Buffer使用Zn分形排布,应结合源矩阵的实际布局选择接口,不能仅根据接口名中的`transpose`判断最终乘法方向。转置搬运仍需满足本页参数说明和约束说明中的分形、对齐及空间要求。 | ||
| 56 | + | ||
| 44 | ## 函数原型 | 57 | ## 函数原型 |
| 45 | 58 | ||
| 46 | ### 2D搬运模式 | 59 | ### 2D搬运模式 |
| @@ -150,6 +163,12 @@ PIPE_MTE1 | |||
| 150 | 163 | ||
| 151 | ## 调用示例 | 164 | ## 调用示例 |
| 152 | 165 | ||
| 166 | +以下示例演示`asc_copy_l12l0b`的非转置搬运。示例输入矩阵A的形状为`M×K`,输入矩阵B的形状为`N×K`,实际计算为`C = A × Bᵀ`,输出形状为`M×N`。Host侧Golden按相同公式计算。 | ||
| 167 | + | ||
| 168 | +本例中的B指代码变量`b`所存储的输入矩阵;它相当于前述接口选择表中预先转置后的输入。 | ||
| 169 | + | ||
| 170 | +涵盖A/B矩阵转置与非转置输入、int8_t/half/float数据类型的完整工程,请参考[load_data_l12l0_950样例](../../../../../../../examples/02_simd_c_api/03_c_api/03_matrix_compute/load_data_l12l0_950/README.md)。 | ||
| 171 | + | ||
| 153 | 将代码保存为`examples.asc`后,可通过`bisheng`命令编译运行,其中`--npu-arch`参数需根据实际产品型号指定对应的NPU架构,具体产品与NPU架构的映射关系请参考[`__NPU_ARCH__`](../../../../../guide/programming_guide/language_extension/simd_builtin_keywords.md#npu-arch)。 | 172 | 将代码保存为`examples.asc`后,可通过`bisheng`命令编译运行,其中`--npu-arch`参数需根据实际产品型号指定对应的NPU架构,具体产品与NPU架构的映射关系请参考[`__NPU_ARCH__`](../../../../../guide/programming_guide/language_extension/simd_builtin_keywords.md#npu-arch)。 |
| 154 | 173 | ||
| 155 | <!-- npu="950" id8 --> | 174 | <!-- npu="950" id8 --> |
| @@ -186,7 +205,7 @@ __global__ __cube__ void AscCopyL12l0bKernel(__gm__ int8_t* a, __gm__ int8_t* b, | |||
| 186 | __cb__ int8_t b_l0[K * N]; | 205 | __cb__ int8_t b_l0[K * N]; |
| 187 | __cc__ int32_t c_l0[ELEMENTS]; | 206 | __cc__ int32_t c_l0[ELEMENTS]; |
| 188 | 207 | ||
| 189 | - // 将A矩阵和B矩阵从GM搬入L1 Buffer,并转换为Nz格式。 | 208 | + // 将形状为M×K的A矩阵和形状为N×K的B矩阵从GM搬入L1 Buffer,并转换为Nz格式。 |
| 190 | asc_set_gm2l1_nz_para(1, 1, 128, 0); | 209 | asc_set_gm2l1_nz_para(1, 1, 128, 0); |
| 191 | asc_copy_gm2l1_nd2nz(a_l1, a, K, asc_load_l2_cache_mode::NORMAL_FIRST_VICTIM, M, K, 0, false); | 210 | asc_copy_gm2l1_nd2nz(a_l1, a, K, asc_load_l2_cache_mode::NORMAL_FIRST_VICTIM, M, K, 0, false); |
| 192 | asc_set_gm2l1_nz_para(1, 1, 128, 0); | 211 | asc_set_gm2l1_nz_para(1, 1, 128, 0); |
| @@ -194,13 +213,13 @@ __global__ __cube__ void AscCopyL12l0bKernel(__gm__ int8_t* a, __gm__ int8_t* b, | |||
| 194 | asc_sync_notify(PIPE_MTE2, PIPE_MTE1, EVENT_ID0); | 213 | asc_sync_notify(PIPE_MTE2, PIPE_MTE1, EVENT_ID0); |
| 195 | asc_sync_wait(PIPE_MTE2, PIPE_MTE1, EVENT_ID0); | 214 | asc_sync_wait(PIPE_MTE2, PIPE_MTE1, EVENT_ID0); |
| 196 | 215 | ||
| 197 | - // 将A矩阵和B矩阵从L1 Buffer分别搬入L0A Buffer和L0B Buffer。 | 216 | + // 将A矩阵和B矩阵从L1 Buffer分别搬入L0A Buffer和L0B Buffer,B侧使用asc_copy_l12l0b非转置搬运。 |
| 198 | asc_copy_l12l0a(a_l0, a_l1, 0, 0, M / CUBE_M, K / CUBE_K, M / CUBE_M, M / CUBE_M); | 217 | asc_copy_l12l0a(a_l0, a_l1, 0, 0, M / CUBE_M, K / CUBE_K, M / CUBE_M, M / CUBE_M); |
| 199 | asc_copy_l12l0b(b_l0, b_l1, 0, 0, N / CUBE_M, K / CUBE_K, N / CUBE_M, N / CUBE_M); | 218 | asc_copy_l12l0b(b_l0, b_l1, 0, 0, N / CUBE_M, K / CUBE_K, N / CUBE_M, N / CUBE_M); |
| 200 | asc_sync_notify(PIPE_MTE1, PIPE_M, EVENT_ID0); | 219 | asc_sync_notify(PIPE_MTE1, PIPE_M, EVENT_ID0); |
| 201 | asc_sync_wait(PIPE_MTE1, PIPE_M, EVENT_ID0); | 220 | asc_sync_wait(PIPE_MTE1, PIPE_M, EVENT_ID0); |
| 202 | 221 | ||
| 203 | - // 执行M=N=K=128的矩阵乘法。 | 222 | + // 执行C = A × Bᵀ的矩阵乘法,其中M=N=K=128。 |
| 204 | asc_mmad(c_l0, a_l0, b_l0, M, K, N, asc_unit_flag_mode::DISABLE, true, false, true); | 223 | asc_mmad(c_l0, a_l0, b_l0, M, K, N, asc_unit_flag_mode::DISABLE, true, false, true); |
| 205 | asc_sync_notify(PIPE_M, PIPE_FIX, EVENT_ID0); | 224 | asc_sync_notify(PIPE_M, PIPE_FIX, EVENT_ID0); |
| 206 | asc_sync_wait(PIPE_M, PIPE_FIX, EVENT_ID0); | 225 | asc_sync_wait(PIPE_M, PIPE_FIX, EVENT_ID0); |
| @@ -225,7 +244,7 @@ void PrintRow(const char* label, const std::vector<T>& data) | |||
| 225 | 244 | ||
| 226 | int main() | 245 | int main() |
| 227 | { | 246 | { |
| 228 | - // 构造输入矩阵,并在Host侧计算Golden结果。 | 247 | + // 构造输入矩阵,并在Host侧按C = A × Bᵀ计算Golden结果。 |
| 229 | std::vector<int8_t> a(M * K), b(N * K); | 248 | std::vector<int8_t> a(M * K), b(N * K); |
| 230 | std::vector<int32_t> output(ELEMENTS), golden(ELEMENTS); | 249 | std::vector<int32_t> output(ELEMENTS), golden(ELEMENTS); |
| 231 | for (uint32_t row = 0; row < M; ++row) { | 250 | for (uint32_t row = 0; row < M; ++row) { |
| @@ -69,6 +69,13 @@ | |||
| 69 | <img src="figures/fixpipe_l0c2ub_split_n.png" width="500"> | 69 | <img src="figures/fixpipe_l0c2ub_split_n.png" width="500"> |
| 70 | </p> | 70 | </p> |
| 71 | 71 | ||
| 72 | +## CV同步说明 | ||
| 73 | + | ||
| 74 | +本样例使用模式2的 `CrossCoreSetFlag<0x2, PIPE_FIX>(0x8)` 在Fixpipe搬运完成后通知同组的两个AIV。两个AIV都必须执行对应的 `CrossCoreWaitFlag<0x2>(0x8)`,确保同步标记被完整消费。 | ||
| 75 | + | ||
| 76 | +- 场景1、2为单目标输出:两个AIV先等待同步,再由AIV1返回,仅AIV0执行UB到GM的搬运。不能在等待同步之前让AIV1提前返回,否则会残留未消费的同步标记。 | ||
| 77 | +- 场景3、4为双目标输出:两个AIV都等待同步,并分别将各自UB中的结果搬运到GM的对应位置。 | ||
| 78 | + | ||
| 72 | ## 编译运行 | 79 | ## 编译运行 |
| 73 | 80 | ||
| 74 | 在本样例根目录下执行如下步骤,编译并执行样例。 | 81 | 在本样例根目录下执行如下步骤,编译并执行样例。 |
| @@ -69,6 +69,13 @@ This example selects different output scenarios through the compilation paramete | |||
| 69 | <img src="figures/fixpipe_l0c2ub_split_n.png" width="500"> | 69 | <img src="figures/fixpipe_l0c2ub_split_n.png" width="500"> |
| 70 | </p> | 70 | </p> |
| 71 | 71 | ||
| 72 | +## Cube-Vector Synchronization | ||
| 73 | + | ||
| 74 | +This example uses mode 2 with `CrossCoreSetFlag<0x2, PIPE_FIX>(0x8)` to notify both AIVs in the same group after the Fixpipe transfer completes. Both AIVs must execute the matching `CrossCoreWaitFlag<0x2>(0x8)` to consume their synchronization tokens. | ||
| 75 | + | ||
| 76 | +- Scenarios 1 and 2 use a single output target: both AIVs wait for synchronization before AIV1 returns, and only AIV0 transfers data from UB to GM. AIV1 must not return before the wait, as doing so leaves an unconsumed synchronization token. | ||
| 77 | +- Scenarios 3 and 4 use dual output targets: both AIVs wait for synchronization and transfer their respective UB results to the corresponding GM locations. | ||
| 78 | + | ||
| 72 | ## Build and Run | 79 | ## Build and Run |
| 73 | 80 | ||
| 74 | Run the following steps in the root directory of this example to build and run the example. | 81 | Run the following steps in the root directory of this example to build and run the example. |
| @@ -88,10 +88,10 @@ public: | |||
| 88 | } | 88 | } |
| 89 | if ASCEND_IS_AIV { | 89 | if ASCEND_IS_AIV { |
| 90 | if constexpr (SCENARIO_NUM == 1 || SCENARIO_NUM == 2) { | 90 | if constexpr (SCENARIO_NUM == 1 || SCENARIO_NUM == 2) { |
| 91 | + AscendC::CrossCoreWaitFlag<0x2>(0x8); | ||
| 91 | if (AscendC::GetSubBlockIdx() != 0) { | 92 | if (AscendC::GetSubBlockIdx() != 0) { |
| 92 | return; | 93 | return; |
| 93 | } | 94 | } |
| 94 | - AscendC::CrossCoreWaitFlag<0x2>(0x8); | ||
| 95 | AscendC::DataCopyParams dataCopyInfo; | 95 | AscendC::DataCopyParams dataCopyInfo; |
| 96 | dataCopyInfo.blockCount = 1; | 96 | dataCopyInfo.blockCount = 1; |
| 97 | dataCopyInfo.blockLen = baseM * baseN * sizeof(outputType) / AscendC::ONE_BLK_SIZE; | 97 | dataCopyInfo.blockLen = baseM * baseN * sizeof(outputType) / AscendC::ONE_BLK_SIZE; |
| @@ -54,7 +54,7 @@ z = x + y | |||
| 54 | 54 | ||
| 55 | ```bash | 55 | ```bash |
| 56 | make -B demo | 56 | make -B demo |
| 57 | - mssanitizer ./demo | 57 | + mssanitizer ./demo # 默认开启内存检测,也可通过传入 --tool=memcheck 开启 |
| 58 | ``` | 58 | ``` |
| 59 | 59 | ||
| 60 | - 非对齐访问:内存访问未满足字节对齐要求。 | 60 | - 非对齐访问:内存访问未满足字节对齐要求。 |
| @@ -72,7 +72,7 @@ z = x + y | |||
| 72 | 72 | ||
| 73 | ```bash | 73 | ```bash |
| 74 | make -B demo | 74 | make -B demo |
| 75 | - mssanitizer ./demo | 75 | + mssanitizer ./demo # 默认开启内存检测,也可通过传入 --tool=memcheck 开启 |
| 76 | ``` | 76 | ``` |
| 77 | 77 | ||
| 78 | - 内存泄漏:申请内存使用后未释放,导致程序在运行过程中内存占用持续增加的异常。 | 78 | - 内存泄漏:申请内存使用后未释放,导致程序在运行过程中内存占用持续增加的异常。 |
| @@ -152,6 +152,32 @@ z = x + y | |||
| 152 | mssanitizer ./demo --tool=initcheck | 152 | mssanitizer ./demo --tool=initcheck |
| 153 | ``` | 153 | ``` |
| 154 | 154 | ||
| 155 | +- **同步异常检测** | ||
| 156 | + | ||
| 157 | + 同步异常检测用于定位同步指令未正确配对的问题,例如发出同步通知后没有执行对应的等待操作。 | ||
| 158 | + | ||
| 159 | + 用户可以在`add_custom`末尾的`asc_unlock(PIPE_MTE3, mutex_id)`之后添加以下同步代码。`asc_sync_notify`与`asc_sync_wait`的源流水、目的流水和事件ID需要一致,且应配对使用。 | ||
| 160 | + | ||
| 161 | + ```cpp | ||
| 162 | + asc_sync_notify(PIPE_MTE3, PIPE_S, EVENT_ID0); | ||
| 163 | + asc_sync_wait(PIPE_MTE3, PIPE_S, EVENT_ID0); | ||
| 164 | + ``` | ||
| 165 | + | ||
| 166 | + 注释掉上面的`asc_sync_wait`,仅保留`asc_sync_notify`,即可构造通知未被等待的同步异常: | ||
| 167 | + | ||
| 168 | + ```cpp | ||
| 169 | + asc_sync_notify(PIPE_MTE3, PIPE_S, EVENT_ID0); | ||
| 170 | + ``` | ||
| 171 | + | ||
| 172 | + 注意:调用`mssanitizer`时需要传入`--tool=synccheck`来开启同步异常检测。此处复现的是同步指令不配对问题,与“竞争检测”中删除流水间同步后产生的数据竞争不同。 | ||
| 173 | + | ||
| 174 | + 重新编译并执行检测: | ||
| 175 | + | ||
| 176 | + ```bash | ||
| 177 | + make -B demo | ||
| 178 | + mssanitizer ./demo --tool=synccheck | ||
| 179 | + ``` | ||
| 180 | + | ||
| 155 | ## 编译运行 | 181 | ## 编译运行 |
| 156 | 182 | ||
| 157 | 在本样例根目录下执行如下步骤,编译并执行样例。 | 183 | 在本样例根目录下执行如下步骤,编译并执行样例。 |
| @@ -172,11 +198,12 @@ z = x + y | |||
| 172 | cmake -DCMAKE_ASC_ARCHITECTURES=dav-3510 ..; make -j | 198 | cmake -DCMAKE_ASC_ARCHITECTURES=dav-3510 ..; make -j |
| 173 | 199 | ||
| 174 | # 根据调测需求执行对应的mssanitizer命令。 | 200 | # 根据调测需求执行对应的mssanitizer命令。 |
| 175 | - mssanitizer ./demo | 201 | + mssanitizer ./demo # 默认开启内存检测,也可通过传入 --tool=memcheck 开启 |
| 176 | mssanitizer ./demo --leak-check=yes | 202 | mssanitizer ./demo --leak-check=yes |
| 177 | mssanitizer ./demo --check-unused-memory=yes | 203 | mssanitizer ./demo --check-unused-memory=yes |
| 178 | mssanitizer ./demo --tool=racecheck | 204 | mssanitizer ./demo --tool=racecheck |
| 179 | mssanitizer ./demo --tool=initcheck | 205 | mssanitizer ./demo --tool=initcheck |
| 206 | + mssanitizer ./demo --tool=synccheck | ||
| 180 | ``` | 207 | ``` |
| 181 | 208 | ||
| 182 | - 编译选项说明 | 209 | - 编译选项说明 |
| @@ -54,7 +54,7 @@ When reproducing anomaly scenarios, modify the code in `ms_sanitizer.asc` accord | |||
| 54 | 54 | ||
| 55 | ```bash | 55 | ```bash |
| 56 | make -B demo | 56 | make -B demo |
| 57 | - mssanitizer ./demo | 57 | + mssanitizer ./demo # Memory detection is enabled by default; --tool=memcheck can also be specified. |
| 58 | ``` | 58 | ``` |
| 59 | 59 | ||
| 60 | - Unaligned access: Memory access does not meet byte alignment requirements. | 60 | - Unaligned access: Memory access does not meet byte alignment requirements. |
| @@ -72,7 +72,7 @@ When reproducing anomaly scenarios, modify the code in `ms_sanitizer.asc` accord | |||
| 72 | 72 | ||
| 73 | ```bash | 73 | ```bash |
| 74 | make -B demo | 74 | make -B demo |
| 75 | - mssanitizer ./demo | 75 | + mssanitizer ./demo # Memory detection is enabled by default; --tool=memcheck can also be specified. |
| 76 | ``` | 76 | ``` |
| 77 | 77 | ||
| 78 | - Memory leak: Allocated memory is used but not freed, causing memory usage to increase continuously during program execution. | 78 | - Memory leak: Allocated memory is used but not freed, causing memory usage to increase continuously during program execution. |
| @@ -152,6 +152,32 @@ When reproducing anomaly scenarios, modify the code in `ms_sanitizer.asc` accord | |||
| 152 | mssanitizer ./demo --tool=initcheck | 152 | mssanitizer ./demo --tool=initcheck |
| 153 | ``` | 153 | ``` |
| 154 | 154 | ||
| 155 | +- **Synchronization Anomaly Detection** | ||
| 156 | + | ||
| 157 | + Synchronization anomaly detection locates incorrectly paired synchronization instructions, such as a synchronization notification without a corresponding wait operation. | ||
| 158 | + | ||
| 159 | + Users can add the following synchronization code after `asc_unlock(PIPE_MTE3, mutex_id)` at the end of `add_custom`. `asc_sync_notify` and `asc_sync_wait` must use the same source pipeline, destination pipeline, and event ID, and must be used in pairs. | ||
| 160 | + | ||
| 161 | + ```cpp | ||
| 162 | + asc_sync_notify(PIPE_MTE3, PIPE_S, EVENT_ID0); | ||
| 163 | + asc_sync_wait(PIPE_MTE3, PIPE_S, EVENT_ID0); | ||
| 164 | + ``` | ||
| 165 | + | ||
| 166 | + Comment out `asc_sync_wait` above and keep only `asc_sync_notify` to reproduce a synchronization anomaly in which the notification has no corresponding wait: | ||
| 167 | + | ||
| 168 | + ```cpp | ||
| 169 | + asc_sync_notify(PIPE_MTE3, PIPE_S, EVENT_ID0); | ||
| 170 | + ``` | ||
| 171 | + | ||
| 172 | + Notice: Pass `--tool=synccheck` when calling `mssanitizer` to enable synchronization anomaly detection. This scenario reproduces unpaired synchronization instructions, unlike the data race caused by removing inter-pipeline synchronization in "Race Condition Detection". | ||
| 173 | + | ||
| 174 | + Rebuild and run detection: | ||
| 175 | + | ||
| 176 | + ```bash | ||
| 177 | + make -B demo | ||
| 178 | + mssanitizer ./demo --tool=synccheck | ||
| 179 | + ``` | ||
| 180 | + | ||
| 155 | ## Build and Run | 181 | ## Build and Run |
| 156 | 182 | ||
| 157 | Run the following steps in the root directory of this example to build and run the example. | 183 | Run the following steps in the root directory of this example to build and run the example. |
| @@ -172,11 +198,12 @@ Run the following steps in the root directory of this example to build and run t | |||
| 172 | cmake -DCMAKE_ASC_ARCHITECTURES=dav-3510 ..; make -j | 198 | cmake -DCMAKE_ASC_ARCHITECTURES=dav-3510 ..; make -j |
| 173 | 199 | ||
| 174 | # Run the corresponding mssanitizer command based on debugging requirements. | 200 | # Run the corresponding mssanitizer command based on debugging requirements. |
| 175 | - mssanitizer ./demo | 201 | + mssanitizer ./demo # Memory detection is enabled by default; --tool=memcheck can also be specified. |
| 176 | mssanitizer ./demo --leak-check=yes | 202 | mssanitizer ./demo --leak-check=yes |
| 177 | mssanitizer ./demo --check-unused-memory=yes | 203 | mssanitizer ./demo --check-unused-memory=yes |
| 178 | mssanitizer ./demo --tool=racecheck | 204 | mssanitizer ./demo --tool=racecheck |
| 179 | mssanitizer ./demo --tool=initcheck | 205 | mssanitizer ./demo --tool=initcheck |
| 206 | + mssanitizer ./demo --tool=synccheck | ||
| 180 | ``` | 207 | ``` |
| 181 | 208 | ||
| 182 | - Build options | 209 | - Build options |