[2b666a]: blitz / array / eval.cc  Maximize  Restore  History

Download this file

1225 lines (1017 with data), 39.0 kB

   1
   2
   3
   4
   5
   6
   7
   8
   9
  10
  11
  12
  13
  14
  15
  16
  17
  18
  19
  20
  21
  22
  23
  24
  25
  26
  27
  28
  29
  30
  31
  32
  33
  34
  35
  36
  37
  38
  39
  40
  41
  42
  43
  44
  45
  46
  47
  48
  49
  50
  51
  52
  53
  54
  55
  56
  57
  58
  59
  60
  61
  62
  63
  64
  65
  66
  67
  68
  69
  70
  71
  72
  73
  74
  75
  76
  77
  78
  79
  80
  81
  82
  83
  84
  85
  86
  87
  88
  89
  90
  91
  92
  93
  94
  95
  96
  97
  98
  99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
 282
 283
 284
 285
 286
 287
 288
 289
 290
 291
 292
 293
 294
 295
 296
 297
 298
 299
 300
 301
 302
 303
 304
 305
 306
 307
 308
 309
 310
 311
 312
 313
 314
 315
 316
 317
 318
 319
 320
 321
 322
 323
 324
 325
 326
 327
 328
 329
 330
 331
 332
 333
 334
 335
 336
 337
 338
 339
 340
 341
 342
 343
 344
 345
 346
 347
 348
 349
 350
 351
 352
 353
 354
 355
 356
 357
 358
 359
 360
 361
 362
 363
 364
 365
 366
 367
 368
 369
 370
 371
 372
 373
 374
 375
 376
 377
 378
 379
 380
 381
 382
 383
 384
 385
 386
 387
 388
 389
 390
 391
 392
 393
 394
 395
 396
 397
 398
 399
 400
 401
 402
 403
 404
 405
 406
 407
 408
 409
 410
 411
 412
 413
 414
 415
 416
 417
 418
 419
 420
 421
 422
 423
 424
 425
 426
 427
 428
 429
 430
 431
 432
 433
 434
 435
 436
 437
 438
 439
 440
 441
 442
 443
 444
 445
 446
 447
 448
 449
 450
 451
 452
 453
 454
 455
 456
 457
 458
 459
 460
 461
 462
 463
 464
 465
 466
 467
 468
 469
 470
 471
 472
 473
 474
 475
 476
 477
 478
 479
 480
 481
 482
 483
 484
 485
 486
 487
 488
 489
 490
 491
 492
 493
 494
 495
 496
 497
 498
 499
 500
 501
 502
 503
 504
 505
 506
 507
 508
 509
 510
 511
 512
 513
 514
 515
 516
 517
 518
 519
 520
 521
 522
 523
 524
 525
 526
 527
 528
 529
 530
 531
 532
 533
 534
 535
 536
 537
 538
 539
 540
 541
 542
 543
 544
 545
 546
 547
 548
 549
 550
 551
 552
 553
 554
 555
 556
 557
 558
 559
 560
 561
 562
 563
 564
 565
 566
 567
 568
 569
 570
 571
 572
 573
 574
 575
 576
 577
 578
 579
 580
 581
 582
 583
 584
 585
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
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
#ifndef BZ_ARRAYEVAL_CC
#define BZ_ARRAYEVAL_CC
#ifndef BZ_ARRAY_H
#error <blitz/array/eval.cc> must be included via <blitz/array.h>
#endif
BZ_NAMESPACE(blitz)
/*
* Assign an expression to an array. For performance reasons, there are
* 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.
*
* _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.
*/
// Fast traversals require <set> from the ISO/ANSI C++ standard library
#ifdef BZ_HAVE_STD
#ifdef BZ_ARRAY_SPACE_FILLING_TRAVERSAL
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,
BZ_ETPARM(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,
BZ_ETPARM(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
template<typename T_numtype, int N_rank> template<typename T_expr, typename T_update>
inline Array<T_numtype, N_rank>&
Array<T_numtype, N_rank>::evaluate(T_expr expr,
T_update)
{
// Check that all arrays have the same shape
#ifdef BZ_DEBUG
if (!expr.shapeCheck(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(shape()),
"Shape check failed." << endl << "Expression:");
BZPRECHECK((T_expr::rank == N_rank) || (T_expr::numArrayOperands == 0),
"Assigned rank " << T_expr::rank << " expression to rank "
<< N_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.
*/
if (numElements() == 0)
return *this;
#ifdef BZ_DEBUG_TRAVERSE
cout << "T_expr::numIndexPlaceholders = " << T_expr::numIndexPlaceholders
<< endl;
cout.flush();
#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.
if (N_rank == 1)
return evaluateWithIndexTraversal1(expr, T_update());
else
return evaluateWithIndexTraversalN(expr, T_update());
}
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(*this, expr, T_update()))
return *this;
#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) * length(ordering(0));
if (cacheNeeded > BZ_L1_CACHE_ESTIMATED_SIZE)
return evaluateWithTiled2DTraversal(expr, T_update());
}
#endif
// If fast traversal isn't available or appropriate, then just
// do a stack traversal.
if (N_rank == 1)
return evaluateWithStackTraversal1(expr, T_update());
else
return evaluateWithStackTraversalN(expr, T_update());
}
}
template<typename T_numtype, int N_rank> template<typename T_expr, typename T_update>
inline Array<T_numtype, N_rank>&
Array<T_numtype, N_rank>::evaluateWithStackTraversal1(
T_expr expr, T_update)
{
#ifdef BZ_DEBUG_TRAVERSE
BZ_DEBUG_MESSAGE("Array<" << BZ_DEBUG_TEMPLATE_AS_STRING_LITERAL(T_numtype)
<< ", " << N_rank << ">: Using stack traversal");
#endif
FastArrayIterator<T_numtype, N_rank> iter(*this);
iter.loadStride(firstRank);
expr.loadStride(firstRank);
bool useUnitStride = iter.isUnitStride(firstRank)
&& expr.isUnitStride(firstRank);
#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);
#ifdef BZ_DEBUG_TRAVERSE
BZ_DEBUG_MESSAGE("BZ_ARRAY_EXPR_USE_COMMON_STRIDE:" << endl
<< " commonStride = " << commonStride << " useCommonStride = "
<< useCommonStride);
#endif
#else
diffType commonStride = 1;
bool useCommonStride = false;
#endif
const T_numtype * last = iter.data() + length(firstRank)
* stride(firstRank);
if (useUnitStride || useCommonStride)
{
#ifdef BZ_USE_FAST_READ_ARRAY_EXPR
#ifdef BZ_DEBUG_TRAVERSE
BZ_DEBUG_MESSAGE("BZ_USE_FAST_READ_ARRAY_EXPR with commonStride");
#endif
diffType ubound = length(firstRank) * commonStride;
T_numtype* restrict data = const_cast<T_numtype*>(iter.data());
if (commonStride == 1)
{
#ifndef BZ_ARRAY_STACK_TRAVERSAL_UNROLL
for (int 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)
{
#ifndef BZ_ARRAY_STACK_TRAVERSAL_CSE_AND_ANTIALIAS
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));
#else
const diffType t1 = i+1;
const diffType t2 = i+2;
const diffType t3 = i+3;
_bz_typename T_expr::T_numtype tmp1, tmp2, tmp3, tmp4;
tmp1 = expr.fastRead(i);
tmp2 = expr.fastRead(BZ_NO_PROPAGATE(t1));
tmp3 = expr.fastRead(BZ_NO_PROPAGATE(t2));
tmp4 = expr.fastRead(BZ_NO_PROPAGATE(t3));
T_update::update(*data++, tmp1);
T_update::update(*data++, tmp2);
T_update::update(*data++, tmp3);
T_update::update(*data++, tmp4);
#endif
}
#endif // BZ_ARRAY_STACK_TRAVERSAL_UNROLL
}
#ifdef BZ_ARRAY_EXPR_USE_COMMON_STRIDE
else {
#ifndef BZ_ARRAY_STACK_TRAVERSAL_UNROLL
for (diffType i=0; i != ubound; i += commonStride)
T_update::update(data[i], expr.fastRead(i));
#else
diffType n1 = (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
}
#endif // BZ_ARRAY_EXPR_USE_COMMON_STRIDE
#else // ! BZ_USE_FAST_READ_ARRAY_EXPR
#ifdef BZ_DEBUG_TRAVERSE
BZ_DEBUG_MESSAGE("Common stride, no fast read");
#endif
while (iter.data() != last)
{
T_update::update(*const_cast<T_numtype*>(iter.data()), *expr);
iter.advance(commonStride);
expr.advance(commonStride);
}
#endif
}
else {
while (iter.data() != last)
{
T_update::update(*const_cast<T_numtype*>(iter.data()), *expr);
iter.advance();
expr.advance();
}
}
return *this;
}
template<typename T_numtype, int N_rank> template<typename T_expr, typename T_update>
inline Array<T_numtype, N_rank>&
Array<T_numtype, N_rank>::evaluateWithStackTraversalN(
T_expr expr, T_update)
{
/*
* 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.
*/
const int maxRank = ordering(0);
// const int secondLastRank = ordering(1);
// Create an iterator for the array receiving the result
FastArrayIterator<T_numtype, N_rank> iter(*this);
// 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.
*/
bool useUnitStride = iter.isUnitStride(maxRank)
&& expr.isUnitStride(maxRank);
/*
* 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
diffType commonStride = 1;
bool useCommonStride = false;
#endif
/*
* The "last" array contains a pointer to the last element
* encountered in each "loop".
*/
const T_numtype* last[N_rank];
// Set up the initial state of the "last" array
for (i=1; i < N_rank; ++i)
last[i] = iter.data() + length(ordering(i)) * stride(ordering(i));
int lastLength = 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 = ordering(i);
int innerLoopRank = 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 (canCollapse(outerLoopRank,innerLoopRank)
&& expr.canCollapse(outerLoopRank,innerLoopRank))
{
#ifdef BZ_DEBUG_TRAVERSE
cout << "Collapsing " << outerLoopRank << " and "
<< innerLoopRank << endl;
#endif
lastLength *= 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 would look
* a lot simpler if it weren't for unit stride and common stride
* optimizations; these clutter up the code with multiple versions.
*/
if ((useUnitStride) || (useCommonStride))
{
#ifdef BZ_USE_FAST_READ_ARRAY_EXPR
/*
* The check for BZ_USE_FAST_READ_ARRAY_EXPR can probably
* be taken out. This was put in place while the unit stride/
* common stride optimizations were being implemented and
* tested.
*/
// Calculate the end of the innermost loop
diffType ubound = lastLength * commonStride;
/*
* This is a real kludge. I didn't want to have to write
* a const and non-const version of FastArrayIterator, so I use a
* const iterator and cast away const. This could
* probably be avoided with some trick, but the whole routine
* is ugly, so why bother.
*/
T_numtype* restrict data = const_cast<T_numtype*>(iter.data());
/*
* BZ_NEEDS_WORK-- need to implement optional unrolling.
*/
if (commonStride == 1)
{
for (diffType i=0; i < ubound; ++i)
T_update::update(*data++, expr.fastRead(i));
}
#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
/*
* 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
// !BZ_USE_FAST_READ_ARRAY_EXPR
// This bit of code not really needed; should remove at some
// point, along with the test for BZ_USE_FAST_READ_ARRAY_EXPR
T_numtype * restrict end = const_cast<T_numtype*>(iter.data())
+ lastLength;
while (iter.data() != end)
{
T_update::update(*const_cast<T_numtype*>(iter.data()), *expr);
iter.advance(commonStride);
expr.advance(commonStride);
}
#endif
}
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 * 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 = 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 = ordering(j-1);
iter.push(j);
expr.push(j);
last[j-1] = iter.data() + length(r2) * stride(r2);
}
// Load the stride for the innermost loop again.
iter.loadStride(maxRank);
expr.loadStride(maxRank);
}
return *this;
}
template<typename T_numtype, int N_rank> template<typename T_expr, typename T_update>
inline Array<T_numtype, N_rank>&
Array<T_numtype, N_rank>::evaluateWithIndexTraversal1(
T_expr expr, T_update)
{
TinyVector<int,N_rank> index;
if (stride(firstRank) == 1)
{
T_numtype * restrict iter = data_ + lbound(firstRank);
int last = ubound(firstRank);
for (index[0] = lbound(firstRank); index[0] <= last;
++index[0])
{
T_update::update(*iter++, expr(index));
}
}
else {
FastArrayIterator<T_numtype, N_rank> iter(*this);
iter.loadStride(0);
int last = ubound(firstRank);
for (index[0] = lbound(firstRank); index[0] <= last;
++index[0])
{
T_update::update(*const_cast<T_numtype*>(iter.data()),
expr(index));
iter.advance();
}
}
return *this;
}
template<typename T_numtype, int N_rank> template<typename T_expr, typename T_update>
inline Array<T_numtype, N_rank>&
Array<T_numtype, N_rank>::evaluateWithIndexTraversalN(
T_expr expr, T_update)
{
// Do a stack-type traversal for the destination array and use
// index traversal for the source expression
const int maxRank = ordering(0);
#ifdef BZ_DEBUG_TRAVERSE
const int secondLastRank = ordering(1);
cout << "Index traversal: N_rank = " << N_rank << endl;
cout << "maxRank = " << maxRank << " secondLastRank = " << secondLastRank
<< endl;
cout.flush();
#endif
FastArrayIterator<T_numtype, N_rank> iter(*this);
for (int i=1; i < N_rank; ++i)
iter.push(ordering(i));
iter.loadStride(maxRank);
TinyVector<int,N_rank> index, last;
index = storage_.base();
for (int i=0; i < N_rank; ++i)
last(i) = storage_.base(i) + length_(i);
// int lastLength = length(maxRank);
while (true) {
for (index[maxRank] = 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(ordering(j));
iter.loadStride(ordering(j));
iter.advance();
index[ordering(j-1)] = base(ordering(j-1));
++index[ordering(j)];
if (index[ordering(j)] != last[ordering(j)])
break;
}
if (j == N_rank)
break;
for (; j > 0; --j)
{
iter.push(ordering(j));
}
iter.loadStride(maxRank);
}
return *this;
}
// Fast traversals require <set> from the ISO/ANSI C++ standard library
#ifdef BZ_HAVE_STD
#ifdef BZ_ARRAY_SPACE_FILLING_TRAVERSAL
template<typename T_numtype, int N_rank> template<typename T_expr, typename T_update>
inline Array<T_numtype, N_rank>&
Array<T_numtype, N_rank>::evaluateWithFastTraversal(
const TraversalOrder<N_rank - 1>& order,
T_expr expr,
T_update)
{
const int maxRank = ordering(0);
#ifdef BZ_DEBUG_TRAVERSE
const int secondLastRank = ordering(1);
cerr << "maxRank = " << maxRank << " secondLastRank = " << secondLastRank
<< endl;
#endif
FastArrayIterator<T_numtype, N_rank> iter(*this);
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 = 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();
}
}
}
return *this;
}
#endif // BZ_ARRAY_SPACE_FILLING_TRAVERSAL
#endif // BZ_HAVE_STD
#ifdef BZ_ARRAY_2D_NEW_STENCIL_TILING
#ifdef BZ_ARRAY_2D_STENCIL_TILING
template<typename T_numtype, int N_rank> template<typename T_expr, typename T_update>
inline Array<T_numtype, N_rank>&
Array<T_numtype, N_rank>::evaluateWithTiled2DTraversal(
T_expr expr, T_update)
{
const int minorRank = ordering(0);
const int majorRank = ordering(1);
FastArrayIterator<T_numtype, N_rank> iter(*this);
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 = length(majorRank);
int maxj = 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
return *this;
}
#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
template<typename T_numtype, int N_rank> template<typename T_expr, typename T_update>
inline Array<T_numtype, N_rank>&
Array<T_numtype, N_rank>::evaluateWithTiled2DTraversal(
T_expr expr, T_update)
{
const int minorRank = ordering(0);
const int majorRank = ordering(1);
const int blockSize = 16;
FastArrayIterator<T_numtype, N_rank> iter(*this);
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 = length(majorRank);
int maxj = 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)
{
int ubound = (nj-bj) * commonStride;
T_numtype* restrict data = const_cast<T_numtype*>
(iter.data());
for (int 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);
}
}
}
return *this;
}
#endif // BZ_ARRAY_2D_STENCIL_TILING
#endif // BZ_ARRAY_2D_NEW_STENCIL_TILING
BZ_NAMESPACE_END
#endif // BZ_ARRAYEVAL_CC

Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:

JavaScript is required for this form.





No, thanks