Back to index

salome-med  6.5.0
test_MPI_Access_Cancel.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 <time.h>
00021 #include <string>
00022 #include <vector>
00023 #include <map>
00024 #include <iostream>
00025 #include <mpi.h>
00026 
00027 #include "MPIAccessTest.hxx"
00028 #include <cppunit/TestAssert.h>
00029 
00030 //#include "CommInterface.hxx"
00031 //#include "ProcessorGroup.hxx"
00032 //#include "MPIProcessorGroup.hxx"
00033 #include "MPIAccess.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 MPIAccessTest::test_MPI_Access_Cancel() {
00045 
00046   cout << "test_MPI_Access_Cancel" << endl ;
00047 
00048 //  MPI_Init(&argc, &argv) ; 
00049 
00050   int size ;
00051   int myrank ;
00052   MPI_Comm_size(MPI_COMM_WORLD,&size) ;
00053   MPI_Comm_rank(MPI_COMM_WORLD,&myrank) ;
00054 
00055   if ( size < 2 ) {
00056     ostringstream strstream ;
00057     strstream << "test_MPI_Access_Cancel must be runned with 2 procs" << endl ;
00058     cout << strstream.str() << endl ;
00059     CPPUNIT_FAIL( strstream.str() ) ;
00060   }
00061 
00062   cout << "test_MPI_Access_Cancel" << myrank << endl ;
00063 
00064   ParaMEDMEM::CommInterface interface ;
00065 
00066   ParaMEDMEM::MPIProcessorGroup* group = new ParaMEDMEM::MPIProcessorGroup(interface) ;
00067 
00068   ParaMEDMEM::MPIAccess mpi_access( group ) ;
00069 
00070   if ( myrank >= 2 ) {
00071     mpi_access.barrier() ;
00072     delete group ;
00073     return ;
00074   }
00075 
00076   int target = 1 - myrank ;
00077   int intsendbuf[5] ;
00078   double doublesendbuf[10] ;
00079   int RequestId[10] ;
00080   int sts ;
00081   int i , j ;
00082   for ( j = 0 ; j < 3 ; j++ ) {
00083      for ( i = 0 ; i < 10 ; i++ ) {
00084         cout << "test" << myrank << " ============================ i " << i
00085              << "============================" << endl ;
00086         if ( myrank == 0 ) {
00087           if ( i < 5 ) {
00088             intsendbuf[i] = i ;
00089             sts = mpi_access.ISend(&intsendbuf[i],1,MPI_INT,target, RequestId[i]) ;
00090             cout << "test" << myrank << " Send MPI_INT RequestId " << RequestId[i]
00091                  << endl ;
00092           }
00093           else {
00094             doublesendbuf[i] = i ;
00095             sts = mpi_access.ISend(&doublesendbuf[i],1,MPI_DOUBLE,target,
00096                                    RequestId[i]) ;
00097             cout << "test" << myrank << " Send MPI_DOUBLE RequestId " << RequestId[i]
00098                  << endl ;
00099           }
00100         }
00101         else {
00102           int flag = false ;
00103           while ( !flag ) {
00104                int source, tag, outcount ;
00105                MPI_Datatype datatype ;
00106                sts = mpi_access.IProbe(target, source, tag, datatype, outcount,
00107                                        flag ) ;
00108                if ( flag ) {
00109                  cout << "test" << myrank << " " << i << " IProbe target " << target
00110                       << " source " << source << " tag " << tag
00111                       << " outcount " << outcount << " flag " << flag << endl ;
00112                }
00113                else {
00114                  cout << "test" << myrank << " flag " << flag << endl ;
00115                  sleep( 1 ) ;
00116                }
00117                if ( flag ) {
00118                  int recvbuf ;
00119                  sts = mpi_access.IRecv(&recvbuf,outcount,MPI_INT,source,
00120                                         RequestId[i] ) ;
00121                  if ( datatype == MPI_INT ) {
00122                    int source, tag, error, outcount ;
00123                    mpi_access.wait( RequestId[i] ) ;
00124                    mpi_access.status( RequestId[i], source, tag, error, outcount,
00125                                       true ) ;
00126                    if ( (outcount != 1) | (recvbuf != i) ) {
00127                      ostringstream strstream ;
00128                      strstream << "======================================================"
00129                                << endl << "test" << myrank << " outcount " << outcount
00130                                << " recvbuf " << recvbuf << " KO" << endl
00131                                << "======================================================"
00132                                << endl ;
00133                      cout << strstream.str() << endl ;
00134                      CPPUNIT_FAIL( strstream.str() ) ;
00135                    }
00136                    cout << "========================================================"
00137                         << endl << "test" << myrank << " outcount " << outcount
00138                         << " recvbuf " << recvbuf << " OK" << endl
00139                         << "========================================================"
00140                         << endl ;
00141                  }
00142                }
00143           }
00144         }
00145         char msgerr[MPI_MAX_ERROR_STRING] ;
00146         int lenerr ;
00147         mpi_access.errorString(sts, msgerr, &lenerr) ;
00148         cout << "test" << myrank << " lenerr " << lenerr << " "
00149              << msgerr << endl ;
00150         if ( sts != MPI_SUCCESS ) {
00151           ostringstream strstream ;
00152           strstream << "==========================================================="
00153                     << endl << "test" << myrank << " KO"
00154                     << "==========================================================="
00155                     << endl ;
00156           cout << strstream.str() << endl ;
00157           CPPUNIT_FAIL( strstream.str() ) ;
00158         }
00159         mpi_access.check() ;
00160      }
00161 
00162      if ( myrank != 0 ) {
00163        int iprobe ;
00164        for ( iprobe = 5 ; iprobe < 10 ; iprobe++ ) {
00165           cout << "test" << myrank << " ============================ iprobe "
00166                << iprobe << "============================" << endl ;
00167           int source, tag, outcount ;
00168           MPI_Datatype datatype ;
00169           int probeflag = false ;
00170           while ( !probeflag ) {
00171                sts = mpi_access.IProbe( target, source, tag, datatype, outcount,
00172                                         probeflag ) ;
00173                char msgerr[MPI_MAX_ERROR_STRING] ;
00174                int lenerr ;
00175                mpi_access.errorString(sts, msgerr, &lenerr) ;
00176                cout << "test" << myrank << " IProbe iprobe " << iprobe
00177                     << " target " << target << " probeflag " << probeflag
00178                     << " tag " << tag << " outcount " << outcount << " datatype "
00179                     << datatype << " lenerr " << lenerr << " " << msgerr << endl ;
00180                if ( sts != MPI_SUCCESS ) {
00181                  ostringstream strstream ;
00182                  strstream << "=========================================================="
00183                            << endl << "test" << myrank << " IProbe KO iprobe " << iprobe
00184                            << endl
00185                            << "=========================================================="
00186                            << endl ;
00187                  cout << strstream.str() << endl ;
00188                  CPPUNIT_FAIL( strstream.str() ) ;
00189                }
00190                if ( !probeflag ) {
00191                  //cout << "========================================================"
00192                  //     << endl << "test" << myrank << " IProbe KO(OK) iprobe " << iprobe
00193                  //     << " probeflag " << probeflag << endl
00194                  //     << "========================================================"
00195                  //     << endl ;
00196                }
00197                else {
00198                  cout << "test" << myrank << " " << iprobe << " IProbe target "
00199                       << target << " source " << source << " tag " << tag
00200                       << " outcount " << outcount << " probeflag " << probeflag
00201                       << endl ;
00202                  if ( datatype != MPI_DOUBLE ) {
00203                    ostringstream strstream ;
00204                    strstream << "========================================================"
00205                              << endl << "test" << myrank << " MPI_DOUBLE KO" << endl
00206                              << "========================================================"
00207                              << endl ;
00208                    cout << strstream.str() << endl ;
00209                    CPPUNIT_FAIL( strstream.str() ) ;
00210                  }
00211                  else {
00212                    int flag ;
00213                    sts = mpi_access.cancel( source, tag, datatype, outcount, flag ) ;
00214                    if ( sts != MPI_SUCCESS || !flag ) {
00215                      mpi_access.errorString(sts, msgerr, &lenerr) ;
00216                      cout << "======================================================"
00217                           << endl << "test" << myrank << " lenerr " << lenerr << " "
00218                           << msgerr << endl << "test" << myrank
00219                           << " Cancel PendingIrecv KO flag " << flag << " iprobe "
00220                           << iprobe << " Irecv completed" << endl
00221                           << "======================================================"
00222                           << endl ;
00223                      //return 1 ;
00224                    }
00225                    else {
00226                      cout << "======================================================"
00227                           << endl << "test" << myrank
00228                           << " Cancel PendingIrecv OK RequestId " << " flag "
00229                           << flag << " iprobe " << iprobe << endl
00230                           << "======================================================"
00231                           << endl ;
00232                    }
00233                  }
00234                  int Reqtarget, Reqtag, Reqerror, Reqoutcount ;
00235                  mpi_access.status( RequestId[iprobe], Reqtarget, Reqtag, Reqerror,
00236                                     Reqoutcount, true ) ;
00237                  cout << "test" << myrank << " Status Reqtarget "<< Reqtarget
00238                       << " Reqtag " << Reqtag << " Reqoutcount " << Reqoutcount
00239                       << endl ;
00240                  int Reqflag ;
00241                  sts = mpi_access.cancel( RequestId[iprobe] , Reqflag ) ;
00242                  cout << "test" << myrank << " " << iprobe
00243                       << " Cancel Irecv done Reqtarget " << Reqtarget
00244                       << " Reqtag " << Reqtag << " Reqoutcount " << Reqoutcount
00245                       << " Reqflag " << Reqflag << endl ;
00246                  if ( sts != MPI_SUCCESS || !Reqflag ) {
00247                    mpi_access.errorString(sts, msgerr, &lenerr) ;
00248                    ostringstream strstream ;
00249                    strstream << "========================================================"
00250                              << endl << "test" << myrank << " lenerr " << lenerr << " "
00251                              << msgerr << endl << "test" << myrank
00252                              << " Cancel Irecv KO Reqflag " << Reqflag << " iprobe "
00253                              << iprobe << endl
00254                              << "========================================================"
00255                              << endl ;
00256                    cout << strstream.str() << endl ;
00257                    CPPUNIT_FAIL( strstream.str() ) ;
00258                  }
00259                  else {
00260                    cout << "========================================================"
00261                         << endl << "test" << myrank
00262                         << " Cancel Irecv OK RequestId " << RequestId[iprobe]
00263                         << " Reqflag " << Reqflag << " iprobe " << iprobe << endl
00264                         << "========================================================"
00265                         << endl ;
00266                    probeflag = Reqflag ;
00267                  }
00268                }
00269           }
00270        }
00271      }
00272      mpi_access.waitAll(10,RequestId) ;
00273      mpi_access.deleteRequests(10,RequestId) ;
00274   }
00275 
00276   int source, tag, outcount, flag ;
00277   MPI_Datatype datatype ;
00278   sts = mpi_access.IProbe(target, source, tag, datatype, outcount, flag ) ;
00279   char msgerr[MPI_MAX_ERROR_STRING] ;
00280   int lenerr ;
00281   mpi_access.errorString(sts, msgerr, &lenerr) ;
00282   cout << "test" << myrank << " lenerr " << lenerr << " "
00283        << msgerr << endl ;
00284   if ( sts != MPI_SUCCESS || flag ) {
00285     ostringstream strstream ;
00286     strstream << "==========================================================="
00287               << endl << "test" << myrank << " IProbe KO flag " << flag
00288               << " remaining unread/cancelled message :" << endl
00289               << " source " << source << " tag " << tag << endl
00290               << "==========================================================="
00291               << endl ;
00292     cout << strstream.str() << endl ;
00293     CPPUNIT_FAIL( strstream.str() ) ;
00294   }
00295 
00296   mpi_access.testAll(10,RequestId,flag) ;
00297   mpi_access.waitAll(10,RequestId) ;
00298   mpi_access.deleteRequests(10,RequestId) ;
00299   mpi_access.testAll(10,RequestId,flag) ;
00300   if ( !flag ) {
00301     ostringstream strstream ;
00302     strstream << "test" << myrank << " flag " << flag << " KO" << endl ;
00303     cout << strstream.str() << endl ;
00304     CPPUNIT_FAIL( strstream.str() ) ;
00305   }
00306   mpi_access.check() ;
00307 
00308   mpi_access.barrier() ;
00309 
00310   delete group ;
00311 
00312 //  MPI_Finalize();
00313 
00314   cout << "test" << myrank << " OK" << endl ;
00315 
00316   return ;
00317 }
00318 
00319 
00320 
00321