Skip to content

Commit cc52821

Browse files
committed
Fix incorrect outputs and improve performance of commonMemSetLargePattern
Change the implementation of commonMemSetLargePattern to use the largest pattern word size supported by the backend into which the pattern can be divided. That is, use 4-byte words if the pattern size is a multiple of 4, 2-byte words for even sizes and 1-byte words for odd sizes. Keep the idea of filling the entire destination region with the first word, and only start strided fill from the second, but implement it correctly. The previous implementation produced incorrect results for any pattern size which wasn't a multiple of 4. For HIP, the strided fill remains to be always in 1-byte increments because HIP API doesn't provide strided multi-byte memset functions like CUDA does. For CUDA, both the initial memset and the strided ones use the largest possible word size. Add a new optimisation skipping the strided fills completely if the pattern is equal to the first word repeated throughout. This is most commonly the case for a pattern of all zeros, but other cases are possible. This optimisation is implemented in both CUDA and HIP adapters.
1 parent b0a9e2b commit cc52821

File tree

2 files changed

+123
-44
lines changed

2 files changed

+123
-44
lines changed

source/adapters/cuda/enqueue.cpp

Lines changed: 61 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -953,35 +953,71 @@ UR_APIEXPORT ur_result_t UR_APICALL urEnqueueMemBufferCopyRect(
953953

954954
// CUDA has no memset functions that allow setting values more than 4 bytes. UR
955955
// API lets you pass an arbitrary "pattern" to the buffer fill, which can be
956-
// more than 4 bytes. We must break up the pattern into 1 byte values, and set
957-
// the buffer using multiple strided calls. The first 4 patterns are set using
958-
// cuMemsetD32Async then all subsequent 1 byte patterns are set using
959-
// cuMemset2DAsync which is called for each pattern.
956+
// more than 4 bytes. We must break up the pattern into 1, 2 or 4-byte values
957+
// and set the buffer using multiple strided calls.
960958
ur_result_t commonMemSetLargePattern(CUstream Stream, uint32_t PatternSize,
961959
size_t Size, const void *pPattern,
962960
CUdeviceptr Ptr) {
963-
// Calculate the number of patterns, stride, number of times the pattern
964-
// needs to be applied, and the number of times the first 32 bit pattern
965-
// needs to be applied.
966-
auto NumberOfSteps = PatternSize / sizeof(uint8_t);
967-
auto Pitch = NumberOfSteps * sizeof(uint8_t);
968-
auto Height = Size / NumberOfSteps;
969-
auto Count32 = Size / sizeof(uint32_t);
970-
971-
// Get 4-byte chunk of the pattern and call cuMemsetD32Async
972-
auto Value = *(static_cast<const uint32_t *>(pPattern));
973-
UR_CHECK_ERROR(cuMemsetD32Async(Ptr, Value, Count32, Stream));
974-
for (auto step = 4u; step < NumberOfSteps; ++step) {
975-
// take 1 byte of the pattern
976-
Value = *(static_cast<const uint8_t *>(pPattern) + step);
977-
978-
// offset the pointer to the part of the buffer we want to write to
979-
auto OffsetPtr = Ptr + (step * sizeof(uint8_t));
980-
981-
// set all of the pattern chunks
982-
UR_CHECK_ERROR(cuMemsetD2D8Async(OffsetPtr, Pitch, Value, sizeof(uint8_t),
983-
Height, Stream));
961+
// Find the largest supported word size into which the pattern can be divided
962+
auto BackendWordSize = PatternSize % 4u == 0u ? 4u
963+
: PatternSize % 2u == 0u ? 2u
964+
: 1u;
965+
966+
// Calculate the number of words in the pattern, the stride, and the number of
967+
// times the pattern needs to be applied
968+
auto NumberOfSteps = PatternSize / BackendWordSize;
969+
auto Pitch = NumberOfSteps * BackendWordSize;
970+
auto Height = Size / PatternSize;
971+
972+
// Same implementation works for any pattern word type (uint8_t, uint16_t,
973+
// uint32_t)
974+
auto memsetImpl = [BackendWordSize, NumberOfSteps, Pitch, Height, Size, Ptr,
975+
&Stream](const auto *pPatternWords,
976+
auto &&continuousMemset, auto &&stridedMemset) {
977+
// If the pattern is 1 word or the first word is repeated throughout, a fast
978+
// continuous fill can be used without the need for slower strided fills
979+
bool UseOnlyFirstValue{true};
980+
for (auto Step{1u}; (Step < NumberOfSteps) && UseOnlyFirstValue; ++Step) {
981+
if (*(pPatternWords + Step) != *pPatternWords) {
982+
UseOnlyFirstValue = false;
983+
}
984+
}
985+
auto OptimizedNumberOfSteps{UseOnlyFirstValue ? 1u : NumberOfSteps};
986+
987+
// Fill the pattern in steps of BackendWordSize bytes. Use a continuous
988+
// fill in the first step because it's faster than a strided fill. Then,
989+
// overwrite the other values in subsequent steps.
990+
for (auto Step{0u}; Step < OptimizedNumberOfSteps; ++Step) {
991+
if (Step == 0) {
992+
UR_CHECK_ERROR(continuousMemset(Ptr, *(pPatternWords),
993+
Size / BackendWordSize, Stream));
994+
} else {
995+
UR_CHECK_ERROR(stridedMemset(Ptr + Step * BackendWordSize, Pitch,
996+
*(pPatternWords + Step), 1u, Height,
997+
Stream));
998+
}
999+
}
1000+
};
1001+
1002+
// Apply the implementation to the chosen pattern word type
1003+
switch (BackendWordSize) {
1004+
case 4u: {
1005+
memsetImpl(static_cast<const uint32_t *>(pPattern), cuMemsetD32Async,
1006+
cuMemsetD2D32Async);
1007+
break;
1008+
}
1009+
case 2u: {
1010+
memsetImpl(static_cast<const uint16_t *>(pPattern), cuMemsetD16Async,
1011+
cuMemsetD2D16Async);
1012+
break;
1013+
}
1014+
default: {
1015+
memsetImpl(static_cast<const uint8_t *>(pPattern), cuMemsetD8Async,
1016+
cuMemsetD2D8Async);
1017+
break;
9841018
}
1019+
}
1020+
9851021
return UR_RESULT_SUCCESS;
9861022
}
9871023

source/adapters/hip/enqueue.cpp

Lines changed: 62 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -704,25 +704,22 @@ UR_APIEXPORT ur_result_t UR_APICALL urEnqueueMemBufferCopyRect(
704704

705705
static inline void memsetRemainPattern(hipStream_t Stream, uint32_t PatternSize,
706706
size_t Size, const void *pPattern,
707-
hipDeviceptr_t Ptr) {
707+
hipDeviceptr_t Ptr,
708+
uint32_t StartOffset) {
709+
// Calculate the number of times the pattern needs to be applied
710+
auto Height = Size / PatternSize;
708711

709-
// Calculate the number of patterns, stride and the number of times the
710-
// pattern needs to be applied.
711-
auto NumberOfSteps = PatternSize / sizeof(uint8_t);
712-
auto Pitch = NumberOfSteps * sizeof(uint8_t);
713-
auto Height = Size / NumberOfSteps;
714-
715-
for (auto step = 4u; step < NumberOfSteps; ++step) {
712+
for (auto step = StartOffset; step < PatternSize; ++step) {
716713
// take 1 byte of the pattern
717714
auto Value = *(static_cast<const uint8_t *>(pPattern) + step);
718715

719716
// offset the pointer to the part of the buffer we want to write to
720-
auto OffsetPtr = reinterpret_cast<void *>(reinterpret_cast<uint8_t *>(Ptr) +
721-
(step * sizeof(uint8_t)));
717+
auto OffsetPtr =
718+
reinterpret_cast<void *>(reinterpret_cast<uint8_t *>(Ptr) + step);
722719

723720
// set all of the pattern chunks
724-
UR_CHECK_ERROR(hipMemset2DAsync(OffsetPtr, Pitch, Value, sizeof(uint8_t),
725-
Height, Stream));
721+
UR_CHECK_ERROR(
722+
hipMemset2DAsync(OffsetPtr, PatternSize, Value, 1u, Height, Stream));
726723
}
727724
}
728725

@@ -735,11 +732,55 @@ static inline void memsetRemainPattern(hipStream_t Stream, uint32_t PatternSize,
735732
ur_result_t commonMemSetLargePattern(hipStream_t Stream, uint32_t PatternSize,
736733
size_t Size, const void *pPattern,
737734
hipDeviceptr_t Ptr) {
735+
// Find the largest supported word size into which the pattern can be divided
736+
auto BackendWordSize = PatternSize % 4u == 0u ? 4u
737+
: PatternSize % 2u == 0u ? 2u
738+
: 1u;
739+
740+
// Calculate the number of patterns
741+
auto NumberOfSteps = PatternSize / BackendWordSize;
742+
743+
// If the pattern is 1 word or the first word is repeated throughout, a fast
744+
// continuous fill can be used without the need for slower strided fills
745+
bool UseOnlyFirstValue{true};
746+
auto checkIfFirstWordRepeats = [&UseOnlyFirstValue,
747+
NumberOfSteps](const auto *pPatternWords) {
748+
for (auto Step{1u}; (Step < NumberOfSteps) && UseOnlyFirstValue; ++Step) {
749+
if (*(pPatternWords + Step) != *pPatternWords) {
750+
UseOnlyFirstValue = false;
751+
}
752+
}
753+
};
738754

739-
// Get 4-byte chunk of the pattern and call hipMemsetD32Async
740-
auto Count32 = Size / sizeof(uint32_t);
741-
auto Value = *(static_cast<const uint32_t *>(pPattern));
742-
UR_CHECK_ERROR(hipMemsetD32Async(Ptr, Value, Count32, Stream));
755+
// Use a continuous fill for the first word in the pattern because it's faster
756+
// than a strided fill. Then, overwrite the other values in subsequent steps.
757+
switch (BackendWordSize) {
758+
case 4u: {
759+
auto *pPatternWords = static_cast<const uint32_t *>(pPattern);
760+
checkIfFirstWordRepeats(pPatternWords);
761+
UR_CHECK_ERROR(
762+
hipMemsetD32Async(Ptr, *pPatternWords, Size / BackendWordSize, Stream));
763+
break;
764+
}
765+
case 2u: {
766+
auto *pPatternWords = static_cast<const uint16_t *>(pPattern);
767+
checkIfFirstWordRepeats(pPatternWords);
768+
UR_CHECK_ERROR(
769+
hipMemsetD16Async(Ptr, *pPatternWords, Size / BackendWordSize, Stream));
770+
break;
771+
}
772+
default: {
773+
auto *pPatternWords = static_cast<const uint8_t *>(pPattern);
774+
checkIfFirstWordRepeats(pPatternWords);
775+
UR_CHECK_ERROR(
776+
hipMemsetD8Async(Ptr, *pPatternWords, Size / BackendWordSize, Stream));
777+
break;
778+
}
779+
}
780+
781+
if (UseOnlyFirstValue) {
782+
return UR_RESULT_SUCCESS;
783+
}
743784

744785
// There is a bug in ROCm prior to 6.0.0 version which causes hipMemset2D
745786
// to behave incorrectly when acting on host pinned memory.
@@ -753,7 +794,7 @@ ur_result_t commonMemSetLargePattern(hipStream_t Stream, uint32_t PatternSize,
753794
// we need to check that isManaged attribute is false.
754795
if (ptrAttribs.hostPointer && !ptrAttribs.isManaged) {
755796
const auto NumOfCopySteps = Size / PatternSize;
756-
const auto Offset = sizeof(uint32_t);
797+
const auto Offset = BackendWordSize;
757798
const auto LeftPatternSize = PatternSize - Offset;
758799
const auto OffsetPatternPtr = reinterpret_cast<const void *>(
759800
reinterpret_cast<const uint8_t *>(pPattern) + Offset);
@@ -768,10 +809,12 @@ ur_result_t commonMemSetLargePattern(hipStream_t Stream, uint32_t PatternSize,
768809
Stream));
769810
}
770811
} else {
771-
memsetRemainPattern(Stream, PatternSize, Size, pPattern, Ptr);
812+
memsetRemainPattern(Stream, PatternSize, Size, pPattern, Ptr,
813+
BackendWordSize);
772814
}
773815
#else
774-
memsetRemainPattern(Stream, PatternSize, Size, pPattern, Ptr);
816+
memsetRemainPattern(Stream, PatternSize, Size, pPattern, Ptr,
817+
BackendWordSize);
775818
#endif
776819
return UR_RESULT_SUCCESS;
777820
}

0 commit comments

Comments
 (0)