Skip to content
Open
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
11 changes: 10 additions & 1 deletion src/ompi/impl/all_reduce.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#ifndef INFINI_CCL_OMPI_IMPL_ALL_REDUCE_H_
#define INFINI_CCL_OMPI_IMPL_ALL_REDUCE_H_

#include <type_traits>

#include "base/all_reduce.h"
#include "communicator.h"
#include "dispatcher.h"
Expand Down Expand Up @@ -66,7 +68,14 @@ class AllReduceImpl<BackendType::kOmpi, device_type> {
for (size_t i = 0; i < count; ++i) {
// TODO(lzm): should later use the unified `Cast` function instead of
// static_cast to support CPU custom types.
typed_buf[i] *= static_cast<T>(scale);
if constexpr (std::is_integral_v<T>) {
// Scale in floating point first; casting `scale` to an integer
// type would truncate it to `0` and zero out the result.
typed_buf[i] =
static_cast<T>(static_cast<double>(typed_buf[i]) * scale);
} else {
typed_buf[i] *= static_cast<T>(scale);
}
}
});
}
Expand Down
10 changes: 9 additions & 1 deletion src/ompi/impl/reduce_scatter.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#define INFINI_CCL_OMPI_IMPL_REDUCE_SCATTER_H_

#include <limits>
#include <type_traits>

#include "base/reduce_scatter.h"
#include "communicator.h"
Expand Down Expand Up @@ -79,7 +80,14 @@ class ReduceScatterImpl<BackendType::kOmpi, device_type> {
for (size_t i = 0; i < recv_count; ++i) {
// TODO(lzm): should later use the unified `Cast` function instead of
// static_cast to support CPU custom types.
typed_buf[i] *= static_cast<T>(scale);
if constexpr (std::is_integral_v<T>) {
// Scale in floating point first; casting `scale` to an integer
// type would truncate it to `0` and zero out the result.
typed_buf[i] =
static_cast<T>(static_cast<double>(typed_buf[i]) * scale);
} else {
typed_buf[i] *= static_cast<T>(scale);
}
}
});
}
Expand Down
Loading