Back to index

salome-med  6.5.0
test_MPI_Access_Cyclic_ISend_IRecv.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 MPIAccessTest::test_MPI_Access_Cyclic_ISend_IRecv() {
00044 
00045   cout << "test_MPI_Access_Cyclic_ISend_IRecv" << endl ;
00046 
00047 //  MPI_Init(&argc, &argv) ; 
00048 
00049   int size ;
00050   int myrank ;
00051   MPI_Comm_size(MPI_COMM_WORLD,&size) ;
00052   MPI_Comm_rank(MPI_COMM_WORLD,&myrank) ;
00053 
00054   if ( size < 3 ) {
00055     cout << "test_MPI_Access_Cyclic_ISend_IRecv must be runned with 3 procs" << endl ;
00056     CPPUNIT_FAIL("test_MPI_Access_Cyclic_ISend_IRecv must be runned with 3 procs") ;
00057   }
00058 
00059   cout << "test_MPI_Access_Cyclic_ISend_IRecv" << myrank << endl ;
00060 
00061   ParaMEDMEM::CommInterface interface ;
00062 
00063   ParaMEDMEM::MPIProcessorGroup* group = new ParaMEDMEM::MPIProcessorGroup(interface) ;
00064 
00065   ParaMEDMEM::MPIAccess mpi_access( group ) ;
00066 
00067 #define maxsend 100
00068 
00069   if ( myrank >= 3 ) {
00070     mpi_access.barrier() ;
00071     delete group ;
00072     return ;
00073   }
00074 
00075   int alltarget[3] = {1 , 2 , 0 } ;
00076   int allsource[3] = {2 , 0 , 1 } ;
00077   int SendRequestId[maxsend] ;
00078   int RecvRequestId[maxsend] ;
00079   int sendbuf[maxsend] ;
00080   int recvbuf[maxsend] ;
00081   int sts ;
00082   int i = 0 ;
00083   if ( myrank == 0 ) {
00084     sendbuf[i] = i ;
00085     sts = mpi_access.ISend(&sendbuf[i],1,MPI_INT,alltarget[myrank],
00086                            SendRequestId[i]) ;
00087     cout << "test" << myrank << " Send RequestId " << SendRequestId[i]
00088          << " tag " << mpi_access.sendMPITag(alltarget[myrank]) << endl ;
00089   }
00090   for ( i = 0 ; i < maxsend ; i++ ) {
00091      recvbuf[i] = -1 ;
00092      sts = mpi_access.IRecv(&recvbuf[i],1,MPI_INT,allsource[myrank],
00093                             RecvRequestId[i]) ;
00094      cout << "test" << myrank << " Recv RequestId " << RecvRequestId[i]
00095           << " tag " << mpi_access.recvMPITag(allsource[myrank]) << endl ;
00096      char msgerr[MPI_MAX_ERROR_STRING] ;
00097      int lenerr ;
00098      mpi_access.errorString(sts, msgerr, &lenerr) ;
00099      cout << "test" << myrank << " lenerr " << lenerr
00100           << " " << msgerr << endl ;
00101 
00102      if ( sts != MPI_SUCCESS ) {
00103        ostringstream strstream ;
00104        strstream << "==========================================================="
00105                  << "test" << myrank << " KO"
00106                  << "==========================================================="
00107                  << endl ;
00108        cout << strstream.str() << endl ;
00109        CPPUNIT_FAIL( strstream.str() ) ;
00110      }
00111      int j ;
00112      for (j = 0 ; j <= i ; j++) {
00113         int flag ;
00114         if ( j < i ) {
00115           cout << "test" << myrank << " " << j << " -> Test-Send("<< SendRequestId[j]
00116                << ")" << endl ;
00117           mpi_access.test( SendRequestId[j], flag ) ;
00118           if ( flag ) {
00119             int target, tag, error, outcount ;
00120             mpi_access.status( SendRequestId[j], target, tag, error, outcount,
00121                                true ) ;
00122             cout << "test" << myrank << " Send RequestId " << SendRequestId[j]
00123                  << " target " << target << " tag " << tag << " error " << error
00124                  << endl ;
00125             mpi_access.deleteRequest( SendRequestId[j] ) ;
00126           }
00127         }
00128         cout << "test" << myrank << " " << j << " -> Test-Recv("<< SendRequestId[j]
00129              << ")" << endl ;
00130         mpi_access.test( RecvRequestId[j], flag ) ;
00131         if ( flag ) {
00132           int source, tag, error, outcount ;
00133           mpi_access.status( RecvRequestId[j], source, tag, error, outcount,
00134                              true ) ;
00135           cout << "test" << myrank << " Recv RequestId" << j << " "
00136                << RecvRequestId[j] << " source " << source << " tag " << tag
00137                << " error " << error << " outcount " << outcount << endl ;
00138           if ( (outcount != 1) | (recvbuf[j] != j) ) {
00139             ostringstream strstream ;
00140             strstream << "====================================================="
00141                       << endl << "test" << myrank << " outcount "
00142                       << outcount << " recvbuf[ " << j << " ] " << recvbuf[j] << " KO"
00143                       << endl << "====================================================="
00144                       << endl ;
00145             cout << strstream.str() << endl ;
00146             CPPUNIT_FAIL( strstream.str() ) ;
00147           }
00148         }
00149      }
00150      if ( myrank == 0 ) {
00151        if ( i != maxsend-1 ) {
00152          sendbuf[i+1] = i + 1 ;
00153          sts = mpi_access.ISend(&sendbuf[i+1],1,MPI_INT,alltarget[myrank],
00154                                 SendRequestId[i+1]) ;
00155          cout << "test" << myrank << " Send RequestId " << SendRequestId[i+1]
00156               << " tag " << mpi_access.sendMPITag(alltarget[myrank]) << endl ;
00157        }
00158      }
00159      else {
00160        sendbuf[i] = i ;
00161        sts = mpi_access.ISend(&sendbuf[i],1,MPI_INT,alltarget[myrank],
00162                               SendRequestId[i]) ;
00163        cout << "test" << myrank << " Send RequestId " << SendRequestId[i]
00164             << " tag " << mpi_access.sendMPITag(alltarget[myrank]) << endl ;
00165      }
00166      mpi_access.errorString(sts, msgerr, &lenerr) ;
00167      cout << "test" << myrank << " lenerr " << lenerr
00168           << " " << msgerr << endl ;
00169 
00170      if ( sts != MPI_SUCCESS ) {
00171        ostringstream strstream ;
00172        strstream << "==========================================================="
00173                  << "test" << myrank << " KO"
00174                  << "==========================================================="
00175                  << endl ;
00176        cout << strstream.str() << endl ;
00177        CPPUNIT_FAIL( strstream.str() ) ;
00178      }
00179      mpi_access.check() ;
00180   }
00181 
00182   int flag ;
00183   mpi_access.testAll(maxsend,SendRequestId,flag) ;
00184   mpi_access.testAll(maxsend,RecvRequestId,flag) ;
00185   mpi_access.waitAll(maxsend,SendRequestId) ;
00186   mpi_access.deleteRequests(maxsend,SendRequestId) ;
00187   mpi_access.waitAll(maxsend,RecvRequestId) ;
00188   mpi_access.deleteRequests(maxsend,RecvRequestId) ;
00189   mpi_access.check() ;
00190   mpi_access.testAll(maxsend,SendRequestId,flag) ;
00191   if ( !flag ) {
00192     ostringstream strstream ;
00193     strstream << "=========================================================" << endl
00194               << "test" << myrank << " TestAllSendflag " << flag << " KO" << endl
00195               << "=========================================================" << endl ;
00196     cout << strstream.str() << endl ;
00197     CPPUNIT_FAIL( strstream.str() ) ;
00198   }
00199   else {
00200     cout << "=========================================================" << endl
00201          << "test" << myrank << " TestAllSendflag " << flag << " OK" << endl
00202          << "=========================================================" << endl ;
00203   }
00204   mpi_access.testAll(maxsend,RecvRequestId,flag) ;
00205   if ( !flag ) {
00206     ostringstream strstream ;
00207     strstream << "=========================================================" << endl
00208               << "test" << myrank << " TestAllRecvflag " << flag << " KO" << endl
00209               << "=========================================================" << endl ;
00210     cout << strstream.str() << endl ;
00211     CPPUNIT_FAIL( strstream.str() ) ;
00212   }
00213   else {
00214     cout << "=========================================================" << endl
00215          << "test" << myrank << " TestAllRecvflag " << flag << " OK" << endl
00216          << "=========================================================" << endl ;
00217   }
00218 
00219   int sendrequests[maxsend] ;
00220   int sendreqsize = mpi_access.sendRequestIds( alltarget[myrank] , maxsend ,
00221                                                sendrequests ) ;
00222   if ( sendreqsize != 0 ) {
00223     ostringstream strstream ;
00224     strstream << "=========================================================" << endl
00225               << "test" << myrank << " sendreqsize " << sendreqsize << " KO" << endl
00226               << "=========================================================" << endl ;
00227     cout << strstream.str() << endl ;
00228     int source, tag, error, outcount ;
00229     mpi_access.status(sendrequests[0], source, tag, error, outcount, true) ;
00230     cout << "test" << myrank << " RequestId " << sendrequests[0]
00231          << " source " << source << " tag " << tag << " error " << error
00232          << " outcount " << outcount << endl ;
00233     CPPUNIT_FAIL( strstream.str() ) ;
00234   }
00235   else {
00236     cout << "=========================================================" << endl
00237          << "test" << myrank << " sendreqsize " << sendreqsize << " OK" << endl
00238          << "=========================================================" << endl ;
00239   }
00240   int recvrequests[maxsend] ;
00241   int recvreqsize = mpi_access.sendRequestIds( allsource[myrank] , maxsend ,
00242                                                recvrequests ) ;
00243   if ( recvreqsize != 0 ) {
00244     ostringstream strstream ;
00245     strstream << "=========================================================" << endl
00246               << "test" << myrank << " recvreqsize " << recvreqsize << " KO" << endl
00247               << "=========================================================" << endl ;
00248     cout << strstream.str() << endl ;
00249     CPPUNIT_FAIL( strstream.str() ) ;
00250   }
00251   else {
00252     cout << "=========================================================" << endl
00253          << "test" << myrank << " recvreqsize " << recvreqsize << " OK" << endl
00254          << "=========================================================" << endl ;
00255   }
00256 
00257   mpi_access.barrier() ;
00258 
00259   delete group ;
00260 
00261 //  MPI_Finalize();
00262 
00263   cout << "test" << myrank << " OK" << endl ;
00264 
00265   return ;
00266 }
00267 
00268 
00269 
00270