-
Notifications
You must be signed in to change notification settings - Fork 4
/
BandedMatrix.cu
207 lines (183 loc) · 5.57 KB
/
BandedMatrix.cu
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
/* BandedMatrix.cu is part of gpumatting and is
* Copyright 2013 Philip G. Lee <[email protected]>
*
* gpumatting is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* gpumatting 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with gpumatting. If not, see <http://www.gnu.org/licenses/>.
*/
#include "BandedMatrix.h"
/*!
* \brief Copy a host matrix to a device matrix.
*/
void bmCopyToDevice( BandedMatrix* dA, BandedMatrix const* hA )
{
*dA = *hA;
cudaMallocPitch( (void**)&(dA->a), &(dA->apitch), hA->rows * sizeof(float), hA->nbands );
cudaMalloc( (void**)&(dA->bands), hA->nbands * sizeof(int) );
cudaMemcpy2D(
(void*)(dA->a), // Destination
dA->apitch, // Destination pitch (bytes)
(const void*)(hA->a), // Source
hA->rows * sizeof(float), // Source pitch (bytes)
hA->rows * sizeof(float), // Source width (bytes)
hA->nbands, // Source height
cudaMemcpyHostToDevice
);
cudaDeviceSynchronize();
dA->apitch /= sizeof(float); // Want the pitch to be in float indices rather than bytes.
cudaMemcpy( (void*)(dA->bands), (void*)(hA->bands), hA->nbands * sizeof(int), cudaMemcpyHostToDevice );
}
void bmDeviceFree( BandedMatrix* dA )
{
cudaFree( dA->bands );
cudaFree( dA->a );
}
/*!
* \brief Damped Jacobi iteration.
*
* Does a single damped Jacobi iteration.
*
* \param xx Output of the iteration.
* \param x Input point of the iteration.
* \param a Matrix
* \param b Right-hand side
* \param omega Damping ratio. 1 means no damping, and 0 means infinite
* damping. Usually, 2/3 is used.
*/
template<int nbands>
__global__ void jacobi_k(
float* xx,
float const* x,
const BandedMatrix a,
float const* b,
float omega
)
{
__shared__ int sdata[nbands];
int nthreads = blockDim.x*gridDim.x;
int i = blockIdx.x*blockDim.x + threadIdx.x;
int j;
float bi;
// Make the shared data store the band offsets.
if( threadIdx.x < nbands )
sdata[threadIdx.x] = a.bands[threadIdx.x];
__syncthreads();
while( true )
{
bi = b[i];
if( i < a.rows )
{
for( j = 0; j < nbands; ++j )
bi -= a.a[i+j*a.apitch] * x[i+sdata[j]];
bi = x[i] + bi/a.a[i+(nbands/2)*a.apitch];
xx[i] = omega*bi + (1.f-omega)*x[i];
}
else
break;
i += nthreads;
}
}
/*!
* \brief b = A*x when A is a sparse banded matrix.
*
* Needs a.nbands * sizeof(int) shared memory.
* NOTE: \c x must have 0 padding so that x[a.bands[i]] and x[N-1+a.bands[i]]
* are valid indices into x for all i.
*/
template<int nbands>
__device__ void bmAx( float* b, const BandedMatrix a, float const* x )
{
//extern __shared__ int sdata[];
__shared__ int sdata[nbands];
int nthreads = blockDim.x*gridDim.x;
int i = blockIdx.x*blockDim.x + threadIdx.x;
int j;
float bi;
// Make the shared data store the band offsets.
if( threadIdx.x < a.nbands )
sdata[threadIdx.x] = a.bands[threadIdx.x];
__syncthreads();
while( true )
{
bi = 0.f;
if( i < a.rows )
{
// NOTE: sometimes i+a.bands[j] is a bad index into x[], but we
// guarantee that when it is a bad index, a[i+j*a->nbands] == 0.f,
// so as long as there is no segfault, we don't have to branch here.
for( j = 0; j < a.nbands; ++j )
bi += a.a[i+j*a.apitch] * x[i+sdata[j]];
b[i] = bi;
}
else
break;
i += nthreads;
}
}
/*!
* \brief b = A*x+y or A*x-y when A is a sparse banded matrix.
*
* Needs a.nbands * sizeof(int) shared memory.
* NOTE: \c x must have 0 padding so that x[a.bands[i]] and x[N-1+a.bands[i]]
* are valid indices into x for all i.
*
* \tparam add If true, do A*x+y, else A*x-y
*/
template <int nbands, bool add>
__device__ void bmAxpy( float* b, const BandedMatrix a, float const* x, float const* y )
{
//extern __shared__ int sdata[];
__shared__ int sdata[nbands];
int nthreads = blockDim.x*gridDim.x;
int i = blockIdx.x*blockDim.x + threadIdx.x;
int j;
float bi;
// Make the shared data store the band offsets.
if( threadIdx.x < a.nbands )
sdata[threadIdx.x] = a.bands[threadIdx.x];
__syncthreads();
while( true )
{
bi = 0.f;
if( i < a.rows )
{
// NOTE: sometimes i+a.bands[j] is a bad index into x[], but we
// guarantee that when it is a bad index, a[i+j*a->nbands] == 0.f,
// so as long as there is no segfault, we don't have to branch here.
for( j = 0; j < a.nbands; ++j )
bi += a.a[i+j*a.apitch] * x[i+sdata[j]];
if( add )
b[i] = bi + y[i];
else
b[i] = bi - y[i];
}
else
break;
i += nthreads;
}
}
/*!
* \sa bmAx()
*/
template <int nbands>
__global__ void bmAx_k( float* b, const BandedMatrix a, float const* x )
{
bmAx<nbands>(b,a,x);
}
/*!
* \sa bmAxpy()
*/
template <int nbands, bool add>
__global__ void bmAxpy_k( float* b, const BandedMatrix a, float const* x, float const* y )
{
bmAxpy<nbands, add>(b,a,x,y);
}