-
Notifications
You must be signed in to change notification settings - Fork 9
/
PT.jl
92 lines (77 loc) · 2.05 KB
/
PT.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
"""
Storage involved in PT algorithms:
$FIELDS
"""
@auto struct PT
"""
The user-provided [`Inputs`](@ref) that determine
the execution of a PT algorithm.
"""
inputs
"""
The [`replicas`](@ref) held by this machine.
"""
replicas
"""
Information shared across all machines, updated between
rounds.
"""
shared
"""
Either a path to a folder shared by all processes,
which is used to save information to disk (checkpoints, samples etc);
or nothing if a completely in-memory algorithm is used.
"""
exec_folder::Union{String, Nothing}
"""
[`recorders`](@ref) from the last round, or empty
[`recorders`](@ref).
"""
reduced_recorders
end
"""
$SIGNATURES
Create a PT struct from provided [`Inputs`](@ref).
Optionally, provide a specific `exec_folder` path (`AbstractString`),
if not one will be created via [`next_exec_folder()`](@ref).
"""
function PT(inputs::Inputs; exec_folder = use_auto_exec_folder)
shared = Shared(inputs)
replicas = create_replicas(inputs, shared)
exec_folder = pt_exec_folder(inputs.checkpoint, exec_folder)
return PT(inputs, replicas, shared, exec_folder, create_recorders(inputs, shared))
end
pt_exec_folder(use_checkpoint, specified_exec_folder) =
if use_checkpoint
if specified_exec_folder == use_auto_exec_folder
next_exec_folder()
else
specified_exec_folder
end
else
nothing
end
Base.show(io::IO, pt::PT) =
if pt.shared.iterators.round == 0 || !pt.inputs.checkpoint
print(io, "PT(checkpoint = false, ...)")
else
print(io, "PT(\"$(pt.exec_folder)\")")
end
"""
$SIGNATURES
A task that should be ran on only one of the processes.
Using the `do .. end` syntax, this can be used as:
```
only_one_process(pt) do
...
end
```
"""
only_one_process(task, pt) =
if load(pt.replicas).my_process_index == 1
task()
end
only_one_process_println(pt, arguments) =
only_one_process(pt) do
println(arguments)
end