• Stars
    star
    113
  • Rank 310,115 (Top 7 %)
  • Language
    C
  • License
    MIT License
  • Created about 15 years ago
  • Updated almost 11 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Barracuda is a Ruby wrapper library for the OpenCL architecture.

Barracuda

Written by Loren Segal in 2009.

SYNOPSIS

Barracuda is a Ruby wrapper library for the OpenCL architecture. OpenCL is a framework for multi-processor computing, most notably allowing a programmer to run parallel programs on a GPU, taking advantage of the many cores available.

Barracuda aims to abstract both CUDA and OpenCL, however for now only OpenCL on OSX 10.6 is supported. Patches to extend this support would be joyously accepted!

INSTALLING

As mentioned above, this library currently only supports OSX 10.6 (or an earlier version with the OpenCL framework, if that's even possible). If you manage to mess with the source and get it working on [insert system here], please submit your patches.

Okay, assuming you have a compatible machine:

sudo gem install barracuda

Or:

git clone git://github.com/lsegal/barracuda
cd barracuda
rake install

USAGE

The basic workflow behind the OpenCL architecture is:

  1. Create a program (and kernel) to be run on the GPU's many cores.
  2. Create input/output buffers to pass data from Ruby to the GPU and back.
  3. Read the output buffer(s) to get your computed data.

In Barracuda, this looks basically like:

  1. Create a Barracuda::Program
  2. Create a Barracuda::Buffer for input and output
  3. Call the kernel method on the program with buffers as arguments
  4. Read output buffers

As you can see, there are only 2 basic classes: Program and Buffer. The program is where you compile your OpenCL code, and the Buffer class is a subclass of Array that contains your data to pass in and out of the kernel method.

EXAMPLE

Consider the following example to sum a bunch of integers:

program = Program.new <<-'eof'
  __kernel void sum(__global int *in, __global int *out) {
    atom_add(out, in[get_global_id(0)]); 
  }
eof

output = Buffer.new(1)
program.sum((1..65536).to_a, output)

puts "The sum is: " + output.data[0].to_s

The above example will compute the sum of integers 1 to 65536 using (at most) 65536 parallel processes and return the result in the 1-dimensional output buffer (which stores integers and is of length 1). The kernel method sum is called by calling the #sum method on the program object, and the arguments are passed in sequentially as the input data (the integers) followed by the output buffer to store the data.

We can also specify the work group size (the number of iterations we need to run). Barracuda automatically selects the size of the largest buffer as the work group size, but in some cases this may be too small or too large. To manually specify the work group size, call the kernel with an options hash:

program.my_kernel_method(..., :times => 512)

OUTPUT BUFFERS

The Buffer class is a superset of both data to be sent and read from the OpenCL kernel method being called. In general, if the Buffer contains nil elements, it is marked as an "output buffer" and the data is read back from OpenCL after the kernel method executes. These nil buffers are not written to OpenCL initially, so they are only meant for output data. On the other hand, if the buffer contains regular data, it is by default considered as input data only, and the data is not read back after the kernel method completes.

In some cases you may want to have a buffer that is both input and output and should be read from after the kernel method finishes. To do this, you mark the buffer as an outvar as so:

program = Program.new <<-'eof'
  __kernel void addN(__global int *data, int N) {
    int i = get_global_id(0);
    data[i] = data[i] + N;
  }
eof

data = [1, 2, 3]
program.addN(data.outvar, 10) 

# prints: [11, 12, 13]
p data 

RETURN VALUE

Generally you need to pass in your output buffer as the buffer to write the data back to. The idiom void method(input, output) is common to write data to output buffers in languages such as C but is a rather clunky API for Ruby. Instead, Barracuda returns the output buffers as the result of the kernel method call. If there is only one output buffer, that buffer is returned as a single result (rather than an array of buffers).

The example above could be simply rewritten as:

# prints: [11, 12, 13]
p program.addN(data.outvar, 10)

CONVERTING TYPES

OpenCL has a variety of native types. Most of them are supported, however some are not. Because Ruby only has the concept of Float and Fixnum (integer), you may need to tell Barracuda the type of your input if you're trying to pass in a char, short or double (or possibly have some signedness restrictions). To do this, simply call .to_type(:my_type) on the input where :my_type is a key in the Barracuda::TYPES hash:

>> Barracuda::TYPES.keys
=> [:bool, :char, :uchar, :short, :ushort, :int, :uint, :long, 
    :ulong, :float, :half, :double, :size_t, :ptrdiff_t, 
    :intptr_t, :uintptr_t]

For example, to pass in a short, do:

program.my_kernel(2.to_type(:short))

This can also be applied to an Array of shorts:

program.my_kernel([1, 2, 3].to_type(:short))

The default type for an array (and buffers) is :int

CLASS DETAILS

Barracuda::Program:

Represents an OpenCL program

Program.new(PROGRAM_SOURCE)  => creates a new program

Program#compile(SOURCE)      => recompiles a program

Program#KERNEL_METHOD(*args) => runs KERNEL_METHOD in the compiled program
  - args should be the arguments defined in the kernel method.
  - supported argument types are Float and Fixnum objects only.
  - if the last arg is a Hash, it should be an options hash with keys:
      - :times => FIXNUM (the number of iterations to run)

Barracuda::Buffer (extends Array):

Data storage to transfer to/from an OpenCL kernel method

Buffer.new(buffer_array) => creates a new input buffer
Buffer.new(size)         => creates a new output buffer of size `size`

Buffer#mark_dirty        => call this if the data was modified between calls

Buffer#dirty?            => returns whether the buffer is marked as dirty

Buffer#outvar            => mark the buffer to be read as output

Buffer#outvar?           => returns whether buffer is marked to be read

GLOSSARY

  • Program: an OpenCL program is generally created from a variant of C that has extra domain specific keywords. A program has at least one "kernel" method, but can have many regular methods.

  • Kernel: a special "entry" method in the program that is exposed to the programmer to be called on via the OpenCL framework. A kernel method is represented by the __kernel keyword before the method body.

  • Buffer: memory storage which is accessible and (generally shared with the program). Buffers are usually marked with the __global keyword in an OpenCL program.

COPYRIGHT & LICENSING

Copyright 2009 Loren Segal, licensed under the MIT License

More Repositories

1

yard

YARD is a Ruby Documentation tool. The Y stands for "Yay!"
Ruby
1,868
star
2

my_toy_compiler

My Toy Compiler. Read about how I did it at the homepage URL
C++
811
star
3

atom-runner

This package will run various script files inside of Atom. It currently supports JavaScript, CoffeeScript, Ruby, and Python. You can add more.
CoffeeScript
280
star
4

cppgo

Call virtual methods on C++ classes from Go without cgo.
Go
61
star
5

yard-spec-plugin

YARD Plugin example that documents RSpec code (live example in homepage)
Ruby
49
star
6

gorb

Auto-generates a native Ruby wrapper stub for a given Go package.
Go
33
star
7

samus

Samus helps you release Open Source Software.
Ruby
27
star
8

yard-js

YARD plugin for documenting JavaScript source code
Ruby
24
star
9

easy_audio

EasyAudio is a simplified Ruby wrapper for the portaudio library.
Ruby
23
star
10

ripper18

Ripper for Ruby 1.8.x
C
22
star
11

atom-rst-preview

Open a rendered version of the ReStructuredText in the current editor with `ctrl-shift-r`.
CoffeeScript
22
star
12

couch-todo

A simple TODO app written in Javascript using CouchDB
13
star
13

things-export

Export my Things.app todo items to a simple webpage
Ruby
12
star
14

brainz

Brainz is an Artificial Neural Network library written in Ruby
Ruby
12
star
15

yard-tmbundle

A Textmate Bundle for YARD
11
star
16

easy_vst

A VST Instrument Plug-In that connects to a remote Ruby process via DRb to process audio samples and MIDI messages.
C++
11
star
17

friend

Adds fine grained visibility semantics to Ruby
C
10
star
18

odb

ODB: A Ruby Object Database
Ruby
10
star
19

jenkins-codebuilder-plugin

CodeBuilder Plugin for Jenkins: Dynamically provisions build agents using AWS CodeBuild
Java
10
star
20

mmmail

Mmm, a Minimalist mail library for Ruby. Works with SMTP or sendmail. Provides a one-call method for simple one-off emailing.
Ruby
9
star
21

yard-blame

Adds git blame info to your docs (alpha)
Ruby
9
star
22

yard-types-parser

Parses YARD formal type declarations into a human readable format.
Ruby
9
star
23

yard-examples

Examples from my Montreal.rb talk (slides at http://is.gd/14bdd)
Ruby
9
star
24

yardoc.org

YARD's website
HTML
8
star
25

metamorph

Transparently defines instance methods inside anonymous module so mixins can override them with proper inheritance.
Ruby
7
star
26

gem-sane-binary

RubyGems plugin to allow gem install to generate executables that work for both Ruby 1.8 and 1.9
Ruby
7
star
27

tadpole

Tadpole: A Small but Extensible Templating Engine for Ruby
Ruby
7
star
28

couchio

Virtual filesystem support for a CouchDB database.
Ruby
7
star
29

jamespath

Implements JMESpath declarative object searching. Like XPath, but for JSON and other structured objects.
Ruby
6
star
30

github-release-from-changelog-action

Creates a GitHub release from a CHANGELOG file
Ruby
6
star
31

trac-export-wiki

Exports Trac wiki pages as local HTML files
Ruby
6
star
32

complexity

Calculates the McCabe cyclomatic complexity index of the methods in your Ruby code.
Ruby
5
star
33

yard-thor

Ruby
4
star
34

NoSpellCheck

Chrome extension to enable Firefox-like spellchecking defaults (only check textareas by default)
JavaScript
4
star
35

atom-go-format

Deprecated. Use go-plus ->
CoffeeScript
4
star
36

force_bind

Adds UnboundMethod#bind_class to bind an UnboundMethod to a class.
C
3
star
37

MiddleButtonScroll

Adds smooth scrolling for the middle button auto-scroll and improves the UI. click to edit
HTML
3
star
38

mob_spawner

Manages and spawns worker threads to run arbitrary shell commands.
Ruby
3
star
39

flutter_multi_instance_handler

Detect and handle multiple instances of a Flutter application.
C++
3
star
40

drmake

Makefile where all the targets execute in isolated Docker containers. Docker+Make = Doctor Make.
Go
3
star
41

dart-opengl

Dart FFI OpenGL Bindings
Dart
3
star
42

whiny_finder

A Rails 1.2.x plugin to add find! and find_by_NAME! methods that raise RecordNotFound exceptions instead of returning nil
Ruby
3
star
43

TankRacer

A small implementation of the Quake3 engine in the form of a racing game
C
3
star
44

py-ruby-decorators

The most Pythonic decorators possible using Ruby syntax
Ruby
2
star
45

yard-rake

YARD Rake plugin
Ruby
2
star
46

ruby2pilar

Ruby
2
star
47

flutter_windows_texture_bug

Bug with TextureRegistry on Windows
C++
2
star
48

Twittle

A Multi-platform Twitter Client written in C++ using wxWidgets
C++
2
star
49

screenshot-action

PowerShell
2
star
50

discovery-srv

A discovery service for microservices
Go
2
star
51

ruby_correct

Ruby
2
star
52

Wiidi

Wiimote MIDI interface application for OSX
Objective-C
2
star
53

rvm-tester

Runs tests across Ruby installations in RVM
Ruby
2
star
54

efnet.social

efnet.social Mastodon instance
Shell
2
star
55

dinit

Dependency Injection (DI) library for Go
Go
2
star
56

gemstats_analysis

Analysis of Gem Usage Statistics
JavaScript
2
star
57

cf_sveltekit_page

TypeScript
1
star
58

multidisplay

JavaScript
1
star
59

yard-rbs-core

HTML
1
star
60

jml4disco

Java Modelling Language 4 Disco: Adds distribution and Boogie support to JML4
Java
1
star
61

yard-go

A YARD plugin that adds support for documenting Go source programs.
Ruby
1
star
62

cf_remix_page

TypeScript
1
star
63

my_fake_project

It's a real project I swear.
Ruby
1
star
64

booger

Verifies Ruby programs using Boogie
Ruby
1
star
65

NoFocusHighlight

Removes auto-complete highlight background and input focus ring
JavaScript
1
star
66

yard-defaultreturn

Default return types for methods in a class/module
Ruby
1
star
67

yard-sitemap

A YARD plugin to build a sitemap.xml for generated HTML documentation.
Ruby
1
star
68

flutter_url_protocol

A unified Flutter library to register custom URL protocol handlers across all supported platforms.
C++
1
star
69

py101

Jupyter Notebook
1
star