File: basic.cc

package info (click to toggle)
rabit 0.0~git20200628.74bf00a-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 720 kB
  • sloc: cpp: 5,015; ansic: 710; python: 360; makefile: 306; sh: 136
file content (35 lines) | stat: -rw-r--r-- 1,015 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
/*!
 *  Copyright (c) 2014 by Contributors
 * \file basic.cc
 * \brief This is an example demonstrating what is Allreduce
 *
 * \author Tianqi Chen
 */
#define _CRT_SECURE_NO_WARNINGS
#define _CRT_SECURE_NO_DEPRECATE
#include <vector>
#include <rabit/rabit.h>
using namespace rabit;
int main(int argc, char *argv[]) {
  int N = 3;
  if (argc > 1) {
    N = atoi(argv[1]);
  }
  std::vector<int> a(N);
  rabit::Init(argc, argv);
  for (int i = 0; i < N; ++i) {
    a[i] = rabit::GetRank() + i;
  }
  printf("@node[%d] before-allreduce: a={%d, %d, %d}\n",
         rabit::GetRank(), a[0], a[1], a[2]);
  // allreduce take max of each elements in all processes
  Allreduce<op::Max>(&a[0], N);
  printf("@node[%d] after-allreduce-max: a={%d, %d, %d}\n",
         rabit::GetRank(), a[0], a[1], a[2]);
  // second allreduce that sums everything up
  Allreduce<op::Sum>(&a[0], N);
  printf("@node[%d] after-allreduce-sum: a={%d, %d, %d}\n",
         rabit::GetRank(), a[0], a[1], a[2]);
  rabit::Finalize();
  return 0;
}