CANN/HCCL Reduce操作示例

发布时间:2026/7/4 9:49:31
CANN/HCCL Reduce操作示例 Collective Communication - Reduce【免费下载链接】hccl集合通信库Huawei Collective Communication Library简称HCCL是基于昇腾AI处理器的高性能集合通信库为计算集群提供高性能、高可靠的通信方案项目地址: https://gitcode.com/cann/hcclSample DescriptionThis sample demonstrates how to use theHcclReduce()API to perform the Reduce operation. It covers the following functions:CallaclrtGetDeviceCount()to detect devices and query the number of available devices.CallHcclGetRootInfo()and userank 0as the root rank to generate the rootinfo identifier.The rootinfo identifier contains the device IP address and device ID. This information must be broadcast to all ranks in the cluster to initialize the communicator.In each thread, callHcclCommInitRootInfo()to initialize the communicator based on the rootinfo identifier.Call theHcclReduce()API to sum the input data of all ranks, send the result to the root node, and display the result.Directory Structure├── main.cc # Sample source file ├── Makefile # Compilation and build configuration file └── reduce # Compiled executable fileEnvironment PreparationEnvironment RequirementsThis sample supports the following products in a single-server N-card configuration (N 2):Ascend 950PR / Ascend 950DTAtlas A3 Training Series Products / Atlas A3 Inference Series ProductsAtlas A2 Training Series ProductsAtlas Training Series ProductsSetting Environment Variables# Set CANN environment variables. The following uses the root user default installation path as an example. source /usr/local/Ascend/cann/set_env.shCompiling and Running the SampleRun the following commands in the sample code directory:make make testNote: You can set theHCCL_OP_EXPANSION_MODEenvironment variable to configure the expansion mode of communication operators. For the supported ranges for different product models, see the usage instructions for this environment variable in the Environment Variable List.# Set the communication operator expansion mode to the AI CPU communication engine export HCCL_OP_EXPANSION_MODEAI_CPUSample OutputThe data of each rank is initialized to 0 through 7. After the Reduce operation, the result on the root node is the sum of the data at the corresponding positions of all ranks (the data of 8 ranks is added).Found 8 NPU device(s) available rankId: 0, output: [ 0 8 16 24 32 40 48 56 ] rankId: 1, output: [ 0 0 0 0 0 0 0 0 ] rankId: 2, output: [ 0 0 0 0 0 0 0 0 ] rankId: 3, output: [ 0 0 0 0 0 0 0 0 ] rankId: 4, output: [ 0 0 0 0 0 0 0 0 ] rankId: 5, output: [ 0 0 0 0 0 0 0 0 ] rankId: 6, output: [ 0 0 0 0 0 0 0 0 ] rankId: 7, output: [ 0 0 0 0 0 0 0 0 ]【免费下载链接】hccl集合通信库Huawei Collective Communication Library简称HCCL是基于昇腾AI处理器的高性能集合通信库为计算集群提供高性能、高可靠的通信方案项目地址: https://gitcode.com/cann/hccl创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考