-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutils_sptrsv_cuda.h
218 lines (192 loc) · 9.19 KB
/
utils_sptrsv_cuda.h
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
208
209
210
211
212
213
214
215
216
217
218
#ifndef _UTILS_SPTRSV_CUDA_
#define _UTILS_SPTRSV_CUDA_
#include "common.h"
#include <cuda_runtime.h>
#include "cusparse.h"
#include "utils_reordering.h"
typedef struct SpTRSV_block
{
int method;
int num_threads;
int num_blocks;
int m;
int nnzTR;
int substitution;
VALUE_TYPE *d_b;
VALUE_TYPE *d_x;
cusparseSolvePolicy_t policy;
cusparseOperation_t trans;
cusparseHandle_t handle;
double alpha_double;
float alpha_float;
cusparseMatDescr_t descr;
csrsv2Info_t info;
void *pBuffer;
int m_lv;
int offset;
int *d_graphInDegree;
VALUE_TYPE *d_left_sum;
int *d_while_profiler;
int *d_id_extractor;
int *d_levelItem;
int nlv;
int *levelPtr;
int *m_lv_array;
int *offset_array;
int *nnz_lv_array;
} SpTRSV_block;
__global__ void sptrsv_syncfree_csc_cuda_analyser(const int *d_cscRowIdx,
const int m,
const int nnz,
int *d_graphInDegree)
{
const int global_id = blockIdx.x * blockDim.x + threadIdx.x; //get_global_id(0);
if (global_id < nnz)
{
atomicAdd(&d_graphInDegree[d_cscRowIdx[global_id]], 1);
}
}
__global__ void sptrsv_syncfree_warpvec_csc_cuda_executor(const int *d_cscColPtr,
const int *d_cscRowIdx,
const VALUE_TYPE *d_cscVal,
int *d_graphInDegree,
VALUE_TYPE *d_left_sum,
const int m,
const int substitution,
const VALUE_TYPE *d_b,
VALUE_TYPE *d_x,
int *d_while_profiler,
int *d_id_extractor,
int *d_levelItem)
{
// Initialize
const int lane_id = (WARP_SIZE - 1) & threadIdx.x;
int global_x_id = 0;
if (!lane_id)
global_x_id = atomicAdd(d_id_extractor, 1);
global_x_id = __shfl_sync(0xffffffff, global_x_id, 0);
if (global_x_id >= m)
return;
// substitution is forward or backward
global_x_id = substitution == SUBSTITUTION_FORWARD ? global_x_id : m - 1 - global_x_id;
// Prefetch
const int colstart = d_cscColPtr[global_x_id] - d_cscColPtr[0];
const int colstop = d_cscColPtr[global_x_id + 1] - d_cscColPtr[0];
const int pos = substitution == SUBSTITUTION_FORWARD ? colstart : colstop - 1;
const VALUE_TYPE coef = (VALUE_TYPE)1 / d_cscVal[pos];
const int perm_id = global_x_id;
const VALUE_TYPE valb = d_b[perm_id];
// Consumer
do
{
__threadfence_block();
} while (d_graphInDegree[perm_id] != 1);
VALUE_TYPE xi = d_left_sum[perm_id];
xi = (valb - xi) * coef;
// Producer
const int start_ptr = substitution == SUBSTITUTION_FORWARD ? colstart + 1 : colstart;
const int stop_ptr = substitution == SUBSTITUTION_FORWARD ? colstop : colstop - 1;
for (int jj = start_ptr + lane_id; jj < stop_ptr; jj += WARP_SIZE)
{
const int j = substitution == SUBSTITUTION_FORWARD ? jj : stop_ptr - 1 - (jj - start_ptr);
const int rowIdx = d_cscRowIdx[j];
atomicAdd(&d_left_sum[rowIdx], xi * d_cscVal[j]);
__threadfence();
atomicSub(&d_graphInDegree[rowIdx], 1);
}
//finish
if (!lane_id)
d_x[perm_id] = xi;
}
__global__ void sptrsv_levelset_threadsca_csr_cuda_executor_fasttrack(const int *d_csrRowPtr,
const int *d_csrColIdx,
const VALUE_TYPE *d_csrVal,
const int m,
const int m_total,
const int offset,
const int substitution,
const VALUE_TYPE *d_b,
VALUE_TYPE *d_x)
{
const int global_x_id = blockIdx.x * blockDim.x + threadIdx.x;
if (global_x_id < m)
{
int rowidx = global_x_id + offset;
rowidx = substitution == SUBSTITUTION_FORWARD ? rowidx : m_total - 1 - global_x_id - offset;
const int pos = substitution == SUBSTITUTION_FORWARD ? (d_csrRowPtr[rowidx + 1] - d_csrRowPtr[0]) - 1 : (d_csrRowPtr[rowidx] - d_csrRowPtr[0]);
d_x[rowidx] = d_b[rowidx] / d_csrVal[pos];
}
}
__global__ void sptrsv_levelset_threadsca_csr_cuda_executor(const int *d_csrRowPtr,
const int *d_csrColIdx,
const VALUE_TYPE *d_csrVal,
const int m,
const int m_total,
const int offset,
const int substitution,
const VALUE_TYPE *d_b,
VALUE_TYPE *d_x)
{
const int global_x_id = blockIdx.x * blockDim.x + threadIdx.x;
if (global_x_id < m)
{
int rowidx = global_x_id + offset;
rowidx = substitution == SUBSTITUTION_FORWARD ? rowidx : m_total - 1 - global_x_id - offset;
const int start = substitution == SUBSTITUTION_FORWARD ? (d_csrRowPtr[rowidx] - d_csrRowPtr[0]) : (d_csrRowPtr[rowidx] - d_csrRowPtr[0]) + 1;
const int stop = substitution == SUBSTITUTION_FORWARD ? (d_csrRowPtr[rowidx + 1] - d_csrRowPtr[0]) : (d_csrRowPtr[rowidx + 1] - d_csrRowPtr[0]) + 1;
VALUE_TYPE sum = 0;
for (int j = start; j < stop - 1; j++)
sum += d_x[d_csrColIdx[j]] * d_csrVal[j];
const int pos = substitution == SUBSTITUTION_FORWARD ? (d_csrRowPtr[rowidx + 1] - d_csrRowPtr[0]) - 1 : d_csrRowPtr[rowidx] - d_csrRowPtr[0];
d_x[rowidx] = (d_b[rowidx] - sum) / d_csrVal[pos];
}
}
__global__ void sptrsv_levelset_warpvec_csr_cuda_executor(const int *d_csrRowPtr,
const int *d_csrColIdx,
const VALUE_TYPE *d_csrVal,
const int m,
const int m_total,
const int offset,
const int substitution,
const VALUE_TYPE *d_b,
VALUE_TYPE *d_x)
{
const int global_id = blockIdx.x * blockDim.x + threadIdx.x;
// Initialize
int rowidx = global_id / WARP_SIZE;
if (rowidx >= m)
return;
rowidx += offset;
rowidx = substitution == SUBSTITUTION_FORWARD ? rowidx : m_total - 1 - rowidx;
const int lane_id = (WARP_SIZE - 1) & threadIdx.x;
const int start = substitution == SUBSTITUTION_FORWARD ? (d_csrRowPtr[rowidx] - d_csrRowPtr[0]) : (d_csrRowPtr[rowidx] - d_csrRowPtr[0]) + 1;
const int stop = substitution == SUBSTITUTION_FORWARD ? (d_csrRowPtr[rowidx + 1] - d_csrRowPtr[0]) : (d_csrRowPtr[rowidx + 1] - d_csrRowPtr[0]) + 1;
VALUE_TYPE sum = 0;
for (int j = start + lane_id; j < stop - 1; j += WARP_SIZE)
{
sum += d_x[d_csrColIdx[j]] * d_csrVal[j];
}
sum = sum_32_shfl(sum);
//finish
if (!lane_id)
{
const int pos = substitution == SUBSTITUTION_FORWARD ? (d_csrRowPtr[rowidx + 1] - d_csrRowPtr[0]) - 1 : (d_csrRowPtr[rowidx] - d_csrRowPtr[0]);
d_x[rowidx] = (d_b[rowidx] - sum) / d_csrVal[pos];
}
}
__global__ void sptrsv_syncfree_csc_cuda_executor_fasttrack(const int *d_cscColPtr,
const int *d_cscRowIdx,
const VALUE_TYPE *d_cscVal,
const int m,
const int substitution,
const VALUE_TYPE *d_b,
VALUE_TYPE *d_x)
{
const int global_x_id = blockIdx.x * blockDim.x + threadIdx.x;
if (global_x_id < m)
{
const int pos = substitution == SUBSTITUTION_FORWARD ? (d_cscColPtr[global_x_id] - d_cscColPtr[0]) : (d_cscColPtr[global_x_id + 1] - d_cscColPtr[0]) - 1;
d_x[global_x_id] = d_b[global_x_id] / d_cscVal[pos];
}
}
#endif