|
| 1 | +/** |
| 2 | + * Copyright (c) 2018-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. An additional grant |
| 7 | + * of patent rights can be found in the PATENTS file in the same directory. |
| 8 | + */ |
| 9 | + |
| 10 | +#include "gloo/gather.h" |
| 11 | + |
| 12 | +#include <cstring> |
| 13 | + |
| 14 | +#include "gloo/common/logging.h" |
| 15 | +#include "gloo/types.h" |
| 16 | + |
| 17 | +namespace gloo { |
| 18 | + |
| 19 | +void gather(const std::shared_ptr<Context>& context, GatherOptions& opts) { |
| 20 | + std::unique_ptr<transport::UnboundBuffer> tmpInBuffer; |
| 21 | + std::unique_ptr<transport::UnboundBuffer> tmpOutBuffer; |
| 22 | + transport::UnboundBuffer* in = nullptr; |
| 23 | + transport::UnboundBuffer* out = nullptr; |
| 24 | + const auto slot = Slot::build(kGatherSlotPrefix, opts.tag); |
| 25 | + |
| 26 | + // Sanity checks |
| 27 | + GLOO_ENFORCE(opts.elementSize > 0); |
| 28 | + |
| 29 | + // Figure out pointer to input buffer |
| 30 | + if (opts.inBuffer) { |
| 31 | + in = opts.inBuffer.get(); |
| 32 | + } else { |
| 33 | + GLOO_ENFORCE(opts.inPtr != nullptr); |
| 34 | + GLOO_ENFORCE(opts.inElements > 0); |
| 35 | + tmpInBuffer = context->createUnboundBuffer( |
| 36 | + opts.inPtr, opts.inElements * opts.elementSize); |
| 37 | + in = tmpInBuffer.get(); |
| 38 | + } |
| 39 | + |
| 40 | + if (context->rank == opts.root) { |
| 41 | + const size_t chunkSize = in->size; |
| 42 | + |
| 43 | + // Figure out pointer to output buffer (only for root rank) |
| 44 | + if (opts.outBuffer) { |
| 45 | + out = opts.outBuffer.get(); |
| 46 | + } else { |
| 47 | + GLOO_ENFORCE(opts.outPtr != nullptr); |
| 48 | + GLOO_ENFORCE(opts.outElements > 0); |
| 49 | + tmpOutBuffer = context->createUnboundBuffer( |
| 50 | + opts.outPtr, opts.outElements * opts.elementSize); |
| 51 | + out = tmpOutBuffer.get(); |
| 52 | + } |
| 53 | + |
| 54 | + // Ensure the output buffer has the right size. |
| 55 | + GLOO_ENFORCE(in->size * context->size == out->size); |
| 56 | + |
| 57 | + // Post receive operations from peers into out buffer |
| 58 | + for (size_t i = 0; i < context->size; i++) { |
| 59 | + if (i == context->rank) { |
| 60 | + continue; |
| 61 | + } |
| 62 | + out->recv(i, slot, i * chunkSize, chunkSize); |
| 63 | + } |
| 64 | + |
| 65 | + // Copy local input to output |
| 66 | + memcpy( |
| 67 | + static_cast<char*>(out->ptr) + (context->rank * chunkSize), |
| 68 | + in->ptr, |
| 69 | + chunkSize); |
| 70 | + |
| 71 | + // Wait for receive operations to complete |
| 72 | + for (size_t i = 0; i < context->size; i++) { |
| 73 | + if (i == context->rank) { |
| 74 | + continue; |
| 75 | + } |
| 76 | + out->waitRecv(); |
| 77 | + } |
| 78 | + } else { |
| 79 | + in->send(opts.root, slot); |
| 80 | + in->waitSend(); |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +} // namespace gloo |
0 commit comments