-
Notifications
You must be signed in to change notification settings - Fork 270
Expand file tree
/
Copy pathCUSPARSE.jl
More file actions
103 lines (76 loc) · 2.16 KB
/
CUSPARSE.jl
File metadata and controls
103 lines (76 loc) · 2.16 KB
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
module CUSPARSE
using ..APIUtils
using ..GPUToolbox
using ..CUDA_Runtime
using ..CUDA
using ..CUDA: CUstream, cuComplex, cuDoubleComplex, libraryPropertyType, cudaDataType
using ..CUDA: unsafe_free!, retry_reclaim, initialize_context, i32, @allowscalar
using ..CUDA.GPUArrays
using CEnum: @cenum
using LinearAlgebra
using LinearAlgebra: HermOrSym
using Adapt: Adapt, adapt
using SparseArrays
const SparseChar = Char
# core library
include("libcusparse.jl")
include("error.jl")
include("array.jl")
include("util.jl")
include("types.jl")
include("linalg.jl")
# low-level wrappers
include("helpers.jl")
include("management.jl")
include("level2.jl")
include("level3.jl")
include("extra.jl")
include("preconditioners.jl")
include("reorderings.jl")
include("conversions.jl")
include("generic.jl")
# high-level integrations
include("interfaces.jl")
include("batched.jl")
## handles
function handle_ctor(ctx)
context!(ctx) do
cusparseCreate()
end
end
function handle_dtor(ctx, handle)
context!(ctx; skip_destroyed=true) do
cusparseDestroy(handle)
end
end
const idle_handles = HandleCache{CuContext,cusparseHandle_t}(handle_ctor, handle_dtor)
function handle()
cuda = CUDA.active_state()
# every task maintains library state per device
LibraryState = @NamedTuple{handle::cusparseHandle_t, stream::CuStream}
states = get!(task_local_storage(), :CUSPARSE) do
Dict{CuContext,LibraryState}()
end::Dict{CuContext,LibraryState}
# get library state
@noinline function new_state(cuda)
new_handle = pop!(idle_handles, cuda.context)
finalizer(current_task()) do task
push!(idle_handles, cuda.context, new_handle)
end
cusparseSetStream(new_handle, cuda.stream)
(; handle=new_handle, cuda.stream)
end
state = get!(states, cuda.context) do
new_state(cuda)
end
# update stream
@noinline function update_stream(cuda, state)
cusparseSetStream(state.handle, cuda.stream)
(; state.handle, cuda.stream)
end
if state.stream != cuda.stream
states[cuda.context] = state = update_stream(cuda, state)
end
return state.handle
end
end