Caffe2 - C++ API
A deep learning, cross platform ML framework
allreduce.cpp
1 #include <c10d/FileStore.hpp>
2 #include <c10d/ProcessGroupGloo.hpp>
3 
4 using namespace ::c10d;
5 
6 int main(int argc, char** argv) {
7  int rank = atoi(getenv("RANK"));
8  int size = atoi(getenv("SIZE"));
9  auto store = std::make_shared<FileStore>("/tmp/c10d_example", size);
10  ProcessGroupGloo pg(store, rank, size);
11 
12  // Create some tensors
13  const auto ntensors = 10;
14  std::vector<at::Tensor> tensors;
15  for (auto i = 0; i < ntensors; i++) {
16  auto x =
17  at::ones({1000, 16 * (i + 1)}, at::TensorOptions(at::CPU(at::kFloat)));
18  tensors.push_back(x);
19  }
20 
21  // Kick off work
22  std::vector<std::shared_ptr<ProcessGroup::Work>> pending;
23  for (auto i = 0; i < ntensors; i++) {
24  std::vector<at::Tensor> tmp = {tensors[i]};
25  pending.push_back(pg.allreduce(tmp));
26  }
27 
28  // Wait for work to complete
29  for (auto& work : pending) {
30  work->wait();
31  }
32 }
Definition: ddp.cpp:21