[MacRuby-devel] OpenCL experiment (request for comments)

Ruben Fonseca ruben at 0x82.com
Tue Jan 26 04:26:19 PST 2010

Hi @all!

I promised Laurent I would start a discussion on OpenCL and Macruby on the ML, and here I am :)

I wrote a small hack for MacRuby that adds basic (and hacky) support for OpenCL kernels running on your GPUs or CPUs. You can read about it on my blog post here http://blog.0x82.com/2010/1/23/opencl-in-macruby-hack-not-very-useful

The branch with the code is located here on github http://github.com/rubenfonseca/macruby. If you are interested on the actual hacky implementation, please look at “opencl.c” file.

Now there are a couple of things I need help. It was my very first (mac)ruby C extension, and I’m not really sure about many details on the implementation (I basically copy-pasted code from other modules eheh). I’ll raise a couple of questions here, I’m sure you can answer some of them :)

- How and were to store primitive values?

   For instance, OpenCL::Device has somewhere inside a “cl_device_id” pointer. However, on other classes (think OpenCL::Context#new) I’ll need to have a reference to that “cl_device_id” pointer somewhere.

   What’s the best way to store the pointer? Inside the Object struct? As an instance var? As an accessor? These later options doesn’t make sense to me, 'cause I’m never interested on getting the “cl_device_id” pointer on a IRB shell for instance... Hope I’m making myself clear.

- How the memory should be managed?

   As I said, I never wrote a MacRuby extension before. When writing the extension, I needed to do a couple of memory allocations. I used “xmalloc” (discovered by looking at other macurby *.c files). However, when I called “free” after I don’t need the memory anymore, all sorts of warnings happened at runtime.

   After I deleted the “free” calls, it all worked, but I’m not sure if I’m leaking memory somehow. On the other hand, maybe the memory is automatically GC’ed :) Can you clear this for me and show me the best practices?

- How to turn the OpenCL API more “Rubyish"

  I have no clue on this one. OpenCL seems like a huge API, and I don’t have a really background knowledge of GPU programming. There are all sorts of variations on each call, and a number of different entities (classes). Any suggestion on how to make this more pleasant to write in Ruby would be very very welcome.

  I even saw Laurent talk about a “Ruby -> OpenCL direct compiler via LLVM bitcode”, but I’m definitely not qualified to even consider that hipotesys. I tried twice creating a very simple compiler with LLVM and failed completely :P

Anyhow, sorry for the long email, but I would be very thankful for any help I can get :)

Rúben Fonseca

More information about the MacRuby-devel mailing list