BLAS、GSL、またはその他の高性能ライブラリを使用して、要素ごとのベクトルとベクトルの乗算を行う手段はありますか?
4 に答える
(質問のタイトルを文字通りに取って...)
はい、BLAS だけで実行できます (ただし、おそらく最も効率的な方法ではありません)。
秘訣は、入力ベクトルの 1 つを対角行列として扱うことです。
⎡a ⎤ ⎡x⎤ ⎡ax⎤
⎢ b ⎥ ⎢y⎥ = ⎢by⎥
⎣ c⎦ ⎣z⎦ ⎣cz⎦
次に、パディングなしで入力として対角行列を取ることができる行列ベクトル乗算関数の 1 つを使用できます。SBMV
例:
void ebeMultiply(const int n, const double *a, const double *x, double *y)
{
extern void dsbmv_(const char *uplo,
const int *n,
const int *k,
const double *alpha,
const double *a,
const int *lda,
const double *x,
const int *incx,
const double *beta,
double *y,
const int *incy);
static const int k = 0; // Just the diagonal; 0 super-diagonal bands
static const double alpha = 1.0;
static const int lda = 1;
static const int incx = 1;
static const double beta = 0.0;
static const int incy = 1;
dsbmv_("L", &n, &k, &alpha, a, &lda, x, &incx, &beta, y, &incy);
}
// Test
#define N 3
static const double a[N] = {1,3,5};
static const double b[N] = {1,10,100};
static double c[N];
int main(int argc, char **argv)
{
ebeMultiply(N, a, b, c);
printf("Result: [%f %f %f]\n", c[0], c[1], c[2]);
return 0;
}
Result: [1.000000 30.000000 500.000000]
MKL の Vector Mathematical Functions Library (VML) には、v?Mul など、Vector に対する一連の数学演算が含まれていることがわかりました。C++ 配列で動作するので、GSL よりも便利です。
std::valarray 1/Quse-intel-optimized-headers
が常にあり、ターゲットがサポートしている場合、SIMD 命令に 頻繁にコンパイルされる (Intel C++ 、G++) 要素単位の操作を定義します。
これらのコンパイラはどちらも自動ベクトル化も行います
- http://software.intel.com/en-us/articles/getting-code-ready-for-parallel-execution-with-intel-parallel-composer/
- http://gcc.gnu.org/projects/tree-ssa/vectorization.html
その場合、あなたはただ書くことができます
#define N 10000
float a[N], b[N], c[N];
void f1() {
for (int i = 1; i < N; i++)
c[i] = a[i] + b[i];
}
ベクトル化されたコードにコンパイルされるのを確認します(SSE4などを使用)
1はい、それらは時代遅れであり、時代遅れと見なされることがよくありますが、実際には両方とも標準であり、タスクに非常によく適合します。
GSLgsl_vector_mul
では、トリックを行います。