mirror of
https://gitlab.com/libeigen/eigen.git
synced 2026-04-10 11:34:33 +08:00
Pull the latest updates from trunk
This commit is contained in:
@@ -203,7 +203,7 @@ if(CUDA_FOUND AND EIGEN_TEST_CUDA)
|
||||
message(STATUS "Flags used to compile cuda code: " ${CMAKE_CXX_FLAGS})
|
||||
|
||||
if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang")
|
||||
set(CUDA_NVCC_FLAGS "-ccbin /usr/bin/clang" CACHE STRING "nvcc flags" FORCE)
|
||||
set(CUDA_NVCC_FLAGS "-ccbin ${CMAKE_C_COMPILER}" CACHE STRING "nvcc flags" FORCE)
|
||||
endif()
|
||||
if(EIGEN_TEST_CUDA_CLANG)
|
||||
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 --cuda-gpu-arch=sm_${EIGEN_CUDA_COMPUTE_ARCH}")
|
||||
@@ -226,6 +226,7 @@ if(CUDA_FOUND AND EIGEN_TEST_CUDA)
|
||||
set(EIGEN_ADD_TEST_FILENAME_EXTENSION "cu")
|
||||
|
||||
ei_add_test(cxx11_tensor_complex_cuda)
|
||||
ei_add_test(cxx11_tensor_complex_cwise_ops_cuda)
|
||||
ei_add_test(cxx11_tensor_reduction_cuda)
|
||||
ei_add_test(cxx11_tensor_argmax_cuda)
|
||||
ei_add_test(cxx11_tensor_cast_float16_cuda)
|
||||
|
||||
@@ -105,6 +105,89 @@ struct TestFunc1
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
#if EIGEN_HAS_VARIADIC_TEMPLATES
|
||||
/* Test functor for the C++11 features. */
|
||||
template <typename Scalar>
|
||||
struct integratorFunctor
|
||||
{
|
||||
typedef Matrix<Scalar, 2, 1> InputType;
|
||||
typedef Matrix<Scalar, 2, 1> ValueType;
|
||||
|
||||
/*
|
||||
* Implementation starts here.
|
||||
*/
|
||||
integratorFunctor(const Scalar gain) : _gain(gain) {}
|
||||
integratorFunctor(const integratorFunctor& f) : _gain(f._gain) {}
|
||||
const Scalar _gain;
|
||||
|
||||
template <typename T1, typename T2>
|
||||
void operator() (const T1 &input, T2 *output, const Scalar dt) const
|
||||
{
|
||||
T2 &o = *output;
|
||||
|
||||
/* Integrator to test the AD. */
|
||||
o[0] = input[0] + input[1] * dt * _gain;
|
||||
o[1] = input[1] * _gain;
|
||||
}
|
||||
|
||||
/* Only needed for the test */
|
||||
template <typename T1, typename T2, typename T3>
|
||||
void operator() (const T1 &input, T2 *output, T3 *jacobian, const Scalar dt) const
|
||||
{
|
||||
T2 &o = *output;
|
||||
|
||||
/* Integrator to test the AD. */
|
||||
o[0] = input[0] + input[1] * dt * _gain;
|
||||
o[1] = input[1] * _gain;
|
||||
|
||||
if (jacobian)
|
||||
{
|
||||
T3 &j = *jacobian;
|
||||
|
||||
j(0, 0) = 1;
|
||||
j(0, 1) = dt * _gain;
|
||||
j(1, 0) = 0;
|
||||
j(1, 1) = _gain;
|
||||
}
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
template<typename Func> void forward_jacobian_cpp11(const Func& f)
|
||||
{
|
||||
typedef typename Func::ValueType::Scalar Scalar;
|
||||
typedef typename Func::ValueType ValueType;
|
||||
typedef typename Func::InputType InputType;
|
||||
typedef typename AutoDiffJacobian<Func>::JacobianType JacobianType;
|
||||
|
||||
InputType x = InputType::Random(InputType::RowsAtCompileTime);
|
||||
ValueType y, yref;
|
||||
JacobianType j, jref;
|
||||
|
||||
const Scalar dt = internal::random<double>();
|
||||
|
||||
jref.setZero();
|
||||
yref.setZero();
|
||||
f(x, &yref, &jref, dt);
|
||||
|
||||
//std::cerr << "y, yref, jref: " << "\n";
|
||||
//std::cerr << y.transpose() << "\n\n";
|
||||
//std::cerr << yref << "\n\n";
|
||||
//std::cerr << jref << "\n\n";
|
||||
|
||||
AutoDiffJacobian<Func> autoj(f);
|
||||
autoj(x, &y, &j, dt);
|
||||
|
||||
//std::cerr << "y j (via autodiff): " << "\n";
|
||||
//std::cerr << y.transpose() << "\n\n";
|
||||
//std::cerr << j << "\n\n";
|
||||
|
||||
VERIFY_IS_APPROX(y, yref);
|
||||
VERIFY_IS_APPROX(j, jref);
|
||||
}
|
||||
#endif
|
||||
|
||||
template<typename Func> void forward_jacobian(const Func& f)
|
||||
{
|
||||
typename Func::InputType x = Func::InputType::Random(f.inputs());
|
||||
@@ -128,7 +211,6 @@ template<typename Func> void forward_jacobian(const Func& f)
|
||||
VERIFY_IS_APPROX(j, jref);
|
||||
}
|
||||
|
||||
|
||||
// TODO also check actual derivatives!
|
||||
template <int>
|
||||
void test_autodiff_scalar()
|
||||
@@ -141,6 +223,7 @@ void test_autodiff_scalar()
|
||||
VERIFY_IS_APPROX(res.value(), foo(p.x(),p.y()));
|
||||
}
|
||||
|
||||
|
||||
// TODO also check actual derivatives!
|
||||
template <int>
|
||||
void test_autodiff_vector()
|
||||
@@ -151,7 +234,7 @@ void test_autodiff_vector()
|
||||
VectorAD ap = p.cast<AD>();
|
||||
ap.x().derivatives() = Vector2f::UnitX();
|
||||
ap.y().derivatives() = Vector2f::UnitY();
|
||||
|
||||
|
||||
AD res = foo<VectorAD>(ap);
|
||||
VERIFY_IS_APPROX(res.value(), foo(p));
|
||||
}
|
||||
@@ -164,6 +247,9 @@ void test_autodiff_jacobian()
|
||||
CALL_SUBTEST(( forward_jacobian(TestFunc1<double,3,2>()) ));
|
||||
CALL_SUBTEST(( forward_jacobian(TestFunc1<double,3,3>()) ));
|
||||
CALL_SUBTEST(( forward_jacobian(TestFunc1<double>(3,3)) ));
|
||||
#if EIGEN_HAS_VARIADIC_TEMPLATES
|
||||
CALL_SUBTEST(( forward_jacobian_cpp11(integratorFunctor<double>(10)) ));
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -71,8 +71,45 @@ void test_cuda_nullary() {
|
||||
}
|
||||
|
||||
|
||||
static void test_cuda_sum_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.sum();
|
||||
|
||||
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.sum();
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
|
||||
void test_cxx11_tensor_complex()
|
||||
{
|
||||
CALL_SUBTEST(test_cuda_nullary());
|
||||
CALL_SUBTEST(test_cuda_sum_reductions());
|
||||
}
|
||||
|
||||
97
unsupported/test/cxx11_tensor_complex_cwise_ops_cuda.cu
Normal file
97
unsupported/test/cxx11_tensor_complex_cwise_ops_cuda.cu
Normal file
@@ -0,0 +1,97 @@
|
||||
// This file is part of Eigen, a lightweight C++ template library
|
||||
// for linear algebra.
|
||||
//
|
||||
// Copyright (C) 2016 Benoit Steiner <benoit.steiner.goog@gmail.com>
|
||||
//
|
||||
// This Source Code Form is subject to the terms of the Mozilla
|
||||
// Public License v. 2.0. If a copy of the MPL was not distributed
|
||||
// with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
||||
|
||||
#define EIGEN_TEST_NO_LONGDOUBLE
|
||||
#define EIGEN_TEST_FUNC cxx11_tensor_complex_cwise_ops
|
||||
#define EIGEN_USE_GPU
|
||||
|
||||
#if defined __CUDACC_VER__ && __CUDACC_VER__ >= 70500
|
||||
#include <cuda_fp16.h>
|
||||
#endif
|
||||
#include "main.h"
|
||||
#include <unsupported/Eigen/CXX11/Tensor>
|
||||
|
||||
using Eigen::Tensor;
|
||||
|
||||
template<typename T>
|
||||
void test_cuda_complex_cwise_ops() {
|
||||
const int kNumItems = 2;
|
||||
std::size_t complex_bytes = kNumItems * sizeof(std::complex<T>);
|
||||
|
||||
std::complex<T>* d_in1;
|
||||
std::complex<T>* d_in2;
|
||||
std::complex<T>* d_out;
|
||||
cudaMalloc((void**)(&d_in1), complex_bytes);
|
||||
cudaMalloc((void**)(&d_in2), complex_bytes);
|
||||
cudaMalloc((void**)(&d_out), complex_bytes);
|
||||
|
||||
Eigen::CudaStreamDevice stream;
|
||||
Eigen::GpuDevice gpu_device(&stream);
|
||||
|
||||
Eigen::TensorMap<Eigen::Tensor<std::complex<T>, 1, 0, int>, Eigen::Aligned> gpu_in1(
|
||||
d_in1, kNumItems);
|
||||
Eigen::TensorMap<Eigen::Tensor<std::complex<T>, 1, 0, int>, Eigen::Aligned> gpu_in2(
|
||||
d_in2, kNumItems);
|
||||
Eigen::TensorMap<Eigen::Tensor<std::complex<T>, 1, 0, int>, Eigen::Aligned> gpu_out(
|
||||
d_out, kNumItems);
|
||||
|
||||
const std::complex<T> a(3.14f, 2.7f);
|
||||
const std::complex<T> b(-10.6f, 1.4f);
|
||||
|
||||
gpu_in1.device(gpu_device) = gpu_in1.constant(a);
|
||||
gpu_in2.device(gpu_device) = gpu_in2.constant(b);
|
||||
|
||||
enum CwiseOp {
|
||||
Add = 0,
|
||||
Sub,
|
||||
Mul,
|
||||
Div
|
||||
};
|
||||
|
||||
Tensor<std::complex<T>, 1, 0, int> actual(kNumItems);
|
||||
for (int op = Add; op <= Div; op++) {
|
||||
std::complex<T> expected;
|
||||
switch (static_cast<CwiseOp>(op)) {
|
||||
case Add:
|
||||
gpu_out.device(gpu_device) = gpu_in1 + gpu_in2;
|
||||
expected = a + b;
|
||||
break;
|
||||
case Sub:
|
||||
gpu_out.device(gpu_device) = gpu_in1 - gpu_in2;
|
||||
expected = a - b;
|
||||
break;
|
||||
case Mul:
|
||||
gpu_out.device(gpu_device) = gpu_in1 * gpu_in2;
|
||||
expected = a * b;
|
||||
break;
|
||||
case Div:
|
||||
gpu_out.device(gpu_device) = gpu_in1 / gpu_in2;
|
||||
expected = a / b;
|
||||
break;
|
||||
}
|
||||
assert(cudaMemcpyAsync(actual.data(), d_out, complex_bytes, cudaMemcpyDeviceToHost,
|
||||
gpu_device.stream()) == cudaSuccess);
|
||||
assert(cudaStreamSynchronize(gpu_device.stream()) == cudaSuccess);
|
||||
|
||||
for (int i = 0; i < kNumItems; ++i) {
|
||||
VERIFY_IS_APPROX(actual(i), expected);
|
||||
}
|
||||
}
|
||||
|
||||
cudaFree(d_in1);
|
||||
cudaFree(d_in2);
|
||||
cudaFree(d_out);
|
||||
}
|
||||
|
||||
|
||||
void test_cxx11_tensor_complex_cwise_ops()
|
||||
{
|
||||
CALL_SUBTEST(test_cuda_complex_cwise_ops<float>());
|
||||
CALL_SUBTEST(test_cuda_complex_cwise_ops<double>());
|
||||
}
|
||||
Reference in New Issue
Block a user