/
lsqr.cl
152 lines (146 loc) · 4.8 KB
/
lsqr.cl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/***************************************************************************
* *
* Copyright (c) 2011, 2012 *
* Jose Luis Cercos Pita <jlcercos@gmail.com> *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU Lesser General Public License (LGPL) *
* as published by the Free Software Foundation; either version 2 of *
* the License, or (at your option) any later version. *
* for detail see the LICENCE text file. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU Library General Public License for more details. *
* *
* You should have received a copy of the GNU Library General Public *
* License along with this program; if not, write to the Free Software *
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 *
* USA *
* *
***************************************************************************/
/** Compute residuals of the solution stimator for a linear system.
* @param A Linear system matrix.
* @param B Linear system independent term.
* @param X Solution estimation.
* @param R Residuals.
* @param n Linear system dimension.
*/
__kernel void r(__global float* A,
__global float* B,
__global float* X,
__global float* R,
unsigned int n)
{
// find position in global arrays
unsigned int i = get_global_id(0);
unsigned int j;
if(i >= n)
return;
// Evaluate the row
R[i] = B[i];
for(j=0;j<n;j++){
R[i] -= A[j + i*n]*X[j];
}
}
/** Compute inner product between a matrix and a vector.
* @param A Matrix.
* @param X Vector.
* @param Y Result.
* @param n Linear system dimension.
*/
__kernel void dot_mat_vec(__global float* A,
__global float* X,
__global float* Y,
unsigned int n)
{
// find position in global arrays
unsigned int i = get_global_id(0);
unsigned int j;
if(i >= n)
return;
// Evaluate the row
Y[i] = 0.f;
for(j=0;j<n;j++){
Y[i] += A[j + i*n]*X[j];
}
}
/** Compute inner product between a transposed matrix and a vector.
* @param A Matrix.
* @param X Vector.
* @param Y Result.
* @param n Linear system dimension.
*/
__kernel void dot_matT_vec(__global float* A,
__global float* X,
__global float* Y,
unsigned int n)
{
// find position in global arrays
unsigned int i = get_global_id(0);
unsigned int j;
if(i >= n)
return;
// Evaluate the row
Y[i] = 0.f;
for(j=0;j<n;j++){
Y[i] += A[i + j*n]*X[j];
}
}
/** Create u vector for the next iteration.
* @note u loads beta inside, you must compute
* the norm and divide him.
* @param A Linear system matrix.
* @param u0 u vector from previous step.
* @param v0 v vector from previous step.
* @param u Looked u vector for next step.
* @param alpha \$ \alpha_{i} \$.
* @param n Linear system dimension.
*/
__kernel void u(__global float* A,
__global float* u0,
__global float* v0,
__global float* u,
float alpha,
unsigned int n)
{
// find position in global arrays
unsigned int i = get_global_id(0);
unsigned int j;
if(i >= n)
return;
// Evaluate the row
u[i] = - alpha * u0[i];
for(j=0;j<n;j++){
u[i] += A[j + i*n]*v0[j];
}
}
/** Create v vector for the next iteration.
* @note v loads alpha inside, you must compute
* the norm and divide him.
* @param A Linear system matrix.
* @param u u vector for next step.
* @param v0 v vector from previous step.
* @param v Looked v vector for next step.
* @param beta \$ \beta_{i+1} \$.
* @param n Linear system dimension.
*/
__kernel void v(__global float* A,
__global float* u,
__global float* v0,
__global float* v,
float beta,
unsigned int n)
{
// find position in global arrays
unsigned int i = get_global_id(0);
unsigned int j;
if(i >= n)
return;
// Evaluate the row
v[i] = - beta * v0[i];
for(j=0;j<n;j++){
v[i] += A[i + j*n]*u[j];
}
}