use gategen::boolvar::*;
use gategen::gatesim::*;
use gategen::intvar::*;
use gatenative::mapper::*;
use gatenative::opencl_build_exec::*;
use gatenative::*;
use opencl3::device::{get_all_devices, Device, CL_DEVICE_TYPE_GPU};
fn mul_add_circuit() -> Circuit<u32> {
call32(|| {
let a = U10Var32::var();
let b = U10Var32::var();
let c = U10Var32::var();
let r = &a * &b + &c;
r.to_translated_circuit(a.concat(b).concat(c).iter())
})
}
fn main() -> Result<(), Box<dyn std::error::Error>> {
let device = Device::new(
*get_all_devices(CL_DEVICE_TYPE_GPU)?
.get(0)
.expect("No OpenCL devices"),
);
let circuit = mul_add_circuit();
let builder = OpenCLBuilder::new(&device, None);
let mut builder = OpenCLBasicMapperBuilder::new(builder);
builder.transform_helpers();
builder.add_with_config(
"mul_add",
circuit,
CodeConfig::new()
.arg_inputs(Some(&(24..30).collect::<Vec<_>>()))
.elem_inputs(Some(&(0..24).collect::<Vec<_>>()))
.aggr_output_code(Some(
r##"{
size_t i, j;
// buf are accumulators
global uint* buf = (global uint*)output;
uint temp[TYPE_LEN];
// transform to 32-bit word array.
OUTPUT_TRANSFORM_B10(temp, o0, o1, o2, o3, o4, o5, o6, o7, o8, o9);
for (i = 0; i < TYPE_LEN; i += 16)
for (j = 0; j < 16; j++)
atomic_add(buf + j, temp[i + j]);
}"##,
))
.aggr_output_len(Some(16)),
);
let mut execs = builder.build()?;
let input = execs[0].new_data(16);
let output = execs[0].execute_direct(
&input,
0u64,
|sum, _, output, arg| {
eprintln!("Arg Input: {}", arg);
sum + output.into_iter().map(|x| u64::from(*x)).sum::<u64>()
},
|_| false,
)?;
println!("Sum: {}", output);
Ok(())
}