-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathCompressedMatrix.cu
165 lines (139 loc) · 4.31 KB
/
CompressedMatrix.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
/* CompressedMatrix.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 "CompressedMatrix.h"
#include <cuda.h>
void csmInit( CompressedMatrix* m, int rows, int cols, size_t length )
{
m->rows = rows;
m->cols = cols;
m->nnz = 0;
m->length = length;
cudaMalloc( &(m->k), length );
cudaMalloc( &(m->j), length );
cudaMalloc( &(m->p), rows+1 );
cudaMemset( m->p, 0x00, (rows+1)*sizeof(int));
}
__global__ void csmInit( CompressedMatrix* m, int rows, int cols, int* p, int* j, float* k, size_t nnz )
{
m->rows = rows;
m->cols = cols;
m->nnz = nnz;
m->length = nnz;
m->p = p;
m->j = j;
m->k = k;
}
// Thread i is responsible in each iteration for b[i + nthreads*iteration].
// sdata[ti] is used in each block to store partial result for b[i + nthreads*iteration].
template<bool symmetric, bool addy>
__device__ void csmAxpy( float* b, CompressedMatrix const* a, float const* x, float const* y )
{
extern __shared__ float sdata[];
int nthreads = blockDim.x*gridDim.x;
int ti = threadIdx.x;
int i = blockIdx.x*blockDim.x + threadIdx.x;
float* mysdata = sdata+ti;
int row = i;
int numrows = a->rows;
int ndx, ndxEnd;
while(true)
{
if( row <= numrows )
{
ndx = a->p[row];
ndxEnd = a->p[row+1];
*mysdata = 0.0f;
while( ndx < ndxEnd )
{
*mysdata += a->k[ndx] * x[a->j[ndx]];
++ndx;
}
}
// Wait so that sdata is fully populated.
// Since all threads sync'd, this should result in sequential access.
__syncthreads();
if( row <= numrows )
b[row] = *mysdata + *y;
// If any thread has fallen off, they will all fall off next iteration,
// so end it now!
if( __any( row > numrows ) )
break;
row += nthreads;
}
}
template<bool symmetric>
__device__ void csmAx( float* b, CompressedMatrix const* a, float const* x )
{
extern __shared__ float sdata[];
int nthreads = blockDim.x*gridDim.x;
int ti = threadIdx.x;
int i = blockIdx.x*blockDim.x + threadIdx.x;
float* mysdata = sdata+ti;
b += i;
float const* bend = b + a->rows;
int const* ap = a->p + i;
int const* aj;
float const* ak;
float const* akend;
while(true)
{
if( b < bend )
{
ak = a->k + *ap;
aj = a->j + *ap;
akend = a->k + *(ap+1);
*mysdata = 0.0f;
while( ak < akend )
{
*mysdata += *ak * x[*aj];
++ak;
++aj;
}
// Wait so that sdata is fully populated.
__syncthreads();
// Since all threads sync'd, this should result in sequential access.
*b = *mysdata;
}
else
{
// These threads have fallen off the end, so just have them sit.
__syncthreads();
}
// If any thread has fallen off, they will all fall off next iteration,
// so end it now!
if( __any( b >= bend ) )
break;
b += nthreads;
ap += nthreads;
}
}
__global__ void csmAxpy_k( float* b, CompressedMatrix const* a, float const* x, float const* y )
{
csmAxpy<false, true>(b, a, x, y);
}
__global__ void csmAxpy_k_symm( float* b, CompressedMatrix const* a, float const* x, float const* y )
{
csmAxpy<true, true>(b, a, x, y);
}
__global__ void csmAxmy_k( float* b, CompressedMatrix const* a, float const* x, float const* y )
{
csmAxpy<false, false>(b, a, x, y);
}
__global__ void csmAxmy_k_symm( float* b, CompressedMatrix const* a, float const* x, float const* y )
{
csmAxpy<true, false>(b, a, x, y);
}