This file is indexed.

/usr/include/blitz/globeval.cc is in libblitz0-dev 1:0.10-3.3.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

   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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
/***************************************************************************
 * blitz/array/eval.cc  Evaluate expression and assign to an array.
 *
 * $Id$
 *
 * Copyright (C) 1997-2011 Todd Veldhuizen <tveldhui@acm.org>
 *
 * This file is a part of Blitz.
 *
 * Blitz is free software: you can redistribute it and/or modify 
 * it under the terms of the GNU Lesser General Public License
 * as published by the Free Software Foundation, either version 3
 * of the License, or (at your option) any later version.
 *
 * Blitz is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public 
 * License along with Blitz.  If not, see <http://www.gnu.org/licenses/>.
 * 
 * Suggestions:          blitz-devel@lists.sourceforge.net
 * Bugs:                 blitz-support@lists.sourceforge.net    
 *
 * For more information, please see the Blitz++ Home Page:
 *    https://sourceforge.net/projects/blitz/
 *
 ****************************************************************************/
#ifndef BZ_GLOBEVAL_CC
#define BZ_GLOBEVAL_CC

#include <blitz/ranks.h>
#include <blitz/tvevaluate.h>
#include <blitz/blitz.h>

BZ_NAMESPACE(blitz)


// Fast traversals require <set> from the ISO/ANSI C++ standard library
#ifdef BZ_HAVE_STD
#ifdef BZ_ARRAY_SPACE_FILLING_TRAVERSAL


/** _bz_tryFastTraversal is a helper class.  Fast traversals are only
    attempted if the expression looks like a stencil -- it's at least
    three-dimensional, has at least six array operands, and there are
    no index placeholders in the expression.  These are all things
    which can be checked at compile time, so the if()/else() syntax
    has been replaced with this class template.  
*/
template<bool canTryFastTraversal>
struct _bz_tryFastTraversal {
    template<typename T_numtype, int N_rank, typename T_expr, typename T_update>
    static bool tryFast(Array<T_numtype,N_rank>& array, 
        T_expr expr, T_update)
    {
        return false;
    }
};

template<>
struct _bz_tryFastTraversal<true> {
    template<typename T_numtype, int N_rank, typename T_expr, typename T_update>
    static bool tryFast(Array<T_numtype,N_rank>& array, 
        T_expr expr, T_update)
    {
        // See if there's an appropriate space filling curve available.
        // Currently fast traversals use an N-1 dimensional curve.  The
        // Nth dimension column corresponding to each point on the curve
        // is traversed in the normal fashion.
        TraversalOrderCollection<N_rank-1> traversals;
        TinyVector<int, N_rank - 1> traversalGridSize;

        for (int i=0; i < N_rank - 1; ++i)
            traversalGridSize[i] = array.length(array.ordering(i+1));

#ifdef BZ_DEBUG_TRAVERSE
cout << "traversalGridSize = " << traversalGridSize << endl;
cout.flush();
#endif

        const TraversalOrder<N_rank-1>* order =
            traversals.find(traversalGridSize);

        if (order)
        {
#ifdef BZ_DEBUG_TRAVERSE
    cerr << "Array<" << BZ_DEBUG_TEMPLATE_AS_STRING_LITERAL(T_numtype)
         << ", " << N_rank << ">: Using stack traversal" << endl;
#endif
            // A curve was available -- use fast traversal.
            array.evaluateWithFastTraversal(*order, expr, T_update());
            return true;
        }

        return false;
    }
};
#endif // BZ_ARRAY_SPACE_FILLING_TRAVERSAL
#endif // BZ_HAVE_STD


/** Helper class that implements the evaluation routines for different
    ranks. */
template<int N> struct _bz_evaluator {
  template<typename T_dest, typename T_expr, typename T_update>
  static void evaluateWithStackTraversal(T_dest&, T_expr, T_update);
  template<typename T_dest, typename T_expr, typename T_update>
  static void evaluateWithIndexTraversal(T_dest&, T_expr, T_update);
};
template<> struct _bz_evaluator<1> {
  template<typename T_dest, typename T_expr, typename T_update>
  static void evaluateWithStackTraversal(T_dest&, T_expr, T_update);
  template<typename T_dest, typename T_expr, typename T_update>
  static void evaluateWithIndexTraversal(T_dest&, T_expr, T_update);
};

/**
  Assign an expression to a container.  For performance reasons, this
  function forwards to functions implementing one of several traversal
  mechanisms:
 
  - Index traversal scans through the destination array in storage order.
    The expression is evaluated using a TinyVector<int,N> operand.  This
    version is used only when there are index placeholders in the expression
    (see <blitz/indexexpr.h>)
  - Stack traversal also scans through the destination array in storage
    order.  However, push/pop stack iterators are used.
  - Fast traversal follows a Hilbert (or other) space-filling curve to
    improve cache reuse for stencilling operations.  Currently, the
    space filling curves must be generated by calling 
    generateFastTraversalOrder(TinyVector<int,N_dimensions>).
  - 2D tiled traversal follows a tiled traversal, to improve cache reuse
    for 2D stencils.  Space filling curves have too much overhead to use
    in two-dimensions. 
 */
template<typename T_dest, typename T_expr, typename T_update>
_bz_forceinline void
_bz_evaluate(T_dest& dest, T_expr expr, T_update)
{
  typedef typename T_dest::T_numtype T_numtype;
  const int N_rank = T_dest::rank_;

    // Check that all arrays have the same shape
#ifdef BZ_DEBUG
    if (!expr.shapeCheck(dest.shape()))
    {
      if (assertFailMode == false)
      {
        cerr << "[Blitz++] Shape check failed: Module " << __FILE__
             << " line " << __LINE__ << endl
             << "          Expression: ";
        prettyPrintFormat format(true);   // Use terse formatting
        BZ_STD_SCOPE(string) str;
        expr.prettyPrint(str, format);
        cerr << str << endl ;
      }

#if 0
// Shape dumping is broken by change to using string for prettyPrint
             << "          Shapes: " << shape() << " = ";
        prettyPrintFormat format2;
        format2.setDumpArrayShapesMode();
        expr.prettyPrint(cerr, format2);
        cerr << endl;
#endif
        BZ_PRE_FAIL;
    }
#endif

    BZPRECHECK(expr.shapeCheck(dest.shape()),
        "Shape check failed." << endl << "Expression:");

    BZPRECHECK((T_expr::rank_ == T_dest::rank_) || 
	       (T_expr::numArrayOperands == 0), 
	       "Assigned rank " << T_expr::rank_ << " expression to rank " 
	       << T_dest::rank_ << " array.");

    /*
     * Check that the arrays are not empty (e.g. length 0 arrays)
     * This fixes a bug found by Peter Bienstman, 6/16/99, where
     * Array<double,2> A(0,0),B(0,0); B=A(tensor::j,tensor::i);
     * went into an infinite loop.
     */

    const sizeType n = dest.numElements();
    if (n == 0) {
#ifdef BZ_DEBUG_TRAVERSE
      BZ_DEBUG_MESSAGE("Evaluating empty array, nothing to do");
#endif
      return;
    }
    // \todo this does not alvays compile, so eliminate for now.
    // if (n == 1) {
    //   // shortcut here since it's easy
    //   T_update::update(*dest.dataFirst(), expr(expr.lbound()));
    //   return;
    // }

#ifdef BZ_DEBUG_TRAVERSE
    BZ_DEBUG_MESSAGE( "T_expr::numIndexPlaceholders = " << T_expr::numIndexPlaceholders);
#endif

    // Tau profiling code.  Provide Tau with a pretty-printed version of
    // the expression.
    // NEEDS_WORK-- use a static initializer somehow.

#ifdef BZ_TAU_PROFILING
    static BZ_STD_SCOPE(string) exprDescription;
    if (!exprDescription.length())   // faked static initializer
    {
        exprDescription = "A";
        prettyPrintFormat format(true);   // Terse mode on
        format.nextArrayOperandSymbol();
        T_update::prettyPrint(exprDescription);
        expr.prettyPrint(exprDescription, format);
    }
    TAU_PROFILE(" ", exprDescription, TAU_BLITZ);
#endif

    // Determine which evaluation mechanism to use 
    if (T_expr::numIndexPlaceholders > 0)
    {
        // The expression involves index placeholders, so have to
        // use index traversal rather than stack traversal.

      _bz_evaluator<T_dest::rank_>::evaluateWithIndexTraversal(dest, expr, T_update());
      return;
    }
    else {

        // If this expression looks like an array stencil, then attempt to
        // use a fast traversal order.
        // Fast traversals require <set> from the ISO/ANSI C++ standard
        // library.

#ifdef BZ_HAVE_STD
#ifdef BZ_ARRAY_SPACE_FILLING_TRAVERSAL

        enum { isStencil = (N_rank >= 3) && (T_expr::numArrayOperands > 6)
            && (T_expr::numIndexPlaceholders == 0) };

        if (_bz_tryFastTraversal<isStencil>::tryFast(dest, expr, T_update()))
            return;

#endif
#endif

#ifdef BZ_ARRAY_2D_STENCIL_TILING
        // Does this look like a 2-dimensional stencil on a largeish
        // array?

        if ((N_rank == 2) && (T_expr::numArrayOperands >= 5))
        {
            // Use a heuristic to determine whether a tiled traversal
            // is desirable.  First, estimate how much L1 cache is needed 
            // to achieve a high hit rate using the stack traversal.
            // Try to err on the side of using tiled traversal even when
            // it isn't strictly needed.

            // Assumptions:
            //    Stencil width 3
            //    3 arrays involved in stencil
            //    Uniform data type in arrays (all T_numtype)
            
            int cacheNeeded = 3 * 3 * sizeof(T_numtype) * dest.length(dest.ordering(0));
            if (cacheNeeded > BZ_L1_CACHE_ESTIMATED_SIZE) {
	      _bz_evaluateWithTiled2DTraversal(dest, expr, T_update());
		return;
	    }
        }

#endif

        // If fast traversal isn't available or appropriate, then just
        // do a stack traversal.
//#pragma forceinline recursive
	_bz_evaluator<T_dest::rank_>::evaluateWithStackTraversal(dest, expr, T_update());
	return;
    }
}

/** This class performs the vectorized update through the update()
    method. It is a class because it is specialized to do nothing for
    instances where the simd vector width is 1. This avoids tricky
    infinite template recursions on multicomponent containers. */
template<typename T_numtype, typename T_expr, typename T_update, int N>
struct chunked_updater {

  static _bz_forceinline void
  aligned_update(T_numtype* data, T_expr expr, diffType i) {

    const bool unroll = N < BZ_TV_EVALUATE_UNROLL_LENGTH;
    _tv_evaluator<unroll, N>::evaluate_aligned
      (data+i, expr.template fastRead_tv<N>(i), T_update());
  };

  static _bz_forceinline void
  unaligned_update(T_numtype* data, T_expr expr, diffType i) {
    const bool unroll = N < BZ_TV_EVALUATE_UNROLL_LENGTH;
    _tv_evaluator<unroll, N>::evaluate_unaligned
      (data+i, expr.template fastRead_tv<N>(i), T_update());
  };

};

/** specialization ensures we don't try to instantiate chunked_updates
    for types with a vecWidth of 1, as this leads to infinite template
    instantiation recursion. */
template<typename T_numtype, typename T_expr, typename T_update>
struct chunked_updater<T_numtype, T_expr, T_update, 1> {
  static _bz_forceinline void 
  aligned_update(T_numtype* data, T_expr expr, diffType i) {
    BZPRECONDITION(0); };
  static _bz_forceinline void
  unaligned_update(T_numtype* data, T_expr expr, diffType i) {
    BZPRECONDITION(0); };
};


/** A metaprogram that uses the chunked_updater to assign an
    unknown-length expression to a pointer by unrolling in a binary
    fashion. This way, we can get "almost-compile-time" unrolling of a
    length only known at runtime. I+1 is the number of significant
    bits in the longest length to consider. In this way, assigning a
    vector of length 7 is I=2 and will take 3 operations. The
    metaprogram counts down, that way it will start with large updates
    which will be aligned for aligned expressions. */
template<int I> 
class _bz_meta_binaryAssign {
public:
    template<typename T_data, typename T_expr, typename T_update>
    static _bz_forceinline void assign(T_data* data, T_expr expr,
				       diffType ubound, diffType pos, 
				       T_update) {
      if(ubound&(1<<I)) {
	chunked_updater<T_data, T_expr, T_update, 1<<I >::
	  unaligned_update(data, expr, pos); 
	pos += (1<<I);
      }
      _bz_meta_binaryAssign<I-1>::assign(data, expr, ubound, pos, T_update());
      }
        
};

/** Partial specialization for bit 0 uses the scalar update. */
template<> 
class _bz_meta_binaryAssign<0> {
public:
    template<typename T_data, typename T_expr, typename T_update>
    static _bz_forceinline void assign(T_data* data, T_expr expr,
				       diffType ubound, diffType pos, 
				       T_update) {
      if(ubound&1) {
	T_update::update(data[pos], expr.fastRead(pos));
	++pos;
      }
      // this ends the metaprogram.
    }
};

/** Unit-stride evaluator, which takes pre-computed destination and
    bounds and just does the unit-stride evaluation for a specified
    length. This is essentially a 1D operation used by both the rank-1
    and rank-N traversals, so it's common to both. This can use
    vectorized update, so if both dest and expr are unit stride, we
    redirect here. This function then deals with unaligned or
    misaligned situations. There is no explicit unrolling option here,
    since it's already vectorized using the chunk_updater. \todo Would
    it be useful to retain the unrolled loop for scalar
    architectures?  */
template<typename T_dest, typename T_expr, typename T_update>
_bz_forceinline void
_bz_evaluateWithUnitStride(T_dest& dest, typename T_dest::T_iterator& iter,
			   T_expr expr, diffType ubound, T_update)
{
  typedef typename T_dest::T_numtype T_numtype;
  T_numtype* restrict data = const_cast<T_numtype*>(iter.data());
  diffType i=0;

#ifdef BZ_DEBUG_TRAVERSE
  BZ_DEBUG_MESSAGE("\tunit stride expression with length: "<< ubound << ".");
#endif

  // If the minWidth is set to 0, there are elements in the expression
  // which can NOT use the vectorized expression (i.e, stencils). In
  // that case, we fall through to the scalar loop
  const bool unvectorizable = (T_expr::minWidth==0);

  if(!unvectorizable && (ubound < 1<<BZ_MAX_BITS_FOR_BINARY_UNROLL)) {
    // for short expressions, it's more important to lose
    // overhead. Single-element ones have already been dealt with, but
    // for lengths that are have fewer significant bits than
    // max_bits_for_unroll we do a binary-style unroll here. (We don't
    // worry about simd widths either, because we essentially just
    // present the compiler with a vectorizable view. It will do
    // sensible things even if the expressions are not vectorizable.)
#ifdef BZ_DEBUG_TRAVERSE
    BZ_DEBUG_MESSAGE("\tshort expression, using binary meta-unroll assignment.");
#endif

    _bz_meta_binaryAssign<BZ_MAX_BITS_FOR_BINARY_UNROLL-1>::
      assign(data, expr, ubound, 0, T_update());
    return;
  }

  // calculate uneven elements at the beginning of dest
  const diffType uneven_start=simdTypes<T_numtype>::offsetToAlignment(data);

  // we can only guarantee alignment if all operands have the same
  // width and are not mutually misaligned
  const bool can_align = 
    (T_expr::minWidth == T_expr::maxWidth) &&
    (T_expr::minWidth == int(simdTypes<T_numtype>::vecWidth)) &&
    expr.isVectorAligned(uneven_start);

  // When we come out here, we KNOW that expressions shorter than
  // 1<<BZ_MAX_BITS_FOR_BINARY_UNROLL have been taken care of. At that
  // point, it is efficient to effectively unroll the loop using a
  // vector width larger than the simd width.
  const int loop_width= BZ_VECTORIZED_LOOP_WIDTH;

#ifdef BZ_DEBUG_TRAVERSE
  if(T_expr::minWidth!=T_expr::maxWidth) {
    BZ_DEBUG_MESSAGE("\texpression has mixed width: " << T_expr::minWidth << "-" <<T_expr::maxWidth);
  } else {
    BZ_DEBUG_MESSAGE("\texpression SIMD width: " << T_expr::minWidth);
  }
  BZ_DEBUG_MESSAGE("\tdestination SIMD width: " << simdTypes<T_numtype>::vecWidth);
  if(loop_width>1) {
  if(!expr.isVectorAligned(uneven_start)) {
    BZ_DEBUG_MESSAGE("\toperands have different alignments");
  }
  if(!can_align) {
    BZ_DEBUG_MESSAGE("\tcannot guarantee alignment - using unaligned vectorization")
      } else {
    BZ_DEBUG_MESSAGE("\texpression can be aligned");
  }
  if(loop_width<=ubound) {
    BZ_DEBUG_MESSAGE("\tusing vectorization width " << loop_width);
  } else {
    BZ_DEBUG_MESSAGE("\texpression not long enough to be vectorized");
  }
  } else {
    BZ_DEBUG_MESSAGE("\texpression cannot be vectorized");
  }
#endif


  if(!unvectorizable && (loop_width>1)) {
    // If the expression can be aligned, we do so.
    if(can_align) {
#ifdef BZ_DEBUG_TRAVERSE
      if(i<uneven_start) {
	BZ_DEBUG_MESSAGE("\tscalar loop for " << uneven_start << " unaligned starting elements");
      }
#endif
#ifdef BZ_USE_ALIGNMENT_PRAGMAS
#pragma ivdep
#endif
      for (; i < uneven_start; ++i)
//#pragma forceinline recursive
	T_update::update(data[i], expr.fastRead(i));
      
      // and then the vectorized part
#ifdef BZ_DEBUG_TRAVERSE
      if(i<=ubound-loop_width) {
	BZ_DEBUG_MESSAGE("\taligned vectorized loop with width " << loop_width << " starting at " << i);
      }
#endif
      for (; i <= ubound-loop_width; i+=loop_width)
//#pragma forceinline recursive
	chunked_updater<T_numtype, T_expr, T_update, loop_width>::
	  aligned_update(data, expr, i);
    }
    else {
      // if we can not line up the expressions, alignment doesn't
      // matter and we just start using unaligned vectorized
      // instructions from element 0
#ifdef BZ_DEBUG_TRAVERSE
      if(i<=ubound-loop_width) {
	BZ_DEBUG_MESSAGE("\tunaligned vectorized loop with width " << loop_width << " starting at " << i);
      }
#endif
      for (; i <= ubound-loop_width; i+=loop_width)
//#pragma forceinline recursive
	chunked_updater<T_numtype, T_expr, T_update, loop_width>::
	  unaligned_update(data, expr, i);
    }
  }

  // now complete the loop with the tailing scalar elements not done
  // in the chunked loop.
#ifdef BZ_DEBUG_TRAVERSE
  if(i<ubound) {
    BZ_DEBUG_MESSAGE("\tscalar loop for " << ubound-i << " trailing elements starting at " << i);
  }
#endif
#ifdef BZ_USE_ALIGNMENT_PRAGMAS
#pragma ivdep
#endif
  for (; i < ubound; ++i)
//#pragma forceinline recursive
    T_update::update(data[i], expr.fastRead(i));

#ifdef BZ_DEBUG_TRAVERSE
  BZ_DEBUG_MESSAGE("\tunit stride evaluation done")
#endif
}


/** Common-stride evaluator. Used for common but non-unit
    strides. Note that the stride can be negative, so we need to use a
    signed type. */
template<typename T_dest, typename T_expr, typename T_update>
_bz_forceinline void
_bz_evaluateWithCommonStride(T_dest& dest, typename T_dest::T_iterator& iter,
			     T_expr expr, diffType ubound, 
			     diffType commonStride, 
			     T_update)
{
#ifdef BZ_DEBUG_TRAVERSE 
     BZ_DEBUG_MESSAGE("\tcommon stride = " << commonStride);
#endif

  typedef typename T_dest::T_numtype T_numtype;
  T_numtype* restrict data = const_cast<T_numtype*>(iter.data());

#ifndef BZ_ARRAY_STACK_TRAVERSAL_UNROLL
# ifdef BZ_USE_ALIGNMENT_PRAGMAS
# pragma ivdep
# endif
  for (diffType i=0; i != ubound; i += commonStride)
    T_update::update(data[i], expr.fastRead(i));
#else
  diffType n1 = (dest.length(firstRank) & 3) * commonStride;
	 
  diffType i = 0;
  for (; i != n1; i += commonStride)
    T_update::update(data[i], expr.fastRead(i));
	 
  diffType strideInc = 4 * commonStride;
  for (; i != ubound; i += strideInc)
    {
      T_update::update(data[i], expr.fastRead(i));
      diffType i2 = i + commonStride;
      T_update::update(data[i2], expr.fastRead(i2));
      diffType i3 = i + 2 * commonStride;
      T_update::update(data[i3], expr.fastRead(i3));
      diffType i4 = i + 3 * commonStride;
      T_update::update(data[i4], expr.fastRead(i4));
    }
#endif  // BZ_ARRAY_STACK_TRAVERSAL_UNROLL
  return;
}


/* 1-d stack traversal evaluation. Forwards to evaluateWithUnitStride
   or evaluateWithCommonStride, if applicable, otherwise does the slow
   different-stride update. */
template<typename T_dest, typename T_expr, typename T_update>
_bz_forceinline void
_bz_evaluator<1>::
evaluateWithStackTraversal(T_dest& dest, T_expr expr, T_update)
{
#ifdef BZ_DEBUG_TRAVERSE
  BZ_DEBUG_MESSAGE("_bz_evaluator<1>: Using stack traversal");
#endif

  typename T_dest::T_iterator iter(dest);

  // if we only have one element, strides don't matter. In that case,
  // we just evaluate that right now so we don't have to deal with it.
  if(dest.length(firstRank)==1) {
#ifdef BZ_DEBUG_TRAVERSE
  BZ_DEBUG_MESSAGE("\tshortcutting evaluation of single-element expression");
#endif
    T_update::update(*const_cast<typename T_dest::T_numtype*>(iter.data()), *expr);
    return;
  }

  iter.loadStride(firstRank);
  expr.loadStride(firstRank);

  const bool useUnitStride = iter.isUnitStride()
    && expr.isUnitStride();

  if(useUnitStride) {
    const diffType ubound = dest.length(firstRank);
    _bz_evaluateWithUnitStride(dest, iter, expr, ubound, T_update());
    return;
  }

#ifdef BZ_ARRAY_EXPR_USE_COMMON_STRIDE
  diffType commonStride = expr.suggestStride(firstRank);
  if (iter.suggestStride(firstRank) > commonStride)
    commonStride = iter.suggestStride(firstRank);
  bool useCommonStride = iter.isStride(firstRank,commonStride)
    && expr.isStride(firstRank,commonStride);
#else
  diffType commonStride = 1;
  bool useCommonStride = false;
#endif

  if (useCommonStride) {
    const diffType ubound = dest.length(firstRank) * commonStride;
    _bz_evaluateWithCommonStride(dest, iter, expr, ubound, commonStride, T_update());
    return;
  }

#ifdef BZ_DEBUG_TRAVERSE
  BZ_DEBUG_MESSAGE("\tnot common stride");
#endif

  // not common stride
  typedef typename T_dest::T_numtype T_numtype;
  const T_numtype * last = iter.data() + dest.length(firstRank) 
    * dest.stride(firstRank);
     
  while (iter.data() != last)
    {
      T_update::update(*const_cast<T_numtype*>(iter.data()), *expr);
      iter.advance();
      expr.advance();
    }
}


/**
   Perform a stack traversal of a rank >1 expression. A stack
   traversal replaces the usual nested loops:
      
   for (int i=A.lbound(firstDim); i <= A.ubound(firstDim); ++i)
     for (int j=A.lbound(secondDim); j <= A.ubound(secondDim); ++j)
       for (int k=A.lbound(thirdDim); k <= A.ubound(thirdDim); ++k)
         A(i,j,k) = 0;
      
   with a stack data structure.  The stack allows this single routine
   to replace any number of nested loops.
      
   For each dimension (loop), these quantities are needed:
   - a pointer to the first element encountered in the loop
   - the stride associated with the dimension/loop
   - a pointer to the last element encountered in the loop
   
   The basic idea is that entering each loop is a "push" onto the
   stack, and exiting each loop is a "pop".  In practice, this
   routine treats accesses the stack in a random-access way,
   which confuses the picture a bit.  But conceptually, that's
   what is going on.

   ordering(0) gives the dimension associated with the smallest
   stride (usually; the exceptions have to do with subarrays and
   are uninteresting).  We call this dimension maxRank; it will
   become the innermost "loop".
      
   Ordering the loops from ordering(N_rank-1) down to
   ordering(0) ensures that the largest stride is associated
   with the outermost loop, and the smallest stride with the
   innermost.  This is critical for good performance on
   cached machines.
*/

template<int N>
template<typename T_dest, typename T_expr, typename T_update>
_bz_forceinline void
_bz_evaluator<N>::
evaluateWithStackTraversal(T_dest& dest, T_expr expr, T_update)
 {
#ifdef BZ_DEBUG_TRAVERSE
   BZ_DEBUG_MESSAGE("_bz_evaluator<" << N << ">: Using stack traversal");
#endif

   typedef typename T_dest::T_numtype T_numtype;
   const int N_rank = T_dest::rank();

     const int maxRank = dest.ordering(0);
     // const int secondLastRank = ordering(1);

     // Create an iterator for the array receiving the result
     typename T_dest::T_iterator iter(dest);

     // Set the initial stack configuration by pushing the pointer
     // to the first element of the array onto the stack N times.

     int i;
     for (i=1; i < N_rank; ++i)
     {
	 iter.push(i);
	 expr.push(i);
     }

     // Load the strides associated with the innermost loop.
     iter.loadStride(maxRank);
     expr.loadStride(maxRank);

     /* 
      * Is the stride in the innermost loop equal to 1?  If so,
      * we might take advantage of this and generate more
      * efficient code.
      */
     const bool useUnitStride = iter.isUnitStride()
                          && expr.isUnitStride();

    /*
     * Do all array operands share a common stride in the innermost
     * loop?  If so, we can generate more efficient code (but only
     * if this optimization has been enabled).
     */
#ifdef BZ_ARRAY_EXPR_USE_COMMON_STRIDE
    diffType commonStride = expr.suggestStride(maxRank);
    if (iter.suggestStride(maxRank) > commonStride)
        commonStride = iter.suggestStride(maxRank);
    bool useCommonStride = iter.isStride(maxRank,commonStride)
        && expr.isStride(maxRank,commonStride);

#ifdef BZ_DEBUG_TRAVERSE
    BZ_DEBUG_MESSAGE("BZ_ARRAY_EXPR_USE_COMMON_STRIDE" << endl
        << "commonStride = " << commonStride << " useCommonStride = "
        << useCommonStride);
#endif

#else
    const diffType commonStride = 1;
    const bool useCommonStride = false;
#endif

    /*
     * The "last" array contains a pointer to the last element
     * encountered in each "loop".
     */
    const T_numtype* last[T_dest::rank_];

    // Set up the initial state of the "last" array
    for (i=1; i < N_rank; ++i)
        last[i] = iter.data() + dest.length(dest.ordering(i)) * dest.stride(dest.ordering(i));

    diffType lastLength = dest.length(maxRank);
    int firstNoncollapsedLoop = 1;

#ifdef BZ_COLLAPSE_LOOPS

    /*
     * This bit of code handles collapsing loops.  When possible,
     * the N nested loops are converted into a single loop (basically,
     * the N-dimensional array is treated as a long vector).
     * This is important for cases where the length of the innermost
     * loop is very small, for example a 100x100x3 array.
     * If this code can't collapse all the loops into a single loop,
     * it will collapse as many loops as possible starting from the
     * innermost and working out.
     */

    // Collapse loops when possible
    for (i=1; i < N_rank; ++i)
    {
        // Figure out which pair of loops we are considering combining.
        int outerLoopRank = iter.ordering(i);
        int innerLoopRank = iter.ordering(i-1);

        /*
         * The canCollapse() routines look at the strides and extents
         * of the loops, and determine if they can be combined into
         * one loop.
         */

        if (iter.canCollapse(outerLoopRank,innerLoopRank) 
          && expr.canCollapse(outerLoopRank,innerLoopRank))
        {
#ifdef BZ_DEBUG_TRAVERSE
            cout << "Collapsing " << outerLoopRank << " and " 
                 << innerLoopRank << endl;
#endif
            lastLength *= dest.length(outerLoopRank);
            firstNoncollapsedLoop = i+1;
        }
        else  
            break;
    }

#endif // BZ_COLLAPSE_LOOPS

    /*
     * Now we actually perform the loops.  This while loop contains
     * two parts: first, the innermost loop is performed.  Then we
     * exit the loop, and pop our way down the stack until we find
     * a loop that isn't completed.  We then restart the inner loops
     * and push them onto the stack.
     */

    while (true) {

        /*
         * This bit of code handles the innermost loop.  It just uses
         * the separate evaluation functions depeding on the
         * stride. */

      diffType ubound = lastLength * commonStride;
      
      if (useUnitStride || useCommonStride) {
	if(useUnitStride)
	  _bz_evaluateWithUnitStride(dest, iter, expr, ubound, T_update());
	else 
	  _bz_evaluateWithCommonStride(dest, iter, expr, ubound, commonStride,
				       T_update());

	/*
	 * Tidy up for the fact that we haven't actually been
	 * incrementing the iterators in the innermost loop, by
	 * faking it afterward.
	 */
	iter.advance(lastLength * commonStride);
	expr.advance(lastLength * commonStride);
      }
      else {
            /*
             * We don't have a unit stride or common stride in the innermost
             * loop.  This is going to hurt performance.  Luckily 95% of
             * the time, we hit the cases above.
             */
            T_numtype * restrict end = const_cast<T_numtype*>(iter.data())
                + lastLength * dest.stride(maxRank);

            while (iter.data() != end)
            {
	      T_update::update(*const_cast<T_numtype*>(iter.data()), *expr);
                iter.advance();
                expr.advance();
            }
        }


        /*
         * We just finished the innermost loop.  Now we pop our way down
         * the stack, until we hit a loop that hasn't completed yet.
         */ 
        int j = firstNoncollapsedLoop;
        for (; j < N_rank; ++j)
        {
            // Get the next loop
            int r = dest.ordering(j);

            // Pop-- this restores the data pointers to the first element
            // encountered in the loop.
            iter.pop(j);
            expr.pop(j);

            // Load the stride associated with this loop, and increment
            // once.
            iter.loadStride(r);
            expr.loadStride(r);
            iter.advance();
            expr.advance();

            // If we aren't at the end of this loop, then stop popping.
            if (iter.data() != last[j])
                break;
        }

        // Are we completely done?
        if (j == N_rank)
            break;

        // No, so push all the inner loops back onto the stack.
        for (; j >= firstNoncollapsedLoop; --j)
        {
            int r2 = dest.ordering(j-1);
            iter.push(j);
            expr.push(j);
            last[j-1] = iter.data() + dest.length(r2) * dest.stride(r2);
        }

        // Load the stride for the innermost loop again.
        iter.loadStride(maxRank);
        expr.loadStride(maxRank);
    }
}


template<typename T_dest, typename T_expr, typename T_update>
_bz_forceinline void
_bz_evaluator<1>::
evaluateWithIndexTraversal(T_dest& dest, T_expr expr, T_update)
{
  typedef typename T_dest::T_numtype T_numtype;

  TinyVector<int,T_dest::rank_> index;

  if (dest.stride(firstRank) == 1) {
    T_numtype * restrict iter = dest.data();
    int last = dest.ubound(firstRank);
    
    for (index[0] = dest.lbound(firstRank); index[0] <= last;
	 ++index[0]) {
      T_update::update(*iter++, expr(index));
    }
  }
  else {
    typename T_dest::T_iterator iter(dest);
    iter.loadStride(0);
    int last = iter.ubound(firstRank);
    
    for (index[0] = iter.lbound(firstRank); index[0] <= last;
	 ++index[0]) {
      T_update::update(*const_cast<T_numtype*>(iter.data()), 
		       expr(index));
      iter.advance();
    }
  }
}

  template<int N>
template<typename T_dest, typename T_expr, typename T_update>
_bz_forceinline void
_bz_evaluator<N>::
evaluateWithIndexTraversal(T_dest& dest, T_expr expr, T_update)
{
  typedef typename T_dest::T_numtype T_numtype;
  const int N_rank = T_dest::rank();

    // Do a stack-type traversal for the destination array and use
    // index traversal for the source expression
   
    const int maxRank = dest.ordering(0);

#ifdef BZ_DEBUG_TRAVERSE
    const int secondLastRank = dest.ordering(1);
    cout << "Index traversal: N_rank = " << N_rank << endl;
    cout << "maxRank = " << maxRank << " secondLastRank = " << secondLastRank
         << endl;
    cout.flush();
#endif

    typename T_dest::T_iterator iter(dest);
    for (int i=1; i < N_rank; ++i)
        iter.push(iter.ordering(i));

    iter.loadStride(maxRank);

    TinyVector<int,T_dest::rank_> index, last;

    index = dest.base();

    for (int i=0; i < N_rank; ++i)
      last(i) = dest.base(i) + dest.length(i);

    // int lastLength = length(maxRank);

    while (true) {

        for (index[maxRank] = dest.base(maxRank); 
             index[maxRank] < last[maxRank]; 
             ++index[maxRank])
        {
#ifdef BZ_DEBUG_TRAVERSE
#if 0
    cout << "(" << index[0] << "," << index[1] << ") " << endl;
    cout.flush();
#endif
#endif

            T_update::update(*const_cast<T_numtype*>(iter.data()), expr(index));
            iter.advance();
        }

        int j = 1;
        for (; j < N_rank; ++j)
        {
            iter.pop(dest.ordering(j));
            iter.loadStride(dest.ordering(j));
            iter.advance();

            index[dest.ordering(j-1)] = dest.base(dest.ordering(j-1));
            ++index[dest.ordering(j)];
            if (index[dest.ordering(j)] != last[dest.ordering(j)])
                break;
        }

        if (j == N_rank)
            break;

        for (; j > 0; --j)
        {
            iter.push(dest.ordering(j));
        }
        iter.loadStride(maxRank);
    }
}

// Fast traversals require <set> from the ISO/ANSI C++ standard library

#ifdef BZ_HAVE_STD
#ifdef BZ_ARRAY_SPACE_FILLING_TRAVERSAL

template<typename T_dest, typename T_expr, typename T_update>
_bz_forceinline void
_bz_evaluateWithFastTraversal(T_dest& dest,     
			      const TraversalOrder<T_dest::rank() - 1>& order, 
			      T_expr expr, T_update)
{
  typedef typename T_dest::T_numtype T_numtype;
  const int N_rank = T_dest::rank();

    const int maxRank = dest.ordering(0);

#ifdef BZ_DEBUG_TRAVERSE
    const int secondLastRank = dest.ordering(1);
    cerr << "maxRank = " << maxRank << " secondLastRank = " << secondLastRank
         << endl;
#endif

    T_dest::T_iterator iter(dest);
    iter.push(0);
    expr.push(0);

    bool useUnitStride = iter.isUnitStride(maxRank) 
                          && expr.isUnitStride(maxRank);

#ifdef BZ_ARRAY_EXPR_USE_COMMON_STRIDE
    diffType commonStride = expr.suggestStride(maxRank);
    if (iter.suggestStride(maxRank) > commonStride)
        commonStride = iter.suggestStride(maxRank);
    bool useCommonStride = iter.isStride(maxRank,commonStride)
        && expr.isStride(maxRank,commonStride);
#else
    diffType commonStride = 1;
    bool useCommonStride = false;
#endif

    int lastLength = dest.length(maxRank);

    for (int i=0; i < order.length(); ++i)
    {
        iter.pop(0);
        expr.pop(0);

#ifdef BZ_DEBUG_TRAVERSE
    cerr << "Traversing: " << order[i] << endl;
#endif
        // Position the iterator at the start of the next column       
        for (int j=1; j < N_rank; ++j)
        {
            iter.loadStride(ordering(j));
            expr.loadStride(ordering(j));

            int offset = order[i][j-1];
            iter.advance(offset);
            expr.advance(offset);
        }

        iter.loadStride(maxRank);
        expr.loadStride(maxRank);

        // Evaluate the expression along the column

        if ((useUnitStride) || (useCommonStride))
        {
#ifdef BZ_USE_FAST_READ_ARRAY_EXPR
            diffType ubound = lastLength * commonStride;
            T_numtype* restrict data = const_cast<T_numtype*>(iter.data());

            if (commonStride == 1)
            {            
 #ifndef BZ_ARRAY_FAST_TRAVERSAL_UNROLL
                for (diffType i=0; i < ubound; ++i)
                    T_update::update(*data++, expr.fastRead(i));
 #else
                diffType n1 = ubound & 3;
                diffType i=0;
                for (; i < n1; ++i)
                    T_update::update(*data++, expr.fastRead(i));

                for (; i < ubound; i += 4)
                {
                    T_update::update(*data++, expr.fastRead(i));
                    T_update::update(*data++, expr.fastRead(i+1));
                    T_update::update(*data++, expr.fastRead(i+2));
                    T_update::update(*data++, expr.fastRead(i+3));
                }
 #endif  // BZ_ARRAY_FAST_TRAVERSAL_UNROLL
            }
 #ifdef BZ_ARRAY_EXPR_USE_COMMON_STRIDE
            else {
                for (diffType i=0; i < ubound; i += commonStride)
                    T_update::update(data[i], expr.fastRead(i));
            }
 #endif // BZ_ARRAY_EXPR_USE_COMMON_STRIDE

            iter.advance(lastLength * commonStride);
            expr.advance(lastLength * commonStride);
#else   // ! BZ_USE_FAST_READ_ARRAY_EXPR
            T_numtype* restrict last = const_cast<T_numtype*>(iter.data()) 
                + lastLength * commonStride;

            while (iter.data() != last)
            {
                T_update::update(*const_cast<T_numtype*>(iter.data()), *expr);
                iter.advance(commonStride);
                expr.advance(commonStride);
            }
#endif  // BZ_USE_FAST_READ_ARRAY_EXPR

        }
        else {
            // No common stride

            T_numtype* restrict last = const_cast<T_numtype*>(iter.data()) 
                + lastLength * stride(maxRank);

            while (iter.data() != last)
            {
                T_update::update(*const_cast<T_numtype*>(iter.data()), *expr);
                iter.advance();
                expr.advance();
            }
        }
    }
}

#endif // BZ_ARRAY_SPACE_FILLING_TRAVERSAL
#endif // BZ_HAVE_STD

#ifdef BZ_ARRAY_2D_NEW_STENCIL_TILING

#ifdef BZ_ARRAY_2D_STENCIL_TILING

// what is diff between new and old?
template<typename T_dest, typename T_expr, typename T_update>
_bz_forceinline void
_bz_evaluateWithTiled2DTraversal(T_dest& dest, T_expr expr, T_update)
{
  typedef typename T_dest::T_numtype T_numtype;
  const int N_rank = T_dest::rank();

  typename T_dest::T_iterator iter(dest);

    const int minorRank = iter.ordering(0);
    const int majorRank = iter.ordering(1);

    iter.push(0);
    expr.push(0);

#ifdef BZ_2D_STENCIL_DEBUG
    int count = 0;
#endif

    bool useUnitStride = iter.isUnitStride(minorRank)
                          && expr.isUnitStride(minorRank);

#ifdef BZ_ARRAY_EXPR_USE_COMMON_STRIDE
    diffType commonStride = expr.suggestStride(minorRank);
    if (iter.suggestStride(minorRank) > commonStride)
        commonStride = iter.suggestStride(minorRank);
    bool useCommonStride = iter.isStride(minorRank,commonStride)
        && expr.isStride(minorRank,commonStride);
#else
    diffType commonStride = 1;
    bool useCommonStride = false;
#endif

    // Determine if a common major stride exists
    diffType commonMajorStride = expr.suggestStride(majorRank);
    if (iter.suggestStride(majorRank) > commonMajorStride)
        commonMajorStride = iter.suggestStride(majorRank);
    bool haveCommonMajorStride = iter.isStride(majorRank,commonMajorStride)
        && expr.isStride(majorRank,commonMajorStride);


    int maxi = dest.length(majorRank);
    int maxj = dest.length(minorRank);

    const int tileHeight = 16, tileWidth = 3;

    int bi, bj;
    for (bi=0; bi < maxi; bi += tileHeight)
    {
        int ni = bi + tileHeight;
        if (ni > maxi)
            ni = maxi;

        // Move back to the beginning of the array
        iter.pop(0);
        expr.pop(0);

        // Move to the start of this tile row
        iter.loadStride(majorRank);
        iter.advance(bi);
        expr.loadStride(majorRank);
        expr.advance(bi);

        // Save this position
        iter.push(1);
        expr.push(1);

        for (bj=0; bj < maxj; bj += tileWidth)
        {
            // Move to the beginning of the tile row
            iter.pop(1);
            expr.pop(1);

            // Move to the top of the current tile (bi,bj)
            iter.loadStride(minorRank);
            iter.advance(bj);
            expr.loadStride(minorRank);
            expr.advance(bj);

            if (bj + tileWidth <= maxj)
            {
                // Strip mining

                if ((useUnitStride) && (haveCommonMajorStride))
                {
                    diffType offset = 0;
                    T_numtype* restrict data = const_cast<T_numtype*>
                        (iter.data());

                    for (int i=bi; i < ni; ++i)
                    {
                        _bz_typename T_expr::T_numtype tmp1, tmp2, tmp3;

                        // Common subexpression elimination -- compilers
                        // won't necessarily do this on their own.
                        diffType t1 = offset+1;
                        diffType t2 = offset+2;

                        tmp1 = expr.fastRead(offset);
                        tmp2 = expr.fastRead(t1);
                        tmp3 = expr.fastRead(t2);

                        T_update::update(data[0], tmp1);
                        T_update::update(data[1], tmp2);
                        T_update::update(data[2], tmp3);

                        offset += commonMajorStride;
                        data += commonMajorStride;

#ifdef BZ_2D_STENCIL_DEBUG
    count += 3;
#endif
                    }
                }
                else {

                    for (int i=bi; i < ni; ++i)
                    {
                        iter.loadStride(minorRank);
                        expr.loadStride(minorRank);

                        // Loop through current row elements
                        T_update::update(*const_cast<T_numtype*>(iter.data()),
                            *expr);
                        iter.advance();
                        expr.advance();

                        T_update::update(*const_cast<T_numtype*>(iter.data()),
                            *expr);
                        iter.advance();
                        expr.advance();

                        T_update::update(*const_cast<T_numtype*>(iter.data()),
                            *expr);
                        iter.advance(-2);
                        expr.advance(-2);

                        iter.loadStride(majorRank);
                        expr.loadStride(majorRank);
                        iter.advance();
                        expr.advance();

#ifdef BZ_2D_STENCIL_DEBUG
    count += 3;
#endif

                    }
                }
            }
            else {

                // This code handles partial tiles at the bottom of the
                // array.

                for (int j=bj; j < maxj; ++j)
                {
                    iter.loadStride(majorRank);
                    expr.loadStride(majorRank);

                    for (int i=bi; i < ni; ++i)
                    {
                        T_update::update(*const_cast<T_numtype*>(iter.data()),
                            *expr);
                        iter.advance();
                        expr.advance();
#ifdef BZ_2D_STENCIL_DEBUG
    ++count;
#endif

                    }

                    // Move back to the top of this column
                    iter.advance(bi-ni);
                    expr.advance(bi-ni);

                    // Move over to the next column
                    iter.loadStride(minorRank);
                    expr.loadStride(minorRank);

                    iter.advance();
                    expr.advance();
                }
            }
        }
    }

#ifdef BZ_2D_STENCIL_DEBUG
    cout << "BZ_2D_STENCIL_DEBUG: count = " << count << endl;
#endif
}

#endif // BZ_ARRAY_2D_STENCIL_TILING
#endif // BZ_ARRAY_2D_NEW_STENCIL_TILING



#ifndef BZ_ARRAY_2D_NEW_STENCIL_TILING

#ifdef BZ_ARRAY_2D_STENCIL_TILING

// what is diff between new and old?
template<typename T_dest, typename T_expr, typename T_update>
_bz_forceinline void
_bz_evaluateWithTiled2DTraversal(T_dest& dest, T_expr expr, T_update)
{
  typedef typename T_dest::T_numtype T_numtype;

    typename T_dest::T_iterator iter(dest);

    const int minorRank = iter.ordering(0);
    const int majorRank = iter.ordering(1);

    const int blockSize = 16;
    
    iter.push(0);
    expr.push(0);

    bool useUnitStride = iter.isUnitStride(minorRank)
                          && expr.isUnitStride(minorRank);

#ifdef BZ_ARRAY_EXPR_USE_COMMON_STRIDE
    diffType commonStride = expr.suggestStride(minorRank);
    if (iter.suggestStride(minorRank) > commonStride)
        commonStride = iter.suggestStride(minorRank);
    bool useCommonStride = iter.isStride(minorRank,commonStride)
        && expr.isStride(minorRank,commonStride);
#else
    diffType commonStride = 1;
    bool useCommonStride = false;
#endif

    int maxi = dest.length(majorRank);
    int maxj = dest.length(minorRank);

    int bi, bj;
    for (bi=0; bi < maxi; bi += blockSize)
    {
        int ni = bi + blockSize;
        if (ni > maxi)
            ni = maxi;

        for (bj=0; bj < maxj; bj += blockSize)
        {
            int nj = bj + blockSize;
            if (nj > maxj)
                nj = maxj;

            // Move to the beginning of the array
            iter.pop(0);
            expr.pop(0);

            // Move to the beginning of the tile (bi,bj)
            iter.loadStride(majorRank);
            iter.advance(bi);
            iter.loadStride(minorRank);
            iter.advance(bj);

            expr.loadStride(majorRank);
            expr.advance(bi);
            expr.loadStride(minorRank);
            expr.advance(bj);

            // Loop through tile rows
            for (int i=bi; i < ni; ++i)
            {
                // Save the beginning of this tile row
                iter.push(1);
                expr.push(1);

                // Load the minor stride
                iter.loadStride(minorRank);
                expr.loadStride(minorRank);

                if (useUnitStride)
                {
                    T_numtype* restrict data = const_cast<T_numtype*>
                        (iter.data());

                    int ubound = (nj-bj);
                    for (int j=0; j < ubound; ++j)
                        T_update::update(*data++, expr.fastRead(j));
                }
#ifdef BZ_ARRAY_EXPR_USE_COMMON_STRIDE
                else if (useCommonStride)
                {
                    const diffType ubound = (nj-bj) * commonStride;
                    T_numtype* restrict data = const_cast<T_numtype*>
                        (iter.data());

                    for (diffType j=0; j < ubound; j += commonStride)
                        T_update::update(data[j], expr.fastRead(j));
                }
#endif
                else {
                    for (int j=bj; j < nj; ++j)
                    {
                        // Loop through current row elements
                        T_update::update(*const_cast<T_numtype*>(iter.data()), 
					 *expr);
                        iter.advance();
                        expr.advance();
                    }
                }

                // Move back to the beginning of the tile row, then
                // move to the next row
                iter.pop(1);
                iter.loadStride(majorRank);
                iter.advance(1);

                expr.pop(1);
                expr.loadStride(majorRank);
                expr.advance(1);
            }
        }
    }
}
#endif // BZ_ARRAY_2D_STENCIL_TILING
#endif // BZ_ARRAY_2D_NEW_STENCIL_TILING

BZ_NAMESPACE_END

#endif // BZ_ARRAYEVAL_CC