Boost.Compute比普通CPU慢?

时间:2014-06-18 08:17:10

标签: c++ boost opencl boost-compute

我刚开始玩Boost.Compute,看看它能为我们带来多快的速度,我写了一个简单的程序:

#include <iostream>
#include <vector>
#include <algorithm>
#include <boost/foreach.hpp>
#include <boost/compute/core.hpp>
#include <boost/compute/platform.hpp>
#include <boost/compute/algorithm.hpp>
#include <boost/compute/container/vector.hpp>
#include <boost/compute/functional/math.hpp>
#include <boost/compute/types/builtin.hpp>
#include <boost/compute/function.hpp>
#include <boost/chrono/include.hpp>

namespace compute = boost::compute;

int main()
{
    // generate random data on the host
    std::vector<float> host_vector(16000);
    std::generate(host_vector.begin(), host_vector.end(), rand);

    BOOST_FOREACH (auto const& platform, compute::system::platforms())
    {
        std::cout << "====================" << platform.name() << "====================\n";
        BOOST_FOREACH (auto const& device, platform.devices())
        {
            std::cout << "device: " << device.name() << std::endl;
            compute::context context(device);
            compute::command_queue queue(context, device);
            compute::vector<float> device_vector(host_vector.size(), context);

            // copy data from the host to the device
            compute::copy(
                host_vector.begin(), host_vector.end(), device_vector.begin(), queue
            );

            auto start = boost::chrono::high_resolution_clock::now();
            compute::transform(device_vector.begin(),
                       device_vector.end(),
                       device_vector.begin(),
                       compute::sqrt<float>(), queue);

            auto ans = compute::accumulate(device_vector.begin(), device_vector.end(), 0, queue);
            auto duration = boost::chrono::duration_cast<boost::chrono::milliseconds>(boost::chrono::high_resolution_clock::now() - start);
            std::cout << "ans: " << ans << std::endl;
            std::cout << "time: " << duration.count() << " ms" << std::endl;
            std::cout << "-------------------\n";
        }
    }
    std::cout << "====================plain====================\n";
    auto start = boost::chrono::high_resolution_clock::now();
    std::transform(host_vector.begin(),
                host_vector.end(),
                host_vector.begin(),
                [](float v){ return std::sqrt(v); });

    auto ans = std::accumulate(host_vector.begin(), host_vector.end(), 0);
    auto duration = boost::chrono::duration_cast<boost::chrono::milliseconds>(boost::chrono::high_resolution_clock::now() - start);
    std::cout << "ans: " << ans << std::endl;
    std::cout << "time: " << duration.count() << " ms" << std::endl;

    return 0;
}

这是我机器上的示例输出(win7 64位):

====================Intel(R) OpenCL====================
device: Intel(R) Core(TM) i7-4770 CPU @ 3.40GHz
ans: 1931421
time: 64 ms
-------------------
device: Intel(R) HD Graphics 4600
ans: 1931421
time: 64 ms
-------------------
====================NVIDIA CUDA====================
device: Quadro K600
ans: 1931421
time: 4 ms
-------------------
====================plain====================
ans: 1931421
time: 0 ms

我的问题是:为什么普通(非opencl)版本更快?

3 个答案:

答案 0 :(得分:8)

正如其他人所说,你的内核中很可能没有足够的计算来让你在GPU上运行一组数据是值得的(你受到内核编译时间和GPU传输时间的限制) 。

为了获得更好的性能数字,你应该多次运行算法(并且很可能会抛弃第一个算法,因为它会包含编译和存储内核的时间,因为它会更大)。

此外,您不应将transform()accumulate()作为单独的操作运行,而应使用融合的transform_reduce()算法,该算法使用单个内核执行转换和缩减。代码如下所示:

float ans = 0;
compute::transform_reduce(
    device_vector.begin(),
    device_vector.end(),
    &ans,
    compute::sqrt<float>(),
    compute::plus<float>(),
    queue
);
std::cout << "ans: " << ans << std::endl;

您还可以使用Boost.Compute和-DBOOST_COMPUTE_USE_OFFLINE_CACHE编译代码,这将启用脱机内核缓存(这需要与boost_filesystem链接)。然后,您使用的内核将存储在您的文件系统中,并且只在您第一次运行应用程序时进行编译(默认情况下,Linux上的NVIDIA已经这样做了。)

答案 1 :(得分:2)

我可以看到造成重大差异的一个可能原因。比较CPU和GPU数据流: -

CPU              GPU

                 copy data to GPU

                 set up compute code

calculate sqrt   calculate sqrt

sum              sum

                 copy data from GPU

鉴于此,似乎英特尔芯片在一般计算上只是有点垃圾,NVidia可能正在遭受额外的数据复制和设置GPU进行计算。

你应该尝试相同的程序,但操作要复杂得多 - sqrt和sum太简单了,无法克服使用GPU的额外开销。例如,你可以尝试计算Mandlebrot积分。

在你的例子中,将lambda移动到累积中会更快(一次通过内存而不是两遍)

答案 2 :(得分:1)

由于您未正确测量时间,导致结果不佳。

OpenCL设备拥有自己的时间计数器,它们与主机计数器无关。每个OpenCL任务都有4个状态,可以查询的计时器:(来自Khronos web site)

  1. CL_PROFILING_COMMAND_QUEUED,当事件标识的命令被主机排入命令队列时
  2. CL_PROFILING_COMMAND_SUBMIT,当由已排队的事件标识的命令由主机提交给与命令队列关联的设备时。
  3. CL_PROFILING_COMMAND_START,当事件标识的命令在设备上开始执行时。
  4. CL_PROFILING_COMMAND_END,当事件标识的命令在设备上完成执行时。
  5. 考虑到,计时器设备端。所以,测量内核和命令队列性能,您可以查询这些计时器。在您的情况下,需要2个最后一个计时器。

    在您的示例代码中,您需要测量主机时间,其中包括数据传输时间(如 Skizz 所述)以及在命令队列维护上浪费的所有时间。

    因此,要了解实际的内核性能,您需要将cl_event传递给内核(不知道如何在boost :: compute中执行)&amp;查询该事件的性能计数器或使您的内核真正巨大&amp;很难隐藏所有开销。