/
device.jl
173 lines (138 loc) · 4.5 KB
/
device.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
struct HIPDevice
device::hipDevice_t
device_id::Cint
gcn_arch::String
wavefrontsize::Cint
end
const DEFAULT_DEVICE = Ref{Union{Nothing, HIPDevice}}(nothing)
const ALL_DEVICES = Vector{HIPDevice}()
function HIPDevice(device_id::Integer)
isempty(ALL_DEVICES) || return ALL_DEVICES[device_id]
device_ref = Ref{hipDevice_t}()
hipDeviceGet(device_ref, device_id - 1) |> check
props = properties(device_id - 1)
gcn_arch = unsafe_string(pointer([props.gcnArchName...]))
HIPDevice(device_ref[], device_id, gcn_arch, props.warpSize)
end
"""
device_id(d::HIPDevice)
Zero-based device ID as expected by HIP functions.
Differs from [`AMDGPU.device_id`](@ref) method by `1`.
"""
device_id(d::HIPDevice) = d.device_id - 1
"""
wavefrontsize(d::HIPDevice)::Cint
Get size of the wavefront. AMD GPUs support either 32 or 64.
"""
wavefrontsize(d::HIPDevice)::Cint = d.wavefrontsize
"""
gcn_arch(d::HIPDevice)::String
Get GCN architecture for the device.
"""
gcn_arch(d::HIPDevice)::String = d.gcn_arch
function stack_size()
value = Ref{Csize_t}()
hipDeviceGetLimit(value, hipLimitStackSize) |> check
value[]
end
function stack_size!(value::Integer)
hipDeviceSetLimit(hipLimitStackSize, value) |> check
end
function heap_size()
value = Ref{Csize_t}()
hipDeviceGetLimit(value, hipLimitMallocHeapSize) |> check
value[]
end
function heap_size!(value::Integer)
hipDeviceSetLimit(hipLimitMallocHeapSize, value) |> check
end
Base.unsafe_convert(::Type{hipDevice_t}, device::HIPDevice) = device.device
Base.:(==)(a::HIPDevice, b::HIPDevice) = a.device == b.device
Base.hash(dev::HIPDevice, h::UInt) = hash(dev.device, h)
"""
name(dev::HIPDevice)::String
Get name of the device.
"""
function name(dev::HIPDevice)::String
name_vec = zeros(Cuchar, 64)
hipDeviceGetName(pointer(name_vec), Cint(64), dev.device) |> check
return strip(String(name_vec), '\0')
end
"""
properties(dev::HIPDevice)::hipDeviceProp_t
Get all properties for the device.
See HIP documentation for `hipDeviceProp_t` for the meaning of each field.
"""
properties(dev::HIPDevice) = properties(device_id(dev))
function properties(dev_id::Int)
props_ref = Ref{hipDeviceProp_t}()
hipGetDeviceProperties(props_ref, dev_id) |> check
props_ref[]
end
function attribute(dev::HIPDevice, attr)
v = Ref{Cint}()
hipDeviceGetAttribute(v, attr, device_id(dev)) |> check
v[]
end
function ndevices()
count_ref = Ref{Cint}()
hipGetDeviceCount(count_ref) |> check
count_ref[]
end
function devices()
isempty(ALL_DEVICES) || return copy(ALL_DEVICES)
devs = Vector{HIPDevice}(undef, ndevices())
for i in 1:length(devs)
d = HIPDevice(i)
arch = gcn_arch(d)
if occursin("gfx11", arch) && VERSION < v"1.10-"
@error "Navi 3 ($arch) requires Julia 1.10+ and ROCm 5.5 or higher."
end
devs[i] = d
end
append!(ALL_DEVICES, devs)
return devs
end
function device()
device_id_ref = Ref{Cint}()
hipGetDevice(device_id_ref) |> check
return HIPDevice(device_id_ref[] + 1)
end
device!(device::HIPDevice) = hipSetDevice(device_id(device)) |> check
device!(device_id::Integer) = hipSetDevice(device_id - 1) |> check
function device!(f::Base.Callable, device::HIPDevice)
old_device_id_ref = Ref{Cint}()
hipGetDevice(old_device_id_ref) |> check
device!(device)
try
f()
finally
device!(old_device_id_ref[] + 1)
end
end
function can_access_peer(dev::HIPDevice, peer::HIPDevice)
result = Ref{Cint}(0)
hipDeviceCanAccessPeer(result, device_id(dev), device_id(peer)) |> check
return result[] == 1
end
# Pretty-printing.
function Base.show(io::IO, dev::HIPDevice)
print(io, "HIPDevice(id=$(dev.device_id), name=$(name(dev)), gcn_arch=$(dev.gcn_arch))")
end
function __pretty_data(dev::HIPDevice)
props = properties(dev)
name_ptr = pointer([props.name...])
name = unsafe_string(name_ptr)
reshape(String[
"$(dev.device_id)", name, "$(dev.gcn_arch)",
"$(dev.wavefrontsize)", "$(Base.format_bytes(props.totalGlobalMem))",
], 1, :)
end
function Base.show(io::IO, mime::MIME{Symbol("text/plain")}, dev::HIPDevice)
PrettyTables.pretty_table(io, __pretty_data(dev); header=[
"Id", "Name", "GCN arch", "Wavefront", "Memory"])
end
function Base.show(io::IO, mime::MIME{Symbol("text/plain")}, devs::Vector{HIPDevice})
PrettyTables.pretty_table(io, vcat(__pretty_data.(devs)...); header=[
"Id", "Name", "GCN arch", "Wavefront", "Memory"])
end