-
Notifications
You must be signed in to change notification settings - Fork 239
/
Copy patharray.jl
597 lines (496 loc) · 23.6 KB
/
array.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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
# custom extension of CuArray in CUDArt for sparse vectors/matrices
# using CSC format for interop with Julia's native sparse functionality
export CuSparseMatrixCSC, CuSparseMatrixCSR, CuSparseMatrixBSR, CuSparseMatrixCOO,
CuSparseMatrix, AbstractCuSparseMatrix,
CuSparseVector,
CuSparseVecOrMat
using LinearAlgebra: BlasFloat
using SparseArrays: nonzeroinds, dimlub, SparseMatrixCSC, SparseVector
abstract type AbstractCuSparseArray{Tv, Ti, N} <: AbstractSparseArray{Tv, Ti, N} end
const AbstractCuSparseVector{Tv, Ti} = AbstractCuSparseArray{Tv, Ti, 1}
const AbstractCuSparseMatrix{Tv, Ti} = AbstractCuSparseArray{Tv, Ti, 2}
Base.convert(T::Type{<:AbstractCuSparseArray}, m::AbstractArray) = m isa T ? m : T(m)
mutable struct CuSparseVector{Tv, Ti} <: AbstractCuSparseVector{Tv, Ti}
iPtr::CuVector{Ti}
nzVal::CuVector{Tv}
len::Int
nnz::Ti
function CuSparseVector{Tv, Ti}(iPtr::CuVector{<:Integer}, nzVal::CuVector,
len::Integer) where {Tv, Ti <: Integer}
new{Tv, Ti}(iPtr, nzVal, len, length(nzVal))
end
end
CuSparseVector(A::CuSparseVector) = A
function CuSparseVector{Tv, Ti}(A::CuSparseVector) where {Tv, Ti}
return CuSparseVector{Tv, Ti}(A.iPtr, A.nzVal, A.len)
end
function CUDA.unsafe_free!(xs::CuSparseVector)
unsafe_free!(nonzeroinds(xs))
unsafe_free!(nonzeros(xs))
return
end
mutable struct CuSparseMatrixCSC{Tv, Ti} <: AbstractCuSparseMatrix{Tv, Ti}
colPtr::CuVector{Ti}
rowVal::CuVector{Ti}
nzVal::CuVector{Tv}
dims::NTuple{2,Int}
nnz::Ti
function CuSparseMatrixCSC{Tv, Ti}(colPtr::CuVector{<:Integer}, rowVal::CuVector{<:Integer},
nzVal::CuVector, dims::NTuple{2,<:Integer}) where {Tv, Ti <: Integer}
new{Tv, Ti}(colPtr, rowVal, nzVal, dims, length(nzVal))
end
end
CuSparseMatrixCSC(A::CuSparseMatrixCSC) = A
function CuSparseMatrixCSC{Tv, Ti}(A::CuSparseMatrixCSC) where {Tv, Ti}
return CuSparseMatrixCSC{Tv, Ti}(A.colPtr, A.rowVal, A.nzVal, A.dims)
end
function CUDA.unsafe_free!(xs::CuSparseMatrixCSC)
unsafe_free!(xs.colPtr)
unsafe_free!(rowvals(xs))
unsafe_free!(nonzeros(xs))
return
end
"""
CuSparseMatrixCSR{Tv, Ti} <: AbstractCuSparseMatrix{Tv, Ti}
Container to hold sparse matrices in compressed sparse row (CSR) format on the
GPU.
!!! note
Most CUSPARSE operations work with CSR formatted matrices, rather
than CSC.
!!! compat "CUDA 11"
Support of indices type rather than `Cint` (`Int32`) requires at least CUDA 11.
"""
mutable struct CuSparseMatrixCSR{Tv, Ti} <: AbstractCuSparseMatrix{Tv, Ti}
rowPtr::CuVector{Ti}
colVal::CuVector{Ti}
nzVal::CuVector{Tv}
dims::NTuple{2,Int}
nnz::Ti
function CuSparseMatrixCSR{Tv, Ti}(rowPtr::CuVector{<:Integer}, colVal::CuVector{<:Integer},
nzVal::CuVector, dims::NTuple{2,Int}) where {Tv, Ti<:Integer}
new{Tv, Ti}(rowPtr, colVal, nzVal, dims, length(nzVal))
end
end
CuSparseMatrixCSR(A::CuSparseMatrixCSR) = A
function CuSparseMatrixCSR{Tv, Ti}(A::CuSparseMatrixCSR) where {Tv, Ti}
return CuSparseMatrixCSR{Tv, Ti}(A.rowPtr, A.colVal, A.nzVal, A.dims)
end
function CUDA.unsafe_free!(xs::CuSparseMatrixCSR)
unsafe_free!(xs.rowPtr)
unsafe_free!(xs.colVal)
unsafe_free!(nonzeros(xs))
return
end
"""
Container to hold sparse matrices in block compressed sparse row (BSR) format on
the GPU. BSR format is also used in Intel MKL, and is suited to matrices that are
"block" sparse - rare blocks of non-sparse regions.
"""
mutable struct CuSparseMatrixBSR{Tv, Ti} <: AbstractCuSparseMatrix{Tv, Ti}
rowPtr::CuVector{Ti}
colVal::CuVector{Ti}
nzVal::CuVector{Tv}
dims::NTuple{2,Int}
blockDim::Ti
dir::SparseChar
nnzb::Ti
function CuSparseMatrixBSR{Tv, Ti}(rowPtr::CuVector{<:Integer}, colVal::CuVector{<:Integer},
nzVal::CuVector, dims::NTuple{2,<:Integer},
blockDim::Integer, dir::SparseChar, nnz::Integer) where {Tv, Ti<:Integer}
new{Tv, Ti}(rowPtr, colVal, nzVal, dims, blockDim, dir, nnz)
end
end
CuSparseMatrixBSR(A::CuSparseMatrixBSR) = A
function CuSparseMatrixBSR{Tv, Ti}(A::CuSparseMatrixBSR) where {Tv, Ti}
return CuSparseMatrixBSR{Tv, Ti}(A.rowPtr, A.colVal, A.nzVal, A.dims, A.blockDim, A.dir, A.nnz)
end
function CUDA.unsafe_free!(xs::CuSparseMatrixBSR)
unsafe_free!(xs.rowPtr)
unsafe_free!(xs.colVal)
unsafe_free!(nonzeros(xs))
return
end
"""
Container to hold sparse matrices in coordinate (COO) format on the GPU. COO
format is mainly useful to initially construct sparse matrices, afterwards
switch to [`CuSparseMatrixCSR`](@ref) for more functionality.
"""
mutable struct CuSparseMatrixCOO{Tv, Ti} <: AbstractCuSparseMatrix{Tv, Ti}
rowInd::CuVector{Ti}
colInd::CuVector{Ti}
nzVal::CuVector{Tv}
dims::NTuple{2,Int}
nnz::Ti
function CuSparseMatrixCOO{Tv, Ti}(rowInd::CuVector{<:Integer}, colInd::CuVector{<:Integer},
nzVal::CuVector, dims::NTuple{2,Int}=(dimlub(rowInd),dimlub(colInd)),
nnz::Integer=length(nzVal)) where {Tv, Ti}
new{Tv, Ti}(rowInd,colInd,nzVal,dims,nnz)
end
end
CuSparseMatrixCOO(A::CuSparseMatrixCOO) = A
function CuSparseMatrixCOO{Tv, Ti}(A::CuSparseMatrixCOO) where {Tv, Ti}
return CuSparseMatrixCOO{Tv, Ti}(A.rowInd, A.colInd, A.nzVal, A.dims, A.nnz)
end
"""
Utility union type of [`CuSparseMatrixCSC`](@ref), [`CuSparseMatrixCSR`](@ref),
[`CuSparseMatrixBSR`](@ref), [`CuSparseMatrixCOO`](@ref).
"""
const CuSparseMatrix{Tv, Ti} = Union{
CuSparseMatrixCSC{Tv, Ti},
CuSparseMatrixCSR{Tv, Ti},
CuSparseMatrixBSR{Tv, Ti},
CuSparseMatrixCOO{Tv, Ti}
}
const CuSparseVecOrMat = Union{CuSparseVector,CuSparseMatrix}
# NOTE: we use Cint as default Ti on CUDA instead of Int to provide
# maximum compatiblity to old CUSPARSE APIs
function CuSparseVector{Tv}(iPtr::CuVector{<:Integer}, nzVal::CuVector, len::Integer) where {Tv}
CuSparseVector{Tv, Cint}(convert(CuVector{Cint}, iPtr), nzVal, len)
end
function CuSparseMatrixCSC{Tv}(colPtr::CuVector{<:Integer}, rowVal::CuVector{<:Integer},
nzVal::CuVector, dims::NTuple{2,<:Integer}) where {Tv}
CuSparseMatrixCSC{Tv, Cint}(colPtr, rowVal, nzVal, dims)
end
function CuSparseMatrixCSR{Tv}(rowPtr::CuVector{<:Integer}, colVal::CuVector{<:Integer},
nzVal::CuVector, dims::NTuple{2,Int}) where {Tv}
CuSparseMatrixCSR{Tv, Cint}(rowPtr, colVal, nzVal, dims)
end
function CuSparseMatrixBSR{Tv}(rowPtr::CuVector{<:Integer}, colVal::CuVector{<:Integer},
nzVal::CuVector, dims::NTuple{2,<:Integer},
blockDim::Integer, dir::SparseChar, nnz::Integer) where {Tv}
CuSparseMatrixBSR{Tv, Cint}(rowPtr, colVal, nzVal, dims, blockDim, dir, nnz)
end
function CuSparseMatrixCOO{Tv}(rowInd::CuVector{<:Integer}, colInd::CuVector{<:Integer},
nzVal::CuVector, dims::NTuple{2,Int}=(dimlub(rowInd),dimlub(colInd)),
nnz::Integer=length(nzVal)) where {Tv}
CuSparseMatrixCOO{Tv, Cint}(rowInd,colInd,nzVal,dims,nnz)
end
## convenience constructors
CuSparseVector(iPtr::DenseCuArray{<:Integer}, nzVal::DenseCuArray{T}, len::Integer) where {T} =
CuSparseVector{T}(iPtr, nzVal, len)
CuSparseMatrixCSC(colPtr::DenseCuArray{<:Integer}, rowVal::DenseCuArray{<:Integer},
nzVal::DenseCuArray{T}, dims::NTuple{2,Int}) where {T} =
CuSparseMatrixCSC{T}(colPtr, rowVal, nzVal, dims)
CuSparseMatrixCSR(rowPtr::DenseCuArray, colVal::DenseCuArray, nzVal::DenseCuArray{T}, dims::NTuple{2,Int}) where T =
CuSparseMatrixCSR{T}(rowPtr, colVal, nzVal, dims)
CuSparseMatrixBSR(rowPtr::DenseCuArray, colVal::DenseCuArray, nzVal::DenseCuArray{T}, blockDim, dir, nnz,
dims::NTuple{2,Int}) where T =
CuSparseMatrixBSR{T}(rowPtr, colVal, nzVal, dims, blockDim, dir, nnz)
CuSparseMatrixCOO(rowInd::DenseCuArray, colInd::DenseCuArray, nzVal::DenseCuArray{T}, dims::NTuple{2,Int}, nnz) where T =
CuSparseMatrixCOO{T}(rowInd, colInd, nzVal, dims, nnz)
Base.similar(Vec::CuSparseVector) = CuSparseVector(copy(nonzeroinds(Vec)), similar(nonzeros(Vec)), length(Vec))
Base.similar(Mat::CuSparseMatrixCSC) = CuSparseMatrixCSC(copy(Mat.colPtr), copy(rowvals(Mat)), similar(nonzeros(Mat)), size(Mat))
Base.similar(Mat::CuSparseMatrixCSR) = CuSparseMatrixCSR(copy(Mat.rowPtr), copy(Mat.colVal), similar(nonzeros(Mat)), size(Mat))
Base.similar(Mat::CuSparseMatrixBSR) = CuSparseMatrixBSR(copy(Mat.rowPtr), copy(Mat.colVal), similar(nonzeros(Mat)), Mat.blockDim, Mat.dir, nnz(Mat), size(Mat))
Base.similar(Mat::CuSparseMatrixCOO) = CuSparseMatrixCOO(copy(Mat.rowInd), copy(Mat.colInd), similar(nonzeros(Mat)), size(Mat), nnz(Mat))
Base.similar(Vec::CuSparseVector, T::Type) = CuSparseVector(copy(nonzeroinds(Vec)), similar(nonzeros(Vec), T), length(Vec))
Base.similar(Mat::CuSparseMatrixCSC, T::Type) = CuSparseMatrixCSC(copy(Mat.colPtr), copy(rowvals(Mat)), similar(nonzeros(Mat), T), size(Mat))
Base.similar(Mat::CuSparseMatrixCSR, T::Type) = CuSparseMatrixCSR(copy(Mat.rowPtr), copy(Mat.colVal), similar(nonzeros(Mat), T), size(Mat))
Base.similar(Mat::CuSparseMatrixBSR, T::Type) = CuSparseMatrixBSR(copy(Mat.rowPtr), copy(Mat.colVal), similar(nonzeros(Mat), T), Mat.blockDim, Mat.dir, nnz(Mat), size(Mat))
Base.similar(Mat::CuSparseMatrixCOO, T::Type) = CuSparseMatrixCOO(copy(Mat.rowInd), copy(Mat.colInd), similar(nonzeros(Mat), T), size(Mat), nnz(Mat))
## array interface
Base.length(g::CuSparseVector) = g.len
Base.size(g::CuSparseVector) = (g.len,)
Base.ndims(g::CuSparseVector) = 1
Base.length(g::CuSparseMatrix) = prod(g.dims)
Base.size(g::CuSparseMatrix) = g.dims
Base.ndims(g::CuSparseMatrix) = 2
function Base.size(g::CuSparseVector, d::Integer)
if d == 1
return g.len
elseif d > 1
return 1
else
throw(ArgumentError("dimension must be ≥ 1, got $d"))
end
end
function Base.size(g::CuSparseMatrix, d::Integer)
if 1 <= d <= 2
return g.dims[d]
elseif d > 1
return 1
else
throw(ArgumentError("dimension must be ≥ 1, got $d"))
end
end
Base.eltype(g::CuSparseMatrix{T}) where T = T
## sparse array interface
SparseArrays.nnz(g::AbstractCuSparseArray) = g.nnz
SparseArrays.nonzeros(g::AbstractCuSparseArray) = g.nzVal
SparseArrays.nonzeroinds(g::AbstractCuSparseVector) = g.iPtr
SparseArrays.rowvals(g::CuSparseMatrixCSC) = g.rowVal
LinearAlgebra.issymmetric(M::Union{CuSparseMatrixCSC,CuSparseMatrixCSR}) = false
LinearAlgebra.ishermitian(M::Union{CuSparseMatrixCSC,CuSparseMatrixCSR}) = false
LinearAlgebra.issymmetric(M::Symmetric{CuSparseMatrixCSC}) = true
LinearAlgebra.ishermitian(M::Hermitian{CuSparseMatrixCSC}) = true
LinearAlgebra.istriu(M::UpperTriangular{T,S}) where {T<:BlasFloat, S<:AbstractCuSparseMatrix} = true
LinearAlgebra.istril(M::UpperTriangular{T,S}) where {T<:BlasFloat, S<:AbstractCuSparseMatrix} = false
LinearAlgebra.istriu(M::LowerTriangular{T,S}) where {T<:BlasFloat, S<:AbstractCuSparseMatrix} = false
LinearAlgebra.istril(M::LowerTriangular{T,S}) where {T<:BlasFloat, S<:AbstractCuSparseMatrix} = true
Hermitian{T}(Mat::CuSparseMatrix{T}) where T = Hermitian{T,typeof(Mat)}(Mat,'U')
SparseArrays.nnz(g::CuSparseMatrixBSR) = g.nnzb * g.blockDim * g.blockDim
## indexing
# translations
Base.getindex(A::AbstractCuSparseVector, ::Colon) = copy(A)
Base.getindex(A::AbstractCuSparseMatrix, ::Colon, ::Colon) = copy(A)
Base.getindex(A::AbstractCuSparseMatrix, i, ::Colon) = getindex(A, i, 1:size(A, 2))
Base.getindex(A::AbstractCuSparseMatrix, ::Colon, i) = getindex(A, 1:size(A, 1), i)
Base.getindex(A::AbstractCuSparseMatrix, I::Tuple{Integer,Integer}) = getindex(A, I[1], I[2])
# column slices
function Base.getindex(x::CuSparseMatrixCSC, ::Colon, j::Integer)
checkbounds(x, :, j)
r1 = convert(Int, x.colPtr[j])
r2 = convert(Int, x.colPtr[j+1]) - 1
CuSparseVector(rowvals(x)[r1:r2], nonzeros(x)[r1:r2], size(x, 1))
end
function Base.getindex(x::CuSparseMatrixCSR, i::Integer, ::Colon)
checkbounds(x, i, :)
c1 = convert(Int, x.rowPtr[i])
c2 = convert(Int, x.rowPtr[i+1]) - 1
CuSparseVector(x.colVal[c1:c2], nonzeros(x)[c1:c2], size(x, 2))
end
# row slices
Base.getindex(A::CuSparseMatrixCSC, i::Integer, ::Colon) = CuSparseVector(sparse(A[i, 1:end])) # TODO: optimize
Base.getindex(A::CuSparseMatrixCSR, ::Colon, j::Integer) = CuSparseVector(sparse(A[1:end, j])) # TODO: optimize
function Base.getindex(A::CuSparseVector{Tv, Ti}, i::Integer) where {Tv, Ti}
@boundscheck checkbounds(A, i)
ii = searchsortedfirst(A.iPtr, convert(Ti, i))
(ii > nnz(A) || A.iPtr[ii] != i) && return zero(Tv)
A.nzVal[ii]
end
function Base.getindex(A::CuSparseMatrixCSC{T}, i0::Integer, i1::Integer) where T
@boundscheck checkbounds(A, i0, i1)
r1 = Int(A.colPtr[i1])
r2 = Int(A.colPtr[i1+1]-1)
(r1 > r2) && return zero(T)
r1 = searchsortedfirst(rowvals(A), i0, r1, r2, Base.Order.Forward)
(r1 > r2 || rowvals(A)[r1] != i0) && return zero(T)
nonzeros(A)[r1]
end
function Base.getindex(A::CuSparseMatrixCSR{T}, i0::Integer, i1::Integer) where T
@boundscheck checkbounds(A, i0, i1)
c1 = Int(A.rowPtr[i0])
c2 = Int(A.rowPtr[i0+1]-1)
(c1 > c2) && return zero(T)
c1 = searchsortedfirst(A.colVal, i1, c1, c2, Base.Order.Forward)
(c1 > c2 || A.colVal[c1] != i1) && return zero(T)
nonzeros(A)[c1]
end
function Base.getindex(A::CuSparseMatrixCOO{T}, i0::Integer, i1::Integer) where T
@boundscheck checkbounds(A, i0, i1)
r1 = searchsortedfirst(A.rowInd, i0, Base.Order.Forward)
(r1 > length(A.rowInd) || A.rowInd[r1] > i0) && return zero(T)
r2 = searchsortedfirst(A.rowInd, i0+1, Base.Order.Forward)
c1 = searchsortedfirst(A.colInd, i1, r1, r2, Base.Order.Forward)
(c1 > r2 || A.colInd[c1] > i1) && return zero(T)
nonzeros(A)[c1]
end
function Base.getindex(A::CuSparseMatrixBSR{T}, i0::Integer, i1::Integer) where T
@boundscheck checkbounds(A, i0, i1)
i0_block, i0_idx = fldmod1(i0, A.blockDim)
i1_block, i1_idx = fldmod1(i1, A.blockDim)
block_idx = (i0_idx - 1) * A.blockDim + i1_idx - 1
c1 = Int(A.rowPtr[i0_block])
c2 = Int(A.rowPtr[i0_block+1]-1)
(c1 > c2) && return zero(T)
c1 = searchsortedfirst(A.colVal, i1_block, c1, c2, Base.Order.Forward)
(c1 > c2 || A.colVal[c1] != i1_block) && return zero(T)
nonzeros(A)[c1+block_idx]
end
## interop with sparse CPU arrays
# cpu to gpu
CuSparseVector{Tv, Ti}(Vec::SparseVector) where {Tv, Ti} =
CuSparseVector(CuVector{Ti}(Vec.nzind), CuVector{Tv}(Vec.nzval), length(Vec))
CuSparseVector{Tv, Ti}(Mat::SparseMatrixCSC) where {Tv, Ti} =
size(Mat,2) == 1 ?
CuSparseVector(CuVector{Ti}(Mat.rowval), CuVector{Tv}(Mat.nzval), size(Mat)[1]) :
throw(ArgumentError("The input argument must have a single column"))
CuSparseMatrixCSC{Tv, Ti}(Vec::SparseVector) where {Tv, Ti} =
CuSparseMatrixCSC{Tv}(CuVector{Ti}([1]), CuVector{Ti}(Vec.nzind),
CuVector{Tv}(Vec.nzval), size(Vec))
CuSparseMatrixCSC{Tv, Ti}(Mat::SparseMatrixCSC) where {Tv, Ti} =
CuSparseMatrixCSC{Tv, Ti}(CuVector{Ti}(Mat.colptr), CuVector{Ti}(Mat.rowval),
CuVector{Tv}(Mat.nzval), size(Mat))
CuSparseMatrixCSR{Tv, Ti}(Mat::Transpose{<:Any, <:SparseMatrixCSC}) where {Tv, Ti} =
CuSparseMatrixCSR{Tv, Ti}(CuVector{Ti}(parent(Mat).colptr), CuVector{Ti}(parent(Mat).rowval),
CuVector{Tv}(parent(Mat).nzval), size(Mat))
CuSparseMatrixCSR{Tv, Ti}(Mat::Adjoint{<:Any, <:SparseMatrixCSC}) where {Tv, Ti} =
CuSparseMatrixCSR{Tv, Ti}(CuVector{Ti}(parent(Mat).colptr), CuVector{Ti}(parent(Mat).rowval),
CuVector{Tv}(conj.(parent(Mat).nzval)), size(Mat))
CuSparseMatrixCSR{Tv, Ti}(Mat::SparseMatrixCSC) where {Tv, Ti} = CuSparseMatrixCSR(CuSparseMatrixCSC{Tv, Ti}(Mat))
CuSparseMatrixBSR{Tv, Ti}(Mat::SparseMatrixCSC, blockdim) where {Tv, Ti} = CuSparseMatrixBSR(CuSparseMatrixCSR{Tv, Ti}(Mat), blockdim)
CuSparseMatrixCOO{Tv, Ti}(Mat::SparseMatrixCSC) where {Tv, Ti} = CuSparseMatrixCOO(CuSparseMatrixCSR{Tv, Ti}(Mat))
# NOTE: we eagerly convert the indices to Cint here to avoid additional conversion later on
CuSparseVector{Tv}(Vec::SparseVector) where {Tv} = CuSparseVector{Tv, Cint}(Vec)
CuSparseVector{Tv}(Mat::SparseMatrixCSC) where {Tv} = CuSparseVector{Tv, Cint}(Mat)
CuSparseMatrixCSC{Tv}(Vec::SparseVector) where {Tv} = CuSparseMatrixCSC{Tv, Cint}(Vec)
CuSparseMatrixCSC{Tv}(Mat::SparseMatrixCSC) where {Tv} = CuSparseMatrixCSC{Tv, Cint}(Mat)
CuSparseMatrixCSR{Tv}(Mat::Transpose{<:Any, <:SparseMatrixCSC}) where {Tv} = CuSparseMatrixCSR{Tv, Cint}(Mat)
CuSparseMatrixCSR{Tv}(Mat::Adjoint{<:Any, <:SparseMatrixCSC}) where {Tv} = CuSparseMatrixCSR{Tv, Cint}(Mat)
CuSparseMatrixCSR{Tv}(Mat::SparseMatrixCSC) where {Tv} = CuSparseMatrixCSR{Tv, Cint}(Mat)
CuSparseMatrixBSR{Tv}(Mat::SparseMatrixCSC, blockdim) where {Tv} = CuSparseMatrixBSR{Tv, Cint}(Mat, blockdim)
CuSparseMatrixCOO{Tv}(Mat::SparseMatrixCSC) where {Tv} = CuSparseMatrixCOO{Tv, Cint}(Mat)
# untyped variants
CuSparseVector(x::AbstractSparseArray{T}) where {T} = CuSparseVector{T}(x)
CuSparseMatrixCSC(x::AbstractSparseArray{T}) where {T} = CuSparseMatrixCSC{T}(x)
CuSparseMatrixCSR(x::AbstractSparseArray{T}) where {T} = CuSparseMatrixCSR{T}(x)
CuSparseMatrixBSR(x::AbstractSparseArray{T}, blockdim) where {T} = CuSparseMatrixBSR{T}(x, blockdim)
CuSparseMatrixCOO(x::AbstractSparseArray{T}) where {T} = CuSparseMatrixCOO{T}(x)
CuSparseMatrixCSR(x::Transpose{T}) where {T} = CuSparseMatrixCSR{T}(x)
CuSparseMatrixCSR(x::Adjoint{T}) where {T} = CuSparseMatrixCSR{T}(x)
CuSparseMatrixCSC(x::Transpose{T}) where {T} = CuSparseMatrixCSC{T}(x)
CuSparseMatrixCSC(x::Adjoint{T}) where {T} = CuSparseMatrixCSC{T}(x)
# gpu to cpu
SparseVector(x::CuSparseVector) = SparseVector(length(x), Array(nonzeroinds(x)), Array(nonzeros(x)))
SparseMatrixCSC(x::CuSparseMatrixCSC) = SparseMatrixCSC(size(x)..., Array(x.colPtr), Array(rowvals(x)), Array(nonzeros(x)))
SparseMatrixCSC(x::CuSparseMatrixCSR) = SparseMatrixCSC(CuSparseMatrixCSC(x)) # no direct conversion
SparseMatrixCSC(x::CuSparseMatrixBSR) = SparseMatrixCSC(CuSparseMatrixCSR(x)) # no direct conversion
SparseMatrixCSC(x::CuSparseMatrixCOO) = SparseMatrixCSC(CuSparseMatrixCSR(x)) # no direct conversion
# collect to Array
Base.collect(x::CuSparseVector) = collect(SparseVector(x))
Base.collect(x::CuSparseMatrixCSC) = collect(SparseMatrixCSC(x))
Base.collect(x::CuSparseMatrixCSR) = collect(SparseMatrixCSC(x))
Base.collect(x::CuSparseMatrixBSR) = collect(CuSparseMatrixCSR(x)) # no direct conversion
Base.collect(x::CuSparseMatrixCOO) = collect(CuSparseMatrixCSR(x)) # no direct conversion
Adapt.adapt_storage(::Type{CuArray}, xs::SparseVector) = CuSparseVector(xs)
Adapt.adapt_storage(::Type{CuArray}, xs::SparseMatrixCSC) = CuSparseMatrixCSC(xs)
Adapt.adapt_storage(::Type{CuArray{T}}, xs::SparseVector) where {T} = CuSparseVector{T}(xs)
Adapt.adapt_storage(::Type{CuArray{T}}, xs::SparseMatrixCSC) where {T} = CuSparseMatrixCSC{T}(xs)
Adapt.adapt_storage(::CUDA.CuArrayAdaptor, xs::AbstractSparseArray) =
adapt(CuArray, xs)
Adapt.adapt_storage(::CUDA.CuArrayAdaptor, xs::AbstractSparseArray{<:AbstractFloat}) =
adapt(CuArray{Float32}, xs)
Adapt.adapt_storage(::Type{Array}, xs::CuSparseVector) = SparseVector(xs)
Adapt.adapt_storage(::Type{Array}, xs::CuSparseMatrixCSC) = SparseMatrixCSC(xs)
## copying between sparse GPU arrays
function Base.copyto!(dst::CuSparseVector, src::CuSparseVector)
if length(dst) != length(src)
throw(ArgumentError("Inconsistent Sparse Vector size"))
end
copyto!(nonzeroinds(dst), nonzeroinds(src))
copyto!(nonzeros(dst), nonzeros(src))
dst.nnz = src.nnz
dst
end
function Base.copyto!(dst::CuSparseMatrixCSC, src::CuSparseMatrixCSC)
if size(dst) != size(src)
throw(ArgumentError("Inconsistent Sparse Matrix size"))
end
copyto!(dst.colPtr, src.colPtr)
copyto!(rowvals(dst), rowvals(src))
copyto!(nonzeros(dst), nonzeros(src))
dst.nnz = src.nnz
dst
end
function Base.copyto!(dst::CuSparseMatrixCSR, src::CuSparseMatrixCSR)
if size(dst) != size(src)
throw(ArgumentError("Inconsistent Sparse Matrix size"))
end
copyto!(dst.rowPtr, src.rowPtr)
copyto!(dst.colVal, src.colVal)
copyto!(nonzeros(dst), nonzeros(src))
dst.nnz = src.nnz
dst
end
function Base.copyto!(dst::CuSparseMatrixBSR, src::CuSparseMatrixBSR)
if size(dst) != size(src)
throw(ArgumentError("Inconsistent Sparse Matrix size"))
end
copyto!(dst.rowPtr, src.rowPtr)
copyto!(dst.colVal, src.colVal)
copyto!(nonzeros(dst), nonzeros(src))
dst.dir = src.dir
dst.nnzb = src.nnzb
dst
end
function Base.copyto!(dst::CuSparseMatrixCOO, src::CuSparseMatrixCOO)
if size(dst) != size(src)
throw(ArgumentError("Inconsistent Sparse Matrix size"))
end
copyto!(dst.rowInd, src.rowInd)
copyto!(dst.colInd, src.colInd)
copyto!(nonzeros(dst), nonzeros(src))
dst.nnz = src.nnz
dst
end
Base.copy(Vec::CuSparseVector) = copyto!(similar(Vec), Vec)
Base.copy(Mat::CuSparseMatrixCSC) = copyto!(similar(Mat), Mat)
Base.copy(Mat::CuSparseMatrixCSR) = copyto!(similar(Mat), Mat)
Base.copy(Mat::CuSparseMatrixBSR) = copyto!(similar(Mat), Mat)
Base.copy(Mat::CuSparseMatrixCOO) = copyto!(similar(Mat), Mat)
# input/output
for (gpu, cpu) in [CuSparseVector => SparseVector]
@eval function Base.show(io::IO, ::MIME"text/plain", x::$gpu)
xnnz = length(nonzeros(x))
print(io, length(x), "-element ", typeof(x), " with ", xnnz,
" stored ", xnnz == 1 ? "entry" : "entries")
if xnnz != 0
println(io, ":")
show(IOContext(io, :typeinfo => eltype(x)), $cpu(x))
end
end
end
for (gpu, cpu) in [CuSparseMatrixCSC => SparseMatrixCSC,
CuSparseMatrixCSR => SparseMatrixCSC,
CuSparseMatrixBSR => SparseMatrixCSC,
CuSparseMatrixCOO => SparseMatrixCSC]
@eval Base.show(io::IOContext, x::$gpu) =
show(io, $cpu(x))
@eval function Base.show(io::IO, mime::MIME"text/plain", S::$gpu)
xnnz = nnz(S)
m, n = size(S)
print(io, m, "×", n, " ", typeof(S), " with ", xnnz, " stored ",
xnnz == 1 ? "entry" : "entries")
if !(m == 0 || n == 0)
println(io, ":")
io = IOContext(io, :typeinfo => eltype(S))
if ndims(S) == 1
show(io, $cpu(S))
else
# so that we get the nice Braille pattern
Base.print_array(io, $cpu(S))
end
end
end
end
# interop with device arrays
function Adapt.adapt_structure(to::CUDA.Adaptor, x::CuSparseVector)
return CuSparseDeviceVector(
adapt(to, x.iPtr),
adapt(to, x.nzVal),
length(x), x.nnz
)
end
function Adapt.adapt_structure(to::CUDA.Adaptor, x::CuSparseMatrixCSR)
return CuSparseDeviceMatrixCSR(
adapt(to, x.rowPtr),
adapt(to, x.colVal),
adapt(to, x.nzVal),
size(x), x.nnz
)
end
function Adapt.adapt_structure(to::CUDA.Adaptor, x::CuSparseMatrixCSC)
return CuSparseDeviceMatrixCSC(
adapt(to, x.colPtr),
adapt(to, x.rowVal),
adapt(to, x.nzVal),
size(x), x.nnz
)
end
function Adapt.adapt_structure(to::CUDA.Adaptor, x::CuSparseMatrixBSR)
return CuSparseDeviceMatrixBSR(
adapt(to, x.rowPtr),
adapt(to, x.colVal),
adapt(to, x.nzVal),
size(x), x.blockDim,
x.dir, x.nnzb
)
end
function Adapt.adapt_structure(to::CUDA.Adaptor, x::CuSparseMatrixCOO)
return CuSparseDeviceMatrixCOO(
adapt(to, x.rowInd),
adapt(to, x.colInd),
adapt(to, x.nzVal),
size(x), x.nnz
)
end