Back to index

salome-med  6.5.0
test_AllToAllTimeDEC.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 
00026 #include "MPIAccessDECTest.hxx"
00027 #include <cppunit/TestAssert.h>
00028 
00029 //#include "CommInterface.hxx"
00030 //#include "ProcessorGroup.hxx"
00031 //#include "MPIProcessorGroup.hxx"
00032 #include "MPIAccessDEC.hxx"
00033 #include "LinearTimeInterpolator.hxx"
00034 
00035 // use this define to enable lines, execution of which leads to Segmentation Fault
00036 #define ENABLE_FAULTS
00037 
00038 // use this define to enable CPPUNIT asserts and fails, showing bugs
00039 #define ENABLE_FORCED_FAILURES
00040 
00041 using namespace std;
00042 using namespace ParaMEDMEM;
00043 
00044 void MPIAccessDECTest::test_AllToAllTimeDECSynchronousPointToPoint() {
00045   test_AllToAllTimeDEC( false ) ;
00046 }
00047 void MPIAccessDECTest::test_AllToAllTimeDECAsynchronousPointToPoint() {
00048   test_AllToAllTimeDEC( true ) ;
00049 }
00050 
00051 static void chksts( int sts , int myrank , ParaMEDMEM::MPIAccess * mpi_access ) {
00052   char msgerr[MPI_MAX_ERROR_STRING] ;
00053   int lenerr ;
00054   if ( sts != MPI_SUCCESS ) {
00055     mpi_access->errorString(sts, msgerr, &lenerr) ;
00056     cout << "test_AllToAllTimeDEC" << myrank << " lenerr " << lenerr << " "
00057          << msgerr << endl ;
00058     ostringstream strstream ;
00059     strstream << "==========================================================="
00060               << "test_AllToAllTimeDEC" << myrank << " KO"
00061               << "==========================================================="
00062               << endl ;
00063     cout << strstream.str() << endl ;
00064     CPPUNIT_FAIL( strstream.str() ) ;
00065   }
00066   return ;
00067 }
00068 
00069 void MPIAccessDECTest::test_AllToAllTimeDEC( bool Asynchronous ) {
00070 
00071   cout << "test_AllToAllTimeDEC" << endl ;
00072 
00073   //  MPI_Init(&argc, &argv) ; 
00074 
00075   int size ;
00076   int myrank ;
00077   MPI_Comm_size(MPI_COMM_WORLD,&size) ;
00078   MPI_Comm_rank(MPI_COMM_WORLD,&myrank) ;
00079 
00080   if ( size < 2 || size > 11 ) {
00081     ostringstream strstream ;
00082     strstream << "usage :" << endl
00083               << "mpirun -np <nbprocs> test_AllToAllTimeDEC" << endl
00084               << " (nbprocs >=2)" << endl
00085               << "test must be runned with more than 1 proc and less than 12 procs"
00086               << endl ;
00087     cout << strstream.str() << endl ;
00088     CPPUNIT_FAIL( strstream.str() ) ;
00089   }
00090 
00091   //  int Asynchronous = atoi(argv[1]);
00092 
00093   cout << "test_AllToAllTimeDEC" << myrank << " Asynchronous " << Asynchronous << endl ;
00094 
00095   ParaMEDMEM::CommInterface interface ;
00096   std::set<int> sourceprocs;
00097   std::set<int> targetprocs;
00098   int i ;
00099   for ( i = 0 ; i < size/2 ; i++ ) {
00100     sourceprocs.insert(i);
00101   }
00102   for ( i = size/2 ; i < size ; i++ ) {
00103     targetprocs.insert(i);
00104   }
00105 
00106   ParaMEDMEM::MPIProcessorGroup* sourcegroup = new ParaMEDMEM::MPIProcessorGroup(interface,sourceprocs) ;
00107   ParaMEDMEM::MPIProcessorGroup* targetgroup = new ParaMEDMEM::MPIProcessorGroup(interface,targetprocs) ;
00108 
00109   //  LinearTimeInterpolator * aLinearInterpDEC = new LinearTimeInterpolator( 0.5 ) ;
00110   MPIAccessDEC * MyMPIAccessDEC = new MPIAccessDEC( *sourcegroup , *targetgroup ,
00111                                                     Asynchronous ) ;
00112   //                                                    Asynchronous , LinearInterp , 0.5 ) ;
00113   MyMPIAccessDEC->setTimeInterpolator( LinearTimeInterp ) ;
00114   MPIAccess * mpi_access = MyMPIAccessDEC->getMPIAccess() ;
00115 
00116   cout << "test_AllToAllTimeDEC" << myrank << " Barrier :" << endl ;
00117   mpi_access->barrier() ;
00118   cout << "test_AllToAllTimeDEC" << myrank << " Barrier done" << endl ;
00119   
00120 #define maxproc 11
00121 #define maxreq 10000
00122 #define datamsglength 10
00123 
00124   int sts ;
00125   int sendcount = datamsglength ;
00126   int recvcount = datamsglength ;
00127 
00128   double time = 0 ;
00129   //  double deltatime[maxproc] = {1.,2.1,3.2,4.3,5.4,6.5,7.6,8.7,9.8,10.9,11.} ;
00130   double deltatime[maxproc] = {1.,2.,3.,4.,5.,6.,7.,8.,9.,10.,11.} ;
00131   double maxtime = maxreq ;
00132   double nextdeltatime = deltatime[myrank] ;
00133   //  MyMPIAccessDEC->InitTime( time , deltatime[myrank] , maxtime ) ;
00134   //  for ( time = 0 ; time <= maxtime ; time+=deltatime[myrank] ) {
00135   for ( time = 0 ; time <= maxtime && nextdeltatime != 0 ; time+=nextdeltatime ) {
00136     if ( time != 0 ) {
00137       nextdeltatime = deltatime[myrank] ;
00138       if ( time+nextdeltatime > maxtime ) {
00139         nextdeltatime = 0 ;
00140       }
00141       //       MyMPIAccessDEC->NextTime( nextdeltatime ) ;
00142     }
00143     MyMPIAccessDEC->setTime( time , nextdeltatime ) ;
00144     cout << "test_AllToAllTimeDEC" << myrank << "=====TIME " << time << "=====DELTATIME "
00145          << nextdeltatime << "=====MAXTIME " << maxtime << " ======" << endl ; 
00146     int * sendbuf = new int[datamsglength*size] ;
00147     //     int * sendbuf = (int *) malloc(sizeof(int)*datamsglength*size) ;
00148     int * recvbuf = new int[datamsglength*size] ;
00149     int j ;
00150     for ( j = 0 ; j < datamsglength*size ; j++ ) {
00151       sendbuf[j] = myrank*1000000 + (j/datamsglength)*1000 + j ;
00152       recvbuf[j] = -1 ;
00153     }
00154 
00155     int sts = MyMPIAccessDEC->allToAllTime( sendbuf, sendcount , MPI_INT ,
00156                                             recvbuf, recvcount , MPI_INT ) ;
00157     chksts( sts , myrank , mpi_access ) ;
00158 
00159     //     cout << "test_AllToAllTimeDEC" << myrank << " recvbuf before CheckSent" ;
00160     //     for ( i = 0 ; i < datamsglength*size ; i++ ) {
00161     //        cout << " " << recvbuf[i] ;
00162     //     }
00163     //     cout << endl ;
00164 
00165     //     cout << "test_AllToAllTimeDEC" << myrank << " sendbuf " << sendbuf << endl ;
00166     //     MyMPIAccessDEC->CheckSent() ;
00167 
00168     int nRecvReq = mpi_access->recvRequestIdsSize() ;
00169     if ( nRecvReq != 0 ) {
00170       ostringstream strstream ;
00171       strstream << "=============================================================" << endl
00172                 << "test_AllToAllTimeDEC" << myrank << " WaitAllRecv " << nRecvReq << " Requests # 0 ERROR"
00173                 << endl << "============================================================="
00174                 << endl ;
00175       int *ArrayOfRecvRequests = new int[nRecvReq] ;
00176       int nReq = mpi_access->recvRequestIds( nRecvReq, ArrayOfRecvRequests ) ;
00177       mpi_access->waitAll( nReq , ArrayOfRecvRequests ) ;
00178       delete [] ArrayOfRecvRequests ;
00179       cout << strstream.str() << endl ;
00180       CPPUNIT_FAIL( strstream.str() ) ;
00181     }
00182 
00183     //     cout << "test_AllToAllTimeDEC" << myrank << " recvbuf" << endl ;
00184     bool badrecvbuf = false ;
00185     for ( i = 0 ; i < datamsglength*size ; i++ ) {
00186       if ( recvbuf[i] != (i/datamsglength)*1000000 + myrank*1000 +
00187            myrank*datamsglength+(i%datamsglength) ) {
00188         badrecvbuf = true ;
00189         cout << "test_AllToAllTimeDEC" << myrank << " recvbuf[" << i << "] "
00190              << recvbuf[i] << " # " << (i/datamsglength)*1000000 + myrank*1000 +
00191           myrank*datamsglength+(i%datamsglength) << endl ;
00192       }
00193       else if ( badrecvbuf ) {
00194         cout << "test_AllToAllTimeDEC" << myrank << " recvbuf[" << i << "] "
00195              << recvbuf[i] << " == " << (i/datamsglength)*1000000 + myrank*1000 +
00196           myrank*datamsglength+(i%datamsglength) << endl ;
00197       }
00198     }
00199     if ( badrecvbuf ) {
00200       ostringstream strstream ;
00201       strstream << "==============================================================" << endl
00202                 << "test_AllToAllTimeDEC" << myrank << " badrecvbuf"
00203                 << endl << "============================================================="
00204                 << endl ;
00205       cout << strstream.str() << endl ;
00206       CPPUNIT_FAIL( strstream.str() ) ;
00207     }
00208     delete [] recvbuf ;
00209   }
00210 
00211   cout << "test_AllToAllTimeDEC" << myrank << " final CheckSent" << endl ;
00212   sts = MyMPIAccessDEC->checkSent() ;
00213   if ( sts != MPI_SUCCESS ) {
00214     ostringstream strstream ;
00215     strstream << "================================================================" << endl
00216               << "test_AllToAllTimeDEC" << myrank << " final CheckSent ERROR"
00217               << endl << "================================================================"
00218               << endl ;
00219     cout << strstream.str() << endl ;
00220     CPPUNIT_FAIL( strstream.str() ) ;
00221   }
00222 
00223   int nSendReq = mpi_access->sendRequestIdsSize() ;
00224   cout << "test_AllToAllTimeDEC" << myrank << " final SendRequestIds " << nSendReq << " SendRequests"
00225        << endl ;
00226   if ( nSendReq ) {
00227     int *ArrayOfSendRequests = new int[nSendReq] ;
00228     int nReq = mpi_access->sendRequestIds( nSendReq, ArrayOfSendRequests ) ;
00229     mpi_access->waitAll( nReq , ArrayOfSendRequests ) ;
00230     delete [] ArrayOfSendRequests ;
00231   }
00232 
00233   int nRecvReq = mpi_access->recvRequestIdsSize() ;
00234   if ( nRecvReq ) {
00235     ostringstream strstream ;
00236     strstream << "===============================================================" << endl
00237               << "test_AllToAllTimeDEC" << myrank << " RecvRequestIds " << nRecvReq
00238               << " RecvRequests # 0 Error"
00239               << endl << "==============================================================="
00240               << endl ;
00241     cout << strstream.str() << endl ;
00242     CPPUNIT_FAIL( strstream.str() ) ;
00243   }
00244   else {
00245     cout << "test_AllToAllTimeDEC" << myrank << " RecvRequestIds " << nRecvReq
00246          << " RecvRequests = 0 OK" << endl ;
00247   }
00248 
00249   cout << "test_AllToAllTimeDEC" << myrank << " Barrier :" << endl ;
00250   mpi_access->barrier() ;
00251   cout << "test_AllToAllTimeDEC" << myrank << " Barrier done" << endl ;
00252 
00253   delete sourcegroup ;
00254   delete targetgroup ;
00255   //  delete aLinearInterpDEC ;
00256   delete MyMPIAccessDEC ;
00257 
00258   //  MPI_Finalize();
00259 
00260   cout << "test_AllToAllTimeDEC" << myrank << " OK" << endl ;
00261 
00262   return ;
00263 }
00264 
00265 
00266 
00267