|
| 1 | +/** |
| 2 | + * InfiniCCL Example: Reduce |
| 3 | + * |
| 4 | + * This example demonstrates the API for performing a collective |
| 5 | + * reduction across multiple accelerators and nodes, where only |
| 6 | + * `root` receives the reduced result. |
| 7 | + */ |
| 8 | + |
| 9 | +#include <unistd.h> |
| 10 | + |
| 11 | +#include <iostream> |
| 12 | +#include <vector> |
| 13 | + |
| 14 | +// Public API |
| 15 | +#include "infiniccl.h" |
| 16 | + |
| 17 | +// Example-Specific Utilities |
| 18 | +#include "utils.h" |
| 19 | + |
| 20 | +// Internal Headers (Accessible via example-specific include paths, technically |
| 21 | +// not public APIs) |
| 22 | +#include "backend_manifest.h" |
| 23 | +#include "device.h" |
| 24 | +#include "runtime.h" |
| 25 | +#include "traits.h" |
| 26 | + |
| 27 | +using namespace infini::ccl; |
| 28 | + |
| 29 | +void RunReduceExample(int argc, char **argv, int warmup_iter, int profile_iter, |
| 30 | + const size_t kNumElements) { |
| 31 | + constexpr Device::Type kDevType = |
| 32 | + ListGetBest<DevicePriority>(EnabledDevices{}); |
| 33 | + using Rt = Runtime<kDevType>; |
| 34 | + |
| 35 | + CHECK_INFINI(infiniInit(&argc, &argv)); |
| 36 | + |
| 37 | + int rank, size; |
| 38 | + CHECK_INFINI(infiniGetRank(&rank)); |
| 39 | + CHECK_INFINI(infiniGetSize(&size)); |
| 40 | + |
| 41 | + char hostname[256]; |
| 42 | + gethostname(hostname, sizeof(hostname)); |
| 43 | + |
| 44 | + // Map local rank to GPU device. |
| 45 | + // Note: this is just for info printing. In practice, this part is not needed. |
| 46 | + const char *local_rank_str = std::getenv("OMPI_COMM_WORLD_LOCAL_RANK"); |
| 47 | + int local_rank = 0; |
| 48 | + if (local_rank_str != nullptr) { |
| 49 | + local_rank = std::atoi(local_rank_str); |
| 50 | + } |
| 51 | + |
| 52 | + std::cout << "[Rank " << rank << "] Host: " << hostname |
| 53 | + << " | GPU: " << Device::StringFromType(kDevType) << " " |
| 54 | + << " | Device " << local_rank << std::endl; |
| 55 | + |
| 56 | + // Setup Communicator |
| 57 | + infiniComm_t comm = nullptr; |
| 58 | + CHECK_INFINI(infiniCommInitAll(&comm, size, nullptr)); |
| 59 | + |
| 60 | + // Root of the Reduce |
| 61 | + constexpr int kRoot = 0; |
| 62 | + |
| 63 | + // Prepare Data |
| 64 | + std::vector<float> h_send(kNumElements); |
| 65 | + std::vector<float> h_recv(kNumElements, 0.0f); |
| 66 | + |
| 67 | + // Initialize: each rank provides its (rank + 1) as data. |
| 68 | + for (size_t i = 0; i < kNumElements; i++) { |
| 69 | + h_send[i] = static_cast<float>(rank + 1); |
| 70 | + } |
| 71 | + |
| 72 | + float *d_send, *d_recv; |
| 73 | + size_t total_bytes = kNumElements * sizeof(*d_send); |
| 74 | + CHECK_RT(Rt, Rt::Malloc((void **)&d_send, total_bytes)); |
| 75 | + CHECK_RT(Rt, Rt::Malloc((void **)&d_recv, total_bytes)); |
| 76 | + CHECK_RT(Rt, Rt::Memcpy(d_send, h_send.data(), total_bytes, |
| 77 | + Rt::MemcpyHostToDevice)); |
| 78 | + CHECK_RT(Rt, Rt::Memcpy(d_recv, h_recv.data(), total_bytes, |
| 79 | + Rt::MemcpyHostToDevice)); |
| 80 | + |
| 81 | + if (rank == kRoot) { |
| 82 | + std::cout << "\n=== Performing Reduce on GPU Memory ===" << std::endl; |
| 83 | + std::cout << "Data size: " << kNumElements << " floats (" |
| 84 | + << total_bytes / 1024 / 1024 << " MB)" << std::endl; |
| 85 | + std::cout << "Operation: Sum" << std::endl; |
| 86 | + std::cout << "Root Rank: " << kRoot << std::endl; |
| 87 | + std::cout << "Warm-up iterations: " << warmup_iter << std::endl; |
| 88 | + std::cout << "Profile iterations: " << profile_iter << std::endl; |
| 89 | + } |
| 90 | + |
| 91 | + CHECK_RT(Rt, Rt::StreamSynchronize(nullptr)); |
| 92 | + |
| 93 | + // Warm-up and D2H transfer the answer on `root`. |
| 94 | + CHECK_INFINI(infiniReduce(d_send, d_recv, kNumElements, infiniFloat32, |
| 95 | + infiniSum, kRoot, comm, nullptr)); |
| 96 | + if (rank == kRoot) { |
| 97 | + CHECK_RT(Rt, Rt::Memcpy(h_recv.data(), d_recv, total_bytes, |
| 98 | + Rt::MemcpyDeviceToHost)); |
| 99 | + } |
| 100 | + |
| 101 | + for (int i = 1; i < warmup_iter; ++i) { |
| 102 | + CHECK_INFINI(infiniReduce(d_send, d_recv, kNumElements, infiniFloat32, |
| 103 | + infiniSum, kRoot, comm, nullptr)); |
| 104 | + } |
| 105 | + CHECK_RT(Rt, Rt::StreamSynchronize(nullptr)); |
| 106 | + |
| 107 | + // Profiling |
| 108 | + Timer timer; |
| 109 | + |
| 110 | + for (int i = 0; i < profile_iter; ++i) { |
| 111 | + CHECK_INFINI(infiniReduce(d_send, d_recv, kNumElements, infiniFloat32, |
| 112 | + infiniSum, kRoot, comm, nullptr)); |
| 113 | + } |
| 114 | + |
| 115 | + CHECK_RT(Rt, Rt::StreamSynchronize(nullptr)); |
| 116 | + double elapsed = timer.elapsed_ms() / static_cast<double>(profile_iter); |
| 117 | + |
| 118 | + // Result Validation (only meaningful on `root`). |
| 119 | + if (rank == kRoot) { |
| 120 | + float expected = 0.0f; |
| 121 | + for (int r = 0; r < size; ++r) { |
| 122 | + expected += static_cast<float>(r + 1); |
| 123 | + } |
| 124 | + |
| 125 | + Validator::ValidateResult(h_recv.data(), kNumElements, expected, rank, true, |
| 126 | + "Reduce"); |
| 127 | + |
| 128 | + Metrics metrics{elapsed, total_bytes, size}; |
| 129 | + metrics.Print(); |
| 130 | + } |
| 131 | + |
| 132 | + // Cleanup |
| 133 | + CHECK_RT(Rt, Rt::Free(d_send)); |
| 134 | + CHECK_RT(Rt, Rt::Free(d_recv)); |
| 135 | + |
| 136 | + CHECK_INFINI(infiniCommDestroy(comm)); |
| 137 | + CHECK_INFINI(infiniFinalize()); |
| 138 | + |
| 139 | + if (rank == kRoot) { |
| 140 | + std::cout << "InfiniCCL finalized." << std::endl; |
| 141 | + } |
| 142 | +} |
| 143 | + |
| 144 | +int main(int argc, char **argv) { |
| 145 | + int warmup_iters = 2; |
| 146 | + int profile_iters = 20; |
| 147 | + size_t num_elements = 1 << 20; |
| 148 | + |
| 149 | + RunReduceExample(argc, argv, warmup_iters, profile_iters, num_elements); |
| 150 | + |
| 151 | + return EXIT_SUCCESS; |
| 152 | +} |
0 commit comments