gatenative 0.2.2

The library to execute natively Gate circuits.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
//! OpenCL data transformers (for GPU).
//!
//! The data transformer in this module is working on GPU using OpenCL standard.

use crate::opencl_build_exec::*;
use crate::*;
use static_init::dynamic;

use opencl3::command_queue::CommandQueue;
use opencl3::context::Context;
use opencl3::device::Device;
use opencl3::error_codes::ClError;
use opencl3::kernel::{ExecuteKernel, Kernel};
use opencl3::memory::{Buffer, CL_MEM_READ_WRITE};
use opencl3::program::Program;
use opencl3::types::{cl_uint, cl_ulong, CL_BLOCKING};

use std::collections::HashMap;
use std::hash::{Hash, Hasher};
use std::ops::Deref;
use std::sync::Arc;

struct HashableContext(Arc<Context>);

impl Eq for HashableContext {}

impl PartialEq for HashableContext {
    fn eq(&self, other: &Self) -> bool {
        self.0.get() == other.0.get()
    }
}

impl Hash for HashableContext {
    fn hash<H: Hasher>(&self, state: &mut H) {
        self.0.get().hash(state);
    }
}

const INPUT_TRANSFORMER_SOURCE: &'static str = r##"
kernel void xxx_gate_input_transform(ulong n, ulong input_start, ulong output_start,
        uint word_len, uint input_elem_len, uint output_elem_len, uint bit_mapping_len,
        const global uint* bit_mapping, const global uint* input, global uint* output) {
    const size_t i = get_global_id(0);
    if (i >= n) return;
    const size_t li = get_local_id(0);
    uint ibi;
    local uint local_data[64];
    const uint word_w = word_len >> 5;
    const uint lvi = li >> 5;
    const size_t gidx = i / word_len;
    const uint widx = (i>>5) - gidx * word_w;
    const uint sbit = i & 31;
    const uint input_elem_word_num = input_elem_len >> 5;
    const uint output_group_word_num = output_elem_len * word_w;
    const global uint* input_elem = input + i*input_elem_word_num + input_start;
    global uint* output_group = output + widx + gidx*output_group_word_num + output_start;
    if (li < 64) {
        local_data[li] = 0;
    }
    barrier(CLK_LOCAL_MEM_FENCE);
    for (ibi = 0; ibi < bit_mapping_len; ibi++) {
        const uint inbit = bit_mapping[ibi];
        const uint inbit_val = (input_elem[inbit >> 5] >> (inbit & 31)) & 1;
        atomic_or(&local_data[lvi], (inbit_val << sbit));
        barrier(CLK_LOCAL_MEM_FENCE);
        if (sbit == 0) {
            output_group[word_w*ibi] = local_data[lvi];
            local_data[lvi] = 0;
        }
        barrier(CLK_LOCAL_MEM_FENCE);
    }
}
"##;

#[dynamic]
static mut INPUT_TX_PROGRAMS: HashMap<HashableContext, Arc<Program>> = HashMap::new();

fn get_input_tx_program(context: Arc<Context>) -> Result<Arc<Program>, OpenCLBuildError> {
    let mut tx_programs = INPUT_TX_PROGRAMS.write();
    if let Some(p) = tx_programs.get(&HashableContext(context.clone())) {
        Ok(p.clone())
    } else {
        let p = Arc::new(Program::create_and_build_from_source(
            &context,
            INPUT_TRANSFORMER_SOURCE,
            "",
        )?);
        tx_programs.insert(HashableContext(context.clone()), p.clone());
        Ok(p)
    }
}

/// Input Transformer for OpenCL device.
///
/// This transformer transforms data from external form to internal form.
pub struct OpenCLDataInputTransformer {
    word_len: u32,
    input_elem_len: usize,
    output_elem_len: usize,
    bit_mapping: Arc<Buffer<u32>>,
    bit_mapping_len: usize,
    context: Arc<Context>,
    cmd_queue: Arc<CommandQueue>,
    kernel: Kernel,
    group_len: usize,
}

impl OpenCLDataInputTransformer {
    /// Creates new OpenCL data input transformer. The OpenCL objects passed as context and
    /// command queue as arguments `context` and `cmd_queue`.
    /// `word_len` is length of processor word in bits
    /// and it must be divisible by 32. `input_elem_len` is length of element of input data in
    /// bits and it must be divisible by 32. `output_elem_len` is length of element of output
    /// data (in external form).
    /// `bit_mapping` is list of mapping: index of list is bit of output's element (pack element)
    /// and value is bit of input's (in external form) element.
    pub fn new(
        context: Arc<Context>,
        cmd_queue: Arc<CommandQueue>,
        word_len: u32,
        input_elem_len: usize,
        output_elem_len: usize,
        bit_mapping: &[usize],
    ) -> Result<Self, OpenCLBuildError> {
        assert_eq!((word_len & 31), 0);
        assert_eq!((input_elem_len & 31), 0);
        assert!(input_elem_len >= bit_mapping.iter().copied().max().unwrap());
        assert!(output_elem_len >= bit_mapping.len());
        let device = Device::new(context.devices()[0]);
        let program = get_input_tx_program(context.clone())?;
        let mut buffer = unsafe {
            Buffer::<u32>::create(
                &context,
                CL_MEM_READ_WRITE,
                bit_mapping.len(),
                std::ptr::null_mut(),
            )
            .unwrap()
        };
        let bit_mapping_len = bit_mapping.len();
        unsafe {
            let mut bit_mapping = bit_mapping
                .iter()
                .map(|x| u32::try_from(*x).unwrap())
                .collect::<Vec<_>>();
            cmd_queue.enqueue_write_buffer(
                &mut buffer,
                CL_BLOCKING,
                0,
                &mut bit_mapping[..],
                &[],
            )?;
        }
        let group_len = usize::try_from(device.max_work_group_size().unwrap()).unwrap();
        assert!(group_len >= 32);
        let group_len = std::cmp::min(32 * 64, (group_len >> 5) << 5);
        Ok(Self {
            word_len,
            input_elem_len: ((input_elem_len + 31) >> 5) << 5,
            output_elem_len,
            bit_mapping: Arc::new(buffer),
            bit_mapping_len,
            context,
            cmd_queue,
            group_len,
            kernel: Kernel::create(&program, "xxx_gate_input_transform").unwrap(),
        })
    }

    pub unsafe fn context(&self) -> Arc<Context> {
        self.context.clone()
    }
    pub unsafe fn command_queue(&self) -> Arc<CommandQueue> {
        self.cmd_queue.clone()
    }
}

impl<'a> DataTransformer<'a, OpenCLDataReader<'a>, OpenCLDataWriter<'a>, OpenCLDataHolder>
    for OpenCLDataInputTransformer
{
    type ErrorType = ClError;

    fn transform(&mut self, input: &OpenCLDataHolder) -> Result<OpenCLDataHolder, Self::ErrorType> {
        let mut output = OpenCLDataHolder::new(
            self.output_data_len(input.len()),
            self.context.clone(),
            self.cmd_queue.clone(),
            CL_MEM_READ_WRITE,
        );
        self.transform_reuse(input, &mut output)?;
        Ok(output)
    }

    fn transform_reuse(
        &mut self,
        input: &OpenCLDataHolder,
        output: &mut OpenCLDataHolder,
    ) -> Result<(), Self::ErrorType> {
        assert_eq!(
            input.len() % (((self.word_len as usize) * self.input_elem_len) >> 5),
            0
        );
        assert_eq!(
            output.len() % (((self.word_len as usize) * self.output_elem_len) >> 5),
            0
        );
        let input_elem_word_num = self.input_elem_len >> 5;
        let elem_num = input.len() / input_elem_word_num;
        let num = elem_num;
        let cl_num = cl_ulong::try_from(num).unwrap();
        // println!("ddebug: {} {} {} {}",
        //          elem_num, num, self.word_len_fac1_pow, self.word_len_fac2);
        let cl_input_start = cl_ulong::try_from(input.range.start).unwrap();
        let cl_output_start = cl_ulong::try_from(output.range.start).unwrap();
        let cl_word_len = cl_uint::try_from(self.word_len).unwrap();
        let cl_input_elem_len = cl_uint::try_from(self.input_elem_len).unwrap();
        let cl_output_elem_len = cl_uint::try_from(self.output_elem_len).unwrap();
        let cl_bit_mapping_len = cl_uint::try_from(self.bit_mapping_len).unwrap();
        let output_len = output.len();
        unsafe {
            self.cmd_queue.enqueue_fill_buffer(
                &mut output.buffer,
                &[0u32],
                0,
                4 * output_len,
                &[],
            )?;
        }
        self.cmd_queue.finish().unwrap();
        unsafe {
            ExecuteKernel::new(&self.kernel)
                .set_arg(&cl_num)
                .set_arg(&cl_input_start)
                .set_arg(&cl_output_start)
                .set_arg(&cl_word_len)
                .set_arg(&cl_input_elem_len)
                .set_arg(&cl_output_elem_len)
                .set_arg(&cl_bit_mapping_len)
                .set_arg(self.bit_mapping.deref())
                .set_arg(&input.buffer)
                .set_arg(&output.buffer)
                .set_local_work_size(self.group_len)
                .set_global_work_size(
                    ((num + self.group_len - 1) / self.group_len) * self.group_len,
                )
                .enqueue_nd_range(&self.cmd_queue)?;
        }
        self.cmd_queue.finish()?;
        Ok(())
    }

    fn input_elem_len(&self) -> usize {
        self.input_elem_len
    }
    fn output_elem_len(&self) -> usize {
        self.output_elem_len
    }
}

impl Clone for OpenCLDataInputTransformer {
    fn clone(&self) -> Self {
        let program = get_input_tx_program(self.context.clone()).unwrap();
        Self {
            word_len: self.word_len,
            input_elem_len: self.input_elem_len,
            output_elem_len: self.output_elem_len,
            bit_mapping: self.bit_mapping.clone(),
            bit_mapping_len: self.bit_mapping_len,
            context: self.context.clone(),
            cmd_queue: self.cmd_queue.clone(),
            kernel: Kernel::create(&program, "xxx_gate_input_transform").unwrap(),
            group_len: self.group_len,
        }
    }
}

const OUTPUT_TRANSFORMER_SOURCE: &'static str = r##"
kernel void xxx_gate_output_transform(ulong n, ulong output_start, ulong input_start,
        uint word_len, uint input_elem_len, uint output_elem_len, uint bit_mapping_len,
        const global uint* bit_mapping, const global uint* output, global uint* input) {
    const size_t i = get_global_id(0);
    if (i >= n) return;
    uint ibi;
    const uint word_w = word_len >> 5;
    const size_t gidx = i / word_len;
    const uint widx = (i>>5) - gidx * word_w;
    const uint sbit = i & 31;
    const uint input_elem_word_num = input_elem_len >> 5;
    const uint output_group_word_num = output_elem_len * word_w;
    global uint* input_elem = input + i*input_elem_word_num + input_start;
    const global uint* output_group = output + widx + gidx*output_group_word_num + output_start;
    for (ibi = 0; ibi < bit_mapping_len; ibi++) {
        const uint outbit_val = (output_group[word_w*ibi] >> sbit) & 1;
        const uint inbit = bit_mapping[ibi];
        input_elem[inbit >> 5] |= outbit_val << (inbit & 31);
    }
}
"##;

#[dynamic]
static mut OUTPUT_TX_PROGRAMS: HashMap<HashableContext, Arc<Program>> = HashMap::new();

fn get_output_tx_program(context: Arc<Context>) -> Result<Arc<Program>, OpenCLBuildError> {
    let mut tx_programs = OUTPUT_TX_PROGRAMS.write();
    if let Some(p) = tx_programs.get(&HashableContext(context.clone())) {
        Ok(p.clone())
    } else {
        let p = Arc::new(Program::create_and_build_from_source(
            &context,
            OUTPUT_TRANSFORMER_SOURCE,
            "",
        )?);
        tx_programs.insert(HashableContext(context.clone()), p.clone());
        Ok(p)
    }
}

/// Output Transformer for OpenCL device.
///
/// This transformer transforms data from internal form to external form.
pub struct OpenCLDataOutputTransformer {
    word_len: u32,
    input_elem_len: usize,
    output_elem_len: usize,
    bit_mapping: Arc<Buffer<u32>>,
    bit_mapping_len: usize,
    context: Arc<Context>,
    cmd_queue: Arc<CommandQueue>,
    kernel: Kernel,
    group_len: usize,
}

impl OpenCLDataOutputTransformer {
    // An output_elem_len - number of bits of really single input element.
    // An input_elem_len - number of bits of really single output element.
    // A bit_mapping - index is bit of really input's element,
    //  value is bit of really output's element.

    /// Creates new OpenCL data output transformer. The OpenCL objects passed as context and
    /// command queue as arguments `context` and `cmd_queue`.
    /// `word_len` is length of processor word in bits
    /// and it must be divisible by 32. `input_elem_len` is length of element of input
    /// data (in external form). `output_elem_len` is length of element of output data in
    /// bits and it must be divisible by 32.
    /// `bit_mapping` is list of mapping: index of list is bit of input's element (pack element)
    /// and value is bit of output's (in external form) element.
    pub fn new(
        context: Arc<Context>,
        cmd_queue: Arc<CommandQueue>,
        word_len: u32,
        input_elem_len: usize,
        output_elem_len: usize,
        bit_mapping: &[usize],
    ) -> Result<Self, OpenCLBuildError> {
        let (output_elem_len, input_elem_len) = (input_elem_len, output_elem_len);
        assert_eq!((word_len & 31), 0);
        assert_eq!((input_elem_len & 31), 0);
        assert!(input_elem_len >= bit_mapping.iter().copied().max().unwrap());
        assert!(output_elem_len >= bit_mapping.len());
        let device = Device::new(context.devices()[0]);
        let program = get_output_tx_program(context.clone())?;
        let mut buffer = unsafe {
            Buffer::<u32>::create(
                &context,
                CL_MEM_READ_WRITE,
                bit_mapping.len(),
                std::ptr::null_mut(),
            )
            .unwrap()
        };
        let bit_mapping_len = bit_mapping.len();
        unsafe {
            let mut bit_mapping = bit_mapping
                .iter()
                .map(|x| u32::try_from(*x).unwrap())
                .collect::<Vec<_>>();
            cmd_queue.enqueue_write_buffer(
                &mut buffer,
                CL_BLOCKING,
                0,
                &mut bit_mapping[..],
                &[],
            )?;
        }
        Ok(Self {
            word_len,
            input_elem_len: ((input_elem_len + 31) >> 5) << 5,
            output_elem_len,
            bit_mapping: Arc::new(buffer),
            bit_mapping_len,
            context,
            cmd_queue,
            group_len: usize::try_from(device.max_work_group_size().unwrap()).unwrap(),
            kernel: Kernel::create(&program, "xxx_gate_output_transform").unwrap(),
        })
    }

    pub unsafe fn context(&self) -> Arc<Context> {
        self.context.clone()
    }
    pub unsafe fn command_queue(&self) -> Arc<CommandQueue> {
        self.cmd_queue.clone()
    }
}

impl<'a> DataTransformer<'a, OpenCLDataReader<'a>, OpenCLDataWriter<'a>, OpenCLDataHolder>
    for OpenCLDataOutputTransformer
{
    type ErrorType = ClError;

    fn transform(&mut self, input: &OpenCLDataHolder) -> Result<OpenCLDataHolder, Self::ErrorType> {
        let mut output = OpenCLDataHolder::new(
            self.output_data_len(input.len()),
            self.context.clone(),
            self.cmd_queue.clone(),
            CL_MEM_READ_WRITE,
        );
        self.transform_reuse(input, &mut output)?;
        Ok(output)
    }

    /// changed names of arguments:
    /// output - really input data, input - really output data
    fn transform_reuse(
        &mut self,
        output: &OpenCLDataHolder,
        input: &mut OpenCLDataHolder,
    ) -> Result<(), Self::ErrorType> {
        assert_eq!(
            input.len() % (((self.word_len as usize) * self.input_elem_len) >> 5),
            0
        );
        assert_eq!(
            output.len() % (((self.word_len as usize) * self.output_elem_len) >> 5),
            0
        );
        let input_elem_word_num = self.input_elem_len >> 5;
        let elem_num = input.len() / input_elem_word_num;
        let num = elem_num;
        let cl_num = cl_ulong::try_from(num).unwrap();
        // println!("ddebug: {} {} {} {}",
        //          elem_num, num, self.word_len_fac1_pow, self.word_len_fac2);
        let cl_output_start = cl_ulong::try_from(output.range.start).unwrap();
        let cl_input_start = cl_ulong::try_from(input.range.start).unwrap();
        let cl_word_len = cl_uint::try_from(self.word_len).unwrap();
        let cl_input_elem_len = cl_uint::try_from(self.input_elem_len).unwrap();
        let cl_output_elem_len = cl_uint::try_from(self.output_elem_len).unwrap();
        let cl_bit_mapping_len = cl_uint::try_from(self.bit_mapping_len).unwrap();
        let input_len = input.len();
        unsafe {
            self.cmd_queue.enqueue_fill_buffer(
                &mut input.buffer,
                &[0u32],
                0,
                4 * input_len,
                &[],
            )?;
        }
        self.cmd_queue.finish().unwrap();
        unsafe {
            ExecuteKernel::new(&self.kernel)
                .set_arg(&cl_num)
                .set_arg(&cl_output_start)
                .set_arg(&cl_input_start)
                .set_arg(&cl_word_len)
                .set_arg(&cl_input_elem_len)
                .set_arg(&cl_output_elem_len)
                .set_arg(&cl_bit_mapping_len)
                .set_arg(self.bit_mapping.deref())
                .set_arg(&output.buffer)
                .set_arg(&input.buffer)
                .set_local_work_size(self.group_len)
                .set_global_work_size(
                    ((num + self.group_len - 1) / self.group_len) * self.group_len,
                )
                .enqueue_nd_range(&self.cmd_queue)?;
        }
        self.cmd_queue.finish()?;
        Ok(())
    }

    fn input_elem_len(&self) -> usize {
        self.output_elem_len
    }
    fn output_elem_len(&self) -> usize {
        self.input_elem_len
    }
}

impl Clone for OpenCLDataOutputTransformer {
    fn clone(&self) -> Self {
        let program = get_output_tx_program(self.context.clone()).unwrap();
        Self {
            word_len: self.word_len,
            input_elem_len: self.input_elem_len,
            output_elem_len: self.output_elem_len,
            bit_mapping: self.bit_mapping.clone(),
            bit_mapping_len: self.bit_mapping_len,
            context: self.context.clone(),
            cmd_queue: self.cmd_queue.clone(),
            kernel: Kernel::create(&program, "xxx_gate_output_transform").unwrap(),
            group_len: self.group_len,
        }
    }
}