-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathruntests.jl
More file actions
266 lines (235 loc) · 7.95 KB
/
runtests.jl
File metadata and controls
266 lines (235 loc) · 7.95 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
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
using Test
using ClimaComms
ClimaComms.@import_required_backends
context = ClimaComms.context()
pid, nprocs = ClimaComms.init(context)
device = ClimaComms.device(context)
AT = ClimaComms.array_type(device)
if ClimaComms.iamroot(context)
@info "Running test" context device AT
end
if haskey(ENV, "CLIMACOMMS_TEST_DEVICE")
if ENV["CLIMACOMMS_TEST_DEVICE"] == "CPU"
@test device isa ClimaComms.AbstractCPUDevice
elseif ENV["CLIMACOMMS_TEST_DEVICE"] == "CPUSingleThreaded"
@test device isa ClimaComms.CPUSingleThreaded
elseif ENV["CLIMACOMMS_TEST_DEVICE"] == "CPUMultiThreaded"
@test device isa ClimaComms.CPUMultiThreaded
elseif ENV["CLIMACOMMS_TEST_DEVICE"] == "CUDA"
@test device isa ClimaComms.CUDADevice
end
end
using SafeTestsets
@safetestset "macro hygiene" begin
include("hygiene.jl")
end
if context isa ClimaComms.MPICommsContext
graph_opt_list = [(; persistent = true), (; persistent = false)]
else
graph_opt_list = [()]
end
@testset "tree test $graph_opt" for graph_opt in graph_opt_list
for FT in (Float32, Float64)
# every process communicates with the root
if ClimaComms.iamroot(context)
# send 2*n items to the nth pid, receive 3*n
sendpids = collect(2:nprocs)
sendlengths = [2 * dest for dest in sendpids]
sendarray = AT(fill(zero(FT), (2, sum(sendpids))))
recvpids = collect(2:nprocs)
recvlengths = [3 * dest for dest in recvpids]
recvarray = AT(fill(zero(FT), (3, sum(recvpids))))
else
# send 3*pid items to the 1st pid, receive 2*pid
sendpids = [1]
sendlengths = [3 * pid]
sendarray = AT(fill(zero(FT), (3, pid)))
recvpids = [1]
recvlengths = [2 * pid]
recvarray = AT(fill(zero(FT), (2, pid)))
end
graph_context = ClimaComms.graph_context(
context,
sendarray,
sendlengths,
sendpids,
recvarray,
recvlengths,
recvpids;
graph_opt...,
)
# 1) fill buffers with pid
fill!(sendarray, FT(pid))
ClimaComms.start(graph_context)
ClimaComms.progress(graph_context)
ClimaComms.finish(graph_context)
if ClimaComms.iamroot(context)
offset = 0
for s in 2:nprocs
@test all(
==(FT(s)),
view(recvarray, :, (offset + 1):(offset + s)),
)
offset += s
end
else
@test all(==(FT(1)), recvarray)
end
# 2) send everything back
if ClimaComms.iamroot(context)
sendarray .= view(recvarray, 1:2, :)
else
sendarray .= FT(1)
end
ClimaComms.start(graph_context)
ClimaComms.progress(graph_context)
ClimaComms.finish(graph_context)
@test all(==(FT(pid)), recvarray)
end
end
@testset "linear test $graph_opt" for graph_opt in graph_opt_list
for FT in (Float32, Float64)
# send 2 values up
if pid < nprocs
sendpids = Int[pid + 1]
sendlengths = Int[2]
sendarray = AT(fill(zero(FT), (2,)))
else
sendpids = Int[]
sendlengths = Int[]
sendarray = AT(fill(zero(FT), (0,)))
end
if pid > 1
recvpids = Int[pid - 1]
recvlengths = Int[2]
recvarray = AT(fill(zero(FT), (2,)))
else
recvpids = Int[]
recvlengths = Int[]
recvarray = AT(fill(zero(FT), (0,)))
end
graph_context = ClimaComms.graph_context(
context,
sendarray,
sendlengths,
sendpids,
recvarray,
recvlengths,
recvpids;
graph_opt...,
)
# 1) send pid
if pid < nprocs
sendarray .= FT(pid)
end
ClimaComms.start(graph_context)
ClimaComms.progress(graph_context)
ClimaComms.finish(graph_context)
if pid > 1
@test all(==(FT(pid - 1)), recvarray)
end
# 2) send next
if 1 < pid < nprocs
sendarray .= recvarray
end
ClimaComms.start(graph_context)
ClimaComms.progress(graph_context)
ClimaComms.finish(graph_context)
if pid > 2
@test all(==(FT(pid - 2)), recvarray)
end
end
end
@testset "gather" begin
for FT in (Float32, Float64)
local_array = AT(fill(FT(pid), (3, pid)))
gathered = ClimaComms.gather(context, local_array)
if ClimaComms.iamroot(context)
@test gathered isa AT
@test gathered ==
AT(reduce(hcat, [fill(FT(i), (3, i)) for i in 1:nprocs]))
else
@test isnothing(gathered)
end
end
end
@testset "reduce/reduce!/allreduce" begin
for FT in (Float32, Float64)
pidsum = div(nprocs * (nprocs + 1), 2)
sendrecvbuf = AT(fill(FT(pid), 3))
ClimaComms.allreduce!(context, sendrecvbuf, +)
@test sendrecvbuf == AT(fill(FT(pidsum), 3))
sendrecvbuf = AT(fill(FT(pid), 3))
ClimaComms.reduce!(context, sendrecvbuf, +)
if ClimaComms.iamroot(context)
@test sendrecvbuf == AT(fill(FT(pidsum), 3))
end
sendbuf = AT(fill(FT(pid), 2))
recvbuf = AT(zeros(FT, 2))
ClimaComms.reduce!(context, sendbuf, recvbuf, +)
if ClimaComms.iamroot(context)
@test recvbuf == AT(fill(FT(pidsum), 2))
end
sendbuf = AT(fill(FT(pid), 2))
recvbuf = AT(zeros(FT, 2))
ClimaComms.allreduce!(context, sendbuf, recvbuf, +)
@test recvbuf == AT(fill(FT(pidsum), 2))
recvval = ClimaComms.allreduce(context, FT(pid), +)
@test recvval == FT(pidsum)
recvval = ClimaComms.reduce(context, FT(pid), +)
if ClimaComms.iamroot(context)
@test recvval == FT(pidsum)
end
end
end
@testset "bcast" begin
@test ClimaComms.bcast(context, ClimaComms.iamroot(context))
@test ClimaComms.bcast(context, pid) == 1
@test ClimaComms.bcast(context, "root pid is $pid") == "root pid is 1"
@test ClimaComms.bcast(context, AT(fill(Float32(pid), 3))) ==
AT(fill(Float32(1), 3))
@test ClimaComms.bcast(context, AT(fill(Float64(pid), 3))) ==
AT(fill(Float64(1), 3))
end
@testset "allowscalar" begin
a = AT(rand(3))
local x
ClimaComms.allowscalar(device) do
x = a[1]
end
device isa ClimaComms.CUDADevice && @test_throws ErrorException a[1]
@test x == Array(a)[1]
end
import Adapt
@testset "Adapt" begin
@test Adapt.adapt(Array, ClimaComms.CUDADevice()) ==
ClimaComms.CPUSingleThreaded()
@static if ClimaComms.device() isa ClimaComms.CUDADevice
@test Adapt.adapt(Array, ClimaComms.CUDADevice()) ==
ClimaComms.CPUSingleThreaded()
@test Adapt.adapt(CUDA.CuArray, ClimaComms.CUDADevice()) ==
ClimaComms.CUDADevice()
@test Adapt.adapt(CUDA.CuArray, ClimaComms.CPUSingleThreaded()) ==
ClimaComms.CUDADevice()
end
@test Adapt.adapt(Array, ClimaComms.context(ClimaComms.CUDADevice())) ==
ClimaComms.context(ClimaComms.CPUSingleThreaded())
@static if ClimaComms.device() isa ClimaComms.CUDADevice
@test Adapt.adapt(Array, ClimaComms.context(ClimaComms.CUDADevice())) ==
ClimaComms.context(ClimaComms.CPUSingleThreaded())
@test Adapt.adapt(
CUDA.CuArray,
ClimaComms.context(ClimaComms.CUDADevice()),
) == ClimaComms.context(ClimaComms.CUDADevice())
@test Adapt.adapt(
CUDA.CuArray,
ClimaComms.context(ClimaComms.CPUSingleThreaded()),
) == ClimaComms.context(ClimaComms.CUDADevice())
end
end
@testset "Logging" begin
include("logging.jl")
end
@testset "Adapt" begin
include("adapt.jl")
end