-
Notifications
You must be signed in to change notification settings - Fork 338
Add new style allgather collective #137
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,109 @@ | ||
| /** | ||
| * Copyright (c) 2018-present, Facebook, Inc. | ||
| * All rights reserved. | ||
| * | ||
| * This source code is licensed under the BSD-style license found in the | ||
| * LICENSE file in the root directory of this source tree. An additional grant | ||
| * of patent rights can be found in the PATENTS file in the same directory. | ||
| */ | ||
|
|
||
| #include "gloo/allgather.h" | ||
|
|
||
| #include <cstring> | ||
|
|
||
| #include "gloo/common/logging.h" | ||
| #include "gloo/types.h" | ||
|
|
||
| namespace gloo { | ||
|
|
||
| void allgather(const std::shared_ptr<Context>& context, AllgatherOptions& opts) { | ||
| std::unique_ptr<transport::UnboundBuffer> tmpInBuffer; | ||
| std::unique_ptr<transport::UnboundBuffer> tmpOutBuffer; | ||
| transport::UnboundBuffer* in = nullptr; | ||
| transport::UnboundBuffer* out = nullptr; | ||
| const auto slot = Slot::build(kAllgatherSlotPrefix, opts.tag); | ||
|
|
||
| // Sanity checks | ||
| GLOO_ENFORCE(opts.elementSize > 0); | ||
| const auto recvRank = (context->size + context->rank - 1) % context->size; | ||
| GLOO_ENFORCE(context->getPair(recvRank), "pair missing (rank ", recvRank, ")"); | ||
| const auto sendRank = (context->size + context->rank + 1) % context->size; | ||
| GLOO_ENFORCE(context->getPair(sendRank), "pair missing (rank ", sendRank, ")"); | ||
|
|
||
| // Figure out pointer to input buffer | ||
| if (opts.inBuffer) { | ||
| in = opts.inBuffer.get(); | ||
| } else if (opts.inPtr != nullptr) { | ||
| GLOO_ENFORCE(opts.inElements > 0); | ||
| tmpInBuffer = | ||
| context->createUnboundBuffer(opts.inPtr, opts.inElements * opts.elementSize); | ||
| in = tmpInBuffer.get(); | ||
| } | ||
|
|
||
| // Figure out pointer to output buffer | ||
| if (opts.outBuffer) { | ||
| out = opts.outBuffer.get(); | ||
| } else { | ||
| GLOO_ENFORCE(opts.outPtr != nullptr); | ||
| GLOO_ENFORCE(opts.outElements > 0); | ||
| tmpOutBuffer = | ||
| context->createUnboundBuffer(opts.outPtr, opts.outElements * opts.elementSize); | ||
| out = tmpOutBuffer.get(); | ||
| } | ||
|
|
||
| GLOO_ENFORCE_EQ(out->size, in->size * context->size); | ||
|
|
||
| // If the input buffer is specified, this is NOT an in place operation, | ||
| // and the output buffer needs to be primed with the input. | ||
| if (in != nullptr) { | ||
| memcpy( | ||
| (uint8_t*) out->ptr + context->rank * opts.inElements * opts.elementSize, | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use C++ case, please |
||
| (uint8_t*) in->ptr, | ||
| opts.inElements * opts.elementSize); | ||
| } | ||
|
|
||
| // The chunk size may not be divisible by 2; use dynamic lookup. | ||
| std::array<size_t, 2> chunkSize; | ||
| chunkSize[0] = (opts.inElements * opts.elementSize) / 2; | ||
| chunkSize[1] = (opts.inElements * opts.elementSize) - chunkSize[0]; | ||
| std::array<size_t, 2> chunkOffset; | ||
| chunkOffset[0] = 0; | ||
| chunkOffset[1] = chunkSize[0]; | ||
|
|
||
| for (auto i = 0; i < (context->size - 1) * 2; i++) { | ||
| size_t sendOffset = | ||
| (((context->size + context->rank - (i / 2)) | ||
| * opts.inElements | ||
| * opts.elementSize) | ||
| + chunkOffset[i & 0x1]) | ||
| % (opts.outElements * opts.elementSize); | ||
| size_t recvOffset = | ||
| (((context->size + context->rank - 1 - (i / 2)) | ||
| * opts.inElements | ||
| * opts.elementSize) | ||
| + chunkOffset[i & 0x1]) | ||
| % (opts.outElements * opts.elementSize); | ||
| size_t size = chunkSize[i & 0x1]; | ||
| if (i < 2) { | ||
| out->send(sendRank, slot, sendOffset, size); | ||
| out->recv(recvRank, slot, recvOffset, size); | ||
| continue; | ||
| } | ||
|
|
||
| // Wait for pending operations to complete to synchronize with the | ||
| // previous iteration. Because we kick off two operations before | ||
| // getting here we always wait for the next-to-last operation. | ||
| out->waitSend(); | ||
| out->waitRecv(); | ||
| out->send(sendRank, slot, sendOffset, size); | ||
| out->recv(recvRank, slot, recvOffset, size); | ||
| } | ||
|
|
||
| // Wait for completes | ||
| for (auto i = 0; i < 2; i++) { | ||
| out->waitSend(); | ||
| out->waitRecv(); | ||
| } | ||
| } | ||
|
|
||
| } // namespace gloo | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| /** | ||
| * Copyright (c) 2018-present, Facebook, Inc. | ||
| * All rights reserved. | ||
| * | ||
| * This source code is licensed under the BSD-style license found in the | ||
| * LICENSE file in the root directory of this source tree. An additional grant | ||
| * of patent rights can be found in the PATENTS file in the same directory. | ||
| */ | ||
|
|
||
| #pragma once | ||
|
|
||
| #include "gloo/context.h" | ||
| #include "gloo/transport/unbound_buffer.h" | ||
|
|
||
| namespace gloo { | ||
|
|
||
| struct AllgatherOptions { | ||
| // The input and output can either be specified as a unbound buffer | ||
| // (that can be cached and reused by the caller), or a literal | ||
| // pointer and number of elements stored at that pointer. | ||
| // | ||
| // The operation is executed in place on the output if the input is | ||
| // set to null. The input for this process is assumed to be at the | ||
| // location in the output buffer where it would otherwise be. | ||
| std::unique_ptr<transport::UnboundBuffer> inBuffer; | ||
| void* inPtr; | ||
| size_t inElements; | ||
| std::unique_ptr<transport::UnboundBuffer> outBuffer; | ||
| void* outPtr; | ||
| size_t outElements; | ||
|
|
||
| // Number of bytes per element. | ||
| size_t elementSize; | ||
|
|
||
| // Tag for this gather operation. | ||
| // Must be unique across operations executing in parallel. | ||
| uint32_t tag; | ||
| }; | ||
|
|
||
| void allgather(const std::shared_ptr<Context>& context, AllgatherOptions& opts); | ||
|
|
||
| } // namespace gloo |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,81 @@ | ||
| /** | ||
| * Copyright (c) 2018-present, Facebook, Inc. | ||
| * All rights reserved. | ||
| * | ||
| * This source code is licensed under the BSD-style license found in the | ||
| * LICENSE file in the root directory of this source tree. An additional grant | ||
| * of patent rights can be found in the PATENTS file in the same directory. | ||
| */ | ||
|
|
||
| #include "gloo/gather.h" | ||
|
|
||
| #include <cstring> | ||
|
|
||
| #include "gloo/common/logging.h" | ||
| #include "gloo/types.h" | ||
|
|
||
| namespace gloo { | ||
|
|
||
| void gather(const std::shared_ptr<Context>& context, GatherOptions& opts) { | ||
| std::unique_ptr<transport::UnboundBuffer> tmpInBuffer; | ||
| std::unique_ptr<transport::UnboundBuffer> tmpOutBuffer; | ||
| transport::UnboundBuffer* in = nullptr; | ||
| transport::UnboundBuffer* out = nullptr; | ||
| const auto slot = Slot::build(kGatherSlotPrefix, opts.tag); | ||
|
|
||
| // Sanity checks | ||
| GLOO_ENFORCE(opts.elementSize > 0); | ||
|
|
||
| // Figure out pointer to input buffer | ||
| if (opts.inBuffer) { | ||
| in = opts.inBuffer.get(); | ||
| } else { | ||
| GLOO_ENFORCE(opts.inPtr != nullptr); | ||
| GLOO_ENFORCE(opts.inElements > 0); | ||
| tmpInBuffer = | ||
| context->createUnboundBuffer(opts.inPtr, opts.inElements * opts.elementSize); | ||
| in = tmpInBuffer.get(); | ||
| } | ||
|
|
||
| if (context->rank == opts.root) { | ||
| const size_t chunkSize = in->size; | ||
|
|
||
| // Figure out pointer to output buffer (only for root rank) | ||
| if (opts.outBuffer) { | ||
| out = opts.outBuffer.get(); | ||
| } else { | ||
| GLOO_ENFORCE(opts.outPtr != nullptr); | ||
| GLOO_ENFORCE(opts.outElements > 0); | ||
| tmpOutBuffer = | ||
| context->createUnboundBuffer(opts.outPtr, opts.outElements * opts.elementSize); | ||
| out = tmpOutBuffer.get(); | ||
| } | ||
|
|
||
| // Ensure the output buffer has the right size. | ||
| GLOO_ENFORCE(in->size * context->size == out->size); | ||
|
|
||
| // Post receive operations from peers into out buffer | ||
| for (size_t i = 0; i < context->size; i++) { | ||
| if (i == context->rank) { | ||
| continue; | ||
| } | ||
| out->recv(i, slot, i * chunkSize, chunkSize); | ||
| } | ||
|
|
||
| // Copy local input to output | ||
| memcpy((char*) out->ptr + (context->rank * chunkSize), in->ptr, chunkSize); | ||
|
|
||
| // Wait for receive operations to complete | ||
| for (size_t i = 0; i < context->size; i++) { | ||
| if (i == context->rank) { | ||
| continue; | ||
| } | ||
| out->waitRecv(); | ||
| } | ||
| } else { | ||
| in->send(opts.root, slot); | ||
| in->waitSend(); | ||
| } | ||
| } | ||
|
|
||
| } // namespace gloo |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| /** | ||
| * Copyright (c) 2018-present, Facebook, Inc. | ||
| * All rights reserved. | ||
| * | ||
| * This source code is licensed under the BSD-style license found in the | ||
| * LICENSE file in the root directory of this source tree. An additional grant | ||
| * of patent rights can be found in the PATENTS file in the same directory. | ||
| */ | ||
|
|
||
| #pragma once | ||
|
|
||
| #include "gloo/context.h" | ||
| #include "gloo/transport/unbound_buffer.h" | ||
|
|
||
| namespace gloo { | ||
|
|
||
| struct GatherOptions { | ||
| // The input and output buffers can either be specified as a unbound | ||
| // buffer (that can be cached and reused by the caller), or a | ||
| // literal pointer and number of elements stored at that pointer. | ||
| std::unique_ptr<transport::UnboundBuffer> inBuffer; | ||
| void* inPtr; | ||
| size_t inElements; | ||
| std::unique_ptr<transport::UnboundBuffer> outBuffer; | ||
| void* outPtr; | ||
| size_t outElements; | ||
|
|
||
| // Number of bytes per element. | ||
| size_t elementSize; | ||
|
|
||
| // Rank of receiving process. | ||
| int root; | ||
|
|
||
| // Tag for this gather operation. | ||
| // Must be unique across operations executing in parallel. | ||
| uint32_t tag; | ||
| }; | ||
|
|
||
| void gather(const std::shared_ptr<Context>& context, GatherOptions& opts); | ||
|
|
||
| } // namespace gloo |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe rephrasing pair missing to something that is easier to be understood