Chinaunix

标题: Linux 环境下 intel 核心显卡的 OpenCL 并行计算 [打印本页]

作者: zhujiang73    时间: 2016-05-25 21:57
标题: Linux 环境下 intel 核心显卡的 OpenCL 并行计算
  Intel CPU 内置的核心显卡也是可以做 OpenCL 并行计算的,虽然核心显卡的计算能力并不是很强. 

  Linux 环境下有 Beignet https://www.freedesktop.org/wiki/Software/Beignet/ 
Beignet is an open source implementation of the OpenCL specification - a generic compute oriented API. This code base contains the code to run OpenCL programs on Intel GPUs which basically defines and implements the OpenCL host functions required to initialize the device, create the command queues, the kernels and the programs and run them on the GPU. The code base also contains the compiler part of the stack which is included in backend ...


    配合 Boost.Compute https://github.com/boostorg/compute  和  OpenCV  可以处理图像,比如做一个边缘检测:

    参照  Boost.Compute 里这个例子 https://github.com/boostorg/comp ... cv_sobel_filter.cpp 效果大概是这样:



 下面的代码是在 GPU 平行的:

  1. // Create sobel filter program
  2. extern const std::string  str_cl_sobel = BOOST_COMPUTE_STRINGIZE_SOURCE
  3. (
  4.     //For out of boundary pixels, edge pixel
  5.     // value is returned
  6.     const sampler_t sampler = CLK_ADDRESS_CLAMP_TO_EDGE | CLK_FILTER_NEAREST;
  7.    
  8.     kernel void cl_kernel(read_only image2d_t src, write_only image2d_t dst)
  9.     {
  10.         int x = (int)get_global_id(0);
  11.         int y = (int)get_global_id(1);

  12.         if (x >= get_image_width(src) || y >= get_image_height(src))
  13.                 return;

  14.         //  [(x-1, y+1), (x, y+1), (x+1, y+1)]
  15.         //  [(x-1, y  ), (x, y  ), (x+1, y  )]
  16.         //  [(x-1, y-1), (x, y-1), (x+1, y-1)]

  17.         //  [p02, p12,   p22]
  18.         //  [p01, pixel, p21]
  19.         //  [p00, p10,   p20]

  20.         //Basically finding influence of neighbour pixels on current pixel
  21.         float4 p00 = read_imagef(src, sampler, (int2)(x - 1, y - 1));
  22.         float4 p10 = read_imagef(src, sampler, (int2)(x,     y - 1));
  23.         float4 p20 = read_imagef(src, sampler, (int2)(x + 1, y - 1));

  24.         float4 p01 = read_imagef(src, sampler, (int2)(x - 1, y));
  25.         //pixel that we are working on
  26.         float4 p21 = read_imagef(src, sampler, (int2)(x + 1, y));

  27.         float4 p02 = read_imagef(src, sampler, (int2)(x - 1, y + 1));
  28.         float4 p12 = read_imagef(src, sampler, (int2)(x,     y + 1));
  29.         float4 p22 = read_imagef(src, sampler, (int2)(x + 1, y + 1));

  30.         //Find Gx = kernel + 3x3 around current pixel
  31.         //           Gx = [-1 0 +1]     [p02, p12,   p22]
  32.         //                [-2 0 +2]  +  [p01, pixel, p21]
  33.         //                [-1 0 +1]     [p00, p10,   p20]
  34.         float3 gx = -p00.xyz + p20.xyz +
  35.                     2.0f * (p21.xyz - p01.xyz)
  36.                     -p02.xyz + p22.xyz;

  37.         //Find Gy = kernel + 3x3 around current pixel
  38.         //           Gy = [-1 -2 -1]     [p02, p12,   p22]
  39.         //                [ 0  0  0]  +  [p01, pixel, p21]
  40.         //                [+1 +2 +1]     [p00, p10,   p20]
  41.         float3 gy = p00.xyz + p20.xyz +
  42.                     2.0f * (- p12.xyz + p10.xyz) -
  43.                     p02.xyz - p22.xyz;
  44.         //Find G
  45.         float3 g = native_sqrt(gx * gx + gy * gy);

  46.         // we could also approximate this as g = fabs(gx) + fabs(gy)
  47.         write_imagef(dst, (int2)(x, y), (float4)(g.x, g.y, g.z, 1.0f));
  48.     }
  49. );

复制代码

作者: fender0107401    时间: 2016-05-25 22:17
我也想玩这个。
作者: zhujiang73    时间: 2016-05-25 22:23
回复 2# fender0107401


    把能用的计算能力都用上. 
作者: fender0107401    时间: 2016-05-25 22:38
回复 3# zhujiang73

我经常要处理大量数据,一直都是靠多线程来实现并行,一直想弄这块。现在Boost里面有这个库了,应该能方便很多。

   
作者: windoze    时间: 2016-05-26 01:34
这玩意貌似有bug,在我的mac上老崩……
作者: VIP_fuck    时间: 2016-05-26 09:10
你们玩儿的东西真是高大上啊
作者: 流氓无产者    时间: 2016-05-26 09:34
都高大尚啊,怎么那么多时间research啊
作者: wlmqgzm    时间: 2016-05-26 09:36
不错, 有时间的话, 也学习一下.




欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2