最適化されたブーストuBLASライブラリを使用するために、独自のベクトル代数コードの一部を変換しています。ただし、SymmetricMatrix-SparseVectorの乗算を実行しようとすると、自分の実装よりも約4倍遅いことがわかりました。ベクトルサイズは通常約0〜500で、約70〜80%のエントリはゼロです。
これが私のコードです
void CRoutines::GetA(double a[], double vectorIn[], int sparseVectorIndexes[], int vectorLength, int sparseLength)
{
compressed_vector<double> inVec (vectorLength, sparseLength);
for(int i = 0; i < sparseLength; i++)
{
inVec(sparseVectorIndexes[i]) = vectorIn[sparseVectorIndexes[i]];
}
vector<double> test = prod(inVec, matrix);
for(int i = 0; i < vectorLength; i++)
{
a[i] = test(i);
}
}
sparseVectorIndexesは、入力ベクトルの非ゼロ値のインデックスを格納します。vectorLengthはベクトルの長さであり、sparseLengthはベクトル内の非ゼロの数です。行列は対称行列として格納されsymmetric_matrix<double, lower>
ます。
私自身の実装は、単純なネストされたループの反復であり、行列は単なる2D二重配列です。
void CRoutines::GetA(double a[], double vectorIn[], int sparseVectorIndexes[], int vectorLength, int sparseLength)
{
for (int i = 0; i < vectorLength; i++)
{
double temp = 0;
for (int j = 0; j < sparseLength; j++)
{
int row = sparseVectorIndexes[j];
if (row <= i) // Handle lower triangular sparseness
temp += matrix[i][row] * vectorIn[row];
else
temp += matrix[row][i] * vectorIn[row];
}
a[i] = temp;
}
}
uBLAS 4xが遅いのはなぜですか?掛け算をきちんと書いていませんか?それとも、これにより適した別のライブラリがありますか?
編集:代わりに密なベクトル配列を使用すると、uBLASは2倍遅くなります...