nanosim-lig / opencl-ruby Goto Github PK
View Code? Open in Web Editor NEWOpenCL bindings for Ruby
License: BSD 2-Clause "Simplified" License
OpenCL bindings for Ruby
License: BSD 2-Clause "Simplified" License
Hi again,
I wanted to do a quick comparison between MATLAB and openCL. My first results are that MATLAB is 20 times faster at executing the example addition
operation in the README. That can't be right, can it?
I have a late 2013 Macbook Pro, so it has 3 devices
Here's the benchmark results
user system total real
i7 22.650000 2.110000 24.760000 ( 22.617686)
IrisPro 21.610000 0.560000 22.170000 ( 22.202700)
GeForce 21.830000 0.570000 22.400000 ( 22.734518)
The average running time on MATLAB was 1.26 seconds.
My first guess is that since I'm new to both C and openCL I'm failing to accurately translate that openCL code to MATLAB.
My second guess is that NArray is less efficient than MATLAB's implementation.
Suggestions?
Rather than just a plain OpenCL error I would like to see a permission denied error thrown when trying to use opencl.
[6] pry(main)> OpenCL.platforms.first.devices
OpenCL::Error::DEVICE_NOT_FOUND: -1
After running gem install opencl_ruby_ffi
from my windows console I get following error:
Temporarily enhancing PATH to include DevKit...
Building native extensions. This could take a while...
ERROR: Error installing opencl_ruby_ffi:
ERROR: Failed to build gem native extension.
C:/Ruby22-x64/bin/ruby.exe -r ./siteconf20160512-8692-sv1w01.rb extconf.rb
checking for narray.h... no
checking for narray.h... yes
creating Makefile
make "DESTDIR=" clean
make "DESTDIR="
generating narray_ffi_c-x64-mingw32.def
compiling narray_ffi.c
narray_ffi.c: In function 'na_address':
narray_ffi.c:13:9: warning: cast from pointer to integer of different size [-Wpointer-to-int-cast]
narray_ffi.c: In function 'na_to_ptr':
narray_ffi.c:24:9: warning: unused variable 'address' [-Wunused-variable]
narray_ffi.c: In function 'na_mark_ref_empty':
narray_ffi.c:110:18: warning: unused variable 'a2' [-Wunused-variable]
narray_ffi.c: In function 'na_pointer_to_na':
narray_ffi.c:145:28: warning: cast to pointer from integer of different size [-Wint-to-pointer-cast]
narray_ffi.c: In function 'na_s_to_na_pointer':
narray_ffi.c:186:1: warning: control reaches end of non-void function [-Wreturn-type]
linking shared-object narray_ffi_c.so
narray_ffi.o: In function `na_pointer_to_na':
C:\Ruby22-x64\lib\ruby\gems\2.2.0\gems\narray_ffi-1.2.0\ext\narray_ffi_c/narray_ffi.c:140: undefined reference to `na_get_typecode'
C:\Ruby22-x64\lib\ruby\gems\2.2.0\gems\narray_ffi-1.2.0\ext\narray_ffi_c/narray_ffi.c:160: undefined reference to `na_sizeof'
C:\Ruby22-x64\lib\ruby\gems\2.2.0\gems\narray_ffi-1.2.0\ext\narray_ffi_c/narray_ffi.c:165: undefined reference to `cNArray'
C:\Ruby22-x64\lib\ruby\gems\2.2.0\gems\narray_ffi-1.2.0\ext\narray_ffi_c/narray_ffi.c:152: undefined reference to `na_sizeof'
C:\Ruby22-x64\lib\ruby\gems\2.2.0\gems\narray_ffi-1.2.0\ext\narray_ffi_c/narray_ffi.c:165: undefined reference to `cNArray'
narray_ffi.o: In function `na_to_ptr':
C:\Ruby22-x64\lib\ruby\gems\2.2.0\gems\narray_ffi-1.2.0\ext\narray_ffi_c/narray_ffi.c:36: undefined reference to `na_sizeof'
collect2.exe: error: ld returned 1 exit status
make: *** [narray_ffi_c.so] Error 1
make failed, exit code 2
Gem files will remain installed in C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/narray_ffi-1.2.0 for inspection.
Results logged to C:/Ruby22-x64/lib/ruby/gems/2.2.0/extensions/x64-mingw32/2.2.0/narray_ffi-1.2.0/gem_make.out
any solutions, to solve this problem?
Running on Windows10 x64, OpenCL on Intel CPU
Just an FYI. I released a gem called compute_unit that uses this as a dependency. https://rubygems.org/gems/compute_unit
Hi,
I am unable to run the sample program. This is the sample I am trying to run.
http://stackoverflow.com/a/27064531/648964
I am on Ubuntu 14.04. I installed amd opencl sdk, opencl headers and did "sudo ldconfig". I am running the proprietary catalyst driver. I am not sure if opencl installed correctly. I was using ruby to check which threw the error.
Just starting with your excellent library, having just successfully written my first OpenCL program in Ruby (vector addition) ๐
One niggle: Every time I load the gem file thus, this warning goes to STDOUT. Right at the end of here it seems to auto detect my OpenCL 1.2 framework, which is great, but if I want to suppress the warning how do I do this? Many thanks
require 'opencl_ruby_ffi'
OS: OS X 10.9.5
Hi all,
The next thing I want to do, is try to tap into clBLAS. I imagine that because clBLAS is sitting in openCL I'll need to somehow have a clBLAS ffi connect to this one. And after that I'd like to get using clMAGMA on top of clBLAS!
So in theory I'd have an FFI to clMAGMA dependent on an FFI to clBLAS dependent on an FFI to openCL.
Is that right? Or is there some nice way to talk to clBLAS and clMAGMA via this FFI? The end goal here is to be able to do some nice heterogeneous Linear Algebra in Ruby, but for now I'd like to piece together a simple proof of concept doing matrix-matrix multiplication in Ruby.
So the core of my question is: how would I go about running this sample from Ruby? Will this FFI help or do I need to write my own? If I did write my own, how could I use the work you've done here?
(PS I know that NMatrix is tackling this, but I want to explore a different direction, namely using clBLAS instead of fortran BLAS at the bottom of the heap. Right now NMatrix can't invert a matrix on OSX.)
I am looking to create a list of attached processes per GPU. For nvidia I am using nvidia-smi -i #{index} --query-compute-apps=pid --format=csv,noheader
and was wondering if I can do the same for AMD gpus via OpenCL?
Right now I am scanning the /proc/pid/fd directory and matching device drivers to see that a process is using a GPU but I need to be more granular and find out which process is using which GPU because I might have multiple GPUs and processes. Also /proc often has multiple
/usr/local/lib64/ruby/gems/3.1.0/gems/opencl_ruby_ffi-1.3.11/lib/opencl_ruby_ffi/opencl_ruby_ffi_base_gen.rb:1573>, @autorelease=true>
/usr/local/lib64/ruby/gems/3.1.0/gems/opencl_ruby_ffi-1.3.11/lib/opencl_ruby_ffi/opencl_ruby_ffi_base_gen.rb:1580:in release': undefined method
error_check' for OpenCL::Event:Class (NoMethodError)
error_check( error )
^^^^^^^^^^^
from /usr/local/lib64/ruby/gems/3.1.0/gems/ffi-1.15.5/lib/ffi/autopointer.rb:175:in `call'
from /usr/local/lib64/ruby/gems/3.1.0/gems/ffi-1.15.5/lib/ffi/autopointer.rb:175:in `release'
from /usr/local/lib64/ruby/gems/3.1.0/gems/ffi-1.15.5/lib/ffi/autopointer.rb:150:in `call'
Hello,
I'm trying to create a load balancer that can dispatch tasks to various GPUs if they're present. To do this, I need to launch one process per GPU device, so that this process can dispatch jobs to its GPU. However, OpenCL itself doesn't have a way of providing a unique ID that is unique to a given GPU device. It only gives you information on the "type" of device or the OpenCL version number, which doesn't help me. I did some searching around, and saw that for NVidia devices, the device-unique IDs need to be queried using the CL_DEVICE_PCI_BUS_ID_NV token of the cl_nv_device_a ttribute_query extension.
I found that here: https://anteru.net/2014/08/01/2483/
Is there a way to do this with opencl-ruby? I can see the cl_nv_device_attribute_query extension when I run "device.extensions" but can't seem to find a way to query the cl_nv_device_attribute_query extension.
Thanks for your help!
the opencl-ruby library takes few seconds to load. If I am just interested in getting the device info do I need all the other libraries that are loaded in lib/opencl_ruby_ffi.rb?
If I try to just load opencl_ruby_ffi/Device.rb I get
NameError: uninitialized constant OpenCLRefinements
from /opt/crossbelt/embedded/lib/ruby/gems/2.5.0/gems/opencl_ruby_ffi-1.3.4/lib/opencl_ruby_ffi/Device.rb:1:in `<top (required)>'
Is there any other way to speed up the discovery of opencl devices?
I'm new to GPU programming and don't know where else to ask this question, but is it possible to pass a mixed-type array to the GPU and back without using an NArray? NArray lets me create arrays of a single type, or of a ruby "object" (which I believe is a pointer to a ruby object), but those don't work for my purposes. My setup is Linux, with an NVidia GTX-980 GPU.
I'm processing time-series event data in the GPU using the following struct:
typedef struct { int start_date; int end_date; float starting_value; float delta; } event;
So, I need some array elements to be integers, and others to be floating points.
I was able to create an NArray in ruby of type "object" (a Ruby object) that stores and processes arrays with mixed type elements, and was even able to convert it to a Marshalled object and back with no problems. I run into problems when I try passing the NArray to the GPU, or try passing such a mixed-type array back to Ruby from the GPU. I'm sure the problem is that the ruby "object" is a reference pointer, and the opencl-ruby "create_buffer" or the "enqueue_read_buffer" functions may be treating them as integers without dereferencing the pointers.
Is there any other way to pass a block of mixed data types from ruby to the GPU without using an NArray? Or would it be possible to modify the opencl-ruby code to iterate through an NArray of type "object" to pass the individual integer and float values to the buffer?
Thanks! I'd appreciate any help or insight you can give me.
Cheers,
Jorge
Hi there,
I'm getting an error when I try to run the example code from the README
Steps to reproduce:
gem install opencl_ruby_ffi
example.rb
via ruby example.rb
Error
/Users/KC/.rvm/gems/ruby-2.1.2/gems/ffi-1.9.5/lib/ffi/library.rb:133:in `block in ffi_lib': Could not open library 'libOpenCL.so.1': dlopen(libOpenCL.so.1, 5): image not found. (LoadError)
Could not open library 'libOpenCL.so.1.dylib': dlopen(libOpenCL.so.1.dylib, 5): image not found
from /Users/KC/.rvm/gems/ruby-2.1.2/gems/ffi-1.9.5/lib/ffi/library.rb:100:in `map'
from /Users/KC/.rvm/gems/ruby-2.1.2/gems/ffi-1.9.5/lib/ffi/library.rb:100:in `ffi_lib'
from /Users/KC/.rvm/gems/ruby-2.1.2/gems/opencl_ruby_ffi-0.995/lib/opencl_ruby_ffi/opencl_ruby_ffi_base_gen.rb:9:in `rescue in <module:OpenCL>'
from /Users/KC/.rvm/gems/ruby-2.1.2/gems/opencl_ruby_ffi-0.995/lib/opencl_ruby_ffi/opencl_ruby_ffi_base_gen.rb:6:in `<module:OpenCL>'
from /Users/KC/.rvm/gems/ruby-2.1.2/gems/opencl_ruby_ffi-0.995/lib/opencl_ruby_ffi/opencl_ruby_ffi_base_gen.rb:4:in `<top (required)>'
from /Users/KC/.rvm/rubies/ruby-2.1.2/lib/ruby/2.1.0/rubygems/core_ext/kernel_require.rb:55:in `require'
from /Users/KC/.rvm/rubies/ruby-2.1.2/lib/ruby/2.1.0/rubygems/core_ext/kernel_require.rb:55:in `require'
from /Users/KC/.rvm/gems/ruby-2.1.2/gems/opencl_ruby_ffi-0.995/lib/opencl_ruby_ffi.rb:1:in `<top (required)>'
from /Users/KC/.rvm/rubies/ruby-2.1.2/lib/ruby/2.1.0/rubygems/core_ext/kernel_require.rb:135:in `require'
from /Users/KC/.rvm/rubies/ruby-2.1.2/lib/ruby/2.1.0/rubygems/core_ext/kernel_require.rb:135:in `rescue in require'
from /Users/KC/.rvm/rubies/ruby-2.1.2/lib/ruby/2.1.0/rubygems/core_ext/kernel_require.rb:144:in `require'
from example.rb:1:in `<main>'
I'm very new to FFIs and mixing C with Ruby like this. Are there steps that I've missed such as getting a pre-compiled openCL library or something of that nature? How can I go from nothing to running the sample from the readme?
For clarity here's the sample:
require 'opencl_ruby_ffi'
source = <<EOF
__kernel void addition( float2 alpha, __global const float *x, __global float *y) {
size_t ig = get_global_id(0);
y[ig] = (alpha.s0 + alpha.s1 + x[ig])*0.3333333333333333333f;
}
EOF
platform = OpenCL::platforms.first
device = platform.devices.first
context = OpenCL::create_context(device)
queue = context.create_command_queue(device, :properties => OpenCL::CommandQueue::PROFILING_ENABLE)
prog = context.create_program_with_source( source )
prog.build
a_in = NArray.sfloat(65536).random(1.0)
a_out = NArray.sfloat(65536)
f = OpenCL::Float2::new(3.0,2.0)
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)
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])
queue.enqueue_read_buffer(b_out, a_out, :event_wait_list => [event])
queue.finish
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!"
Is there a correct way of running opencl_ruby_ffi in a forked process?
My ruby code oftentimes forks a ruby process that executes opencl_ruby code... the previous version of opencl_ruby_ffi allowed me to do this as long as I don't initialize opencl_ruby_ffi in a parent process, and then execute code in a child process. To make this work, I would only initialize and run opencl_ruby_ffi within child processes as necessary.
The recent version of opencl_ruby_ffi fails to execute code within a forked process, which makes me believe that it's being initialized the minute ruby loads the library.
The warnings, errors are:
Warning: could not find extension function clGetKernelSubGroupInfoKHR!
/var/lib/gems/2.3.0/gems/opencl_ruby_ffi-1.3.2/lib/opencl_ruby_ffi/opencl_ruby_ffi_base.rb:236:in `error_check': -33 (OpenCL::Error::INVALID_DEVICE)
Is it possible to run opencl_ruby_ffi in a forked process? And if so, what would be the correct way? Below is a modified version of your sample code that fails when running in a forked process (this code works if the 'require' line is inserted inside the fork block):
require 'opencl_ruby_ffi'
pid = fork {
source = <<EOF
__kernel void addition( float2 alpha, __global const float *x, __global float *y) {
size_t ig = get_global_id(0);
y[ig] = (alpha.s0 + alpha.s1 + x[ig])*0.3333333333333333333f;
}
EOF
platform = OpenCL::platforms.first
device = platform.devices.first
context = OpenCL::create_context(device)
queue = context.create_command_queue(device,
:properties => OpenCL::CommandQueue::PROFILING_ENABLE)
prog = context.create_program_with_source( source )
prog.build
a_in = NArray.sfloat(65536).random(1.0)
a_out = NArray.sfloat(65536)
f = OpenCL::Float2::new(3.0,2.0)
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)
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])
queue.enqueue_read_buffer(b_out, a_out, :event_wait_list => [event])
queue.finish
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!"
}
Process.waitall
puts "Done executing."
Hi
Thank you for your great work. opencl-ruby is the best way to use the GPU in Ruby as far as I know . I'd appreciate it if you could support new NArray class library Ruby/Numo::NArray .
Thank you.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.