Ruby有一个很好的openCL包装器吗?

我知道:

https://github.com/lsegal/barracuda

自01/11以来尚未更新

http://rubyforge.org/projects/ruby-opencl/

自03/10以来尚未更新.

这些项目是否死亡?或者他们根本没有改变,因为它们的功能,OpenCL / Ruby从那以后没有改变.有人使用这些项目吗?运气好的话?

如果没有,你能否推荐另一个用于Ruby的opencl宝石?或者这样的通话是如何进行的?只需从Ruby调用raw C?

谢谢!

解决方法

你可以试试 opencl_ruby_ffi,它是积极开发的(由我的一个同事),并且与OpenCL 1.2版一起工作. OpenCL 2.0也应该即将推出.
sudo gem install opencl_ruby_ffi

In Khronos forum你可以找到一个快速的例子来显示它的工作原理:

require 'opencl_ruby_ffi'

# select the first platform/device available
# improve it if you have multiple GPU on your machine
platform = OpenCL::platforms.first
device = platform.devices.first

# prepare the source of GPU kernel
# this is not Ruby but OpenCL C
source = <<EOF
__kernel void addition(  float2 alpha,__global const float *x,__global float *y) {\n\
  size_t ig = get_global_id(0);\n\
  y[ig] = (alpha.s0 + alpha.s1 + x[ig])*0.3333333333333333333f;\n\
}
EOF

# configure OpenCL environment,refer to OCL API if necessary
context = OpenCL::create_context(device)
queue = context.create_command_queue(device,:properties => OpenCL::CommandQueue::PROFILING_ENABLE)

# create and compile the OpenCL C source code
prog = context.create_program_with_source(source)
prog.build

# allocate cpu (=RAM) buffers and 
# fill the input one with random values
a_in = NArray.sfloat(65536).random(1.0)
a_out = NArray.sfloat(65536)

# allocate GPU buffers matching the cpu ones
b_in = context.create_buffer(a_in.size * a_in.element_size,:flags => OpenCL::Mem::copY_HOST_PTR,:host_ptr => a_in)
b_out = context.create_buffer(a_out.size * a_out.element_size)

# create a constant pair of float
f = OpenCL::Float2::new(3.0,2.0)

# trigger the execution of kernel 'addition' on 128 cores
event = prog.addition(queue,[65536],f,b_in,b_out,:local_work_size => [128])
# #Or if you want to be more OpenCL like:
# k = prog.create_kernel("addition")
# k.set_arg(0,f)
# k.set_arg(1,b_in)
# k.set_arg(2,b_out)
# event = queue.enqueue_NDrange_kernel(k,:local_work_size => [128])

# tell OCL to transfer the content GPU buffer b_out 
# to the cpu memory (a_out),but only after `event` (= kernel execution)
# has completed
queue.enqueue_read_buffer(b_out,a_out,:event_wait_list => [event])

# wait for everything in the command queue to finish
queue.finish
# Now a_out contains the result of the addition performed on the GPU

# add some cleanup here ...

# verify that the computation went well
diff = (a_in - a_out*3.0)
65536.times { |i|
  raise "computation error #{i} : #{diff[i]+f.s0+f.s1}" if (diff[i]+f.s0+f.s1).abs > 0.00001
}
puts "Success!"

相关文章

validates:conclusion,:presence=>true,:inclusion=>{...
一、redis集群搭建redis3.0以前,提供了Sentinel工具来监控各...
分享一下我老师大神的人工智能教程。零基础!通俗易懂!风趣...
上一篇博文 ruby传参之引用类型 里边定义了一个方法名 mo...
一编程与编程语言 什么是编程语言? 能够被计算机所识别的表...
Ruby类和对象Ruby是一种完美的面向对象编程语言。面向对象编...