mirror of
https://gitlab.com/libeigen/eigen.git
synced 2025-08-12 11:49:02 +08:00
Use scalar_sum_op and scalar_quotient_op instead of operator+ and operator/ in MeanReducer.
Improves support for std::complex types when compiling for CUDA. Expands on e2e9cdd16970914cf0a892fea5e7c4402b3ede41 and 2bda1b0d93fb627d0c500ec48b20302d44c32cb7 .
This commit is contained in:
parent
d9084ac8e1
commit
949a2da38c
@ -166,7 +166,8 @@ template <typename T> struct MeanReducer
|
|||||||
return pset1<Packet>(initialize());
|
return pset1<Packet>(initialize());
|
||||||
}
|
}
|
||||||
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE T finalize(const T accum) const {
|
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE T finalize(const T accum) const {
|
||||||
return accum / scalarCount_;
|
internal::scalar_quotient_op<T> quotient_op;
|
||||||
|
return quotient_op(accum, T(scalarCount_));
|
||||||
}
|
}
|
||||||
template <typename Packet>
|
template <typename Packet>
|
||||||
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Packet finalizePacket(const Packet& vaccum) const {
|
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Packet finalizePacket(const Packet& vaccum) const {
|
||||||
@ -175,7 +176,10 @@ template <typename T> struct MeanReducer
|
|||||||
template <typename Packet>
|
template <typename Packet>
|
||||||
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE T finalizeBoth(const T saccum, const Packet& vaccum) const {
|
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE T finalizeBoth(const T saccum, const Packet& vaccum) const {
|
||||||
internal::scalar_sum_op<T> sum_op;
|
internal::scalar_sum_op<T> sum_op;
|
||||||
return sum_op(saccum, predux(vaccum)) / (scalarCount_ + packetCount_ * unpacket_traits<Packet>::size);
|
internal::scalar_quotient_op<T> quotient_op;
|
||||||
|
return quotient_op(
|
||||||
|
sum_op(saccum, predux(vaccum)),
|
||||||
|
T(scalarCount_ + packetCount_ * unpacket_traits<Packet>::size));
|
||||||
}
|
}
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
|
@ -107,6 +107,41 @@ static void test_cuda_sum_reductions() {
|
|||||||
gpu_device.deallocate(gpu_out_ptr);
|
gpu_device.deallocate(gpu_out_ptr);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void test_cuda_mean_reductions() {
|
||||||
|
|
||||||
|
Eigen::CudaStreamDevice stream;
|
||||||
|
Eigen::GpuDevice gpu_device(&stream);
|
||||||
|
|
||||||
|
const int num_rows = internal::random<int>(1024, 5*1024);
|
||||||
|
const int num_cols = internal::random<int>(1024, 5*1024);
|
||||||
|
|
||||||
|
Tensor<std::complex<float>, 2> in(num_rows, num_cols);
|
||||||
|
in.setRandom();
|
||||||
|
|
||||||
|
Tensor<std::complex<float>, 0> full_redux;
|
||||||
|
full_redux = in.mean();
|
||||||
|
|
||||||
|
std::size_t in_bytes = in.size() * sizeof(std::complex<float>);
|
||||||
|
std::size_t out_bytes = full_redux.size() * sizeof(std::complex<float>);
|
||||||
|
std::complex<float>* gpu_in_ptr = static_cast<std::complex<float>*>(gpu_device.allocate(in_bytes));
|
||||||
|
std::complex<float>* gpu_out_ptr = static_cast<std::complex<float>*>(gpu_device.allocate(out_bytes));
|
||||||
|
gpu_device.memcpyHostToDevice(gpu_in_ptr, in.data(), in_bytes);
|
||||||
|
|
||||||
|
TensorMap<Tensor<std::complex<float>, 2> > in_gpu(gpu_in_ptr, num_rows, num_cols);
|
||||||
|
TensorMap<Tensor<std::complex<float>, 0> > out_gpu(gpu_out_ptr);
|
||||||
|
|
||||||
|
out_gpu.device(gpu_device) = in_gpu.mean();
|
||||||
|
|
||||||
|
Tensor<std::complex<float>, 0> full_redux_gpu;
|
||||||
|
gpu_device.memcpyDeviceToHost(full_redux_gpu.data(), gpu_out_ptr, out_bytes);
|
||||||
|
gpu_device.synchronize();
|
||||||
|
|
||||||
|
// Check that the CPU and GPU reductions return the same result.
|
||||||
|
VERIFY_IS_APPROX(full_redux(), full_redux_gpu());
|
||||||
|
|
||||||
|
gpu_device.deallocate(gpu_in_ptr);
|
||||||
|
gpu_device.deallocate(gpu_out_ptr);
|
||||||
|
}
|
||||||
|
|
||||||
static void test_cuda_product_reductions() {
|
static void test_cuda_product_reductions() {
|
||||||
|
|
||||||
@ -149,5 +184,6 @@ void test_cxx11_tensor_complex()
|
|||||||
{
|
{
|
||||||
CALL_SUBTEST(test_cuda_nullary());
|
CALL_SUBTEST(test_cuda_nullary());
|
||||||
CALL_SUBTEST(test_cuda_sum_reductions());
|
CALL_SUBTEST(test_cuda_sum_reductions());
|
||||||
|
CALL_SUBTEST(test_cuda_mean_reductions());
|
||||||
CALL_SUBTEST(test_cuda_product_reductions());
|
CALL_SUBTEST(test_cuda_product_reductions());
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user