Skip to content
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

Feature/probinso/gpu merge sort #286

Merged
merged 3 commits into from
Sep 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
106 changes: 76 additions & 30 deletions src/care/KeyValueSorter_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,10 @@ CARE_INLINE void sortKeyValueArrays(host_device_ptr<KeyT> & keys,

auto * rawKeyResult = keyGetter.getRawArrayData(keyResult);
auto * rawValueResult = valueGetter.getRawArrayData(valueResult);

auto custom_comparator = [] CARE_HOST_DEVICE (decltype(*rawKeyData) lhs, decltype(*rawKeyData) rhs) {
return lhs < rhs;
};


// Get the temp storage length
Expand All @@ -94,17 +98,33 @@ CARE_INLINE void sortKeyValueArrays(host_device_ptr<KeyT> & keys,
// When called with a nullptr for temp storage, this returns how much
// temp storage should be allocated.
if (len > 0) {
if constexpr (std::is_arithmetic_v<decltype(*rawKeyData)>) {
#if defined(__CUDACC__)
cub::DeviceRadixSort::SortPairs((void *)d_temp_storage, temp_storage_bytes,
rawKeyData, rawKeyResult,
rawValueData, rawValueResult,
len);
#elif defined(__HIPCC__)
hipcub::DeviceRadixSort::SortPairs((void *)d_temp_storage, temp_storage_bytes,
rawKeyData, rawKeyResult,
rawValueData, rawValueResult,
len);
#endif
}
else {
#if defined(__CUDACC__)
cub::DeviceRadixSort::SortPairs((void *)d_temp_storage, temp_storage_bytes,
rawKeyData, rawKeyResult,
rawValueData, rawValueResult,
len);
cub::DeviceMergeSort::StableSortPairs((void *)d_temp_storage, temp_storage_bytes,
rawKeyData,
rawValueData,
len, custom_comparator);
#elif defined(__HIPCC__)
hipcub::DeviceRadixSort::SortPairs((void *)d_temp_storage, temp_storage_bytes,
rawKeyData, rawKeyResult,
rawValueData, rawValueResult,
len);
hipcub::DeviceMergeSort::StableSortPairs((void *)d_temp_storage, temp_storage_bytes,
rawKeyData,
rawValueData,
len, custom_comparator);
#endif

}
}

// Allocate the temp storage and get raw data to pass to cub
Expand All @@ -118,42 +138,68 @@ CARE_INLINE void sortKeyValueArrays(host_device_ptr<KeyT> & keys,
#if defined(CHAI_THIN_GPU_ALLOCATE)
chai::ArrayManager::getInstance()->setExecutionSpace(chai::GPU);
#endif


if constexpr (std::is_arithmetic_v<KeyT>) {
#if defined(__CUDACC__)
cub::DeviceRadixSort::SortPairs((void *)d_temp_storage, temp_storage_bytes,
rawKeyData, rawKeyResult,
rawValueData, rawValueResult,
len);
#elif defined(__HIPCC__)
hipcub::DeviceRadixSort::SortPairs((void *)d_temp_storage, temp_storage_bytes,
rawKeyData, rawKeyResult,
rawValueData, rawValueResult,
len);
#endif
} else {
#if defined(__CUDACC__)
cub::DeviceRadixSort::SortPairs((void *)d_temp_storage, temp_storage_bytes,
rawKeyData, rawKeyResult,
rawValueData, rawValueResult,
len);
cub::DeviceMergeSort::StableSortPairs((void *)d_temp_storage, temp_storage_bytes,
rawKeyData,
rawValueData,
len,
custom_comparator);
#elif defined(__HIPCC__)
hipcub::DeviceRadixSort::SortPairs((void *)d_temp_storage, temp_storage_bytes,
rawKeyData, rawKeyResult,
rawValueData, rawValueResult,
len);
hipcub::DeviceMergeSort::StableSortPairs((void *)d_temp_storage, temp_storage_bytes,
rawKeyData,
rawValueData,
len,
custom_comparator);
#endif

}

#if defined(CHAI_THIN_GPU_ALLOCATE)
chai::ArrayManager::getInstance()->setExecutionSpace(chai::NONE);
#endif

tmpManaged.free();
}

// Get the result
if (_noCopy) {
if (len > 0) {
keys.free();
values.free();
}
if constexpr (std::is_arithmetic_v<KeyT>) {
// Get the result
if (_noCopy) {
if (len > 0) {
keys.free();
values.free();
}

keys = keyResult;
values = valueResult;
keys = keyResult;
values = valueResult;
}
else {
CARE_STREAM_LOOP(i, 0, len) {
keys[i+start] = keyResult[i];
values[i+start] = valueResult[i];
} CARE_STREAM_LOOP_END

if (len > 0) {
keyResult.free();
valueResult.free();
}
}
}
else {
CARE_STREAM_LOOP(i, 0, len) {
keys[i+start] = keyResult[i];
values[i+start] = valueResult[i];
} CARE_STREAM_LOOP_END

// merge sort did an inplace sort, so the answer is already in keys and Values
if (len > 0) {
keyResult.free();
valueResult.free();
Expand Down
63 changes: 60 additions & 3 deletions src/care/algorithm_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -675,8 +675,13 @@ CARE_INLINE int uniqArray(RAJA::seq_exec exec, care::host_device_ptr<T, Accessor

template <typename T, template <class A> class Accessor>
CARE_INLINE void sortArray(RAJADeviceExec, care::host_device_ptr<T, Accessor> & Array, size_t len, int start, bool noCopy)
{
radixSortArray(Array, len, start, noCopy);
{
if constexpr( std::is_arithmetic_v<typename CHAIDataGetter<T, RAJADeviceExec>::raw_type>) {
radixSortArray(Array, len, start, noCopy);
}
else {
mergeSortArray(Array, len, start, noCopy);
}
}

template <typename T, template <class A> class Accessor>
Expand All @@ -688,7 +693,7 @@ CARE_INLINE void sortArray(RAJADeviceExec, care::host_device_ptr<T, Accessor> &
/************************************************************************
* Function : radixSortArray
* Author(s) : Peter Robinson
* Purpose : ManagedArray API to cub::DeviceRadixSort::SortKeys.
* Purpose : ManagedArray API to [hip]cub::DeviceRadixSort::SortKeys
************************************************************************/
template <typename T, template <class A> class Accessor>
CARE_INLINE void radixSortArray(care::host_device_ptr<T, Accessor> & Array, size_t len, int start, bool noCopy)
Expand Down Expand Up @@ -747,6 +752,58 @@ CARE_INLINE void radixSortArray(care::host_device_ptr<T, Accessor> & Array, size
tmpManaged.free();
}
}

/************************************************************************
* Function : mergeSortArray
* Author(s) : Peter Robinson
* Purpose : ManagedArray API to [hip]cub::DeviceMergeSort::StableSortKeys.
************************************************************************/
template <typename T, template <class A> class Accessor>
CARE_INLINE void mergeSortArray(care::host_device_ptr<T, Accessor> & Array, size_t len, int start, bool noCopy)
{
CHAIDataGetter<T, RAJADeviceExec> getter {};
CHAIDataGetter<char, RAJADeviceExec> charGetter {};
const auto * rawData = getter.getConstRawArrayData(Array) + start;
// get the temp storage length
char * d_temp_storage = nullptr;
size_t temp_storage_bytes = 0;
auto custom_comparator = [] CARE_HOST_DEVICE (const T & lhs, const T & rhs) {
return lhs < rhs;
};
if (len > 0) {
#if defined(__CUDACC__)
cub::DeviceMergeSort::StableSortKeys((void *)d_temp_storage, temp_storage_bytes, rawData, len, custom_comparator);
#elif defined(__HIPCC__)
hipcub::DeviceMergeSort::StableSortKeys((void *)d_temp_storage, temp_storage_bytes, rawData, len, custom_comparator);
#endif
}
// allocate the temp storage

care::host_device_ptr<char> tmpManaged(temp_storage_bytes, "merge_sort_tmpManaged");
d_temp_storage = charGetter.getRawArrayData(tmpManaged);

// do the sort
if (len > 0) {
#if defined(CHAI_THIN_GPU_ALLOCATE)
chai::ArrayManager::getInstance()->setExecutionSpace(chai::GPU);
#endif

#if defined(__CUDACC__)
cub::DeviceMergeSort::StableSortKeys((void *)d_temp_storage, temp_storage_bytes, rawData, len, custom_comparator);
#elif defined(__HIPCC__)
hipcub::DeviceMergeSort::StableSortKeys((void *)d_temp_storage, temp_storage_bytes, rawData, len, custom_comparator);
#endif

#if defined(CHAI_THIN_GPU_ALLOCATE)
chai::ArrayManager::getInstance()->setExecutionSpace(chai::NONE);
#endif
}

// cleanup
if (len > 0) {
tmpManaged.free();
}
}
#else // defined(CARE_GPUCC)

// TODO openMP parallel implementation
Expand Down
Loading