-
Notifications
You must be signed in to change notification settings - Fork 209
/
helpers.jl
320 lines (265 loc) · 9.88 KB
/
helpers.jl
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
# cuSPARSE helper functions
## matrix descriptor
mutable struct CuMatrixDescriptor
handle::cusparseMatDescr_t
function CuMatrixDescriptor()
descr_ref = Ref{cusparseMatDescr_t}()
cusparseCreateMatDescr(descr_ref)
obj = new(descr_ref[])
finalizer(cusparseDestroyMatDescr, obj)
obj
end
end
Base.unsafe_convert(::Type{cusparseMatDescr_t}, desc::CuMatrixDescriptor) = desc.handle
function CuMatrixDescriptor(MatrixType::Char, FillMode::Char, DiagType::Char, IndexBase::Char)
desc = CuMatrixDescriptor()
if MatrixType != 'G'
cusparseSetMatType(desc, MatrixType)
end
cusparseSetMatFillMode(desc, FillMode)
cusparseSetMatDiagType(desc, DiagType)
if IndexBase != 'Z'
cusparseSetMatIndexBase(desc, IndexBase)
end
return desc
end
## dense vector descriptor
mutable struct CuDenseVectorDescriptor
handle::cusparseDnVecDescr_t
function CuDenseVectorDescriptor(T::DataType, n::Integer)
desc_ref = Ref{cusparseDnVecDescr_t}()
cusparseCreateDnVec(desc_ref, n, CU_NULL, T)
obj = new(desc_ref[])
finalizer(cusparseDestroyDnVec, obj)
obj
end
function CuDenseVectorDescriptor(x::DenseCuVector)
desc_ref = Ref{cusparseDnVecDescr_t}()
cusparseCreateDnVec(desc_ref, length(x), x, eltype(x))
obj = new(desc_ref[])
finalizer(cusparseDestroyDnVec, obj)
obj
end
end
Base.unsafe_convert(::Type{cusparseDnVecDescr_t}, desc::CuDenseVectorDescriptor) = desc.handle
## sparse vector descriptor
mutable struct CuSparseVectorDescriptor
handle::cusparseSpVecDescr_t
function CuSparseVectorDescriptor(x::CuSparseVector, IndexBase::Char)
desc_ref = Ref{cusparseSpVecDescr_t}()
cusparseCreateSpVec(desc_ref, length(x), nnz(x), nonzeroinds(x), nonzeros(x),
eltype(nonzeroinds(x)), IndexBase, eltype(x))
obj = new(desc_ref[])
finalizer(cusparseDestroySpVec, obj)
obj
end
end
Base.unsafe_convert(::Type{cusparseSpVecDescr_t}, desc::CuSparseVectorDescriptor) = desc.handle
## dense matrix descriptor
mutable struct CuDenseMatrixDescriptor
handle::cusparseDnMatDescr_t
function CuDenseMatrixDescriptor(T::DataType, m::Integer, n::Integer; transposed::Bool=false)
desc_ref = Ref{cusparseDnMatDescr_t}()
if transposed
cusparseCreateDnMat(desc_ref, n, m, m, CU_NULL, T, 'R')
else
cusparseCreateDnMat(desc_ref, m, n, m, CU_NULL, T, 'C')
end
obj = new(desc_ref[])
finalizer(cusparseDestroyDnMat, obj)
obj
end
function CuDenseMatrixDescriptor(A::DenseCuMatrix; transposed::Bool=false)
desc_ref = Ref{cusparseDnMatDescr_t}()
if transposed
cusparseCreateDnMat(desc_ref, reverse(size(A))..., stride(A,2), A, eltype(A), 'R')
else
cusparseCreateDnMat(desc_ref, size(A)..., stride(A,2), A, eltype(A), 'C')
end
obj = new(desc_ref[])
finalizer(cusparseDestroyDnMat, obj)
obj
end
end
Base.unsafe_convert(::Type{cusparseDnMatDescr_t}, desc::CuDenseMatrixDescriptor) = desc.handle
## sparse matrix descriptor
mutable struct CuSparseMatrixDescriptor
handle::cusparseSpMatDescr_t
function CuSparseMatrixDescriptor(A::CuSparseMatrixCOO, IndexBase::Char; transposed::Bool=false)
desc_ref = Ref{cusparseSpMatDescr_t}()
if transposed
cusparseCreateCoo(
desc_ref,
reverse(size(A))..., nnz(A),
A.colInd, A.rowInd, nonzeros(A),
eltype(A.colInd), IndexBase, eltype(nonzeros(A))
)
else
cusparseCreateCoo(
desc_ref,
size(A)..., nnz(A),
A.rowInd, A.colInd, nonzeros(A),
eltype(A.rowInd), IndexBase, eltype(nonzeros(A))
)
end
obj = new(desc_ref[])
finalizer(cusparseDestroySpMat, obj)
return obj
end
function CuSparseMatrixDescriptor(::Type{CuSparseMatrixCOO}, Tv::DataType, Ti::DataType, m::Integer, n::Integer, IndexBase::Char)
desc_ref = Ref{cusparseSpMatDescr_t}()
cusparseCreateCoo(desc_ref, m, n, Ti(0), CU_NULL, CU_NULL, CU_NULL, Ti, IndexBase, Tv)
obj = new(desc_ref[])
finalizer(cusparseDestroySpMat, obj)
return obj
end
function CuSparseMatrixDescriptor(A::CuSparseMatrixCSR, IndexBase::Char; transposed::Bool=false)
desc_ref = Ref{cusparseSpMatDescr_t}()
if transposed
cusparseCreateCsc(
desc_ref,
reverse(size(A))..., nnz(A),
A.rowPtr, A.colVal, nonzeros(A),
eltype(A.rowPtr), eltype(A.colVal), IndexBase, eltype(nonzeros(A))
)
else
cusparseCreateCsr(
desc_ref,
size(A)..., nnz(A),
A.rowPtr, A.colVal, nonzeros(A),
eltype(A.rowPtr), eltype(A.colVal), IndexBase, eltype(nonzeros(A))
)
end
obj = new(desc_ref[])
finalizer(cusparseDestroySpMat, obj)
return obj
end
function CuSparseMatrixDescriptor(::Type{CuSparseMatrixCSR}, rowPtr::CuVector, Tv::DataType, Ti::DataType, m::Integer, n::Integer, IndexBase::Char)
desc_ref = Ref{cusparseSpMatDescr_t}()
cusparseCreateCsr(desc_ref, m, n, Ti(0), rowPtr, CU_NULL, CU_NULL, Ti, Ti, IndexBase, Tv)
obj = new(desc_ref[])
finalizer(cusparseDestroySpMat, obj)
return obj
end
function CuSparseMatrixDescriptor(A::CuSparseMatrixCSC, IndexBase::Char; transposed::Bool=false)
desc_ref = Ref{cusparseSpMatDescr_t}()
if transposed
# many algorithms, e.g. mv!, mm!, sv! and sm! do not support CSC matrices but we can use CSR matrices that model their transposed.
cusparseCreateCsr(
desc_ref,
reverse(size(A))..., nnz(A),
A.colPtr, rowvals(A), nonzeros(A),
eltype(A.colPtr), eltype(rowvals(A)), IndexBase, eltype(nonzeros(A))
)
else
cusparseCreateCsc(
desc_ref,
size(A)..., nnz(A),
A.colPtr, rowvals(A), nonzeros(A),
eltype(A.colPtr), eltype(rowvals(A)), IndexBase, eltype(nonzeros(A))
)
end
obj = new(desc_ref[])
finalizer(cusparseDestroySpMat, obj)
return obj
end
function CuSparseMatrixDescriptor(::Type{CuSparseMatrixCSC}, colPtr::CuVector, Tv::DataType, Ti::DataType, m::Integer, n::Integer, IndexBase::Char)
desc_ref = Ref{cusparseSpMatDescr_t}()
cusparseCreateCsc(desc_ref, m, n, Ti(0), colPtr, CU_NULL, CU_NULL, Ti, Ti, IndexBase, Tv)
obj = new(desc_ref[])
finalizer(cusparseDestroySpMat, obj)
return obj
end
function CuSparseMatrixDescriptor(A::CuSparseMatrixBSR, IndexBase::Char)
desc_ref = Ref{cusparseSpMatDescr_t}()
cusparseCreateBsr(
desc_ref,
size(A)..., nnz(A),
A.blockDim, A.blockDim,
A.rowPtr, A.colVal, nonzeros(A),
eltype(A.rowPtr), eltype(A.colVal), IndexBase,
eltype(nonzeros(A)), A.dir
)
obj = new(desc_ref[])
finalizer(cusparseDestroySpMat, obj)
return obj
end
end
Base.unsafe_convert(::Type{cusparseSpMatDescr_t}, desc::CuSparseMatrixDescriptor) = desc.handle
mutable struct CuSpGEMMDescriptor
handle::cusparseSpGEMMDescr_t
function CuSpGEMMDescriptor()
descr_ref = Ref{cusparseSpGEMMDescr_t}()
cusparseSpGEMM_createDescr(descr_ref)
obj = new(descr_ref[])
finalizer(cusparseSpGEMM_destroyDescr, obj)
obj
end
end
Base.unsafe_convert(::Type{cusparseSpGEMMDescr_t}, desc::CuSpGEMMDescriptor) = desc.handle
mutable struct CuSparseSpSVDescriptor
handle::cusparseSpSVDescr_t
function CuSparseSpSVDescriptor()
descr_ref = Ref{cusparseSpSVDescr_t}()
cusparseSpSV_createDescr(descr_ref)
obj = new(descr_ref[])
finalizer(cusparseSpSV_destroyDescr, obj)
obj
end
end
Base.unsafe_convert(::Type{cusparseSpSVDescr_t}, desc::CuSparseSpSVDescriptor) = desc.handle
mutable struct CuSparseSpSMDescriptor
handle::cusparseSpSMDescr_t
function CuSparseSpSMDescriptor()
descr_ref = Ref{cusparseSpSMDescr_t}()
cusparseSpSM_createDescr(descr_ref)
obj = new(descr_ref[])
finalizer(cusparseSpSM_destroyDescr, obj)
obj
end
end
Base.unsafe_convert(::Type{cusparseSpSMDescr_t}, desc::CuSparseSpSMDescriptor) = desc.handle
mutable struct IC0Info
info::csric02Info_t
function IC0Info()
info_ref = Ref{csric02Info_t}()
cusparseCreateCsric02Info(info_ref)
obj = new(info_ref[])
finalizer(cusparseDestroyCsric02Info, obj)
obj
end
end
Base.unsafe_convert(::Type{csric02Info_t}, info::IC0Info) = info.info
mutable struct IC0InfoBSR
info::bsric02Info_t
function IC0InfoBSR()
info_ref = Ref{bsric02Info_t}()
cusparseCreateBsric02Info(info_ref)
obj = new(info_ref[])
finalizer(cusparseDestroyBsric02Info, obj)
obj
end
end
Base.unsafe_convert(::Type{bsric02Info_t}, info::IC0InfoBSR) = info.info
mutable struct ILU0Info
info::csrilu02Info_t
function ILU0Info()
info_ref = Ref{csrilu02Info_t}()
cusparseCreateCsrilu02Info(info_ref)
obj = new(info_ref[])
finalizer(cusparseDestroyCsrilu02Info, obj)
obj
end
end
Base.unsafe_convert(::Type{csrilu02Info_t}, info::ILU0Info) = info.info
mutable struct ILU0InfoBSR
info::bsrilu02Info_t
function ILU0InfoBSR()
info_ref = Ref{bsrilu02Info_t}()
cusparseCreateBsrilu02Info(info_ref)
obj = new(info_ref[])
finalizer(cusparseDestroyBsrilu02Info, obj)
obj
end
end
Base.unsafe_convert(::Type{bsrilu02Info_t}, info::ILU0InfoBSR) = info.info