1use std::ffi::CString;
11use std::marker::PhantomData;
12
13use aocl_sparse_sys as sys;
14use aocl_types::{sealed::Sealed, Complex32, Complex64};
15
16use crate::{check_status, trans_raw, Error, IndexBase, MatDescr, Order, Result, SorType, Trans};
17
18pub trait ComplexScalar: Copy + Sized + Sealed {
20 fn axpyi(alpha: Self, x: &[Self], indx: &[sys::aoclsparse_int], y: &mut [Self]) -> Result<()>;
22
23 fn gthr(y: &[Self], indx: &[sys::aoclsparse_int], x: &mut [Self]) -> Result<()>;
25
26 fn sctr(x: &[Self], indx: &[sys::aoclsparse_int], y: &mut [Self]) -> Result<()>;
28
29 fn create_csr(
31 base: IndexBase,
32 m: usize,
33 n: usize,
34 nnz: usize,
35 row_ptr: *mut sys::aoclsparse_int,
36 col_idx: *mut sys::aoclsparse_int,
37 val: *mut Self,
38 ) -> Result<sys::aoclsparse_matrix>;
39
40 fn export_csr(
42 mat: sys::aoclsparse_matrix,
43 ) -> Result<(
44 IndexBase,
45 usize,
46 usize,
47 usize,
48 *mut sys::aoclsparse_int,
49 *mut sys::aoclsparse_int,
50 *mut Self,
51 )>;
52
53 fn mv(
55 op: Trans,
56 alpha: Self,
57 mat: sys::aoclsparse_matrix,
58 descr: &MatDescr,
59 x: &[Self],
60 beta: Self,
61 y: &mut [Self],
62 ) -> Result<()>;
63
64 fn trsv(
66 op: Trans,
67 alpha: Self,
68 mat: sys::aoclsparse_matrix,
69 descr: &MatDescr,
70 b: &[Self],
71 x: &mut [Self],
72 ) -> Result<()>;
73
74 fn ilu_smoother(
76 op: Trans,
77 a: sys::aoclsparse_matrix,
78 descr: &MatDescr,
79 x: &mut [Self],
80 b: &[Self],
81 ) -> Result<()>;
82
83 fn itsol_init(handle: &mut sys::aoclsparse_itsol_handle) -> Result<()>;
85
86 fn itsol_solve(
88 handle: sys::aoclsparse_itsol_handle,
89 n: usize,
90 mat: sys::aoclsparse_matrix,
91 descr: &MatDescr,
92 b: &[Self],
93 x: &mut [Self],
94 rinfo: &mut [Self::Real; 100],
95 ) -> Result<()>;
96
97 fn csrmm(
100 op: Trans,
101 alpha: Self,
102 a: sys::aoclsparse_matrix,
103 descr: &MatDescr,
104 order: Order,
105 b: &[Self],
106 n: usize,
107 ldb: usize,
108 beta: Self,
109 c: &mut [Self],
110 ldc: usize,
111 ) -> Result<()>;
112
113 fn spmmd(
115 op: Trans,
116 a: sys::aoclsparse_matrix,
117 b: sys::aoclsparse_matrix,
118 layout: Order,
119 c: &mut [Self],
120 ldc: usize,
121 ) -> Result<()>;
122
123 fn sp2md(
125 op_a: Trans,
126 descr_a: &MatDescr,
127 a: sys::aoclsparse_matrix,
128 op_b: Trans,
129 descr_b: &MatDescr,
130 b: sys::aoclsparse_matrix,
131 alpha: Self,
132 beta: Self,
133 c: &mut [Self],
134 layout: Order,
135 ldc: usize,
136 ) -> Result<()>;
137
138 unsafe fn add_ffi(
144 op: sys::aoclsparse_operation,
145 a: sys::aoclsparse_matrix,
146 alpha: Self,
147 b: sys::aoclsparse_matrix,
148 out: *mut sys::aoclsparse_matrix,
149 ) -> sys::aoclsparse_status;
150
151 fn sorv(
153 sor_type: SorType,
154 descr: &MatDescr,
155 a: sys::aoclsparse_matrix,
156 omega: Self,
157 alpha: Self,
158 x: &mut [Self],
159 b: &[Self],
160 ) -> Result<()>;
161
162 type Real: Copy + Default;
166}
167
168impl ComplexScalar for Complex32 {
173 type Real = f32;
174
175 fn axpyi(alpha: Self, x: &[Self], indx: &[sys::aoclsparse_int], y: &mut [Self]) -> Result<()> {
176 let status = unsafe {
177 sys::aoclsparse_caxpyi(
178 x.len() as sys::aoclsparse_int,
179 &alpha as *const _ as *const std::os::raw::c_void,
180 x.as_ptr() as *const std::os::raw::c_void,
181 indx.as_ptr(),
182 y.as_mut_ptr() as *mut std::os::raw::c_void,
183 )
184 };
185 check_status("sparse", status)
186 }
187
188 fn gthr(y: &[Self], indx: &[sys::aoclsparse_int], x: &mut [Self]) -> Result<()> {
189 let status = unsafe {
190 sys::aoclsparse_cgthr(
191 x.len() as sys::aoclsparse_int,
192 y.as_ptr() as *const std::os::raw::c_void,
193 x.as_mut_ptr() as *mut std::os::raw::c_void,
194 indx.as_ptr(),
195 )
196 };
197 check_status("sparse", status)
198 }
199
200 fn sctr(x: &[Self], indx: &[sys::aoclsparse_int], y: &mut [Self]) -> Result<()> {
201 let status = unsafe {
202 sys::aoclsparse_csctr(
203 x.len() as sys::aoclsparse_int,
204 x.as_ptr() as *const std::os::raw::c_void,
205 indx.as_ptr(),
206 y.as_mut_ptr() as *mut std::os::raw::c_void,
207 )
208 };
209 check_status("sparse", status)
210 }
211
212 fn create_csr(
213 base: IndexBase,
214 m: usize,
215 n: usize,
216 nnz: usize,
217 row_ptr: *mut sys::aoclsparse_int,
218 col_idx: *mut sys::aoclsparse_int,
219 val: *mut Self,
220 ) -> Result<sys::aoclsparse_matrix> {
221 let mut raw: sys::aoclsparse_matrix = std::ptr::null_mut();
222 let status = unsafe {
223 sys::aoclsparse_create_ccsr(
224 &mut raw,
225 base.raw_for_complex(),
226 m as sys::aoclsparse_int,
227 n as sys::aoclsparse_int,
228 nnz as sys::aoclsparse_int,
229 row_ptr,
230 col_idx,
231 val as *mut sys::aoclsparse_float_complex,
232 )
233 };
234 check_status("sparse", status)?;
235 if raw.is_null() {
236 return Err(Error::AllocationFailed("sparse"));
237 }
238 Ok(raw)
239 }
240
241 fn export_csr(
242 mat: sys::aoclsparse_matrix,
243 ) -> Result<(
244 IndexBase,
245 usize,
246 usize,
247 usize,
248 *mut sys::aoclsparse_int,
249 *mut sys::aoclsparse_int,
250 *mut Self,
251 )> {
252 let mut base: sys::aoclsparse_index_base = 0;
253 let mut m: sys::aoclsparse_int = 0;
254 let mut n: sys::aoclsparse_int = 0;
255 let mut nnz: sys::aoclsparse_int = 0;
256 let mut row_ptr: *mut sys::aoclsparse_int = std::ptr::null_mut();
257 let mut col_ind: *mut sys::aoclsparse_int = std::ptr::null_mut();
258 let mut val: *mut sys::aoclsparse_float_complex = std::ptr::null_mut();
259 let status = unsafe {
260 sys::aoclsparse_export_ccsr(
261 mat,
262 &mut base,
263 &mut m,
264 &mut n,
265 &mut nnz,
266 &mut row_ptr,
267 &mut col_ind,
268 &mut val,
269 )
270 };
271 check_status("sparse", status)?;
272 let base_e = if base == sys::aoclsparse_index_base__aoclsparse_index_base_one {
273 IndexBase::One
274 } else {
275 IndexBase::Zero
276 };
277 Ok((
278 base_e,
279 m as usize,
280 n as usize,
281 nnz as usize,
282 row_ptr,
283 col_ind,
284 val as *mut Self,
285 ))
286 }
287
288 fn mv(
289 op: Trans,
290 alpha: Self,
291 mat: sys::aoclsparse_matrix,
292 descr: &MatDescr,
293 x: &[Self],
294 beta: Self,
295 y: &mut [Self],
296 ) -> Result<()> {
297 let status = unsafe {
298 sys::aoclsparse_cmv(
299 trans_raw(op),
300 &alpha as *const _ as *const sys::aoclsparse_float_complex,
301 mat,
302 descr.as_raw(),
303 x.as_ptr() as *const sys::aoclsparse_float_complex,
304 &beta as *const _ as *const sys::aoclsparse_float_complex,
305 y.as_mut_ptr() as *mut sys::aoclsparse_float_complex,
306 )
307 };
308 check_status("sparse", status)
309 }
310
311 fn trsv(
312 op: Trans,
313 alpha: Self,
314 mat: sys::aoclsparse_matrix,
315 descr: &MatDescr,
316 b: &[Self],
317 x: &mut [Self],
318 ) -> Result<()> {
319 let alpha_raw = sys::aoclsparse_float_complex {
321 real: alpha.re,
322 imag: alpha.im,
323 };
324 let status = unsafe {
325 sys::aoclsparse_ctrsv(
326 trans_raw(op),
327 alpha_raw,
328 mat,
329 descr.as_raw(),
330 b.as_ptr() as *const sys::aoclsparse_float_complex,
331 x.as_mut_ptr() as *mut sys::aoclsparse_float_complex,
332 )
333 };
334 check_status("sparse", status)
335 }
336
337 fn ilu_smoother(
338 op: Trans,
339 a: sys::aoclsparse_matrix,
340 descr: &MatDescr,
341 x: &mut [Self],
342 b: &[Self],
343 ) -> Result<()> {
344 let mut precond_csr_val: *mut sys::aoclsparse_float_complex = std::ptr::null_mut();
345 let status = unsafe {
346 sys::aoclsparse_cilu_smoother(
347 trans_raw(op),
348 a,
349 descr.as_raw(),
350 &mut precond_csr_val,
351 std::ptr::null(),
352 x.as_mut_ptr() as *mut sys::aoclsparse_float_complex,
353 b.as_ptr() as *const sys::aoclsparse_float_complex,
354 )
355 };
356 check_status("sparse", status)
357 }
358
359 fn itsol_init(handle: &mut sys::aoclsparse_itsol_handle) -> Result<()> {
360 let status = unsafe { sys::aoclsparse_itsol_c_init(handle) };
361 check_status("sparse", status)
362 }
363
364 fn itsol_solve(
365 handle: sys::aoclsparse_itsol_handle,
366 n: usize,
367 mat: sys::aoclsparse_matrix,
368 descr: &MatDescr,
369 b: &[Self],
370 x: &mut [Self],
371 rinfo: &mut [f32; 100],
372 ) -> Result<()> {
373 if b.len() < n || x.len() < n {
374 return Err(Error::InvalidArgument(format!(
375 "itsol_solve: b.len()={}, x.len()={}, n={n}",
376 b.len(),
377 x.len()
378 )));
379 }
380 let status = unsafe {
381 sys::aoclsparse_itsol_c_solve(
382 handle,
383 n as sys::aoclsparse_int,
384 mat,
385 descr.as_raw(),
386 b.as_ptr() as *const sys::aoclsparse_float_complex,
387 x.as_mut_ptr() as *mut sys::aoclsparse_float_complex,
388 rinfo.as_mut_ptr(),
389 None,
390 None,
391 std::ptr::null_mut(),
392 )
393 };
394 check_status("sparse", status)
395 }
396
397 fn csrmm(
398 op: Trans,
399 alpha: Self,
400 a: sys::aoclsparse_matrix,
401 descr: &MatDescr,
402 order: Order,
403 b: &[Self],
404 n: usize,
405 ldb: usize,
406 beta: Self,
407 c: &mut [Self],
408 ldc: usize,
409 ) -> Result<()> {
410 let alpha_raw = sys::aoclsparse_float_complex {
411 real: alpha.re,
412 imag: alpha.im,
413 };
414 let beta_raw = sys::aoclsparse_float_complex {
415 real: beta.re,
416 imag: beta.im,
417 };
418 let status = unsafe {
419 sys::aoclsparse_ccsrmm(
420 trans_raw(op),
421 alpha_raw,
422 a,
423 descr.as_raw(),
424 order.raw(),
425 b.as_ptr() as *const sys::aoclsparse_float_complex,
426 n as sys::aoclsparse_int,
427 ldb as sys::aoclsparse_int,
428 beta_raw,
429 c.as_mut_ptr() as *mut sys::aoclsparse_float_complex,
430 ldc as sys::aoclsparse_int,
431 )
432 };
433 check_status("sparse", status)
434 }
435
436 fn spmmd(
437 op: Trans,
438 a: sys::aoclsparse_matrix,
439 b: sys::aoclsparse_matrix,
440 layout: Order,
441 c: &mut [Self],
442 ldc: usize,
443 ) -> Result<()> {
444 let status = unsafe {
445 sys::aoclsparse_cspmmd(
446 trans_raw(op),
447 a,
448 b,
449 layout.raw(),
450 c.as_mut_ptr() as *mut sys::aoclsparse_float_complex,
451 ldc as sys::aoclsparse_int,
452 )
453 };
454 check_status("sparse", status)
455 }
456
457 fn sp2md(
458 op_a: Trans,
459 descr_a: &MatDescr,
460 a: sys::aoclsparse_matrix,
461 op_b: Trans,
462 descr_b: &MatDescr,
463 b: sys::aoclsparse_matrix,
464 alpha: Self,
465 beta: Self,
466 c: &mut [Self],
467 layout: Order,
468 ldc: usize,
469 ) -> Result<()> {
470 let alpha_raw = sys::aoclsparse_float_complex {
471 real: alpha.re,
472 imag: alpha.im,
473 };
474 let beta_raw = sys::aoclsparse_float_complex {
475 real: beta.re,
476 imag: beta.im,
477 };
478 let status = unsafe {
479 sys::aoclsparse_csp2md(
480 trans_raw(op_a),
481 descr_a.as_raw(),
482 a,
483 trans_raw(op_b),
484 descr_b.as_raw(),
485 b,
486 alpha_raw,
487 beta_raw,
488 c.as_mut_ptr() as *mut sys::aoclsparse_float_complex,
489 layout.raw(),
490 ldc as sys::aoclsparse_int,
491 )
492 };
493 check_status("sparse", status)
494 }
495
496 unsafe fn add_ffi(
497 op: sys::aoclsparse_operation,
498 a: sys::aoclsparse_matrix,
499 alpha: Self,
500 b: sys::aoclsparse_matrix,
501 out: *mut sys::aoclsparse_matrix,
502 ) -> sys::aoclsparse_status {
503 let alpha_raw = sys::aoclsparse_float_complex {
504 real: alpha.re,
505 imag: alpha.im,
506 };
507 sys::aoclsparse_cadd(op, a, alpha_raw, b, out)
508 }
509
510 fn sorv(
511 sor_type: SorType,
512 descr: &MatDescr,
513 a: sys::aoclsparse_matrix,
514 omega: Self,
515 alpha: Self,
516 x: &mut [Self],
517 b: &[Self],
518 ) -> Result<()> {
519 let omega_raw = sys::aoclsparse_float_complex {
520 real: omega.re,
521 imag: omega.im,
522 };
523 let alpha_raw = sys::aoclsparse_float_complex {
524 real: alpha.re,
525 imag: alpha.im,
526 };
527 let status = unsafe {
528 sys::aoclsparse_csorv(
529 sor_type.raw(),
530 descr.as_raw(),
531 a,
532 omega_raw,
533 alpha_raw,
534 x.as_mut_ptr() as *mut sys::aoclsparse_float_complex,
535 b.as_ptr() as *const sys::aoclsparse_float_complex,
536 )
537 };
538 check_status("sparse", status)
539 }
540}
541
542impl ComplexScalar for Complex64 {
547 type Real = f64;
548
549 fn axpyi(alpha: Self, x: &[Self], indx: &[sys::aoclsparse_int], y: &mut [Self]) -> Result<()> {
550 let status = unsafe {
551 sys::aoclsparse_zaxpyi(
552 x.len() as sys::aoclsparse_int,
553 &alpha as *const _ as *const std::os::raw::c_void,
554 x.as_ptr() as *const std::os::raw::c_void,
555 indx.as_ptr(),
556 y.as_mut_ptr() as *mut std::os::raw::c_void,
557 )
558 };
559 check_status("sparse", status)
560 }
561
562 fn gthr(y: &[Self], indx: &[sys::aoclsparse_int], x: &mut [Self]) -> Result<()> {
563 let status = unsafe {
564 sys::aoclsparse_zgthr(
565 x.len() as sys::aoclsparse_int,
566 y.as_ptr() as *const std::os::raw::c_void,
567 x.as_mut_ptr() as *mut std::os::raw::c_void,
568 indx.as_ptr(),
569 )
570 };
571 check_status("sparse", status)
572 }
573
574 fn sctr(x: &[Self], indx: &[sys::aoclsparse_int], y: &mut [Self]) -> Result<()> {
575 let status = unsafe {
576 sys::aoclsparse_zsctr(
577 x.len() as sys::aoclsparse_int,
578 x.as_ptr() as *const std::os::raw::c_void,
579 indx.as_ptr(),
580 y.as_mut_ptr() as *mut std::os::raw::c_void,
581 )
582 };
583 check_status("sparse", status)
584 }
585
586 fn create_csr(
587 base: IndexBase,
588 m: usize,
589 n: usize,
590 nnz: usize,
591 row_ptr: *mut sys::aoclsparse_int,
592 col_idx: *mut sys::aoclsparse_int,
593 val: *mut Self,
594 ) -> Result<sys::aoclsparse_matrix> {
595 let mut raw: sys::aoclsparse_matrix = std::ptr::null_mut();
596 let status = unsafe {
597 sys::aoclsparse_create_zcsr(
598 &mut raw,
599 base.raw_for_complex(),
600 m as sys::aoclsparse_int,
601 n as sys::aoclsparse_int,
602 nnz as sys::aoclsparse_int,
603 row_ptr,
604 col_idx,
605 val as *mut sys::aoclsparse_double_complex,
606 )
607 };
608 check_status("sparse", status)?;
609 if raw.is_null() {
610 return Err(Error::AllocationFailed("sparse"));
611 }
612 Ok(raw)
613 }
614
615 fn export_csr(
616 mat: sys::aoclsparse_matrix,
617 ) -> Result<(
618 IndexBase,
619 usize,
620 usize,
621 usize,
622 *mut sys::aoclsparse_int,
623 *mut sys::aoclsparse_int,
624 *mut Self,
625 )> {
626 let mut base: sys::aoclsparse_index_base = 0;
627 let mut m: sys::aoclsparse_int = 0;
628 let mut n: sys::aoclsparse_int = 0;
629 let mut nnz: sys::aoclsparse_int = 0;
630 let mut row_ptr: *mut sys::aoclsparse_int = std::ptr::null_mut();
631 let mut col_ind: *mut sys::aoclsparse_int = std::ptr::null_mut();
632 let mut val: *mut sys::aoclsparse_double_complex = std::ptr::null_mut();
633 let status = unsafe {
634 sys::aoclsparse_export_zcsr(
635 mat,
636 &mut base,
637 &mut m,
638 &mut n,
639 &mut nnz,
640 &mut row_ptr,
641 &mut col_ind,
642 &mut val,
643 )
644 };
645 check_status("sparse", status)?;
646 let base_e = if base == sys::aoclsparse_index_base__aoclsparse_index_base_one {
647 IndexBase::One
648 } else {
649 IndexBase::Zero
650 };
651 Ok((
652 base_e,
653 m as usize,
654 n as usize,
655 nnz as usize,
656 row_ptr,
657 col_ind,
658 val as *mut Self,
659 ))
660 }
661
662 fn mv(
663 op: Trans,
664 alpha: Self,
665 mat: sys::aoclsparse_matrix,
666 descr: &MatDescr,
667 x: &[Self],
668 beta: Self,
669 y: &mut [Self],
670 ) -> Result<()> {
671 let status = unsafe {
672 sys::aoclsparse_zmv(
673 trans_raw(op),
674 &alpha as *const _ as *const sys::aoclsparse_double_complex,
675 mat,
676 descr.as_raw(),
677 x.as_ptr() as *const sys::aoclsparse_double_complex,
678 &beta as *const _ as *const sys::aoclsparse_double_complex,
679 y.as_mut_ptr() as *mut sys::aoclsparse_double_complex,
680 )
681 };
682 check_status("sparse", status)
683 }
684
685 fn trsv(
686 op: Trans,
687 alpha: Self,
688 mat: sys::aoclsparse_matrix,
689 descr: &MatDescr,
690 b: &[Self],
691 x: &mut [Self],
692 ) -> Result<()> {
693 let alpha_raw = sys::aoclsparse_double_complex_ {
694 real: alpha.re,
695 imag: alpha.im,
696 };
697 let status = unsafe {
698 sys::aoclsparse_ztrsv(
699 trans_raw(op),
700 alpha_raw,
701 mat,
702 descr.as_raw(),
703 b.as_ptr() as *const sys::aoclsparse_double_complex,
704 x.as_mut_ptr() as *mut sys::aoclsparse_double_complex,
705 )
706 };
707 check_status("sparse", status)
708 }
709
710 fn ilu_smoother(
711 op: Trans,
712 a: sys::aoclsparse_matrix,
713 descr: &MatDescr,
714 x: &mut [Self],
715 b: &[Self],
716 ) -> Result<()> {
717 let mut precond_csr_val: *mut sys::aoclsparse_double_complex = std::ptr::null_mut();
718 let status = unsafe {
719 sys::aoclsparse_zilu_smoother(
720 trans_raw(op),
721 a,
722 descr.as_raw(),
723 &mut precond_csr_val,
724 std::ptr::null(),
725 x.as_mut_ptr() as *mut sys::aoclsparse_double_complex,
726 b.as_ptr() as *const sys::aoclsparse_double_complex,
727 )
728 };
729 check_status("sparse", status)
730 }
731
732 fn itsol_init(handle: &mut sys::aoclsparse_itsol_handle) -> Result<()> {
733 let status = unsafe { sys::aoclsparse_itsol_z_init(handle) };
734 check_status("sparse", status)
735 }
736
737 fn itsol_solve(
738 handle: sys::aoclsparse_itsol_handle,
739 n: usize,
740 mat: sys::aoclsparse_matrix,
741 descr: &MatDescr,
742 b: &[Self],
743 x: &mut [Self],
744 rinfo: &mut [f64; 100],
745 ) -> Result<()> {
746 if b.len() < n || x.len() < n {
747 return Err(Error::InvalidArgument(format!(
748 "itsol_solve: b.len()={}, x.len()={}, n={n}",
749 b.len(),
750 x.len()
751 )));
752 }
753 let status = unsafe {
754 sys::aoclsparse_itsol_z_solve(
755 handle,
756 n as sys::aoclsparse_int,
757 mat,
758 descr.as_raw(),
759 b.as_ptr() as *const sys::aoclsparse_double_complex,
760 x.as_mut_ptr() as *mut sys::aoclsparse_double_complex,
761 rinfo.as_mut_ptr(),
762 None,
763 None,
764 std::ptr::null_mut(),
765 )
766 };
767 check_status("sparse", status)
768 }
769
770 fn csrmm(
771 op: Trans,
772 alpha: Self,
773 a: sys::aoclsparse_matrix,
774 descr: &MatDescr,
775 order: Order,
776 b: &[Self],
777 n: usize,
778 ldb: usize,
779 beta: Self,
780 c: &mut [Self],
781 ldc: usize,
782 ) -> Result<()> {
783 let alpha_raw = sys::aoclsparse_double_complex_ {
784 real: alpha.re,
785 imag: alpha.im,
786 };
787 let beta_raw = sys::aoclsparse_double_complex_ {
788 real: beta.re,
789 imag: beta.im,
790 };
791 let status = unsafe {
792 sys::aoclsparse_zcsrmm(
793 trans_raw(op),
794 alpha_raw,
795 a,
796 descr.as_raw(),
797 order.raw(),
798 b.as_ptr() as *const sys::aoclsparse_double_complex,
799 n as sys::aoclsparse_int,
800 ldb as sys::aoclsparse_int,
801 beta_raw,
802 c.as_mut_ptr() as *mut sys::aoclsparse_double_complex,
803 ldc as sys::aoclsparse_int,
804 )
805 };
806 check_status("sparse", status)
807 }
808
809 fn spmmd(
810 op: Trans,
811 a: sys::aoclsparse_matrix,
812 b: sys::aoclsparse_matrix,
813 layout: Order,
814 c: &mut [Self],
815 ldc: usize,
816 ) -> Result<()> {
817 let status = unsafe {
818 sys::aoclsparse_zspmmd(
819 trans_raw(op),
820 a,
821 b,
822 layout.raw(),
823 c.as_mut_ptr() as *mut sys::aoclsparse_double_complex,
824 ldc as sys::aoclsparse_int,
825 )
826 };
827 check_status("sparse", status)
828 }
829
830 fn sp2md(
831 op_a: Trans,
832 descr_a: &MatDescr,
833 a: sys::aoclsparse_matrix,
834 op_b: Trans,
835 descr_b: &MatDescr,
836 b: sys::aoclsparse_matrix,
837 alpha: Self,
838 beta: Self,
839 c: &mut [Self],
840 layout: Order,
841 ldc: usize,
842 ) -> Result<()> {
843 let alpha_raw = sys::aoclsparse_double_complex_ {
844 real: alpha.re,
845 imag: alpha.im,
846 };
847 let beta_raw = sys::aoclsparse_double_complex_ {
848 real: beta.re,
849 imag: beta.im,
850 };
851 let status = unsafe {
852 sys::aoclsparse_zsp2md(
853 trans_raw(op_a),
854 descr_a.as_raw(),
855 a,
856 trans_raw(op_b),
857 descr_b.as_raw(),
858 b,
859 alpha_raw,
860 beta_raw,
861 c.as_mut_ptr() as *mut sys::aoclsparse_double_complex,
862 layout.raw(),
863 ldc as sys::aoclsparse_int,
864 )
865 };
866 check_status("sparse", status)
867 }
868
869 unsafe fn add_ffi(
870 op: sys::aoclsparse_operation,
871 a: sys::aoclsparse_matrix,
872 alpha: Self,
873 b: sys::aoclsparse_matrix,
874 out: *mut sys::aoclsparse_matrix,
875 ) -> sys::aoclsparse_status {
876 let alpha_raw = sys::aoclsparse_double_complex_ {
877 real: alpha.re,
878 imag: alpha.im,
879 };
880 sys::aoclsparse_zadd(op, a, alpha_raw, b, out)
881 }
882
883 fn sorv(
884 sor_type: SorType,
885 descr: &MatDescr,
886 a: sys::aoclsparse_matrix,
887 omega: Self,
888 alpha: Self,
889 x: &mut [Self],
890 b: &[Self],
891 ) -> Result<()> {
892 let omega_raw = sys::aoclsparse_double_complex_ {
893 real: omega.re,
894 imag: omega.im,
895 };
896 let alpha_raw = sys::aoclsparse_double_complex_ {
897 real: alpha.re,
898 imag: alpha.im,
899 };
900 let status = unsafe {
901 sys::aoclsparse_zsorv(
902 sor_type.raw(),
903 descr.as_raw(),
904 a,
905 omega_raw,
906 alpha_raw,
907 x.as_mut_ptr() as *mut sys::aoclsparse_double_complex,
908 b.as_ptr() as *const sys::aoclsparse_double_complex,
909 )
910 };
911 check_status("sparse", status)
912 }
913}
914
915enum CsrStorage<T: ComplexScalar> {
920 Owned {
921 _row_ptr: Vec<sys::aoclsparse_int>,
922 _col_ind: Vec<sys::aoclsparse_int>,
923 _val: Vec<T>,
924 },
925 LibraryOwned,
926}
927
928pub struct ComplexSparseMatrix<T: ComplexScalar> {
930 raw: sys::aoclsparse_matrix,
931 #[allow(
932 dead_code,
933 reason = "owns the buffers the C library keeps pointers into"
934 )]
935 storage: CsrStorage<T>,
936 base: IndexBase,
937 m: usize,
938 n: usize,
939 nnz: usize,
940}
941
942impl<T: ComplexScalar> ComplexSparseMatrix<T> {
943 pub fn from_csr(
945 base: IndexBase,
946 m: usize,
947 n: usize,
948 row_ptr: &[sys::aoclsparse_int],
949 col_ind: &[sys::aoclsparse_int],
950 val: &[T],
951 ) -> Result<Self> {
952 if row_ptr.len() != m + 1 {
953 return Err(Error::InvalidArgument(format!(
954 "complex from_csr: row_ptr length {} != m+1 = {}",
955 row_ptr.len(),
956 m + 1
957 )));
958 }
959 let nnz = val.len();
960 if col_ind.len() != nnz {
961 return Err(Error::InvalidArgument(format!(
962 "complex from_csr: col_ind length {} != val length {nnz}",
963 col_ind.len()
964 )));
965 }
966 let mut row_ptr = row_ptr.to_vec();
967 let mut col_ind = col_ind.to_vec();
968 let mut val = val.to_vec();
969 let raw = T::create_csr(
970 base,
971 m,
972 n,
973 nnz,
974 row_ptr.as_mut_ptr(),
975 col_ind.as_mut_ptr(),
976 val.as_mut_ptr(),
977 )?;
978 Ok(Self {
979 raw,
980 storage: CsrStorage::Owned {
981 _row_ptr: row_ptr,
982 _col_ind: col_ind,
983 _val: val,
984 },
985 base,
986 m,
987 n,
988 nnz,
989 })
990 }
991
992 pub unsafe fn from_library_owned(raw: sys::aoclsparse_matrix) -> Result<Self> {
999 if raw.is_null() {
1000 return Err(Error::AllocationFailed("sparse"));
1001 }
1002 let (base, m, n, nnz, _, _, _) = T::export_csr(raw)?;
1003 Ok(Self {
1004 raw,
1005 storage: CsrStorage::LibraryOwned,
1006 base,
1007 m,
1008 n,
1009 nnz,
1010 })
1011 }
1012
1013 pub fn dims(&self) -> (usize, usize) {
1015 (self.m, self.n)
1016 }
1017 pub fn nnz(&self) -> usize {
1019 self.nnz
1020 }
1021 pub fn base(&self) -> IndexBase {
1023 self.base
1024 }
1025
1026 pub fn as_raw(&self) -> sys::aoclsparse_matrix {
1029 self.raw
1030 }
1031
1032 pub fn export_csr(
1034 &self,
1035 ) -> Result<(
1036 IndexBase,
1037 Vec<sys::aoclsparse_int>,
1038 Vec<sys::aoclsparse_int>,
1039 Vec<T>,
1040 )> {
1041 let (base, m, _, nnz, row_ptr, col_ind, val) = T::export_csr(self.raw)?;
1042 let row_ptr = unsafe { std::slice::from_raw_parts(row_ptr, m + 1).to_vec() };
1043 let col_ind = unsafe { std::slice::from_raw_parts(col_ind, nnz).to_vec() };
1044 let val = unsafe { std::slice::from_raw_parts(val, nnz).to_vec() };
1045 Ok((base, row_ptr, col_ind, val))
1046 }
1047}
1048
1049impl<T: ComplexScalar> Drop for ComplexSparseMatrix<T> {
1050 fn drop(&mut self) {
1051 if !self.raw.is_null() {
1052 unsafe {
1053 let _ = sys::aoclsparse_destroy(&mut self.raw);
1054 }
1055 self.raw = std::ptr::null_mut();
1056 }
1057 }
1058}
1059
1060impl<T: ComplexScalar> std::fmt::Debug for ComplexSparseMatrix<T> {
1061 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
1062 f.debug_struct("ComplexSparseMatrix")
1063 .field("m", &self.m)
1064 .field("n", &self.n)
1065 .field("nnz", &self.nnz)
1066 .field("base", &self.base)
1067 .finish()
1068 }
1069}
1070
1071pub fn axpyi<T: ComplexScalar>(
1077 alpha: T,
1078 x: &[T],
1079 indx: &[sys::aoclsparse_int],
1080 y: &mut [T],
1081) -> Result<()> {
1082 if x.len() != indx.len() {
1083 return Err(Error::InvalidArgument(format!(
1084 "axpyi: x.len()={}, indx.len()={}",
1085 x.len(),
1086 indx.len()
1087 )));
1088 }
1089 crate::check_index_vector("axpyi", indx.len(), indx, y.len())?;
1090 T::axpyi(alpha, x, indx, y)
1091}
1092
1093pub fn gthr<T: ComplexScalar>(y: &[T], indx: &[sys::aoclsparse_int], x: &mut [T]) -> Result<()> {
1095 if x.len() != indx.len() {
1096 return Err(Error::InvalidArgument(format!(
1097 "gthr: x.len()={}, indx.len()={}",
1098 x.len(),
1099 indx.len()
1100 )));
1101 }
1102 crate::check_index_vector("gthr", indx.len(), indx, y.len())?;
1103 T::gthr(y, indx, x)
1104}
1105
1106pub fn sctr<T: ComplexScalar>(x: &[T], indx: &[sys::aoclsparse_int], y: &mut [T]) -> Result<()> {
1108 if x.len() != indx.len() {
1109 return Err(Error::InvalidArgument(format!(
1110 "sctr: x.len()={}, indx.len()={}",
1111 x.len(),
1112 indx.len()
1113 )));
1114 }
1115 crate::check_index_vector("sctr", indx.len(), indx, y.len())?;
1116 T::sctr(x, indx, y)
1117}
1118
1119pub fn mv<T: ComplexScalar>(
1121 op: Trans,
1122 alpha: T,
1123 a: &ComplexSparseMatrix<T>,
1124 descr: &MatDescr,
1125 x: &[T],
1126 beta: T,
1127 y: &mut [T],
1128) -> Result<()> {
1129 let (x_len, y_len) = match op {
1130 Trans::No => (a.n, a.m),
1131 Trans::T | Trans::C => (a.m, a.n),
1132 };
1133 if x.len() < x_len || y.len() < y_len {
1134 return Err(Error::InvalidArgument(format!(
1135 "mv: x.len()={}, y.len()={}, expected ({x_len}, {y_len})",
1136 x.len(),
1137 y.len()
1138 )));
1139 }
1140 T::mv(op, alpha, a.raw, descr, x, beta, y)
1141}
1142
1143pub fn trsv<T: ComplexScalar>(
1145 op: Trans,
1146 alpha: T,
1147 a: &ComplexSparseMatrix<T>,
1148 descr: &MatDescr,
1149 b: &[T],
1150 x: &mut [T],
1151) -> Result<()> {
1152 if b.len() < a.m || x.len() < a.m {
1153 return Err(Error::InvalidArgument(format!(
1154 "trsv: b.len()={}, x.len()={}, m={}",
1155 b.len(),
1156 x.len(),
1157 a.m
1158 )));
1159 }
1160 T::trsv(op, alpha, a.raw, descr, b, x)
1161}
1162
1163pub fn csrmm<T: ComplexScalar>(
1166 op: Trans,
1167 alpha: T,
1168 a: &ComplexSparseMatrix<T>,
1169 descr: &MatDescr,
1170 order: Order,
1171 b: &[T],
1172 n: usize,
1173 ldb: usize,
1174 beta: T,
1175 c: &mut [T],
1176 ldc: usize,
1177) -> Result<()> {
1178 let (ar, ac) = crate::opdims(op, a.m, a.n);
1179 crate::check_dense("csrmm", "B", order, ac, n, ldb, b.len())?;
1180 crate::check_dense("csrmm", "C", order, ar, n, ldc, c.len())?;
1181 T::csrmm(op, alpha, a.as_raw(), descr, order, b, n, ldb, beta, c, ldc)
1182}
1183
1184pub fn spmmd<T: ComplexScalar>(
1187 op: Trans,
1188 a: &ComplexSparseMatrix<T>,
1189 b: &ComplexSparseMatrix<T>,
1190 layout: Order,
1191 c: &mut [T],
1192 ldc: usize,
1193) -> Result<()> {
1194 let (ar, _) = crate::opdims(op, a.m, a.n);
1195 crate::check_dense("spmmd", "C", layout, ar, b.n, ldc, c.len())?;
1196 T::spmmd(op, a.as_raw(), b.as_raw(), layout, c, ldc)
1197}
1198
1199pub fn sp2md<T: ComplexScalar>(
1202 op_a: Trans,
1203 descr_a: &MatDescr,
1204 a: &ComplexSparseMatrix<T>,
1205 op_b: Trans,
1206 descr_b: &MatDescr,
1207 b: &ComplexSparseMatrix<T>,
1208 alpha: T,
1209 beta: T,
1210 c: &mut [T],
1211 layout: Order,
1212 ldc: usize,
1213) -> Result<()> {
1214 let (ar, _) = crate::opdims(op_a, a.m, a.n);
1215 let (_, bc) = crate::opdims(op_b, b.m, b.n);
1216 crate::check_dense("sp2md", "C", layout, ar, bc, ldc, c.len())?;
1217 T::sp2md(
1218 op_a,
1219 descr_a,
1220 a.as_raw(),
1221 op_b,
1222 descr_b,
1223 b.as_raw(),
1224 alpha,
1225 beta,
1226 c,
1227 layout,
1228 ldc,
1229 )
1230}
1231
1232pub fn add<T: ComplexScalar>(
1234 op: Trans,
1235 a: &ComplexSparseMatrix<T>,
1236 alpha: T,
1237 b: &ComplexSparseMatrix<T>,
1238) -> Result<ComplexSparseMatrix<T>> {
1239 let mut c_raw: sys::aoclsparse_matrix = std::ptr::null_mut();
1240 let status = unsafe { T::add_ffi(trans_raw(op), a.as_raw(), alpha, b.as_raw(), &mut c_raw) };
1241 check_status("sparse", status)?;
1242 unsafe { ComplexSparseMatrix::from_library_owned(c_raw) }
1243}
1244
1245pub fn sorv<T: ComplexScalar>(
1248 sor_type: SorType,
1249 descr: &MatDescr,
1250 a: &ComplexSparseMatrix<T>,
1251 omega: T,
1252 alpha: T,
1253 x: &mut [T],
1254 b: &[T],
1255) -> Result<()> {
1256 if x.len() < a.dims().1 || b.len() < a.dims().0 {
1257 return Err(Error::InvalidArgument(format!(
1258 "sorv: x.len()={}, b.len()={}, dims=({}, {})",
1259 x.len(),
1260 b.len(),
1261 a.dims().0,
1262 a.dims().1
1263 )));
1264 }
1265 T::sorv(sor_type, descr, a.as_raw(), omega, alpha, x, b)
1266}
1267
1268pub fn ilu_smoother<T: ComplexScalar>(
1270 op: Trans,
1271 a: &ComplexSparseMatrix<T>,
1272 descr: &MatDescr,
1273 x: &mut [T],
1274 b: &[T],
1275) -> Result<()> {
1276 if x.len() < a.n || b.len() < a.m {
1277 return Err(Error::InvalidArgument(format!(
1278 "ilu_smoother: x.len()={}, b.len()={}, dims=({}, {})",
1279 x.len(),
1280 b.len(),
1281 a.m,
1282 a.n
1283 )));
1284 }
1285 T::ilu_smoother(op, a.raw, descr, x, b)
1286}
1287
1288pub struct ComplexIterSolver<T: ComplexScalar> {
1294 handle: sys::aoclsparse_itsol_handle,
1295 _marker: PhantomData<T>,
1296}
1297
1298impl<T: ComplexScalar> ComplexIterSolver<T> {
1299 pub fn new() -> Result<Self> {
1301 let mut handle: sys::aoclsparse_itsol_handle = std::ptr::null_mut();
1302 T::itsol_init(&mut handle)?;
1303 if handle.is_null() {
1304 return Err(Error::AllocationFailed("sparse"));
1305 }
1306 Ok(Self {
1307 handle,
1308 _marker: PhantomData,
1309 })
1310 }
1311
1312 pub fn set_option(&mut self, name: &str, value: &str) -> Result<()> {
1314 let c_name = CString::new(name)
1315 .map_err(|_| Error::InvalidArgument("set_option: name has interior NUL".into()))?;
1316 let c_value = CString::new(value)
1317 .map_err(|_| Error::InvalidArgument("set_option: value has interior NUL".into()))?;
1318 let status = unsafe {
1319 sys::aoclsparse_itsol_option_set(self.handle, c_name.as_ptr(), c_value.as_ptr())
1320 };
1321 check_status("sparse", status)
1322 }
1323
1324 pub fn solve(
1326 &mut self,
1327 mat: &ComplexSparseMatrix<T>,
1328 descr: &MatDescr,
1329 b: &[T],
1330 x: &mut [T],
1331 ) -> Result<Box<[T::Real; 100]>> {
1332 let n = mat.n;
1333 if mat.m != mat.n {
1334 return Err(Error::InvalidArgument(format!(
1335 "iterative solve requires square matrix; got ({}, {})",
1336 mat.m, mat.n
1337 )));
1338 }
1339 let mut rinfo: Box<[T::Real; 100]> = Box::new([T::Real::default(); 100]);
1340 T::itsol_solve(self.handle, n, mat.raw, descr, b, x, &mut rinfo)?;
1341 Ok(rinfo)
1342 }
1343}
1344
1345impl<T: ComplexScalar> Drop for ComplexIterSolver<T> {
1346 fn drop(&mut self) {
1347 if !self.handle.is_null() {
1348 unsafe {
1349 sys::aoclsparse_itsol_destroy(&mut self.handle);
1350 }
1351 self.handle = std::ptr::null_mut();
1352 }
1353 }
1354}
1355
1356impl<T: ComplexScalar> std::fmt::Debug for ComplexIterSolver<T> {
1357 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
1358 f.debug_struct("ComplexIterSolver").finish_non_exhaustive()
1359 }
1360}
1361
1362pub fn symgs_c64(
1372 op: Trans,
1373 a: &ComplexSparseMatrix<Complex64>,
1374 descr: &MatDescr,
1375 alpha: Complex64,
1376 b: &[Complex64],
1377 x: &mut [Complex64],
1378) -> Result<()> {
1379 crate::check_len("symgs_c64", "b", b.len(), a.m)?;
1380 crate::check_len("symgs_c64", "x", x.len(), a.n)?;
1381 let alpha_raw = sys::aoclsparse_double_complex_ {
1382 real: alpha.re,
1383 imag: alpha.im,
1384 };
1385 let status = unsafe {
1386 sys::aoclsparse_zsymgs(
1387 trans_raw(op),
1388 a.as_raw(),
1389 descr.as_raw(),
1390 alpha_raw,
1391 b.as_ptr() as *const sys::aoclsparse_double_complex,
1392 x.as_mut_ptr() as *mut sys::aoclsparse_double_complex,
1393 )
1394 };
1395 check_status("sparse", status)
1396}
1397
1398pub fn symgs_c32(
1400 op: Trans,
1401 a: &ComplexSparseMatrix<Complex32>,
1402 descr: &MatDescr,
1403 alpha: Complex32,
1404 b: &[Complex32],
1405 x: &mut [Complex32],
1406) -> Result<()> {
1407 crate::check_len("symgs_c32", "b", b.len(), a.m)?;
1408 crate::check_len("symgs_c32", "x", x.len(), a.n)?;
1409 let alpha_raw = sys::aoclsparse_float_complex {
1410 real: alpha.re,
1411 imag: alpha.im,
1412 };
1413 let status = unsafe {
1414 sys::aoclsparse_csymgs(
1415 trans_raw(op),
1416 a.as_raw(),
1417 descr.as_raw(),
1418 alpha_raw,
1419 b.as_ptr() as *const sys::aoclsparse_float_complex,
1420 x.as_mut_ptr() as *mut sys::aoclsparse_float_complex,
1421 )
1422 };
1423 check_status("sparse", status)
1424}
1425
1426pub fn symgs_mv_c64(
1428 op: Trans,
1429 a: &ComplexSparseMatrix<Complex64>,
1430 descr: &MatDescr,
1431 alpha: Complex64,
1432 b: &[Complex64],
1433 x: &mut [Complex64],
1434 y: &mut [Complex64],
1435) -> Result<()> {
1436 crate::check_len("symgs_mv_c64", "b", b.len(), a.m)?;
1437 crate::check_len("symgs_mv_c64", "x", x.len(), a.n)?;
1438 crate::check_len("symgs_mv_c64", "y", y.len(), a.m)?;
1439 let alpha_raw = sys::aoclsparse_double_complex_ {
1440 real: alpha.re,
1441 imag: alpha.im,
1442 };
1443 let status = unsafe {
1444 sys::aoclsparse_zsymgs_mv(
1445 trans_raw(op),
1446 a.as_raw(),
1447 descr.as_raw(),
1448 alpha_raw,
1449 b.as_ptr() as *const sys::aoclsparse_double_complex,
1450 x.as_mut_ptr() as *mut sys::aoclsparse_double_complex,
1451 y.as_mut_ptr() as *mut sys::aoclsparse_double_complex,
1452 )
1453 };
1454 check_status("sparse", status)
1455}
1456
1457pub fn symgs_mv_c32(
1459 op: Trans,
1460 a: &ComplexSparseMatrix<Complex32>,
1461 descr: &MatDescr,
1462 alpha: Complex32,
1463 b: &[Complex32],
1464 x: &mut [Complex32],
1465 y: &mut [Complex32],
1466) -> Result<()> {
1467 crate::check_len("symgs_mv_c32", "b", b.len(), a.m)?;
1468 crate::check_len("symgs_mv_c32", "x", x.len(), a.n)?;
1469 crate::check_len("symgs_mv_c32", "y", y.len(), a.m)?;
1470 let alpha_raw = sys::aoclsparse_float_complex {
1471 real: alpha.re,
1472 imag: alpha.im,
1473 };
1474 let status = unsafe {
1475 sys::aoclsparse_csymgs_mv(
1476 trans_raw(op),
1477 a.as_raw(),
1478 descr.as_raw(),
1479 alpha_raw,
1480 b.as_ptr() as *const sys::aoclsparse_float_complex,
1481 x.as_mut_ptr() as *mut sys::aoclsparse_float_complex,
1482 y.as_mut_ptr() as *mut sys::aoclsparse_float_complex,
1483 )
1484 };
1485 check_status("sparse", status)
1486}
1487
1488pub fn set_value_c64(
1492 a: &mut ComplexSparseMatrix<Complex64>,
1493 row_idx: i32,
1494 col_idx: i32,
1495 val: Complex64,
1496) -> Result<()> {
1497 let val_raw = sys::aoclsparse_double_complex_ {
1498 real: val.re,
1499 imag: val.im,
1500 };
1501 let status = unsafe {
1502 sys::aoclsparse_zset_value(
1503 a.as_raw(),
1504 row_idx as sys::aoclsparse_int,
1505 col_idx as sys::aoclsparse_int,
1506 val_raw,
1507 )
1508 };
1509 check_status("sparse", status)
1510}
1511pub fn set_value_c32(
1513 a: &mut ComplexSparseMatrix<Complex32>,
1514 row_idx: i32,
1515 col_idx: i32,
1516 val: Complex32,
1517) -> Result<()> {
1518 let val_raw = sys::aoclsparse_float_complex {
1519 real: val.re,
1520 imag: val.im,
1521 };
1522 let status = unsafe {
1523 sys::aoclsparse_cset_value(
1524 a.as_raw(),
1525 row_idx as sys::aoclsparse_int,
1526 col_idx as sys::aoclsparse_int,
1527 val_raw,
1528 )
1529 };
1530 check_status("sparse", status)
1531}
1532
1533pub fn update_values_c64(
1535 a: &mut ComplexSparseMatrix<Complex64>,
1536 val: &mut [Complex64],
1537) -> Result<()> {
1538 let status = unsafe {
1539 sys::aoclsparse_zupdate_values(
1540 a.as_raw(),
1541 val.len() as sys::aoclsparse_int,
1542 val.as_mut_ptr() as *mut sys::aoclsparse_double_complex,
1543 )
1544 };
1545 check_status("sparse", status)
1546}
1547pub fn update_values_c32(
1549 a: &mut ComplexSparseMatrix<Complex32>,
1550 val: &mut [Complex32],
1551) -> Result<()> {
1552 let status = unsafe {
1553 sys::aoclsparse_cupdate_values(
1554 a.as_raw(),
1555 val.len() as sys::aoclsparse_int,
1556 val.as_mut_ptr() as *mut sys::aoclsparse_float_complex,
1557 )
1558 };
1559 check_status("sparse", status)
1560}
1561
1562pub fn trsm_c64(
1567 op: Trans,
1568 alpha: Complex64,
1569 a: &ComplexSparseMatrix<Complex64>,
1570 descr: &MatDescr,
1571 b: &[Complex64],
1572 n_rhs: usize,
1573 ldb: usize,
1574 x: &mut [Complex64],
1575 ldx: usize,
1576) -> Result<()> {
1577 crate::check_dense("trsm", "B", Order::RowMajor, a.m, n_rhs, ldb, b.len())?;
1578 crate::check_dense("trsm", "X", Order::RowMajor, a.m, n_rhs, ldx, x.len())?;
1579 let alpha_raw = sys::aoclsparse_double_complex_ {
1580 real: alpha.re,
1581 imag: alpha.im,
1582 };
1583 let status = unsafe {
1584 sys::aoclsparse_ztrsm(
1585 trans_raw(op),
1586 alpha_raw,
1587 a.as_raw(),
1588 descr.as_raw(),
1589 sys::aoclsparse_order__aoclsparse_order_row,
1590 b.as_ptr() as *const sys::aoclsparse_double_complex,
1591 n_rhs as sys::aoclsparse_int,
1592 ldb as sys::aoclsparse_int,
1593 x.as_mut_ptr() as *mut sys::aoclsparse_double_complex,
1594 ldx as sys::aoclsparse_int,
1595 )
1596 };
1597 check_status("sparse", status)
1598}
1599
1600pub fn trsm_c32(
1602 op: Trans,
1603 alpha: Complex32,
1604 a: &ComplexSparseMatrix<Complex32>,
1605 descr: &MatDescr,
1606 b: &[Complex32],
1607 n_rhs: usize,
1608 ldb: usize,
1609 x: &mut [Complex32],
1610 ldx: usize,
1611) -> Result<()> {
1612 crate::check_dense("trsm", "B", Order::RowMajor, a.m, n_rhs, ldb, b.len())?;
1613 crate::check_dense("trsm", "X", Order::RowMajor, a.m, n_rhs, ldx, x.len())?;
1614 let alpha_raw = sys::aoclsparse_float_complex {
1615 real: alpha.re,
1616 imag: alpha.im,
1617 };
1618 let status = unsafe {
1619 sys::aoclsparse_ctrsm(
1620 trans_raw(op),
1621 alpha_raw,
1622 a.as_raw(),
1623 descr.as_raw(),
1624 sys::aoclsparse_order__aoclsparse_order_row,
1625 b.as_ptr() as *const sys::aoclsparse_float_complex,
1626 n_rhs as sys::aoclsparse_int,
1627 ldb as sys::aoclsparse_int,
1628 x.as_mut_ptr() as *mut sys::aoclsparse_float_complex,
1629 ldx as sys::aoclsparse_int,
1630 )
1631 };
1632 check_status("sparse", status)
1633}
1634
1635pub fn dotmv_c64(
1639 op: Trans,
1640 alpha: Complex64,
1641 a: &ComplexSparseMatrix<Complex64>,
1642 descr: &MatDescr,
1643 x: &[Complex64],
1644 beta: Complex64,
1645 y: &mut [Complex64],
1646 d: &mut Complex64,
1647) -> Result<()> {
1648 let (xl, yl) = crate::mv_lens(op, a.m, a.n);
1649 crate::check_len("dotmv_c64", "x", x.len(), xl)?;
1650 crate::check_len("dotmv_c64", "y", y.len(), yl)?;
1651 let alpha_raw = sys::aoclsparse_double_complex_ {
1652 real: alpha.re,
1653 imag: alpha.im,
1654 };
1655 let beta_raw = sys::aoclsparse_double_complex_ {
1656 real: beta.re,
1657 imag: beta.im,
1658 };
1659 let status = unsafe {
1660 sys::aoclsparse_zdotmv(
1661 trans_raw(op),
1662 alpha_raw,
1663 a.as_raw(),
1664 descr.as_raw(),
1665 x.as_ptr() as *const sys::aoclsparse_double_complex,
1666 beta_raw,
1667 y.as_mut_ptr() as *mut sys::aoclsparse_double_complex,
1668 d as *mut _ as *mut sys::aoclsparse_double_complex,
1669 )
1670 };
1671 check_status("sparse", status)
1672}
1673
1674pub fn dotmv_c32(
1676 op: Trans,
1677 alpha: Complex32,
1678 a: &ComplexSparseMatrix<Complex32>,
1679 descr: &MatDescr,
1680 x: &[Complex32],
1681 beta: Complex32,
1682 y: &mut [Complex32],
1683 d: &mut Complex32,
1684) -> Result<()> {
1685 let (xl, yl) = crate::mv_lens(op, a.m, a.n);
1686 crate::check_len("dotmv_c32", "x", x.len(), xl)?;
1687 crate::check_len("dotmv_c32", "y", y.len(), yl)?;
1688 let alpha_raw = sys::aoclsparse_float_complex {
1689 real: alpha.re,
1690 imag: alpha.im,
1691 };
1692 let beta_raw = sys::aoclsparse_float_complex {
1693 real: beta.re,
1694 imag: beta.im,
1695 };
1696 let status = unsafe {
1697 sys::aoclsparse_cdotmv(
1698 trans_raw(op),
1699 alpha_raw,
1700 a.as_raw(),
1701 descr.as_raw(),
1702 x.as_ptr() as *const sys::aoclsparse_float_complex,
1703 beta_raw,
1704 y.as_mut_ptr() as *mut sys::aoclsparse_float_complex,
1705 d as *mut _ as *mut sys::aoclsparse_float_complex,
1706 )
1707 };
1708 check_status("sparse", status)
1709}
1710
1711pub fn dotui_c64(
1713 x: &[Complex64],
1714 indx: &[sys::aoclsparse_int],
1715 y: &[Complex64],
1716) -> Result<Complex64> {
1717 crate::check_index_vector("dotui_c64", x.len(), indx, y.len())?;
1718 let mut out = Complex64::new(0.0, 0.0);
1719 let status = unsafe {
1720 sys::aoclsparse_zdotui(
1721 x.len() as sys::aoclsparse_int,
1722 x.as_ptr() as *const std::os::raw::c_void,
1723 indx.as_ptr(),
1724 y.as_ptr() as *const std::os::raw::c_void,
1725 &mut out as *mut _ as *mut std::os::raw::c_void,
1726 )
1727 };
1728 check_status("sparse", status)?;
1729 Ok(out)
1730}
1731pub fn dotui_c32(
1733 x: &[Complex32],
1734 indx: &[sys::aoclsparse_int],
1735 y: &[Complex32],
1736) -> Result<Complex32> {
1737 crate::check_index_vector("dotui_c32", x.len(), indx, y.len())?;
1738 let mut out = Complex32::new(0.0, 0.0);
1739 let status = unsafe {
1740 sys::aoclsparse_cdotui(
1741 x.len() as sys::aoclsparse_int,
1742 x.as_ptr() as *const std::os::raw::c_void,
1743 indx.as_ptr(),
1744 y.as_ptr() as *const std::os::raw::c_void,
1745 &mut out as *mut _ as *mut std::os::raw::c_void,
1746 )
1747 };
1748 check_status("sparse", status)?;
1749 Ok(out)
1750}
1751
1752pub fn dotci_c64(
1754 x: &[Complex64],
1755 indx: &[sys::aoclsparse_int],
1756 y: &[Complex64],
1757) -> Result<Complex64> {
1758 crate::check_index_vector("dotci_c64", x.len(), indx, y.len())?;
1759 let mut out = Complex64::new(0.0, 0.0);
1760 let status = unsafe {
1761 sys::aoclsparse_zdotci(
1762 x.len() as sys::aoclsparse_int,
1763 x.as_ptr() as *const std::os::raw::c_void,
1764 indx.as_ptr(),
1765 y.as_ptr() as *const std::os::raw::c_void,
1766 &mut out as *mut _ as *mut std::os::raw::c_void,
1767 )
1768 };
1769 check_status("sparse", status)?;
1770 Ok(out)
1771}
1772pub fn dotci_c32(
1774 x: &[Complex32],
1775 indx: &[sys::aoclsparse_int],
1776 y: &[Complex32],
1777) -> Result<Complex32> {
1778 crate::check_index_vector("dotci_c32", x.len(), indx, y.len())?;
1779 let mut out = Complex32::new(0.0, 0.0);
1780 let status = unsafe {
1781 sys::aoclsparse_cdotci(
1782 x.len() as sys::aoclsparse_int,
1783 x.as_ptr() as *const std::os::raw::c_void,
1784 indx.as_ptr(),
1785 y.as_ptr() as *const std::os::raw::c_void,
1786 &mut out as *mut _ as *mut std::os::raw::c_void,
1787 )
1788 };
1789 check_status("sparse", status)?;
1790 Ok(out)
1791}
1792
1793pub fn gthrs_c64(y: &[Complex64], x: &mut [Complex64], stride: i32) -> Result<()> {
1797 crate::check_stride("gthrs_c64", x.len(), stride, y.len())?;
1798 let status = unsafe {
1799 sys::aoclsparse_zgthrs(
1800 x.len() as sys::aoclsparse_int,
1801 y.as_ptr() as *const std::os::raw::c_void,
1802 x.as_mut_ptr() as *mut std::os::raw::c_void,
1803 stride as sys::aoclsparse_int,
1804 )
1805 };
1806 check_status("sparse", status)
1807}
1808pub fn gthrs_c32(y: &[Complex32], x: &mut [Complex32], stride: i32) -> Result<()> {
1810 crate::check_stride("gthrs_c32", x.len(), stride, y.len())?;
1811 let status = unsafe {
1812 sys::aoclsparse_cgthrs(
1813 x.len() as sys::aoclsparse_int,
1814 y.as_ptr() as *const std::os::raw::c_void,
1815 x.as_mut_ptr() as *mut std::os::raw::c_void,
1816 stride as sys::aoclsparse_int,
1817 )
1818 };
1819 check_status("sparse", status)
1820}
1821
1822pub fn gthrz_c64(
1824 y: &mut [Complex64],
1825 indx: &[sys::aoclsparse_int],
1826 x: &mut [Complex64],
1827) -> Result<()> {
1828 crate::check_index_vector("gthrz_c64", x.len(), indx, y.len())?;
1829 let status = unsafe {
1830 sys::aoclsparse_zgthrz(
1831 x.len() as sys::aoclsparse_int,
1832 y.as_mut_ptr() as *mut std::os::raw::c_void,
1833 x.as_mut_ptr() as *mut std::os::raw::c_void,
1834 indx.as_ptr(),
1835 )
1836 };
1837 check_status("sparse", status)
1838}
1839pub fn gthrz_c32(
1841 y: &mut [Complex32],
1842 indx: &[sys::aoclsparse_int],
1843 x: &mut [Complex32],
1844) -> Result<()> {
1845 crate::check_index_vector("gthrz_c32", x.len(), indx, y.len())?;
1846 let status = unsafe {
1847 sys::aoclsparse_cgthrz(
1848 x.len() as sys::aoclsparse_int,
1849 y.as_mut_ptr() as *mut std::os::raw::c_void,
1850 x.as_mut_ptr() as *mut std::os::raw::c_void,
1851 indx.as_ptr(),
1852 )
1853 };
1854 check_status("sparse", status)
1855}
1856
1857pub fn sctrs_c64(x: &[Complex64], y: &mut [Complex64], stride: i32) -> Result<()> {
1859 crate::check_stride("sctrs_c64", x.len(), stride, y.len())?;
1860 let status = unsafe {
1861 sys::aoclsparse_zsctrs(
1862 x.len() as sys::aoclsparse_int,
1863 x.as_ptr() as *const std::os::raw::c_void,
1864 stride as sys::aoclsparse_int,
1865 y.as_mut_ptr() as *mut std::os::raw::c_void,
1866 )
1867 };
1868 check_status("sparse", status)
1869}
1870pub fn sctrs_c32(x: &[Complex32], y: &mut [Complex32], stride: i32) -> Result<()> {
1872 crate::check_stride("sctrs_c32", x.len(), stride, y.len())?;
1873 let status = unsafe {
1874 sys::aoclsparse_csctrs(
1875 x.len() as sys::aoclsparse_int,
1876 x.as_ptr() as *const std::os::raw::c_void,
1877 stride as sys::aoclsparse_int,
1878 y.as_mut_ptr() as *mut std::os::raw::c_void,
1879 )
1880 };
1881 check_status("sparse", status)
1882}
1883
1884pub fn syrkd_c64(
1888 op_a: Trans,
1889 a: &ComplexSparseMatrix<Complex64>,
1890 alpha: Complex64,
1891 beta: Complex64,
1892 c: &mut [Complex64],
1893 order_c: Order,
1894 ldc: usize,
1895) -> Result<()> {
1896 let (r, _) = crate::opdims(op_a, a.m, a.n);
1897 crate::check_dense("syrkd", "C", order_c, r, r, ldc, c.len())?;
1898 let alpha_raw = sys::aoclsparse_double_complex_ {
1899 real: alpha.re,
1900 imag: alpha.im,
1901 };
1902 let beta_raw = sys::aoclsparse_double_complex_ {
1903 real: beta.re,
1904 imag: beta.im,
1905 };
1906 let status = unsafe {
1907 sys::aoclsparse_zsyrkd(
1908 trans_raw(op_a),
1909 a.as_raw(),
1910 alpha_raw,
1911 beta_raw,
1912 c.as_mut_ptr() as *mut sys::aoclsparse_double_complex,
1913 order_c.raw(),
1914 ldc as sys::aoclsparse_int,
1915 )
1916 };
1917 check_status("sparse", status)
1918}
1919pub fn syrkd_c32(
1921 op_a: Trans,
1922 a: &ComplexSparseMatrix<Complex32>,
1923 alpha: Complex32,
1924 beta: Complex32,
1925 c: &mut [Complex32],
1926 order_c: Order,
1927 ldc: usize,
1928) -> Result<()> {
1929 let (r, _) = crate::opdims(op_a, a.m, a.n);
1930 crate::check_dense("syrkd", "C", order_c, r, r, ldc, c.len())?;
1931 let alpha_raw = sys::aoclsparse_float_complex {
1932 real: alpha.re,
1933 imag: alpha.im,
1934 };
1935 let beta_raw = sys::aoclsparse_float_complex {
1936 real: beta.re,
1937 imag: beta.im,
1938 };
1939 let status = unsafe {
1940 sys::aoclsparse_csyrkd(
1941 trans_raw(op_a),
1942 a.as_raw(),
1943 alpha_raw,
1944 beta_raw,
1945 c.as_mut_ptr() as *mut sys::aoclsparse_float_complex,
1946 order_c.raw(),
1947 ldc as sys::aoclsparse_int,
1948 )
1949 };
1950 check_status("sparse", status)
1951}
1952
1953pub fn syprd_c64(
1956 op_a: Trans,
1957 a: &ComplexSparseMatrix<Complex64>,
1958 b: &[Complex64],
1959 order_b: Order,
1960 ldb: usize,
1961 alpha: Complex64,
1962 beta: Complex64,
1963 c: &mut [Complex64],
1964 order_c: Order,
1965 ldc: usize,
1966) -> Result<()> {
1967 let (bd, cd) = if op_a == Trans::No {
1968 (a.n, a.m)
1969 } else {
1970 (a.m, a.n)
1971 };
1972 crate::check_dense("syprd", "B", order_b, bd, bd, ldb, b.len())?;
1973 crate::check_dense("syprd", "C", order_c, cd, cd, ldc, c.len())?;
1974 let alpha_raw = sys::aoclsparse_double_complex_ {
1975 real: alpha.re,
1976 imag: alpha.im,
1977 };
1978 let beta_raw = sys::aoclsparse_double_complex_ {
1979 real: beta.re,
1980 imag: beta.im,
1981 };
1982 let status = unsafe {
1983 sys::aoclsparse_zsyprd(
1984 trans_raw(op_a),
1985 a.as_raw(),
1986 b.as_ptr() as *const sys::aoclsparse_double_complex,
1987 order_b.raw(),
1988 ldb as sys::aoclsparse_int,
1989 alpha_raw,
1990 beta_raw,
1991 c.as_mut_ptr() as *mut sys::aoclsparse_double_complex,
1992 order_c.raw(),
1993 ldc as sys::aoclsparse_int,
1994 )
1995 };
1996 check_status("sparse", status)
1997}
1998pub fn syprd_c32(
2000 op_a: Trans,
2001 a: &ComplexSparseMatrix<Complex32>,
2002 b: &[Complex32],
2003 order_b: Order,
2004 ldb: usize,
2005 alpha: Complex32,
2006 beta: Complex32,
2007 c: &mut [Complex32],
2008 order_c: Order,
2009 ldc: usize,
2010) -> Result<()> {
2011 let (bd, cd) = if op_a == Trans::No {
2012 (a.n, a.m)
2013 } else {
2014 (a.m, a.n)
2015 };
2016 crate::check_dense("syprd", "B", order_b, bd, bd, ldb, b.len())?;
2017 crate::check_dense("syprd", "C", order_c, cd, cd, ldc, c.len())?;
2018 let alpha_raw = sys::aoclsparse_float_complex {
2019 real: alpha.re,
2020 imag: alpha.im,
2021 };
2022 let beta_raw = sys::aoclsparse_float_complex {
2023 real: beta.re,
2024 imag: beta.im,
2025 };
2026 let status = unsafe {
2027 sys::aoclsparse_csyprd(
2028 trans_raw(op_a),
2029 a.as_raw(),
2030 b.as_ptr() as *const sys::aoclsparse_float_complex,
2031 order_b.raw(),
2032 ldb as sys::aoclsparse_int,
2033 alpha_raw,
2034 beta_raw,
2035 c.as_mut_ptr() as *mut sys::aoclsparse_float_complex,
2036 order_c.raw(),
2037 ldc as sys::aoclsparse_int,
2038 )
2039 };
2040 check_status("sparse", status)
2041}
2042
2043pub fn create_csc_c64(
2047 base: IndexBase,
2048 m: usize,
2049 n: usize,
2050 nnz: usize,
2051 col_ptr: &mut [sys::aoclsparse_int],
2052 row_idx: &mut [sys::aoclsparse_int],
2053 val: &mut [Complex64],
2054) -> Result<sys::aoclsparse_matrix> {
2055 let mut raw: sys::aoclsparse_matrix = std::ptr::null_mut();
2056 let status = unsafe {
2057 sys::aoclsparse_create_zcsc(
2058 &mut raw,
2059 base.raw_for_complex(),
2060 m as sys::aoclsparse_int,
2061 n as sys::aoclsparse_int,
2062 nnz as sys::aoclsparse_int,
2063 col_ptr.as_mut_ptr(),
2064 row_idx.as_mut_ptr(),
2065 val.as_mut_ptr() as *mut sys::aoclsparse_double_complex,
2066 )
2067 };
2068 check_status("sparse", status)?;
2069 if raw.is_null() {
2070 return Err(Error::AllocationFailed("sparse"));
2071 }
2072 Ok(raw)
2073}
2074pub fn create_csc_c32(
2076 base: IndexBase,
2077 m: usize,
2078 n: usize,
2079 nnz: usize,
2080 col_ptr: &mut [sys::aoclsparse_int],
2081 row_idx: &mut [sys::aoclsparse_int],
2082 val: &mut [Complex32],
2083) -> Result<sys::aoclsparse_matrix> {
2084 let mut raw: sys::aoclsparse_matrix = std::ptr::null_mut();
2085 let status = unsafe {
2086 sys::aoclsparse_create_ccsc(
2087 &mut raw,
2088 base.raw_for_complex(),
2089 m as sys::aoclsparse_int,
2090 n as sys::aoclsparse_int,
2091 nnz as sys::aoclsparse_int,
2092 col_ptr.as_mut_ptr(),
2093 row_idx.as_mut_ptr(),
2094 val.as_mut_ptr() as *mut sys::aoclsparse_float_complex,
2095 )
2096 };
2097 check_status("sparse", status)?;
2098 if raw.is_null() {
2099 return Err(Error::AllocationFailed("sparse"));
2100 }
2101 Ok(raw)
2102}
2103
2104pub fn create_tcsr_c64(
2107 base: IndexBase,
2108 m: usize,
2109 n: usize,
2110 nnz: usize,
2111 row_ptr_l: &mut [sys::aoclsparse_int],
2112 row_ptr_u: &mut [sys::aoclsparse_int],
2113 col_idx_l: &mut [sys::aoclsparse_int],
2114 col_idx_u: &mut [sys::aoclsparse_int],
2115 val_l: &mut [Complex64],
2116 val_u: &mut [Complex64],
2117) -> Result<sys::aoclsparse_matrix> {
2118 let mut raw: sys::aoclsparse_matrix = std::ptr::null_mut();
2119 let status = unsafe {
2120 sys::aoclsparse_create_ztcsr(
2121 &mut raw,
2122 base.raw_for_complex(),
2123 m as sys::aoclsparse_int,
2124 n as sys::aoclsparse_int,
2125 nnz as sys::aoclsparse_int,
2126 row_ptr_l.as_mut_ptr(),
2127 row_ptr_u.as_mut_ptr(),
2128 col_idx_l.as_mut_ptr(),
2129 col_idx_u.as_mut_ptr(),
2130 val_l.as_mut_ptr() as *mut sys::aoclsparse_double_complex,
2131 val_u.as_mut_ptr() as *mut sys::aoclsparse_double_complex,
2132 )
2133 };
2134 check_status("sparse", status)?;
2135 if raw.is_null() {
2136 return Err(Error::AllocationFailed("sparse"));
2137 }
2138 Ok(raw)
2139}
2140pub fn create_tcsr_c32(
2142 base: IndexBase,
2143 m: usize,
2144 n: usize,
2145 nnz: usize,
2146 row_ptr_l: &mut [sys::aoclsparse_int],
2147 row_ptr_u: &mut [sys::aoclsparse_int],
2148 col_idx_l: &mut [sys::aoclsparse_int],
2149 col_idx_u: &mut [sys::aoclsparse_int],
2150 val_l: &mut [Complex32],
2151 val_u: &mut [Complex32],
2152) -> Result<sys::aoclsparse_matrix> {
2153 let mut raw: sys::aoclsparse_matrix = std::ptr::null_mut();
2154 let status = unsafe {
2155 sys::aoclsparse_create_ctcsr(
2156 &mut raw,
2157 base.raw_for_complex(),
2158 m as sys::aoclsparse_int,
2159 n as sys::aoclsparse_int,
2160 nnz as sys::aoclsparse_int,
2161 row_ptr_l.as_mut_ptr(),
2162 row_ptr_u.as_mut_ptr(),
2163 col_idx_l.as_mut_ptr(),
2164 col_idx_u.as_mut_ptr(),
2165 val_l.as_mut_ptr() as *mut sys::aoclsparse_float_complex,
2166 val_u.as_mut_ptr() as *mut sys::aoclsparse_float_complex,
2167 )
2168 };
2169 check_status("sparse", status)?;
2170 if raw.is_null() {
2171 return Err(Error::AllocationFailed("sparse"));
2172 }
2173 Ok(raw)
2174}
2175
2176impl IndexBase {
2179 pub(crate) fn raw_for_complex(self) -> sys::aoclsparse_index_base {
2180 match self {
2181 IndexBase::Zero => sys::aoclsparse_index_base__aoclsparse_index_base_zero,
2182 IndexBase::One => sys::aoclsparse_index_base__aoclsparse_index_base_one,
2183 }
2184 }
2185}
2186
2187#[cfg(test)]
2188mod tests {
2189 use super::*;
2190
2191 #[test]
2192 fn axpyi_c64_round_trip() {
2193 let mut y = [
2196 Complex64::new(1.0, 0.0),
2197 Complex64::new(2.0, 0.0),
2198 Complex64::new(3.0, 0.0),
2199 Complex64::new(4.0, 0.0),
2200 ];
2201 let x = [Complex64::new(10.0, 1.0), Complex64::new(20.0, 2.0)];
2202 let indx: [sys::aoclsparse_int; 2] = [0, 2];
2203 axpyi(Complex64::new(3.0, 0.0), &x, &indx, &mut y).unwrap();
2204 assert!((y[0].re - 31.0).abs() < 1e-12);
2205 assert!((y[0].im - 3.0).abs() < 1e-12);
2206 assert!((y[2].re - 63.0).abs() < 1e-12);
2207 assert!((y[2].im - 6.0).abs() < 1e-12);
2208 assert!((y[1].re - 2.0).abs() < 1e-12);
2209 assert!((y[3].re - 4.0).abs() < 1e-12);
2210 }
2211
2212 #[test]
2213 fn complex_sparse_matrix_round_trip_c64() {
2214 let val = [Complex64::new(1.0, 0.0), Complex64::new(1.0, 0.0)];
2216 let col: [sys::aoclsparse_int; 2] = [0, 1];
2217 let rp: [sys::aoclsparse_int; 3] = [0, 1, 2];
2218 let mat =
2219 ComplexSparseMatrix::<Complex64>::from_csr(IndexBase::Zero, 2, 2, &rp, &col, &val)
2220 .unwrap();
2221 assert_eq!(mat.dims(), (2, 2));
2222 assert_eq!(mat.nnz(), 2);
2223 let (base, rp2, col2, val2) = mat.export_csr().unwrap();
2224 assert_eq!(base, IndexBase::Zero);
2225 assert_eq!(rp2, [0, 1, 2]);
2226 assert_eq!(col2, [0, 1]);
2227 assert!((val2[0].re - 1.0).abs() < 1e-12);
2228 }
2229
2230 #[test]
2231 fn complex_mv_identity_c64() {
2232 let val = [Complex64::new(1.0, 0.0), Complex64::new(1.0, 0.0)];
2234 let col: [sys::aoclsparse_int; 2] = [0, 1];
2235 let rp: [sys::aoclsparse_int; 3] = [0, 1, 2];
2236 let mat =
2237 ComplexSparseMatrix::<Complex64>::from_csr(IndexBase::Zero, 2, 2, &rp, &col, &val)
2238 .unwrap();
2239 let descr = MatDescr::new().unwrap();
2240 let x = [Complex64::new(3.0, 1.0), Complex64::new(4.0, -2.0)];
2241 let mut y = [Complex64::new(0.0, 0.0); 2];
2242 mv(
2243 Trans::No,
2244 Complex64::new(1.0, 0.0),
2245 &mat,
2246 &descr,
2247 &x,
2248 Complex64::new(0.0, 0.0),
2249 &mut y,
2250 )
2251 .unwrap();
2252 assert!((y[0].re - 3.0).abs() < 1e-12);
2253 assert!((y[0].im - 1.0).abs() < 1e-12);
2254 assert!((y[1].re - 4.0).abs() < 1e-12);
2255 assert!((y[1].im + 2.0).abs() < 1e-12);
2256 }
2257
2258 #[test]
2259 fn complex_mv_identity_c32() {
2260 let val = [Complex32::new(1.0, 0.0), Complex32::new(1.0, 0.0)];
2261 let col: [sys::aoclsparse_int; 2] = [0, 1];
2262 let rp: [sys::aoclsparse_int; 3] = [0, 1, 2];
2263 let mat =
2264 ComplexSparseMatrix::<Complex32>::from_csr(IndexBase::Zero, 2, 2, &rp, &col, &val)
2265 .unwrap();
2266 let descr = MatDescr::new().unwrap();
2267 let x = [Complex32::new(3.0, 1.0), Complex32::new(4.0, -2.0)];
2268 let mut y = [Complex32::new(0.0, 0.0); 2];
2269 mv(
2270 Trans::No,
2271 Complex32::new(1.0, 0.0),
2272 &mat,
2273 &descr,
2274 &x,
2275 Complex32::new(0.0, 0.0),
2276 &mut y,
2277 )
2278 .unwrap();
2279 assert!((y[0].re - 3.0).abs() < 1e-6);
2280 assert!((y[0].im - 1.0).abs() < 1e-6);
2281 assert!((y[1].re - 4.0).abs() < 1e-6);
2282 assert!((y[1].im + 2.0).abs() < 1e-6);
2283 }
2284
2285 #[test]
2286 fn complex_csrmm_identity_c64() {
2287 let val = [Complex64::new(1.0, 0.0), Complex64::new(1.0, 0.0)];
2289 let col: [sys::aoclsparse_int; 2] = [0, 1];
2290 let rp: [sys::aoclsparse_int; 3] = [0, 1, 2];
2291 let a = ComplexSparseMatrix::<Complex64>::from_csr(IndexBase::Zero, 2, 2, &rp, &col, &val)
2292 .unwrap();
2293 let descr = MatDescr::new().unwrap();
2294 let b = [
2298 Complex64::new(1.0, 1.0),
2299 Complex64::new(2.0, -1.0),
2300 Complex64::new(3.0, 0.0),
2301 Complex64::new(4.0, 2.0),
2302 ];
2303 let mut c = [Complex64::new(0.0, 0.0); 4];
2304 crate::complex::csrmm(
2305 Trans::No,
2306 Complex64::new(1.0, 0.0),
2307 &a,
2308 &descr,
2309 Order::RowMajor,
2310 &b,
2311 2,
2312 2,
2313 Complex64::new(0.0, 0.0),
2314 &mut c,
2315 2,
2316 )
2317 .unwrap();
2318 for (got, want) in c.iter().zip(b.iter()) {
2319 assert!((got.re - want.re).abs() < 1e-12);
2320 assert!((got.im - want.im).abs() < 1e-12);
2321 }
2322 }
2323
2324 #[test]
2325 fn complex_add_identity_plus_identity_c64() {
2326 let val = [Complex64::new(1.0, 0.0), Complex64::new(1.0, 0.0)];
2327 let col: [sys::aoclsparse_int; 2] = [0, 1];
2328 let rp: [sys::aoclsparse_int; 3] = [0, 1, 2];
2329 let a = ComplexSparseMatrix::<Complex64>::from_csr(IndexBase::Zero, 2, 2, &rp, &col, &val)
2330 .unwrap();
2331 let b = ComplexSparseMatrix::<Complex64>::from_csr(IndexBase::Zero, 2, 2, &rp, &col, &val)
2332 .unwrap();
2333 let c = crate::complex::add(Trans::No, &a, Complex64::new(1.0, 0.0), &b).unwrap();
2334 let (_, _, _, val_c) = c.export_csr().unwrap();
2335 assert_eq!(val_c.len(), 2);
2336 for v in &val_c {
2337 assert!((v.re - 2.0).abs() < 1e-12);
2338 assert!(v.im.abs() < 1e-12);
2339 }
2340 }
2341}