|
| 1 | +/** |
| 2 | + * InfiniCCL Example: Thread-per-GPU Single-Node AllGather |
| 3 | + * |
| 4 | + * This example validates out-of-place and in-place AllGather across two GPUs |
| 5 | + * through InfiniCCL's native CCL backend without an MPI launcher. |
| 6 | + */ |
| 7 | + |
| 8 | +#include <algorithm> |
| 9 | +#include <atomic> |
| 10 | +#include <cstddef> |
| 11 | +#include <cstdlib> |
| 12 | +#include <iostream> |
| 13 | +#include <thread> |
| 14 | +#include <vector> |
| 15 | + |
| 16 | +#include "backend_manifest.h" |
| 17 | +#include "infiniccl.h" |
| 18 | +#include "utils.h" |
| 19 | + |
| 20 | +using namespace infini::ccl; |
| 21 | + |
| 22 | +namespace { |
| 23 | + |
| 24 | +constexpr int kRankCount = 2; |
| 25 | +constexpr size_t kNumElements = 1 << 10; |
| 26 | + |
| 27 | +struct ThreadArgs { |
| 28 | + int rank; |
| 29 | + infinicclUniqueId id; |
| 30 | + std::atomic_bool* all_correct; |
| 31 | +}; |
| 32 | + |
| 33 | +bool Validate(const std::vector<float>& output, int rank, const char* mode) { |
| 34 | + for (int source = 0; source < kRankCount; ++source) { |
| 35 | + const float expected = static_cast<float>(source + 1); |
| 36 | + const size_t offset = static_cast<size_t>(source) * kNumElements; |
| 37 | + for (size_t i = 0; i < kNumElements; ++i) { |
| 38 | + if (output[offset + i] != expected) { |
| 39 | + std::cerr << mode << " validation failed on rank " << rank |
| 40 | + << " at source rank " << source << ", element " << i |
| 41 | + << ": expected " << expected << ", got " << output[offset + i] |
| 42 | + << "." << std::endl; |
| 43 | + return false; |
| 44 | + } |
| 45 | + } |
| 46 | + } |
| 47 | + return true; |
| 48 | +} |
| 49 | + |
| 50 | +void WorkerThread(ThreadArgs args) { |
| 51 | + constexpr Device::Type kDevType = |
| 52 | + ListGetBest<DevicePriority>(EnabledDevices{}); |
| 53 | + using Rt = Runtime<kDevType>; |
| 54 | + |
| 55 | + CHECK_RT(Rt, Rt::SetDevice(args.rank)); |
| 56 | + |
| 57 | + infinicclComm_t comm = nullptr; |
| 58 | + CHECK_INFINI(infinicclCommInitRank(&comm, kRankCount, args.id, args.rank)); |
| 59 | + |
| 60 | + std::vector<float> host_send(kNumElements, static_cast<float>(args.rank + 1)); |
| 61 | + std::vector<float> host_recv(kNumElements * kRankCount, 0.0f); |
| 62 | + float* device_send = nullptr; |
| 63 | + float* device_recv = nullptr; |
| 64 | + const size_t send_bytes = kNumElements * sizeof(float); |
| 65 | + const size_t recv_bytes = send_bytes * kRankCount; |
| 66 | + |
| 67 | + CHECK_RT(Rt, Rt::Malloc(reinterpret_cast<void**>(&device_send), send_bytes)); |
| 68 | + CHECK_RT(Rt, Rt::Malloc(reinterpret_cast<void**>(&device_recv), recv_bytes)); |
| 69 | + CHECK_RT(Rt, Rt::Memcpy(device_send, host_send.data(), send_bytes, |
| 70 | + Rt::MemcpyHostToDevice)); |
| 71 | + |
| 72 | + CHECK_INFINI(infinicclAllGather(device_send, device_recv, kNumElements, |
| 73 | + infinicclFloat32, comm, nullptr)); |
| 74 | + CHECK_RT(Rt, Rt::Memcpy(host_recv.data(), device_recv, recv_bytes, |
| 75 | + Rt::MemcpyDeviceToHost)); |
| 76 | + if (!Validate(host_recv, args.rank, "Out-of-place AllGather")) { |
| 77 | + args.all_correct->store(false, std::memory_order_relaxed); |
| 78 | + } |
| 79 | + |
| 80 | + std::fill(host_recv.begin(), host_recv.end(), 0.0f); |
| 81 | + std::fill_n(host_recv.begin() + static_cast<size_t>(args.rank) * kNumElements, |
| 82 | + kNumElements, static_cast<float>(args.rank + 1)); |
| 83 | + CHECK_RT(Rt, Rt::Memcpy(device_recv, host_recv.data(), recv_bytes, |
| 84 | + Rt::MemcpyHostToDevice)); |
| 85 | + |
| 86 | + float* local_block = |
| 87 | + device_recv + static_cast<size_t>(args.rank) * kNumElements; |
| 88 | + CHECK_INFINI(infinicclAllGather(local_block, device_recv, kNumElements, |
| 89 | + infinicclFloat32, comm, nullptr)); |
| 90 | + CHECK_RT(Rt, Rt::Memcpy(host_recv.data(), device_recv, recv_bytes, |
| 91 | + Rt::MemcpyDeviceToHost)); |
| 92 | + if (!Validate(host_recv, args.rank, "In-place AllGather")) { |
| 93 | + args.all_correct->store(false, std::memory_order_relaxed); |
| 94 | + } |
| 95 | + |
| 96 | + CHECK_RT(Rt, Rt::Free(device_send)); |
| 97 | + CHECK_RT(Rt, Rt::Free(device_recv)); |
| 98 | + CHECK_INFINI(infinicclCommDestroy(comm)); |
| 99 | +} |
| 100 | + |
| 101 | +} // namespace |
| 102 | + |
| 103 | +int main() { |
| 104 | + infinicclUniqueId shared_id; |
| 105 | + CHECK_INFINI(infinicclGetUniqueId(&shared_id)); |
| 106 | + |
| 107 | + std::atomic_bool all_correct{true}; |
| 108 | + std::vector<std::thread> threads; |
| 109 | + threads.reserve(kRankCount); |
| 110 | + for (int rank = 0; rank < kRankCount; ++rank) { |
| 111 | + threads.emplace_back(WorkerThread, |
| 112 | + ThreadArgs{rank, shared_id, &all_correct}); |
| 113 | + } |
| 114 | + for (auto& thread : threads) { |
| 115 | + thread.join(); |
| 116 | + } |
| 117 | + |
| 118 | + if (!all_correct.load(std::memory_order_relaxed)) { |
| 119 | + return EXIT_FAILURE; |
| 120 | + } |
| 121 | + std::cout << "AllGather validation passed." << std::endl; |
| 122 | + return EXIT_SUCCESS; |
| 123 | +} |
0 commit comments