simple/
simple.rs

1use custos_math::{
2    custos::{OpenCL, Read, CPU},
3    BaseOps, Matrix,
4};
5
6fn main() -> custos::Result<()> {
7    //select() ... sets CPU as 'global device'
8    // -> when device is not specified in an operation, the 'global device' is used
9    let cpu = CPU::new();
10
11    with_select(&cpu);
12    specify_device(&cpu);
13
14    using_opencl()
15}
16
17fn with_select(cpu: &CPU) {
18    let a: Matrix<i32> = Matrix::from((cpu, (2, 3), [1, 2, 3, 4, 5, 6]));
19    let b = Matrix::from((cpu, (2, 3), [6, 5, 4, 3, 2, 1]));
20
21    let c = a + b;
22    assert_eq!(c.read(), vec![7, 7, 7, 7, 7, 7]);
23}
24
25fn specify_device(cpu: &CPU) {
26    //device is specified in every operation
27    let a = Matrix::from((cpu, (2, 2), [0.25f32, 0.5, 0.75, 1.]));
28    let b = Matrix::from((cpu, (2, 2), [1., 2., 3., 4.]));
29
30    let c_cpu = cpu.mul(&a, &b);
31    assert_eq!(cpu.read(&c_cpu), vec![0.25, 1., 2.25, 4.,]);
32}
33
34fn using_opencl() -> custos::Result<()> {
35    //OpenCL device (GPU)
36    let cl = OpenCL::new(0)?;
37
38    let a = Matrix::from((&cl, (2, 2), [0.25f32, 0.5, 0.75, 1.]));
39    let b = Matrix::from((&cl, (2, 2), [1., 2., 3., 4.]));
40
41    let c = a * b;
42    assert_eq!(c.read(), vec![0.25, 1., 2.25, 4.,]);
43    Ok(())
44}