Collective Communication

Collective communication refers to a communication mode in which a plurality of NPUs participate in data transmission to form a collective operation. It is usually used in scenarios such as gradient synchronization and parameter update between different NPUs in a large-scale cluster.

HCCL supports communication operators such as AllReduce, Broadcast, AllGather, Scatter, ReduceScatter, Reduce, AlltoAll, and AlltoAllV, and provides corresponding APIs for quickly implementing collective communication capabilities.

Broadcast

The Broadcast operation is to broadcast the data of the root rank in the communicator to other ranks.

Note: Only one root rank is allowed in a communicator.

Related API: HcclBroadcast

Scatter

The Scatter operation is to evenly distribute data of the root rank in the communicator to other ranks.

Note: Only one root rank is allowed in a communicator.

Related API: HcclScatter

AllGather

The AllGather operation is to resort the inputs of all ranks in a communicator by rank ID (in ascending order), combine the inputs, and send the result to the output buffers of all ranks.

For the AllGather operation, each rank receives a set of data that is resorted by rank ID, that is, AllGather outputs of all ranks are the same.

Related API: HcclAllGather

AllGatherV

The AllGatherV operation is to resort the inputs of all ranks in a communicator by rank ID (in ascending order), combine the inputs, and send the result to the outputs of all ranks. Different from the AllGather operation, AllGatherV allows different data sizes to be configured for the input of different ranks in the communicator.

For the AllGatherV operation, each rank receives a set of data that is resorted by rank ID, that is, AllGatherV outputs of all ranks are the same.

Related API: HcclAllGatherV

Reduce

The Reduce operation is to perform reduction (sum, prod, max, and min) over the input data of all ranks in a communicator, and then send the result to the output buffer of the root rank.

Note: Only one root rank is allowed in a communicator.

Related API: HcclReduce

AllReduce

The AllReduce operation is to perform reduction (sum, prod, max, and min) over the input data of all ranks in a communicator, and then send the result to the output buffers of all ranks.

Note: Each rank can have only one input.

Related API: HcclAllReduce

ReduceScatter

The ReduceScatter operation is to evenly divide the input data of all ranks in a communicator into rank size parts, and then perform reduction (sum, prod, max, and min) on 1/rank size part of data of each rank. Finally, the result is distributed to the output buffer of each rank based on the number.

Related API: HcclReduceScatter

ReduceScatterV

The ReduceScatterV operation is similar to the ReduceScatter operation. The difference is that the ReduceScatterV operation supports the configuration of different data sizes for different ranks in a communicator. (The data size can be configured for different IDs in a rank, but the data size of the same ID in different ranks must be the same.) After the reduction operation (sum, prod, max, and min) is performed on data with the same ID in each rank, the result is distributed to the output buffer of each rank based on the ID.

Related API: HcclReduceScatterV

AlltoAll

The AlltoAll operation is to send the same-sized data to all ranks in the communicator, and receive the same-sized data from all ranks.

The AlltoAll operation is to divide input data into blocks in a specific dimension, send the blocks to other ranks in sequence, receive input data from other ranks, and concatenate data in a specific dimension in sequence.

Related API: HcclAlltoAll

AlltoAllV

The AlltoAllV operation is to send data (whose size can be customized) to all ranks in the communicator and receive data from all ranks.

Related API: HcclAlltoAllV

AlltoAllVC

The AlltoAllVC operation is to send data (whose size can be customized) to all ranks in the communicator and receive data from all ranks. Compared with AlltoAllV, AlltoAllVC uses the input parameter sendCountMatrix to pass the RX and TX parameters of all ranks.

Related API: HcclAlltoAllVC

API Call

The following uses the HcclAllReduce API as an example. The prototype definition of the HcclAllReduce API is as follows:

1
HcclResult HcclAllReduce(void *sendBuf, void *recvBuf, uint64_t count, HcclDataType dataType, HcclReduceOp op, HcclComm comm, aclrtStream stream)

HcclAllReduce is used to perform the reduction operation on the input of all ranks within the communicator and broadcast the result to the output of all ranks. The op parameter is used to specify the reduction operation type, and the current version supports sum, prod, max, and min. HcclAllReduce allows only one input for each rank.

As shown in the following code snippet, all data in the input memory in the communicator is added in the float32 data format (in the example, only one piece of data is involved in each rank), and then the addition result is sent to the output memory of all ranks.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
void* hostBuf = nullptr;
void* sendBuf = nullptr;
void* recvBuf = nullptr;
uint64_t count = 1;
int malloc_kSize = count * sizeof(float);
aclrtStream stream;
aclrtCreateStream(&stream);

// Allocate memory for collective communication.
aclrtMalloc((void**)&sendbuff, malloc_kSize, ACL_MEM_MALLOC_HUGE_ONLY); 
aclrtMalloc((void**)&recvbuff, malloc_kSize, ACL_MEM_MALLOC_HUGE_ONLY);

// Initialize the input memory.
aclrtMallocHost((void**)&hostBuf, malloc_kSize);
aclrtMemcpy((void*)sendBuf, malloc_kSize, (void*)hostBuf, malloc_kSize, ACL_MEMCPY_HOST_TO_DEVICE);

// Perform collective communication.
HcclAllReduce((void *)sendBuf, (void*)recvBuf, count, HCCL_DATA_TYPE_FP32, HCCL_REDUCE_SUM, hcclComm, stream);

For details about the complete call sample of the HcclAllReduce API, see the HcclAllReduce operation code samples of different communicator initialization modes in Sample Code.