-
Notifications
You must be signed in to change notification settings - Fork 221
/
test_dimension.py
897 lines (711 loc) · 33.1 KB
/
test_dimension.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
from itertools import product
import numpy as np
from sympy import And
import pytest
from conftest import skipif
from devito import (ConditionalDimension, Grid, Function, TimeFunction, SparseFunction, # noqa
Eq, Operator, Constant, Dimension, SubDimension, switchconfig)
from devito.ir.iet import Expression, Iteration, FindNodes, retrieve_iteration_tree
from devito.types import Array
@skipif('ops')
class TestSubDimension(object):
def test_interior(self):
"""
Tests application of an Operator consisting of a single equation
over the ``interior`` subdomain.
"""
grid = Grid(shape=(4, 4, 4))
x, y, z = grid.dimensions
interior = grid.interior
u = TimeFunction(name='u', grid=grid)
eqn = [Eq(u.forward, u + 2, subdomain=interior)]
op = Operator(eqn)
op.apply(time_M=2)
assert np.all(u.data[1, 1:-1, 1:-1, 1:-1] == 6.)
assert np.all(u.data[1, :, 0] == 0.)
assert np.all(u.data[1, :, -1] == 0.)
assert np.all(u.data[1, :, :, 0] == 0.)
assert np.all(u.data[1, :, :, -1] == 0.)
@skipif('yask')
def test_domain_vs_interior(self):
"""
Tests application of an Operator consisting of two equations, one
over the whole domain (default), and one over the ``interior`` subdomain.
"""
grid = Grid(shape=(4, 4, 4))
x, y, z = grid.dimensions
t = grid.stepping_dim # noqa
interior = grid.interior
u = TimeFunction(name='u', grid=grid) # noqa
eqs = [Eq(u.forward, u + 1),
Eq(u.forward, u.forward + 2, subdomain=interior)]
op = Operator(eqs, dse='noop', dle='noop')
trees = retrieve_iteration_tree(op)
assert len(trees) == 2
op.apply(time_M=1)
assert np.all(u.data[1, 0, :, :] == 1)
assert np.all(u.data[1, -1, :, :] == 1)
assert np.all(u.data[1, :, 0, :] == 1)
assert np.all(u.data[1, :, -1, :] == 1)
assert np.all(u.data[1, :, :, 0] == 1)
assert np.all(u.data[1, :, :, -1] == 1)
assert np.all(u.data[1, 1:3, 1:3, 1:3] == 3)
def test_subdim_middle(self):
"""
Tests that instantiating SubDimensions using the classmethod
constructors works correctly.
"""
grid = Grid(shape=(4, 4, 4))
x, y, z = grid.dimensions
t = grid.stepping_dim # noqa
u = TimeFunction(name='u', grid=grid) # noqa
xi = SubDimension.middle(name='xi', parent=x,
thickness_left=1,
thickness_right=1)
eqs = [Eq(u.forward, u + 1)]
eqs = [e.subs(x, xi) for e in eqs]
op = Operator(eqs)
u.data[:] = 1.0
op.apply(time_M=1)
assert np.all(u.data[1, 0, :, :] == 1)
assert np.all(u.data[1, -1, :, :] == 1)
assert np.all(u.data[1, 1:3, :, :] == 2)
@skipif('yask')
def test_symbolic_size(self):
"""Check the symbolic size of all possible SubDimensions is as expected."""
grid = Grid(shape=(4,))
x, = grid.dimensions
thickness = 4
xleft = SubDimension.left(name='xleft', parent=x, thickness=thickness)
assert xleft.symbolic_size == xleft.thickness.left[0]
xi = SubDimension.middle(name='xi', parent=x,
thickness_left=thickness, thickness_right=thickness)
assert xi.symbolic_size == (x.symbolic_max - x.symbolic_min -
xi.thickness.left[0] - xi.thickness.right[0] + 1)
xright = SubDimension.right(name='xright', parent=x, thickness=thickness)
assert xright.symbolic_size == xright.thickness.right[0]
def test_bcs(self):
"""
Tests application of an Operator consisting of multiple equations
defined over different sub-regions, explicitly created through the
use of SubDimensions.
"""
grid = Grid(shape=(20, 20))
x, y = grid.dimensions
t = grid.stepping_dim
thickness = 4
u = TimeFunction(name='u', save=None, grid=grid, space_order=0, 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)
xright = SubDimension.right(name='xright', parent=x, thickness=thickness)
yi = SubDimension.middle(name='yi', parent=y,
thickness_left=thickness, thickness_right=thickness)
t_in_centre = Eq(u[t+1, xi, yi], 1)
leftbc = Eq(u[t+1, xleft, yi], u[t+1, xleft+1, yi] + 1)
rightbc = Eq(u[t+1, xright, yi], u[t+1, xright-1, yi] + 1)
op = Operator([t_in_centre, leftbc, rightbc])
op.apply(time_m=1, time_M=1)
assert np.all(u.data[0, :, 0:thickness] == 0.)
assert np.all(u.data[0, :, -thickness:] == 0.)
assert all(np.all(u.data[0, i, thickness:-thickness] == (thickness+1-i))
for i in range(thickness))
assert all(np.all(u.data[0, -i, thickness:-thickness] == (thickness+2-i))
for i in range(1, thickness + 1))
assert np.all(u.data[0, thickness:-thickness, thickness:-thickness] == 1.)
@skipif('yask')
def test_flow_detection_interior(self):
"""
Test detection of flow directions when SubDimensions are used
(in this test they are induced by the ``interior`` subdomain).
Stencil uses values at new timestep as well as those at previous ones
This forces an evaluation order onto x.
Weights are:
x=0 x=1 x=2 x=3
t=N 2 ---3
v /
t=N+1 o--+----4
Flow dependency should traverse x in the negative direction
x=2 x=3 x=4 x=5 x=6
t=0 0 --- 0 -- 1 -- 0
v / v / v /
t=1 44 -+--- 11 -+--- 2--+ -- 0
"""
grid = Grid(shape=(10, 10))
x, y = grid.dimensions
interior = grid.interior
u = TimeFunction(name='u', grid=grid, save=10, time_order=1, space_order=0)
step = Eq(u.forward, 2*u
+ 3*u.subs(x, x+x.spacing)
+ 4*u.forward.subs(x, x+x.spacing),
subdomain=interior)
op = Operator(step)
u.data[0, 5, 5] = 1.0
op.apply(time_M=0)
assert u.data[1, 5, 5] == 2
assert u.data[1, 4, 5] == 11
assert u.data[1, 3, 5] == 44
assert u.data[1, 2, 5] == 4*44
assert u.data[1, 1, 5] == 4*4*44
# This point isn't updated because of the `interior` selection
assert u.data[1, 0, 5] == 0
assert np.all(u.data[1, 6:, :] == 0)
assert np.all(u.data[1, :, 0:5] == 0)
assert np.all(u.data[1, :, 6:] == 0)
@skipif('yask')
@pytest.mark.parametrize('exprs,expected,', [
# Carried dependence in both /t/ and /x/
(['Eq(u[t+1, x, y], u[t+1, x-1, y] + u[t, x, y])'], 'y'),
(['Eq(u[t+1, x, y], u[t+1, x-1, y] + u[t, x, y], subdomain=interior)'], 'yi'),
# Carried dependence in both /t/ and /y/
(['Eq(u[t+1, x, y], u[t+1, x, y-1] + u[t, x, y])'], 'x'),
(['Eq(u[t+1, x, y], u[t+1, x, y-1] + u[t, x, y], subdomain=interior)'], 'xi'),
# Carried dependence in /y/, leading to separate /y/ loops, one
# going forward, the other backward
(['Eq(u[t+1, x, y], u[t+1, x, y-1] + u[t, x, y], subdomain=interior)',
'Eq(u[t+1, x, y], u[t+1, x, y+1] + u[t, x, y], subdomain=interior)'], 'xi'),
])
def test_iteration_property_parallel(self, exprs, expected):
"""Tests detection of sequental and parallel Iterations when applying
equations over different subdomains."""
grid = Grid(shape=(20, 20))
x, y = grid.dimensions # noqa
t = grid.time_dim # noqa
interior = grid.interior # noqa
u = TimeFunction(name='u', grid=grid, save=10, time_order=1) # noqa
# List comprehension would need explicit locals/globals mappings to eval
for i, e in enumerate(list(exprs)):
exprs[i] = eval(e)
op = Operator(exprs, dle='noop')
iterations = FindNodes(Iteration).visit(op)
assert all(i.is_Sequential for i in iterations if i.dim.name != expected)
assert all(i.is_Parallel for i in iterations if i.dim.name == expected)
@skipif('yask')
@pytest.mark.parametrize('exprs,expected,', [
# All parallel, the innermost Iteration gets vectorized
(['Eq(u[time, x, yleft], u[time, x, yleft] + 1.)'], ['yleft']),
# All outers are parallel, carried dependence in `yleft`, so the middle
# Iteration over `x` gets vectorized
(['Eq(u[time, x, yleft], u[time, x, yleft+1] + 1.)'], ['x']),
# Only the middle Iteration is parallel, so no vectorization (the Iteration
# is left non-vectorised for OpenMP parallelism)
(['Eq(u[time+1, x, yleft], u[time, x, yleft+1] + u[time+1, x, yleft+1])'], [])
])
def test_iteration_property_vector(self, exprs, expected):
"""Tests detection of vector Iterations when using subdimensions."""
grid = Grid(shape=(20, 20))
x, y = grid.dimensions # noqa
time = grid.time_dim # noqa
# The leftmost 10 elements
yleft = SubDimension.left(name='yleft', parent=y, thickness=10) # noqa
u = TimeFunction(name='u', grid=grid, save=10, time_order=0, space_order=1) # noqa
# List comprehension would need explicit locals/globals mappings to eval
for i, e in enumerate(list(exprs)):
exprs[i] = eval(e)
op = Operator(exprs, dle='simd')
iterations = FindNodes(Iteration).visit(op)
vectorized = [i.dim.name for i in iterations if i.is_Vectorized]
assert set(vectorized) == set(expected)
def test_subdimmiddle_parallel(self):
"""
Tests application of an Operator consisting of a subdimension
defined over different sub-regions, explicitly created through the
use of SubDimensions.
"""
grid = Grid(shape=(20, 20))
x, y = grid.dimensions
t = grid.stepping_dim
thickness = 4
u = TimeFunction(name='u', save=None, grid=grid, space_order=0, time_order=1)
xi = SubDimension.middle(name='xi', parent=x,
thickness_left=thickness, thickness_right=thickness)
yi = SubDimension.middle(name='yi', parent=y,
thickness_left=thickness, thickness_right=thickness)
# a 5 point stencil that can be computed in parallel
centre = Eq(u[t+1, xi, yi], u[t, xi, yi] + u[t, xi-1, yi]
+ u[t, xi+1, yi] + u[t, xi, yi-1] + u[t, xi, yi+1])
u.data[0, 10, 10] = 1.0
op = Operator([centre])
iterations = FindNodes(Iteration).visit(op)
assert all(i.is_Affine and i.is_Parallel for i in iterations if i.dim in [xi, yi])
op.apply(time_m=0, time_M=0)
assert np.all(u.data[1, 9:12, 10] == 1.0)
assert np.all(u.data[1, 10, 9:12] == 1.0)
# Other than those, it should all be 0
u.data[1, 9:12, 10] = 0.0
u.data[1, 10, 9:12] = 0.0
assert np.all(u.data[1, :] == 0)
def test_subdimleft_parallel(self):
"""
Tests application of an Operator consisting of a subdimension
defined over different sub-regions, explicitly created through the
use of SubDimensions.
This tests that flow direction is not being automatically inferred
from whether the subdimension is on the left or right boundary.
"""
grid = Grid(shape=(20, 20))
x, y = grid.dimensions
t = grid.stepping_dim
thickness = 4
u = TimeFunction(name='u', save=None, grid=grid, space_order=0, time_order=1)
xl = SubDimension.left(name='xl', parent=x, thickness=thickness)
yi = SubDimension.middle(name='yi', parent=y,
thickness_left=thickness, thickness_right=thickness)
# Can be done in parallel
eq = Eq(u[t+1, xl, yi], u[t, xl, yi] + 1)
op = Operator([eq])
iterations = FindNodes(Iteration).visit(op)
assert all(i.is_Affine and i.is_Parallel for i in iterations if i.dim in [xl, yi])
op.apply(time_m=0, time_M=0)
assert np.all(u.data[1, 0:thickness, 0:thickness] == 0)
assert np.all(u.data[1, 0:thickness, -thickness:] == 0)
assert np.all(u.data[1, 0:thickness, thickness:-thickness] == 1)
assert np.all(u.data[1, thickness+1:, :] == 0)
def test_subdimmiddle_notparallel(self):
"""
Tests application of an Operator consisting of a subdimension
defined over different sub-regions, explicitly created through the
use of SubDimensions.
Different from ``test_subdimmiddle_parallel`` because an interior
dimension cannot be evaluated in parallel.
"""
grid = Grid(shape=(20, 20))
x, y = grid.dimensions
t = grid.stepping_dim
thickness = 4
u = TimeFunction(name='u', save=None, grid=grid, space_order=0, time_order=1)
xi = SubDimension.middle(name='xi', parent=x,
thickness_left=thickness, thickness_right=thickness)
yi = SubDimension.middle(name='yi', parent=y,
thickness_left=thickness, thickness_right=thickness)
# flow dependencies in x and y which should force serial execution
# in reverse direction
centre = Eq(u[t+1, xi, yi], u[t, xi, yi] + u[t+1, xi+1, yi+1])
u.data[0, 10, 10] = 1.0
op = Operator([centre])
iterations = FindNodes(Iteration).visit(op)
assert all(i.is_Affine and i.is_Sequential for i in iterations if i.dim == xi)
assert all(i.is_Affine and i.is_Parallel for i in iterations if i.dim == yi)
op.apply(time_m=0, time_M=0)
for i in range(4, 11):
assert u.data[1, i, i] == 1.0
u.data[1, i, i] = 0.0
assert np.all(u.data[1, :] == 0)
def test_subdimleft_notparallel(self):
"""
Tests application of an Operator consisting of a subdimension
defined over different sub-regions, explicitly created through the
use of SubDimensions.
This tests that flow direction is not being automatically inferred
from whether the subdimension is on the left or right boundary.
"""
grid = Grid(shape=(20, 20))
x, y = grid.dimensions
t = grid.stepping_dim
thickness = 4
u = TimeFunction(name='u', save=None, grid=grid, space_order=1, time_order=0)
xl = SubDimension.left(name='xl', parent=x, thickness=thickness)
yi = SubDimension.middle(name='yi', parent=y,
thickness_left=thickness, thickness_right=thickness)
# Flows inward (i.e. forward) rather than outward
eq = Eq(u[t+1, xl, yi], u[t+1, xl-1, yi] + 1)
op = Operator([eq])
iterations = FindNodes(Iteration).visit(op)
assert all(i.is_Affine and i.is_Sequential for i in iterations if i.dim == xl)
assert all(i.is_Affine and i.is_Parallel for i in iterations if i.dim == yi)
op.apply(time_m=1, time_M=1)
assert all(np.all(u.data[0, :thickness, thickness+i] == [1, 2, 3, 4])
for i in range(12))
assert np.all(u.data[0, thickness:] == 0)
assert np.all(u.data[0, :, thickness+12:] == 0)
def test_subdim_fd(self):
"""
Test that the FD shortcuts are handled correctly with SubDimensions
"""
grid = Grid(shape=(20, 20))
x, y = grid.dimensions
u = TimeFunction(name='u', save=None, grid=grid, space_order=1, time_order=1)
u.data[:] = 2.
# Flows inward (i.e. forward) rather than outward
eq = [Eq(u.forward, u.dx + u.dy, subdomain=grid.interior)]
op = Operator(eq)
op.apply(time_M=0)
assert np.all(u.data[1, -1, :] == 2.)
assert np.all(u.data[1, :, 0] == 2.)
assert np.all(u.data[1, :, -1] == 2.)
assert np.all(u.data[1, 0, :] == 2.)
assert np.all(u.data[1, 1:18, 1:18] == 0.)
@skipif('yask')
def test_arrays_defined_over_subdims(self):
"""
Check code generation when an Array uses a SubDimension.
"""
grid = Grid(shape=(3,))
x, = grid.dimensions
xi, = grid.interior.dimensions
f = Function(name='f', grid=grid)
a = Array(name='a', dimensions=(xi,), dtype=grid.dtype)
op = Operator([Eq(a[xi], 1), Eq(f, f + a[xi + 1], subdomain=grid.interior)],
dle=('advanced', {'openmp': False}))
assert len(op.parameters) == 6
# neither `x_size` nor `xi_size` are expected here
assert not any(i.name in ('x_size', 'xi_size') for i in op.parameters)
# Try running it -- regardless of what it will produce, this should run
# ie, this checks this error isn't raised:
# "ValueError: No value found for parameter xi_size"
op()
@skipif(['yask', 'ops'])
class TestConditionalDimension(object):
"""A collection of tests to check the correct functioning of
ConditionalDimensions."""
def test_basic(self):
nt = 19
grid = Grid(shape=(11, 11))
time = grid.time_dim
u = TimeFunction(name='u', grid=grid)
assert(grid.stepping_dim in u.indices)
u2 = TimeFunction(name='u2', grid=grid, save=nt)
assert(time in u2.indices)
factor = 4
time_subsampled = ConditionalDimension('t_sub', parent=time, factor=factor)
usave = TimeFunction(name='usave', grid=grid, save=(nt+factor-1)//factor,
time_dim=time_subsampled)
assert(time_subsampled in usave.indices)
eqns = [Eq(u.forward, u + 1.), Eq(u2.forward, u2 + 1.), Eq(usave, u)]
op = Operator(eqns)
op.apply(t_M=nt-2)
assert np.all(np.allclose(u.data[(nt-1) % 3], nt-1))
assert np.all([np.allclose(u2.data[i], i) for i in range(nt)])
assert np.all([np.allclose(usave.data[i], i*factor)
for i in range((nt+factor-1)//factor)])
def test_basic_shuffles(self):
"""
Like ``test_basic``, but with different equation orderings. Nevertheless,
we assert against the same exact values as in ``test_basic``, since we
save `u`, not `u.forward`.
"""
nt = 19
grid = Grid(shape=(11, 11))
time = grid.time_dim
u = TimeFunction(name='u', grid=grid)
u2 = TimeFunction(name='u2', grid=grid, save=nt)
factor = 4
time_subsampled = ConditionalDimension('t_sub', parent=time, factor=factor)
usave = TimeFunction(name='usave', grid=grid, save=(nt+factor-1)//factor,
time_dim=time_subsampled)
# Shuffle 1
eqns = [Eq(usave, u), Eq(u.forward, u + 1.), Eq(u2.forward, u2 + 1.)]
op = Operator(eqns)
op.apply(t_M=nt-2)
assert np.all(np.allclose(u.data[(nt-1) % 3], nt-1))
assert np.all([np.allclose(u2.data[i], i) for i in range(nt)])
assert np.all([np.allclose(usave.data[i], i*factor)
for i in range((nt+factor-1)//factor)])
# Shuffle 2
usave.data[:] = 0.
u.data[:] = 0.
u2.data[:] = 0.
eqns = [Eq(u.forward, u + 1.), Eq(usave, u), Eq(u2.forward, u2 + 1.)]
op = Operator(eqns)
op.apply(t_M=nt-2)
assert np.all(np.allclose(u.data[(nt-1) % 3], nt-1))
assert np.all([np.allclose(u2.data[i], i) for i in range(nt)])
assert np.all([np.allclose(usave.data[i], i*factor)
for i in range((nt+factor-1)//factor)])
def test_spacial_subsampling(self):
"""
Test conditional dimension for the spatial ones.
This test saves u every two grid points :
u2[x, y] = u[2*x, 2*y]
"""
nt = 19
grid = Grid(shape=(12, 12))
time = grid.time_dim
u = TimeFunction(name='u', grid=grid, save=nt)
assert(grid.time_dim in u.indices)
# Creates subsampled spatial dimensions and accordine grid
dims = tuple([ConditionalDimension(d.name+'sub', parent=d, factor=2)
for d in u.grid.dimensions])
grid2 = Grid((6, 6), dimensions=dims, time_dimension=time)
u2 = TimeFunction(name='u2', grid=grid2, save=nt)
assert(time in u2.indices)
eqns = [Eq(u.forward, u + 1.), Eq(u2, u)]
op = Operator(eqns)
op.apply(time_M=nt-2)
# Verify that u2[x,y]= u[2*x, 2*y]
assert np.allclose(u.data[:-1, 0:-1:2, 0:-1:2], u2.data[:-1, :, :])
def test_subsampled_fd(self):
"""
Test that the FD shortcuts are handled correctly with ConditionalDimensions
"""
grid = Grid(shape=(21, 21))
time = grid.time_dim
# Creates subsampled spatial dimensions and accordine grid
dims = tuple([ConditionalDimension(d.name+'sub', parent=d, factor=2)
for d in grid.dimensions])
grid2 = Grid((6, 6), dimensions=dims, time_dimension=time)
u2 = TimeFunction(name='u2', grid=grid2, space_order=2, time_order=1)
u2.data.fill(2.)
eqns = [Eq(u2.forward, u2.dx + u2.dy)]
op = Operator(eqns)
op.apply(time_M=0, x_M=11, y_M=11)
# Verify that u2 contains subsampled fd values
assert np.all(u2.data[0, :, :] == 2.)
assert np.all(u2.data[1, 0, 0] == 0.)
assert np.all(u2.data[1, -1, -1] == -40.)
assert np.all(u2.data[1, 0, -1] == -20.)
assert np.all(u2.data[1, -1, 0] == -20.)
assert np.all(u2.data[1, 1:-1, 0] == 0.)
assert np.all(u2.data[1, 0, 1:-1] == 0.)
assert np.all(u2.data[1, 1:-1, -1] == -20.)
assert np.all(u2.data[1, -1, 1:-1] == -20.)
assert np.all(u2.data[1, 1:4, 1:4] == 0.)
# This test generates an openmp loop form which makes older gccs upset
@switchconfig(openmp=False)
def test_nothing_in_negative(self):
"""Test the case where when the condition is false, there is nothing to do."""
nt = 4
grid = Grid(shape=(11, 11))
time = grid.time_dim
u = TimeFunction(name='u', save=nt, grid=grid)
assert(grid.time_dim in u.indices)
factor = 4
time_subsampled = ConditionalDimension('t_sub', parent=time, factor=factor)
usave = TimeFunction(name='usave', grid=grid, save=(nt+factor-1)//factor,
time_dim=time_subsampled)
assert(time_subsampled in usave.indices)
eqns = [Eq(usave, u)]
op = Operator(eqns)
u.data[:] = 1.0
usave.data[:] = 0.0
op.apply(time_m=1, time_M=1)
assert np.allclose(usave.data, 0.0)
op.apply(time_m=0, time_M=0)
assert np.allclose(usave.data, 1.0)
def test_laplace(self):
grid = Grid(shape=(20, 20, 20))
x, y, z = grid.dimensions
time = grid.time_dim
t = grid.stepping_dim
tsave = ConditionalDimension(name='tsave', parent=time, factor=2)
u = TimeFunction(name='u', grid=grid, save=None, time_order=2)
usave = TimeFunction(name='usave', grid=grid, time_dim=tsave,
time_order=0, space_order=0)
steps = []
# save of snapshot
steps.append(Eq(usave, u))
# standard laplace-like thing
steps.append(Eq(u[t+1, x, y, z],
u[t, x, y, z] - u[t-1, x, y, z]
+ u[t, x-1, y, z] + u[t, x+1, y, z]
+ u[t, x, y-1, z] + u[t, x, y+1, z]
+ u[t, x, y, z-1] + u[t, x, y, z+1]))
op = Operator(steps)
u.data[:] = 0.0
u.data[0, 10, 10, 10] = 1.0
op.apply(time_m=0, time_M=0)
assert np.sum(u.data[0, :, :, :]) == 1.0
assert np.sum(u.data[1, :, :, :]) == 7.0
assert np.all(usave.data[0, :, :, :] == u.data[0, :, :, :])
def test_as_expr(self):
nt = 19
grid = Grid(shape=(11, 11))
time = grid.time_dim
u = TimeFunction(name='u', grid=grid)
assert(grid.stepping_dim in u.indices)
u2 = TimeFunction(name='u2', grid=grid, save=nt)
assert(time in u2.indices)
factor = 4
time_subsampled = ConditionalDimension('t_sub', parent=time, factor=factor)
usave = TimeFunction(name='usave', grid=grid, save=(nt+factor-1)//factor,
time_dim=time_subsampled)
assert(time_subsampled in usave.indices)
eqns = [Eq(u.forward, u + 1.), Eq(u2.forward, u2 + 1.),
Eq(usave, time_subsampled * u)]
op = Operator(eqns)
op.apply(t=nt-2)
assert np.all(np.allclose(u.data[(nt-1) % 3], nt-1))
assert np.all([np.allclose(u2.data[i], i) for i in range(nt)])
assert np.all([np.allclose(usave.data[i], i*factor*i)
for i in range((nt+factor-1)//factor)])
def test_shifted(self):
nt = 19
grid = Grid(shape=(11, 11))
time = grid.time_dim
u = TimeFunction(name='u', grid=grid)
assert(grid.stepping_dim in u.indices)
u2 = TimeFunction(name='u2', grid=grid, save=nt)
assert(time in u2.indices)
factor = 4
time_subsampled = ConditionalDimension('t_sub', parent=time, factor=factor)
usave = TimeFunction(name='usave', grid=grid, save=2, time_dim=time_subsampled)
assert(time_subsampled in usave.indices)
t_sub_shift = Constant(name='t_sub_shift', dtype=np.int32)
eqns = [Eq(u.forward, u + 1.), Eq(u2.forward, u2 + 1.),
Eq(usave.subs(time_subsampled, time_subsampled - t_sub_shift), u)]
op = Operator(eqns)
# Starting at time_m=10, so time_subsampled - t_sub_shift is in range
op.apply(time_m=10, time_M=nt-2, t_sub_shift=3)
assert np.all(np.allclose(u.data[0], 8))
assert np.all([np.allclose(u2.data[i], i - 10) for i in range(10, nt)])
assert np.all([np.allclose(usave.data[i], 2+i*factor) for i in range(2)])
def test_no_index(self):
"""Test behaviour when the ConditionalDimension is used as a symbol in
an expression."""
nt = 19
grid = Grid(shape=(11, 11))
time = grid.time_dim
u = TimeFunction(name='u', grid=grid)
assert(grid.stepping_dim in u.indices)
v = Function(name='v', grid=grid)
factor = 4
time_subsampled = ConditionalDimension('t_sub', parent=time, factor=factor)
eqns = [Eq(u.forward, u + 1), Eq(v, v + u*u*time_subsampled)]
op = Operator(eqns)
op.apply(t_M=nt-2)
assert np.all(np.allclose(u.data[(nt-1) % 3], nt-1))
# expected result is 1024
# v = u[0]**2 * 0 + u[4]**2 * 1 + u[8]**2 * 2 + u[12]**2 * 3 + u[16]**2 * 4
# with u[t] = t
# v = 16 * 1 + 64 * 2 + 144 * 3 + 256 * 4 = 1600
assert np.all(np.allclose(v.data, 1600))
def test_no_index_sparse(self):
"""Test behaviour when the ConditionalDimension is used as a symbol in
an expression over sparse data objects."""
grid = Grid(shape=(4, 4), extent=(3.0, 3.0))
time = grid.time_dim
f = TimeFunction(name='f', grid=grid, save=1)
f.data[:] = 0.
coordinates = [(0.5, 0.5), (0.5, 2.5), (2.5, 0.5), (2.5, 2.5)]
sf = SparseFunction(name='sf', grid=grid, npoint=4, coordinates=coordinates)
sf.data[:] = 1.
sd = sf.dimensions[sf._sparse_position]
# We want to write to `f` through `sf` so that we obtain the
# following 4x4 grid (the '*' show the position of the sparse points)
# We do that by emulating an injection
#
# 0 --- 0 --- 0 --- 0
# | * | | * |
# 0 --- 1 --- 1 --- 0
# | | | |
# 0 --- 1 --- 1 --- 0
# | * | | * |
# 0 --- 0 --- 0 --- 0
radius = 1
indices = [(i, i+radius) for i in sf._coordinate_indices]
bounds = [i.symbolic_size - radius for i in grid.dimensions]
eqs = []
for e, i in enumerate(product(*indices)):
args = [j > 0 for j in i]
args.extend([j < k for j, k in zip(i, bounds)])
condition = And(*args, evaluate=False)
cd = ConditionalDimension('sfc%d' % e, parent=sd, condition=condition)
index = [time] + list(i)
eqs.append(Eq(f[index], f[index] + sf[cd]))
op = Operator(eqs)
op.apply(time=0)
assert np.all(f.data[0, 1:-1, 1:-1] == 1.)
assert np.all(f.data[0, 0] == 0.)
assert np.all(f.data[0, -1] == 0.)
assert np.all(f.data[0, :, 0] == 0.)
assert np.all(f.data[0, :, -1] == 0.)
def test_symbolic_factor(self):
"""
Test ConditionalDimension with symbolic factor (provided as a Constant).
"""
g = Grid(shape=(4, 4, 4))
u = TimeFunction(name='u', grid=g, time_order=0)
fact = Constant(name='fact', dtype=np.int32, value=4)
tsub = ConditionalDimension(name='tsub', parent=g.time_dim, factor=fact)
usave = TimeFunction(name='usave', grid=g, time_dim=tsub, save=4)
op = Operator([Eq(u, u + 1), Eq(usave, u)])
op.apply(time=7) # Use `fact`'s default value, 4
assert np.all(usave.data[0] == 1)
assert np.all(usave.data[1] == 5)
u.data[:] = 0.
op.apply(time=7, fact=2)
assert np.all(usave.data[0] == 1)
assert np.all(usave.data[1] == 3)
assert np.all(usave.data[2] == 5)
assert np.all(usave.data[3] == 7)
def test_implicit_dims(self):
"""
Test ConditionalDimension as an implicit dimension for an equation.
"""
# This test makes an Operator that should create a vector of increasing
# integers, but stop incrementing when a certain stop value is reached
shape = (50,)
stop_value = 20
time = Dimension(name='time')
f = TimeFunction(name='f', shape=shape, dimensions=[time])
# The condition to stop incrementing
cond = ConditionalDimension(name='cond',
parent=time, condition=f[time] < stop_value)
eqs = [Eq(f.forward, f), Eq(f.forward, f.forward + 1, implicit_dims=[cond])]
op = Operator(eqs)
op.apply(time_M=shape[0] - 2)
# Make the same calculation in python to assert the result
F = np.zeros(shape[0])
for i in range(shape[0]):
F[i] = i if i < stop_value else stop_value
assert np.all(f.data == F)
def test_no_fusion_simple(self):
"""
If ConditionalDimensions are present, then Clusters must not be fused so
that ultimately Eqs get scheduled to different loop nests.
"""
grid = Grid(shape=(4, 4, 4))
time = grid.time_dim
f = TimeFunction(name='f', grid=grid)
g = Function(name='g', grid=grid)
h = Function(name='h', grid=grid)
# No ConditionalDimensions yet. Will be fused and optimized
eqns = [Eq(f.forward, f + 1),
Eq(h, f + 1),
Eq(g, f + 1)]
op = Operator(eqns)
exprs = FindNodes(Expression).visit(op._func_table['bf0'].root)
assert len(exprs) == 4
assert exprs[1].expr.rhs is exprs[0].output
assert exprs[2].expr.rhs is exprs[0].output
assert exprs[3].expr.rhs is exprs[0].output
# Now with a ConditionalDimension. No fusion, no optimization
ctime = ConditionalDimension(name='ctime', parent=time, condition=time > 4)
eqns = [Eq(f.forward, f + 1),
Eq(h, f + 1),
Eq(g, f + 1, implicit_dims=[ctime])]
op = Operator(eqns)
exprs = FindNodes(Expression).visit(op._func_table['bf0'].root)
assert len(exprs) == 3
assert exprs[1].expr.rhs is exprs[0].output
assert exprs[2].expr.rhs is exprs[0].output
exprs = FindNodes(Expression).visit(op._func_table['bf1'].root)
assert len(exprs) == 1
def test_no_fusion_convoluted(self):
"""
Conceptually like `test_no_fusion_simple`, but with more expressions
and non-trivial data flow.
"""
grid = Grid(shape=(4, 4, 4))
time = grid.time_dim
f = TimeFunction(name='f', grid=grid)
g = Function(name='g', grid=grid)
h = Function(name='h', grid=grid)
ctime = ConditionalDimension(name='ctime', parent=time, condition=time > 4)
eqns = [Eq(f.forward, f + 1),
Eq(h, f + 1),
Eq(g, f + 1, implicit_dims=[ctime]),
Eq(f.forward, f + 1, implicit_dims=[ctime]),
Eq(f.forward, f + 1),
Eq(g, f + 1)]
op = Operator(eqns)
exprs = FindNodes(Expression).visit(op._func_table['bf0'].root)
assert len(exprs) == 3
assert exprs[1].expr.rhs is exprs[0].output
assert exprs[2].expr.rhs is exprs[0].output
exprs = FindNodes(Expression).visit(op._func_table['bf1'].root)
assert len(exprs) == 2
exprs = FindNodes(Expression).visit(op._func_table['bf2'].root)
assert len(exprs) == 3
assert exprs[1].expr.rhs is exprs[0].output
assert exprs[2].expr.rhs is exprs[0].output