-
Notifications
You must be signed in to change notification settings - Fork 4
/
Architectures.jl
85 lines (64 loc) · 2.27 KB
/
Architectures.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
module Architectures
export Architecture, SingleDeviceArchitecture
export Arch, get_backend, get_device, activate!, set_device!, heuristic_groupsize, pointertype
using Chmy
using KernelAbstractions
"""
Architecture
Abstract type representing an architecture.
"""
abstract type Architecture end
"""
SingleDeviceArchitecture <: Architecture
A struct representing an architecture that operates on a single CPU or GPU device.
"""
struct SingleDeviceArchitecture{B,D} <: Architecture
backend::B
device::D
function SingleDeviceArchitecture(backend, device)
set_device!(device)
return new{typeof(backend),typeof(device)}(backend, device)
end
end
"""
SingleDeviceArchitecture(arch::Architecture)
Create a `SingleDeviceArchitecture` object retrieving backend and device from `arch`.
"""
SingleDeviceArchitecture(arch::Architecture) = SingleDeviceArchitecture(get_backend(arch), get_device(arch))
"""
Arch(backend::Backend; device_id::Integer=1)
Create an architecture object for the specified backend and device.
# Arguments
- `backend`: The backend to use for computation.
- `device_id=1`: The ID of the device to use.
"""
function Arch(backend::Backend; device_id::Integer=1)
dev = get_device(backend, device_id)
return SingleDeviceArchitecture(backend, dev)
end
"""
get_backend(arch::SingleDeviceArchitecture)
Get the backend associated with a SingleDeviceArchitecture.
"""
get_backend(arch::SingleDeviceArchitecture) = arch.backend
"""
get_device(arch::SingleDeviceArchitecture)
Get the device associated with a SingleDeviceArchitecture.
"""
get_device(arch::SingleDeviceArchitecture) = arch.device
"""
activate!(arch::SingleDeviceArchitecture; priority=:normal)
Activate the given architecture on the specified device and set the priority of the
backend. For the priority accepted values are `:normal`, `:low` and `:high`.
"""
function activate!(arch::SingleDeviceArchitecture; priority=:normal)
set_device!(arch.device)
KernelAbstractions.priority!(arch.backend, priority)
end
# CPU
get_device(::CPU, device_id) = nothing
set_device!(::Nothing) = nothing
heuristic_groupsize(::CPU, ::Val{N}) where {N} = 256
Base.unsafe_wrap(::CPU, ptr::Ptr, dims) = unsafe_wrap(Array, ptr, dims)
pointertype(::CPU, T::DataType) = Ptr{T}
end