我有一个特征/ C ++代码,其目的是执行行主稀疏密集和密集主要稀疏矩阵乘法both of which are multithreaded in Eigen。
然而,我发现只有行主要稀疏 - 密集乘法是缩放,而不是密集col主稀疏乘法。为什么是这样?下面是代码和时间。
/*timer function*/
double getHighResolutionTime(void) {
struct timeval tod;
gettimeofday(&tod, NULL);
double time_seconds = (double) tod.tv_sec + ((double) tod.tv_usec / 1000000.0);
return time_seconds;
}
...
//define Col Major Sparse
Map<SparseMatrix<double,ColMajor> > gcol (m, n, nz, jc_int, ir_int, pr);
//define the same matrix but Row Major
Map<SparseMatrix<double,RowMajor> > grow (m, n, nz, jc_int, ir_int, pr);
//define dense matrix
Map<MatrixXd> G (PR1, M, N );
//define result
Map<MatrixXd> result (PR2, M, N);
//row major sparse - dense product
double tic=getHighResolutionTime();
result=grow*G;
double toc=getHighResolutionTime();
printf("\nsparse-dense time: %f seconds", (toc - tic));
//dense - col major sparse product
tic=getHighResolutionTime();
result=G*gcol;
toc=getHighResolutionTime();
printf("\ndense-sparse time: %f seconds\n", (toc - tic));
使用1,2,4,8和16个线程(在16核机器上)的输出和时序。只有稀疏密集的鳞片,不是密集稀疏的。
Using 1 threads...
sparse-dense time: 5.184886 seconds
dense-sparse time: 3.278560 seconds
Using 2 threads...
sparse-dense time: 2.808550 seconds
dense-sparse time: 3.275191 seconds
Using 4 threads...
sparse-dense time: 1.589596 seconds
dense-sparse time: 3.278983 seconds
Using 8 threads...
sparse-dense time: 1.005600 seconds
dense-sparse time: 3.279466 seconds
Using 16 threads...
sparse-dense time: 0.736803 seconds
dense-sparse time: 3.278893 seconds
附加信息:矩阵是7000x7000随机,真实和双重。稀疏矩阵是随机的,密度为1%。两次乘法的数值结果都是正确的。我正在使用以下标志进行编译:
-fomit-frame-pointer -O3 -DNDEBUG -fopenmp -march=native -fPIC
编辑:
ggael的答案很有效。以下是新的缩放:
Using 1 threads...
sparse-dense time: 5.070809 seconds
dense-sparse time: 3.270347 seconds
Using 2 threads...
sparse-dense time: 2.786790 seconds
dense-sparse time: 2.070378 seconds
Using 4 threads...
sparse-dense time: 1.580925 seconds
dense-sparse time: 1.243466 seconds
Using 8 threads...
sparse-dense time: 1.000152 seconds
dense-sparse time: 0.887953 seconds
Using 16 threads...
sparse-dense time: 0.898228 seconds
dense-sparse time: 0.909603 seconds