Expand description
§
| GitHub
Macros for easier/faster working with the ocl
crate. ocl-macros
currently supports rust macros for easy ocl::Kernel and ocl::Buffer creation.
This crate is still in early development. If you encounter any problems or have a suggestion, feel free to open an issue.
§Simple Example
This is an example use-case of ocl-macros:
use ocl::{Context, Device, Platform, Program, Queue};
use ocl_macros::*;
let PROGRAM_SRC: &str = r#"
kernel void add_one(global float* var) {
const uint n = get_global_id(0);
// This program adds 1.0f to each element in the buffer
var[n] += 1.0f;
}
"#;
// Initialize OpenCL context/queue
let platform = Platform::default();
// Get first device on default platform
let device = default_device!();
let context = Context::builder()
.platform(platform)
.devices(device)
.build()
.unwrap();
let queue = Queue::new(&context, device, None).unwrap();
let program = Program::builder()
.devices(device)
.src(PROGRAM_SRC)
.build(&context)
.unwrap();
// Create new float buffer with 100 elements of starting value 0.0f32
let buffer = buffer!(&queue, 100, 0.0f32);
// Create the "add_one" kernel with work size 100 and buffer as first unnamed argument
let kernel = kernel!(program, queue, "add_one", 100, &buffer);
// Run kernel (This is unsafe)
unsafe { kernel.enq().unwrap(); }
// Get buffer content as new vector
let vec = bget!(buffer);
// The elements are now 1.0f32!
assert!(vec[0] == 1.0f32);
This code uses various macros to create a new buffer and kernel with the created buffer as an unnamed argument. The kernel is then executed. The buffer content is read into a new vector and verified. It has changed from 0.0 to 1.0!
Modules§
Macros§
- bget
- Creates a new vector from a buffer.
- bread
- Reads a buffer into a vector.
- buffer
- Creates a buffer of the specified size with MEM_READ_WRITE flags. The type is inferred from the fill value.
- buffer_
flags - Creates a buffer of the specified size and flags. The type is inferred from the fill value.
- bwrite
- Writes a vector into a buffer.
- default_
device - Returns the first available device on the default platform.
- device_
vec - Returns a vector of all available devices on the default platform.
- kernel
- Builds a kernel with arguments from a program, queue, kernel name and work size. Adds unnamed arguments or named arguments given as tuples of (“name”, arg).
- kernel_
args - Adds unnamed arguments or named arguments given as tuples of (“name”, arg) to Kernel.
- kernel_
builder - Creates a KernelBuilder from a program, queue, kernel name and work size.
- pq_
kernel - Builds a kernel with arguments from a ProQue and the kernel name. Adds unnamed arguments or named arguments given as tuples of (“name”, arg).
- pq_
kernel_ n - Builds a kernel with named arguments from a ProQue and the kernel name. Adds named arguments given as tuples of (“name”, arg).
- proque
- Creates an ocl process queue struct.