Back to index

salome-med  6.5.0
test_MPI_Access_Time_0.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 "MPIAccessTest.hxx"
00027 #include <cppunit/TestAssert.h>
00028 
00029 //#include "CommInterface.hxx"
00030 //#include "ProcessorGroup.hxx"
00031 //#include "MPIProcessorGroup.hxx"
00032 #include "MPIAccess.hxx"
00033 
00034 // use this define to enable lines, execution of which leads to Segmentation Fault
00035 #define ENABLE_FAULTS
00036 
00037 // use this define to enable CPPUNIT asserts and fails, showing bugs
00038 #define ENABLE_FORCED_FAILURES
00039 
00040 using namespace std;
00041 using namespace ParaMEDMEM;
00042 
00043 void chksts( int sts , int myrank , ParaMEDMEM::MPIAccess * mpi_access ) {
00044   char msgerr[MPI_MAX_ERROR_STRING] ;
00045   int lenerr ;
00046   if ( sts != MPI_SUCCESS ) {
00047     mpi_access->errorString(sts, msgerr, &lenerr) ;
00048     cout << "test" << myrank << " lenerr " << lenerr << " "
00049          << msgerr << endl ;
00050     ostringstream strstream ;
00051     strstream << "==========================================================="
00052               << "test" << myrank << " KO"
00053               << "==========================================================="
00054               << endl ;
00055     cout << strstream.str() << endl ;
00056     CPPUNIT_FAIL( strstream.str() ) ;
00057   }
00058 return ;
00059 }
00060 
00061 void MPIAccessTest::test_MPI_Access_Time_0() {
00062 
00063   cout << "test_MPI_Access_Time_0" << endl ;
00064 
00065 //  MPI_Init(&argc, &argv) ; 
00066 
00067   int size ;
00068   int myrank ;
00069   MPI_Comm_size(MPI_COMM_WORLD,&size) ;
00070   MPI_Comm_rank(MPI_COMM_WORLD,&myrank) ;
00071 
00072   if ( size < 2 ) {
00073     ostringstream strstream ;
00074     strstream << "usage :" << endl
00075               << "mpirun -np <nbprocs> test_MPI_Access_Time_0" <<endl
00076               << " nbprocs =2" << endl
00077               << "test must be runned with 2 procs" << endl ;
00078     cout << strstream.str() << endl ;
00079     CPPUNIT_FAIL( strstream.str() ) ;
00080   }
00081 
00082 #define maxreq 100
00083 
00084   double t ;
00085   double dt[2] = {2., 1.} ;
00086   double maxt = maxreq/dt[myrank] ;
00087 
00088   cout << "test_MPI_Access_Time_0 rank" << myrank << endl ;
00089 
00090   ParaMEDMEM::CommInterface interface ;
00091 
00092   ParaMEDMEM::MPIProcessorGroup* group = new ParaMEDMEM::MPIProcessorGroup(interface) ;
00093 
00094   ParaMEDMEM::MPIAccess * mpi_access = new ParaMEDMEM::MPIAccess( group ) ;
00095 
00096   if ( myrank >= 2 ) {
00097     cout << "test_MPI_Access_Time_0 rank" << myrank << " --> mpi_access->barrier" << endl ;
00098     mpi_access->barrier() ;
00099     cout << "test_MPI_Access_Time_0 rank" << myrank << " <-- mpi_access->barrier" << endl ;
00100     cout << "test_MPI_Access_Time_0 rank" << myrank << " --> mpi_access->barrier" << endl ;
00101     mpi_access->barrier() ;
00102     cout << "test_MPI_Access_Time_0 rank" << myrank << " <-- mpi_access->barrier" << endl ;
00103     delete group ;
00104     delete mpi_access ;
00105     cout << "test_MPI_Access_Time" << myrank << " OK" << endl ;
00106     return ;
00107   }
00108 
00109   int target = 1 - myrank ;
00110   int SendTimeRequestId[maxreq] ;
00111   int RecvTimeRequestId[maxreq] ;
00112   int SendRequestId[maxreq] ;
00113   int RecvRequestId[maxreq] ;
00114   int sts ;
00115   int sendbuf[maxreq] ;
00116   int recvbuf[maxreq] ;
00117   ParaMEDMEM::TimeMessage aSendTimeMsg[maxreq] ;
00118   int lasttime = -1 ;
00119   ParaMEDMEM::TimeMessage RecvTimeMessages[maxreq+1] ;
00120   ParaMEDMEM::TimeMessage *aRecvTimeMsg = &RecvTimeMessages[1] ;
00121 //  mpi_access->Trace() ;
00122   int istep = 0 ;
00123   for ( t = 0 ; t < maxt ; t = t+dt[myrank] ) {
00124      cout << "test" << myrank << " ==========================TIME " << t
00125           << " ==========================" << endl ;
00126      if ( myrank == 0 ) {
00127        aSendTimeMsg[istep].time = t ;
00128        aSendTimeMsg[istep].deltatime = dt[myrank] ;
00129        //aSendTimeMsg[istep].maxtime = maxt ;
00130        if ( t+dt[myrank] >= maxt ) {
00131          aSendTimeMsg[istep].deltatime = 0 ;
00132        }
00133        sts = mpi_access->ISend( &aSendTimeMsg[istep] , 1 ,
00134                                mpi_access->timeType() , target ,
00135                                SendTimeRequestId[istep]) ;
00136        cout << "test" << myrank << " ISend TimeRequestId " << SendTimeRequestId[istep]
00137             << " tag " << mpi_access->MPITag(SendTimeRequestId[istep]) << endl ;
00138        chksts( sts , myrank , mpi_access ) ;
00139        sendbuf[istep] = istep ;
00140        sts = mpi_access->ISend(&sendbuf[istep],1,MPI_INT,target, SendRequestId[istep]) ;
00141        cout << "test" << myrank << " ISend Data RequestId " << SendRequestId[istep]
00142             << " tag " << mpi_access->MPITag(SendRequestId[istep]) << endl ;
00143        chksts( sts , myrank , mpi_access ) ;
00144 //CheckSent
00145 //=========
00146        int sendrequests[2*maxreq] ;
00147        int sendreqsize = mpi_access->sendRequestIds( target , 2*maxreq ,
00148                                                     sendrequests ) ;
00149        int j , flag ;
00150        for ( j = 0 ; j < sendreqsize ; j++ ) {
00151           sts = mpi_access->test( sendrequests[j] , flag ) ;
00152           chksts( sts , myrank , mpi_access ) ;
00153           if ( flag ) {
00154             mpi_access->deleteRequest( sendrequests[j] ) ;
00155             cout << "test" << myrank << " " << j << ". " << sendrequests[j]
00156                  << " sendrequest deleted" << endl ;
00157           }
00158        }
00159      }
00160      else {
00161 //InitRecv
00162 //========
00163        if ( t == 0 ) {
00164          aRecvTimeMsg[lasttime].time = 0 ;
00165          sts = mpi_access->IRecv( &aRecvTimeMsg[lasttime+1] , 1 ,
00166                                  mpi_access->timeType() ,
00167                                  target , RecvTimeRequestId[lasttime+1]) ;
00168          cout << "test" << myrank << " t == 0 IRecv TimeRequestId "
00169               << RecvTimeRequestId[lasttime+1]
00170               << " MPITag " << mpi_access->MPITag( RecvTimeRequestId[lasttime+1] )
00171               << " MPICompleted "
00172               << mpi_access->MPICompleted( RecvTimeRequestId[lasttime+1] ) << endl ;
00173          chksts( sts , myrank , mpi_access ) ;
00174        }
00175        else {
00176          cout << "test" << myrank << " t # 0 lasttime " << lasttime << endl ;
00177 //InitialOutTime
00178 //==============
00179          bool outtime = false ;
00180          if ( lasttime != -1 ) {
00181            if ( t <= aRecvTimeMsg[lasttime-1].time ) {
00182              ostringstream strstream ;
00183              strstream << "==========================================================="
00184                        << endl << "test" << myrank << " t " << t << " <= "
00185                        << "aRecvTimeMsg[ " << lasttime << "-1 ].time "
00186                        << aRecvTimeMsg[lasttime-1].time << " KO" << endl
00187                        << "==========================================================="
00188                        << endl ;
00189              cout << strstream.str() << endl ;
00190              CPPUNIT_FAIL( strstream.str() ) ;
00191            }
00192            else {
00193              cout << "==========================================================="
00194                   << endl << "test" << myrank << " t " << t << " > "
00195                   << "aRecvTimeMsg[ " << lasttime << "-1 ].time "
00196                   << aRecvTimeMsg[lasttime-1].time << " OK" << endl
00197                   << "==========================================================="
00198                   << endl ;
00199            }
00200            //outtime = ((aRecvTimeMsg[lasttime].time +
00201            //            aRecvTimeMsg[lasttime].deltatime) >=
00202            //           aRecvTimeMsg[lasttime].maxtime) ;
00203            outtime = aRecvTimeMsg[lasttime].deltatime == 0 ;
00204          }
00205 // CheckRecv - CheckTime
00206 // On a lasttime tel que :
00207 // aRecvTimeMsg[ lasttime-1 ].time < T(i-1) <= aRecvTimeMsg[ lasttime ].time
00208 // On cherche lasttime tel que :
00209 // aRecvTimeMsg[ lasttime-1 ].time < T(i) <= aRecvTimeMsg[ lasttime ].time
00210          if ( t <= aRecvTimeMsg[lasttime].time ) {
00211            outtime = false ;
00212          }
00213          cout << "test" << myrank << " while outtime( " << outtime << " && t " << t
00214               << " > aRecvTimeMsg[ " << lasttime << " ] "
00215               << aRecvTimeMsg[lasttime].time << " )" << endl ;
00216          while ( !outtime && (t > aRecvTimeMsg[lasttime].time) ) {
00217               lasttime += 1 ;
00218 //TimeMessage
00219 //===========
00220               sts = mpi_access->wait( RecvTimeRequestId[lasttime] ) ;
00221               chksts( sts , myrank , mpi_access ) ;
00222               cout << "test" << myrank << " Wait done RecvTimeRequestId "
00223                    << RecvTimeRequestId[lasttime] << " lasttime " << lasttime
00224                    << " tag " << mpi_access->MPITag(RecvTimeRequestId[lasttime])
00225                    << aRecvTimeMsg[lasttime] << endl ;
00226               if ( lasttime == 0 ) {
00227                 aRecvTimeMsg[lasttime-1] = aRecvTimeMsg[lasttime] ;
00228               }
00229               mpi_access->deleteRequest( RecvTimeRequestId[lasttime] ) ;
00230 
00231               double deltatime = aRecvTimeMsg[lasttime].deltatime ;
00232               //double maxtime = aRecvTimeMsg[lasttime].maxtime ;
00233               double nexttime = aRecvTimeMsg[lasttime].time + deltatime ;
00234               cout << "test" << myrank << " t " << t << " lasttime " << lasttime
00235                    << " deltatime " << deltatime
00236                    << " nexttime " << nexttime << endl ;
00237               //if ( nexttime < maxtime && t > nexttime ) {
00238               if ( deltatime != 0 && t > nexttime ) {
00239 //CheckRecv :
00240 //=========   
00241                 //while ( nexttime < maxtime && t > nexttime ) {
00242                 while ( deltatime != 0 && t > nexttime ) {
00243                      int source, MPITag, outcount ;
00244                      MPI_Datatype datatype ;
00245                      sts = mpi_access->probe( target , source, MPITag, datatype,
00246                                              outcount ) ;
00247                      chksts( sts , myrank , mpi_access ) ;
00248 // Cancel DataMessages jusqu'a un TimeMessage
00249                      int cancelflag ;
00250                      while ( !mpi_access->isTimeMessage( MPITag ) ) {
00251                           sts = mpi_access->cancel( source, MPITag, datatype, outcount ,
00252                           //sts = mpi_access->cancel( source, datatype, outcount ,
00253                                                    //RecvRequestId[lasttime] ,
00254                                                    cancelflag ) ;
00255                           cout << "test" << myrank << " Recv TO CANCEL RequestId "
00256                                << RecvRequestId[lasttime]
00257                                << " tag " << mpi_access->recvMPITag( target )
00258                                << " cancelflag " << cancelflag << endl ;
00259                           chksts( sts , myrank , mpi_access ) ;
00260                           sts = mpi_access->probe( target , source, MPITag, datatype,
00261                                                   outcount ) ;
00262                           chksts( sts , myrank , mpi_access ) ;
00263                      }
00264 //On peut avancer en temps
00265                      nexttime += deltatime ;
00266                      //if ( nexttime < maxtime && t > nexttime ) {
00267                      if ( deltatime != 0 && t > nexttime ) {
00268 // Cancel du TimeMessage
00269                        sts = mpi_access->cancel( source, MPITag, datatype, outcount ,
00270                        //sts = mpi_access->cancel( source, datatype, outcount ,
00271                                                 //RecvRequestId[lasttime] ,
00272                                                 cancelflag ) ;
00273                        cout << "test" << myrank << " Time TO CANCEL RequestId "
00274                             << RecvRequestId[lasttime]
00275                             << " tag " << mpi_access->recvMPITag( target )
00276                             << " cancelflag " << cancelflag << endl ;
00277                        chksts( sts , myrank , mpi_access ) ;
00278                      }
00279                 }
00280               }
00281               else {
00282 //DoRecv
00283 //======
00284                 cout << "test" << myrank << " Recv target " << target
00285                      << " lasttime " << lasttime
00286                      << " lasttime-1 " << aRecvTimeMsg[lasttime-1]
00287                      << " lasttime " << aRecvTimeMsg[lasttime]
00288                      << endl ;
00289                 sts = mpi_access->recv(&recvbuf[lasttime],1,MPI_INT,target,
00290                                        RecvRequestId[lasttime]) ;
00291                 cout << "test" << myrank << " Recv RequestId "
00292                      << RecvRequestId[lasttime]
00293                      << " tag " << mpi_access->recvMPITag( target )
00294                      << endl ;
00295                 chksts( sts , myrank , mpi_access ) ;
00296               }
00297               //outtime = ((aRecvTimeMsg[lasttime].time +
00298               //            aRecvTimeMsg[lasttime].deltatime) >=
00299               //           aRecvTimeMsg[lasttime].maxtime) ;
00300               outtime = aRecvTimeMsg[lasttime].deltatime == 0 ;
00301               if ( !outtime ) {
00302 // Une lecture asynchrone d'un message temps a l'avance
00303                 sts = mpi_access->IRecv( &aRecvTimeMsg[lasttime+1] , 1 ,
00304                                         mpi_access->timeType() , target ,
00305                                         RecvTimeRequestId[lasttime+1]) ;
00306                 cout << "test" << myrank << " IRecv TimeRequestId "
00307                      << RecvTimeRequestId[lasttime+1] << " MPITag "
00308                      << mpi_access->MPITag( RecvTimeRequestId[lasttime+1] )
00309                      << " MPICompleted "
00310                      << mpi_access->MPICompleted( RecvTimeRequestId[lasttime+1] )
00311                      << endl ;
00312                 chksts( sts , myrank , mpi_access ) ;
00313               }
00314               else if ( t <= aRecvTimeMsg[lasttime].time ) {
00315                 outtime = false ;
00316               }
00317          }
00318          
00319          //printf("DEBUG t %.15f Msg[lasttime-1] %.15f Msg[lasttime] %.15f \n",t,
00320          //       aRecvTimeMsg[lasttime-1].time,aRecvTimeMsg[lasttime].time) ;
00321          if ( ((t <= aRecvTimeMsg[lasttime-1].time) ||
00322                (t > aRecvTimeMsg[lasttime].time)) && !outtime ) {
00323            ostringstream strstream ;
00324            strstream << "==========================================================="
00325                      << endl << "test" << myrank << " t " << t << " <= "
00326                      << "aRecvTimeMsg[ " << lasttime << "-1 ].time "
00327                      << aRecvTimeMsg[lasttime-1].time << " ou t " << t << " > "
00328                      << "aRecvTimeMsg[ " << lasttime << " ].time "
00329                      << aRecvTimeMsg[lasttime].time << endl
00330                      << " ou bien outtime " << outtime << " KO RequestTimeIds "
00331                      << RecvTimeRequestId[lasttime-1] << " " << RecvTimeRequestId[lasttime]
00332                      << " RequestIds "
00333                      << RecvRequestId[lasttime-1] << " " << RecvRequestId[lasttime] << endl
00334                      << "==========================================================="
00335                      << endl ;
00336            cout << strstream.str() << endl ;
00337            CPPUNIT_FAIL( strstream.str() ) ;
00338          }
00339          else {
00340            cout << "==========================================================="
00341                 << endl << "test" << myrank 
00342                 << " aRecvTimeMsg[ " << lasttime << "-1 ].time "
00343                 << aRecvTimeMsg[lasttime-1].time << " < t " << t << " <= "
00344                 << "aRecvTimeMsg[ " << lasttime << " ].time "
00345                 << aRecvTimeMsg[lasttime].time << endl
00346                 << " ou bien outtime " << outtime << " OK RequestTimeIds "
00347                 << RecvTimeRequestId[lasttime-1] << " " << RecvTimeRequestId[lasttime]
00348                 << " RequestIds "
00349                 << RecvRequestId[lasttime-1] << " " << RecvRequestId[lasttime] << endl
00350                 << "==========================================================="
00351                 << endl ;
00352          }
00353        }
00354      }
00355      chksts( sts , myrank , mpi_access ) ;
00356      istep = istep + 1 ;
00357   }
00358 
00359   cout << "test" << myrank << " Barrier :" << endl ;
00360   mpi_access->barrier() ;
00361 
00362   mpi_access->check() ;
00363 
00364   if ( myrank == 0 ) {
00365 //CheckFinalSent
00366 //==============
00367     cout << "test" << myrank << " CheckFinalSent :" << endl ;
00368     int sendrequests[2*maxreq] ;
00369     int sendreqsize = mpi_access->sendRequestIds( target , 2*maxreq , sendrequests ) ;
00370     int j ;
00371     for ( j = 0 ; j < sendreqsize ; j++ ) {
00372        sts = mpi_access->wait( sendrequests[j] ) ;
00373        chksts( sts , myrank , mpi_access ) ;
00374        mpi_access->deleteRequest( sendrequests[j] ) ;
00375        cout << "test" << myrank << " " << j << ". " << sendrequests[j] << " deleted"
00376             << endl ;
00377     }
00378   }
00379   else {
00380     cout << "test" << myrank << " CheckFinalRecv :" << endl ;
00381     int recvrequests[2*maxreq] ;
00382     int recvreqsize = mpi_access->recvRequestIds( target , 2*maxreq , recvrequests ) ;
00383     int cancelflag ;
00384     int j ;
00385     for ( j = 0 ; j < recvreqsize ; j++ ) {
00386        sts = mpi_access->cancel( recvrequests[j] , cancelflag ) ;
00387        chksts( sts , myrank , mpi_access ) ;
00388        mpi_access->deleteRequest( recvrequests[j] ) ;
00389        cout << "test" << myrank << " " << j << ". " << recvrequests[j] << " deleted"
00390             << " cancelflag " << cancelflag << endl ;
00391     }
00392     int source, MPITag, outcount , flag ;
00393     MPI_Datatype datatype ;
00394     sts = mpi_access->IProbe( target , source, MPITag, datatype,
00395                              outcount , flag ) ;
00396     chksts( sts , myrank , mpi_access ) ;
00397     while ( flag ) {
00398          sts = mpi_access->cancel( source, MPITag, datatype, outcount ,
00399          //sts = mpi_access->cancel( source, datatype, outcount ,
00400                                   //RecvRequestId[lasttime] ,
00401                                   cancelflag ) ;
00402          cout << "test" << myrank << " TO CANCEL RequestId "
00403               << RecvRequestId[lasttime]
00404               << " tag " << mpi_access->recvMPITag( target )
00405               << " cancelflag " << cancelflag << endl ;
00406          chksts( sts , myrank , mpi_access ) ;
00407          sts = mpi_access->IProbe( target , source, MPITag, datatype,
00408                                   outcount , flag ) ;
00409          chksts( sts , myrank , mpi_access ) ;
00410     }
00411   }
00412   mpi_access->check() ;
00413 
00414   if ( myrank == 0 ) {
00415     int sendrequests[2*maxreq] ;
00416     int sendreqsize = mpi_access->sendRequestIds( target , 2*maxreq , sendrequests ) ;
00417     if ( sendreqsize != 0 ) {
00418       ostringstream strstream ;
00419       strstream << "=========================================================" << endl
00420                 << "test" << myrank << " sendreqsize " << sendreqsize << " KO" << endl
00421                 << "=========================================================" << endl ;
00422       cout << strstream.str() << endl ;
00423       CPPUNIT_FAIL( strstream.str() ) ;
00424     }
00425     else {
00426       cout << "=========================================================" << endl
00427            << "test" << myrank << " sendreqsize " << sendreqsize << " OK" << endl
00428            << "=========================================================" << endl ;
00429     }
00430   }
00431   else {
00432     int recvrequests[2*maxreq] ;
00433     int recvreqsize = mpi_access->recvRequestIds( target , 2*maxreq , recvrequests ) ;
00434     if ( recvreqsize != 0 ) {
00435       ostringstream strstream ;
00436       strstream << "=========================================================" << endl
00437                 << "test" << myrank << " recvreqsize " << recvreqsize << " KO" << endl
00438                 << "=========================================================" << endl ;
00439       cout << strstream.str() << endl ;
00440       CPPUNIT_FAIL( strstream.str() ) ;
00441     }
00442     else {
00443       cout << "=========================================================" << endl
00444            << "test" << myrank << " recvreqsize " << recvreqsize << " OK" << endl
00445            << "=========================================================" << endl ;
00446     }
00447   }
00448 
00449   int i ;
00450   for ( i = 0 ; i <= lasttime ; i++ ) {
00451      cout << "test" << myrank << " " << i << ". RecvTimeMsg "
00452           << aRecvTimeMsg[i].time << " recvbuf " << recvbuf[i] << endl ;
00453   }
00454 
00455   cout << "test_MPI_Access_Time_0 rank" << myrank << " --> mpi_access->barrier" << endl ;
00456   mpi_access->barrier() ;
00457   cout << "test_MPI_Access_Time_0 rank" << myrank << " <-- mpi_access->barrier" << endl ;
00458 
00459   delete group ;
00460   delete mpi_access ;
00461 
00462 //  MPI_Finalize();
00463 
00464   cout << "test" << myrank << " OK" << endl ;
00465 
00466   return ;
00467 }
00468 
00469 
00470 
00471