1/**
2 * Copyright (c) 2017-present, Facebook, Inc.
3 * All rights reserved.
4 *
5 * This source code is licensed under the BSD-style license found in the
6 * LICENSE file in the root directory of this source tree.
7 */
8
9#include "gloo/algorithm.h"
10
11#include "gloo/common/logging.h"
12
13namespace gloo {
14
15// Do host reduce/bcast on buf size less than 256KB and device reduce above
16const size_t kOnDeviceThreshold = 256 * 1024;
17
18Algorithm::Algorithm(const std::shared_ptr<Context>& context)
19 : context_(context),
20 contextRank_(context_->rank),
21 contextSize_(context_->size) {}
22
23// Have to provide implementation for pure virtual destructor.
24Algorithm::~Algorithm() noexcept(false) {}
25
26std::unique_ptr<transport::Pair>& Algorithm::getPair(int i) {
27 return context_->getPair(i);
28}
29
30// Helper for ring algorithms
31std::unique_ptr<transport::Pair>& Algorithm::getLeftPair() {
32 auto rank = (context_->size + context_->rank - 1) % context_->size;
33 GLOO_ENFORCE(context_->getPair(rank), "pair missing (index ", rank, ")");
34 return context_->getPair(rank);
35}
36
37// Helper for ring algorithms
38std::unique_ptr<transport::Pair>& Algorithm::getRightPair() {
39 auto rank = (context_->rank + 1) % context_->size;
40 GLOO_ENFORCE(context_->getPair(rank), "pair missing (index ", rank, ")");
41 return context_->getPair(rank);
42}
43
44} // namespace gloo
45