BLAS/PP.pm untested

filter: Statementstmt 0.0% 0 / 309 Branchbran 0.0% 0 / 132 Conditioncond 0.0% 0 / 28 MC/DCmcdc 0.0% 0 / 18 Subroutinesub 0.0% 0 / 25 Podpod 0.0% 0 / 19 totaltotal 0.0% 0 / 531 CC 66 SCAR 42.1
CC
66
Cov
0%
gemm_d
182.0
axpby_d
110.0
waxpby_d
110.0
CRAP 67.3
1 ## Math/BLAS/PP.pm --- basic linear algebra subroutines.
2  
3 # Copyright (C) 2010 Ralph Schleicher. All rights reserved.
4  
5 # This program is free software; you can redistribute it and/or
6 # modify it under the same terms as Perl itself.
7  
8 ## Code:
9  
10 package Math::BLAS::PP;
11  
12 use strict;
13 use warnings;
14 use Carp;
15 use Exporter qw(import);
16 use POSIX qw(:float_h);
17  
18 use Math::BLAS::Enum;
19  
20 BEGIN
21 {
22 our $VERSION = '1.02';
23 our @EXPORT = qw(dot_d
24 norm_d
25 sum_d
26 min_val_d
27 amin_val_d
28 max_val_d
29 amax_val_d
30 sumsq_d
31 scale_d
32 rscale_d
33 axpby_d
34 waxpby_d
35 copy_d
36 swap_d
37 gemv_d
38 gemm_d);
39 }
40  
41 sub EBUG () { 'Should not happen' }
42 sub EINVAL () { 'Invalid argument' }
43  
44 # Machine parameters.
45 my $SMALL = 1 / DBL_MAX < DBL_MIN ? DBL_MIN : (1 + DBL_EPSILON) / DBL_MAX;
46 my $LARGE = 1 / $SMALL;
47  
48 ## Reduction operations.
49  
50 # Dot product.
51 sub dot_d ($$$$$$$$$$;$)
52 {
53 # Arguments.
54 my ($n, $alpha, $x, $x_ind, $x_incr, $y, $y_ind, $y_incr, $beta, $r, $r_ind) = @_;
55  
56 # Return quickly.
57 return undef if $n < 0;
58  
59 # Code.
60 my $dot = 0;
61  
62 if ($alpha != 0)
63 {
64 foreach (1 .. $n)
65 {
66 $dot += $$x[$x_ind] * $$y[$y_ind];
67  
68 $x_ind += $x_incr;
69 $y_ind += $y_incr;
70 }
71  
72 $dot *= $alpha
73 if $alpha != 1;
74 }
75  
76 if ($beta != 0)
77 {
78 my $r_val = ref ($r) ? $$r[$r_ind] : $r;
79  
80 $r_val *= $beta
81 if $beta != 1;
82  
83 $dot += $r_val;
84 }
85  
86 # Update scalar.
87 $$r[$r_ind] = $dot
88 if ref ($r);
89  
90 # Return value.
91 $dot;
92 }
93  
94 # Vector norms.
95 sub norm_d ($$$$$)
96 {
97 # Arguments.
98 my ($norm, $n, $x, $x_ind, $x_incr) = @_;
99  
100 # Return quickly.
101 return 0 if $n <= 0;
102  
103 # Code.
104 if ($norm == BLAS_ONE_NORM)
105 {
106 $norm = 0;
107  
108 foreach (1 .. $n)
109 {
110 $norm += abs ($$x[$x_ind]);
111  
112 $x_ind += $x_incr;
113 }
114 }
115 elsif ($norm == BLAS_TWO_NORM || $norm == BLAS_FROBENIUS_NORM)
116 {
117 my ($sumsq, $scale) = sumsq_d ($n, $x, $x_ind, $x_incr);
118  
119 $norm = $scale * sqrt ($sumsq);
120 }
121 elsif ($norm == BLAS_INF_NORM)
122 {
123 (undef, $norm) = amax_val_d ($n, $x, $x_ind, $x_incr);
124 }
125 else
126 {
127 croak (EINVAL);
128 }
129  
130 # Return value.
131 $norm;
132 }
133  
134 # Sum.
135 sub sum_d ($$$$)
136 {
137 # Arguments.
138 my ($n, $x, $x_ind, $x_incr) = @_;
139  
140 # Return quickly.
141 return 0 if $n <= 0;
142  
143 # Code.
144 my $sum = 0;
145  
146 foreach (1 .. $n)
147 {
148 $sum += $$x[$x_ind];
149  
150 $x_ind += $x_incr;
151 }
152  
153 # Return value.
154 $sum;
155 }
156  
157 # Minimum value.
158 sub min_val_d ($$$$)
159 {
160 # Arguments.
161 my ($n, $x, $x_ind, $x_incr) = @_;
162  
163 # Return quickly.
164 return (undef, 0) if $n <= 0;
165  
166 # Code.
167 my $offs = 0;
168 my $val = $$x[$x_ind];
169 my $tem;
170  
171 foreach (1 .. $n - 1)
172 {
173 $x_ind += $x_incr;
174  
175 $tem = $$x[$x_ind];
176 next unless $tem < $val;
177  
178 $offs = $_;
179 $val = $tem;
180 }
181  
182 # Return values.
183 ($offs, $val);
184 }
185  
186 # Minimum absolute value.
187 sub amin_val_d ($$$$)
188 {
189 # Arguments.
190 my ($n, $x, $x_ind, $x_incr) = @_;
191  
192 # Return quickly.
193 return (undef, 0) if $n <= 0;
194  
195 # Code.
196 my $offs = 0;
197 my $val = abs ($$x[$x_ind]);
198 my $tem;
199  
200 foreach (1 .. $n - 1)
201 {
202 $x_ind += $x_incr;
203  
204 $tem = abs ($$x[$x_ind]);
205 next unless $tem < $val;
206  
207 $offs = $_;
208 $val = $tem;
209 }
210  
211 # Return values.
212 ($offs, $val);
213 }
214  
215 # Maximum value.
216 sub max_val_d ($$$$)
217 {
218 # Arguments.
219 my ($n, $x, $x_ind, $x_incr) = @_;
220  
221 # Return quickly.
222 return (undef, 0) if $n <= 0;
223  
224 # Code.
225 my $offs = 0;
226 my $val = $$x[$x_ind];
227 my $tem;
228  
229 foreach (1 .. $n - 1)
230 {
231 $x_ind += $x_incr;
232  
233 $tem = $$x[$x_ind];
234 next unless $tem > $val;
235  
236 $offs = $_;
237 $val = $tem;
238 }
239  
240 # Return values.
241 ($offs, $val);
242 }
243  
244 # Maximum absolute value.
245 sub amax_val_d ($$$$)
246 {
247 # Arguments.
248 my ($n, $x, $x_ind, $x_incr) = @_;
249  
250 # Return quickly.
251 return (undef, 0) if $n <= 0;
252  
253 # Code.
254 my $offs = 0;
255 my $val = abs ($$x[$x_ind]);
256 my $tem;
257  
258 foreach (1 .. $n - 1)
259 {
260 $x_ind += $x_incr;
261  
262 $tem = abs ($$x[$x_ind]);
263 next unless $tem > $val;
264  
265 $offs = $_;
266 $val = $tem;
267 }
268  
269 # Return values.
270 ($offs, $val);
271 }
272  
273 # Sum of squares.
274 sub sumsq_d ($$$$;$$)
275 {
276 # Arguments.
277 my ($n, $x, $x_ind, $x_incr, $sumsq, $scale) = @_;
278  
279 # Code.
280 if ($n > 0)
281 {
282 my $tem;
283  
284 $sumsq //= 0;
285 $scale //= 1;
286  
287 foreach (1 .. $n)
288 {
289 if ($$x[$x_ind] != 0)
290 {
291 $tem = abs ($$x[$x_ind]);
292 if ($scale < $tem)
293 {
294 $sumsq *= ($scale / $tem) ** 2;
295 $sumsq += 1;
296  
297 $scale = $tem;
298 }
299 else
300 {
301 $sumsq += ($tem / $scale) ** 2;
302 }
303 }
304  
305 $x_ind += $x_incr;
306 }
307 }
308  
309 # Return values.
310 ($sumsq, $scale);
311 }
312  
313 ## Vector operations.
314  
315 # Scale.
316 sub scale_d ($$$$$)
317 {
318 # Arguments.
319 my ($n, $alpha, $x, $x_ind, $x_incr) = @_;
320  
321 # Return quickly.
322 return if $alpha == 1;
323  
324 # Code.
325 foreach (1 .. $n)
326 {
327 $$x[$x_ind] *= $alpha;
328  
329 $x_ind += $x_incr;
330 }
331 }
332  
333 # Reciprocal scale.
334 sub rscale_d ($$$$$)
335 {
336 # Arguments.
337 my ($n, $alpha, $x, $x_ind, $x_incr) = @_;
338  
339 # Return quickly.
340 return if $n <= 0;
341  
342 # Code.
343 my $mul;
344 my $num = 1;
345 my $den = $alpha;
346  
347 my $done = 0;
348 until ($done)
349 {
350 my $num1 = $num / $LARGE;
351 my $den1 = $den * $SMALL;
352  
353 if (abs ($den1) > abs ($num) && $num != 0)
354 {
355 # Pre-multiply by $SMALL.
356 $mul = $SMALL;
357 $den = $den1;
358 }
359 elsif (abs ($num1) > abs ($den))
360 {
361 # Pre-multiply by $LARGE.
362 $mul = $LARGE;
363 $num = $num1;
364 }
365 else
366 {
367 $mul = $num / $den;
368 $done = 1;
369 }
370  
371 scale_d ($n, $mul, $x, $x_ind, $x_incr);
372 }
373 }
374  
375 # Scaled vector accumulation.
376 sub axpby_d ($$$$$$$$$)
377 {
378 # Arguments.
379 my ($n, $alpha, $x, $x_ind, $x_incr, $beta, $y, $y_ind, $y_incr) = @_;
380  
381 # Return quickly.
382 return if $n <= 0;
383  
384 # Code.
385 if ($alpha == 0)
386 {
387 if ($beta == 0)
388 {
389 foreach (1 .. $n)
390 {
391 $$y[$y_ind] = 0;
392  
393 $y_ind += $y_incr;
394 }
395 }
396 elsif ($beta != 1)
397 {
398 foreach (1 .. $n)
399 {
400 $$y[$y_ind] *= $beta;
401  
402 $y_ind += $y_incr;
403 }
404 }
405 }
406 elsif ($alpha == 1)
407 {
408 if ($beta == 0)
409 {
410 foreach (1 .. $n)
411 {
412 $$y[$y_ind] = $$x[$x_ind];
413  
414 $x_ind += $x_incr;
415 $y_ind += $y_incr;
416 }
417 }
418 elsif ($beta == 1)
419 {
420 foreach (1 .. $n)
421 {
422 $$y[$y_ind] += $$x[$x_ind];
423  
424 $x_ind += $x_incr;
425 $y_ind += $y_incr;
426 }
427 }
428 else
429 {
430 foreach (1 .. $n)
431 {
432 $$y[$y_ind] *= $beta;
433 $$y[$y_ind] += $$x[$x_ind];
434  
435 $x_ind += $x_incr;
436 $y_ind += $y_incr;
437 }
438 }
439 }
440 else
441 {
442 if ($beta == 0)
443 {
444 foreach (1 .. $n)
445 {
446 $$y[$y_ind] = $alpha * $$x[$x_ind];
447  
448 $x_ind += $x_incr;
449 $y_ind += $y_incr;
450 }
451 }
452 elsif ($beta == 1)
453 {
454 foreach (1 .. $n)
455 {
456 $$y[$y_ind] += $alpha * $$x[$x_ind];
457  
458 $x_ind += $x_incr;
459 $y_ind += $y_incr;
460 }
461 }
462 else
463 {
464 foreach (1 .. $n)
465 {
466 $$y[$y_ind] *= $beta;
467 $$y[$y_ind] += $alpha * $$x[$x_ind];
468  
469 $x_ind += $x_incr;
470 $y_ind += $y_incr;
471 }
472 }
473 }
474 }
475  
476 # Scaled vector addition.
477 sub waxpby_d ($$$$$$$$$$$$)
478 {
479 # Arguments.
480 my ($n, $alpha, $x, $x_ind, $x_incr, $beta, $y, $y_ind, $y_incr, $w, $w_ind, $w_incr) = @_;
481  
482 # Return quickly.
483 return if $n <= 0;
484  
485 # Code.
486 if ($alpha == 0)
487 {
488 if ($beta == 0)
489 {
490 foreach (1 .. $n)
491 {
492 $$w[$w_ind] = 0;
493  
494 $w_ind += $w_incr;
495 }
496 }
497 elsif ($beta == 1)
498 {
499 foreach (1 .. $n)
500 {
501 $$w[$w_ind] = $$y[$y_ind];
502  
503 $w_ind += $w_incr;
504 $y_ind += $y_incr;
505 }
506 }
507 else
508 {
509 foreach (1 .. $n)
510 {
511 $$w[$w_ind] = $beta * $$y[$y_ind];
512  
513 $w_ind += $w_incr;
514 $y_ind += $y_incr;
515 }
516 }
517 }
518 elsif ($alpha == 1)
519 {
520 if ($beta == 0)
521 {
522 foreach (1 .. $n)
523 {
524 $$w[$w_ind] = $$x[$x_ind];
525  
526 $w_ind += $w_incr;
527 $x_ind += $x_incr;
528 }
529 }
530 elsif ($beta == 1)
531 {
532 foreach (1 .. $n)
533 {
534 $$w[$w_ind] = $$x[$x_ind] + $$y[$y_ind];
535  
536 $w_ind += $w_incr;
537 $x_ind += $x_incr;
538 $y_ind += $y_incr;
539 }
540 }
541 else
542 {
543 foreach (1 .. $n)
544 {
545 $$w[$w_ind] = $$x[$x_ind] + $beta * $$y[$y_ind];
546  
547 $w_ind += $w_incr;
548 $x_ind += $x_incr;
549 $y_ind += $y_incr;
550 }
551 }
552 }
553 else
554 {
555 if ($beta == 0)
556 {
557 foreach (1 .. $n)
558 {
559 $$w[$w_ind] = $alpha * $$x[$x_ind];
560  
561 $w_ind += $w_incr;
562 $x_ind += $x_incr;
563 }
564 }
565 elsif ($beta == 1)
566 {
567 foreach (1 .. $n)
568 {
569 $$w[$w_ind] = $alpha * $$x[$x_ind] + $$y[$y_ind];
570  
571 $w_ind += $w_incr;
572 $x_ind += $x_incr;
573 $y_ind += $y_incr;
574 }
575 }
576 else
577 {
578 foreach (1 .. $n)
579 {
580 $$w[$w_ind] = $alpha * $$x[$x_ind] + $beta * $$y[$y_ind];
581  
582 $w_ind += $w_incr;
583 $x_ind += $x_incr;
584 $y_ind += $y_incr;
585 }
586 }
587 }
588 }
589  
590 ## Data movement with vectors.
591  
592 # Copy vector.
593 sub copy_d ($$$$$$$)
594 {
595 # Arguments.
596 my ($n, $x, $x_ind, $x_incr, $y, $y_ind, $y_incr) = @_;
597  
598 # Return quickly.
599 return if $n <= 0;
600  
601 # Code.
602 foreach (1 .. $n)
603 {
604 $$y[$y_ind] = $$x[$x_ind];
605  
606 $x_ind += $x_incr;
607 $y_ind += $y_incr;
608 }
609 }
610  
611 # Swap vectors.
612 sub swap_d ($$$$$$$)
613 {
614 # Arguments.
615 my ($n, $x, $x_ind, $x_incr, $y, $y_ind, $y_incr) = @_;
616  
617 # Return quickly.
618 return if $n <= 0;
619  
620 # Code.
621 my $tem;
622  
623 foreach (1 .. $n)
624 {
625 $tem = $$x[$x_ind];
626 $$x[$x_ind] = $$y[$y_ind];
627 $$y[$y_ind] = $tem;
628  
629 $x_ind += $x_incr;
630 $y_ind += $y_incr;
631 }
632 }
633  
634 ## Matrix/vector operations.
635  
636 # General matrix/vector multiplication.
637 sub gemv_d ($$$$$$$$$$$$$$)
638 {
639 # Arguments.
640 my ($a_op, $m, $n, $alpha, $a, $a_ind, $a_incr, $x, $x_ind, $x_incr, $beta, $y, $y_ind, $y_incr) = @_;
641  
642 my $form = ($a_op == BLAS_NO_TRANS ? 1 :
643 ($a_op == BLAS_TRANS || $a_op == BLAS_CONJ_TRANS ? 2 :
644 0));
645  
646 croak (EINVAL)
647 if $form == 0;
648  
649 # Return quickly.
650 return if $m <= 0 || $n <= 0;
651  
652 # Code.
653 if ($form == 1)
654 {
655 # y <- alpha * A * x + beta * y
656 foreach (1 .. $m)
657 {
658 dot_d ($n, $alpha, $a, $a_ind, 1, $x, $x_ind, $x_incr, $beta, $y, $y_ind);
659  
660 $a_ind += $a_incr;
661 $y_ind += $y_incr;
662 }
663  
664 return;
665 }
666  
667 if ($form == 2)
668 {
669 # y <- alpha * A' * x + beta * y
670 foreach (1 .. $n)
671 {
672 dot_d ($m, $alpha, $a, $a_ind, $a_incr, $x, $x_ind, $x_incr, $beta, $y, $y_ind);
673  
674 $a_ind += 1;
675 $y_ind += $y_incr;
676 }
677  
678 return;
679 }
680  
681 croak (EBUG);
682 }
683  
684 ## Matrix operations.
685  
686 ## Matrix/matrix operations.
687  
688 # General matrix/matrix multiplication.
689 sub gemm_d ($$$$$$$$$$$$$$$$)
690 {
691 # Arguments.
692 my ($a_op, $b_op, $m, $n, $k, $alpha, $a, $a_ind, $a_incr, $b, $b_ind, $b_incr, $beta, $c, $c_ind, $c_incr) = @_;
693  
694 my $form = ($a_op == BLAS_NO_TRANS ?
695 ($b_op == BLAS_NO_TRANS ? 1 :
696 ($b_op == BLAS_TRANS || $b_op == BLAS_CONJ_TRANS ? 3 : 0)) :
697 ($a_op == BLAS_TRANS || $a_op == BLAS_CONJ_TRANS ?
698 ($b_op == BLAS_NO_TRANS ? 2 :
699 ($b_op == BLAS_TRANS || $b_op == BLAS_CONJ_TRANS ? 4 : 0)) :
700 0));
701  
702 croak (EINVAL)
703 if $form == 0;
704  
705 # Return quickly.
706 return if $m <= 0 || $n <= 0;
707  
708 # Code.
709 $c_incr -= $n;
710  
711 if ($form == 1)
712 {
713 # C <- alpha * A * B + beta * C
714 my $b_start = $b_ind;
715  
716 foreach (1 .. $m)
717 {
718 foreach (1 .. $n)
719 {
720 dot_d ($k, $alpha, $a, $a_ind, 1, $b, $b_ind, $b_incr, $beta, $c, $c_ind);
721  
722 ++$b_ind;
723 ++$c_ind;
724 }
725  
726 $a_ind += $a_incr;
727 $b_ind = $b_start;
728 $c_ind += $c_incr;
729 }
730  
731 return;
732 }
733  
734 if ($form == 2)
735 {
736 # C <- alpha * A' * B + beta * C
737 my $b_start = $b_ind;
738  
739 foreach (1 .. $m)
740 {
741 foreach (1 .. $n)
742 {
743 dot_d ($k, $alpha, $a, $a_ind, $a_incr, $b, $b_ind, $b_incr, $beta, $c, $c_ind);
744  
745 ++$b_ind;
746 ++$c_ind;
747 }
748  
749 $a_ind += 1;
750 $b_ind = $b_start;
751 $c_ind += $c_incr;
752 }
753  
754 return;
755 }
756  
757 if ($form == 3)
758 {
759 # C <- alpha * A * B' + beta * C
760 my $b_start = $b_ind;
761  
762 foreach (1 .. $m)
763 {
764 foreach (1 .. $n)
765 {
766 dot_d ($k, $alpha, $a, $a_ind, 1, $b, $b_ind, 1, $beta, $c, $c_ind);
767  
768 $b_ind += $b_incr;
769 $c_ind += 1;
770 }
771  
772 $a_ind += $a_incr;
773 $b_ind = $b_start;
774 $c_ind += $c_incr;
775 }
776  
777 return;
778 }
779  
780 if ($form == 4)
781 {
782 # C <- alpha * A' * B' + beta * C
783 my $b_start = $b_ind;
784  
785 foreach (1 .. $m)
786 {
787 foreach (1 .. $n)
788 {
789 dot_d ($k, $alpha, $a, $a_ind, $a_incr, $b, $b_ind, 1, $beta, $c, $c_ind);
790  
791 $b_ind += $b_incr;
792 $c_ind += 1;
793 }
794  
795 $a_ind += 1;
796 $b_ind = $b_start;
797 $c_ind += $c_incr;
798 }
799  
800 return;
801 }
802  
803 croak (EBUG);
804 }
805  
806 1;
807  
808 __END__