Back to index

salome-med  6.5.0
test_AllToAllvTimeDoubleDEC.cxx
Go to the documentation of this file.
00001 // Copyright (C) 2007-2012  CEA/DEN, EDF R&D
00002 //
00003 // This library is free software; you can redistribute it and/or
00004 // modify it under the terms of the GNU Lesser General Public
00005 // License as published by the Free Software Foundation; either
00006 // version 2.1 of the License.
00007 //
00008 // This library is distributed in the hope that it will be useful,
00009 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00010 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00011 // Lesser General Public License for more details.
00012 //
00013 // You should have received a copy of the GNU Lesser General Public
00014 // License along with this library; if not, write to the Free Software
00015 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
00016 //
00017 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
00018 //
00019 
00020 #include <math.h>
00021 #include <string>
00022 #include <vector>
00023 #include <map>
00024 #include <iostream>
00025 #include <mpi.h>
00026 #include <ctime>
00027 
00028 #include "MPIAccessDECTest.hxx"
00029 #include <cppunit/TestAssert.h>
00030 
00031 //#include "CommInterface.hxx"
00032 //#include "ProcessorGroup.hxx"
00033 //#include "MPIProcessorGroup.hxx"
00034 #include "MPIAccessDEC.hxx"
00035 #include "LinearTimeInterpolator.hxx"
00036 
00037 // use this define to enable lines, execution of which leads to Segmentation Fault
00038 #define ENABLE_FAULTS
00039 
00040 // use this define to enable CPPUNIT asserts and fails, showing bugs
00041 #define ENABLE_FORCED_FAILURES
00042 
00043 using namespace std;
00044 using namespace ParaMEDMEM;
00045 
00046 void MPIAccessDECTest::test_AllToAllvTimeDoubleDECSynchronousPointToPoint() {
00047   test_AllToAllvTimeDoubleDEC( false ) ;
00048 }
00049 void MPIAccessDECTest::test_AllToAllvTimeDoubleDECAsynchronousPointToPoint() {
00050   test_AllToAllvTimeDoubleDEC( true ) ;
00051 }
00052 
00053 static void chksts( int sts , int myrank , ParaMEDMEM::MPIAccess * mpi_access ) {
00054   char msgerr[MPI_MAX_ERROR_STRING] ;
00055   int lenerr ;
00056   if ( sts != MPI_SUCCESS ) {
00057     mpi_access->errorString(sts, msgerr, &lenerr) ;
00058     cout << "test" << myrank << " lenerr " << lenerr << " "
00059          << msgerr << endl ;
00060     ostringstream strstream ;
00061     strstream << "==========================================================="
00062               << "test" << myrank << " KO"
00063               << "==========================================================="
00064               << endl ;
00065     cout << strstream.str() << endl ;
00066     CPPUNIT_FAIL( strstream.str() ) ;
00067   }
00068   return ;
00069 }
00070 
00071 void MPIAccessDECTest::test_AllToAllvTimeDoubleDEC( bool Asynchronous ) {
00072 
00073   cout << "test_AllToAllvTimeDoubleDEC" << endl ;
00074 
00075 //  MPI_Init(&argc, &argv) ; 
00076 
00077   int size ;
00078   int myrank ;
00079   MPI_Comm_size(MPI_COMM_WORLD,&size) ;
00080   MPI_Comm_rank(MPI_COMM_WORLD,&myrank) ;
00081 
00082   if ( size < 2 || size > 11 ) {
00083     ostringstream strstream ;
00084     strstream << "usage :" << endl
00085               << "mpirun -np <nbprocs> test_AllToAllTimeDEC" << endl
00086               << " (nbprocs >=2)" << endl
00087               << "test must be runned with more than 1 proc and less than 12 procs"
00088               << endl ;
00089     cout << strstream.str() << endl ;
00090     CPPUNIT_FAIL( strstream.str() ) ;
00091   }
00092 
00093 //  int Asynchronous = atoi(argv[1]) ;
00094 
00095   cout << "test_AllToAllvTimeDoubleDEC" << myrank << " Asynchronous " << Asynchronous << endl ;
00096 
00097   ParaMEDMEM::CommInterface interface ;
00098   std::set<int> sourceprocs;
00099   std::set<int> targetprocs;
00100   int i ;
00101   for ( i = 0 ; i < size/2 ; i++ ) {
00102      sourceprocs.insert(i);
00103   }
00104   for ( i = size/2 ; i < size ; i++ ) {
00105      targetprocs.insert(i);
00106   }
00107 
00108   ParaMEDMEM::MPIProcessorGroup* sourcegroup = new ParaMEDMEM::MPIProcessorGroup(interface,sourceprocs) ;
00109   ParaMEDMEM::MPIProcessorGroup* targetgroup = new ParaMEDMEM::MPIProcessorGroup(interface,targetprocs) ;
00110 
00111 //  TimeInterpolator * aLinearInterpDEC = new LinearTimeInterpolator( 0 ) ;
00112   MPIAccessDEC * MyMPIAccessDEC = new MPIAccessDEC( *sourcegroup , *targetgroup ,
00113                                                     Asynchronous ) ;
00114 //                                                    Asynchronous , LinearInterp , 0.5 ) ;
00115   MyMPIAccessDEC->setTimeInterpolator( LinearTimeInterp ) ;
00116   MPIAccess * mpi_access = MyMPIAccessDEC->getMPIAccess() ;
00117 
00118   cout << "test_AllToAllvTimeDoubleDEC" << myrank << " Barrier :" << endl ;
00119   mpi_access->barrier() ;
00120 
00121 #define maxproc 11
00122 #define maxreq 100
00123 #define datamsglength 10
00124 
00125   int sts ;
00126   int *sendcounts = new int[size] ;
00127   int *sdispls = new int[size] ;
00128   int *recvcounts = new int[size] ;
00129   int *rdispls = new int[size] ;
00130   int *sendtimecounts = new int[size] ;
00131   int *stimedispls = new int[size] ;
00132   int *recvtimecounts = new int[size] ;
00133   int *rtimedispls = new int[size] ;
00134   for ( i = 0 ; i < size ; i++ ) {
00135      sendcounts[i] = datamsglength-i ;
00136      sdispls[i] = i*datamsglength ;
00137      recvcounts[i] = datamsglength-myrank ;
00138      rdispls[i] = i*datamsglength ;
00139      sendtimecounts[i] = 1 ;
00140      stimedispls[i] = 0 ;
00141      recvtimecounts[i] = 1 ;
00142      rtimedispls[i] = i ;
00143   }
00144 
00145   double timeLoc[maxproc] ;
00146   double deltatime[maxproc] = {1.,2.1,3.2,4.3,5.4,6.5,7.6,8.7,9.8,10.9,11.} ;
00147   double maxtime[maxproc] ;
00148   double nextdeltatime[maxproc] ;
00149   for ( i = 0 ; i < size ; i++ ) {
00150      timeLoc[i] = 0 ;
00151      maxtime[i] = maxreq ;
00152      nextdeltatime[i] = deltatime[i] ;
00153   }
00154   time_t begintime = time(NULL) ;
00155   for ( timeLoc[myrank] = 0 ; timeLoc[myrank] <= maxtime[myrank] && nextdeltatime[myrank] != 0 ;
00156         timeLoc[myrank]+=nextdeltatime[myrank] ) {
00157 //local and target times
00158      int target ;
00159      for ( target = 0 ; target < size ; target++ ) {
00160         nextdeltatime[target] = deltatime[target] ;
00161         if ( timeLoc[target] != 0 ) {
00162           if ( timeLoc[target]+nextdeltatime[target] > maxtime[target] ) {
00163             nextdeltatime[target] = 0 ;
00164           }
00165         }
00166         if ( target != myrank ) {
00167           while ( timeLoc[myrank] >= timeLoc[target] ) {
00168                timeLoc[target] += deltatime[target] ;
00169           }
00170         }
00171      }
00172      MyMPIAccessDEC->setTime( timeLoc[myrank] , nextdeltatime[myrank] ) ;
00173      cout << "test" << myrank << "=====TIME " << timeLoc[myrank] << "=====DELTATIME "
00174           << nextdeltatime[myrank] << "=====MAXTIME " << maxtime[myrank] << " ======"
00175           << endl ; 
00176      double * sendbuf = new double[datamsglength*size] ;
00177 //     double * sendbuf = (double *) malloc(sizeof(double)*datamsglength*size) ;
00178      double * recvbuf = new double[datamsglength*size] ;
00179      int j ;
00180      //cout << "test_AllToAllvTimeDoubleDEC" << myrank << " sendbuf" ;
00181      for ( target = 0 ; target < size ; target++ ) {
00182         for ( j = 0 ; j < datamsglength ; j++ ) {
00183            //sendbuf[j] = myrank*10000 + (j/datamsglength)*100 + j ;
00184            sendbuf[target*datamsglength+j] = myrank*1000000 + target*10000 +
00185                                              (timeLoc[myrank]/deltatime[myrank])*100 + j ;
00186            //cout << " " << (int ) sendbuf[target*datamsglength+j] ;
00187            recvbuf[target*datamsglength+j] = -1 ;
00188         }
00189         //cout << endl ;
00190      }
00191 
00192      int sts = MyMPIAccessDEC->allToAllvTime( sendbuf, sendcounts , sdispls , MPI_DOUBLE ,
00193                                             recvbuf, recvcounts , rdispls , MPI_DOUBLE ) ;
00194      chksts( sts , myrank , mpi_access ) ;
00195 
00196 //     cout << "test_AllToAllvTimeDoubleDEC" << myrank << " recvbuf before CheckSent" ;
00197 //     for ( i = 0 ; i < datamsglength*size ; i++ ) {
00198 //        cout << " " << recvbuf[i] ;
00199 //     }
00200 //     cout << endl ;
00201 
00202      int nRecvReq = mpi_access->recvRequestIdsSize() ;
00203      if ( nRecvReq != 0 ) {
00204        ostringstream strstream ;
00205        strstream << "=============================================================" << endl
00206                  << "test_AllToAllvTimeDoubleDEC" << myrank << " WaitAllRecv "
00207                  << nRecvReq << " Requests # 0 ERROR"
00208                  << endl << "============================================================"
00209                  << endl ;
00210        int *ArrayOfRecvRequests = new int[nRecvReq] ;
00211        int nReq = mpi_access->recvRequestIds( nRecvReq, ArrayOfRecvRequests ) ;
00212        mpi_access->waitAll( nReq , ArrayOfRecvRequests ) ;
00213        delete [] ArrayOfRecvRequests ;
00214        cout << strstream.str() << endl ;
00215        CPPUNIT_FAIL( strstream.str() ) ;
00216      }
00217 
00218 //     cout << "test_AllToAllvTimeDoubleDEC" << myrank << " check of recvbuf" << endl ;
00219      bool badrecvbuf = false ;
00220      for ( target = 0 ; target < size ; target++ ) {
00221         int j ;
00222         for ( j = 0 ; j < datamsglength ; j++ ) {
00223            int index = target*datamsglength+j ;
00224            if ( j < recvcounts[target] ) {
00225              if ( fabs(recvbuf[index] - (target*1000000 + myrank*10000 +
00226                   (timeLoc[target]/deltatime[target])*100 + j)) > 101) {
00227                badrecvbuf = true ;
00228                cout << "test_AllToAllvTimeDoubleDEC" << myrank << " target " << target << " timeLoc[target] "
00229                     << timeLoc[target] << " recvbuf[" << index << "] " << (int ) recvbuf[index]
00230                     << " # " << (int ) (target*1000000 +
00231                        myrank*10000 + (timeLoc[target]/deltatime[target])*100 + j)
00232                     << endl ;
00233              }
00234              else if ( badrecvbuf ) {
00235                cout << "test_AllToAllvTimeDoubleDEC" << myrank << " recvbuf[" << index << "] "
00236                     << recvbuf[index] << " ~= " << (int ) (target*1000000 +
00237                        myrank*10000 + (timeLoc[target]/deltatime[target])*100 + j) << endl ;
00238              }
00239            }
00240            else if ( recvbuf[index] != -1 ) {
00241              badrecvbuf = true ;
00242              cout << "test_AllToAllvTimeDoubleDEC" << myrank << " recvbuf[" << index << "] "
00243                   << recvbuf[index] << " # -1" << endl ;
00244            }
00245         }
00246      }
00247      if ( badrecvbuf ) {
00248        ostringstream strstream ;
00249        strstream << "==================================================================" << endl
00250                  << "test_AllToAllvTimeDoubleDEC" << myrank << " badrecvbuf"
00251                  << endl << "=================================================================="
00252                  << endl ;
00253        cout << strstream.str() << endl ;
00254        CPPUNIT_FAIL( strstream.str() ) ;
00255      }
00256      delete [] recvbuf ;
00257   }
00258 
00259   cout << "test_AllToAllvTimeDoubleDEC" << myrank << " Barrier :" << endl ;
00260   mpi_access->barrier() ;
00261 
00262   cout << "test_AllToAllvTimeDoubleDEC" << myrank << " CheckFinalSent" << endl ;
00263   sts = MyMPIAccessDEC->checkFinalSent() ;
00264   if ( sts != MPI_SUCCESS ) {
00265     ostringstream strstream ;
00266     strstream << "=================================================================" << endl
00267               << "test_AllToAllvTimeDoubleDEC" << myrank << " CheckFinalSent ERROR"
00268               << endl << "================================================================="
00269               << endl ;
00270     cout << strstream.str() << endl ;
00271     CPPUNIT_FAIL( strstream.str() ) ;
00272   }
00273 
00274   cout << "test_AllToAllvTimeDoubleDEC" << myrank << " CheckFinalRecv" << endl ;
00275   sts = MyMPIAccessDEC->checkFinalRecv() ;
00276   if ( sts != MPI_SUCCESS ) {
00277     ostringstream strstream ;
00278     strstream << "=================================================================" << endl
00279               << "test_AllToAllvTimeDoubleDEC" << myrank << " CheckFinalRecv ERROR"
00280               << endl << "================================================================"
00281               << endl ;
00282     cout << strstream.str() << endl ;
00283     CPPUNIT_FAIL( strstream.str() ) ;
00284   }
00285 
00286   int nRecvReq = mpi_access->recvRequestIdsSize() ;
00287   if ( nRecvReq ) {
00288     ostringstream strstream ;
00289     strstream << "===============================================================" << endl
00290               << "test_AllToAllvTimeDoubleDEC" << myrank << " RecvRequestIds " << nRecvReq
00291               << " RecvRequests # 0 Error"
00292               << endl << "==============================================================="
00293               << endl ;
00294     cout << strstream.str() << endl ;
00295     CPPUNIT_FAIL( strstream.str() ) ;
00296   }
00297   else {
00298     cout << "test_AllToAllvTimeDoubleDEC" << myrank << " RecvRequestIds " << nRecvReq
00299          << " RecvRequests = 0 OK" << endl ;
00300   }
00301 
00302   time_t endtime = time(NULL) ;
00303   cout << "test_AllToAllvTimeDoubleDEC" << myrank << " begintime " << begintime << " endtime " << endtime
00304        << " elapse " << endtime-begintime << " " << maxtime[myrank]/deltatime[myrank]
00305        << " calls to AllToAll" << endl ;
00306 
00307   cout << "test" << myrank << " Barrier :" << endl ;
00308   mpi_access->barrier() ;
00309 
00310   delete sourcegroup ;
00311   delete targetgroup ;
00312   delete MyMPIAccessDEC ;
00313 //  delete aLinearInterpDEC ;
00314 
00315   delete [] sendcounts ;
00316   delete [] sdispls ;
00317   delete [] recvcounts ;
00318   delete [] rdispls ;
00319   delete [] sendtimecounts ;
00320   delete [] stimedispls ;
00321   delete [] recvtimecounts ;
00322   delete [] rtimedispls ;
00323 
00324 //  MPI_Finalize();
00325 
00326   endtime = time(NULL) ;
00327 
00328   cout << "test_AllToAllvTimeDoubleDEC" << myrank << " OK begintime " << begintime << " endtime " << endtime
00329        << " elapse " << endtime-begintime << " " << maxtime[myrank]/deltatime[myrank]
00330        << " calls to AllToAll" << endl ;
00331 
00332   return ;
00333 }
00334 
00335 
00336 
00337