c++ - Intel HD GPU 与 Intel CPU 性能比较

标签 c++ linux performance opencl intel

我是 OpenCL 的新手,目前对其性能有一些疑问。

我有 Intel(R) Core(TM) i5-4460 CPU @ 3.20GHz + ubuntu + Beignet(Intel 开源 openCL 库参见:http://arrayfire.com/opencl-on-intel-hd-iris-graphics-on-linux/ http://www.freedesktop.org/wiki/Software/Beignet/)

我有一个简单的长凳

#define __CL_ENABLE_EXCEPTIONS
#include "CL/cl.hpp"
#include <vector>
#include <iostream>
#include <iterator>
#include <algorithm>

using namespace cl;
using namespace std;

void CPUadd(vector<float> & A, vector<float> & B, vector<float> & C)
{
    for (int i = 0; i < A.size(); i++)
    {
        C[i] = A[i] + B[i];
    }
}

int main(int argc, char* argv[]) {
    Context(CL_DEVICE_TYPE_GPU);
    static const unsigned elements = 1000000;
    vector<float> data(elements, 6);
    Buffer a(begin(data), end(data), true, false);
    Buffer b(begin(data), end(data), true, false);
    Buffer c(CL_MEM_READ_WRITE, elements * sizeof(float));

    Program addProg(R"d(
        kernel
        void add(   global const float * restrict const a,
                    global const float * restrict const b,
                    global       float * restrict const c) {
            unsigned idx = get_global_id(0);
            c[idx] = a[idx] + b[idx] + a[idx] * b[idx] + 5;
        }
    )d", true);

    auto add = make_kernel<Buffer, Buffer, Buffer>(addProg, "add");

#if 1
    for (int i = 0; i < 4000; i++)
    {
        add(EnqueueArgs(elements), a, b, c);
    }
    vector<float> result(elements);
    cl::copy(c, begin(result), end(result));
#else
    vector<float> result(elements);
    for (int i = 0; i < 4000; i++)
    {
        CPUadd(data, data, result);
    }
#endif

    //std::copy(begin(result), end(result), ostream_iterator<float>(cout, ", "));
}

根据我的测量,Intel HD 比单 CPU 快 20 倍(见上面的基准)。它对我来说似乎太小了,因为在使用 4 个内核的情况下,我只能在 GPU 上获得 5 倍的加速。我写的板凳是否正确,加速似乎是现实的?不幸的是,在我的例子中,clinfo 找不到 CPU 作为 OpenCL 设备,所以我无法进行直接比较。

更新

测量

$ g++ -o main main.cpp -lOpenCL -std=c++11 $时间./主要 真正的 0m37.316s 用户 0m37.280s 系统 0m0.016s $ g++ -o main main.cpp -lOpenCL -std=c++11 $时间./主要 实际 0m2.349s 用户 0m0.524s 系统 0m0.624s

总计:GPU 为 2.349 - 0.524 = 1.825 CPU 37.316 - 0.524 = 36.724

36.724/1.825 = 比单个 CPU 快 20.12 倍 => 比完整 CPU 快 5 倍。

最佳答案

您正在比较的两个实现在功能上是等效的。

您的 CPU 实现需要减少 30% 的内存带宽(这可以解释性能)。它仅访问数组 AB,而 GPU 内核使用 3 个数组 abc

关于c++ - Intel HD GPU 与 Intel CPU 性能比较,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34046063/

相关文章:

performance - 如何有效地创建两个向量中所有值对的热图?

macos - Mac OS X 上映射的成本是多少?

c++ - 如何将 ASN 中的 SIZE OF 运算符与另一个变量组合?

c++ - 当 unique_ptr 在 vector 中时,对 std::unique_ptr<> 拥有的对象的引用/ptr 是否安全?

c++ - 在我的应用程序中需要 >=GTK 3.16。如何分配?

c - 黑客 - 剥削的艺术 : debugging buffer overflow example

multithreading - 为线程设置亲和性掩码有真正的性能提升吗?

c++ - 重现 Qt : Segmentation fault when debugging code 中的错误

c++ - 类函数可以有不同类对象的参数吗?

php - Linux 权限和 PHP - 组