Source

mpi3-fortran / ompi / contrib / vt / vt / extlib / otf / tools / otfprofile / collect_data.cpp

Full commit
   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
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
/*
 This is part of the OTF library. Copyright by ZIH, TU Dresden 2005-2012.
 Authors: Andreas Knuepfer, Robert Dietrich, Matthias Jurenz
*/

#include <cassert>
#include <iostream>
#include <sstream>
#include <stdio.h>
#include <string.h>
#include <assert.h>

#include "otf.h"
#include "otfaux.h"

#include "collect_data.h"
#include "otfprofile.h"


using namespace std;
/*store current callpath for each process  */
map<uint32_t,string> callpathMap;

static void prepare_progress( AllData& alldata, uint64_t max_bytes ) {

    Progress& progress= alldata.progress;

    progress.cur_bytes= 0;
    progress.max_bytes= max_bytes;

#ifdef OTFPROFILE_MPI
    progress.ranks_left= alldata.numRanks -1;

    if ( 1 < alldata.numRanks ) {

        /* reduce max. bytes to rank 0 */
        uint64_t sum_max_bytes;
        MPI_Reduce( &max_bytes, &sum_max_bytes, 1, MPI_LONG_LONG_INT, MPI_SUM,
                    0, MPI_COMM_WORLD );

        if ( 0 == alldata.myRank ) {

            progress.max_bytes= sum_max_bytes;

            progress.recv_buffers= new uint64_t[alldata.numRanks-1];
            assert( progress.recv_buffers );
            progress.recv_requests= new MPI_Request[alldata.numRanks-1];
            assert( progress.recv_requests );
            progress.recv_statuses= new MPI_Status[alldata.numRanks-1];
            assert( progress.recv_statuses );
            progress.recv_indices= new int[alldata.numRanks-1];
            assert( progress.recv_indices );
            progress.rank_cur_bytes= new uint64_t[alldata.numRanks-1];
            assert( progress.rank_cur_bytes );

            /* initialize array of current bytes read and start
            persistent communication */

            for ( uint32_t i= 0; i < alldata.numRanks -1; i++ ) {

                progress.rank_cur_bytes[i]= 0;

                /* create persistent request handle */
                MPI_Recv_init( &(progress.recv_buffers[i]), 1,
                               MPI_LONG_LONG_INT, i+1, Progress::MSG_TAG,
                               MPI_COMM_WORLD,
                               &(progress.recv_requests[i]) );

                /* start persistent communication */
                MPI_Start( &(progress.recv_requests[i]) );

            }

        } else { /* 0 != alldata.myRank */

            /* initialize request handle for sending progress to rank 0 */
            progress.send_request = MPI_REQUEST_NULL;

        }

        /* block until all worker ranks have reached this point to avoid that the
        progress does a big jump at beginning */
        MPI_Barrier( MPI_COMM_WORLD );

    }
#endif /* OTFPROFILE_MPI */

    if ( 0 == alldata.myRank ) {

        /* show initial progress */
        printf( "%7.2f %%\r", 0.0 );
        fflush( stdout );
    }

}


static void update_progress( AllData& alldata, uint64_t delta_bytes,
                bool wait= false ) {

    Progress& progress= alldata.progress;

    progress.cur_bytes += delta_bytes;

    uint64_t sum_cur_bytes= progress.cur_bytes;

#ifdef OTFPROFILE_MPI
    if ( 1 < alldata.numRanks ) {

        if ( 0 == alldata.myRank ) {

            /* get current bytes read from all worker ranks */

            int out_count;
            uint32_t i;

            /* either wait or test for one or more updates from worker ranks */

            if ( wait )
            {

                MPI_Waitsome( alldata.numRanks - 1, progress.recv_requests,
                              &out_count, progress.recv_indices,
                              progress.recv_statuses );

            } else {

                MPI_Testsome( alldata.numRanks - 1, progress.recv_requests,
                              &out_count, progress.recv_indices,
                              progress.recv_statuses );

            }

            if ( MPI_UNDEFINED != out_count ) {

                for ( i= 0; i < (uint32_t) out_count; i++ ) {

                    int index= progress.recv_indices[i];

                    /* worker rank (index+1) is finished? */
                    if ( (uint64_t)-1 == progress.recv_buffers[index] ) {

                        /* this rank is finished */
                        progress.ranks_left--;

                    } else {

                        /* update rank's current bytes read and restart
                        persistent communication */

                        progress.rank_cur_bytes[index]= progress.recv_buffers[index];

                        MPI_Start( &(progress.recv_requests[progress.recv_indices[i]]) );

                    }
                }

            }

            /* recompute sum of current bytes read */
            for( i= 0; i < alldata.numRanks -1; i++ ) {

                sum_cur_bytes += progress.rank_cur_bytes[i];
            }

        } else { /* 0 != alldata.myRank */

            int do_send = 1;
            MPI_Status status;

            /* send only if it's the first send or the request handle isn't
            currently in use */

            if ( MPI_REQUEST_NULL != progress.send_request ) {

                MPI_Test( &(progress.send_request), &do_send, &status );

            }

            if ( do_send ) {

                MPI_Issend( &(progress.cur_bytes), 1, MPI_LONG_LONG_INT, 0,
                            Progress::MSG_TAG, MPI_COMM_WORLD,
                            &progress.send_request );
            }

        }

    }
#endif /* OTFPROFILE_MPI */

    if ( 0 == alldata.myRank ) {

        /* show progress */

        double percent =
            100.0 * (double) sum_cur_bytes / (double) progress.max_bytes;

        static const char signs[2]= { '.',' ' };
        static int signi= 0;

        printf( "%7.2f %% %c\r", percent, signs[signi] );
        fflush( stdout );

        signi^= 1;

    }
}


static void finish_progress( AllData& alldata ) {

#ifdef OTFPROFILE_MPI
    Progress& progress= alldata.progress;

    if ( 1 < alldata.numRanks ) {

        if ( 0 == alldata.myRank ) {

            /* update progress until all worker ranks are
            finished / all bytes are read */

            while ( 0 < progress.ranks_left ) {

                update_progress( alldata, 0, true );
            }

            /* ensure that all requests are inactive before freeing memory */
            MPI_Waitall( alldata.numRanks - 1, progress.recv_requests,
                         progress.recv_statuses );

            /* free memory */
            delete [] progress.recv_buffers;
            delete [] progress.recv_requests;
            delete [] progress.recv_statuses;
            delete [] progress.recv_indices;
            delete [] progress.rank_cur_bytes;

        } else { /* 0 != alldata.myRank */

            MPI_Status status;
            MPI_Wait( &(progress.send_request), &status );

            /* send last current bytes read to rank 0 */
            MPI_Send( &(progress.cur_bytes), 1, MPI_LONG_LONG_INT, 0,
                      Progress::MSG_TAG, MPI_COMM_WORLD );

            /* send marker (-1) to rank 0 which indicates that this worker rank
            is finished */

            progress.cur_bytes = (uint64_t) -1;
            MPI_Send( &(progress.cur_bytes), 1, MPI_LONG_LONG_INT, 0,
                      Progress::MSG_TAG, MPI_COMM_WORLD );

        }

    }
#endif /* OTFPROFILE_MPI */

    if ( 0 == alldata.myRank ) {

        /* show final progress */
        printf( "%7.2f %% done\n", 100.0 );

    }
}


/* definition record handler functions */

static int handle_def_creator( void* fha, uint32_t stream, const char* creator,
               OTF_KeyValueList* kvlist ) {

    AllData* alldata= (AllData*) fha;


    alldata->creator= creator;

    return OTF_RETURN_OK;
}


static int handle_def_version( void* fha, uint32_t stream,
               uint8_t major, uint8_t minor, uint8_t sub, const char* suffix,
               OTF_KeyValueList* kvlist ) {

    AllData* alldata= (AllData*) fha;


    ostringstream version;
    version << (int)major << "." << (int)minor;
    if ( sub > 0 ) {

        version << "." << (int)sub;

    }
    version << suffix;

    alldata->version= version.str();

    return OTF_RETURN_OK;
}


static int handle_def_comment( void* fha, uint32_t stream, const char* comment,
               OTF_KeyValueList* kvlist ) {

    AllData* alldata= (AllData*) fha;


    /* add new-line between each comment record */
    if ( 0 < alldata->comments.length() ) {

        alldata->comments+= "\n";

    }


    /* wrap lines after 80 characters */

    const string::size_type LINE_WRAP= 80;

    string tmp= comment;

    do {

        if ( tmp.length() <= LINE_WRAP ) {

            alldata->comments+= tmp;
            break;

        } else {

            string::size_type next_wrap=
                tmp.find_last_of( " .!?:;,", LINE_WRAP -1 );
            next_wrap= ( string::npos == next_wrap ) ? LINE_WRAP : next_wrap +1;

            alldata->comments+= tmp.substr( 0, next_wrap ) + '\n';
            tmp= tmp.substr( next_wrap );

        }

    } while( 0 != tmp.length() );

    return OTF_RETURN_OK;
}


static int handle_def_timerres( void* fha, uint32_t stream,
               uint64_t ticksPerSecond, OTF_KeyValueList* kvlist ) {

    AllData* alldata= (AllData*) fha;


    alldata->timerResolution= ticksPerSecond;

    return OTF_RETURN_OK;
}


static int handle_def_process( void* fha, uint32_t stream, uint32_t process,
               const char* name, uint32_t parent, OTF_KeyValueList* kvlist ) {

    AllData* alldata= (AllData*) fha;


    alldata->allProcesses.insert( Process( process, parent ) );
    alldata->processIdNameMap[process]= name;

    return OTF_RETURN_OK;
}


static int handle_def_function( void* fha, uint32_t stream, uint32_t function,
               const char* name, uint32_t funcGroup, uint32_t source,
               OTF_KeyValueList* kvlist ) {

    AllData* alldata= (AllData*) fha;


    alldata->functionIdNameMap[function]= name;

    return OTF_RETURN_OK;
}


static int handle_def_collop( void* fha, uint32_t stream, uint32_t collOp,
               const char* name, uint32_t type, OTF_KeyValueList* kvlist ) {

    AllData* alldata= (AllData*) fha;


    alldata->collectiveOperationsToClasses[collOp]= type;

    return OTF_RETURN_OK;
}


static int handle_def_counter( void* fha, uint32_t stream, uint32_t counter,
               const char* name, uint32_t properties, uint32_t counterGroup,
               const char* unit, OTF_KeyValueList* kvlist ) {

    AllData* alldata= (AllData*) fha;


    if ( OTF_COUNTER_TYPE_ACC == ( properties & OTF_COUNTER_TYPE_BITS ) ) {

        alldata->countersOfInterest.insert( counter );

        alldata->counterIdNameMap[counter]= name;

    }

    return OTF_RETURN_OK;
}


static int handle_def_keyvalue( void* fha, uint32_t stream, uint32_t key,
               OTF_Type type, const char* name, const char* description,
               OTF_KeyValueList* kvlist ) {

    AllData* alldata= (AllData*) fha;


    if ( 0 == strcmp( name, OTFAUX_KEYVALUE_TUD_P2P_RECEIVED_TIME_NAME ) ) {

        alldata->recvTimeKey= key;

    }

    return OTF_RETURN_OK;
}


/* event record handler functions */

static int handle_enter( void* fha, uint64_t time, uint32_t function,
               uint32_t process, uint32_t source, OTF_KeyValueList* kvlist ) {

    AllData* alldata= (AllData*) fha;


    list<StackType>& stack= alldata->stackPerProcess[ process ];
    stack.push_back( StackType( function, time ) );

    if (alldata->params.dispersion.mode == DISPERSION_MODE_PERCALLPATH)
    {
        /* store current callpath */
        std::ostringstream os;
        os << " "<<function;
        callpathMap[process] += os.str();
        /* save maximum length, for buffer allocation in reduce_data.cpp*/
        if(alldata->maxCallpathLength < callpathMap[process].length())
            alldata->maxCallpathLength = callpathMap[process].length();
    }
    
    return OTF_RETURN_OK;
}


static int handle_leave( void* fha, uint64_t time, uint32_t function,
               uint32_t process, uint32_t source, OTF_KeyValueList* kvlist ) {

    AllData* alldata= (AllData*) fha;


    list<StackType>& stack= alldata->stackPerProcess[ process ];
    assert( !stack.empty() );

    StackType& top= stack.back();
    list<StackType>::reverse_iterator parent_it= ++stack.rbegin();

    uint64_t func= top.fid;
    uint64_t incl= time - top.timestamp;
    uint64_t excl= incl - top.childDuration;

    if ( parent_it != stack.rend() ) {

        parent_it->childDuration += incl;

    }


    for ( map< uint64_t, StackType::CounterData >::const_iterator it=
          top.counterIdDataMap.begin( );
          it != top.counterIdDataMap.end( ); it++ ) {

        const uint64_t& counter= it->first;
        const uint64_t& firstvalue= it->second.firstValue;
        const uint64_t& lastvalue= it->second.lastValue;
        const uint64_t& lasttime= it->second.lastTime;

        if ( lasttime == time && firstvalue != (uint64_t)-1 &&
             lastvalue != (uint64_t)-1 ) {

            uint64_t counter_incl= lastvalue - firstvalue;
            uint64_t counter_excl= counter_incl - it->second.childDelta;

            alldata->counterMapPerFunctionRank[ Triple( process, func, counter ) ]
                .add( 1, counter_excl, counter_incl );

            if ( parent_it != stack.rend() ) {

                parent_it->counterIdDataMap[ counter ].childDelta+= counter_incl;

            }

        }

    }


    stack.pop_back();

    alldata->functionMapPerRank[ Pair( process, func ) ].add( 1, excl, incl );

    if(alldata->params.dispersion.mode == DISPERSION_MODE_PERCALLPATH)
    {
        /* store function by process, callpath and functionId*/
        alldata->functionCallpathMapPerRank[ TripleCallpath( process, callpathMap[process],func ) ].add( 1, excl, incl );
        alldata->functionCallpathMapPerRank[ TripleCallpath( process, callpathMap[process],func ) ].callpath = callpathMap[process];
        /* reduce callpath step at leave */
        callpathMap[process] = callpathMap[process].substr (0,callpathMap[process].find_last_of(" "));
    }

    return OTF_RETURN_OK;
}


static int handle_counter( void* fha, uint64_t time, uint32_t process,
               uint32_t counter, uint64_t value, OTF_KeyValueList* kvlist ) {

    AllData* alldata= (AllData*) fha;


    list<StackType>& stack= alldata->stackPerProcess[ process ];

    if ( stack.empty( ) ) {

        return OTF_RETURN_OK;

    }

    if ( alldata->countersOfInterest.find( counter ) ==
         alldata->countersOfInterest.end( ) ) {

        return OTF_RETURN_OK;

    }

    StackType& top= stack.back( );

    if ( time == top.timestamp ) {

        top.counterIdDataMap[ counter ].firstValue= value;

    } else {

        map< uint64_t, StackType::CounterData >::iterator it=
            top.counterIdDataMap.find( counter );

        if ( it != top.counterIdDataMap.end() ) {

            StackType::CounterData& top_counter= it->second;
            top_counter.lastValue= value;
            top_counter.lastTime= time;

        }
    }

    return OTF_RETURN_OK;
}


static int handle_send( void* fha, uint64_t time, uint32_t sender,
               uint32_t receiver, uint32_t group, uint32_t type,
               uint32_t length, uint32_t source, OTF_KeyValueList* kvlist ) {

    AllData* alldata= (AllData*) fha;


    double duration= 0.0;

    /* get matching receive time from key-values, if available */

    if ( 0 != alldata->recvTimeKey ) {

        uint64_t recv_time;
        if ( 0 == OTF_KeyValueList_getUint64( kvlist, alldata->recvTimeKey,
                 &recv_time ) ) {

            /* ignore "backward-running" messages */
            if( recv_time > time ) {

                duration= (double) ( recv_time - time );

            }

        }
    }

    alldata->messageMapPerRankPair[ Pair(sender, receiver) ]
        .add_send( 1, length, duration );
    alldata->messageMapPerRank[ sender ].add_send( 1, length, duration );

    /* get message speed */

    if ( length > 0 && duration > 0.0 ) {

        uint64_t speed_bin=
            Logi( (uint64_t)(
                  ( (double)length * (double)alldata->timerResolution ) /
                  duration ), MessageSpeedData::BIN_LOG_BASE );

        uint64_t length_bin= Logi( length, MessageSpeedData::BIN_LOG_BASE );

        alldata->messageSpeedMapPerLength[ Pair( speed_bin, length_bin ) ]
            .add( 1 );

    }

    return OTF_RETURN_OK;
}


static int handle_recv( void* fha, uint64_t time, uint32_t receiver,
               uint32_t sender, uint32_t group, uint32_t type, uint32_t length,
               uint32_t source, OTF_KeyValueList* kvlist ) {

    AllData* alldata= (AllData*) fha;


    /* duration will never be available at receive event */
    double duration= 0.0;

    alldata->messageMapPerRankPair[ Pair(receiver, sender) ]
        .add_recv( 1, length, duration );
    alldata->messageMapPerRank[ receiver ].add_recv( 1, length, duration );

    return OTF_RETURN_OK;
}

static int handle_begin_collop( void* fha, uint64_t time, uint32_t process,
               uint32_t collOp, uint64_t matchingId, uint32_t procGroup,
               uint32_t rootProc, uint64_t sent, uint64_t received,
               uint32_t scltoken, OTF_KeyValueList* kvlist ) {

    AllData* alldata= (AllData*) fha;


    alldata->pendingCollectives[ Pair( matchingId, process ) ]=
        PendingCollective( collOp, sent, received, time );

    return OTF_RETURN_OK;
}

static int handle_end_collop( void* fha, uint64_t time, uint32_t process,
               uint64_t matchingId, OTF_KeyValueList* kvlist ) {

    AllData* alldata= (AllData*) fha;


    /* get corresponding pending collective operation */

    map< Pair, PendingCollective, ltPair >::iterator pending_it=
        alldata->pendingCollectives.find( Pair( matchingId, process ) );
    assert( pending_it != alldata->pendingCollectives.end() );

    const PendingCollective& pending= pending_it->second;

    /* get class of collective operation */

    map< uint64_t, uint64_t >::const_iterator op_class_it=
        alldata->collectiveOperationsToClasses.find( pending.collop );
    assert( op_class_it != alldata->collectiveOperationsToClasses.end() );

    const uint64_t& op_class= op_class_it->second;

    /* calculate duration */
    double duration= (double) ( time - pending.begin_time );

    /* add collective operation to statistics */

    if ( OTF_COLLECTIVE_TYPE_BARRIER == op_class ) {

        alldata->collectiveMapPerRank[ Pair( process, op_class ) ]
            .add_send( 1, 0, duration );
        alldata->collectiveMapPerRank[ Pair( process, op_class ) ]
            .add_recv( 1, 0, duration );

    } else {

        if ( 0 < pending.bytes_send ) {

            alldata->collectiveMapPerRank[ Pair( process, op_class ) ]
                .add_send( 1, pending.bytes_send, duration );

        }
        if ( 0 < pending.bytes_recv ) {

            alldata->collectiveMapPerRank[ Pair( process, op_class ) ]
                .add_recv( 1, pending.bytes_recv, duration );

        }

    }

    /* erase processed pending collective operation from map */
    alldata->pendingCollectives.erase( pending_it );

    return OTF_RETURN_OK;
}


static int handle_function_summary( void* fha, uint64_t time, uint32_t func,
               uint32_t process, uint64_t count, uint64_t exclTime,
               uint64_t inclTime, OTF_KeyValueList* kvlist ) {

    AllData* alldata= (AllData*) fha;


    /* add/overwrite function statistics */

    FunctionData tmp;

    tmp.count.cnt = tmp.count.sum = count;
    tmp.count.min = tmp.count.max = 0;

    tmp.excl_time.cnt = count;
    tmp.excl_time.sum = exclTime;
    tmp.excl_time.min = tmp.excl_time.max = 0;

    tmp.incl_time.cnt = count;
    tmp.incl_time.sum = inclTime;
    tmp.incl_time.min = tmp.incl_time.max = 0;

    alldata->functionMapPerRank[ Pair( process, func ) ]= tmp;

    return OTF_RETURN_OK;
}


static int handle_message_summary( void* fha, uint64_t time, uint32_t process,
               uint32_t peer, uint32_t comm, uint32_t type, uint64_t sentNumber,
               uint64_t receivedNumber, uint64_t sentBytes,
               uint64_t receivedBytes, OTF_KeyValueList* kvlist ) {

    AllData* alldata= (AllData*) fha;


    /* do handle this record only if there is a peer and no
    communicator and tag (default behavior of VampirTrace) */
    if ( 0 != peer && 0 == comm && 0 == type ) {

        /* add/overwrite message statistics */

        MessageData tmp;

        if ( 0 < sentNumber ) {

            tmp.count_send.cnt= tmp.count_send.sum= sentNumber;
            tmp.count_send.min= tmp.count_send.max= 0;

            tmp.bytes_send.cnt= sentNumber;
            tmp.bytes_send.sum= sentBytes;
            tmp.bytes_send.min= tmp.bytes_send.max= 0;

        }
        if ( 0 < receivedNumber ) {

            tmp.count_recv.cnt= tmp.count_recv.sum= receivedNumber;
            tmp.count_recv.min= tmp.count_recv.max= 0;

            tmp.bytes_recv.cnt= receivedNumber;
            tmp.bytes_recv.sum= receivedBytes;
            tmp.bytes_recv.min= tmp.bytes_recv.max= 0;

        }

        alldata->messageMapPerRankPair[ Pair(process, peer) ]= tmp;
        alldata->messageMapPerRank[ process ]= tmp;
    }

    return OTF_RETURN_OK;
}


static int handle_collop_summary( void* fha, uint64_t time, uint32_t process,
               uint32_t comm, uint32_t collOp, uint64_t sentNumber,
               uint64_t receivedNumber, uint64_t sentBytes,
               uint64_t receivedBytes, OTF_KeyValueList* kvlist ) {


    AllData* alldata= (AllData*) fha;


    /* do handle this record only if there is a coll.-op and no communicator
    (default behavior of VampirTrace) */
    if ( 0 != collOp && 0 == comm ) {

        /* get class of collective operation */

        map< uint64_t, uint64_t >::const_iterator op_class_it=
            alldata->collectiveOperationsToClasses.find( collOp );
        assert( op_class_it != alldata->collectiveOperationsToClasses.end() );

        const uint64_t& op_class= op_class_it->second;

        /* add/overwrite collective operation statistics */

        CollectiveData tmp;

        if ( 0 < sentNumber ) {

            tmp.count_send.cnt= tmp.count_send.sum= sentNumber;
            tmp.count_send.min= tmp.count_send.max= 0;

            tmp.bytes_send.cnt= sentNumber;
            tmp.bytes_send.sum= sentBytes;
            tmp.bytes_send.min= tmp.bytes_send.max= 0;

        }
        if ( 0 < receivedNumber ) {

            tmp.count_recv.cnt= tmp.count_recv.sum= receivedNumber;
            tmp.count_recv.min= tmp.count_recv.max= 0;

            tmp.bytes_recv.cnt= receivedNumber;
            tmp.bytes_recv.sum= receivedBytes;
            tmp.bytes_recv.min= tmp.bytes_recv.max= 0;

        }

        alldata->collectiveMapPerRank[ Pair( process, op_class ) ]= tmp;

    }

    return OTF_RETURN_OK;
}


static bool read_definitions( AllData& alldata, OTF_Reader* reader ) {

    bool error= false;

    /* open OTF handler array */
    OTF_HandlerArray* handlers= OTF_HandlerArray_open( );
    assert( handlers );

    /* set record handler functions */

    OTF_HandlerArray_setHandler( handlers,
        (OTF_FunctionPointer*) handle_def_creator,
        OTF_DEFCREATOR_RECORD );
    OTF_HandlerArray_setHandler( handlers,
        (OTF_FunctionPointer*) handle_def_version,
        OTF_DEFVERSION_RECORD );
    OTF_HandlerArray_setHandler( handlers,
        (OTF_FunctionPointer*) handle_def_comment,
        OTF_DEFINITIONCOMMENT_RECORD );
    OTF_HandlerArray_setHandler( handlers,
        (OTF_FunctionPointer*) handle_def_timerres,
        OTF_DEFTIMERRESOLUTION_RECORD );
    OTF_HandlerArray_setHandler( handlers,
        (OTF_FunctionPointer*) handle_def_process,
        OTF_DEFPROCESS_RECORD );
    OTF_HandlerArray_setHandler( handlers,
        (OTF_FunctionPointer*) handle_def_function,
        OTF_DEFFUNCTION_RECORD );
    OTF_HandlerArray_setHandler( handlers,
        (OTF_FunctionPointer*) handle_def_collop,
        OTF_DEFCOLLOP_RECORD );
    OTF_HandlerArray_setHandler( handlers,
        (OTF_FunctionPointer*) handle_def_counter,
        OTF_DEFCOUNTER_RECORD );
    OTF_HandlerArray_setHandler( handlers,
        (OTF_FunctionPointer*) handle_def_keyvalue,
        OTF_DEFKEYVALUE_RECORD );

    /* set record handler's first arguments */

    OTF_HandlerArray_setFirstHandlerArg( handlers, &alldata,
        OTF_DEFCREATOR_RECORD );
    OTF_HandlerArray_setFirstHandlerArg( handlers, &alldata,
        OTF_DEFVERSION_RECORD );
    OTF_HandlerArray_setFirstHandlerArg( handlers, &alldata,
        OTF_DEFINITIONCOMMENT_RECORD );
    OTF_HandlerArray_setFirstHandlerArg( handlers, &alldata,
        OTF_DEFTIMERRESOLUTION_RECORD );
    OTF_HandlerArray_setFirstHandlerArg( handlers, &alldata,
        OTF_DEFPROCESS_RECORD );
    OTF_HandlerArray_setFirstHandlerArg( handlers, &alldata,
        OTF_DEFFUNCTION_RECORD );
    OTF_HandlerArray_setFirstHandlerArg( handlers, &alldata,
        OTF_DEFCOLLOP_RECORD );
    OTF_HandlerArray_setFirstHandlerArg( handlers, &alldata,
        OTF_DEFCOUNTER_RECORD );
    OTF_HandlerArray_setFirstHandlerArg( handlers, &alldata,
        OTF_DEFKEYVALUE_RECORD );

    /* read definitions */
    uint64_t read_ret= OTF_Reader_readDefinitions( reader, handlers );
    if ( OTF_READ_ERROR == read_ret ) {

        cerr << "ERROR: Could not read definitions." << endl;
        error= true;

    }

    /* close OTF handler array */
    OTF_HandlerArray_close( handlers );

    return !error;
}


#ifdef OTFPROFILE_MPI
static void share_definitions( AllData& alldata ) {

    assert( 1 < alldata.numRanks );

    char* buffer;
    int buffer_size= 0;
    int buffer_pos= 0;

    /* get size needed to send definitions to workers */

    if ( 0 == alldata.myRank ) {

        int size;

        MPI_Pack_size( 1 + alldata.functionIdNameMap.size() * 2 +
                       1 + alldata.counterIdNameMap.size() * 2 +
                       1 + alldata.collectiveOperationsToClasses.size() * 2 +
                       1 + alldata.countersOfInterest.size() +
                       1 /* timerResolution */ +
                       1 /* recvTimeKey */,
                       MPI_LONG_LONG_INT, MPI_COMM_WORLD, &buffer_size );

        /* functionIdNameMap seconds */
        for ( map< uint64_t, string >::const_iterator it =
              alldata.functionIdNameMap.begin();
              it != alldata.functionIdNameMap.end(); it++ ) {

            MPI_Pack_size( it->second.length() +1, MPI_CHAR,
                           MPI_COMM_WORLD, &size );
            buffer_size += size;

        }

        /* counterIdNameMap seconds */
        for ( map< uint64_t, string >::const_iterator it =
              alldata.counterIdNameMap.begin();
              it != alldata.counterIdNameMap.end(); it++ ) {

            MPI_Pack_size( it->second.length() +1, MPI_CHAR,
                           MPI_COMM_WORLD, &size );
            buffer_size += size;

        }

        /* get size of additional definitions needed for CSV creation */
        if ( alldata.params.create_csv ) {

            /* processIdNameMap.size() + firsts */
            MPI_Pack_size( 1 + alldata.processIdNameMap.size() * 2,
                           MPI_LONG_LONG_INT, MPI_COMM_WORLD, &size );
            buffer_size += size;

            /* processIdNameMap seconds */
            for ( map< uint64_t, string >::const_iterator it =
                  alldata.processIdNameMap.begin();
                  it != alldata.processIdNameMap.end(); it++ ) {

                MPI_Pack_size( it->second.length() +1, MPI_CHAR,
                               MPI_COMM_WORLD, &size );
                buffer_size += size;

            }

        }

    }

    /* broadcast buffer size */
    MPI_Bcast( &buffer_size, 1, MPI_INT, 0, MPI_COMM_WORLD );

    /* allocate buffer */
    buffer= new char[ buffer_size ];
    assert( buffer );

    /* pack definitions to buffer */

    if ( 0 == alldata.myRank ) {

        /* functionIdNameMap.size() */
        uint64_t funcid_name_map_size= alldata.functionIdNameMap.size();
        MPI_Pack( &funcid_name_map_size, 1, MPI_LONG_LONG_INT, buffer,
                  buffer_size, &buffer_pos, MPI_COMM_WORLD );

        /* functionIdNameMap */
        for ( map< uint64_t, string >::const_iterator it =
              alldata.functionIdNameMap.begin();
              it != alldata.functionIdNameMap.end(); it++ ) {

            /* functionIdNameMap.first */
            uint64_t first= it->first;
            MPI_Pack( &first, 1, MPI_LONG_LONG_INT, buffer, buffer_size,
                      &buffer_pos, MPI_COMM_WORLD );

            /* functionIdNameMap.second.length() */
            uint64_t second_length= it->second.length() +1;
            MPI_Pack( &second_length, 1, MPI_LONG_LONG_INT, buffer, buffer_size,
                      &buffer_pos, MPI_COMM_WORLD );

            /* functionIdNameMap.second */
            char* second= strdup( it->second.c_str() );
            assert( second );
            MPI_Pack( second, second_length, MPI_CHAR, buffer, buffer_size,
                      &buffer_pos, MPI_COMM_WORLD );
            free( second );

        }

        /* counterIdNameMap.size() */
        uint64_t cntrid_name_map_size= alldata.counterIdNameMap.size();
        MPI_Pack( &cntrid_name_map_size, 1, MPI_LONG_LONG_INT, buffer,
                  buffer_size, &buffer_pos, MPI_COMM_WORLD );

        /* counterIdNameMap */
        for ( map< uint64_t, string >::const_iterator it =
              alldata.counterIdNameMap.begin();
              it != alldata.counterIdNameMap.end(); it++ ) {

            /* counterIdNameMap.first */
            uint64_t first= it->first;
            MPI_Pack( &first, 1, MPI_LONG_LONG_INT, buffer, buffer_size,
                      &buffer_pos, MPI_COMM_WORLD );

            /* counterIdNameMap.second.length() */
            uint64_t second_length= it->second.length() +1;
            MPI_Pack( &second_length, 1, MPI_LONG_LONG_INT, buffer, buffer_size,
                      &buffer_pos, MPI_COMM_WORLD );

            /* counterIdNameMap.second */
            char* second= strdup( it->second.c_str() );
            assert( second );
            MPI_Pack( second, second_length, MPI_CHAR, buffer, buffer_size,
                      &buffer_pos, MPI_COMM_WORLD );
            free( second );

        }

        /* collectiveOperationsToClasses.size() */
        uint64_t collop_classes_map_size=
            alldata.collectiveOperationsToClasses.size();
        MPI_Pack( &collop_classes_map_size, 1, MPI_LONG_LONG_INT, buffer,
                  buffer_size, &buffer_pos, MPI_COMM_WORLD );

        /* collectiveOperationsToClasses */
        for ( map< uint64_t, uint64_t >::const_iterator it =
              alldata.collectiveOperationsToClasses.begin();
              it != alldata.collectiveOperationsToClasses.end(); it++ ) {

            /* collectiveOperationsToClasses.first */
            uint64_t first= it->first;
            MPI_Pack( &first, 1, MPI_LONG_LONG_INT, buffer, buffer_size,
                      &buffer_pos, MPI_COMM_WORLD );
            /* collectiveOperationsToClasses.second */
            uint64_t second= it->second;
            MPI_Pack( &second, 1, MPI_LONG_LONG_INT, buffer, buffer_size,
                      &buffer_pos, MPI_COMM_WORLD );

        }

        /* countersOfInterest.size() */
        uint64_t counters_size= alldata.countersOfInterest.size();
        MPI_Pack( &counters_size, 1, MPI_LONG_LONG_INT, buffer, buffer_size,
                  &buffer_pos, MPI_COMM_WORLD );

        /* countersOfInterest */
        for ( set< uint64_t >::const_iterator it=
              alldata.countersOfInterest.begin();
              it != alldata.countersOfInterest.end(); it++ ) {

            uint64_t counter= *it;
            MPI_Pack( &counter, 1, MPI_LONG_LONG_INT, buffer, buffer_size,
                      &buffer_pos, MPI_COMM_WORLD );

        }

        /* recvTimeKey */
        MPI_Pack( &(alldata.recvTimeKey), 1, MPI_LONG_LONG_INT, buffer,
                  buffer_size, &buffer_pos, MPI_COMM_WORLD );

        /* timerResolution */
        MPI_Pack( &(alldata.timerResolution), 1, MPI_LONG_LONG_INT, buffer,
                  buffer_size, &buffer_pos, MPI_COMM_WORLD );

        /* pack additional definitions needed for CSV creation */
        if ( alldata.params.create_csv ) {

            /* processIdNameMap.size() */
            uint64_t procid_name_map_size= alldata.processIdNameMap.size();
            MPI_Pack( &procid_name_map_size, 1, MPI_LONG_LONG_INT, buffer,
                      buffer_size, &buffer_pos, MPI_COMM_WORLD );

            /* processIdNameMap */
            for ( map< uint64_t, string >::const_iterator it =
                  alldata.processIdNameMap.begin();
                  it != alldata.processIdNameMap.end(); it++ ) {

                /* processIdNameMap.first */
                uint64_t first= it->first;
                MPI_Pack( &first, 1, MPI_LONG_LONG_INT, buffer, buffer_size,
                          &buffer_pos, MPI_COMM_WORLD );

                /* processIdNameMap.second.length() */
                uint64_t second_length= it->second.length() +1;
                MPI_Pack( &second_length, 1, MPI_LONG_LONG_INT, buffer, buffer_size,
                          &buffer_pos, MPI_COMM_WORLD );

                /* processIdNameMap.second */
                char* second= strdup( it->second.c_str() );
                assert( second );
                MPI_Pack( second, second_length, MPI_CHAR, buffer, buffer_size,
                          &buffer_pos, MPI_COMM_WORLD );
                free( second );

            }

        }

    }

    /* broadcast definitions buffer */
    MPI_Bcast( buffer, buffer_size, MPI_PACKED, 0, MPI_COMM_WORLD );

    /* unpack definitions from buffer */

    if ( 0 != alldata.myRank ) {

        /* functionIdNameMap.size() */
        uint64_t funcid_name_map_size;
        MPI_Unpack( buffer, buffer_size, &buffer_pos,
                    &funcid_name_map_size, 1, MPI_LONG_LONG_INT,
                    MPI_COMM_WORLD );

        /* functionIdNameMap */
        for ( uint64_t i= 0; i < funcid_name_map_size; i++ ) {

            /* functionIdNameMap.first */
            uint64_t first;
            MPI_Unpack( buffer, buffer_size, &buffer_pos, &first, 1,
                        MPI_LONG_LONG_INT, MPI_COMM_WORLD );

            /* functionIdNameMap.second.length() */
            uint64_t second_length;

            MPI_Unpack( buffer, buffer_size, &buffer_pos, &second_length, 1,
                        MPI_LONG_LONG_INT, MPI_COMM_WORLD );

            /* functionIdNameMap.second */
            char* second= new char[ second_length ];
            assert( second );
            MPI_Unpack( buffer, buffer_size, &buffer_pos, second,
                        second_length, MPI_CHAR, MPI_COMM_WORLD );

            alldata.functionIdNameMap[ first ]= second;

            delete [] second;
        }

        /* counterIdNameMap.size() */
        uint64_t cntrid_name_map_size;
        MPI_Unpack( buffer, buffer_size, &buffer_pos,
                    &cntrid_name_map_size, 1, MPI_LONG_LONG_INT,
                    MPI_COMM_WORLD );

        /* counterIdNameMap */
        for ( uint64_t i= 0; i < cntrid_name_map_size; i++ ) {

            /* counterIdNameMap.first */
            uint64_t first;
            MPI_Unpack( buffer, buffer_size, &buffer_pos, &first, 1,
                        MPI_LONG_LONG_INT, MPI_COMM_WORLD );

            /* counterIdNameMap.second.length() */
            uint64_t second_length;

            MPI_Unpack( buffer, buffer_size, &buffer_pos, &second_length, 1,
                        MPI_LONG_LONG_INT, MPI_COMM_WORLD );

            /* counterIdNameMap.second */
            char* second= new char[ second_length ];
            assert( second );
            MPI_Unpack( buffer, buffer_size, &buffer_pos, second,
                        second_length, MPI_CHAR, MPI_COMM_WORLD );

            alldata.counterIdNameMap[ first ]= second;

            delete [] second;
        }

        /* collectiveOperationsToClasses.size() */
        uint64_t collop_classes_map_size;
        MPI_Unpack( buffer, buffer_size, &buffer_pos,
                    &collop_classes_map_size, 1, MPI_LONG_LONG_INT,
                    MPI_COMM_WORLD );

        /* collectiveOperationsToClasses */
        for ( uint64_t i= 0; i < collop_classes_map_size; i++ ) {

            /* collectiveOperationsToClasses.first */
            uint64_t first;
            MPI_Unpack( buffer, buffer_size, &buffer_pos, &first, 1,
                        MPI_LONG_LONG_INT, MPI_COMM_WORLD );
            /* collectiveOperationsToClasses.second */
            uint64_t second;
            MPI_Unpack( buffer, buffer_size, &buffer_pos, &second, 1,
                        MPI_LONG_LONG_INT, MPI_COMM_WORLD );

            alldata.collectiveOperationsToClasses[ first ]= second;

        }

        /* countersOfInterest.size() */
        uint64_t counters_size;
        MPI_Unpack( buffer, buffer_size, &buffer_pos,
                    &counters_size, 1, MPI_LONG_LONG_INT, MPI_COMM_WORLD );

        /* countersOfInterest */
        for ( uint64_t i= 0; i < counters_size; i++ ) {

            uint64_t counter;
            MPI_Unpack( buffer, buffer_size, &buffer_pos, &counter, 1,
                        MPI_LONG_LONG_INT, MPI_COMM_WORLD );

            alldata.countersOfInterest.insert( counter );

        }

        /* recvTimeKey */
        MPI_Unpack( buffer, buffer_size, &buffer_pos, &(alldata.recvTimeKey),
                    1, MPI_LONG_LONG_INT, MPI_COMM_WORLD );

        /* timerResolution */
        MPI_Unpack( buffer, buffer_size, &buffer_pos,
                    &(alldata.timerResolution), 1, MPI_LONG_LONG_INT,
                    MPI_COMM_WORLD );
 
        /* unpack additional definitions needed for CSV creation */
        if ( alldata.params.create_csv ) {

            /* processIdNameMap.size() */
            uint64_t procid_name_map_size;
            MPI_Unpack( buffer, buffer_size, &buffer_pos,
                        &procid_name_map_size, 1, MPI_LONG_LONG_INT,
                        MPI_COMM_WORLD );

            /* processIdNameMap */
            for ( uint64_t i= 0; i < procid_name_map_size; i++ ) {

                /* processIdNameMap.first */
                uint64_t first;
                MPI_Unpack( buffer, buffer_size, &buffer_pos, &first, 1,
                            MPI_LONG_LONG_INT, MPI_COMM_WORLD );

                /* processIdNameMap.second.length() */
                uint64_t second_length;

                MPI_Unpack( buffer, buffer_size, &buffer_pos, &second_length, 1,
                            MPI_LONG_LONG_INT, MPI_COMM_WORLD );

                /* processIdNameMap.second */
                char* second= new char[ second_length ];
                assert( second );
                MPI_Unpack( buffer, buffer_size, &buffer_pos, second,
                            second_length, MPI_CHAR, MPI_COMM_WORLD );

                alldata.processIdNameMap[ first ]= second;

                delete [] second;
            }
        }
    }

    delete[] buffer;
}
#endif /* OTFPROFILE_MPI */


static bool read_events( AllData& alldata, OTF_Reader* reader ) {

    bool error= false;

    /* open OTF handler array */
    OTF_HandlerArray* handlers= OTF_HandlerArray_open( );
    assert( handlers );

    /* set record handler functions */

    OTF_HandlerArray_setHandler( handlers,
        (OTF_FunctionPointer*) handle_enter,
        OTF_ENTER_RECORD );
    OTF_HandlerArray_setHandler( handlers,
        (OTF_FunctionPointer*) handle_leave,
        OTF_LEAVE_RECORD );
    OTF_HandlerArray_setHandler( handlers,
        (OTF_FunctionPointer*) handle_counter,
        OTF_COUNTER_RECORD );
    OTF_HandlerArray_setHandler( handlers,
        (OTF_FunctionPointer*) handle_send,
        OTF_SEND_RECORD );
    OTF_HandlerArray_setHandler( handlers,
        (OTF_FunctionPointer*) handle_recv,
        OTF_RECEIVE_RECORD );
    OTF_HandlerArray_setHandler( handlers,
        (OTF_FunctionPointer*) handle_begin_collop,
        OTF_BEGINCOLLOP_RECORD );
    OTF_HandlerArray_setHandler( handlers,
        (OTF_FunctionPointer*) handle_end_collop,
        OTF_ENDCOLLOP_RECORD );

    /* set record handler's first arguments */

    OTF_HandlerArray_setFirstHandlerArg( handlers, &alldata,
        OTF_ENTER_RECORD );
    OTF_HandlerArray_setFirstHandlerArg( handlers, &alldata,
        OTF_LEAVE_RECORD );
    OTF_HandlerArray_setFirstHandlerArg( handlers, &alldata,
        OTF_COUNTER_RECORD );
    OTF_HandlerArray_setFirstHandlerArg( handlers, &alldata,
        OTF_SEND_RECORD );
    OTF_HandlerArray_setFirstHandlerArg( handlers, &alldata,
        OTF_RECEIVE_RECORD );
    OTF_HandlerArray_setFirstHandlerArg( handlers, &alldata,
        OTF_BEGINCOLLOP_RECORD );
    OTF_HandlerArray_setFirstHandlerArg( handlers, &alldata,
        OTF_ENDCOLLOP_RECORD );

    /* select processes to read */
    OTF_Reader_setProcessStatusAll( reader, 0 );
    for ( uint32_t i= 0; i < alldata.myProcessesNum; i++ ) {

        OTF_Reader_enableProcess( reader, alldata.myProcessesList[ i ] );
    }

    /* prepare progress */
    if ( alldata.params.progress ) {

        OTF_Reader_setRecordLimit( reader, 0 );

        if ( OTF_READ_ERROR != OTF_Reader_readEvents( reader, handlers ) ) {

            uint64_t min, cur, max;

            OTF_Reader_eventBytesProgress( reader, &min, &cur, &max );
            prepare_progress( alldata, max );

        }

        OTF_Reader_setRecordLimit( reader, Progress::EVENTS_RECORD_LIMIT );

    }

    /* read events */

    uint64_t records_read= 0;

    while ( OTF_READ_ERROR !=
            ( records_read= OTF_Reader_readEvents( reader, handlers ) ) ) {

        /* update progress */
        if ( alldata.params.progress ) {

            uint64_t min, cur, max;
            static uint64_t last_cur= 0;

            OTF_Reader_eventBytesProgress( reader, &min, &cur, &max );
            update_progress( alldata, cur - last_cur );

            last_cur = cur;

        }

        /* stop reading if done */
        if ( 0 == records_read )
            break;
    }

    /* show error message if reading failed */
    if ( OTF_READ_ERROR == records_read ) {

        cerr << "ERROR: Could not read events." << endl;
        error= true;

    }

    /* close OTF handler array */
    OTF_HandlerArray_close( handlers );

    return !error;
}


static bool read_statistics( AllData& alldata, OTF_Reader* reader ) {

    bool error= false;

    /* open OTF handler array */
    OTF_HandlerArray* handlers= OTF_HandlerArray_open( );
    assert( handlers );

    /* set record handler functions */

    OTF_HandlerArray_setHandler( handlers,
        (OTF_FunctionPointer*) handle_function_summary,
        OTF_FUNCTIONSUMMARY_RECORD );
    OTF_HandlerArray_setHandler( handlers,
        (OTF_FunctionPointer*) handle_message_summary,
        OTF_MESSAGESUMMARY_RECORD );
    OTF_HandlerArray_setHandler( handlers,
        (OTF_FunctionPointer*) handle_collop_summary,
        OTF_COLLOPSUMMARY_RECORD );

    /* set record handler's first arguments */

    OTF_HandlerArray_setFirstHandlerArg( handlers, &alldata,
        OTF_FUNCTIONSUMMARY_RECORD );
    OTF_HandlerArray_setFirstHandlerArg( handlers, &alldata,
        OTF_MESSAGESUMMARY_RECORD );
    OTF_HandlerArray_setFirstHandlerArg( handlers, &alldata,
        OTF_COLLOPSUMMARY_RECORD );

    /* select processes to read */
    OTF_Reader_setProcessStatusAll( reader, 0 );
    for ( uint32_t i= 0; i < alldata.myProcessesNum; i++ ) {

        OTF_Reader_enableProcess( reader, alldata.myProcessesList[ i ] );
    }

    /* prepare progress */
    if ( alldata.params.progress ) {

        OTF_Reader_setRecordLimit( reader, 0 );

        if ( OTF_READ_ERROR != OTF_Reader_readStatistics( reader, handlers ) ) {

            uint64_t min, cur, max;
            OTF_Reader_statisticBytesProgress( reader, &min, &cur, &max );
            prepare_progress( alldata, max );

        }

        OTF_Reader_setRecordLimit( reader, Progress::STATS_RECORD_LIMIT );

    }

    /* read statistics */

    uint64_t records_read= 0;

    while ( OTF_READ_ERROR !=
            ( records_read= OTF_Reader_readStatistics( reader, handlers ) ) ) {

        /* update progress */
        if ( alldata.params.progress ) {

            uint64_t min, cur, max;
            static uint64_t last_cur= 0;

            OTF_Reader_statisticBytesProgress( reader, &min, &cur, &max );
            update_progress( alldata, cur - last_cur );

            last_cur = cur;

        }

        /* stop reading if done */
        if ( 0 == records_read )
            break;
    }

    /* show error message if reading failed */
    if ( OTF_READ_ERROR == records_read ) {

        cerr << "ERROR: Could not read statistics." << endl;
        error= true;

    }

    /* close OTF handler array */
    OTF_HandlerArray_close( handlers );

    return !error;
}


bool CollectData( AllData& alldata ) {
	alldata.maxCallpathLength = 0;
    bool error= false;

    /* start runtime measurement for collecting data */
    StartMeasurement( alldata, 1, true, "collect data" );

    /* open OTF file manager and reader */

    OTF_FileManager* manager=
        OTF_FileManager_open( alldata.params.max_file_handles );
    assert( manager );

    OTF_Reader* reader=
        OTF_Reader_open( alldata.params.input_file_prefix.c_str(), manager );
    assert( reader );

    do {

        if ( 0 == alldata.myRank ) {

            /* read definitions */

            VerbosePrint( alldata, 1, true, "reading definitions\n" );

            error= !read_definitions( alldata, reader );

        }

#ifdef OTFPROFILE_MPI
        /* broadcast error indicator to workers */
        if ( SyncError( alldata, error, 0 ) ) {

            break;

        }

        /* share definitions needed for reading events to workers */

        if ( 1 < alldata.numRanks ) {

            share_definitions( alldata );

        }
#endif /* OTFPROFILE_MPI */

        /* either read data from events or statistics */

        if ( alldata.params.read_from_stats ) {

            VerbosePrint( alldata, 1, true, "reading statistics\n" );

            error= !read_statistics( alldata, reader );

        } else {

            VerbosePrint( alldata, 1, true, "reading events\n" );

            error= !read_events( alldata, reader );

        }

        /* finish progress */
        if ( alldata.params.progress ) {

            finish_progress( alldata );

        }

#ifdef OTFPROFILE_MPI
        /* synchronize error indicator with workers */
        SyncError( alldata, error );
#endif /* OTFPROFILE_MPI */

    } while( false );

    /* close OTF file manager and reader */

    OTF_Reader_close( reader );
    OTF_FileManager_close( manager );

    if ( !error ) {

        /* stop runtime measurement for collecting data */
        StopMeasurement( alldata, true, "collect data" );

    }

    return !error;
}