/
test_mpi.py
1636 lines (1344 loc) · 61.7 KB
/
test_mpi.py
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 numpy as np
import pytest
from unittest.mock import patch
from conftest import skipif
from devito import (Grid, Constant, Function, TimeFunction, SparseFunction,
SparseTimeFunction, Dimension, ConditionalDimension, SubDimension,
Eq, Inc, NODE, Operator, norm, inner, configuration, switchconfig,
generic_derivative)
from devito.data import LEFT, RIGHT
from devito.ir.iet import Call, Conditional, Iteration, FindNodes, retrieve_iteration_tree
from devito.mpi import MPI
from examples.seismic.acoustic import acoustic_setup
pytestmark = skipif(['yask', 'ops', 'nompi'])
class TestDistributor(object):
@pytest.mark.parallel(mode=[2, 4])
def test_partitioning(self):
grid = Grid(shape=(15, 15))
f = Function(name='f', grid=grid)
distributor = grid.distributor
expected = { # nprocs -> [(rank0 shape), (rank1 shape), ...]
2: [(8, 15), (7, 15)],
4: [(8, 8), (8, 7), (7, 8), (7, 7)]
}
assert f.shape == expected[distributor.nprocs][distributor.myrank]
@pytest.mark.parallel(mode=[2, 4])
def test_partitioning_fewer_dims(self):
"""Test domain decomposition for Functions defined over a strict subset
of grid-decomposed dimensions."""
size_x, size_y = 16, 16
grid = Grid(shape=(size_x, size_y))
x, y = grid.dimensions
# A function with fewer dimensions that in `grid`
f = Function(name='f', grid=grid, dimensions=(x,), shape=(size_x,))
distributor = grid.distributor
expected = { # nprocs -> [(rank0 shape), (rank1 shape), ...]
2: [(8,), (8,)],
4: [(8,), (8,), (8,), (8,)]
}
assert f.shape == expected[distributor.nprocs][distributor.myrank]
@pytest.mark.parallel(mode=9)
def test_neighborhood_horizontal_2d(self):
grid = Grid(shape=(3, 3))
x, y = grid.dimensions
distributor = grid.distributor
# Rank map:
# ---------------y
# | 0 | 1 | 2 |
# -------------
# | 3 | 4 | 5 |
# -------------
# | 6 | 7 | 8 |
# -------------
# |
# x
PN = MPI.PROC_NULL
expected = {
0: {x: {LEFT: PN, RIGHT: 3}, y: {LEFT: PN, RIGHT: 1}},
1: {x: {LEFT: PN, RIGHT: 4}, y: {LEFT: 0, RIGHT: 2}},
2: {x: {LEFT: PN, RIGHT: 5}, y: {LEFT: 1, RIGHT: PN}},
3: {x: {LEFT: 0, RIGHT: 6}, y: {LEFT: PN, RIGHT: 4}},
4: {x: {LEFT: 1, RIGHT: 7}, y: {LEFT: 3, RIGHT: 5}},
5: {x: {LEFT: 2, RIGHT: 8}, y: {LEFT: 4, RIGHT: PN}},
6: {x: {LEFT: 3, RIGHT: PN}, y: {LEFT: PN, RIGHT: 7}},
7: {x: {LEFT: 4, RIGHT: PN}, y: {LEFT: 6, RIGHT: 8}},
8: {x: {LEFT: 5, RIGHT: PN}, y: {LEFT: 7, RIGHT: PN}},
}
assert expected[distributor.myrank][x] == distributor.neighborhood[x]
assert expected[distributor.myrank][y] == distributor.neighborhood[y]
@pytest.mark.parallel(mode=9)
def test_neighborhood_diagonal_2d(self):
grid = Grid(shape=(3, 3))
x, y = grid.dimensions
distributor = grid.distributor
# Rank map:
# ---------------y
# | 0 | 1 | 2 |
# -------------
# | 3 | 4 | 5 |
# -------------
# | 6 | 7 | 8 |
# -------------
# |
# x
PN = MPI.PROC_NULL
expected = {
0: {(LEFT, LEFT): PN, (LEFT, RIGHT): PN, (RIGHT, LEFT): PN, (RIGHT, RIGHT): 4}, # noqa
1: {(LEFT, LEFT): PN, (LEFT, RIGHT): PN, (RIGHT, LEFT): 3, (RIGHT, RIGHT): 5},
2: {(LEFT, LEFT): PN, (LEFT, RIGHT): PN, (RIGHT, LEFT): 4, (RIGHT, RIGHT): PN}, # noqa
3: {(LEFT, LEFT): PN, (LEFT, RIGHT): 1, (RIGHT, LEFT): PN, (RIGHT, RIGHT): 7},
4: {(LEFT, LEFT): 0, (LEFT, RIGHT): 2, (RIGHT, LEFT): 6, (RIGHT, RIGHT): 8},
5: {(LEFT, LEFT): 1, (LEFT, RIGHT): PN, (RIGHT, LEFT): 7, (RIGHT, RIGHT): PN},
6: {(LEFT, LEFT): PN, (LEFT, RIGHT): 4, (RIGHT, LEFT): PN, (RIGHT, RIGHT): PN}, # noqa
7: {(LEFT, LEFT): 3, (LEFT, RIGHT): 5, (RIGHT, LEFT): PN, (RIGHT, RIGHT): PN},
8: {(LEFT, LEFT): 4, (LEFT, RIGHT): PN, (RIGHT, LEFT): PN, (RIGHT, RIGHT): PN} # noqa
}
assert all(expected[distributor.myrank][i] == distributor.neighborhood[i]
for i in [(LEFT, LEFT), (LEFT, RIGHT), (RIGHT, LEFT), (RIGHT, RIGHT)])
@pytest.mark.parallel(mode=[2, 4])
def test_ctypes_neighborhood(self):
grid = Grid(shape=(4, 4))
distributor = grid.distributor
PN = MPI.PROC_NULL
attrs = ['ll', 'lc', 'lr', 'cl', 'cc', 'cr', 'rl', 'rc', 'rr']
expected = { # nprocs -> [(rank0 xleft xright ...), (rank1 xleft ...), ...]
2: [(PN, PN, PN, PN, 0, PN, PN, 1, PN),
(PN, 0, PN, PN, 1, PN, PN, PN, PN)],
4: [(PN, PN, PN, PN, 0, 1, PN, 2, 3),
(PN, PN, PN, 0, 1, PN, 2, 3, PN),
(PN, 0, 1, PN, 2, 3, PN, PN, PN),
(0, 1, PN, 2, 3, PN, PN, PN, PN)]
}
mapper = dict(zip(attrs, expected[distributor.nprocs][distributor.myrank]))
obj = distributor._obj_neighborhood
value = obj._arg_defaults()[obj.name]
assert all(getattr(value._obj, k) == v for k, v in mapper.items())
class TestFunction(object):
@pytest.mark.parallel(mode=2)
def test_halo_exchange_bilateral(self):
"""
Test halo exchange between two processes organised in a 2x1 cartesian grid.
On the left, the initial ``data_with_inhalo``; on the right, the situation
after the halo exchange.
rank0 rank0
0 0 0 0 0 0 0 0 0 0 0 0
0 1 1 1 1 0 0 1 1 1 1 0
0 1 1 1 1 0 0 1 1 1 1 0
0 1 1 1 1 0 0 1 1 1 1 0
0 1 1 1 1 0 0 1 1 1 1 0
0 0 0 0 0 0 0 2 2 2 2 0
---->
rank1 rank1
0 0 0 0 0 0 0 1 1 1 1 0
0 2 2 2 2 0 0 2 2 2 2 0
0 2 2 2 2 0 0 2 2 2 2 0
0 2 2 2 2 0 0 2 2 2 2 0
0 2 2 2 2 0 0 2 2 2 2 0
0 0 0 0 0 0 0 0 0 0 0 0
"""
grid = Grid(shape=(12, 12))
x, y = grid.dimensions
f = Function(name='f', grid=grid)
f.data[:] = grid.distributor.myrank + 1
# Now trigger a halo exchange...
f.data_with_halo # noqa
glb_pos_map = grid.distributor.glb_pos_map
if LEFT in glb_pos_map[x]:
assert np.all(f.data_ro_domain[:] == 1.)
assert np.all(f._data_ro_with_inhalo[-1, 1:-1] == 2.)
assert np.all(f._data_ro_with_inhalo[0, :] == 0.)
else:
assert np.all(f.data_ro_domain[:] == 2.)
assert np.all(f._data_ro_with_inhalo[0, 1:-1] == 1.)
assert np.all(f._data_ro_with_inhalo[-1, :] == 0.)
assert np.all(f._data_ro_with_inhalo[:, 0] == 0.)
assert np.all(f._data_ro_with_inhalo[:, -1] == 0.)
@pytest.mark.parallel(mode=2)
def test_halo_exchange_bilateral_asymmetric(self):
"""
Test halo exchange between two processes organised in a 2x1 cartesian grid.
In this test, the size of left and right halo regions have different size.
On the left, the initial ``data_with_inhalo``; on the right, the situation
after the halo exchange.
rank0 rank0
0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 1 1 1 1 0 0 0 1 1 1 1 0 0
0 1 1 1 1 0 0 0 1 1 1 1 0 0
0 1 1 1 1 0 0 0 1 1 1 1 0 0
0 1 1 1 1 0 0 0 1 1 1 1 0 0
0 0 0 0 0 0 0 0 2 2 2 2 0 0
0 0 0 0 0 0 0 0 2 2 2 2 0 0
---->
rank1 rank1
0 0 0 0 0 0 0 0 1 1 1 1 0 0
0 2 2 2 2 0 0 0 2 2 2 2 0 0
0 2 2 2 2 0 0 0 2 2 2 2 0 0
0 2 2 2 2 0 0 0 2 2 2 2 0 0
0 2 2 2 2 0 0 0 2 2 2 2 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0
"""
grid = Grid(shape=(12, 12))
x, y = grid.dimensions
f = Function(name='f', grid=grid, space_order=(1, 1, 2))
f.data[:] = grid.distributor.myrank + 1
# Now trigger a halo exchange...
f.data_with_halo # noqa
glb_pos_map = grid.distributor.glb_pos_map
if LEFT in glb_pos_map[x]:
assert np.all(f.data_ro_domain[:] == 1.)
assert np.all(f._data_ro_with_inhalo[-2:, 1:-2] == 2.)
assert np.all(f._data_ro_with_inhalo[0:1, :] == 0.)
else:
assert np.all(f.data_ro_domain[:] == 2.)
assert np.all(f._data_ro_with_inhalo[:1, 1:-2] == 1.)
assert np.all(f._data_ro_with_inhalo[-2:, :] == 0.)
assert np.all(f._data_ro_with_inhalo[:, :1] == 0.)
assert np.all(f._data_ro_with_inhalo[:, -2:] == 0.)
@pytest.mark.parallel(mode=4)
def test_halo_exchange_quadrilateral(self):
"""
Test halo exchange between four processes organised in a 2x2 cartesian grid.
The initial ``data_with_inhalo`` looks like:
rank0 rank1
0 0 0 0 0 0 0 0 0 0 0 0
0 1 1 1 1 0 0 2 2 2 2 0
0 1 1 1 1 0 0 2 2 2 2 0
0 1 1 1 1 0 0 2 2 2 2 0
0 1 1 1 1 0 0 2 2 2 2 0
0 0 0 0 0 0 0 0 0 0 0 0
rank2 rank3
0 0 0 0 0 0 0 0 0 0 0 0
0 3 3 3 3 0 0 4 4 4 4 0
0 3 3 3 3 0 0 4 4 4 4 0
0 3 3 3 3 0 0 4 4 4 4 0
0 3 3 3 3 0 0 4 4 4 4 0
0 0 0 0 0 0 0 0 0 0 0 0
After the halo exchange, the following is expected and tested for:
rank0 rank1
0 0 0 0 0 0 0 0 0 0 0 0
0 1 1 1 1 2 1 2 2 2 2 0
0 1 1 1 1 2 1 2 2 2 2 0
0 1 1 1 1 2 1 2 2 2 2 0
0 1 1 1 1 2 1 2 2 2 2 0
0 3 3 3 3 4 3 4 4 4 4 0
rank2 rank3
0 1 1 1 1 2 1 2 2 2 2 0
0 3 3 3 3 4 3 4 4 4 4 0
0 3 3 3 3 4 3 4 4 4 4 0
0 3 3 3 3 4 3 4 4 4 4 0
0 3 3 3 3 4 3 4 4 4 4 0
0 0 0 0 0 0 0 0 0 0 0 0
"""
grid = Grid(shape=(12, 12))
x, y = grid.dimensions
f = Function(name='f', grid=grid)
f.data[:] = grid.distributor.myrank + 1
# Now trigger a halo exchange...
f.data_with_halo # noqa
glb_pos_map = grid.distributor.glb_pos_map
if LEFT in glb_pos_map[x] and LEFT in glb_pos_map[y]:
assert np.all(f._data_ro_with_inhalo[0] == 0.)
assert np.all(f._data_ro_with_inhalo[:, 0] == 0.)
assert np.all(f._data_ro_with_inhalo[1:-1, -1] == 2.)
assert np.all(f._data_ro_with_inhalo[-1, 1:-1] == 3.)
assert f._data_ro_with_inhalo[-1, -1] == 4.
elif LEFT in glb_pos_map[x] and RIGHT in glb_pos_map[y]:
assert np.all(f._data_ro_with_inhalo[0] == 0.)
assert np.all(f._data_ro_with_inhalo[:, -1] == 0.)
assert np.all(f._data_ro_with_inhalo[1:-1, 0] == 1.)
assert np.all(f._data_ro_with_inhalo[-1, 1:-1] == 4.)
assert f._data_ro_with_inhalo[-1, 0] == 3.
elif RIGHT in glb_pos_map[x] and LEFT in glb_pos_map[y]:
assert np.all(f._data_ro_with_inhalo[-1] == 0.)
assert np.all(f._data_ro_with_inhalo[:, 0] == 0.)
assert np.all(f._data_ro_with_inhalo[1:-1, -1] == 4.)
assert np.all(f._data_ro_with_inhalo[0, 1:-1] == 1.)
assert f._data_ro_with_inhalo[0, -1] == 2.
else:
assert np.all(f._data_ro_with_inhalo[-1] == 0.)
assert np.all(f._data_ro_with_inhalo[:, -1] == 0.)
assert np.all(f._data_ro_with_inhalo[1:-1, 0] == 3.)
assert np.all(f._data_ro_with_inhalo[0, 1:-1] == 2.)
assert f._data_ro_with_inhalo[0, 0] == 1.
@pytest.mark.parallel(mode=4)
@pytest.mark.parametrize('shape,expected', [
((15, 15), [((0, 8), (0, 8)), ((0, 8), (8, 15)),
((8, 15), (0, 8)), ((8, 15), (8, 15))]),
])
def test_local_indices(self, shape, expected):
grid = Grid(shape=shape)
f = Function(name='f', grid=grid)
assert all(i == slice(*j)
for i, j in zip(f.local_indices, expected[grid.distributor.myrank]))
class TestSparseFunction(object):
@pytest.mark.parallel(mode=4)
@pytest.mark.parametrize('coords', [
((1., 1.), (1., 3.), (3., 1.), (3., 3.)),
])
def test_ownership(self, coords):
"""Given a sparse point ``p`` with known coordinates, this test checks
that the MPI rank owning ``p`` is retrieved correctly."""
grid = Grid(shape=(4, 4), extent=(4.0, 4.0))
sf = SparseFunction(name='sf', grid=grid, npoint=4, coordinates=coords)
# The domain decomposition is so that the i-th MPI rank gets exactly one
# sparse point `p` and, incidentally, `p` is logically owned by `i`
assert len(sf.gridpoints) == 1
assert all(grid.distributor.glb_to_rank(i) == grid.distributor.myrank
for i in sf.gridpoints)
@pytest.mark.parallel(mode=4)
@pytest.mark.parametrize('coords,expected', [
([(0.5, 0.5), (1.5, 2.5), (1.5, 1.5), (2.5, 1.5)], [[0.], [1.], [2.], [3.]]),
])
def test_local_indices(self, coords, expected):
grid = Grid(shape=(4, 4), extent=(3.0, 3.0))
data = np.array([0., 1., 2., 3.])
coords = np.array(coords)
sf = SparseFunction(name='sf', grid=grid, npoint=len(coords))
# Each of the 4 MPI ranks get one (randomly chosen) sparse point
assert sf.npoint == 1
sf.coordinates.data[:] = coords
sf.data[:] = data
expected = np.array(expected[grid.distributor.myrank])
assert np.all(sf.data == expected)
@pytest.mark.parallel(mode=4)
def test_scatter_gather(self):
"""
Test scattering and gathering of sparse data from and to a single MPI rank.
The initial data distribution (A, B, C, and D are generic values) looks like:
rank0 rank1 rank2 rank3
[D] [C] [B] [A]
Logically (i.e., given point coordinates and domain decomposition), A belongs
to rank0, B belongs to rank1, etc. Thus, after scattering, the data distribution
is expected to be:
rank0 rank1 rank2 rank3
[A] [B] [C] [D]
Then, locally on each rank, a trivial *2 multiplication is performed:
rank0 rank1 rank2 rank3
[A*2] [B*2] [C*2] [D*2]
Finally, we gather the data values and we get:
rank0 rank1 rank2 rank3
[D*2] [C*2] [B*2] [A*2]
"""
grid = Grid(shape=(4, 4), extent=(4.0, 4.0))
# Initialization
data = np.array([3, 2, 1, 0])
coords = np.array([(3., 3.), (3., 1.), (1., 3.), (1., 1.)])
sf = SparseFunction(name='sf', grid=grid, npoint=len(coords), coordinates=coords)
sf.data[:] = data
# Scatter
loc_data = sf._dist_scatter()[sf]
loc_coords = sf._dist_scatter()[sf.coordinates]
assert len(loc_data) == 1
assert loc_data[0] == grid.distributor.myrank
# Do some local computation
loc_data = loc_data*2
# Gather
sf._dist_gather(loc_data, loc_coords)
assert len(sf.data) == 1
assert np.all(sf.data == data[sf.local_indices]*2)
@pytest.mark.parallel(mode=4)
def test_sparse_coords(self):
grid = Grid(shape=(21, 31, 21), extent=(20, 30, 20))
x, y, z = grid.dimensions
coords = np.zeros((21*31, 3))
coords[:, 0] = np.asarray([i for i in range(21) for j in range(31)])
coords[:, 1] = np.asarray([j for i in range(21) for j in range(31)])
sf = SparseFunction(name="s", grid=grid, coordinates=coords, npoint=21*31)
u = Function(name="u", grid=grid, space_order=1)
u.data[:, :, 0] = np.reshape(np.asarray([i+j for i in range(21)
for j in range(31)]), (21, 31))
op = Operator(sf.interpolate(u))
op.apply()
for i in range(21*31):
coords_loc = sf.coordinates.data[i, 1]
if coords_loc is not None:
coords_loc += sf.coordinates.data[i, 0]
assert sf.data[i] == coords_loc
class TestOperatorSimple(object):
@pytest.mark.parallel(mode=[2, 4, 8])
def test_trivial_eq_1d(self):
grid = Grid(shape=(32,))
x = grid.dimensions[0]
t = grid.stepping_dim
f = TimeFunction(name='f', grid=grid)
f.data_with_halo[:] = 1.
op = Operator(Eq(f.forward, f[t, x-1] + f[t, x+1] + 1))
op.apply(time=1)
assert np.all(f.data_ro_domain[1] == 3.)
if f.grid.distributor.myrank == 0:
assert f.data_ro_domain[0, 0] == 5.
assert np.all(f.data_ro_domain[0, 1:] == 7.)
elif f.grid.distributor.myrank == f.grid.distributor.nprocs - 1:
assert f.data_ro_domain[0, -1] == 5.
assert np.all(f.data_ro_domain[0, :-1] == 7.)
else:
assert np.all(f.data_ro_domain[0] == 7.)
@pytest.mark.parallel(mode=[2])
def test_trivial_eq_1d_asymmetric(self):
grid = Grid(shape=(32,))
x = grid.dimensions[0]
t = grid.stepping_dim
f = TimeFunction(name='f', grid=grid)
f.data_with_halo[:] = 1.
op = Operator(Eq(f.forward, f[t, x+1] + 1))
op.apply(time=1)
assert np.all(f.data_ro_domain[1] == 2.)
if f.grid.distributor.myrank == 0:
assert np.all(f.data_ro_domain[0] == 3.)
else:
assert np.all(f.data_ro_domain[0, :-1] == 3.)
assert f.data_ro_domain[0, -1] == 2.
@pytest.mark.parallel(mode=2)
def test_trivial_eq_1d_save(self):
grid = Grid(shape=(32,))
x = grid.dimensions[0]
time = grid.time_dim
f = TimeFunction(name='f', grid=grid, save=5)
f.data_with_halo[:] = 1.
op = Operator(Eq(f.forward, f[time, x-1] + f[time, x+1] + 1))
op.apply()
time_M = op._prepare_arguments()['time_M']
assert np.all(f.data_ro_domain[1] == 3.)
glb_pos_map = f.grid.distributor.glb_pos_map
if LEFT in glb_pos_map[x]:
assert np.all(f.data_ro_domain[-1, time_M:] == 31.)
else:
assert np.all(f.data_ro_domain[-1, :-time_M] == 31.)
@pytest.mark.parallel(mode=[(4, 'basic'), (4, 'diag'), (4, 'overlap'),
(4, 'overlap2'), (4, 'full')])
def test_trivial_eq_2d(self):
grid = Grid(shape=(8, 8,))
x, y = grid.dimensions
t = grid.stepping_dim
f = TimeFunction(name='f', grid=grid, space_order=1)
f.data_with_halo[:] = 1.
eqn = Eq(f.forward, f[t, x-1, y] + f[t, x+1, y] + f[t, x, y-1] + f[t, x, y+1])
op = Operator(eqn)
op.apply(time=1)
# Expected computed values
corner, side, interior = 10., 13., 16.
glb_pos_map = f.grid.distributor.glb_pos_map
assert np.all(f.data_ro_domain[0, 1:-1, 1:-1] == interior)
if LEFT in glb_pos_map[x] and LEFT in glb_pos_map[y]:
assert f.data_ro_domain[0, 0, 0] == corner
assert np.all(f.data_ro_domain[0, 1:, :1] == side)
assert np.all(f.data_ro_domain[0, :1, 1:] == side)
elif LEFT in glb_pos_map[x] and RIGHT in glb_pos_map[y]:
assert f.data_ro_domain[0, 0, -1] == corner
assert np.all(f.data_ro_domain[0, :1, :-1] == side)
assert np.all(f.data_ro_domain[0, 1:, -1:] == side)
elif RIGHT in glb_pos_map[x] and LEFT in glb_pos_map[y]:
assert f.data_ro_domain[0, -1, 0] == corner
assert np.all(f.data_ro_domain[0, -1:, 1:] == side)
assert np.all(f.data_ro_domain[0, :-1, :1] == side)
else:
assert f.data_ro_domain[0, -1, -1] == corner
assert np.all(f.data_ro_domain[0, :-1, -1:] == side)
assert np.all(f.data_ro_domain[0, -1:, :-1] == side)
@pytest.mark.parallel(mode=[(8, 'basic'), (8, 'diag'), (8, 'overlap'),
(8, 'overlap2'), (8, 'full')])
def test_trivial_eq_3d(self):
grid = Grid(shape=(8, 8, 8))
x, y, z = grid.dimensions
t = grid.stepping_dim
f = TimeFunction(name='f', grid=grid, space_order=1)
f.data_with_halo[:] = 1.
eqn = Eq(f.forward, (f[t, x-1, y, z] + f[t, x+1, y, z] +
f[t, x, y-1, z] + f[t, x, y+1, z] +
f[t, x, y, z-1] + f[t, x, y, z+1]))
op = Operator(eqn)
op.apply(time=1)
# Expected computed values
corner, side, face, interior = 21., 26., 31., 36.
# Situation at t=0
assert np.all(f.data_ro_domain[1] == 6.)
# Situation at t=1
# 1) corners
for i in [[0, 0, 0], [0, 0, -1], [0, -1, 0], [0, -1, -1],
[-1, 0, 0], [-1, 0, -1], [-1, -1, 0], [-1, -1, -1]]:
assert f.data_ro_domain[[0] + i] in [corner, None]
# 2) sides
for i in [[0, 0, slice(1, -1)], [0, -1, slice(1, -1)],
[0, slice(1, -1), 0], [0, slice(1, -1), -1],
[-1, 0, slice(1, -1)], [-1, -1, slice(1, -1)],
[-1, slice(1, -1), 0], [-1, slice(1, -1), -1],
[slice(1, -1), 0, 0], [slice(1, -1), 0, -1],
[slice(1, -1), -1, 0], [slice(1, -1), -1, -1]]:
assert np.all(f.data_ro_domain[[0] + i] == side)
# 3) faces
for i in [[0, slice(1, -1), slice(1, -1)], [-1, slice(1, -1), slice(1, -1)],
[slice(1, -1), 0, slice(1, -1)], [slice(1, -1), -1, slice(1, -1)],
[slice(1, -1), slice(1, -1), 0], [slice(1, -1), slice(1, -1), -1]]:
assert np.all(f.data_ro_domain[[0] + i] == face)
# 4) interior
assert np.all(f.data_ro_domain[0, 1:-1, 1:-1, 1:-1] == interior)
@pytest.mark.parallel(mode=[(4, 'basic'), (4, 'diag')])
def test_multiple_eqs_funcs(self):
grid = Grid(shape=(12,))
x = grid.dimensions[0]
t = grid.stepping_dim
f = TimeFunction(name='f', grid=grid)
f.data_with_halo[:] = 0.
g = TimeFunction(name='g', grid=grid)
g.data_with_halo[:] = 0.
op = Operator([Eq(f.forward, f[t, x+1] + g[t, x-1] + 1),
Eq(g.forward, f[t, x-1] + g[t, x+1] + 1)])
op.apply(time=1)
assert np.all(f.data_ro_domain[1] == 1.)
if f.grid.distributor.myrank == 0:
assert f.data_ro_domain[0, 0] == 2.
assert np.all(f.data_ro_domain[0, 1:] == 3.)
elif f.grid.distributor.myrank == f.grid.distributor.nprocs - 1:
assert f.data_ro_domain[0, -1] == 2.
assert np.all(f.data_ro_domain[0, :-1] == 3.)
else:
assert np.all(f.data_ro_domain[0] == 3.)
# Also check that there are no redundant halo exchanges. Here, only
# two are expected before the `x` Iteration, one for `f` and one for `g`
calls = FindNodes(Call).visit(op)
assert len(calls) == 2
@pytest.mark.parallel(mode=2)
def test_reapply_with_different_functions(self):
grid1 = Grid(shape=(30, 30, 30))
f1 = Function(name='f', grid=grid1, space_order=4)
op = Operator(Eq(f1, 1.))
op.apply()
grid2 = Grid(shape=(40, 40, 40))
f2 = Function(name='f', grid=grid2, space_order=4)
# Re-application
op.apply(f=f2)
assert np.all(f1.data == 1.)
assert np.all(f2.data == 1.)
class TestCodeGeneration(object):
@pytest.mark.parallel(mode=1)
def test_avoid_haloupdate_as_nostencil_basic(self):
grid = Grid(shape=(12,))
f = TimeFunction(name='f', grid=grid)
g = Function(name='g', grid=grid)
op = Operator([Eq(f.forward, f + 1.),
Eq(g, f + 1.)])
calls = FindNodes(Call).visit(op)
assert len(calls) == 0
@pytest.mark.parallel(mode=1)
def test_avoid_haloupdate_as_nostencil_advanced(self):
grid = Grid(shape=(4, 4))
u = TimeFunction(name='u', grid=grid, space_order=4, time_order=2, save=None)
v = TimeFunction(name='v', grid=grid, space_order=0, time_order=0, save=5)
g = Function(name='g', grid=grid, space_order=0)
i = Function(name='i', grid=grid, space_order=0)
shift = Constant(name='shift', dtype=np.int32)
step = Eq(u.forward, u - u.backward + 1)
g_inc = Inc(g, u * v.subs(grid.time_dim, grid.time_dim - shift))
i_inc = Inc(i, (v*v).subs(grid.time_dim, grid.time_dim - shift))
op = Operator([step, g_inc, i_inc])
# No stencil in the expressions, so no halo update required!
calls = FindNodes(Call).visit(op)
assert len(calls) == 0
@pytest.mark.parallel(mode=1)
def test_avoid_redundant_haloupdate(self):
grid = Grid(shape=(12,))
x = grid.dimensions[0]
t = grid.stepping_dim
i = Dimension(name='i')
j = Dimension(name='j')
f = TimeFunction(name='f', grid=grid)
g = Function(name='g', grid=grid)
op = Operator([Eq(f.forward, f[t, x-1] + f[t, x+1] + 1.),
Inc(f[t+1, i], 1.), # no halo update as it's an Inc
Eq(g, f[t, j] + 1)]) # access `f` at `t`, not `t+1`!
calls = FindNodes(Call).visit(op)
assert len(calls) == 1
@pytest.mark.parallel(mode=1)
def test_avoid_haloupdate_if_distr_but_sequential(self):
grid = Grid(shape=(12,))
x = grid.dimensions[0]
t = grid.stepping_dim
f = TimeFunction(name='f', grid=grid)
# There is an anti-dependence between the first and second Eqs, so
# the compiler places them in different x-loops. However, none of the
# two loops should be preceded by a halo exchange, though for different
# reasons:
# * the equation in the first loop has no stencil
# * the equation in the second loop is inherently sequential, so the
# compiler should be sufficiently smart to see that there is no point
# in adding a halo exchange
op = Operator([Eq(f, f + 1),
Eq(f, f[t, x-1] + f[t, x+1] + 1.)])
iterations = FindNodes(Iteration).visit(op)
assert len(iterations) == 3
calls = FindNodes(Call).visit(op)
assert len(calls) == 0
@pytest.mark.parallel(mode=1)
def test_avoid_haloupdate_with_subdims(self):
grid = Grid(shape=(4,))
x = grid.dimensions[0]
t = grid.stepping_dim
thickness = 4
u = TimeFunction(name='u', grid=grid, time_order=1)
xleft = SubDimension.left(name='xleft', parent=x, thickness=thickness)
xi = SubDimension.middle(name='xi', parent=x,
thickness_left=thickness, thickness_right=thickness)
eq_centre = Eq(u[t+1, xi], u[t, xi-1] + u[t, xi+1] + 1.)
eq_left = Eq(u[t+1, xleft], u[t+1, xleft+1] + u[t, xleft+1] + 1.)
# There is only one halo update -- for the `eq_centre` expression.
# `eq_left` gets no halo update since it's a left-SubDimension, which by
# default (i.e., unless one passes `local=False` to SubDimension.left) is
# assumed to be a local Dimension.
op = Operator([eq_centre, eq_left])
calls = FindNodes(Call).visit(op)
assert len(calls) == 1
@pytest.mark.parallel(mode=1)
def test_avoid_haloupdate_with_constant_index(self):
grid = Grid(shape=(4,))
x = grid.dimensions[0]
t = grid.stepping_dim
u = TimeFunction(name='u', grid=grid)
eq = Eq(u.forward, u[t, 1] + u[t, 1 + x.symbolic_min] + u[t, x])
op = Operator(eq)
calls = FindNodes(Call).visit(op)
assert len(calls) == 0
@pytest.mark.parallel(mode=1)
def test_hoist_haloupdate_if_no_flowdep(self):
grid = Grid(shape=(12,))
x = grid.dimensions[0]
t = grid.stepping_dim
i = Dimension(name='i')
f = TimeFunction(name='f', grid=grid)
g = Function(name='g', grid=grid)
h = Function(name='h', grid=grid)
op = Operator([Eq(f.forward, f[t, x-1] + f[t, x+1] + 1.),
Inc(g[i], f[t, h[i]] + 1.)])
calls = FindNodes(Call).visit(op)
assert len(calls) == 1
# Below, there is a flow-dependence along `x`, so a further halo update
# before the Inc is required
op = Operator([Eq(f.forward, f[t, x-1] + f[t, x+1] + 1.),
Inc(g[i], f[t+1, h[i]] + 1.)])
calls = FindNodes(Call).visit(op)
assert len(calls) == 2
@pytest.mark.parallel(mode=[(2, 'basic'), (2, 'diag')])
def test_redo_haloupdate_due_to_antidep(self):
grid = Grid(shape=(12,))
x = grid.dimensions[0]
t = grid.stepping_dim
f = TimeFunction(name='f', grid=grid)
g = TimeFunction(name='g', grid=grid)
op = Operator([Eq(f.forward, f[t, x-1] + f[t, x+1] + 1.),
Eq(g.forward, f[t+1, x-1] + f[t+1, x+1] + g)])
op.apply(time=0)
calls = FindNodes(Call).visit(op)
assert len(calls) == 2
assert np.all(f.data_ro_domain[1] == 1.)
glb_pos_map = f.grid.distributor.glb_pos_map
if LEFT in glb_pos_map[x]:
assert np.all(g.data_ro_domain[1, 1:] == 2.)
else:
assert np.all(g.data_ro_domain[1, :-1] == 2.)
@pytest.mark.parametrize('expr,expected', [
('f[t,x-1,y] + f[t,x+1,y]', {'rc', 'lc'}),
('f[t,x,y-1] + f[t,x,y+1]', {'cr', 'cl'}),
('f[t,x-1,y-1] + f[t,x,y+1]', {'cr', 'rr', 'rc', 'cl'}),
('f[t,x-1,y-1] + f[t,x+1,y+1]', {'cr', 'rr', 'rc', 'cl', 'll', 'lc'}),
])
@pytest.mark.parallel(mode=[(1, 'diag')])
def test_diag_comm_scheme(self, expr, expected):
"""
Check that the 'diag' mode does not generate more communications
than strictly necessary.
"""
grid = Grid(shape=(4, 4))
x, y = grid.dimensions # noqa
t = grid.stepping_dim # noqa
f = TimeFunction(name='f', grid=grid) # noqa
op = Operator(Eq(f.forward, eval(expr)), dle=('advanced', {'openmp': False}))
calls = FindNodes(Call).visit(op._func_table['haloupdate0'])
destinations = {i.arguments[-2].field for i in calls}
assert destinations == expected
@pytest.mark.parallel(mode=[(1, 'full')])
def test_poke_progress(self):
grid = Grid(shape=(4, 4))
x, y = grid.dimensions
t = grid.stepping_dim
f = TimeFunction(name='f', grid=grid)
eqn = Eq(f.forward, f[t, x-1, y] + f[t, x+1, y] + f[t, x, y-1] + f[t, x, y+1])
op = Operator(eqn)
trees = retrieve_iteration_tree(op._func_table['compute0'].root)
assert len(trees) == 2
tree = trees[0]
# Make sure `pokempi0` is the last node within the outer Iteration
assert len(tree) == 2
assert len(tree.root.nodes) == 2
call = tree.root.nodes[1]
assert call.name == 'pokempi0'
assert call.arguments[0].name == 'msg0'
if configuration['openmp']:
# W/ OpenMP, we prod until all comms have completed
assert call.then_body[0].body[0].is_While
# W/ OpenMP, we expect dynamic thread scheduling
assert 'dynamic,1' in tree.root.pragmas[0].value
else:
# W/o OpenMP, it's a different story
assert call._single_thread
# Now we do as before, but enforcing loop blocking (by default off,
# as heuristically it is not enabled when the Iteration nest has depth < 3)
op = Operator(eqn, dle=('advanced', {'blockinner': True}))
trees = retrieve_iteration_tree(op._func_table['bf0'].root)
assert len(trees) == 2
tree = trees[1]
# Make sure `pokempi0` is the last node within the inner Iteration over blocks
assert len(tree) == 2
assert len(tree.root.nodes[0].nodes) == 2
call = tree.root.nodes[0].nodes[1]
assert call.name == 'pokempi0'
assert call.arguments[0].name == 'msg0'
if configuration['openmp']:
# W/ OpenMP, we prod until all comms have completed
assert call.then_body[0].body[0].is_While
# W/ OpenMP, we expect dynamic thread scheduling
assert 'dynamic,1' in tree.root.pragmas[0].value
else:
# W/o OpenMP, it's a different story
assert call._single_thread
class TestOperatorAdvanced(object):
@pytest.mark.parallel(mode=4)
def test_injection_wodup(self):
"""
Test injection operator when the sparse points don't need to be replicated
("wodup" -> w/o duplication) over multiple MPI ranks.
"""
grid = Grid(shape=(4, 4), extent=(3.0, 3.0))
f = Function(name='f', grid=grid, space_order=0)
f.data[:] = 0.
coords = np.array([(0.5, 0.5), (0.5, 2.5), (2.5, 0.5), (2.5, 2.5)])
sf = SparseFunction(name='sf', grid=grid, npoint=len(coords), coordinates=coords)
sf.data[:] = 4.
# This is the situation at this point
# O is a grid point
# * is a sparse point
#
# O --- O --- O --- O
# | * | | * |
# O --- O --- O --- O
# | | | |
# O --- O --- O --- O
# | * | | * |
# O --- O --- O --- O
op = Operator(sf.inject(field=f, expr=sf + 1))
op.apply()
assert np.all(f.data == 1.25)
@pytest.mark.parallel(mode=4)
def test_injection_wodup_wtime(self):
"""
Just like ``test_injection_wodup``, but using a SparseTimeFunction
instead of a SparseFunction. Hence, the data scattering/gathering now
has to correctly pack/unpack multidimensional arrays.
"""
grid = Grid(shape=(4, 4), extent=(3.0, 3.0))
save = 3
f = TimeFunction(name='f', grid=grid, save=save, space_order=0)
f.data[:] = 0.
coords = np.array([(0.5, 0.5), (0.5, 2.5), (2.5, 0.5), (2.5, 2.5)])
sf = SparseTimeFunction(name='sf', grid=grid, nt=save,
npoint=len(coords), coordinates=coords)
sf.data[0, :] = 4.
sf.data[1, :] = 8.
sf.data[2, :] = 12.
op = Operator(sf.inject(field=f, expr=sf + 1))
op.apply()
assert np.all(f.data[0] == 1.25)
assert np.all(f.data[1] == 2.25)
assert np.all(f.data[2] == 3.25)
@pytest.mark.parallel(mode=4)
def test_injection_dup(self):
"""
Test injection operator when the sparse points are replicated over
multiple MPI ranks.
"""
grid = Grid(shape=(4, 4), extent=(3.0, 3.0))
x, y = grid.dimensions
f = Function(name='f', grid=grid)
f.data[:] = 0.
coords = [(0.5, 0.5), (1.5, 2.5), (1.5, 1.5), (2.5, 1.5)]
sf = SparseFunction(name='sf', grid=grid, npoint=len(coords), coordinates=coords)
sf.data[:] = 4.
# Global view (left) and local view (right, after domain decomposition)
# O is a grid point
# x is a halo point
# A, B, C, D are sparse points
# Rank0 Rank1
# O --- O --- O --- O O --- O --- x x --- O --- O
# | A | | | | A | | | | |
# O --- O --- O --- O O --- O --- x x --- O --- O
# | | C | B | --> | | C | | C | B |
# O --- O --- O --- O x --- x --- x x --- x --- x
# | | D | | Rank2 Rank3
# O --- O --- O --- O x --- x --- x x --- x --- x
# | | C | | C | B |
# O --- O --- x x --- O --- O
# | | D | | D | |
# O --- O --- x x --- O --- O
#
# Expected `f.data` (global view)
#
# 1.25 --- 1.25 --- 0.00 --- 0.00
# | | | |
# 1.25 --- 2.50 --- 2.50 --- 1.25
# | | | |
# 0.00 --- 2.50 --- 3.75 --- 1.25
# | | | |
# 0.00 --- 1.25 --- 1.25 --- 0.00
op = Operator(sf.inject(field=f, expr=sf + 1))
op.apply()
glb_pos_map = grid.distributor.glb_pos_map
if LEFT in glb_pos_map[x] and LEFT in glb_pos_map[y]: # rank0
assert np.all(f.data_ro_domain == [[1.25, 1.25], [1.25, 2.5]])
elif LEFT in glb_pos_map[x] and RIGHT in glb_pos_map[y]: # rank1
assert np.all(f.data_ro_domain == [[0., 0.], [2.5, 1.25]])
elif RIGHT in glb_pos_map[x] and LEFT in glb_pos_map[y]:
assert np.all(f.data_ro_domain == [[0., 2.5], [0., 1.25]])
elif RIGHT in glb_pos_map[x] and RIGHT in glb_pos_map[y]:
assert np.all(f.data_ro_domain == [[3.75, 1.25], [1.25, 0.]])
@pytest.mark.parallel(mode=4)
def test_interpolation_wodup(self):
grid = Grid(shape=(4, 4), extent=(3.0, 3.0))
f = Function(name='f', grid=grid, space_order=0)
f.data[:] = 4.
coords = [(0.5, 0.5), (0.5, 2.5), (2.5, 0.5), (2.5, 2.5)]
sf = SparseFunction(name='sf', grid=grid, npoint=len(coords), coordinates=coords)
sf.data[:] = 0.
# This is the situation at this point
# O is a grid point
# * is a sparse point
#
# O --- O --- O --- O
# | * | | * |
# O --- O --- O --- O
# | | | |
# O --- O --- O --- O
# | * | | * |
# O --- O --- O --- O
op = Operator(sf.interpolate(expr=f))
op.apply()