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