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/context.h"
10
11#include "gloo/common/error.h"
12#include "gloo/common/logging.h"
13#include "gloo/transport/device.h"
14#include "gloo/transport/unbound_buffer.h"
15
16namespace gloo {
17
18static const std::chrono::seconds kTimeoutDefault = std::chrono::seconds(30);
19
20Context::Context(int rank, int size, int base)
21 : rank(rank),
22 size(size),
23 base(base),
24 slot_(0),
25 timeout_(kTimeoutDefault) {
26 GLOO_ENFORCE_GE(rank, 0);
27 GLOO_ENFORCE_LT(rank, size);
28 GLOO_ENFORCE_GE(size, 1);
29}
30
31Context::~Context() {
32}
33
34std::shared_ptr<transport::Device>& Context::getDevice() {
35 GLOO_ENFORCE(device_, "Device not set!");
36 return device_;
37}
38
39std::unique_ptr<transport::Pair>& Context::getPair(int i) {
40 GLOO_ENFORCE(transportContext_, "Transport context not set!");
41 return transportContext_->getPair(i);
42}
43
44std::unique_ptr<transport::UnboundBuffer> Context::createUnboundBuffer(
45 void* ptr, size_t size) {
46 return transportContext_->createUnboundBuffer(ptr, size);
47}
48
49int Context::nextSlot(int numToSkip) {
50 GLOO_ENFORCE_GT(numToSkip, 0);
51 auto temp = slot_;
52 slot_ += numToSkip;
53 return temp;
54}
55
56void Context::closeConnections() {
57 for (auto i = 0; i < size; i++) {
58 auto& pair = getPair(i);
59 if (pair) {
60 pair->close();
61 }
62 }
63}
64
65void Context::setTimeout(std::chrono::milliseconds timeout=kTimeoutDefault) {
66 GLOO_ENFORCE(timeout.count() >= 0, "Invalid timeout");
67 timeout_ = timeout;
68}
69
70std::chrono::milliseconds Context::getTimeout() const {
71 return timeout_;
72}
73
74} // namespace gloo
75