14

我知道:

https://github.com/lsegal/barracuda

自 01/11 以来未更新

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

自 03/10 以来没有更新。

这些项目死了吗?或者它们只是因为它们的功能而没有改变,而 OpenCL/Ruby 从那时起就没有改变。有人在使用这些项目吗?运气好的话?

如果没有,你能推荐另一个用于 Ruby 的 opencl gem 吗?或者这种电话通常是如何完成的?只需从 Ruby 调用原始 C?

4

3 回答 3

4

您可以尝试opencl_ruby_ffi,它是积极开发的(由我的一位同事)并且与 OpenCL 1.2 版配合得很好。OpenCL 2.0 也应该很快就会推出。

sudo gem install opencl_ruby_ffi

在 Khronos 论坛中,您可以找到一个展示其工作原理的简单示例:

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, [65536],: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!"
于 2014-11-21T15:16:25.933 回答
2

您可能希望将任何您想要的 C 功能打包为 gem。这非常简单,通过这种方式,您可以将所有 c 逻辑包装在一个特定的命名空间中,您可以在其他项目中重用该命名空间。

http://guides.rubygems.org/c-extensions/

于 2013-01-26T22:43:21.347 回答
0

如果你想用 GPU 做高速计算,Cumo / NArray 是一个不错的选择。Cumo 具有与 NArray 相同的接口。虽然它是 cuda 而不是 opencl。

https://github.com/sonots/cumo

于 2019-01-03T11:20:15.613 回答