|
| 1 | +using Base.BinaryPlatforms |
| 2 | + |
| 3 | +const rocm_sdk_core_jll_uuid = Base.UUID("9ab9228b-5f62-5ec0-95ae-72487824505f") |
| 4 | +const preferences = Base.get_preferences(rocm_sdk_core_jll_uuid) |
| 5 | +Base.record_compiletime_preference(rocm_sdk_core_jll_uuid, "local") |
| 6 | + |
| 7 | +const local_preference = if haskey(preferences, "local") |
| 8 | + if isa(preferences["local"], Bool) |
| 9 | + preferences["local"] |
| 10 | + elseif isa(preferences["local"], String) |
| 11 | + use_local = tryparse(Bool, preferences["local"]) |
| 12 | + if use_local === nothing |
| 13 | + @error "ROCm local preference is not valid; expected a boolean, but got '$(preferences["local"])'" |
| 14 | + missing |
| 15 | + else |
| 16 | + use_local |
| 17 | + end |
| 18 | + else |
| 19 | + @error "ROCm local preference is not valid; expected a boolean, but got '$(preferences["local"])'" |
| 20 | + missing |
| 21 | + end |
| 22 | +else |
| 23 | + missing |
| 24 | +end |
| 25 | + |
| 26 | +try |
| 27 | + using HSARuntime_jll |
| 28 | +catch |
| 29 | + # during initial package installation, HSARuntime_jll may not be available. |
| 30 | + # in that case, we just won't select an artifact. |
| 31 | +end |
| 32 | + |
| 33 | +struct hsa_agent_t |
| 34 | + handle::UInt64 |
| 35 | +end |
| 36 | + |
| 37 | +const HSA_AGENT_INFO_NAME::Cint = 0 |
| 38 | + |
| 39 | +@enum hsa_status_t::Cint begin |
| 40 | + HSA_STATUS_SUCCESS = 0x0 |
| 41 | + HSA_STATUS_INFO_BREAK = 0x1 |
| 42 | + |
| 43 | + HSA_STATUS_ERROR = 0x1000 |
| 44 | + HSA_STATUS_ERROR_INVALID_ARGUMENT = 0x1001 |
| 45 | + HSA_STATUS_ERROR_INVALID_QUEUE_CREATION = 0x1002 |
| 46 | + HSA_STATUS_ERROR_INVALID_ALLOCATION = 0x1003 |
| 47 | + HSA_STATUS_ERROR_INVALID_AGENT = 0x1004 |
| 48 | + HSA_STATUS_ERROR_INVALID_REGION = 0x1005 |
| 49 | + HSA_STATUS_ERROR_INVALID_SIGNAL = 0x1006 |
| 50 | + HSA_STATUS_ERROR_INVALID_QUEUE = 0x1007 |
| 51 | + HSA_STATUS_ERROR_OUT_OF_RESOURCES = 0x1008 |
| 52 | + HSA_STATUS_ERROR_INVALID_PACKET_FORMAT = 0x1009 |
| 53 | + HSA_STATUS_ERROR_RESOURCE_FREE = 0x100A |
| 54 | + HSA_STATUS_ERROR_NOT_INITIALIZED = 0x100B |
| 55 | + HSA_STATUS_ERROR_REFCOUNT_OVERFLOW = 0x100C |
| 56 | + HSA_STATUS_ERROR_INCOMPATIBLE_ARGUMENTS = 0x100D |
| 57 | + HSA_STATUS_ERROR_INVALID_INDEX = 0x100E |
| 58 | + HSA_STATUS_ERROR_INVALID_ISA = 0x100F |
| 59 | + |
| 60 | + HSA_STATUS_ERROR_INVALID_CODE_OBJECT = 0x1010 |
| 61 | + HSA_STATUS_ERROR_INVALID_EXECUTABLE = 0x1011 |
| 62 | + HSA_STATUS_ERROR_FROZEN_EXECUTABLE = 0x1012 |
| 63 | + HSA_STATUS_ERROR_INVALID_SYMBOL_NAME = 0x1013 |
| 64 | + HSA_STATUS_ERROR_VARIABLE_ALREADY_DEFINED = 0x1014 |
| 65 | + HSA_STATUS_ERROR_VARIABLE_UNDEFINED = 0x1015 |
| 66 | + HSA_STATUS_ERROR_EXCEPTION = 0x1016 |
| 67 | + HSA_STATUS_ERROR_INVALID_ISA_NAME = 0x1017 |
| 68 | + HSA_STATUS_ERROR_INVALID_CODE_SYMBOL = 0x1018 |
| 69 | + HSA_STATUS_ERROR_INVALID_EXECUTABLE_SYMBOL = 0x1019 |
| 70 | + |
| 71 | + HSA_STATUS_ERROR_INVALID_FILE = 0x1020 |
| 72 | + HSA_STATUS_ERROR_INVALID_CODE_OBJECT_READER = 0x1021 |
| 73 | + HSA_STATUS_ERROR_INVALID_CACHE = 0x1022 |
| 74 | + HSA_STATUS_ERROR_INVALID_WAVEFRONT = 0x1023 |
| 75 | + HSA_STATUS_ERROR_INVALID_SIGNAL_GROUP = 0x1024 |
| 76 | + HSA_STATUS_ERROR_INVALID_RUNTIME_STATE = 0x1025 |
| 77 | + HSA_STATUS_ERROR_FATAL = 0x1026 |
| 78 | +end |
| 79 | + |
| 80 | +function callback(agent::hsa_agent_t, data::Ptr{Vector{String}}) |
| 81 | + a = Base.unsafe_pointer_to_objref(data) |
| 82 | + _name = zeros(Cchar, 64) |
| 83 | + status = @ccall libhsa_runtime64.hsa_agent_get_info(agent::hsa_agent_t, HSA_AGENT_INFO_NAME::Cint, _name::Ptr{Cchar})::hsa_status_t |
| 84 | + if status == HSA_STATUS_SUCCESS |
| 85 | + GC.@preserve _name push!(a, Base.unsafe_string(pointer(_name))) |
| 86 | + end |
| 87 | + return status |
| 88 | +end |
| 89 | + |
| 90 | +function agent_names() |
| 91 | + r = Ref(String[]) |
| 92 | + ptr = Base.unsafe_convert(Ptr{Vector{String}}, r) |
| 93 | + cb = @cfunction(callback, hsa_status_t, (hsa_agent_t, Ptr{Vector{String}})) |
| 94 | + status = @ccall libhsa_runtime64.hsa_init()::hsa_status_t |
| 95 | + status != HSA_STATUS_SUCCESS && error(status) |
| 96 | + status = @ccall libhsa_runtime64.hsa_iterate_agents(cb::Ptr{Cvoid}, ptr::Ptr{Vector{String}})::hsa_status_t |
| 97 | + status != HSA_STATUS_SUCCESS && error(status) |
| 98 | + status = @ccall libhsa_runtime64.hsa_shut_down()::hsa_status_t |
| 99 | + status != HSA_STATUS_SUCCESS && error(status) |
| 100 | + return r[] |
| 101 | +end |
| 102 | + |
| 103 | +function name_to_platform(name::String) |
| 104 | + if startswith(name, "gfx101") |
| 105 | + return "gfx101x_dgpu" |
| 106 | + elseif startswith(name, "gfx103") |
| 107 | + return "gfx103x_dgpu" |
| 108 | + elseif startswith(name, "gfx110") |
| 109 | + return "gfx110x_all" |
| 110 | + elseif name == "gfx1150" |
| 111 | + return "gfx1150" |
| 112 | + elseif name == "gfx1151" |
| 113 | + return "gfx1151" |
| 114 | + elseif startswith(name, "gfx120") |
| 115 | + return "gfx120x_all" |
| 116 | + elseif startswith(name, "gfx90") |
| 117 | + return "gfx90x_dcgpu" |
| 118 | + elseif startswith(name, "gfx94") |
| 119 | + return "gfx94x_dcgpu" |
| 120 | + elseif startswith(name, "gfx950") |
| 121 | + return "gfx950_dcgpu" |
| 122 | + else |
| 123 | + return nothing |
| 124 | + end |
| 125 | +end |
| 126 | + |
| 127 | +function detect_rocm_platform() |
| 128 | + names = try |
| 129 | + agent_names() |
| 130 | + catch e |
| 131 | + @warn "Failed to detect ROCm platform: $e" |
| 132 | + String[] |
| 133 | + end |
| 134 | + filter!(startswith("gfx"), names) |
| 135 | + |
| 136 | + if isempty(names) |
| 137 | + @warn "No ROCm GPU agents detected on this system." |
| 138 | + return "none" |
| 139 | + end |
| 140 | + |
| 141 | + platforms = unique!(filter(!isnothing, map(name_to_platform, names))) |
| 142 | + if isempty(platforms) |
| 143 | + @warn "Unrecognized ROCm GPU agents detected on this system: $(join(names, ", "))." |
| 144 | + return "none" |
| 145 | + elseif length(platforms) > 1 |
| 146 | + @warn "Multiple supported ROCm platforms detected on this system: $(join(platforms, ", ")). Using the first one. Override by setting the `rocm_platform` preference." |
| 147 | + end |
| 148 | + |
| 149 | + return first(platforms) |
| 150 | +end |
| 151 | + |
| 152 | +function augment_platform!(platform::Platform) |
| 153 | + # Only augment Linux x86_64 platforms |
| 154 | + if Sys.islinux() && arch(platform) == "x86_64" |
| 155 | + if !haskey(platform, "rocm_platform") |
| 156 | + platform["rocm_platform"] = detect_rocm_platform() |
| 157 | + end |
| 158 | + |
| 159 | + # Store the fact that we're using a local ROCm installation |
| 160 | + platform["rocm_local"] = string(local_preference !== missing && local_preference) |
| 161 | + end |
| 162 | + |
| 163 | + return platform |
| 164 | +end |
0 commit comments