-
Notifications
You must be signed in to change notification settings - Fork 256
Expand file tree
/
Copy pathtest_gpu_common.py
More file actions
1739 lines (1325 loc) · 62.6 KB
/
test_gpu_common.py
File metadata and controls
1739 lines (1325 loc) · 62.6 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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import cloudpickle as pickle
import numpy as np
import pytest
import scipy.sparse
import sympy
from conftest import assert_structure, skipif
from devito import (
Buffer, ConditionalDimension, Constant, Dimension, Eq, Function, Grid, Inc,
MatrixSparseTimeFunction, Operator, SparseTimeFunction, SubDimension, SubDomain,
SubDomainSet, TensorTimeFunction, TimeFunction, assign, configuration, exp,
switchconfig, switchenv
)
from devito.arch import Cpu64, Device, get_cpu_info, get_gpu_info
from devito.exceptions import InvalidArgument
from devito.ir import (
Conditional, Expression, FindNodes, FindSymbols, Section, retrieve_iteration_tree
)
from devito.passes.iet.languages.openmp import OmpIteration
from devito.types import DeviceID, DeviceRM, Lock, NPThreads, PThreadArray, Symbol
pytestmark = skipif(['nodevice'], whole_module=True)
class TestGPUInfo:
def test_get_gpu_info(self):
info = get_gpu_info()
known = ['nvidia', 'tesla', 'geforce', 'quadro', 'amd', 'unspecified']
try:
assert info['architecture'].lower() in known
except KeyError:
# There might be than one GPUs, but for now we don't care
# as we're not really exploiting this info yet...
pytest.xfail("Unsupported platform for get_gpu_info")
def custom_compiler(self):
grid = Grid(shape=(4, 4))
u = TimeFunction(name='u', grid=grid)
eqn = Eq(u.forward, u + 1)
with switchconfig(compiler='custom'):
op = Operator(eqn)()
# Check jit-compilation and correct execution
op.apply(time_M=10)
assert np.all(u.data[1] == 11)
def test_host_threads(self):
plat = configuration['platform']
assert isinstance(plat, Device)
nth = plat.cores_physical
assert nth == get_cpu_info()['physical']
assert nth == Cpu64("test").cores_physical
@switchconfig(platform='intel64', autopadding=True)
def test_autopad_with_platform_switch(self):
grid = Grid(shape=(10, 10))
f = Function(name='f', grid=grid, space_order=0)
assert f.shape_allocated[0] == 10
info = get_gpu_info()
if info['vendor'] == 'INTEL':
assert f.shape_allocated[1] == 16
elif info['vendor'] == 'NVIDIA':
assert f.shape_allocated[1] == 32
elif info['vendor'] == 'AMD':
assert f.shape_allocated[1] == 64
class TestDeviceID:
"""
Test that device IDs and associated environment variables such as
CUDA_VISIBLE_DEVICES are correctly handled.
"""
@pytest.mark.parametrize('env_variables', [{"CUDA_VISIBLE_DEVICES": "1"},
{"CUDA_VISIBLE_DEVICES": "1,2"},
{"CUDA_VISIBLE_DEVICES": "1,0"},
{"ROCR_VISIBLE_DEVICES": "1"},
{"HIP_VISIBLE_DEVICES": " 1"}])
def test_visible_devices(self, env_variables):
"""
Test that physical device IDs used for querying memory on a device via
nvidia-smi correctly account for visible-device environment variables.
"""
grid = Grid(shape=(10, 10))
u = Function(name='u', grid=grid)
eq = Eq(u, u+1)
with switchenv(env_variables):
op1 = Operator(eq)
argmap1 = op1.arguments()
# All variants in parameterisation should yield deviceid 1
assert argmap1._physical_deviceid == 1
# Check that physical deviceid is 0 when no environment variables set
op2 = Operator(eq)
argmap2 = op2.arguments()
# Default physical deviceid expected to be 0
assert argmap2._physical_deviceid == 0
@pytest.mark.parallel(mode=2)
@pytest.mark.parametrize('visible_devices', [
"1,2", "1,0", "0,2,3",
# Per rank VISIBLE_DEVICE
("1", "0"),
# Oversubscribed
"1",
])
def test_visible_devices_mpi(self, visible_devices, mode):
"""
Test that physical device IDs used for querying memory on a device via
nvidia-smi correctly account for visible-device environment variables
when using MPI.
"""
grid = Grid(shape=(10, 10))
rank = grid.distributor.myrank
u = Function(name='u', grid=grid)
eq = Eq(u, u+1)
if isinstance(visible_devices, tuple):
cu_device = visible_devices[rank]
expected = int(cu_device)
else:
cu_device = visible_devices
devices = visible_devices.split(',')
expected = int(devices[rank % len(devices)])
with switchenv({'CUDA_VISIBLE_DEVICES': cu_device}):
op1 = Operator(eq)
argmap1 = op1.arguments()
assert argmap1._physical_deviceid == expected
# In default case, physical deviceid will equal rank
op2 = Operator(eq)
argmap2 = op2.arguments()
assert argmap2._physical_deviceid == rank
def test_visible_devices_with_devito_deviceid(self):
"""Test interaction between CUDA_VISIBLE_DEVICES and DEVITO_DEVICEID"""
grid = Grid(shape=(10, 10))
u = Function(name='u', grid=grid)
eq = Eq(u, u+1)
with switchenv({'CUDA_VISIBLE_DEVICES': "1,3"}), switchconfig(deviceid=1):
op = Operator(eq)
argmap = op.arguments()
# deviceid should see the world from within CUDA_VISIBLE_DEVICES
# So should be the second of the two visible devices specified (3)
assert argmap._physical_deviceid == 3
with switchenv({'CUDA_VISIBLE_DEVICES': "1"}), switchconfig(deviceid=0):
op1 = Operator(eq)
argmap1 = op1.arguments()
assert argmap1._physical_deviceid == 1
@pytest.mark.parallel(mode=2)
def test_deviceid_per_rank(self, mode):
"""
Test that Device IDs set by the user on a per-rank basis do not
get modified.
"""
# Reversed order to ensure it is different to default
user_set_deviceids = (1, 0)
grid = Grid(shape=(10, 10))
u = Function(name='u', grid=grid)
rank = grid.distributor.myrank
deviceid = user_set_deviceids[rank]
op = Operator(Eq(u, u+1))
argmap = op.arguments(deviceid=deviceid)
assert argmap._physical_deviceid == deviceid
class TestCodeGeneration:
def test_maxpar_option(self):
"""
Make sure the `cire-maxpar` option is set to True by default.
"""
grid = Grid(shape=(10, 10, 10))
u = TimeFunction(name='u', grid=grid, space_order=4)
eq = Eq(u.forward, u.dy.dy)
op = Operator(eq)
trees = retrieve_iteration_tree(op)
assert len(trees) == 2
assert trees[0][0] is trees[1][0]
assert trees[0][1] is not trees[1][1]
@pytest.mark.parametrize('dtype', [np.complex64, np.complex128])
def test_complex(self, dtype):
grid = Grid((5, 5))
x, y = grid.dimensions
c = Constant(name='c', dtype=dtype)
u = Function(name="u", grid=grid, dtype=dtype)
eq = Eq(u, x + sympy.I*y + exp(sympy.I + x.spacing) * c)
op = Operator(eq)
op(c=1.0 + 2.0j)
# Check against numpy
dx = grid.spacing_map[x.spacing]
xx, yy = np.meshgrid(np.linspace(0, 4, 5), np.linspace(0, 4, 5))
npres = xx + 1j*yy + np.exp(1j + dx) * (1.0 + 2.0j)
assert np.allclose(u.data, npres.T, rtol=5e-7, atol=0)
class TestPassesOptional:
def test_linearize(self):
grid = Grid(shape=(4, 4))
u = TimeFunction(name='u', grid=grid)
eqn = Eq(u.forward, u + 1)
op = Operator(eqn, opt=('advanced', {'linearize': True}))
# Check generated code
assert 'uL0' in str(op)
# Check jit-compilation and correct execution
op.apply(time_M=10)
assert np.all(u.data[1] == 11)
class TestPassesEdgeCases:
def test_fission(self):
nt = 20
grid = Grid(shape=(10, 10))
time = grid.time_dim
usave = TimeFunction(name='usave', grid=grid, save=nt, time_order=2)
vsave = TimeFunction(name='vsave', grid=grid, save=nt, time_order=2)
ctime0 = ConditionalDimension(name='ctime', parent=time, condition=time > 4)
ctime1 = ConditionalDimension(name='ctime', parent=time, condition=time <= 4)
eqns = [Eq(usave, time + 0.2, implicit_dims=[ctime0]),
Eq(vsave, time + 0.2, implicit_dims=[ctime1])]
op = Operator(eqns)
# Check generated code
trees = retrieve_iteration_tree(op, mode='superset')
assert len(trees) == 2
assert trees[0].root is trees[1].root
assert trees[0][1] is not trees[1][1]
assert trees[0].root.dim is time
assert not trees[0].root.pragmas
assert trees[0][1].pragmas
assert trees[1][1].pragmas
op.apply()
expected = np.full(shape=(20, 10, 10), fill_value=0.2, dtype=np.float32)
for i in range(nt):
expected[i] += i
assert np.all(usave.data[5:] == expected[5:])
assert np.all(vsave.data[:5] == expected[:5])
def test_incr_perfect_outer(self):
grid = Grid((5, 5))
d = Dimension(name="d")
u = Function(name="u", dimensions=(*grid.dimensions, d),
grid=grid, shape=(*grid.shape, 5), )
v = Function(name="v", dimensions=(*grid.dimensions, d),
grid=grid, shape=(*grid.shape, 5))
w = Function(name="w", grid=grid)
u.data.fill(1)
v.data.fill(2)
summation = Inc(w, u*v)
op = Operator([summation])
assert 'reduction' not in str(op)
assert 'collapse(2)' in str(op)
assert 'parallel' in str(op)
op()
assert np.all(w.data == 10)
def test_reduction_many_dims(self):
grid = Grid(shape=(25, 25, 25))
u = TimeFunction(name='u', grid=grid, time_order=1, save=Buffer(1))
s = Symbol(name='s', dtype=np.float32)
eqns = [Eq(s, 0),
Inc(s, 2*u + 1)]
op0 = Operator(eqns)
op1 = Operator(eqns, opt=('advanced', {'mapify-reduce': True}))
tree, = retrieve_iteration_tree(op0)
assert 'collapse(3) reduction(+:s)' in str(tree[1].pragmas[0])
tree, = retrieve_iteration_tree(op1)
assert 'collapse(3) reduction(+:s)' in str(tree[1].pragmas[0])
class Bundle(SubDomain):
"""
We use this SubDomain to enforce Eqs to end up in different loops.
"""
name = 'bundle'
def define(self, dimensions):
x, y, z = dimensions
return {x: ('middle', 0, 0), y: ('middle', 0, 0), z: ('middle', 0, 0)}
class TestStreaming:
@pytest.mark.parametrize('opt', [
('tasking', 'orchestrate'),
('tasking', 'orchestrate', {'linearize': True}),
])
def test_tasking_in_isolation(self, opt):
nt = 10
bundle0 = Bundle()
grid = Grid(shape=(10, 10, 10), subdomains=bundle0)
tmp = Function(name='tmp', grid=grid)
u = TimeFunction(name='u', grid=grid, save=nt)
v = TimeFunction(name='v', grid=grid)
eqns = [Eq(tmp, v),
Eq(v.forward, v + 1),
Eq(u.forward, tmp, subdomain=bundle0)]
op = Operator(eqns, opt=opt)
# Check generated code
assert len(retrieve_iteration_tree(op)) == 3
assert len([i for i in FindSymbols().visit(op) if isinstance(i, Lock)]) == 1
sections = FindNodes(Section).visit(op)
assert len(sections) == 3
assert str(sections[0].body[0].body[0].body[0].body[0]) == 'while(lock0[0] == 0);'
body = op._func_table['release_lock0'].root.body
assert str(body.body[0].condition) == 'Ne(lock0[0], 2)'
assert str(body.body[1]) == 'lock0[0] = 0;'
body = op._func_table['activate0'].root.body
assert str(body.body[0].condition) == 'Ne(sdata0[0].flag, 1)'
assert str(body.body[1]) == 'sdata0[0].time = time;'
assert str(body.body[2]) == 'sdata0[0].flag = 2;'
op.apply(time_M=nt-2)
assert np.all(u.data[nt-1] == 8)
def test_tasking_fused(self):
nt = 10
bundle0 = Bundle()
grid = Grid(shape=(10, 10, 10), subdomains=bundle0)
tmp = Function(name='tmp', grid=grid)
u = TimeFunction(name='u', grid=grid, save=nt)
v = TimeFunction(name='v', grid=grid, save=nt)
w = TimeFunction(name='w', grid=grid)
eqns = [Eq(w.forward, w + 1),
Eq(tmp, w.forward),
Eq(u.forward, tmp, subdomain=bundle0),
Eq(v.forward, tmp, subdomain=bundle0)]
op = Operator(eqns, opt=('tasking', 'fuse', 'orchestrate', {'linearize': False}))
# Check generated code
assert len(retrieve_iteration_tree(op)) == 3
locks = [i for i in FindSymbols().visit(op) if isinstance(i, Lock)]
assert len(locks) == 1 # Only 1 because it's only `tmp` that needs protection
assert len(op._func_table) == 5
exprs = FindNodes(Expression).visit(op._func_table['copy_to_host0'].root)
b = 17 if configuration['language'] == 'openacc' else 16 # No `qid` w/ OMP
assert str(exprs[b]) == 'const int deviceid = sdata0->deviceid;'
assert str(exprs[b+1]) == 'volatile int time = sdata0->time;'
assert str(exprs[b+2]) == 'lock0[0] = 1;'
assert exprs[b+3].write is u
assert exprs[b+4].write is v
assert str(exprs[b+5]) == 'lock0[0] = 2;'
assert str(exprs[b+6]) == 'sdata0->flag = 1;'
op.apply(time_M=nt-2)
assert np.all(u.data[nt-1] == 9)
assert np.all(v.data[nt-1] == 9)
def test_tasking_unfused_two_locks(self):
nt = 10
bundle0 = Bundle()
grid = Grid(shape=(10, 10, 10), subdomains=bundle0)
tmp0 = Function(name='tmp0', grid=grid)
tmp1 = Function(name='tmp1', grid=grid)
u = TimeFunction(name='u', grid=grid, save=nt)
v = TimeFunction(name='v', grid=grid, save=nt)
w = TimeFunction(name='w', grid=grid)
eqns = [Eq(w.forward, w + 1),
Eq(tmp0, w.forward),
Eq(tmp1, w.forward),
Eq(u.forward, tmp0, subdomain=bundle0),
Eq(v.forward, tmp1, subdomain=bundle0)]
op = Operator(eqns, opt=('tasking', 'fuse', 'orchestrate', {'linearize': False}))
# Check generated code
trees = retrieve_iteration_tree(op)
assert len(trees) == 3
assert len([i for i in FindSymbols().visit(op) if isinstance(i, Lock)]) == 5
sections = FindNodes(Section).visit(op)
assert len(sections) == 4
assert (str(sections[1].body[0].body[0].body[0].body[0]) ==
'while(lock0[0] == 0 || lock1[0] == 0);') # Wait-lock
body = sections[2].body[0].body[0]
assert str(body.body[0]) == 'release_lock0(lock0);'
assert str(body.body[1]) == 'activate0(time,sdata0);'
assert len(op._func_table) == 5
exprs = FindNodes(Expression).visit(op._func_table['copy_to_host0'].root)
b = 18 if configuration['language'] == 'openacc' else 17 # No `qid` w/ OMP
assert str(exprs[b]) == 'lock0[0] = 1;'
op.apply(time_M=nt-2)
assert np.all(u.data[nt-1] == 9)
assert np.all(v.data[nt-1] == 9)
def test_tasking_forcefuse(self):
nt = 10
bundle0 = Bundle()
grid = Grid(shape=(10, 10, 10), subdomains=bundle0)
tmp0 = Function(name='tmp0', grid=grid)
tmp1 = Function(name='tmp1', grid=grid)
u = TimeFunction(name='u', grid=grid, save=nt)
v = TimeFunction(name='v', grid=grid, save=nt)
w = TimeFunction(name='w', grid=grid)
eqns = [Eq(w.forward, w + 1),
Eq(tmp0, w.forward),
Eq(tmp1, w.forward),
Eq(u.forward, tmp0, subdomain=bundle0),
Eq(v.forward, tmp1, subdomain=bundle0)]
op = Operator(eqns, opt=('tasking', 'fuse', 'orchestrate',
{'fuse-tasks': True, 'linearize': False}))
# Check generated code
assert len(retrieve_iteration_tree(op)) == 3
assert len([i for i in FindSymbols().visit(op) if isinstance(i, Lock)]) == 2
sections = FindNodes(Section).visit(op)
assert len(sections) == 3
assert (str(sections[1].body[0].body[0].body[0].body[0]) ==
'while(lock0[0] == 0 || lock1[0] == 0);') # Wait-lock
body = op._func_table['release_lock0'].root.body
assert str(body.body[0].condition) == 'Ne(lock0[0], 2) | Ne(lock1[0], 2)'
assert str(body.body[1]) == 'lock0[0] = 0;' # Set-lock
assert str(body.body[2]) == 'lock1[0] = 0;' # Set-lock
body = op._func_table['activate0'].root.body
assert str(body.body[0].condition) == 'Ne(sdata0[0].flag, 1)' # Wait-thread
assert str(body.body[1]) == 'sdata0[0].time = time;'
assert str(body.body[2]) == 'sdata0[0].flag = 2;'
assert len(op._func_table) == 5
exprs = FindNodes(Expression).visit(op._func_table['copy_to_host0'].root)
b = 21 if configuration['language'] == 'openacc' else 20 # No `qid` w/ OMP
assert str(exprs[b]) == 'lock0[0] = 1;'
assert str(exprs[b+1]) == 'lock1[0] = 1;'
assert exprs[b+2].write is u
assert exprs[b+3].write is v
op.apply(time_M=nt-2)
assert np.all(u.data[nt-1] == 9)
assert np.all(v.data[nt-1] == 9)
@pytest.mark.parametrize('opt', [
('tasking', 'orchestrate'),
('tasking', 'streaming', 'orchestrate'),
])
def test_attempt_tasking_but_no_temporaries(self, opt):
grid = Grid(shape=(10, 10, 10))
u = TimeFunction(name='u', grid=grid, save=10)
op = Operator(Eq(u.forward, u + 1), opt=opt)
piters = FindNodes(OmpIteration).visit(op)
assert len(piters) == 0
op = Operator(Eq(u.forward, u + 1), opt=(opt, {'par-disabled': False}))
# Degenerates to host execution with no data movement, since `u` is
# a host Function
piters = FindNodes(OmpIteration).visit(op)
assert len(piters) == 1
assert isinstance(piters.pop(), OmpIteration)
def test_tasking_multi_output(self):
nt = 10
bundle0 = Bundle()
grid = Grid(shape=(10, 10, 10), subdomains=bundle0)
t = grid.stepping_dim
x, y, z = grid.dimensions
u = TimeFunction(name='u', grid=grid, time_order=2)
u1 = TimeFunction(name='u', grid=grid, time_order=2)
usave = TimeFunction(name='usave', grid=grid, save=nt)
usave1 = TimeFunction(name='usave', grid=grid, save=nt)
eqns = [Eq(u.forward, u + 1),
Eq(usave, u.forward + u + u.backward + u[t, x-1, y, z],
subdomain=bundle0)]
op0 = Operator(eqns, opt=('noop', {'gpu-fit': usave}))
op1 = Operator(eqns, opt=('tasking', 'orchestrate', {'linearize': False}))
# Check generated code
assert len(retrieve_iteration_tree(op1)) == 2
assert len([i for i in FindSymbols().visit(op1) if isinstance(i, Lock)]) == 1
sections = FindNodes(Section).visit(op1)
assert len(sections) == 2
assert str(sections[0].body[0].body[0].body[0].body[0]) ==\
'while(lock0[t2] == 0);'
body = op1._func_table['release_lock0'].root.body
for i in range(3):
assert 'lock0[t' in str(body.body[1 + i]) # Set-lock
body = op1._func_table['activate0'].root.body
assert str(body.body[-1]) == 'sdata0[wi0].flag = 2;'
assert len(op1._func_table) == 5
exprs = FindNodes(Expression).visit(op1._func_table['copy_to_host0'].root)
b = 21 if configuration['language'] == 'openacc' else 20 # No `qid` w/ OMP
for i in range(3):
assert 'lock0[t' in str(exprs[b + i])
assert exprs[b+3].write is usave
op0.apply(time_M=nt-2)
op1.apply(time_M=nt-2, u=u1, usave=usave1)
assert np.all(u.data[:] == u1.data[:])
assert np.all(usave.data[:] == usave1.data[:])
def test_tasking_lock_placement(self):
grid = Grid(shape=(10, 10, 10))
f = Function(name='f', grid=grid, space_order=2)
u = TimeFunction(name='u', grid=grid)
usave = TimeFunction(name='usave', grid=grid, save=10)
eqns = [Eq(f, u + 1),
Eq(u.forward, f.dx + u + 1),
Eq(usave, u)]
op = Operator(eqns, opt=('tasking', 'orchestrate'))
# Check generated code -- the wait-lock is expected in section1
assert len(retrieve_iteration_tree(op)) == 3
assert len([i for i in FindSymbols().visit(op) if isinstance(i, Lock)]) == 1
sections = FindNodes(Section).visit(op)
assert len(sections) == 3
assert sections[0].body[0].body[0].body[0].is_Iteration
assert str(sections[1].body[0].body[0].body[0].body[0]) ==\
'while(lock0[t1] == 0);'
@pytest.mark.parametrize('opt,ntmps', [
(('buffering', 'streaming', 'orchestrate'), 3),
(('buffering', 'streaming', 'orchestrate', {'linearize': True}), 3),
])
def test_streaming_basic(self, opt, ntmps):
nt = 10
grid = Grid(shape=(4, 4))
u = TimeFunction(name='u', grid=grid)
usave = TimeFunction(name='usave', grid=grid, save=nt)
for i in range(nt):
usave.data[i, :] = i
eqn = Eq(u.forward, u + usave)
op = Operator(eqn, opt=opt)
# Check generated code
if configuration['language'] == 'openacc':
assert len(op._func_table) == 9
else:
assert len(op._func_table) == 8
assert len([i for i in FindSymbols().visit(op) if i.is_Array]) == ntmps
op.apply(time_M=nt-2)
assert np.all(u.data[0] == 28)
assert np.all(u.data[1] == 36)
@pytest.mark.parametrize('opt,ntmps', [
(('buffering', 'streaming', 'orchestrate'), 14),
(('buffering', 'streaming', 'fuse', 'orchestrate', {'fuse-tasks': True}), 8),
])
def test_streaming_two_buffers(self, opt, ntmps):
nt = 10
grid = Grid(shape=(4, 4))
u = TimeFunction(name='u', grid=grid)
usave = TimeFunction(name='usave', grid=grid, save=nt)
vsave = TimeFunction(name='vsave', grid=grid, save=nt)
for i in range(nt):
usave.data[i, :] = i
vsave.data[i, :] = i
eqn = Eq(u.forward, u + usave + vsave)
op = Operator(eqn, opt=opt)
# Check generated code
arrays = [i for i in FindSymbols().visit(op) if i.is_Array]
if configuration['language'] == 'openacc':
assert len(op._func_table) == 9
assert len(arrays) == ntmps
else:
assert len(op._func_table) == 8
assert len(arrays) == ntmps - 1
op.apply(time_M=nt-2)
assert np.all(u.data[0] == 56)
assert np.all(u.data[1] == 72)
def test_streaming_fused(self):
nt = 10
grid = Grid(shape=(4, 4))
u = TimeFunction(name='u', grid=grid)
v = TimeFunction(name='v', grid=grid)
usave = TimeFunction(name='usave', grid=grid, save=nt)
vsave = TimeFunction(name='vsave', grid=grid, save=nt)
for i in range(nt):
usave.data[i, :] = i
vsave.data[i, :] = i
eqns = [Eq(u.forward, u + usave + vsave),
Eq(v.forward, v + usave + vsave)]
op = Operator(eqns, opt=('buffering', 'streaming', 'fuse', 'orchestrate'))
# Check generated code
trees = retrieve_iteration_tree(op)
assert len(trees) == 2
assert trees[0][-1].nodes[0].body[0].write is u
assert trees[0][-1].nodes[0].body[1].write is v
op.apply(time_M=nt-2)
assert np.all(u.data[0] == 56)
assert np.all(v.data[0] == 56)
assert np.all(u.data[1] == 72)
assert np.all(v.data[1] == 72)
@pytest.mark.parametrize('opt', [
('buffering', 'streaming', 'orchestrate'),
])
def test_streaming_conddim_forward(self, opt):
nt = 10
grid = Grid(shape=(4, 4))
time_dim = grid.time_dim
factor = Constant(name='factor', value=2, dtype=np.int32)
time_sub = ConditionalDimension(name="time_sub", parent=time_dim, factor=factor)
u = TimeFunction(name='u', grid=grid)
usave = TimeFunction(name='usave', grid=grid, time_order=0,
save=(int(nt//factor.data)), time_dim=time_sub)
for i in range(usave.save):
usave.data[i, :] = i
eqn = Eq(u.forward, u.forward + u + usave)
op = Operator(eqn, opt=opt)
# TODO: we are *not* using the last entry of usave, so we gotta ensure
# it is *not* streamed on to the device (thus avoiding dangerous leaks).
# But how can we explicitly check this?
time_M = 6
op.apply(time_M=time_M)
# We entered the eq four times (at time=0,2,4,6)
# Since factor=2, we *only* write to u.data[(time+1)%2]=u.data[1]
assert np.all(u.data[0] == 0)
# 1st time u[1] = u[0]+u[1]+usave[0] = 0+0+0 = 0
# 2nd time u[1] = u[0]+u[1]+usave[1] = 0+0+1 = 1
# 3rd time u[1] = u[0]+u[1]+usave[2] = 0+1+2 = 3
# 4th time u[1] = u[0]+u[1]+usave[3] = 0+3+3 = 6
assert np.all(u.data[1] == 6)
@pytest.mark.parametrize('opt', [
('buffering', 'streaming', 'orchestrate'),
])
def test_streaming_conddim_backward(self, opt):
nt = 10
grid = Grid(shape=(4, 4))
time_dim = grid.time_dim
factor = Constant(name='factor', value=2, dtype=np.int32)
time_sub = ConditionalDimension(name="time_sub", parent=time_dim, factor=factor)
u = TimeFunction(name='u', grid=grid)
usave = TimeFunction(name='usave', grid=grid, time_order=0,
save=(int(nt//factor.data)), time_dim=time_sub)
for i in range(usave.save):
usave.data[i, :] = i
eqn = Eq(u.backward, u.backward + u + usave)
op = Operator(eqn, opt=opt)
# TODO: we are *not* using the first two entries of usave, so we gotta ensure
# they are *not* streamed on to the device (thus avoiding dangerous leaks).
# But how can we explicitly check this?
time_m = 4
op.apply(time_m=time_m, time_M=nt-2)
# We entered the eq three times (at time=8,6,4)
# Since factor=2, we *only* write to u.data[(time-1)%2]=u.data[1]
assert np.all(u.data[0] == 0)
# 1st time u[1] = u[0]+u[1]+usave[4] = 0+0+4 = 4
# 2nd time u[1] = u[0]+u[1]+usave[3] = 0+4+3 = 7
# 3rd time u[1] = u[0]+u[1]+usave[2] = 0+7+2 = 9
assert np.all(u.data[1] == 9)
@pytest.mark.parametrize('opt,ntmps', [
(('buffering', 'streaming', 'orchestrate'), 3),
])
def test_streaming_multi_input(self, opt, ntmps):
nt = 100
grid = Grid(shape=(10, 10))
u = TimeFunction(name='u', grid=grid, save=nt, time_order=2, space_order=2)
v = TimeFunction(name='v', grid=grid, save=None, time_order=2, space_order=2)
grad = Function(name='grad', grid=grid)
grad1 = Function(name='grad', grid=grid)
v.data[:] = 0.02
for i in range(nt):
u.data[i, :] = i + 0.1
eqn = Eq(grad, grad - u.dt2 * v)
op0 = Operator(eqn, opt=('noop', {'gpu-fit': u}))
op1 = Operator(eqn, opt=opt)
# Check generated code
if configuration['language'] == 'openacc':
assert len(op1._func_table) == 9
else:
assert len(op1._func_table) == 8
assert len([i for i in FindSymbols().visit(op1) if i.is_Array]) == ntmps
op0.apply(time_M=nt-2, dt=0.1)
op1.apply(time_M=nt-2, dt=0.1, grad=grad1)
assert np.all(grad.data == grad1.data)
def test_streaming_multi_input_conddim_foward(self):
nt = 10
grid = Grid(shape=(4, 4))
time_dim = grid.time_dim
x, y = grid.dimensions
factor = Constant(name='factor', value=2, dtype=np.int32)
time_sub = ConditionalDimension(name="time_sub", parent=time_dim, factor=factor)
u = TimeFunction(name='u', grid=grid, time_order=2, save=nt, time_dim=time_sub)
v = TimeFunction(name='v', grid=grid)
v1 = TimeFunction(name='v', grid=grid)
for i in range(u.save):
u.data[i, :] = i
expr = u.dt2 + 3.*u.dt(x0=time_sub - time_sub.spacing)
eqns = [Eq(v.forward, v + expr + 1.)]
op0 = Operator(eqns, opt=('noop', {'gpu-fit': u}))
op1 = Operator(eqns, opt=('buffering', 'streaming', 'orchestrate'))
op0.apply(time_M=nt, dt=.01)
op1.apply(time_M=nt, dt=.01, v=v1)
assert np.all(v.data == v1.data)
def test_streaming_multi_input_conddim_backward(self):
nt = 10
grid = Grid(shape=(4, 4))
time_dim = grid.time_dim
x, y = grid.dimensions
factor = Constant(name='factor', value=2, dtype=np.int32)
time_sub = ConditionalDimension(name="time_sub", parent=time_dim, factor=factor)
u = TimeFunction(name='u', grid=grid, time_order=2, save=nt, time_dim=time_sub)
v = TimeFunction(name='v', grid=grid)
v1 = TimeFunction(name='v', grid=grid)
for i in range(u.save):
u.data[i, :] = i
expr = u.dt2 + 3.*u.dt(x0=time_sub - time_sub.spacing)
eqns = [Eq(v.backward, v + expr + 1.)]
op0 = Operator(eqns, opt=('noop', {'gpu-fit': u}))
op1 = Operator(eqns, opt=('buffering', 'streaming', 'orchestrate'))
op0.apply(time_M=nt, dt=.01)
op1.apply(time_M=nt, dt=.01, v=v1)
assert np.all(v.data == v1.data)
@pytest.mark.parametrize('opt,ntmps', [
(('buffering', 'streaming', 'orchestrate'), 3),
])
def test_streaming_postponed_deletion(self, opt, ntmps):
nt = 10
grid = Grid(shape=(10, 10, 10))
u = TimeFunction(name='u', grid=grid)
v = TimeFunction(name='v', grid=grid)
usave = TimeFunction(name='usave', grid=grid, save=nt)
u1 = TimeFunction(name='u', grid=grid)
v1 = TimeFunction(name='v', grid=grid)
for i in range(nt):
usave.data[i, :] = i
eqns = [Eq(u.forward, u + usave),
Eq(v.forward, v + u.forward.dx + usave)]
op0 = Operator(eqns, opt=('noop', {'gpu-fit': usave}))
op1 = Operator(eqns, opt=opt)
# Check generated code
if configuration['language'] == 'openacc':
assert len(op1._func_table) == 9
else:
assert len(op1._func_table) == 8
assert len([i for i in FindSymbols().visit(op1) if i.is_Array]) == ntmps
op0.apply(time_M=nt-1)
op1.apply(time_M=nt-1, u=u1, v=v1)
assert np.all(u.data == u1.data)
assert np.all(v.data == v1.data)
def test_composite_buffering_tasking(self):
nt = 10
bundle0 = Bundle()
grid = Grid(shape=(4, 4, 4), subdomains=bundle0)
u = TimeFunction(name='u', grid=grid, time_order=2)
u1 = TimeFunction(name='u', grid=grid, time_order=2)
usave = TimeFunction(name='usave', grid=grid, save=nt)
usave1 = TimeFunction(name='usave', grid=grid, save=nt)
eqns = [Eq(u.forward, u*1.1 + 1),
Eq(usave, u.dt2, subdomain=bundle0)]
op0 = Operator(eqns, opt=('noop', {'gpu-fit': usave}))
op1 = Operator(eqns, opt=('buffering', 'tasking', 'orchestrate'))
# Check generated code -- thanks to buffering only expect 1 lock!
assert len(retrieve_iteration_tree(op0)) == 2
assert len(retrieve_iteration_tree(op1)) == 3
symbols = FindSymbols().visit(op1)
assert len([i for i in symbols if isinstance(i, Lock)]) == 1
threads = [i for i in symbols if isinstance(i, PThreadArray)]
assert len(threads) == 1
assert threads[0].size == 1
op0.apply(time_M=nt-1, dt=0.1)
op1.apply(time_M=nt-1, dt=0.1, u=u1, usave=usave1)
assert np.all(u.data == u1.data)
assert np.all(usave.data == usave1.data)
def test_composite_buffering_tasking_multi_output(self):
nt = 10
bundle0 = Bundle()
grid = Grid(shape=(4, 4, 4), subdomains=bundle0)
u = TimeFunction(name='u', grid=grid, time_order=2)
v = TimeFunction(name='v', grid=grid, time_order=2)
usave = TimeFunction(name='usave', grid=grid, save=nt)
vsave = TimeFunction(name='vsave', grid=grid, save=nt)
eqns = [Eq(u.forward, u + 1),
Eq(v.forward, v + 1),
Eq(usave, u, subdomain=bundle0),
Eq(vsave, v, subdomain=bundle0)]
async_degree = 3
op0 = Operator(eqns, opt=('noop', {'gpu-fit': (usave, vsave)}))
op1 = Operator(eqns, opt=('buffering', 'tasking', 'topofuse', 'orchestrate',
{'buf-async-degree': async_degree}))
op2 = Operator(eqns, opt=('buffering', 'tasking', 'topofuse', 'orchestrate',
{'buf-async-degree': async_degree,
'fuse-tasks': True}))
# Check generated code -- thanks to buffering only expect 1 lock!
assert len(retrieve_iteration_tree(op0)) == 2
assert len(retrieve_iteration_tree(op1)) == 4
assert len(retrieve_iteration_tree(op2)) == 3
symbols = FindSymbols().visit(op1)
assert len([i for i in symbols if isinstance(i, Lock)]) == 5
threads = [i for i in symbols if isinstance(i, PThreadArray)]
assert len(threads) == 2
assert threads[0].size.size == async_degree
assert threads[1].size.size == async_degree
symbols = FindSymbols().visit(op2)
assert len([i for i in symbols if isinstance(i, Lock)]) == 1 + 1
threads = [i for i in symbols if isinstance(i, PThreadArray)]
assert len(threads) == 1
assert threads[0].size.size == async_degree
# It is true that the usave and vsave eqns are separated in two different
# loop nests, but they eventually get mapped to the same pair of efuncs,
# since devito attempts to maximize code reuse
if configuration['language'] == 'openacc':
assert len(op1._func_table) == 8
else:
assert len(op1._func_table) == 7
# Check output
op0.apply(time_M=nt-1)
for op in [op1, op2]:
u1 = TimeFunction(name='u', grid=grid, time_order=2)
v1 = TimeFunction(name='v', grid=grid, time_order=2)
usave1 = TimeFunction(name='usave', grid=grid, save=nt)
vsave1 = TimeFunction(name='vsave', grid=grid, save=nt)
op.apply(time_M=nt-1, u=u1, v=v1, usave=usave1, vsave=vsave1)
assert np.all(u.data == u1.data)
assert np.all(v.data == v1.data)
assert np.all(usave.data == usave1.data)
assert np.all(vsave.data == vsave1.data)
def test_composite_full_0(self):
nt = 10
grid = Grid(shape=(10, 10, 10))
u = TimeFunction(name='u', grid=grid)
u1 = TimeFunction(name='u', grid=grid)
fsave = TimeFunction(name='fsave', grid=grid, save=nt)
usave = TimeFunction(name='usave', grid=grid, save=nt)
usave1 = TimeFunction(name='usave', grid=grid, save=nt)
for i in range(nt):
fsave.data[i, :] = i
eqns = [Eq(u.forward, u + fsave + 1),
Eq(usave, u)]
op0 = Operator(eqns, opt=('noop', {'gpu-fit': (fsave, usave)}))
op1 = Operator(eqns, opt=('buffering', 'tasking', 'streaming', 'orchestrate'))
# Check generated code