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
|
/*=========================================================================
Program: Visualization Toolkit
Module: TestRandomMomentStatisticsMPI.cxx
Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
All rights reserved.
See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notice for more information.
=========================================================================*/
/*
* Copyright 2011 Sandia Corporation.
* Under the terms of Contract DE-AC04-94AL85000, there is a non-exclusive
* license for use of this work by or on behalf of the
* U.S. Government. Redistribution and use in source and binary forms, with
* or without modification, are permitted provided that this Notice and any
* statement of authorship are reproduced on all copies.
*/
// .SECTION Thanks
// Thanks to Philippe Pebay, David Thompson and Janine Bennett from Sandia National Laboratories
// for implementing this test.
// This class was extended to auto-correlative statistics by Philippe Pebay, Kitware 2013
#include <mpi.h>
#include "vtkDescriptiveStatistics.h"
#include "vtkPAutoCorrelativeStatistics.h"
#include "vtkPDescriptiveStatistics.h"
#include "vtkCorrelativeStatistics.h"
#include "vtkPCorrelativeStatistics.h"
#include "vtkMultiCorrelativeStatistics.h"
#include "vtkPMultiCorrelativeStatistics.h"
#include "vtkPPCAStatistics.h"
#include "vtkDoubleArray.h"
#include "vtkIdTypeArray.h"
#include "vtkInformation.h"
#include "vtkMath.h"
#include "vtkMPIController.h"
#include "vtkMultiBlockDataSet.h"
#include "vtkStdString.h"
#include "vtkTable.h"
#include "vtkTimerLog.h"
#include "vtkVariantArray.h"
#include "vtksys/CommandLineArguments.hxx"
#include <sstream>
namespace
{
struct RandomSampleStatisticsArgs
{
int nVals;
double absTol;
bool skipDescriptive;
bool skipPDescriptive;
bool skipPAutoCorrelative;
bool skipPCorrelative;
bool skipPMultiCorrelative;
bool skipPPCA;
int* retVal;
int ioRank;
};
// This will be called by all processes
void RandomSampleStatistics( vtkMultiProcessController* controller, void* arg )
{
// Get test parameters
RandomSampleStatisticsArgs* args = reinterpret_cast<RandomSampleStatisticsArgs*>( arg );
*(args->retVal) = 0;
// Get MPI communicator
vtkMPICommunicator* com = vtkMPICommunicator::SafeDownCast( controller->GetCommunicator() );
// Get local rank
int myRank = com->GetLocalProcessId();
// Seed random number generator
vtkMath::RandomSeed( static_cast<int>( vtkTimerLog::GetUniversalTime() ) * ( myRank + 1 ) );
// Generate an input table that contains samples of mutually independent random variables
int nUniform = 2;
int nNormal = 2;
int nVariables = nUniform + nNormal;
vtkTable* inputData = vtkTable::New();
vtkDoubleArray* doubleArray[4];
vtkStdString columnNames[] = { "Standard Uniform 0",
"Standard Uniform 1",
"Standard Normal 0",
"Standard Normal 1" };
// Standard uniform samples
for ( int c = 0; c < nUniform; ++ c )
{
doubleArray[c] = vtkDoubleArray::New();
doubleArray[c]->SetNumberOfComponents( 1 );
doubleArray[c]->SetName( columnNames[c] );
double x;
for ( int r = 0; r < args->nVals; ++ r )
{
x = vtkMath::Random();
doubleArray[c]->InsertNextValue( x );
}
inputData->AddColumn( doubleArray[c] );
doubleArray[c]->Delete();
}
// Standard normal samples
for ( int c = nUniform; c < nVariables; ++ c )
{
doubleArray[c] = vtkDoubleArray::New();
doubleArray[c]->SetNumberOfComponents( 1 );
doubleArray[c]->SetName( columnNames[c] );
double x;
for ( int r = 0; r < args->nVals; ++ r )
{
x = vtkMath::Gaussian();
doubleArray[c]->InsertNextValue( x );
}
inputData->AddColumn( doubleArray[c] );
doubleArray[c]->Delete();
}
// Create timer to be used by all tests
vtkTimerLog *timer = vtkTimerLog::New();
// Storage for cross-checking between aggregated serial vs. parallel descriptive statistics
int n2Rows = 2 * nVariables;
double* extrema_agg = new double[n2Rows];
double* extrema_par = new double[n2Rows];
double* cardsAndMeans_agg = new double[n2Rows];
double* cardsAndMeans_par = new double[n2Rows];
// ************************** Serial descriptive Statistics **************************
// Skip serial descriptive statistics if requested
if ( ! args->skipDescriptive )
{
// Synchronize and start clock
com->Barrier();
timer->StartTimer();
// For verification, instantiate a serial descriptive statistics engine and set its ports
vtkDescriptiveStatistics* ds = vtkDescriptiveStatistics::New();
ds->SetInputData( vtkStatisticsAlgorithm::INPUT_DATA, inputData );
// Select all columns
for ( int c = 0; c < nVariables; ++ c )
{
ds->AddColumn( columnNames[c] );
}
// Test (serially) with Learn operation only (this is only to verify parallel statistics)
ds->SetLearnOption( true );
ds->SetDeriveOption( false );
ds->SetAssessOption( false );
ds->SetTestOption( false );
ds->Update();
// Get output data and meta tables
vtkMultiBlockDataSet* outputMetaDS = vtkMultiBlockDataSet::SafeDownCast( ds->GetOutputDataObject( vtkStatisticsAlgorithm::OUTPUT_MODEL ) );
vtkTable* outputPrimary = vtkTable::SafeDownCast( outputMetaDS->GetBlock( 0 ) );
// Collect and aggregate serial cardinalities, extrema, and means
int nRows = outputPrimary->GetNumberOfRows();
// Make sure that the correct number of rows were retrieved
if ( nRows != nVariables )
{
vtkGenericWarningMacro("Incorrect number of retrieved variables: "
<< nRows
<< " <> "
<< nVariables);
*(args->retVal) = 1;
}
// Aggregate serial results
double* extrema_l = new double[n2Rows];
double* cardsAndMeans_l = new double[n2Rows];
double dn;
for ( vtkIdType r = 0; r < nRows; ++ r )
{
dn = outputPrimary->GetValueByName( r, "Cardinality" ).ToDouble();
cardsAndMeans_l[2 * r] = dn;
cardsAndMeans_l[2 * r + 1] = dn * outputPrimary->GetValueByName( r, "Mean" ).ToDouble();
extrema_l[2 * r] = outputPrimary->GetValueByName( r, "Minimum" ).ToDouble();
// Collect -max instead of max so a single reduce op. (minimum) can process both extrema at a time
extrema_l[2 * r + 1] = - outputPrimary->GetValueByName( r, "Maximum" ).ToDouble();
}
// Reduce all extremal values, and gather all cardinalities and means, directly on I/O node
if ( ! com->Reduce( extrema_l,
extrema_agg,
n2Rows,
vtkCommunicator::MIN_OP,
args->ioRank ) )
{
vtkGenericWarningMacro("MPI error: process "
<<myRank
<< "could not reduce extrema. Serial vs. parallel cross-check will be meaningless.");
*(args->retVal) = 1;
}
if ( ! com->Reduce( cardsAndMeans_l,
cardsAndMeans_agg,
n2Rows,
vtkCommunicator::SUM_OP,
args->ioRank ) )
{
vtkGenericWarningMacro("MPI error: process "
<<myRank
<< "could not reduce cardinalities and means. Serial vs. parallel cross-check will be meaningless.");
*(args->retVal) = 1;
}
// Synchronize and stop clock
com->Barrier();
timer->StopTimer();
if ( myRank == args->ioRank )
{
cout << "\n## Completed serial calculations of descriptive statistics:\n"
<< " With partial aggregation calculated on process "
<< args->ioRank
<< "\n"
<< " Wall time: "
<< timer->GetElapsedTime()
<< " sec.\n";
cout << " Calculated the following primary statistics:\n";
for ( vtkIdType r = 0; r < nRows; ++ r )
{
cout << " "
<< outputPrimary->GetColumnName( 0 )
<< "="
<< outputPrimary->GetValue( r, 0 ).ToString()
<< " "
<< "Cardinality"
<< "="
<< cardsAndMeans_agg[2 * r]
<< " "
<< "Minimum"
<< "="
<< extrema_agg[2 * r]
<< " "
<< "Maximum"
<< "="
<< - extrema_agg[2 * r + 1]
<< " "
<< "Mean"
<< "="
<< cardsAndMeans_agg[2 * r + 1] / cardsAndMeans_agg[2 * r]
<< "\n";
}
}
// Clean up
delete [] cardsAndMeans_l;
delete [] extrema_l;
ds->Delete();
} // if ( ! args->skipDescriptive )
else if ( myRank == args->ioRank )
{
cout << "\n## Skipped serial calculations of descriptive statistics.\n";
}
// ************************** Parallel Descriptive Statistics **************************
// Skip parallel descriptive statistics if requested
if ( ! args->skipPDescriptive )
{
// Now on to the actual parallel descriptive engine
// "68-95-99.7 rule" for 1 up to numRuleVal standard deviations
int numRuleVal = 6;
// Reference values
double sigmaRuleVal[] = { 68.2689492137,
95.4499736104,
99.7300203937,
99.9936657516,
99.9999426697,
99.9999998027 };
// Tolerances
double sigmaRuleTol[] = { 1.,
.5,
.1,
.05,
.01,
.005 };
// Synchronize and start clock
com->Barrier();
timer->StartTimer();
// Instantiate a parallel descriptive statistics engine and set its input data
vtkPDescriptiveStatistics* pds = vtkPDescriptiveStatistics::New();
pds->SetInputData( vtkStatisticsAlgorithm::INPUT_DATA, inputData );
// Select all columns
for ( int c = 0; c < nVariables; ++ c )
{
pds->AddColumn( columnNames[c] );
}
// Test (in parallel) with Learn, Derive, and Assess operations turned on
pds->SetLearnOption( true );
pds->SetDeriveOption( true );
pds->SetAssessOption( true );
pds->SetTestOption( false );
pds->SignedDeviationsOff(); // Use unsigned deviations
pds->Update();
// Synchronize and stop clock
com->Barrier();
timer->StopTimer();
// Get output data and meta tables
vtkMultiBlockDataSet* outputMetaDS = vtkMultiBlockDataSet::SafeDownCast( pds->GetOutputDataObject( vtkStatisticsAlgorithm::OUTPUT_MODEL ) );
vtkTable* outputPrimary = vtkTable::SafeDownCast( outputMetaDS->GetBlock( 0 ) );
vtkTable* outputDerived = vtkTable::SafeDownCast( outputMetaDS->GetBlock( 1 ) );
vtkTable* outputData = pds->GetOutput( vtkStatisticsAlgorithm::OUTPUT_DATA );
if ( myRank == args->ioRank )
{
cout << "\n## Completed parallel calculation of descriptive statistics (with assessment):\n"
<< " Total sample size: "
<< outputPrimary->GetValueByName( 0, "Cardinality" ).ToInt()
<< " \n"
<< " Wall time: "
<< timer->GetElapsedTime()
<< " sec.\n";
cout << " Calculated the following primary statistics:\n";
for ( vtkIdType r = 0; r < outputPrimary->GetNumberOfRows(); ++ r )
{
cout << " ";
for ( int i = 0; i < outputPrimary->GetNumberOfColumns(); ++ i )
{
cout << outputPrimary->GetColumnName( i )
<< "="
<< outputPrimary->GetValue( r, i ).ToString()
<< " ";
}
cout << "\n";
// Store cardinalities, extremal and means for cross-verification
double dn = outputPrimary->GetValueByName( r, "Cardinality" ).ToDouble();
cardsAndMeans_par[2 * r] = dn;
cardsAndMeans_par[2 * r + 1] = dn * outputPrimary->GetValueByName( r, "Mean" ).ToDouble();
extrema_par[2 * r] = outputPrimary->GetValueByName( r, "Minimum" ).ToDouble();
extrema_par[2 * r + 1] = - outputPrimary->GetValueByName( r, "Maximum" ).ToDouble();
}
cout << " Calculated the following derived statistics:\n";
for ( vtkIdType r = 0; r < outputDerived->GetNumberOfRows(); ++ r )
{
cout << " ";
for ( int i = 0; i < outputDerived->GetNumberOfColumns(); ++ i )
{
cout << outputDerived->GetColumnName( i )
<< "="
<< outputDerived->GetValue( r, i ).ToString()
<< " ";
}
cout << "\n";
}
}
// Verify that the DISTRIBUTED standard normal samples indeed statisfy the 68-95-99.7 rule
if ( myRank == args->ioRank )
{
cout << "\n## Verifying whether the distributed standard normal samples satisfy the 68-95-99.7 rule:\n";
}
// For each normal variable, count deviations of more than 1, ..., numRuleVal standard deviations from the mean
for ( int c = 0; c < nNormal; ++ c )
{
// Use assessed values (relative deviations) to check distribution
std::ostringstream relDevName;
relDevName << "d(Standard Normal "
<< c
<< ")";
// Verification can be done only if assessed column is present
vtkAbstractArray* relDevArr = outputData->GetColumnByName( relDevName.str().c_str() );
if ( relDevArr )
{
// Assessed column should be an array of doubles
vtkDoubleArray* relDev = vtkArrayDownCast<vtkDoubleArray>( relDevArr );
if ( relDev )
{
// Allocate and initialize counters
int* outsideStdv_l = new int[numRuleVal];
for ( int d = 0; d < numRuleVal; ++ d )
{
outsideStdv_l[d] = 0;
}
// Count outliers
double dev;
int n = outputData->GetNumberOfRows();
for ( vtkIdType r = 0; r < n; ++ r )
{
dev = relDev->GetValue( r );
// Count for all deviations from 1 to numRuleVal
for ( int d = 0; d < numRuleVal; ++ d )
{
if ( dev >= 1. + d )
{
++ outsideStdv_l[d];
}
} //
} // for ( vtkIdType r = 0; r < n; ++ r )
// Sum all local counters
int* outsideStdv_g = new int[numRuleVal];
com->AllReduce( outsideStdv_l,
outsideStdv_g,
numRuleVal,
vtkCommunicator::SUM_OP );
// Print out percentages of sample points within 1, ..., numRuleVal standard deviations from the mean.
if ( myRank == args->ioRank )
{
cout << " "
<< outputData->GetColumnName( nUniform + c )
<< ":\n";
for ( int d = 0; d < numRuleVal; ++ d )
{
double testVal = ( 1. - outsideStdv_g[d] / static_cast<double>( outputPrimary->GetValueByName( 0, "Cardinality" ).ToInt() ) ) * 100.;
cout << " "
<< testVal
<< "% within "
<< d + 1
<< " standard deviation(s) from the mean.\n";
// Test some statistics
if ( fabs ( testVal - sigmaRuleVal[d] ) > sigmaRuleTol[d] )
{
vtkGenericWarningMacro("Incorrect value.");
*(args->retVal) = 1;
}
}
}
// Clean up
delete [] outsideStdv_l;
delete [] outsideStdv_g;
} // if ( relDev )
else
{
vtkGenericWarningMacro("Column "
<< relDevName.str().c_str()
<< " on process "
<< myRank
<< " is not of type double." );
*(args->retVal) = 1;
}
} // if ( relDevArr )
else
{
vtkGenericWarningMacro("No assessment column called "
<< relDevName.str().c_str()
<< " on process "
<< myRank);
*(args->retVal) = 1;
}
} // for ( int c = 0; c < nNormal; ++ c )
// Clean up
pds->Delete();
} // if ( ! args->skipPDescriptive )
else if ( myRank == args->ioRank )
{
cout << "\n## Skipped calculation of parallel descriptive statistics.\n";
}
// Cross-verify aggregated serial vs. parallel results only if both were calculated
if ( ! args->skipDescriptive && ! args->skipPDescriptive )
{
if ( myRank == args->ioRank )
{
cout << "\n## Cross-verifying aggregated serial vs. parallel descriptive statistics (within "
<< args->absTol
<< " absolute tolerance).\n";
for ( int i = 0; i < n2Rows; ++ i )
{
if ( fabs( cardsAndMeans_agg[i] - cardsAndMeans_par[i] ) > args->absTol )
{
vtkGenericWarningMacro("Incorrect value(s) : "
<< cardsAndMeans_agg[i]
<< " <> "
<< cardsAndMeans_par[i]);
*(args->retVal) = 1;
}
if ( extrema_agg[i] != extrema_par[i] )
{
vtkGenericWarningMacro("Incorrect value(s) : "
<< extrema_agg[i]
<< " <> "
<< extrema_par[i]);
*(args->retVal) = 1;
}
} // for ( int i = 0; i < n2Rows; ++ i )
} // if ( myRank == args->ioRank )
} // if ( ! args->skipPDescriptive && ! args->skipPDescriptive )
else if ( myRank == args->ioRank )
{
cout << "\n## Skipped cross-verification of aggregated serial vs. parallel descriptive statistics.\n";
}
// Clean up
delete [] cardsAndMeans_agg;
delete [] cardsAndMeans_par;
delete [] extrema_agg;
delete [] extrema_par;
// ************************** Parallel Auto-Correlative Statistics **************************
// Skip parallel correlative statistics if requested
if ( ! args->skipPAutoCorrelative )
{
// Synchronize and start clock
com->Barrier();
timer->StartTimer();
// Instantiate a parallel auto-correlative statistics engine and set its input
vtkPAutoCorrelativeStatistics* pas = vtkPAutoCorrelativeStatistics::New();
pas->SetInputData( vtkStatisticsAlgorithm::INPUT_DATA, inputData );
// Select all columns
for ( int c = 0; c < nVariables; ++ c )
{
pas->AddColumn( columnNames[c] );
}
// Create input parameter table for the stationary case
vtkIdTypeArray* timeLags = vtkIdTypeArray::New();
timeLags->SetName( "Time Lags" );
timeLags->SetNumberOfTuples( 1 );
timeLags->SetValue( 0, 0 );
vtkTable* paramTable = vtkTable::New();
paramTable->AddColumn( timeLags );
timeLags->Delete();
// Set spatial cardinality
pas->SetSliceCardinality( args->nVals );
// Set parameters for autocorrelation of whole data set with respect to itself
pas->SetInputData( vtkStatisticsAlgorithm::LEARN_PARAMETERS, paramTable );
paramTable->Delete();
// Test (in parallel) with Learn, Derive operations turned on
pas->SetLearnOption( true );
pas->SetDeriveOption( true );
pas->SetAssessOption( false );
pas->SetTestOption( false );
pas->Update();
// Get output data and meta tables
vtkMultiBlockDataSet* outputMetaDS = vtkMultiBlockDataSet::SafeDownCast( pas->GetOutputDataObject( vtkStatisticsAlgorithm::OUTPUT_MODEL ) );
// Synchronize and stop clock
com->Barrier();
timer->StopTimer();
if ( myRank == args->ioRank )
{
cout << "\n## Completed parallel calculation of auto-correlative statistics (without assessment):\n"
<< " Total sample size: "
<< vtkTable::SafeDownCast( outputMetaDS->GetBlock( 0 ) )->GetValueByName( 0, "Cardinality").ToInt()
<< " \n"
<< " Wall time: "
<< timer->GetElapsedTime()
<< " sec.\n";
cout << " Calculated the following statistics:\n";
unsigned int nbm1 = outputMetaDS->GetNumberOfBlocks() - 1;
for ( unsigned int b = 0; b < nbm1; ++ b )
{
const char* tabName = outputMetaDS->GetMetaData( b )->Get( vtkCompositeDataSet::NAME() );
cerr << " "
<< tabName
<< "\n";
vtkTable* outputMeta = vtkTable::SafeDownCast( outputMetaDS->GetBlock( b ) );
for ( vtkIdType r = 0; r < outputMeta->GetNumberOfRows(); ++ r )
{
cout << " ";
for ( int i = 0; i < outputMeta->GetNumberOfColumns(); ++ i )
{
cout << outputMeta->GetColumnName( i )
<< "="
<< outputMeta->GetValue( r, i ).ToString()
<< " ";
}
cout << "\n";
}
}
const char* tabName = outputMetaDS->GetMetaData( nbm1 )->Get( vtkCompositeDataSet::NAME() );
cerr << " "
<< tabName
<< "\n";
vtkTable* outputMeta = vtkTable::SafeDownCast( outputMetaDS->GetBlock( nbm1 ) );
outputMeta->Dump();
}
// Clean up
pas->Delete();
} // if ( ! args->skipPAutoCorrelative )
else if ( myRank == args->ioRank )
{
cout << "\n## Skipped calculation of parallel auto-correlative statistics.\n";
}
// ************************** Parallel Correlative Statistics **************************
// Skip parallel correlative statistics if requested
if ( ! args->skipPCorrelative )
{
// Synchronize and start clock
com->Barrier();
timer->StartTimer();
// Instantiate a parallel correlative statistics engine and set its input
vtkPCorrelativeStatistics* pcs = vtkPCorrelativeStatistics::New();
pcs->SetInputData( vtkStatisticsAlgorithm::INPUT_DATA, inputData );
// Select column pairs (uniform vs. uniform, normal vs. normal)
pcs->AddColumnPair( columnNames[0], columnNames[1] );
pcs->AddColumnPair( columnNames[2], columnNames[3] );
// Test (in parallel) with Learn, Derive operations turned on
pcs->SetLearnOption( true );
pcs->SetDeriveOption( true );
pcs->SetAssessOption( false );
pcs->SetTestOption( false );
pcs->Update();
// Get output data and meta tables
vtkMultiBlockDataSet* outputMetaDS = vtkMultiBlockDataSet::SafeDownCast( pcs->GetOutputDataObject( vtkStatisticsAlgorithm::OUTPUT_MODEL ) );
vtkTable* outputPrimary = vtkTable::SafeDownCast( outputMetaDS->GetBlock( 0 ) );
vtkTable* outputDerived = vtkTable::SafeDownCast( outputMetaDS->GetBlock( 1 ) );
// Synchronize and stop clock
com->Barrier();
timer->StopTimer();
if ( myRank == args->ioRank )
{
cout << "\n## Completed parallel calculation of correlative statistics (with assessment):\n"
<< " Total sample size: "
<< outputPrimary->GetValueByName( 0, "Cardinality" ).ToInt()
<< " \n"
<< " Wall time: "
<< timer->GetElapsedTime()
<< " sec.\n";
cout << " Calculated the following primary statistics:\n";
for ( vtkIdType r = 0; r < outputPrimary->GetNumberOfRows(); ++ r )
{
cout << " ";
for ( int i = 0; i < outputPrimary->GetNumberOfColumns(); ++ i )
{
cout << outputPrimary->GetColumnName( i )
<< "="
<< outputPrimary->GetValue( r, i ).ToString()
<< " ";
}
cout << "\n";
}
cout << " Calculated the following derived statistics:\n";
for ( vtkIdType r = 0; r < outputDerived->GetNumberOfRows(); ++ r )
{
cout << " ";
for ( int i = 0; i < outputDerived->GetNumberOfColumns(); ++ i )
{
cout << outputDerived->GetColumnName( i )
<< "="
<< outputDerived->GetValue( r, i ).ToString()
<< " ";
}
cout << "\n";
}
}
// Clean up
pcs->Delete();
} // if ( ! args->skipPCorrelative )
else if ( myRank == args->ioRank )
{
cout << "\n## Skipped calculation of parallel correlative statistics.\n";
}
// ************************** Parallel Multi-Correlative Statistics **************************
// Skip parallel multi-correlative statistics if requested
if ( ! args->skipPMultiCorrelative )
{
// Synchronize and start clock
com->Barrier();
timer->StartTimer();
// Instantiate a parallel correlative statistics engine and set its ports
vtkPMultiCorrelativeStatistics* pmcs = vtkPMultiCorrelativeStatistics::New();
pmcs->SetInputData( 0, inputData );
// Select column pairs (uniform vs. uniform, normal vs. normal)
pmcs->SetColumnStatus( columnNames[0], true );
pmcs->SetColumnStatus( columnNames[1], true );
pmcs->RequestSelectedColumns();
pmcs->ResetAllColumnStates();
pmcs->SetColumnStatus( columnNames[2], true );
pmcs->SetColumnStatus( columnNames[3], true );
pmcs->RequestSelectedColumns();
pmcs->ResetAllColumnStates();
pmcs->SetColumnStatus( columnNames[0], true );
pmcs->SetColumnStatus( columnNames[1], true );
pmcs->SetColumnStatus( columnNames[2], true );
pmcs->SetColumnStatus( columnNames[3], true );
pmcs->RequestSelectedColumns();
// Test (in parallel) with Learn, Derive, and Assess operations turned on
// Test is not implemented for multi-correlative
pmcs->SetLearnOption( true );
pmcs->SetDeriveOption( true );
pmcs->SetAssessOption( true );
pmcs->SetTestOption( true );
pmcs->Update();
// Get output meta tables
vtkMultiBlockDataSet* outputMetaDS = vtkMultiBlockDataSet::SafeDownCast( pmcs->GetOutputDataObject( vtkStatisticsAlgorithm::OUTPUT_MODEL ) );
// Synchronize and stop clock
com->Barrier();
timer->StopTimer();
if ( myRank == args->ioRank )
{
cout << "\n## Completed parallel calculation of multi-correlative statistics (with assessment):\n"
<< " Total sample size: "
<< vtkTable::SafeDownCast( outputMetaDS->GetBlock( 0 ) )->GetValueByName( 0, "Entries").ToInt()
<< " \n"
<< " Wall time: "
<< timer->GetElapsedTime()
<< " sec.\n";
vtkTable* outputMeta;
for ( unsigned int b = 1; b < outputMetaDS->GetNumberOfBlocks(); ++ b )
{
outputMeta = vtkTable::SafeDownCast( outputMetaDS->GetBlock( b ) );
outputMeta->Dump();
}
}
// Clean up
pmcs->Delete();
} // if ( ! args->skipPMultiCorrelative )
else if ( myRank == args->ioRank )
{
cout << "\n## Skipped calculation of parallel multi-correlative statistics.\n";
}
// ************************** Parallel PCA Statistics **************************
// Skip parallel PCA statistics if requested
if ( ! args->skipPPCA )
{
// Synchronize and start clock
com->Barrier();
timer->StartTimer();
// Instantiate a parallel pca statistics engine and set its ports
vtkPPCAStatistics* pcas = vtkPPCAStatistics::New();
pcas->SetInputData( vtkStatisticsAlgorithm::INPUT_DATA, inputData );
// Select column pairs (uniform vs. uniform, normal vs. normal)
pcas->SetColumnStatus( columnNames[0], true );
pcas->SetColumnStatus( columnNames[1], true );
pcas->RequestSelectedColumns();
pcas->ResetAllColumnStates();
pcas->SetColumnStatus( columnNames[2], true );
pcas->SetColumnStatus( columnNames[3], true );
pcas->RequestSelectedColumns();
pcas->ResetAllColumnStates();
pcas->SetColumnStatus( columnNames[0], true );
pcas->SetColumnStatus( columnNames[1], true );
pcas->SetColumnStatus( columnNames[2], true );
pcas->SetColumnStatus( columnNames[3], true );
pcas->RequestSelectedColumns();
// Test (in parallel) with all operations except for Test (not implemented in parallel for PCA)
pcas->SetLearnOption( true );
pcas->SetDeriveOption( true );
pcas->SetAssessOption( true );
pcas->SetTestOption( false );
pcas->Update();
// Get output meta tables
vtkMultiBlockDataSet* outputMetaDS = vtkMultiBlockDataSet::SafeDownCast( pcas->GetOutputDataObject( vtkStatisticsAlgorithm::OUTPUT_MODEL ) );
// Synchronize and stop clock
com->Barrier();
timer->StopTimer();
if ( myRank == args->ioRank )
{
cout << "\n## Completed parallel calculation of pca statistics (with assessment):\n"
<< " Total sample size: "
<< vtkTable::SafeDownCast( outputMetaDS->GetBlock( 0 ) )->GetValueByName( 0, "Entries").ToInt()
<< " \n"
<< " Wall time: "
<< timer->GetElapsedTime()
<< " sec.\n";
vtkTable* outputMeta;
for ( unsigned int b = 1; b < outputMetaDS->GetNumberOfBlocks(); ++ b )
{
outputMeta = vtkTable::SafeDownCast( outputMetaDS->GetBlock( b ) );
outputMeta->Dump();
}
}
// Clean up
pcas->Delete();
} // if ( ! args->skipPPCA )
else if ( myRank == args->ioRank )
{
cout << "\n## Skipped calculation of parallel PCA statistics.\n";
}
// Clean up
inputData->Delete();
timer->Delete();
}
}
//----------------------------------------------------------------------------
int TestRandomPMomentStatisticsMPI( int argc, char* argv[] )
{
// **************************** MPI Initialization ***************************
vtkMPIController* controller = vtkMPIController::New();
controller->Initialize( &argc, &argv );
// If an MPI controller was not created, terminate in error.
if ( ! controller->IsA( "vtkMPIController" ) )
{
vtkGenericWarningMacro("Failed to initialize a MPI controller.");
controller->Delete();
return 1;
}
vtkMPICommunicator* com = vtkMPICommunicator::SafeDownCast( controller->GetCommunicator() );
// ************************** Find an I/O node ********************************
int* ioPtr;
int ioRank;
int flag;
MPI_Comm_get_attr( MPI_COMM_WORLD,
MPI_IO,
&ioPtr,
&flag );
if ( ( ! flag ) || ( *ioPtr == MPI_PROC_NULL ) )
{
// Getting MPI attributes did not return any I/O node found.
ioRank = MPI_PROC_NULL;
vtkGenericWarningMacro("No MPI I/O nodes found.");
// As no I/O node was found, we need an unambiguous way to report the problem.
// This is the only case when a testValue of -1 will be returned
controller->Finalize();
controller->Delete();
return -1;
}
else
{
if ( *ioPtr == MPI_ANY_SOURCE )
{
// Anyone can do the I/O trick--just pick node 0.
ioRank = 0;
}
else
{
// Only some nodes can do I/O. Make sure everyone agrees on the choice (min).
com->AllReduce( ioPtr,
&ioRank,
1,
vtkCommunicator::MIN_OP );
}
}
// Get local rank and print out of I/O node
int myRank = com->GetLocalProcessId();
if ( myRank == ioRank )
{
cout << "\n# Process "
<< ioRank
<< " will be the I/O node.\n";
}
// Check how many processes have been made available
int numProcs = controller->GetNumberOfProcesses();
if ( myRank == ioRank )
{
cout << "\n# Running test with "
<< numProcs
<< " processes...\n";
}
// **************************** Parse command line ***************************
// Set default argument values
int nVals = 100000;
double absTol = 1.e-6;
bool skipDescriptive = false;
bool skipPDescriptive = false;
bool skipPAutoCorrelative = false;
bool skipPCorrelative = false;
bool skipPMultiCorrelative = false;
bool skipPPCA = false;
// Initialize command line argument parser
vtksys::CommandLineArguments clArgs;
clArgs.Initialize( argc, argv );
clArgs.StoreUnusedArguments( false );
// Parse per-process cardinality of each pseudo-random sample
clArgs.AddArgument("--n-per-proc",
vtksys::CommandLineArguments::SPACE_ARGUMENT,
&nVals, "Per-process cardinality of each pseudo-random sample");
// Parse absolute tolerance to cross-verify aggregated serial vs. parallel descriptive stats
clArgs.AddArgument("--abs-tol",
vtksys::CommandLineArguments::SPACE_ARGUMENT,
&absTol, "Absolute tolerance to cross-verify aggregated serial and parallel descriptive statistics");
// Parse whether serial descriptive statistics should be skipped (for faster testing)
clArgs.AddArgument("--skip-Descriptive",
vtksys::CommandLineArguments::NO_ARGUMENT,
&skipDescriptive, "Skip serial descriptive statistics");
// Parse whether parallel descriptive statistics should be skipped (for faster testing)
clArgs.AddArgument("--skip-PDescriptive",
vtksys::CommandLineArguments::NO_ARGUMENT,
&skipPDescriptive, "Skip parallel descriptive statistics");
// Parse whether parallel auto-correlative statistics should be skipped (for faster testing)
clArgs.AddArgument("--skip-PAutoCorrelative",
vtksys::CommandLineArguments::NO_ARGUMENT,
&skipPAutoCorrelative, "Skip parallel auto-correlative statistics");
// Parse whether parallel correlative statistics should be skipped (for faster testing)
clArgs.AddArgument("--skip-PCorrelative",
vtksys::CommandLineArguments::NO_ARGUMENT,
&skipPCorrelative, "Skip parallel correlative statistics");
// Parse whether parallel multi-correlative statistics should be skipped (for faster testing)
clArgs.AddArgument("--skip-PMultiCorrelative",
vtksys::CommandLineArguments::NO_ARGUMENT,
&skipPMultiCorrelative, "Skip parallel multi-correlative statistics");
// Parse whether parallel PCA statistics should be skipped (for faster testing)
clArgs.AddArgument("--skip-PPCA",
vtksys::CommandLineArguments::NO_ARGUMENT,
&skipPPCA, "Skip parallel PCA statistics");
// If incorrect arguments were provided, provide some help and terminate in error.
if ( ! clArgs.Parse() )
{
if ( myRank == ioRank )
{
cerr << "Usage: "
<< clArgs.GetHelp()
<< "\n";
}
controller->Finalize();
controller->Delete();
return 1;
}
// ************************** Initialize test *********************************
// Parameters for regression test.
int testValue = 0;
RandomSampleStatisticsArgs args;
args.nVals = nVals;
args.absTol = absTol;
args.skipDescriptive = skipDescriptive;
args.skipPDescriptive = skipPDescriptive;
args.skipPAutoCorrelative = skipPAutoCorrelative;
args.skipPCorrelative = skipPCorrelative;
args.skipPMultiCorrelative = skipPMultiCorrelative;
args.skipPPCA = skipPPCA;
args.retVal = &testValue;
args.ioRank = ioRank;
// Execute the function named "process" on both processes
controller->SetSingleMethod( RandomSampleStatistics, &args );
controller->SingleMethodExecute();
// Clean up and exit
if ( com->GetLocalProcessId() == ioRank )
{
cout << "\n# Test completed.\n\n";
}
controller->Finalize();
controller->Delete();
return testValue;
}
|