* Copyright (c) 2025 Huawei Technologies Co., Ltd.
* This program is free software, you can redistribute it and/or modify it under the terms and conditions of
* CANN Open Software License Agreement Version 2.0 (the "License").
* Please refer to the License for details. You may not use this file except in compliance with the License.
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED,
* INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE.
* See LICENSE in the root of the software repository for the full text of the License.
*/
#include <iostream>
#include <vector>
#include <memory>
#include <thread>
#include <chrono>
#include <cstring>
#include <hccl/hccl.h>
#include <hccl/hccl_types.h>
#include <mpi.h>
#define ACLCHECK(ret) \
do { \
if (ret != ACL_SUCCESS) { \
printf("acl interface return err %s:%d, retcode: %d \n", __FILE__, __LINE__, ret); \
return ret; \
} \
} while (0)
#define HCCLCHECK(ret) \
do { \
if (ret != HCCL_SUCCESS) { \
printf("hccl interface return err %s:%d, retcode: %d \n", __FILE__, __LINE__, ret); \
return ret; \
} \
} while (0)
struct ThreadContext {
HcclComm comm;
uint32_t device;
uint32_t devCount;
};
int Sample(void *arg)
{
ThreadContext *ctx = (ThreadContext *)arg;
void *hostBuf = nullptr;
void *sendBuf = nullptr;
void *recvBuf = nullptr;
uint32_t device = ctx->device;
uint64_t count = ctx->devCount;
size_t mallocSize = count * sizeof(float);
ACLCHECK(aclrtMalloc(&sendBuf, mallocSize, ACL_MEM_MALLOC_HUGE_ONLY));
ACLCHECK(aclrtMalloc(&recvBuf, mallocSize, ACL_MEM_MALLOC_HUGE_ONLY));
ACLCHECK(aclrtMallocHost(&hostBuf, mallocSize));
float *tmpHostBuff = static_cast<float *>(hostBuf);
for (uint64_t i = 0; i < count; ++i) {
tmpHostBuff[i] = static_cast<float>(i);
}
ACLCHECK(aclrtMemcpy(sendBuf, mallocSize, hostBuf, mallocSize, ACL_MEMCPY_HOST_TO_DEVICE));
aclrtStream stream;
ACLCHECK(aclrtCreateStream(&stream));
HCCLCHECK(HcclAllReduce(sendBuf, recvBuf, count, HCCL_DATA_TYPE_FP32, HCCL_REDUCE_SUM, ctx->comm, stream));
ACLCHECK(aclrtSynchronizeStream(stream));
std::this_thread::sleep_for(std::chrono::seconds(device));
void *resultBuff;
ACLCHECK(aclrtMallocHost(&resultBuff, mallocSize));
ACLCHECK(aclrtMemcpy(resultBuff, mallocSize, recvBuf, mallocSize, ACL_MEMCPY_DEVICE_TO_HOST));
float *tmpResBuff = static_cast<float *>(resultBuff);
std::cout << "rankId: " << device << ", output: [";
for (uint64_t i = 0; i < count; ++i) {
std::cout << " " << tmpResBuff[i];
}
std::cout << " ]" << std::endl;
ACLCHECK(aclrtFreeHost(resultBuff));
ACLCHECK(aclrtFree(sendBuf));
ACLCHECK(aclrtFree(recvBuf));
ACLCHECK(aclrtFreeHost(hostBuf));
ACLCHECK(aclrtDestroyStream(stream));
return 0;
}
int main(int argc, char *argv[])
{
MPI_Init(&argc, &argv);
int procSize = 0;
int procRank = 0;
MPI_Comm_size(MPI_COMM_WORLD, &procSize);
MPI_Comm_rank(MPI_COMM_WORLD, &procRank);
uint32_t devId = static_cast<uint32_t>(procRank);
uint32_t devCount = static_cast<uint32_t>(procSize);
ACLCHECK(aclInit(NULL));
ACLCHECK(aclrtSetDevice(static_cast<int32_t>(devId)));
HcclRootInfo rootInfo;
uint32_t rootRank = 0;
if (devId == rootRank) {
HCCLCHECK(HcclGetRootInfo(&rootInfo));
}
MPI_Bcast(&rootInfo, HCCL_ROOT_INFO_BYTES, MPI_CHAR, rootRank, MPI_COMM_WORLD);
MPI_Barrier(MPI_COMM_WORLD);
HcclCommConfig config;
HcclCommConfigInit(&config);
config.hcclBufferSize = 1024;
config.hcclDeterministic = 1;
std::strcpy(config.hcclCommName, "comm_1");
HcclComm hcclComm;
HCCLCHECK(HcclCommInitRootInfoConfig(devCount, &rootInfo, devId, &config, &hcclComm));
struct ThreadContext args;
args.comm = hcclComm;
args.device = devId;
args.devCount = devCount;
Sample((void *)&args);
HCCLCHECK(HcclCommDestroy(hcclComm));
ACLCHECK(aclrtResetDevice(static_cast<int32_t>(devId)));
ACLCHECK(aclFinalize());
MPI_Finalize();
return 0;
}