Back to index

salome-med  6.5.0
test_MPI_Access_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_ISend_IRecv() {
00044 
00045   cout << "test_MPI_Access_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 < 2 ) {
00055     cout << "test_MPI_Access_ISend_IRecv must be runned with 2 procs" << endl ;
00056     CPPUNIT_FAIL("test_MPI_Access_ISend_IRecv must be runned with 2 procs") ;
00057   }
00058 
00059   cout << "test_MPI_Access_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 maxreq 100
00068 
00069   if ( myrank >= 2 ) {
00070     mpi_access.barrier() ;
00071     delete group ;
00072     return ;
00073   }
00074 
00075   int target = 1 - myrank ;
00076   int SendRequestId[maxreq] ;
00077   int RecvRequestId[maxreq] ;
00078   int sts ;
00079   int sendbuf[maxreq] ;
00080   int recvbuf[maxreq] ;
00081   int i ;
00082   for ( i = 0 ; i < maxreq ; i++ ) {
00083     if ( myrank == 0 ) {
00084       sendbuf[i] = i ;
00085       sts = mpi_access.ISend(&sendbuf[i],1,MPI_INT,target, SendRequestId[i]) ;
00086       cout << "test" << myrank << " ISend RequestId " << SendRequestId[i]
00087            << " tag " << mpi_access.sendMPITag(target) << endl ;
00088     }
00089     else {
00090       sts = mpi_access.IRecv(&recvbuf[i],1,MPI_INT,target, RecvRequestId[i]) ;
00091       cout << "test" << myrank << " IRecv RequestId " << RecvRequestId[i]
00092            << " tag " << mpi_access.recvMPITag(target) << endl ;
00093     }
00094     int j ;
00095     for (j = 0 ; j <= i ; j++) {
00096       int flag ;
00097       if ( myrank == 0 ) {
00098         mpi_access.test( SendRequestId[j], flag ) ;
00099       }
00100       else {
00101         mpi_access.test( RecvRequestId[j], flag ) ;
00102       }
00103       if ( flag ) {
00104         int target,source, tag, error, outcount ;
00105         if ( myrank == 0 ) {
00106           mpi_access.status( SendRequestId[j], target, tag, error, outcount,
00107                              true ) ;
00108           cout << "test" << myrank << " Test(Send RequestId " << SendRequestId[j]
00109                << ") : target " << target << " tag " << tag << " error " << error
00110                << " flag " << flag << endl ;
00111         }
00112         else {
00113           mpi_access.status( RecvRequestId[j], source, tag, error, outcount,
00114                              true ) ;
00115           cout << "test" << myrank << " Test(Recv RequestId "
00116                << RecvRequestId[j] << ") : source " << source << " tag " << tag
00117                << " error " << error << " outcount " << outcount
00118                << " flag " << flag << endl ;
00119           if ( (outcount != 1) | (recvbuf[j] != j) ) {
00120             ostringstream strstream ;
00121             strstream << "==========================================================="
00122                       << endl << "test" << myrank << " outcount "
00123                       << outcount << " recvbuf " << recvbuf[j] << " KO" << endl
00124                       << "==========================================================="
00125                       << endl ;
00126             cout << strstream.str() << endl ;
00127             CPPUNIT_FAIL( strstream.str() ) ;
00128           }
00129           //else {
00130           //  cout << "==========================================================="
00131           //       << endl << "test" << myrank << " outcount " << outcount
00132           //       << " RequestId " << RecvRequestId[j] << " recvbuf "
00133           //       << recvbuf[j] << " OK" << endl
00134           //       << "==========================================================="
00135           //       << endl ;
00136           //}
00137         }
00138       }
00139     }
00140     char msgerr[MPI_MAX_ERROR_STRING] ;
00141     int lenerr ;
00142     mpi_access.errorString(sts, msgerr, &lenerr) ;
00143     cout << "test" << myrank << " lenerr " << lenerr << " "
00144          << msgerr << endl ;
00145 
00146     if ( sts != MPI_SUCCESS ) {
00147       ostringstream strstream ;
00148       strstream << "==========================================================="
00149                 << "test" << myrank << " KO"
00150                 << "==========================================================="
00151                 << endl ;
00152       cout << strstream.str() << endl ;
00153       CPPUNIT_FAIL( strstream.str() ) ;
00154     }
00155   }
00156 
00157   mpi_access.check() ;
00158   if ( myrank == 0 ) {
00159     mpi_access.waitAll(maxreq, SendRequestId) ;
00160     mpi_access.deleteRequests(maxreq, SendRequestId) ;
00161   }
00162   else {
00163     mpi_access.waitAll(maxreq, RecvRequestId) ;
00164     mpi_access.deleteRequests(maxreq, RecvRequestId) ;
00165   }
00166   mpi_access.check() ;
00167 
00168   if ( myrank == 0 ) {
00169     int sendrequests[maxreq] ;
00170     int sendreqsize = mpi_access.sendRequestIds( target , maxreq , sendrequests ) ;
00171     int i ;
00172     if ( sendreqsize != 0 ) {
00173       ostringstream strstream ;
00174       strstream << "=========================================================" << endl
00175                 << "test" << myrank << " sendreqsize " << sendreqsize << " KO" << endl
00176                 << "=========================================================" << endl ;
00177       cout << strstream.str() << endl ;
00178       for ( i = 0 ; i < sendreqsize ; i++ ) {
00179         cout << "test" << myrank << " sendrequests[ " << i << " ] = "
00180              << sendrequests[i] << endl ;
00181       }
00182       CPPUNIT_FAIL( strstream.str() ) ;
00183     }
00184     else {
00185       cout << "=========================================================" << endl
00186            << "test" << myrank << " sendreqsize " << sendreqsize << " OK" << endl
00187            << "=========================================================" << endl ;
00188     }
00189   }
00190   else {
00191     int recvrequests[maxreq] ;
00192     int recvreqsize = mpi_access.sendRequestIds( target , maxreq , recvrequests ) ;
00193     if ( recvreqsize != 0 ) {
00194       ostringstream strstream ;
00195       strstream << "=========================================================" << endl
00196                 << "test" << myrank << " recvreqsize " << recvreqsize << " KO" << endl
00197                 << "=========================================================" << endl ;
00198       cout << strstream.str() << endl ;
00199       CPPUNIT_FAIL( strstream.str() ) ;
00200     }
00201     else {
00202       cout << "=========================================================" << endl
00203            << "test" << myrank << " recvreqsize " << recvreqsize << " OK" << endl
00204            << "=========================================================" << endl ;
00205     }
00206   }
00207 
00208   mpi_access.barrier() ;
00209 
00210   delete group ;
00211 
00212   //  MPI_Finalize();
00213 
00214   cout << "test" << myrank << " OK" << endl ;
00215 
00216   return ;
00217 }
00218 
00219 
00220 
00221