编译创建 Eigen::ThreadPoolDevice 对象的代码时出错

Error compiling code which creates Eigen::ThreadPoolDevice object

特征版本 3.3.90

gcc 版本 5.4.0


我目前正在尝试为我的 Eigen Tensor 项目添加多线程支持,但在创建评估张量操作所需的 ThreadPoolDevice 时遇到了问题。

产生错误的最小代码示例如下所示。这是基于 Eigen 文档中的示例 shown here

#include <iostream>
#define EIGEN_USE_THREADS
#include <unsupported/Eigen/CXX11/Tensor>
#include <unsupported/Eigen/CXX11/ThreadPool>

int main()
{
    std::cout << "Eigen Version " << EIGEN_WORLD_VERSION << "." << EIGEN_MAJOR_VERSION << "." << EIGEN_MINOR_VERSION << std::endl;

    // Create the Eigen ThreadPoolDevice.
    Eigen::ThreadPoolDevice my_device(4 /* number of threads to use */);

    Eigen::Tensor<float, 2> a(40, 40);
    Eigen::Tensor<float, 2> b(40, 40);

    // Now just use the device when evaluating expressions.
    Eigen::array<Eigen::IndexPair<int>, 1> product_dims = { Eigen::IndexPair<int>(1, 0) };
    Eigen::Tensor<float, 2> c;
    c.device(my_device) = a.contract(b, product_dims);

    std::cout << "Result was " << c << std::endl;

    return 0;
}

这会产生如下所示的错误消息:

thread_pool_tests.cpp: In function ‘int main()’:
thread_pool_tests.cpp:12:68: error: no matching function for call to ‘Eigen::ThreadPoolDevice::ThreadPoolDevice(int)’
  Eigen::ThreadPoolDevice my_device(4 /* number of threads to use */);
                                                                    ^
In file included from /usr/local/include/unsupported/Eigen/CXX11/Tensor:103:0,
                 from thread_pool_tests.cpp:4:
/usr/local/include/unsupported/Eigen/CXX11/src/Tensor/TensorDeviceThreadPool.h:55:3: note: candidate: Eigen::ThreadPoolDevice::ThreadPoolDevice(Eigen::ThreadPoolInterface*, int, Eigen::Allocator*)
   ThreadPoolDevice(ThreadPoolInterface* pool, int num_cores, Allocator* allocator = NULL)
   ^
/usr/local/include/unsupported/Eigen/CXX11/src/Tensor/TensorDeviceThreadPool.h:55:3: note:   candidate expects 3 arguments, 1 provided
/usr/local/include/unsupported/Eigen/CXX11/src/Tensor/TensorDeviceThreadPool.h:53:8: note: candidate: constexpr Eigen::ThreadPoolDevice::ThreadPoolDevice(const Eigen::ThreadPoolDevice&)
 struct ThreadPoolDevice {
        ^
/usr/local/include/unsupported/Eigen/CXX11/src/Tensor/TensorDeviceThreadPool.h:53:8: note:   no known conversion for argument 1 from ‘int’ to ‘const Eigen::ThreadPoolDevice&’
/usr/local/include/unsupported/Eigen/CXX11/src/Tensor/TensorDeviceThreadPool.h:53:8: note: candidate: constexpr Eigen::ThreadPoolDevice::ThreadPoolDevice(Eigen::ThreadPoolDevice&&)
/usr/local/include/unsupported/Eigen/CXX11/src/Tensor/TensorDeviceThreadPool.h:53:8: note:   no known conversion for argument 1 from ‘int’ to ‘Eigen::ThreadPoolDevice&&’

看来此版本中的 ThreadPoolDevice 构造函数还需要指向从 ThreadPoolInterface 派生的对象的指针。但是我找不到这种形式的任何例子。有谁知道我如何解决这个问题并开始在我的应用程序中使用 ThreadPoolDevices。

谢谢。

查看错误信息和ThreadPoolDevice(ThreadPoolInterface* pool, int num_cores) : pool_(pool), num_threads_(num_cores) { } here, on line 107我要冒昧建议你需要创建并传入一个ThreadPool的地址给构造函数。该源文件的顶部有一个 ThreadPool typedef。

免责声明 -- 我没有测试过这个。这是基于对源代码文件的检查。 NonBlockingThreadPool.h

Eigen::ThreadPool pool(4/*number of threads*/);
Eigen::ThreadPoolDevice my_device(&pool, 4 /* number of threads to use */);

我想知道您的示例文档是否已过时,或者只是错误...