| 1 | ## Math/BLAS.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; | ||
| 11 | |||
| 12 | use strict; | ||
| 13 | use warnings; | ||
| 14 | use Exporter qw(import); | ||
| 15 | |||
| 16 | use Math::BLAS::Enum; | ||
| 17 | use Math::BLAS::PP; | ||
| 18 | |||
| 19 | BEGIN | ||
| 20 | { | ||
| 21 | our $VERSION = '1.01'; | ||
| 22 | our @EXPORT = (); | ||
| 23 | our @EXPORT_OK = (); | ||
| 24 | our %EXPORT_TAGS = (); | ||
| 25 | |||
| 26 | # Named constants. | ||
| 27 | $EXPORT_TAGS{enum} | ||
| 28 | = [ @Math::BLAS::Enum::EXPORT ]; | ||
| 29 | push (@EXPORT_OK, @{ $EXPORT_TAGS{enum} }); | ||
| 30 | push (@EXPORT, @{ $EXPORT_TAGS{enum} }); | ||
| 31 | |||
| 32 | # Subroutines. | ||
| 33 | $EXPORT_TAGS{sub} | ||
| 34 | = [qw(blas_dot | ||
| 35 | blas_norm | ||
| 36 | blas_sum | ||
| 37 | blas_min_val | ||
| 38 | blas_amin_val | ||
| 39 | blas_max_val | ||
| 40 | blas_amax_val | ||
| 41 | blas_sumsq | ||
| 42 | blas_rscale | ||
| 43 | blas_axpby | ||
| 44 | blas_waxpby | ||
| 45 | blas_copy | ||
| 46 | blas_swap | ||
| 47 | blas_gemv | ||
| 48 | blas_gemm)]; | ||
| 49 | push (@EXPORT_OK, @{ $EXPORT_TAGS{sub} }); | ||
| 50 | push (@EXPORT, @{ $EXPORT_TAGS{sub} }); | ||
| 51 | |||
| 52 | # Define aliases. | ||
| 53 | if (1) | ||
| 54 | { | ||
| 55 | no strict 'refs'; | ||
| 56 | my ($s, @bare) = (); | ||
| 57 | |||
| 58 | foreach (@{ $EXPORT_TAGS{sub} }) | ||
| 59 | { | ||
| 60 | ($s = $_) =~ s/\Ablas_//; | ||
| 61 | |||
| 62 | *{$s} = \&{$_}; | ||
| 63 | push (@bare, $s); | ||
| 64 | } | ||
| 65 | |||
| 66 | $EXPORT_TAGS{bare} = [@bare]; | ||
| 67 | push (@EXPORT_OK, @bare); | ||
| 68 | } | ||
| 69 | |||
| 70 | $EXPORT_TAGS{all} = [ @EXPORT_OK ]; | ||
| 71 | } | ||
| 72 | |||
| 73 | # Dot product. | ||
| 74 | sub blas_dot ($$$%) | ||
| 75 | { | ||
| 76 | my ($n, $x, $y, %opt) = @_; | ||
| 77 | |||
| 78 | dot_d (int ($n), | ||
| 79 | $opt{alpha} // 1, | ||
| 80 | $x, | ||
| 81 | int ($opt{x_ind} // 0), | ||
| 82 | int ($opt{x_incr} // 1), | ||
| 83 | $y, | ||
| 84 | int ($opt{y_ind} // 0), | ||
| 85 | int ($opt{y_incr} // 1), | ||
| 86 | $opt{beta} // 0, | ||
| 87 | $opt{r} // 0, | ||
| 88 | int ($opt{r_ind} // 0)); | ||
| 89 | } | ||
| 90 | |||
| 91 | # Vector norms. | ||
| 92 | sub blas_norm ($$%) | ||
| 93 | { | ||
| 94 | my ($n, $x, %opt) = @_; | ||
| 95 | |||
| 96 | norm_d ($opt{norm} // BLAS_ONE_NORM, | ||
| 97 | int ($n), | ||
| 98 | $x, | ||
| 99 | int ($opt{x_ind} // 0), | ||
| 100 | int ($opt{x_incr} // 1)); | ||
| 101 | } | ||
| 102 | |||
| 103 | # Sum. | ||
| 104 | sub blas_sum ($$%) | ||
| 105 | { | ||
| 106 | my ($n, $x, %opt) = @_; | ||
| 107 | |||
| 108 | sum_d (int ($n), | ||
| 109 | $x, | ||
| 110 | int ($opt{x_ind} // 0), | ||
| 111 | int ($opt{x_incr} // 1)); | ||
| 112 | } | ||
| 113 | |||
| 114 | # Minimum value. | ||
| 115 | sub blas_min_val ($$%) | ||
| 116 | { | ||
| 117 | my ($n, $x, %opt) = @_; | ||
| 118 | |||
| 119 | min_val_d (int ($n), | ||
| 120 | $x, | ||
| 121 | int ($opt{x_ind} // 0), | ||
| 122 | int ($opt{x_incr} // 1)); | ||
| 123 | } | ||
| 124 | |||
| 125 | # Minimum absolute value. | ||
| 126 | sub blas_amin_val ($$%) | ||
| 127 | { | ||
| 128 | my ($n, $x, %opt) = @_; | ||
| 129 | |||
| 130 | amin_val_d (int ($n), | ||
| 131 | $x, | ||
| 132 | int ($opt{x_ind} // 0), | ||
| 133 | int ($opt{x_incr} // 1)); | ||
| 134 | } | ||
| 135 | |||
| 136 | # Maximum value. | ||
| 137 | sub blas_max_val ($$%) | ||
| 138 | { | ||
| 139 | my ($n, $x, %opt) = @_; | ||
| 140 | |||
| 141 | max_val_d (int ($n), | ||
| 142 | $x, | ||
| 143 | int ($opt{x_ind} // 0), | ||
| 144 | int ($opt{x_incr} // 1)); | ||
| 145 | } | ||
| 146 | |||
| 147 | # Maximum absolute value. | ||
| 148 | sub blas_amax_val ($$%) | ||
| 149 | { | ||
| 150 | my ($n, $x, %opt) = @_; | ||
| 151 | |||
| 152 | amax_val_d (int ($n), | ||
| 153 | $x, | ||
| 154 | int ($opt{x_ind} // 0), | ||
| 155 | int ($opt{x_incr} // 1)); | ||
| 156 | } | ||
| 157 | |||
| 158 | # Sum of squares. | ||
| 159 | sub blas_sumsq ($$%) | ||
| 160 | { | ||
| 161 | my ($n, $x, %opt) = @_; | ||
| 162 | |||
| 163 | sumsq_d (int ($n), | ||
| 164 | $x, | ||
| 165 | int ($opt{x_ind} // 0), | ||
| 166 | int ($opt{x_incr} // 1), | ||
| 167 | $opt{sumsq}, | ||
| 168 | $opt{scale}); | ||
| 169 | } | ||
| 170 | |||
| 171 | # Reciprocal scale. | ||
| 172 | sub blas_rscale ($$%) | ||
| 173 | { | ||
| 174 | my ($n, $x, %opt) = @_; | ||
| 175 | |||
| 176 | rscale_d (int ($n), | ||
| 177 | $opt{alpha} // 1, | ||
| 178 | $x, | ||
| 179 | int ($opt{x_ind} // 0), | ||
| 180 | int ($opt{x_incr} // 1)); | ||
| 181 | } | ||
| 182 | |||
| 183 | # Scaled vector accumulation. | ||
| 184 | sub blas_axpby ($$$%) | ||
| 185 | { | ||
| 186 | my ($n, $x, $y, %opt) = @_; | ||
| 187 | |||
| 188 | axpby_d (int ($n), | ||
| 189 | $opt{alpha} // 1, | ||
| 190 | $x, | ||
| 191 | int ($opt{x_ind} // 0), | ||
| 192 | int ($opt{x_incr} // 1), | ||
| 193 | $opt{beta} // 1, | ||
| 194 | $y, | ||
| 195 | int ($opt{y_ind} // 0), | ||
| 196 | int ($opt{y_incr} // 1)); | ||
| 197 | } | ||
| 198 | |||
| 199 | # Scaled vector addition. | ||
| 200 | sub blas_waxpby ($$$$%) | ||
| 201 | { | ||
| 202 | my ($n, $x, $y, $w, %opt) = @_; | ||
| 203 | |||
| 204 | waxpby_d (int ($n), | ||
| 205 | $opt{alpha} // 1, | ||
| 206 | $x, | ||
| 207 | int ($opt{x_ind} // 0), | ||
| 208 | int ($opt{x_incr} // 1), | ||
| 209 | $opt{beta} // 1, | ||
| 210 | $y, | ||
| 211 | int ($opt{y_ind} // 0), | ||
| 212 | int ($opt{y_incr} // 1), | ||
| 213 | $w, | ||
| 214 | int ($opt{w_ind} // 0), | ||
| 215 | int ($opt{w_incr} // 1)); | ||
| 216 | } | ||
| 217 | |||
| 218 | # Copy vector. | ||
| 219 | sub blas_copy ($$$%) | ||
| 220 | { | ||
| 221 | my ($n, $x, $y, %opt) = @_; | ||
| 222 | |||
| 223 | copy_d (int ($n), | ||
| 224 | $x, | ||
| 225 | int ($opt{x_ind} // 0), | ||
| 226 | int ($opt{x_incr} // 1), | ||
| 227 | $y, | ||
| 228 | int ($opt{y_ind} // 0), | ||
| 229 | int ($opt{y_incr} // 1)); | ||
| 230 | } | ||
| 231 | |||
| 232 | # Swap vectors. | ||
| 233 | sub blas_swap ($$$%) | ||
| 234 | { | ||
| 235 | my ($n, $x, $y, %opt) = @_; | ||
| 236 | |||
| 237 | swap_d (int ($n), | ||
| 238 | $x, | ||
| 239 | int ($opt{x_ind} // 0), | ||
| 240 | int ($opt{x_incr} // 1), | ||
| 241 | $y, | ||
| 242 | int ($opt{y_ind} // 0), | ||
| 243 | int ($opt{y_incr} // 1)); | ||
| 244 | } | ||
| 245 | |||
| 246 | # General matrix/vector multiplication. | ||
| 247 | sub blas_gemv ($$$$$%) | ||
| 248 | { | ||
| 249 | my ($m, $n, $a, $x, $y, %opt) = @_; | ||
| 250 | |||
| 251 | my $a_op = $opt{a_op} // BLAS_NO_TRANS; | ||
| 252 | |||
| 253 | gemv_d ($a_op, | ||
| 254 | int ($m), | ||
| 255 | int ($n), | ||
| 256 | $opt{alpha} // 1, | ||
| 257 | $a, | ||
| 258 | int ($opt{a_ind} // 0), | ||
| 259 | int ($opt{a_incr} // ($a_op == BLAS_NO_TRANS ? $n : $m)), | ||
| 260 | $x, | ||
| 261 | int ($opt{x_ind} // 0), | ||
| 262 | int ($opt{x_incr} // 1), | ||
| 263 | $opt{beta} // 0, | ||
| 264 | $y, | ||
| 265 | int ($opt{y_ind} // 0), | ||
| 266 | int ($opt{y_incr} // 1)); | ||
| 267 | } | ||
| 268 | |||
| 269 | # General matrix/matrix multiplication. | ||
| 270 | sub blas_gemm ($$$$$$%) | ||
| 271 | { | ||
| 272 | my ($m, $n, $k, $a, $b, $c, %opt) = @_; | ||
| 273 | |||
| 274 | my $a_op = $opt{a_op} // BLAS_NO_TRANS; | ||
| 275 | my $b_op = $opt{b_op} // BLAS_NO_TRANS; | ||
| 276 | |||
| 277 | gemm_d ($a_op, | ||
| 278 | $b_op, | ||
| 279 | int ($m), | ||
| 280 | int ($n), | ||
| 281 | int ($k), | ||
| 282 | $opt{alpha} // 1, | ||
| 283 | $a, | ||
| 284 | int ($opt{a_ind} // 0), | ||
| 285 | int ($opt{a_incr} // ($a_op == BLAS_NO_TRANS ? $k : $m)), | ||
| 286 | $b, | ||
| 287 | int ($opt{b_ind} // 0), | ||
| 288 | int ($opt{b_incr} // ($b_op == BLAS_NO_TRANS ? $n : $k)), | ||
| 289 | $opt{beta} // 0, | ||
| 290 | $c, | ||
| 291 | int ($opt{c_ind} // 0), | ||
| 292 | int ($opt{c_incr} // $n)); | ||
| 293 | } | ||
| 294 | |||
| 295 | 1; | ||
| 296 | |||
| 297 | __END__ |