Back to index

salome-med  6.5.0
test_AllToAllvTimeDEC.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 <string>
00021 #include <vector>
00022 #include <map>
00023 #include <iostream>
00024 #include <mpi.h>
00025 #include <ctime>
00026 
00027 #include "MPIAccessDECTest.hxx"
00028 #include <cppunit/TestAssert.h>
00029 
00030 //#include "CommInterface.hxx"
00031 //#include "ProcessorGroup.hxx"
00032 //#include "MPIProcessorGroup.hxx"
00033 #include "MPIAccessDEC.hxx"
00034 #include "LinearTimeInterpolator.hxx"
00035 
00036 // use this define to enable lines, execution of which leads to Segmentation Fault
00037 #define ENABLE_FAULTS
00038 
00039 // use this define to enable CPPUNIT asserts and fails, showing bugs
00040 #define ENABLE_FORCED_FAILURES
00041 
00042 using namespace std;
00043 using namespace ParaMEDMEM;
00044 
00045 void MPIAccessDECTest::test_AllToAllvTimeDECSynchronousNative() {
00046   test_AllToAllvTimeDEC( false , true ) ;
00047 }
00048 void MPIAccessDECTest::test_AllToAllvTimeDECSynchronousPointToPoint() {
00049   test_AllToAllvTimeDEC( false , false ) ;
00050 }
00051 void MPIAccessDECTest::test_AllToAllvTimeDECAsynchronousPointToPoint() {
00052   test_AllToAllvTimeDEC( true , false ) ;
00053 }
00054 
00055 static void chksts( int sts , int myrank , ParaMEDMEM::MPIAccess * mpi_access ) {
00056   char msgerr[MPI_MAX_ERROR_STRING] ;
00057   int lenerr ;
00058   if ( sts != MPI_SUCCESS ) {
00059     mpi_access->errorString(sts, msgerr, &lenerr) ;
00060     cout << "test_AllToAllvTimeDEC" << myrank << " lenerr " << lenerr << " "
00061          << msgerr << endl ;
00062     ostringstream strstream ;
00063     strstream << "==========================================================="
00064               << "test_AllToAllvTimeDEC" << myrank << " KO"
00065               << "==========================================================="
00066               << endl ;
00067     cout << strstream.str() << endl ;
00068     CPPUNIT_FAIL( strstream.str() ) ;
00069   }
00070   return ;
00071 }
00072 
00073 void MPIAccessDECTest::test_AllToAllvTimeDEC( bool Asynchronous , bool UseMPINative ) {
00074 
00075   cout << "test_AllToAllvTimeDEC" << endl ;
00076 
00077   //  MPI_Init(&argc, &argv) ; 
00078 
00079   int size ;
00080   int myrank ;
00081   MPI_Comm_size(MPI_COMM_WORLD,&size) ;
00082   MPI_Comm_rank(MPI_COMM_WORLD,&myrank) ;
00083 
00084   if ( size < 2 || size > 11 ) {
00085     ostringstream strstream ;
00086     strstream << "usage :" << endl
00087               << "mpirun -np <nbprocs> test_AllToAllTimeDEC" << endl
00088               << " (nbprocs >=2)" << endl
00089               << "test must be runned with more than 1 proc and less than 12 procs"
00090               << endl ;
00091     cout << strstream.str() << endl ;
00092     CPPUNIT_FAIL( strstream.str() ) ;
00093   }
00094 
00095   //  int Asynchronous = atoi(argv[1]) ;
00096   int UseMPI_Alltoallv = UseMPINative ;
00097   //  if ( argc == 3 ) {
00098   //    UseMPI_Alltoallv = atoi(argv[2]) ;
00099   //  }
00100 
00101   cout << "test_AllToAllvTimeDEC" << myrank << " Asynchronous " << Asynchronous
00102        << " UseMPI_Alltoallv " << UseMPI_Alltoallv << endl ;
00103 
00104   ParaMEDMEM::CommInterface interface ;
00105   std::set<int> sourceprocs;
00106   std::set<int> targetprocs;
00107   int i ;
00108   for ( i = 0 ; i < size/2 ; i++ ) {
00109     sourceprocs.insert(i);
00110   }
00111   for ( i = size/2 ; i < size ; i++ ) {
00112     targetprocs.insert(i);
00113   }
00114 
00115   ParaMEDMEM::MPIProcessorGroup* sourcegroup = new ParaMEDMEM::MPIProcessorGroup(interface,sourceprocs) ;
00116   ParaMEDMEM::MPIProcessorGroup* targetgroup = new ParaMEDMEM::MPIProcessorGroup(interface,targetprocs) ;
00117 
00118   //  TimeInterpolator * aLinearInterpDEC = new LinearTimeInterpolator( 0.5 ) ;
00119   MPIAccessDEC * MyMPIAccessDEC = new MPIAccessDEC( *sourcegroup , *targetgroup ,
00120                                                     Asynchronous ) ;
00121   //                                                    Asynchronous , LinearInterp , 0.5 ) ;
00122   MyMPIAccessDEC->setTimeInterpolator( LinearTimeInterp , 0.5 ) ;
00123   MPIAccess * mpi_access = MyMPIAccessDEC->getMPIAccess() ;
00124 
00125   cout << "test_AllToAllvTimeDEC" << myrank << " Barrier :" << endl ;
00126   mpi_access->barrier() ;
00127   cout << "test_AllToAllvTimeDEC" << myrank << " Barrier done" << endl ;
00128 
00129 #define maxproc 11
00130 #define maxreq 10000
00131 #define datamsglength 10
00132 
00133   int sts ;
00134   int *sendcounts = new int[size] ;
00135   int *sdispls = new int[size] ;
00136   int *recvcounts = new int[size] ;
00137   int *rdispls = new int[size] ;
00138   int *sendtimecounts = new int[size] ;
00139   int *stimedispls = new int[size] ;
00140   int *recvtimecounts = new int[size] ;
00141   int *rtimedispls = new int[size] ;
00142   for ( i = 0 ; i < size ; i++ ) {
00143     sendcounts[i] = datamsglength-i ;
00144     sdispls[i] = i*datamsglength ;
00145     recvcounts[i] = datamsglength-myrank ;
00146     rdispls[i] = i*datamsglength ;
00147     sendtimecounts[i] = 1 ;
00148     stimedispls[i] = 0 ;
00149     recvtimecounts[i] = 1 ;
00150     rtimedispls[i] = i ;
00151     //rtimedispls[i] = i*mpi_access->TimeExtent() ;
00152   }
00153 
00154   double timeLoc = 0 ;
00155   double deltatime[maxproc] = {1.,2.1,3.2,4.3,5.4,6.5,7.6,8.7,9.8,10.9,11.} ;
00156   double maxtime ;
00157   double nextdeltatime = deltatime[myrank] ;
00158   if ( UseMPI_Alltoallv ) {
00159     maxtime = maxreq*nextdeltatime - 0.1 ;
00160   }
00161   else {
00162     maxtime = maxreq ;
00163     //    MyMPIAccessDEC->InitTime( time , nextdeltatime , maxtime ) ;
00164   }
00165   time_t begintime = time(NULL) ;
00166   //  for ( time = 0 ; time <= maxtime ; time+=deltatime[myrank] ) {
00167   for ( timeLoc = 0 ; timeLoc <= maxtime && nextdeltatime != 0 ; timeLoc+=nextdeltatime ) {
00168     nextdeltatime = deltatime[myrank] ;
00169     if ( timeLoc != 0 ) {
00170       nextdeltatime = deltatime[myrank] ;
00171       if ( timeLoc+nextdeltatime > maxtime ) {
00172         nextdeltatime = 0 ;
00173       }
00174       //       MyMPIAccessDEC->NextTime( nextdeltatime ) ;
00175     }
00176     MyMPIAccessDEC->setTime( timeLoc , nextdeltatime ) ;
00177     cout << "test_AllToAllvTimeDEC" << myrank << "=====TIME " << time << "=====DELTATIME "
00178          << nextdeltatime << "=====MAXTIME " << maxtime << " ======" << endl ; 
00179     int * sendbuf = new int[datamsglength*size] ;
00180     //     int * sendbuf = (int *) malloc(sizeof(int)*datamsglength*size) ;
00181     int * recvbuf = new int[datamsglength*size] ;
00182     int j ;
00183     for ( j = 0 ; j < datamsglength*size ; j++ ) {
00184       sendbuf[j] = myrank*1000000 + (j/datamsglength)*1000 + j ;
00185       recvbuf[j] = -1 ;
00186     }
00187 
00188     if ( UseMPI_Alltoallv ) {
00189       const MPI_Comm* comm = MyMPIAccessDEC->getComm();
00190       TimeMessage * aSendTimeMessage = new TimeMessage ;
00191       aSendTimeMessage->time = timeLoc ;
00192       //       aSendTimeMessage->deltatime = deltatime[myrank] ;
00193       aSendTimeMessage->deltatime = nextdeltatime ;
00194       //       aSendTimeMessage->maxtime = maxtime ;
00195       aSendTimeMessage->tag = (int ) (timeLoc/deltatime[myrank]) ;
00196       TimeMessage * aRecvTimeMessage = new TimeMessage[size] ;
00197       interface.allToAllV(aSendTimeMessage, sendtimecounts , stimedispls ,
00198                           mpi_access->timeType() ,
00199                           aRecvTimeMessage, recvtimecounts , rtimedispls ,
00200                           mpi_access->timeType() , *comm ) ;
00201       //       for ( j = 0 ; j < size ; j++ ) {
00202       //          cout << "test_AllToAllvTimeDEC" << myrank << " TimeMessage received " << j << " "
00203       //               << aRecvTimeMessage[j] << endl ;
00204       //       }
00205       delete aSendTimeMessage ;
00206       delete [] aRecvTimeMessage ;
00207       interface.allToAllV(sendbuf, sendcounts , sdispls , MPI_INT ,
00208                           recvbuf, recvcounts , rdispls , MPI_INT , *comm ) ;
00209       //       free(sendbuf) ;
00210       delete [] sendbuf ;
00211     }
00212     else {
00213       int sts = MyMPIAccessDEC->allToAllvTime( sendbuf, sendcounts , sdispls , MPI_INT ,
00214                                                recvbuf, recvcounts , rdispls , MPI_INT ) ;
00215       chksts( sts , myrank , mpi_access ) ;
00216     }
00217 
00218     //     cout << "test_AllToAllvTimeDEC" << myrank << " recvbuf before CheckSent" ;
00219     //     for ( i = 0 ; i < datamsglength*size ; i++ ) {
00220     //        cout << " " << recvbuf[i] ;
00221     //     }
00222     //     cout << endl ;
00223 
00224     //     cout << "test_AllToAllvTimeDEC" << myrank << " sendbuf " << sendbuf << endl ;
00225     //     MyMPIAccessDEC->CheckSent() ;
00226 
00227     int nRecvReq = mpi_access->recvRequestIdsSize() ;
00228     if ( nRecvReq != 0 ) {
00229       ostringstream strstream ;
00230       strstream << "=============================================================" << endl
00231                 << "test_AllToAllvTimeDEC" << myrank << " WaitAllRecv " << nRecvReq << " Requests # 0 ERROR"
00232                 << endl << "============================================================="
00233                 << endl ;
00234       int *ArrayOfRecvRequests = new int[nRecvReq] ;
00235       int nReq = mpi_access->recvRequestIds( nRecvReq, ArrayOfRecvRequests ) ;
00236       mpi_access->waitAll( nReq , ArrayOfRecvRequests ) ;
00237       delete [] ArrayOfRecvRequests ;
00238       cout << strstream.str() << endl ;
00239       CPPUNIT_FAIL( strstream.str() ) ;
00240     }
00241 
00242     //     cout << "test_AllToAllvTimeDEC" << myrank << " check of recvbuf" << endl ;
00243     bool badrecvbuf = false ;
00244     for ( i = 0 ; i < size ; i++ ) {
00245       int j ;
00246       for ( j = 0 ; j < datamsglength ; j++ ) {
00247         int index = i*datamsglength+j ;
00248         if ( j < recvcounts[i] ) {
00249           if ( recvbuf[index] != (index/datamsglength)*1000000 + myrank*1000 +
00250                myrank*datamsglength+(index%datamsglength) ) {
00251             badrecvbuf = true ;
00252             cout << "test_AllToAllvTimeDEC" << myrank << " recvbuf[" << index << "] "
00253                  << recvbuf[index] << " # " << (index/datamsglength)*1000000 +
00254               myrank*1000 +
00255               myrank*datamsglength+(index%datamsglength) << endl ;
00256           }
00257           else if ( badrecvbuf ) {
00258             cout << "test_AllToAllvTimeDEC" << myrank << " recvbuf[" << index << "] "
00259                  << recvbuf[index] << " == " << (index/datamsglength)*1000000 +
00260               myrank*1000 +
00261               myrank*datamsglength+(index%datamsglength) << endl ;
00262           }
00263         }
00264         else if ( recvbuf[index] != -1 ) {
00265           badrecvbuf = true ;
00266           cout << "test_AllToAllvTimeDEC" << myrank << " recvbuf[" << index << "] "
00267                << recvbuf[index] << " # -1" << endl ;
00268         }
00269       }
00270     }
00271     if ( badrecvbuf ) {
00272       ostringstream strstream ;
00273       strstream << "==============================================================" << endl
00274                 << "test_AllToAllvTimeDEC" << myrank << " badrecvbuf"
00275                 << endl << "============================================================="
00276                 << endl ;
00277       cout << strstream.str() << endl ;
00278       CPPUNIT_FAIL( strstream.str() ) ;
00279     }
00280     delete [] recvbuf ;
00281   }
00282 
00283   cout << "test_AllToAllvTimeDEC" << myrank << " Barrier :" << endl ;
00284   mpi_access->barrier() ;
00285   cout << "test_AllToAllvTimeDEC" << myrank << " Barrier done" << endl ;
00286 
00287   cout << "test_AllToAllvTimeDEC" << myrank << " CheckFinalSent" << endl ;
00288   sts = MyMPIAccessDEC->checkFinalSent() ;
00289   if ( sts != MPI_SUCCESS ) {
00290     ostringstream strstream ;
00291     strstream << "================================================================" << endl
00292               << "test_AllToAllvTimeDEC" << myrank << " final CheckSent ERROR"
00293               << endl << "================================================================"
00294               << endl ;
00295     cout << strstream.str() << endl ;
00296     CPPUNIT_FAIL( strstream.str() ) ;
00297   }
00298 
00299   cout << "test_AllToAllvTimeDEC" << myrank << " CheckFinalRecv" << endl ;
00300   sts = MyMPIAccessDEC->checkFinalRecv() ;
00301   if ( sts != MPI_SUCCESS ) {
00302     ostringstream strstream ;
00303     strstream << "================================================================" << endl
00304               << "test_AllToAllvTimeDEC" << myrank << " CheckFinalRecv ERROR"
00305               << endl << "================================================================"
00306               << endl ;
00307     cout << strstream.str() << endl ;
00308     CPPUNIT_FAIL( strstream.str() ) ;
00309   }
00310 
00311   int nRecvReq = mpi_access->recvRequestIdsSize() ;
00312   if ( nRecvReq ) {
00313     ostringstream strstream ;
00314     strstream << "===============================================================" << endl
00315               << "test_AllToAllvTimeDEC" << myrank << " RecvRequestIds " << nRecvReq
00316               << " RecvRequests # 0 Error"
00317               << endl << "==============================================================="
00318               << endl ;
00319     cout << strstream.str() << endl ;
00320     CPPUNIT_FAIL( strstream.str() ) ;
00321   }
00322   else {
00323     cout << "test_AllToAllvTimeDEC" << myrank << " RecvRequestIds " << nRecvReq
00324          << " RecvRequests = 0 OK" << endl ;
00325   }
00326 
00327   time_t endtime = time(NULL) ;
00328   cout << "test_AllToAllvTimeDEC" << myrank << " begintime " << begintime << " endtime " << endtime
00329        << " elapse " << endtime-begintime << " " << maxtime/deltatime[myrank]
00330        << " calls to AllToAll" << endl ;
00331 
00332   cout << "test_AllToAllvTimeDEC" << myrank << " Barrier :" << endl ;
00333   mpi_access->barrier() ;
00334   cout << "test_AllToAllvTimeDEC" << myrank << " Barrier done" << endl ;
00335 
00336   delete sourcegroup ;
00337   delete targetgroup ;
00338   delete MyMPIAccessDEC ;
00339   //  delete aLinearInterpDEC ;
00340 
00341   delete [] sendcounts ;
00342   delete [] sdispls ;
00343   delete [] recvcounts ;
00344   delete [] rdispls ;
00345   delete [] sendtimecounts ;
00346   delete [] stimedispls ;
00347   delete [] recvtimecounts ;
00348   delete [] rtimedispls ;
00349 
00350   //  MPI_Finalize();
00351 
00352   endtime = time(NULL) ;
00353 
00354   cout << "test_AllToAllvTimeDEC" << myrank << " OK begintime " << begintime << " endtime " << endtime
00355        << " elapse " << endtime-begintime << " " << maxtime/deltatime[myrank]
00356        << " calls to AllToAll" << endl ;
00357 
00358   return ;
00359 }
00360 
00361 
00362 
00363