Creating Resources
Communication Resource Computing
- Thread: indicates the concurrency relationship within a single rank. Tasks on the same thread are executed in strict sequence, and tasks on different threads can be executed concurrently. Regardless of the execution mode, threads can be classified into one primary thread and multiple secondary threads. The primary thread controls the start and end of tasks on the secondary threads. The primary thread must be allocated, and the number of secondary threads to be allocated is separately calculated by each communication algorithm.
- Notify: implements synchronization, including intra-rank inter-thread synchronization and inter-rank synchronization.
- Channel: used for cross-rank data transfer.
- Ring communication algorithm
The following figure shows the hardware topology of the ring algorithm. Each rank receives data from the previous rank and sends data to the next rank.

The number of communication resources required by the ring communication algorithm in the preceding networking is as follows:
- Thread: Each rank communicates with the ranks on its left and right, which can be performed asynchronously. Therefore, each rank requires two thread resources.
- Thread Notify: It is used for inter-thread synchronization. Since a thread requires only one Notify to receive POST signals from another, a single Notify is sufficient for two-thread synchronization.
- Channel: Each rank communicates with the ranks on its left and right. Therefore, each rank requires two communication channels.
- Channel Notify: Each rank synchronizes with the left and right ranks through paired Record and Wait operations. Consequently, each channel requires two Notify resources, aggregating to a total of four Notify resources across both channels of each rank.
- Mesh communication algorithm
The following figure shows the hardware topology of the mesh algorithm. Each rank directly communicates with all other ranks.

The number of communication resources required by the mesh communication algorithm in the preceding networking is as follows:
- Thread: Each rank communicates with all other ranks, which can be performed asynchronously. Therefore, each rank requires three thread resources.
- Thread Notify: It is used for inter-thread synchronization. Since a thread requires only one Notify to receive POST signals from another, two Notify resources are sufficient for three-thread synchronization.
- Channel: Each rank communicates with all other ranks. Therefore, each rank requires three communication channels.
- Channel Notify: Each rank performs full mesh synchronization with all other ranks, dividing synchronization into pre-sync and post-sync operations that must be used in pairs. Pre-sync notifies remote ranks that data or memory is ready for remote read/write access, while post-sync signals that the local read/write operations are complete. Consequently, each rank with three channels requires two Notify resources per channel, aggregating to a total of six Notify resources.
The following table describes the number of communication resources required by each rank for the ring communication algorithm and mesh communication algorithm.
Communication Resource |
Ring Algorithm |
Mesh Algorithm |
|---|---|---|
Thread |
2 |
3 |
Notify |
4 |
6 |
Channel |
2 |
3 |
Sample Code
The following uses the custom P2P Send/Receive communication operators as an example to describe the code snippet for creating resources on the host.
- Allocate the context memory to store resource information.
uint64_t size = size of the context memory to be allocated; void *ctx = nullptr; char *tag = tag used to save resources; CommEngine engine = COMM_ENGINE_AICPU_TS; // Select an engine. HcclResult ret = HcclEngineCtxGet(comm, tag, engine, &ctx, &size); if (ret != HCCL_SUCCESS) { // Resource for the tag has not been created yet. HcclEngineCtxCreate(comm, tag, engine, size, ctx); }else { // Resource has been created before. Use ctx directly. } - Allocate thread resources.
CommEngine engine = CommEngine::COMM_ENGINE_AICPU; // AICPU communication engine uint32_t threadNum = 1; // Allocate a communication thread. uint32_t notifyNumPerThread = 0; // Only one communication thread is involved, and inter-thread synchronization is not required. Therefore, no additional synchronization resources are required in the communication thread. ThreadHandle thread; HcclThreadAcquire(comm, engine, threadNum, notifyNumPerThread, &thread); // Save the thread handle to the memory pointed by ctx for future use.
- Establish a communication channel between two ranks.
HcclChannelDesc channelDesc; HcclChannelDescInit(&channelDesc, 1); channelDesc.remoteRank = destRank; channelDesc.channelProtocol = CommProtocol::COMM_PROTOCOL_HCCS; channelDesc.notifyNum = 2; // Both pre-sync and post-sync depend on Notify, thereby requiring two Notify resources in total. ChannelHandle channelHandle; HcclChannelAcquire(comm, engine, &channelDesc, 1, &channelHandle); // Save the channel handle to the memory pointed by ctx for future use.
- Allocate communication memory.
// Allocate and obtain the local communication memory. void *localAddr; uint64_t localSize; HcclGetHcclBuffer(comm, &localAddr, &localSize); // Obtain the remote communication memory for subsequent reads and writes. void *remoteAddr; uint64_t remoteSize; HcclChannelGetHcclBuffer(comm, channelHandle, &remoteAddr, &remoteSize); // Save the memory address to the memory pointed by ctx for future use.