-
Notifications
You must be signed in to change notification settings - Fork 450
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
[VL] Row based sort follow-up #6579
Merged
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
66165e3
add shuffle write wall time metrics
marin-ma 04f19bf
radix sort
marin-ma e197971
partial revert
marin-ma 71e5da8
update metrics
marin-ma b77599b
address comments & fix
marin-ma 94b32cb
fix
marin-ma cd6eb1d
fix
marin-ma 1a30342
fix
marin-ma 8b03f02
fake array_ allocation
marin-ma 29b1acb
use raw buffer for array_
marin-ma 18c4e54
use spark conf for radix sort and buffer size
marin-ma d2d08f9
fix qsort
marin-ma a9d3457
prealloc sortedBuffer
marin-ma 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 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 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 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 was deleted.
Oops, something went wrong.
This file contains 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 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,157 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
#include <algorithm> | ||
#include <cassert> | ||
#include <iostream> | ||
#include <vector> | ||
|
||
namespace gluten { | ||
|
||
template <typename Element> | ||
class RadixSort { | ||
public: | ||
/** | ||
* Sorts a given array of longs using least-significant-digit radix sort. This routine assumes | ||
* you have extra space at the end of the array at least equal to the number of records. The | ||
* sort is destructive and may relocate the data positioned within the array. | ||
* | ||
* @param array array of long elements followed by at least that many empty slots. | ||
* @param numRecords number of data records in the array. | ||
* @param startByteIndex the first byte (in range [0, 7]) to sort each long by, counting from the | ||
* least significant byte. | ||
* @param endByteIndex the last byte (in range [0, 7]) to sort each long by, counting from the | ||
* least significant byte. Must be greater than startByteIndex. | ||
* | ||
* @return The starting index of the sorted data within the given array. We return this instead | ||
* of always copying the data back to position zero for efficiency. | ||
*/ | ||
static int32_t sort(Element* array, size_t size, int64_t numRecords, int32_t startByteIndex, int32_t endByteIndex) { | ||
assert(startByteIndex >= 0 && "startByteIndex should >= 0"); | ||
assert(endByteIndex <= 7 && "endByteIndex should <= 7"); | ||
assert(endByteIndex > startByteIndex); | ||
assert(numRecords * 2 <= size); | ||
|
||
int64_t inIndex = 0; | ||
int64_t outIndex = numRecords; | ||
|
||
if (numRecords > 0) { | ||
auto counts = getCounts(array, numRecords, startByteIndex, endByteIndex); | ||
|
||
for (auto i = startByteIndex; i <= endByteIndex; i++) { | ||
if (!counts[i].empty()) { | ||
sortAtByte(array, numRecords, counts[i], i, inIndex, outIndex); | ||
std::swap(inIndex, outIndex); | ||
} | ||
} | ||
} | ||
|
||
return static_cast<int32_t>(inIndex); | ||
} | ||
|
||
private: | ||
/** | ||
* Performs a partial sort by copying data into destination offsets for each byte value at the | ||
* specified byte offset. | ||
* | ||
* @param array array to partially sort. | ||
* @param numRecords number of data records in the array. | ||
* @param counts counts for each byte value. This routine destructively modifies this array. | ||
* @param byteIdx the byte in a long to sort at, counting from the least significant byte. | ||
* @param inIndex the starting index in the array where input data is located. | ||
* @param outIndex the starting index where sorted output data should be written. | ||
*/ | ||
static void sortAtByte( | ||
Element* array, | ||
int64_t numRecords, | ||
std::vector<int64_t>& counts, | ||
int32_t byteIdx, | ||
int64_t inIndex, | ||
int64_t outIndex) { | ||
assert(counts.size() == 256); | ||
|
||
auto offsets = transformCountsToOffsets(counts, outIndex); | ||
|
||
for (auto offset = inIndex; offset < inIndex + numRecords; ++offset) { | ||
auto bucket = (array[offset] >> (byteIdx * 8)) & 0xff; | ||
array[offsets[bucket]++] = array[offset]; | ||
} | ||
} | ||
|
||
/** | ||
* Computes a value histogram for each byte in the given array. | ||
* | ||
* @param array array to count records in. | ||
* @param numRecords number of data records in the array. | ||
* @param startByteIndex the first byte to compute counts for (the prior are skipped). | ||
* @param endByteIndex the last byte to compute counts for. | ||
* | ||
* @return a vector of eight 256-element count arrays, one for each byte starting from the least | ||
* significant byte. If the byte does not need sorting the vector entry will be empty. | ||
*/ | ||
static std::vector<std::vector<int64_t>> | ||
getCounts(Element* array, int64_t numRecords, int32_t startByteIndex, int32_t endByteIndex) { | ||
std::vector<std::vector<int64_t>> counts; | ||
counts.resize(8); | ||
|
||
// Optimization: do a fast pre-pass to determine which byte indices we can skip for sorting. | ||
// If all the byte values at a particular index are the same we don't need to count it. | ||
int64_t bitwiseMax = 0; | ||
int64_t bitwiseMin = -1L; | ||
for (auto offset = 0; offset < numRecords; ++offset) { | ||
auto value = array[offset]; | ||
bitwiseMax |= value; | ||
bitwiseMin &= value; | ||
} | ||
auto bitsChanged = bitwiseMin ^ bitwiseMax; | ||
|
||
// Compute counts for each byte index. | ||
for (auto i = startByteIndex; i <= endByteIndex; i++) { | ||
if (((bitsChanged >> (i * 8)) & 0xff) != 0) { | ||
counts[i].resize(256); | ||
for (auto offset = 0; offset < numRecords; ++offset) { | ||
counts[i][(array[offset] >> (i * 8)) & 0xff]++; | ||
} | ||
} | ||
} | ||
|
||
return counts; | ||
} | ||
|
||
/** | ||
* Transforms counts into the proper output offsets for the sort type. | ||
* | ||
* @param counts counts for each byte value. This routine destructively modifies this vector. | ||
* @param numRecords number of data records in the original data array. | ||
* @param outputOffset output offset in bytes from the base array object. | ||
* | ||
* @return the input counts vector. | ||
*/ | ||
static std::vector<int64_t>& transformCountsToOffsets(std::vector<int64_t>& counts, int64_t outputOffset) { | ||
assert(counts.size() == 256); | ||
|
||
int64_t pos = outputOffset; | ||
for (auto i = 0; i < 256; i++) { | ||
auto tmp = counts[i & 0xff]; | ||
counts[i & 0xff] = pos; | ||
pos += tmp; | ||
} | ||
|
||
return counts; | ||
} | ||
}; | ||
|
||
} // namespace gluten |
This file contains 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 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.
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.
Is the file a 1:1 porting from vanilla Spark's Java code? If so can we add some comments somewhere in code to note that?