Cholesky Factorization (CF) needs to access matrix elements along both column and row, so that to the sparse matrix, addressing wocould always be the greatest problem, this utility for dense matrix is quite easy. my brute-forced implementation is much slower than MKL'sDcsrilu0.
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 wocould 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.
1:# Include <iostream>
2:
3:_ DEVICE _ bool readcsr (const double * Vals, const int * Rows, const int * cols, const int R, const int C, double * V)
4:{
5:Int I, J;
6:
7:I = rows [R];
8:J = rows [R + 1];
9:
10:I = Cols [I];
11:J = Cols [J-1];
12:
13:If (C <I)
14:{
15:Return-1;
16:}
17:
18:If (C> J)
19:{
20:Return 1;
21:}
22:
23:For (I = rows [R]; I <rows [R + 1]; ++ I)
24:{
25:J = Cols [I];
26:If (j = C)
27:{
28:* V = Vals [I];
29:Return 0;
30:}
31:}
32:
33:Return-1;
34:}
35:
36:_ DEVICE _ void writecsr (double * Vals, const int * Rows, const int * cols, const int R, const int C, const Double V)
37:{
38:Int I, J;
39:For (I = rows [R]; I <rows [R + 1]; ++ I)
40:{
41:J = Cols [I];
42:If (j = C)
43:{
44:Vals [I] = V;
45:}
46:}
47:}
48:
49:_ Global _ void phase1 (const int K, double * Vals, const int * Rows, const int * Cols)
50:{
51:Int I = blockidx. X;
52:If (I> K)
53:{
54:Double aK = 0.0;
55:Readcsr (Vals, rows, cols, K, K, & Akk );
56:Akk = SQRT (Akk );
57:
58:Double AIK = 0.0f;
59:If (readcsr (Vals, rows, cols, I, K, & AIK) = 0)
60:{
61:Writecsr (Vals, rows, cols, I, K, AIK/Akk );
62:}
63:}
64:}
65:
66:_ Global _ void phase2 (const int K, double * Vals, const int * Rows, const int * Cols)
67:{
68:Int J = blockidx. X;
69:Int r = griddim. X;
70:
71:If (j> K)
72:{
73:For (INT I = J; I <r; ++ I)
74:{
75:Double AIJ = 0.0;
76:Int A = readcsr (Vals, rows, cols, I, j, & AIJ );
77:If (A = 0)
78:{
79:Double AIK = 0.0, AJK = 0.0;
80:Readcsr (Vals, rows, cols, I, K, & AIK );
81:Readcsr (Vals, rows, cols, J, K, & AJK );
82:Writecsr (Vals, rows, cols, I, j, AIJ-AIK * AJK );
83:}
84:Else if (a> 0)
85:{
86:Break;
87:}
88:}
89:}
90:}
91:
92:_ Global _ void phase3 (const int K, double * Vals, const int * Rows, const int * Cols)
93:{
94:Double aK = 0.0;
95:Readcsr (Vals, rows, cols, K, K, & Akk );
96:Akk = SQRT (Akk );
97:Writecsr (Vals, rows, cols, K, K, Akk );
98:}
99:
100:Void test (const int numrow, double * Vals, const int * Rows, const int * Cols)
101:{
102:For (int K = 0; k <numrow; ++ K)
103:{
104:STD: cout <k <STD: Endl;
105:Phase1 <numrow, 1> (K, Vals, rows, cols );
106:Phase2 <numrow, 1> (K, Vals, rows, cols );
107:Phase3 <1, 1> (K, Vals, rows, cols );
108:}
109:}
110:
111:
APL. 24
Found a paper used Co., ELLPACK-R. Instead of CSR. I will take a try.