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

时间:2019-04-10 12:24:03

标签: c++ multithreading eigen tensor

本征版本3.3.90

gcc版本5.4.0


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

产生错误的最小代码示例如下所示。这是基于Eigen Docs中的示例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。

谢谢。

1 个答案:

答案 0 :(得分:3)

查看错误消息和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 */);

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