Epetra Package Browser (Single Doxygen Collection) Development
test/Comm/cxx_main.cpp
Go to the documentation of this file.
00001 //@HEADER
00002 // ************************************************************************
00003 //
00004 //               Epetra: Linear Algebra Services Package
00005 //                 Copyright 2011 Sandia Corporation
00006 //
00007 // Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
00008 // the U.S. Government retains certain rights in this software.
00009 //
00010 // Redistribution and use in source and binary forms, with or without
00011 // modification, are permitted provided that the following conditions are
00012 // met:
00013 //
00014 // 1. Redistributions of source code must retain the above copyright
00015 // notice, this list of conditions and the following disclaimer.
00016 //
00017 // 2. Redistributions in binary form must reproduce the above copyright
00018 // notice, this list of conditions and the following disclaimer in the
00019 // documentation and/or other materials provided with the distribution.
00020 //
00021 // 3. Neither the name of the Corporation nor the names of the
00022 // contributors may be used to endorse or promote products derived from
00023 // this software without specific prior written permission.
00024 //
00025 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
00026 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00027 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
00028 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
00029 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
00030 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00031 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00032 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00033 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00034 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00035 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00036 //
00037 // Questions? Contact Michael A. Heroux (maherou@sandia.gov)
00038 //
00039 // ************************************************************************
00040 //@HEADER
00041 
00042 #include <limits.h>
00043 
00044 // Epetra_Comm Test routine
00045 #include "../epetra_test_err.h"
00046 #include "Epetra_Time.h"
00047 #include "Epetra_Util.h"
00048 #include "Epetra_Distributor.h"
00049 #include "Epetra_SerialComm.h"
00050 #include "Epetra_IntSerialDenseVector.h"
00051 #include "Epetra_Version.h"
00052 
00053 #ifdef EPETRA_MPI
00054 #include <mpi.h>
00055 #include "Epetra_MpiComm.h"
00056 
00057 int checkMpiDataClass(bool verbose);
00058 #endif
00059 
00060 int checkSerialDataClass(bool verbose);
00061 int checkCommMethods(Epetra_Comm& petracomm,
00062                      bool verbose, bool verbose1,
00063                      int& NumProc, int& rank);
00064 int checkRankAndSize(Epetra_Comm& petracomm, bool verbose, int rank, int size);
00065 void checkBarrier(Epetra_Comm& petracomm, bool verbose, int rank);
00066 
00067 int checkDistributor(Epetra_Distributor* distr,
00068                      Epetra_Comm& Comm);
00069 
00070 int main(int argc, char* argv[]) {
00071   bool verbose = false;  // used to set verbose false on non-root processors
00072   bool verbose1 = false; // user's command-line argument
00073   // Check if we should print results to standard out
00074   if (argc>1) if (argv[1][0]=='-' && argv[1][1]=='v') verbose1 = true;
00075 
00076   int ierr = 0;
00077   int returnierr = 0;
00078   int size = 1;
00079   int rank = 0;
00080 
00081   if (verbose1)
00082     cout << Epetra_Version() << endl << endl;
00083 
00084   // Test Epetra_SerialComm
00085   if(verbose1) cout << "Testing Epetra_SerialComm..." << endl;
00086   Epetra_SerialComm serialcomm;
00087   if (verbose1) cout << serialcomm << endl;
00088   ierr = checkRankAndSize(serialcomm, verbose1, rank, size);
00089   EPETRA_TEST_ERR(ierr,returnierr);
00090   if (verbose1 && ierr==0) cout << "Checked OK\n\n" <<endl;
00091   // method testing
00092   int numProc = serialcomm.NumProc();
00093   ierr = checkCommMethods(serialcomm, verbose, verbose1, numProc, rank);
00094   EPETRA_TEST_ERR(ierr,returnierr);
00095   if (verbose1 && ierr==0) cout << "Checked OK\n\n" <<endl;
00096   // clone
00097   if(verbose1) cout << "SerialComm Clone.." << endl;
00098   Epetra_Comm* cloned_serialcomm = serialcomm.Clone();
00099   ierr = checkCommMethods(*cloned_serialcomm, verbose, verbose1, numProc, rank);
00100   delete cloned_serialcomm;
00101   EPETRA_TEST_ERR(ierr,returnierr);
00102   if (verbose1 && ierr==0) cout << "Checked OK\n\n" <<endl;
00103   // check inner data class
00104   ierr = checkSerialDataClass(verbose1);
00105   EPETRA_TEST_ERR(ierr,returnierr);
00106   if (verbose1 && ierr==0) cout << "Checked OK\n\n" <<endl;
00107 
00108   Epetra_Distributor* serialdistr = serialcomm.CreateDistributor();
00109   ierr = checkDistributor(serialdistr, serialcomm);
00110   delete serialdistr;
00111   EPETRA_TEST_ERR(ierr, returnierr);
00112 
00113   // Test Epetra_MpiComm
00114 #ifdef EPETRA_MPI
00115   // Initialize MPI
00116   if(verbose1) cout << "Testing Epetra_MpiComm..." << endl;
00117   MPI_Init(&argc,&argv);
00118   MPI_Comm_size(MPI_COMM_WORLD, &size);
00119   MPI_Comm_rank(MPI_COMM_WORLD, &rank);
00120   Epetra_MpiComm petracomm( MPI_COMM_WORLD );
00121   ierr = checkRankAndSize(petracomm, verbose1, rank, size);
00122   EPETRA_TEST_ERR(ierr,returnierr);
00123   if (verbose1 && ierr==0) cout << "Checked OK\n\n" <<endl;
00124   MPI_Comm MPIComm1 = petracomm.Comm();
00125   int size1, rank1;
00126   MPI_Comm_size(MPIComm1, &size1);
00127   MPI_Comm_rank(MPIComm1, &rank1);
00128   if (verbose1) cout << petracomm <<  ".  Using MPI_Comm from Petra_Comm:"
00129                            << " Processor " << rank1 << " of " << size1
00130                            << " (should be the same)." << endl;
00131   EPETRA_TEST_ERR(!(rank1==rank),ierr);
00132   EPETRA_TEST_ERR(!(size1==size),ierr);
00133   checkBarrier(petracomm, verbose1, rank);
00134 
00135   // method testing
00136   numProc = petracomm.NumProc();
00137   ierr = checkCommMethods(petracomm, verbose, verbose1, numProc, rank);
00138   EPETRA_TEST_ERR(ierr,returnierr);
00139   if (verbose1 && ierr==0) cout << "Checked OK\n\n" <<endl;
00140 
00141   // clone
00142   if(verbose1) cout << "MpiComm Clone.." << endl;
00143   Epetra_Comm* cloned_mpicomm = petracomm.Clone();
00144   ierr = checkCommMethods(*cloned_mpicomm, verbose, verbose1, numProc, rank);
00145   delete cloned_mpicomm;
00146   EPETRA_TEST_ERR(ierr,returnierr);
00147   if (verbose1 && ierr==0) cout << "Checked OK\n\n" <<endl;
00148 
00149   // check inner data class
00150   petracomm.Barrier();
00151   ierr = checkMpiDataClass(verbose1);
00152   EPETRA_TEST_ERR(ierr,returnierr);
00153   if (verbose1 && ierr==0) cout << "Checked OK\n\n" <<endl;
00154 
00155   Epetra_Distributor* plldistr = petracomm.CreateDistributor();
00156   ierr = checkDistributor(plldistr, petracomm);
00157   delete plldistr;
00158   EPETRA_TEST_ERR(ierr, returnierr);
00159 
00160   petracomm.Barrier();
00161   MPI_Finalize();
00162 #endif
00163 
00164   return(returnierr);
00165 }
00166 
00167 //=============================================================================
00168 void checkBarrier(Epetra_Comm& petracomm, bool verbose, int rank) {
00169   // Do some timing to test barrier function
00170   int MyPID = petracomm.MyPID();
00171   Epetra_Time before_barrier(petracomm);
00172   Epetra_Time after_barrier(petracomm);
00173   // Give each processor rank+1 amount of work
00174   // Time before barrier should increase roughly linearly
00175   // Time after barrier should be same for all processors
00176   double sum = 0.0;
00177   for (int j=0; j<rank+1; j++)
00178     for (int i=0; i<1000000; i++)
00179       sum += ((double )rand())/((double) RAND_MAX);
00180   sum /= rank+1;
00181   if (verbose) cout << "Processor " << MyPID
00182                     << " Time to reach barrier: "
00183                     << before_barrier.ElapsedTime() << endl;
00184   petracomm.Barrier();
00185   if (verbose) cout << "Processor " << MyPID << " Sum result  "
00186                     << sum << " Time to get beyond barrier: "
00187                     << after_barrier.ElapsedTime() << endl;
00188 
00189   petracomm.Barrier();
00190 }
00191 
00192 //=============================================================================
00193 int checkRankAndSize(Epetra_Comm& petracomm, bool verbose, int rank, int size) {
00194   int ierr = 0;
00195   //if(verbose) cout << "CRS Breakpoint 1" << endl;
00196   int MyPID = petracomm.MyPID();
00197   //if(verbose) cout << "CRS Breakpoint 2" << endl;
00198   int NumProc = petracomm.NumProc();
00199   EPETRA_TEST_ERR(!(MyPID==rank),ierr);
00200   EPETRA_TEST_ERR(!(NumProc==size),ierr);
00201   petracomm.Barrier();
00202   return(ierr);
00203 }
00204 
00205 //=============================================================================
00206 int checkCommMethods(Epetra_Comm& petracomm, bool verbose, bool verbose1, int& NumProc, int& rank) {
00207   int i,j;
00208   int forierr = 0;
00209   int ierr = 0;
00210 
00211   verbose = (petracomm.MyPID() == 0);  // Turn verbose on;
00212                                        // it is always false in main.
00213 
00214   // Some vars needed for the following tests
00215   int count = 4;
00216   int* iInputs = new int[count]; // General array for int type tests
00217   for (i=0; i<count; i++)
00218     iInputs[i] = 10*(i + rank - 2) + rank;
00219     // if these values are changed, the expected maxs, mins, sums, etc must also change.
00220     //NOTE: Broadcst() does not use these values.  The lines that need to be changed are located
00221     //in the "Values for ****** tests" sections directly below.
00222 
00223   double* dInputs = new double[count]; // General array for double type tests
00224   for (i=0; i<count; i++)
00225     dInputs[i] = pow(2.0,i-rank);
00226     // if these values are changed, the expected maxs, mins, sums, etc must also change.
00227     //NOTE: Broadcst() does not use these values.  The lines that need to be changed are located
00228     //in the "Values for ****** tests" sections directly below.
00229 
00230 
00231   // Values for Broadcast tests
00232   int* iVals = new int[count];
00233   if (rank == 0) {
00234     for (i=0; i<count; i++)
00235        iVals[i] = i; // if these values are changed, the values in iBVals must also be changed
00236   }
00237 
00238   int* iBVals = new int[count]; // Values to be checked against the values broadcast to the non root processors
00239   for (i=0; i<count; i++)
00240     iBVals[i] = i; // if these values are changed, the values in iVals must also be changed
00241   double* dVals = new double[count];
00242   if (rank == 0) {
00243      for (i=0; i<count; i++)
00244        dVals[i] = double(i); // if these values are changed, the values in dBVals must also be changed
00245   }
00246   double* dBVals = new double[count]; // Values to be checked against the values broadcast to the non root processors
00247   for (i=0; i<count; i++)
00248     dBVals[i] = i; // if these values are changed, the values in dVals must also be changed
00249 
00250   long long* llVals = new long long[count];
00251   if (rank == 0) {
00252      for (i=0; i<count; i++)
00253        llVals[i] = (long long)i+INT_MAX; // if these values are changed, the values in llBVals must also be changed
00254   }
00255   long long* llBVals = new long long[count]; // Values to be checked against the values broadcast to the non root processors
00256   for (i=0; i<count; i++)
00257     llBVals[i] = (long long)i+INT_MAX; // if these values are changed, the values in dVals must also be changed
00258 
00259   const char *cConst = "Heidi, do you want a cookie?";
00260   int cCount = strlen(cConst)+1;
00261   char* cVals = new char[cCount];
00262   if (rank == 0) {
00263      strcpy(cVals, cConst);        // if these values are changed,
00264      cVals[cCount-1] = '\0';       // the values in cBVals must also be changed
00265   }
00266   char* cBVals = new char[cCount]; // Values to be checked against the values
00267                                    // broadcast to the non root processors
00268   strcpy(cBVals, cConst);          // if these values are changed,
00269   cBVals[cCount-1] = '\0';         // the values in cVals must also be changed
00270 
00271   // Values for MaxAll tests
00272   int* iMyGlobalMaxs = new int[count];
00273   for (i=0; i<count; i++)
00274     iMyGlobalMaxs[i]=10 * (i + NumProc-1 -2) +  NumProc-1; // if these values are changed, iInput must be changed
00275                                                            //as well as all other values dependent on iInput
00276   double* dMyGlobalMaxs = new double[count];
00277   for (i=0; i<count; i++)
00278     dMyGlobalMaxs[i]= pow(2.0,i); //if these values are changed, dInput must be changed
00279                                   //as well as all other values dependent on dInput
00280 
00281 
00282   // Values for MinAll tests
00283   int* iMyGlobalMins = new int[count];
00284   for (i=0; i<count; i++)
00285     iMyGlobalMins[i]= 10 * (i - 2); //if these values are changed, iInput must be changed
00286                                     //as well as all other values dependent on iInput
00287   double* dMyGlobalMins = new double[count];
00288   for (i=0; i<count; i++)
00289     dMyGlobalMins[i]= pow(2.0,i-(NumProc-1)); //if these values are changed, dInput must be changed
00290                                               //as well as all other values dependent on dInput
00291 
00292 
00293   // Values for SumAll tests
00294   int* iMyGlobalSums = new int[count];
00295   for (i=0; i<count; i++){
00296     iMyGlobalSums[i]=0;
00297     for (j=0; j<NumProc; j++)
00298       iMyGlobalSums[i] += 10*(i+j-2) + j;// if these values are changed, iInput must be changed     
00299   }                                      //as well as all other values dependent on iInput
00300 
00301   double* dMyGlobalSums = new double[count];
00302   for (i=0; i<count; i++){
00303     dMyGlobalSums[i]=0;
00304     for (j=0; j<NumProc; j++)
00305       dMyGlobalSums[i] += pow(2.0,i-j);// if these values are changed, dInput must be changed
00306   }                                    //as well as all other values dependent on dInput
00307 
00308 
00309   // Values for ScanSum tests
00310   int* iMyScanSums = new int[count];
00311   for (i=0; i<count; i++)
00312     iMyScanSums[i] = int((rank+1)*(10*(2*i+rank-4)+rank)*.5);// if these values are changed,
00313                                                              //iInput must be changed as well as
00314                                                              //all other values dependent on iInput
00315   double* dMyScanSums = new double[count];
00316   for (i=0; i<count; i++) {
00317     dMyScanSums[i] = 0;
00318     for (j=0; j<=rank; j++)
00319       dMyScanSums[i] += pow(2.0,i-j); //if these values are changed, dInput must be changed
00320   }                                   //as well as all other values dependent on dInput
00321 
00322 
00323   // Values for Gather tests
00324   int totalVals = count*NumProc;
00325   int* iMyOrderedVals = new int[totalVals];
00326   double* dMyOrderedVals = new double[totalVals];
00327   int k=0;
00328   for (j=0; j<NumProc; j++) {
00329     for (i=0; i<count; i++) {
00330       iMyOrderedVals[k] = 10*(i + j - 2) + j;; // if these values are changed, iInput must be changed
00331                                                //as well as all other values dependent on iInput
00332       dMyOrderedVals[k] = pow(2.0,i-j); // if these values are changed, dInput must be changed
00333                                         //as well as all other values dependent on dInput
00334       k++;
00335     }
00336   }
00337   petracomm.Barrier();
00338 
00339 
00340   // Method testing section
00341   // Test the Broadcast functions
00342   EPETRA_TEST_ERR(petracomm.Broadcast(iVals,count,0),ierr);
00343   if (verbose1) {
00344     if (rank == 0)
00345       cout << "The values on the root processor are: ";
00346     else
00347       cout << "The values on processor " << rank << " are: ";
00348     for (i=0; i<count; i++)
00349       cout << iVals[i] << " ";
00350     cout << endl;
00351   }
00352   // ierr = 0; need to track errors the whole way through the file - this line of code seems like a bad idea
00353   forierr = 0;
00354   for (i=0; i<count; i++)
00355     forierr += !(iVals[i] == iBVals[i]); // otherwise Broadcast didn't occur properly
00356   EPETRA_TEST_ERR(forierr,ierr);
00357   delete [] iVals;
00358   delete [] iBVals;
00359   petracomm.Barrier();
00360   if (verbose) cout << endl << "Broadcast (type int) test passed!" << endl << endl;// If test gets to here the test passed,
00361                                                                                    //only output on one node
00362   petracomm.Barrier();
00363 
00364   EPETRA_TEST_ERR(petracomm.Broadcast(dVals,count,0),ierr);
00365   if (verbose1) {
00366     if (rank == 0)
00367       cout << "The values on the root processor are: ";
00368     else
00369       cout << "The values on processor " << rank << " are: ";
00370     for (i=0; i<count; i++)
00371       cout << dVals[i] << " ";
00372     cout << endl;
00373   }
00374   forierr = 0;
00375   for (i=0; i<count; i++)
00376     forierr += !(dVals[i] == dBVals[i]); // otherwise Broadcast didn't occur properly
00377   EPETRA_TEST_ERR(forierr,ierr);
00378   delete [] dVals;
00379   delete [] dBVals;
00380   petracomm.Barrier();
00381   if (verbose) cout << endl << "Broadcast (type double) test passed!" << endl << endl;// If test gets to here the test passed,
00382                                                                                       //only output on one node
00383   petracomm.Barrier();
00384 
00385   EPETRA_TEST_ERR(petracomm.Broadcast(llVals,count,0),ierr);
00386   if (verbose1) {
00387     if (rank == 0)
00388       cout << "The values on the root processor are: ";
00389     else
00390       cout << "The values on processor " << rank << " are: ";
00391     for (i=0; i<count; i++)
00392       cout << llVals[i] << " ";
00393     cout << endl;
00394   }
00395   forierr = 0;
00396   for (i=0; i<count; i++)
00397     forierr += !(llVals[i] == llBVals[i]); // otherwise Broadcast didn't occur properly
00398   EPETRA_TEST_ERR(forierr,ierr);
00399   delete [] llVals;
00400   delete [] llBVals;
00401   petracomm.Barrier();
00402   if (verbose) cout << endl << "Broadcast (type long long) test passed!" << endl << endl;// If test gets to here the test passed,
00403                                                                                       //only output on one node
00404   petracomm.Barrier();
00405 
00406   EPETRA_TEST_ERR(petracomm.Broadcast(cVals,cCount,0),ierr);
00407   if (verbose1) {
00408     if (rank == 0)
00409       cout << "The values on the root processor are: " << cVals << endl;
00410     else
00411       cout << "The values on processor " << rank << " are: " << cVals << endl;
00412   }
00413   forierr = 0;
00414   for (i=0; i<cCount; i++)
00415     forierr += !(cVals[i] == cBVals[i]); // otherwise Broadcast didn't work.
00416   EPETRA_TEST_ERR(forierr,ierr);
00417   delete [] cVals;
00418   delete [] cBVals;
00419   petracomm.Barrier();
00420   // If test gets to here the test passed,
00421   if (verbose)
00422     cout << endl << "Broadcast (type char) test passed!" << endl << endl;
00423                                                                                       //only output on one node
00424   petracomm.Barrier();
00425 
00426  // Test the MaxAll functions
00427   int* iGlobalMaxs = new int[count];
00428   if (verbose1) {
00429     cout << "The values on processor " << rank << " are: ";
00430     for (i=0; i<count; i++)
00431       cout << iInputs[i] << " ";
00432     cout << endl;
00433   }
00434   EPETRA_TEST_ERR(petracomm.MaxAll(iInputs,iGlobalMaxs,count),ierr);
00435   petracomm.Barrier();
00436 
00437   if (verbose1) {
00438     cout << "The max values according to processor " << rank << " are: ";
00439     for (i=0; i<count; i++)
00440       cout << iGlobalMaxs[i] << " ";
00441     cout << endl;
00442   }
00443   forierr = 0;
00444   for (i=0; i<count; i++)
00445     forierr += !(iMyGlobalMaxs[i] == iGlobalMaxs[i]);
00446   EPETRA_TEST_ERR(forierr,ierr);
00447 
00448   delete [] iGlobalMaxs;
00449   delete [] iMyGlobalMaxs;
00450   petracomm.Barrier();
00451   if (verbose) cout << endl << "MaxAll (type int) test passed!" << endl << endl;// If test gets to here the test passed,
00452                                                                                 //only output on one node
00453   petracomm.Barrier();
00454 
00455   double* dGlobalMaxs = new double[count];
00456   if (verbose1) {
00457     cout << "The values on processor " << rank << " are: ";
00458     for (i=0; i<count; i++)
00459       cout << dInputs[i] << " ";
00460     cout << endl;
00461   }
00462   EPETRA_TEST_ERR(petracomm.MaxAll(dInputs,dGlobalMaxs,count),ierr);
00463   petracomm.Barrier();
00464 
00465   if (verbose1) {
00466     cout << "The max values according to processor " << rank << " are: ";
00467     for (i=0; i<count; i++)
00468       cout << dGlobalMaxs[i] << " ";
00469     cout << endl;
00470   }
00471   forierr = 0;
00472   for (i=0; i<count; i++)
00473     forierr += !(Epetra_Util::Chop(dMyGlobalMaxs[i] - dGlobalMaxs[i]) == 0);
00474   EPETRA_TEST_ERR(forierr,ierr);
00475   delete [] dGlobalMaxs;
00476   delete [] dMyGlobalMaxs;
00477   petracomm.Barrier();
00478   if (verbose) cout << endl << "MaxAll (type double) test passed!" << endl << endl;// If test gets to here the test passed,
00479                                                                                    //only output on one node
00480   petracomm.Barrier();
00481 
00482 
00483  // Test the MinAll functions
00484   int* iGlobalMins = new int[count];
00485   if (verbose1) {
00486     cout << "The values on processor " << rank << " are: ";
00487     for (i=0; i<count; i++)
00488       cout << iInputs[i] << " ";
00489     cout << endl;
00490   }
00491   EPETRA_TEST_ERR(petracomm.MinAll(iInputs,iGlobalMins,count),ierr);
00492   petracomm.Barrier();
00493 
00494   if (verbose1) {
00495     cout << "The min values according to processor " << rank << " are: ";
00496     for (i=0; i<count; i++)
00497       cout << iGlobalMins[i] << " ";
00498     cout << endl;
00499   }
00500   forierr = 0;
00501   for (i=0; i<count; i++)
00502     forierr += !(iMyGlobalMins[i] == iGlobalMins[i]); // otherwise calculated min is wrong
00503   EPETRA_TEST_ERR(forierr,ierr);
00504   delete [] iGlobalMins;
00505   delete [] iMyGlobalMins;
00506   petracomm.Barrier();
00507   if (verbose) cout << endl << "MinAll (type int) test passed!" << endl << endl;// If test gets to here the test passed,
00508                                                                                 //only output on one node
00509   petracomm.Barrier();
00510 
00511   double* dGlobalMins = new double[count];
00512   if (verbose1) {
00513     cout << "The values on processor " << rank << " are: ";
00514     for (i=0; i<count; i++)
00515       cout << dInputs[i] << " ";
00516     cout << endl;
00517   }
00518   EPETRA_TEST_ERR(petracomm.MinAll(dInputs,dGlobalMins,count),ierr);
00519   petracomm.Barrier();
00520 
00521   if (verbose1) {
00522     cout << "The min values according to processor " << rank << " are: ";
00523     for (i=0; i<count; i++)
00524       cout << dGlobalMins[i] << " ";
00525     cout << endl;
00526   }
00527   forierr = 0;
00528   for (i=0; i<count; i++)
00529     forierr += !(Epetra_Util::Chop(dMyGlobalMins[i] - dGlobalMins[i]) == 0); // otherwise calculated min is wrong
00530   EPETRA_TEST_ERR(forierr,ierr);
00531   delete [] dGlobalMins;
00532   delete [] dMyGlobalMins;
00533   petracomm.Barrier();
00534   if (verbose) cout << endl << "MinAll (type double) test passed!" << endl << endl;// If test gets to here the test passed,
00535                                                                                    //only output on one node
00536   petracomm.Barrier();
00537 
00538 
00539  // Test the SumAll functions
00540   int* iGlobalSums = new int[count];
00541   if (verbose1) {
00542     cout << "The values on processor " << rank << " are: ";
00543     for (i=0; i<count; i++)
00544       cout << iInputs[i] << " ";
00545     cout << endl;
00546   }
00547   EPETRA_TEST_ERR(petracomm.SumAll(iInputs,iGlobalSums,count),ierr);
00548   petracomm.Barrier();
00549 
00550   if (verbose1) {
00551     cout << "The sums of all values according to processor " << rank << " are: ";
00552     for (i=0; i<count; i++)
00553       cout << iGlobalSums[i] << " ";
00554     cout << endl;
00555   }
00556   forierr = 0;
00557   for (i=0; i<count; i++)
00558     forierr += !(iMyGlobalSums[i] == iGlobalSums[i]); // otherwise calculated sum is wrong
00559   EPETRA_TEST_ERR(forierr,ierr);
00560   delete [] iGlobalSums;
00561   delete [] iMyGlobalSums;
00562   petracomm.Barrier();
00563   if (verbose) cout << endl << "SumAll (type int) test passed!" << endl << endl;// If test gets to here the test passed,
00564                                                                                 //only output on one node
00565   petracomm.Barrier();
00566 
00567   double* dGlobalSums = new double[count];
00568   if (verbose1) {
00569     cout << "The values on processor " << rank << " are: ";
00570     for (i=0; i<count; i++)
00571       cout << dInputs[i] << " ";
00572     cout << endl;
00573   }
00574   EPETRA_TEST_ERR(petracomm.SumAll(dInputs,dGlobalSums,count),ierr);
00575   petracomm.Barrier();
00576 
00577   if (verbose1) {
00578     cout << "The sums of all values according to processor " << rank << " are: ";
00579     for (i=0; i<count; i++)
00580       cout << dGlobalSums[i] << " ";
00581     cout << endl;
00582   }
00583   forierr = 0;
00584   for (i=0; i<count; i++)
00585     forierr += !(Epetra_Util::Chop(dMyGlobalSums[i] - dGlobalSums[i]) == 0); // otherwise calculated sum is wrong
00586   EPETRA_TEST_ERR(forierr,ierr);
00587 
00588   delete [] dGlobalSums;
00589   delete [] dMyGlobalSums;
00590   petracomm.Barrier();
00591   if (verbose) cout << endl << "SumAll (type double) test passed!" << endl << endl;// If test gets to here the test passed,
00592                                                                                    //only output on one node
00593   petracomm.Barrier();
00594 
00595 
00596  // Test the ScanSum functions
00597   int* iScanSums = new int[count];
00598   if (verbose1) {
00599     cout << "The values on processor " << rank << " are: ";
00600     for (i=0; i<count; i++)
00601       cout << iInputs[i] << " ";
00602     cout << endl;
00603   }
00604 
00605   EPETRA_TEST_ERR(petracomm.ScanSum(iInputs,iScanSums,count),ierr);
00606   petracomm.Barrier();
00607 
00608   if (verbose1) {
00609     cout << "The sums of all values on processors 0 - " << rank << " are: ";
00610     for (i=0; i<count; i++) {
00611       cout << iScanSums[i] << " ";
00612     }
00613     cout << endl;
00614   }
00615   forierr = 0;
00616   for (i=0; i<count; i++)
00617     forierr += !(iMyScanSums[i] == iScanSums[i]);
00618   EPETRA_TEST_ERR(forierr,ierr);
00619   delete [] iScanSums;
00620   delete [] iMyScanSums;
00621   petracomm.Barrier();
00622   if (verbose) cout << endl << "ScanSum (type int) test passed!" << endl << endl;// If test gets to here the test passed,
00623                                                                                  //only output on one node
00624   petracomm.Barrier();
00625 
00626   double* dScanSums = new double[count];
00627   if (verbose1) {
00628     cout << "The values on processor " << rank << " are: ";
00629     for (i=0; i<count; i++)
00630       cout << dInputs[i] << " ";
00631     cout << endl;
00632   }
00633 
00634   EPETRA_TEST_ERR(petracomm.ScanSum(dInputs,dScanSums,count),ierr);
00635   petracomm.Barrier();
00636 
00637   if (verbose1) {
00638     cout << "The sums of all values on processors 0 - " << rank << " are: ";
00639     for (i=0; i<count; i++) {
00640       cout << dScanSums[i] << " ";
00641     }
00642     cout << endl;
00643   }
00644   forierr = 0;
00645   for (i=0; i<count; i++)
00646     forierr += !(Epetra_Util::Chop(dMyScanSums[i] - dScanSums[i])== 0);
00647   EPETRA_TEST_ERR(forierr,ierr);
00648   delete [] dScanSums;
00649   delete [] dMyScanSums;
00650   petracomm.Barrier();
00651   if (verbose) cout << endl << "ScanSum (type double) test passed!" << endl << endl;// If test gets to here the test passed,
00652                                                                                     //only output on one node
00653   petracomm.Barrier();
00654 
00655 
00656  // Test the Gather functions
00657   int* iOrderedVals = new int[totalVals];
00658   if (verbose1) {
00659     cout << "The values on processor " << rank << " are: ";
00660     for (i=0; i<count; i++)
00661       cout << iInputs[i] << " ";
00662     cout << endl;
00663   }
00664 
00665   EPETRA_TEST_ERR(petracomm.GatherAll(iInputs,iOrderedVals,count),ierr);
00666   petracomm.Barrier();
00667 
00668   if (verbose1) {
00669     cout << "The combined list of all values from all processors according to processor " << rank << " is: ";
00670     for (i=0; i<totalVals; i++) {
00671       cout << iOrderedVals[i] << " ";
00672     }
00673     cout << endl;
00674   }
00675   forierr = 0;
00676   for (i=0; i<totalVals; i++)
00677     forierr += !(iMyOrderedVals[i] == iOrderedVals[i]);
00678   EPETRA_TEST_ERR(forierr,ierr);
00679   delete [] iOrderedVals;
00680   delete [] iMyOrderedVals;
00681   petracomm.Barrier();
00682   if (verbose) cout << endl << "GatherAll (type int) test passed!" << endl << endl;// If test gets to here the test passed,
00683                                                                                    //only output on one node
00684   petracomm.Barrier();
00685 
00686   double* dOrderedVals = new double[totalVals];
00687   if (verbose1) {
00688     cout << "The values on processor " << rank << " are: ";
00689     for (i=0; i<count; i++)
00690       cout << dInputs[i] << " ";
00691     cout << endl;
00692   }
00693 
00694   EPETRA_TEST_ERR(petracomm.GatherAll(dInputs,dOrderedVals,count),ierr);
00695   petracomm.Barrier();
00696 
00697   if (verbose1) {
00698     cout << "The combined list of all values from all processors according to processor " << rank << " is: ";
00699     for (i=0; i<totalVals; i++) {
00700       cout << dOrderedVals[i] << " ";
00701     }
00702     cout << endl;
00703   }
00704   forierr = 0;
00705   for (i=0; i<totalVals; i++)
00706     forierr += !(Epetra_Util::Chop(dMyOrderedVals[i] - dOrderedVals[i]) == 0);
00707   EPETRA_TEST_ERR(forierr,ierr);
00708   delete [] dOrderedVals;
00709   delete [] dMyOrderedVals;
00710   petracomm.Barrier();
00711   if (verbose) cout << endl << "GatherAll (type double) test passed!" << endl << endl;// If test gets to here the test passed,
00712                                                                                       //only output on one node
00713   petracomm.Barrier();
00714 
00715   delete[] dInputs;
00716   delete[] iInputs;
00717 
00718   return(ierr);
00719 }
00720 
00721 //=============================================================================
00722 int checkSerialDataClass(bool verbose) {
00723   int ierr = 0;
00724   if(verbose) cout << "Testing Reference Counting... ";               
00725   Epetra_SerialComm c1;
00726   int c1count = c1.ReferenceCount();
00727   const Epetra_SerialCommData* c1addr = c1.DataPtr();
00728   EPETRA_TEST_ERR(!(c1count==1),ierr); // count should be 1
00729   if(verbose) cout << "Default constructor. \nc1= " << c1count << "  " << c1addr << endl;
00730 
00731   Epetra_SerialComm* c2 = new Epetra_SerialComm(c1);
00732   int c2count = c2->ReferenceCount();
00733   const Epetra_SerialCommData* c2addr = c2->DataPtr();
00734   int c1countold = c1count;
00735   c1count = c1.ReferenceCount();
00736   EPETRA_TEST_ERR(!(c2count==c1count && c1count==(c1countold+1)),ierr); // both counts should be 2
00737   EPETRA_TEST_ERR(!(c1addr==c2addr),ierr); // addresses should be same
00738   if(verbose) cout << "Copy constructor(heap). \nc1= " << c1count << "  " << c1addr
00739                     << "\nc2= " << c2count << "  " << c2addr << endl;
00740   delete c2;
00741   c1countold = c1count;
00742   c1count = c1.ReferenceCount();
00743   EPETRA_TEST_ERR(!(c1count==c1countold-1),ierr); // count should have decremented (to 1)
00744   EPETRA_TEST_ERR(!(c1addr==c1.DataPtr()),ierr); // c1addr should be unchanged
00745   if(verbose) cout << "c2 Deleted. \nc1= " << c1count << "  " << c1addr << endl;
00746   { // inside own set of brackets so that c2a will be automatically at end of brackets
00747     // so that we can test to make sure objects on the stack deallocate correctly
00748     Epetra_SerialComm c2a(c1);
00749     c2count = c2a.ReferenceCount();
00750     c2addr = c2a.DataPtr();
00751     c1countold = c1count;
00752     c1count = c1.ReferenceCount();
00753     EPETRA_TEST_ERR(!(c2count==c1count && c1count==c1countold+1),ierr); // both counts should be 2
00754     EPETRA_TEST_ERR(!(c1addr==c2addr),ierr); // addresses should be same
00755     if(verbose) cout << "Copy constructor(stack). \nc1= " << c1count << "  " << c1addr
00756                       << "\nc2a= " << c2count << "  " << c2addr << endl;
00757   }
00758   c1countold = c1count;
00759   c1count = c1.ReferenceCount();
00760   EPETRA_TEST_ERR(!(c1count==c1countold-1),ierr); // count should have decremented (to 1)
00761   EPETRA_TEST_ERR(!(c1addr==c1.DataPtr()),ierr); // c1addr should be unchanged
00762   if(verbose) cout << "c2a Destroyed. \nc1= " << c1count << "  " << c1addr << endl;
00763   if(verbose) cout << "Assignment operator, post construction" << endl;
00764   Epetra_SerialComm c3;
00765   int c3count = c3.ReferenceCount();
00766   const Epetra_SerialCommData* c3addr = c3.DataPtr();
00767   EPETRA_TEST_ERR(!(c3count==1),ierr); // c3count should be 1 initially
00768   EPETRA_TEST_ERR(!(c1addr!=c3addr),ierr); // c1 and c3 should have different ptr addresses
00769   if(verbose)cout << "Prior to assignment: \nc1=" << c1count << "  " << c1addr
00770                    << "\nc3=" << c3count << "  " << c3addr << endl;
00771   c3 = c1;
00772   c3count = c3.ReferenceCount();
00773   c3addr = c3.DataPtr();
00774   c1countold = c1count;
00775   c1count = c1.ReferenceCount();
00776   EPETRA_TEST_ERR(!(c3count==c1count && c1count==c1countold+1),ierr); // both counts should be 2
00777   EPETRA_TEST_ERR(!(c1addr==c3addr),ierr); // addresses should be same
00778   if(verbose)cout << "After assignment: \nc1=" << c1count << "  " << c1addr
00779                    << "\nc3=" << c3count << "  " << c3addr << endl;
00780   return(ierr);
00781 }
00782 
00783 //=============================================================================
00784 #ifdef EPETRA_MPI
00785 int checkMpiDataClass(bool verbose) {
00786   int ierr = 0;
00787   if(verbose) cout << "Testing Reference Counting... ";               
00788   Epetra_MpiComm c1( MPI_COMM_WORLD );
00789   int c1count = c1.ReferenceCount();
00790   const Epetra_MpiCommData* c1addr = c1.DataPtr();
00791   EPETRA_TEST_ERR(!(c1count==1),ierr); // count should be 1
00792   if(verbose) cout << "Default constructor. \nc1= " << c1count << "  " << c1addr << endl;
00793 
00794   Epetra_MpiComm* c2 = new Epetra_MpiComm(c1);
00795   int c2count = c2->ReferenceCount();
00796   const Epetra_MpiCommData* c2addr = c2->DataPtr();
00797   int c1countold = c1count;
00798   c1count = c1.ReferenceCount();
00799   EPETRA_TEST_ERR(!(c2count==c1count && c1count==(c1countold+1)),ierr); // both counts should be 2
00800   EPETRA_TEST_ERR(!(c1addr==c2addr),ierr); // addresses should be same
00801   if(verbose) cout << "Copy constructor(heap). \nc1= " << c1count << "  " << c1addr
00802                     << "\nc2= " << c2count << "  " << c2addr << endl;
00803   delete c2;
00804   c1countold = c1count;
00805   c1count = c1.ReferenceCount();
00806   EPETRA_TEST_ERR(!(c1count==c1countold-1),ierr); // count should have decremented (to 1)
00807   EPETRA_TEST_ERR(!(c1addr==c1.DataPtr()),ierr); // c1addr should be unchanged
00808   if(verbose) cout << "c2 Deleted. \nc1= " << c1count << "  " << c1addr << endl;
00809   { // inside own set of brackets so that c2a will be automatically at end of brackets
00810     // so that we can test to make sure objects on the stack deallocate correctly
00811     Epetra_MpiComm c2a(c1);
00812     c2count = c2a.ReferenceCount();
00813     c2addr = c2a.DataPtr();
00814     c1countold = c1count;
00815     c1count = c1.ReferenceCount();
00816     EPETRA_TEST_ERR(!(c2count==c1count && c1count==c1countold+1),ierr); // both counts should be 2
00817     EPETRA_TEST_ERR(!(c1addr==c2addr),ierr); // addresses should be same
00818     if(verbose) cout << "Copy constructor(stack). \nc1= " << c1count << "  " << c1addr
00819                       << "\nc2a= " << c2count << "  " << c2addr << endl;
00820   }
00821   c1countold = c1count;
00822   c1count = c1.ReferenceCount();
00823   EPETRA_TEST_ERR(!(c1count==c1countold-1),ierr); // count should have decremented (to 1)
00824   EPETRA_TEST_ERR(!(c1addr==c1.DataPtr()),ierr); // c1addr should be unchanged
00825   if(verbose) cout << "c2a Destroyed. \nc1= " << c1count << "  " << c1addr << endl;
00826   if(verbose) cout << "Assignment operator, post construction" << endl;
00827   Epetra_MpiComm c3( MPI_COMM_WORLD );
00828   int c3count = c3.ReferenceCount();
00829   const Epetra_MpiCommData* c3addr = c3.DataPtr();
00830   EPETRA_TEST_ERR(!(c3count==1),ierr); // c3count should be 1 initially
00831   EPETRA_TEST_ERR(!(c1addr!=c3addr),ierr); // c1 and c3 should have different ptr addresses
00832   if(verbose)cout << "Prior to assignment: \nc1=" << c1count << "  " << c1addr
00833                    << "\nc3=" << c3count << "  " << c3addr << endl;
00834   c3 = c1;
00835   c3count = c3.ReferenceCount();
00836   c3addr = c3.DataPtr();
00837   c1countold = c1count;
00838   c1count = c1.ReferenceCount();
00839   EPETRA_TEST_ERR(!(c3count==c1count && c1count==c1countold+1),ierr); // both counts should be 2
00840   EPETRA_TEST_ERR(!(c1addr==c3addr),ierr); // addresses should be same
00841   if(verbose)cout << "After assignment: \nc1=" << c1count << "  " << c1addr
00842                    << "\nc3=" << c3count << "  " << c3addr << endl;
00843   return(ierr);
00844 }
00845 #endif
00846 
00847 int checkDistributor(Epetra_Distributor* distr,
00848                      Epetra_Comm& Comm)
00849 {
00850   int numprocs = Comm.NumProc();
00851 
00852   int numExportIDs = numprocs;
00853   int* exportPIDs = new int[numExportIDs];
00854   for(int p=0; p<numExportIDs; ++p) {
00855     exportPIDs[p] = p;
00856   }
00857 
00858   bool deterministic = true;
00859   int numRemoteIDs = 0;
00860 
00861   int err = distr->CreateFromSends(numExportIDs, exportPIDs,
00862                                    deterministic, numRemoteIDs);
00863 
00864   //numRemoteIDs should equal numExportIDs.
00865 
00866   int returnValue = numRemoteIDs == numExportIDs ? 0 : -99;
00867 
00868   delete [] exportPIDs;
00869 
00870   if (returnValue + err != 0) {
00871     return(returnValue + err);
00872   }
00873 
00874   int* exportIDs = new int[numExportIDs];
00875   for(int i=0; i<numExportIDs; ++i) {
00876     exportIDs[i] = i+1;
00877   }
00878 
00879   int len_imports = 0;
00880   char* imports = NULL;
00881 
00882   err = distr->Do((char*)exportIDs, sizeof(int),
00883                   len_imports, imports);
00884 
00885   delete [] exportIDs;
00886 
00887   if (len_imports > 0) {
00888     delete [] imports;
00889   }
00890 
00891   return(err);
00892 }
00893 
00894 /*
00895   end of file cxx_main.cpp
00896 */
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines