Git Product home page Git Product logo

opencl-ruby's People

Contributors

kerilk avatar sylrelo avatar yassinhox avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

opencl-ruby's Issues

Performance Benchmarks

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?

The Details

  1. My openCL code
    gist

I have a late 2013 Macbook Pro, so it has 3 devices

  • 2.3 GHz Intel Core i7
  • Intel Iris Pro
  • NVIDIA GeForce GT 750M 2048 MB

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)
  1. My MATLAB code
    gist

The average running time on MATLAB was 1.26 seconds.

My Guesses

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?

throw permission denied error

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

gem install opencl_ruby_ffi fails

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

How to stop warning 'Warning OpenCL 1.x loader detected!'

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

clBLAS FFI or use this one?

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.)

Question: Is there a way to find all processes that are using the GPU

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

undefined method `error_check' for OpenCL::Event:Class (NoMethodError)

/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'

Get Unique Identifier for Device

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!

  • Jorge

slow to load

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?

Passing A Mixed-Type Array to and from the GPU

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

Running opencl-ruby for the first time

Hi there,

I'm getting an error when I try to run the example code from the README

Steps to reproduce:

  1. Install gem with gem install opencl_ruby_ffi
  2. Run 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!"

Running opencl_ruby_ffi in a forked process

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."

support for Numo::NArray

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.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.