crseo 2.5.3

Cuda Engined Optics Rust Interface
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
use ffi::{gpu_double, gpu_float, gpu_int};
use std::ops::{AddAssign, Mul, SubAssign};

pub type Single = gpu_float;
pub type Double = gpu_double;
pub type Int = gpu_int;

pub trait CuType {
    fn new() -> Self;
    fn build(&mut self, size: i32);
    fn malloc(&mut self);
    fn assign_f32(&mut self, _ptr: *const f32) {}
    fn assign_f64(&mut self, _ptr: *mut f64) {}
    fn assign_i32(&mut self, _ptr: *mut i32) {}
    fn drop(&mut self);
}
impl CuType for Double {
    fn new() -> Self {
        Default::default()
    }
    fn build(&mut self, size: i32) {
        unsafe {
            self.setup1(size);
        }
    }
    fn malloc(&mut self) {
        unsafe {
            self.dev_malloc();
        }
    }
    fn assign_f64(&mut self, ptr: *mut f64) {
        self.dev_data = ptr;
    }
    fn drop(&mut self) {
        unsafe {
            self.free_dev();
        }
    }
}
impl CuType for Int {
    fn new() -> Self {
        Default::default()
    }
    fn build(&mut self, size: i32) {
        unsafe {
            self.setup1(size);
        }
    }
    fn malloc(&mut self) {
        unsafe {
            self.dev_malloc();
        }
    }
    fn assign_i32(&mut self, ptr: *mut i32) {
        self.dev_data = ptr;
    }
    fn drop(&mut self) {
        unsafe {
            self.free_dev();
        }
    }
}
impl CuType for Single {
    fn new() -> Self {
        Default::default()
    }
    fn build(&mut self, size: i32) {
        unsafe {
            self.setup1(size);
        }
    }
    fn malloc(&mut self) {
        unsafe {
            self.dev_malloc();
        }
    }
    fn assign_f32(&mut self, ptr: *const f32) {
        self.dev_data = ptr as *mut _;
    }
    fn drop(&mut self) {
        unsafe {
            self.free_dev();
        }
    }
}
#[derive(Default)]
pub struct Cu<T: CuType> {
    _c_: T,
    /// number of rows
    pub n_rows: usize,
    /// number of columns
    pub n_cols: usize,
    dev_alloc: bool,
}

impl<T: CuType> Cu<T> {
    /// Creates an empty CUDA array
    pub fn new() -> Cu<T> {
        Cu {
            _c_: CuType::new(),
            n_rows: 0,
            n_cols: 0,
            dev_alloc: false,
        }
    }
    pub fn array(n_rows: usize, n_cols: usize) -> Cu<T> {
        Cu {
            _c_: CuType::new(),
            n_rows,
            n_cols,
            dev_alloc: false,
        }
    }
    pub fn vector(n_rows: usize) -> Cu<T> {
        Cu {
            _c_: CuType::new(),
            n_rows,
            n_cols: 1,
            dev_alloc: false,
        }
    }
    pub fn size(&self) -> usize {
        self.n_rows * self.n_cols
    }
    pub fn n_rows(&self) -> usize {
        self.n_rows
    }
    pub fn n_cols(&self) -> usize {
        self.n_cols
    }
    pub fn is_empty(&self) -> bool {
        self.size() == 0
    }
    pub fn malloc(&mut self) -> &mut Self {
        let s = self.size();
        self._c_.build(s as i32);
        self._c_.malloc();
        self.dev_alloc = true;
        self
    }
}
impl Cu<Int> {
    pub fn from_ptr(&mut self, ptr: *mut i32) {
        let s = self.size();
        self._c_.build(s as i32);
        self._c_.assign_i32(ptr);
    }
}
impl Cu<Single> {
    pub fn from_ptr(&mut self, ptr: *mut f32) {
        let s = self.size();
        self._c_.build(s as i32);
        self._c_.assign_f32(ptr);
    }
    pub fn as_ptr(&self) -> *const f32 {
        self._c_.dev_data
    }
    pub fn as_mut_ptr(&mut self) -> *mut f32 {
        self._c_.dev_data
    }
    pub fn to_dev(&mut self, host_data: &[f32]) -> &mut Self {
        if !self.dev_alloc {
            self.malloc();
        }
        unsafe {
            self._c_.host_data = host_data.as_ptr() as *mut f32;
            self._c_.host2dev();
        }
        self
    }
    pub fn from_dev(&mut self) -> Vec<f32> {
        let mut v = vec![0f32; self.size()];
        unsafe {
            self._c_.host_data = v.as_mut_ptr();
            self._c_.dev2host();
        }
        v
    }
    pub fn mv(&self, x: &Cu<Single>) -> Cu<Single> {
        assert_eq!(x.n_cols(), 1, "x must be a vector (n_col=n=1)!");
        assert_eq!(
            x.size(),
            self.n_cols,
            "the number of columns ({}) do not match the length ({})of x",
            self.n_cols,
            x.size()
        );
        let mut y = Cu::<Single>::vector(self.n_rows);
        y.malloc();
        unsafe {
            let mut s = self._c_;
            s.mv(&mut y._c_, &x._c_);
        }
        y
    }
    pub fn mv_unchecked(&self, y: &mut Cu<Single>, x: &Cu<Single>) {
        unsafe {
            let mut s = self._c_;
            s.mv(&mut y._c_, &x._c_);
        }
    }
    pub fn qr(&mut self) -> &mut Self {
        unsafe {
            self._c_.qr(self.n_rows as i32);
        }
        self
    }
    pub fn qr_solve(&mut self, b: &mut Cu<Single>) -> Cu<Single> {
        let mut x = Cu::<Single>::vector(self.n_cols);
        x.malloc();
        unsafe {
            self._c_.qr_solve(&mut x._c_, &mut b._c_);
        }
        x
    }
    pub fn qr_solve_as_ptr(&mut self, x: &mut Cu<Single>, b: &mut Cu<Single>) {
        unsafe {
            self._c_.qr_solve(&mut x._c_, &mut b._c_);
        }
    }
}
impl Mul for &Cu<Single> {
    type Output = Cu<Single>;

    fn mul(self, rhs: Self) -> Cu<Single> {
        self.mv(rhs)
    }
}
impl Mul<f32> for &Cu<Single> {
    type Output = Cu<Single>;

    fn mul(self, rhs: f32) -> Cu<Single> {
        let mut other = self.clone();
        unsafe {
            other._c_.scale(rhs);
        }
        other
    }
}
impl AddAssign for Cu<Single> {
    fn add_assign(&mut self, other: Self) {
        unsafe {
            self._c_.axpy(&other._c_, 1.0);
        }
    }
}
impl SubAssign for Cu<Single> {
    fn sub_assign(&mut self, other: Self) {
        unsafe {
            self._c_.axpy(&other._c_, -1.0);
        }
    }
}
impl Clone for Cu<Single> {
    fn clone(&self) -> Self {
        let mut s = self._c_;
        let mut other = Cu::<Single>::array(self.n_rows, self.n_cols);
        other.malloc();
        unsafe {
            s.dev2dev(&mut other._c_);
        }
        other
    }
}
impl<T: CuType> Drop for Cu<T> {
    fn drop(&mut self) {
        if self.dev_alloc {
            self._c_.drop();
        }
        self.dev_alloc = false;
    }
}
impl From<Vec<f32>> for Cu<Single> {
    fn from(item: Vec<f32>) -> Self {
        let mut this = Cu::<Single>::vector(item.len());
        this.to_dev(&mut item.clone());
        this
    }
}
impl From<&mut [f32]> for Cu<Single> {
    fn from(item: &mut [f32]) -> Self {
        let mut this = Cu::<Single>::vector(item.len());
        this.to_dev(item);
        this
    }
}
impl From<&[f32]> for Cu<Single> {
    fn from(item: &[f32]) -> Self {
        let mut this = Cu::<Single>::vector(item.len());
        this.to_dev(item);
        this
    }
}
impl From<&[&Cu<Single>]> for Cu<Single> {
    fn from(item: &[&Cu<Single>]) -> Self {
        let this = Cu::<Single>::vector(item.len());
        this.mv(item[0]);
        this
    }
}
impl From<Vec<f64>> for Cu<Single> {
    fn from(item: Vec<f64>) -> Self {
        let mut this = Cu::<Single>::vector(item.len());
        this.to_dev(&mut item.into_iter().map(|x| x as f32).collect::<Vec<f32>>());
        this
    }
}
impl From<&[f64]> for Cu<Single> {
    fn from(item: &[f64]) -> Self {
        let mut this = Cu::<Single>::vector(item.len());
        this.to_dev(&mut item.into_iter().map(|x| *x as f32).collect::<Vec<f32>>());
        this
    }
}
impl From<Vec<Vec<f32>>> for Cu<Single> {
    fn from(item: Vec<Vec<f32>>) -> Self {
        let n_cols = item.len();
        let n_rows = item[0].len();
        let mut flat_item: Vec<f32> = item.iter().cloned().flatten().collect();
        let mut this = Cu::<Single>::array(n_rows, n_cols);
        this.to_dev(&mut flat_item);
        this
    }
}

impl From<Cu<Single>> for Vec<f32> {
    fn from(item: Cu<Single>) -> Self {
        let mut q = item;
        q.from_dev()
    }
}
impl From<Cu<Single>> for Vec<f64> {
    fn from(item: Cu<Single>) -> Self {
        let mut q = item;
        q.from_dev().into_iter().map(|x| x as f64).collect()
    }
}

impl From<&mut Cu<Single>> for Vec<f32> {
    fn from(item: &mut Cu<Single>) -> Self {
        item.from_dev()
    }
}

impl Cu<Int> {
    pub fn from_dev(&mut self) -> Vec<i32> {
        let mut v = vec![0i32; self.size()];
        unsafe {
            self._c_.host_data = v.as_mut_ptr();
            self._c_.dev2host();
        }
        v
    }
}
impl Cu<Double> {
    pub fn from_dev(&mut self) -> Vec<f64> {
        let mut v = vec![0f64; self.size()];
        unsafe {
            self._c_.host_data = v.as_mut_ptr();
            self._c_.dev2host();
        }
        v
    }
    pub fn to_dev(&mut self, host_data: &mut [f64]) -> &mut Self {
        if !self.dev_alloc {
            self.malloc();
        }
        unsafe {
            self._c_.host_data = host_data.as_mut_ptr();
            self._c_.host2dev();
        }
        self
    }
    pub fn as_ptr(&mut self) -> *mut f64 {
        self._c_.dev_data
    }
    pub fn as_mut_ptr(&mut self) -> *mut f64 {
        self._c_.dev_data
    }
}
impl From<Vec<f64>> for Cu<Double> {
    fn from(item: Vec<f64>) -> Self {
        let mut this = Cu::<Double>::vector(item.len());
        this.to_dev(&mut item.clone());
        this
    }
}
impl From<Vec<Vec<f64>>> for Cu<Double> {
    fn from(item: Vec<Vec<f64>>) -> Self {
        let n_cols = item.len();
        let n_rows = item[0].len();
        let mut flat_item: Vec<f64> = item.iter().cloned().flatten().collect();
        let mut this = Cu::<Double>::array(n_rows, n_cols);
        this.to_dev(&mut flat_item);
        this
    }
}
impl From<Cu<Double>> for Vec<f64> {
    fn from(item: Cu<Double>) -> Self {
        let mut q = item;
        q.from_dev()
    }
}
impl From<Cu<Int>> for Vec<i32> {
    fn from(item: Cu<Int>) -> Self {
        let mut q = item;
        q.from_dev()
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn cu_todev() {
        let mut d = Cu::<Single>::vector(5);
        let mut v = vec![1f32; 5];
        d.malloc();
        d.to_dev(&mut v.as_mut_slice());
    }

    #[test]
    fn cu_fromdev() {
        let mut d = Cu::<Single>::vector(5);
        let mut v = vec![1f32; 5];
        d.malloc().to_dev(&mut v.as_mut_slice());
        let w = d.from_dev();
        println!("w: {:?}", w);
    }

    #[test]
    fn cu_toptr() {
        let mut d = Cu::<Single>::vector(5);
        let mut v = vec![4.321f32; 5];
        d.malloc().to_dev(&mut v.as_mut_slice());
        let w = d.from_dev();
        println!("w: {:?}", w);
        let mut dd = Cu::<Single>::vector(5);
        dd.from_ptr(d.as_mut_ptr());
        let w = dd.from_dev();
        println!("w: {:?}", w);
    }

    #[test]
    fn cu_from_into() {
        let d_v = Cu::<Single>::from(vec![1f32; 7]);
        let u: Vec<f32> = d_v.into();
        println!("u: {:?}", u);
        assert_eq!(u, vec![1f32; 7]);
    }

    #[test]
    fn cu_into_from() {
        let v = vec![1f32; 7];
        let d_v: Cu<Single> = v.into();
        let u = Vec::<f32>::from(d_v);
        assert_eq!(u, vec![1f32; 7]);
    }
}