您的位置:首页 > 其它

Loser’s “Brute-forced Cholesky Factorization for Sparse Matrix on CUDA”

2012-04-23 20:19 323 查看
Cholesky Factorization (CF) needs to access matrix elements along both column and row, so that to the sparse matrix, addressing would always be the greatest problem, this utility for dense matrix is quite easy. My brute-forced implementation is much slower than MKL's dcsrilu0.

CF method is a highly serialized algorithm, it processes the whole matrix row by row, and to the 1st row, its complexity is log(n*n/2), and then it would be faster and faster.

In fact I thought several methods as following,

Gathering and Scattering. As the bottleneck is the loop in Phase2, it need to access column and row, we may use gathering and scattering to simplify memeory access, but that’s as the same as CPU’s.
Transpose the matrix as a backup, so that access column will countious, but need to sync both matrix at the end of each step, still a overhead.
Here is the code, workable, slow, you may improve it by yourself, I switched to CPU, no sh*t & fu*k GPU.

#include <iostream>


 


__device__ bool ReadCSR(const double *vals, const int *rows, const int *cols, const int r, const int c, double * v)


{


int i, j;


 


i = rows[r];


j = rows[r + 1];


 


i = cols[i];


j = cols[j - 1];


 


if (c < i)


{


return -1;


}


 


if (c > j)


{


return 1;


}


 


for (i = rows[r]; i < rows[r + 1]; ++ i)


{


j = cols[i];


if (j == c)


{


*v = vals[i];


return 0;


}


}


 


return -1;


}


 


__device__ void WriteCSR(double * vals, const int * rows, const int * cols, const int r, const int c, const double v)


{


int i, j;


for (i = rows[r]; i < rows[r + 1]; ++ i)


{


j = cols[i];


if (j == c)


{


vals[i] = v;


}


}


}


 


__global__ void Phase1(const int k, double *vals, const int *rows, const int *cols)


{


int i = blockIdx.x;


if (i > k)


{


double Akk = 0.0;


ReadCSR(vals, rows, cols, k, k, &Akk);


Akk = sqrt(Akk);


 


double Aik = 0.0f;


if (ReadCSR(vals, rows, cols, i, k, &Aik) == 0)


{


WriteCSR(vals, rows, cols, i, k, Aik / Akk);


}


}


}


 


__global__ void Phase2(const int k, double *vals, const int *rows, const int *cols)


{


int j = blockIdx.x;


int r = gridDim.x;


 


if (j > k)


{


for (int i = j; i < r; ++ i)


{


double Aij = 0.0;


int a = ReadCSR(vals, rows, cols, i, j, &Aij);


if (a == 0)


{


double Aik = 0.0, Ajk = 0.0;


ReadCSR(vals, rows, cols, i, k, &Aik);


ReadCSR(vals, rows, cols, j, k, &Ajk);


WriteCSR(vals, rows, cols, i, j, Aij - Aik * Ajk);


}


else if (a > 0)


{


break;


}


}


}


}


 


__global__ void Phase3(const int k, double *vals, const int *rows, const int *cols)


{


double Akk = 0.0;


ReadCSR(vals, rows, cols, k, k, &Akk);


Akk = sqrt(Akk);


WriteCSR(vals, rows, cols, k, k, Akk);


}


 


void test(const int numRow, double *vals, const int *rows, const int *cols)


{


for (int k = 0; k < numRow; ++ k)


{


std::cout << k << std::endl;


Phase1<<<numRow, 1>>>(k, vals, rows, cols);


Phase2<<<numRow, 1>>>(k, vals, rows, cols);


Phase3<<<1,      1>>>(k, vals, rows, cols);


}


}


 


 


Apl. 24

Found a paper used ELLPACK-R instead of CSR. I will take a try.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐