Back to index

salome-med  6.5.0
test_MPI_Access_SendRecv.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_SendRecv() {
00044 
00045   cout << "MPIAccessTest::test_MPI_Access_SendRecv" << 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 << "MPIAccessTest::test_MPI_Access_SendRecv must be runned with 2 procs" << endl ;
00056     CPPUNIT_FAIL("test_MPI_Access_SendRecv must be runned with 2 procs") ;
00057   }
00058 
00059   cout << "MPIAccessTest::test_MPI_Access_SendRecv" << 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   if ( myrank >= 2 ) {
00068     mpi_access.barrier() ;
00069     delete group ;
00070     return ;
00071   }
00072 
00073   int target = 1 - myrank ;
00074   int sendRequestId[10] ;
00075   int recvRequestId[10] ;
00076   int sts ;
00077   int i ;
00078   for ( i = 0 ; i < 10 ; i++ ) {
00079      int recvbuf ;
00080      int outcount ;
00081      if ( i & 1 ) {
00082        outcount = -1 ;
00083        sts = mpi_access.sendRecv(&i,1,MPI_INT,target, sendRequestId[i],
00084                                  &recvbuf,1,MPI_INT,target, recvRequestId[i],
00085                                  &outcount) ;
00086      }
00087      else {
00088        sts = mpi_access.sendRecv(&i,1,MPI_INT,target, sendRequestId[i],
00089                                  &recvbuf,1,MPI_INT,target, recvRequestId[i]) ;
00090 //       outcount = mpi_access.MPIOutCount( recvRequestId[i] ) ;
00091        outcount = 1 ;
00092      }
00093      cout << "test" << myrank << " Send sendRequestId " << sendRequestId[i]
00094           << " tag " << mpi_access.sendMPITag(target)
00095           << " recvRequestId " << recvRequestId[i]
00096           << " tag " << mpi_access.recvMPITag(target)
00097           << " outcount " << outcount << " MPIOutCount "
00098           << mpi_access.MPIOutCount( recvRequestId[i] ) << endl ;
00099      if ( (outcount != 1) | (recvbuf != i) ) {
00100        ostringstream strstream ;
00101        strstream << "==========================================================="
00102                  << "test" << myrank << " outcount " << outcount
00103                  << " recvbuf " << recvbuf << " KO"
00104                  << "==========================================================="
00105                  << endl ;
00106        cout << strstream.str() << endl ;
00107        CPPUNIT_FAIL( strstream.str() ) ;
00108      }
00109      char msgerr[MPI_MAX_ERROR_STRING] ;
00110      int lenerr ;
00111      mpi_access.errorString(sts, msgerr, &lenerr) ;
00112      cout << "test" << myrank << " lenerr " << lenerr << " "
00113           << msgerr << endl ;
00114 
00115      if ( sts != MPI_SUCCESS ) {
00116        ostringstream strstream ;
00117        strstream << "==========================================================="
00118                  << "test" << myrank << " KO"
00119                  << "==========================================================="
00120                  << endl ;
00121        cout << strstream.str() << endl ;
00122        CPPUNIT_FAIL( strstream.str() ) ;
00123      }
00124      mpi_access.check() ;
00125   }
00126 
00127   int flag ;
00128   mpi_access.testAll(10,sendRequestId,flag) ;
00129   if ( !flag ) {
00130     ostringstream strstream ;
00131     strstream << "test" << myrank << " flag " << flag << " KO" << endl ;
00132     cout << strstream.str() << endl ;
00133     CPPUNIT_FAIL( strstream.str() ) ;
00134   }
00135   mpi_access.waitAll(10,sendRequestId) ;
00136   mpi_access.testAll(10,recvRequestId,flag) ;
00137   if ( !flag ) {
00138     ostringstream strstream ;
00139     strstream << "test" << myrank << " flag " << flag << " KO" << endl ;
00140     cout << strstream.str() << endl ;
00141     CPPUNIT_FAIL( strstream.str() ) ;
00142   }
00143   mpi_access.waitAll(10,recvRequestId) ;
00144   mpi_access.check() ;
00145 
00146   int sendrequests[10] ;
00147   int sendreqsize = mpi_access.sendRequestIds( target , 10 , sendrequests ) ;
00148   if ( sendreqsize != 0 ) {
00149     ostringstream strstream ;
00150     strstream << "=========================================================" << endl
00151               << "test" << myrank << " sendreqsize " << sendreqsize << " KO" << endl
00152               << "=========================================================" << endl ;
00153     cout << strstream.str() << endl ;
00154     CPPUNIT_FAIL( strstream.str() ) ;
00155   }
00156   int recvrequests[10] ;
00157   int recvreqsize = mpi_access.sendRequestIds( target , 10 , recvrequests ) ;
00158   if ( recvreqsize != 0 ) {
00159     ostringstream strstream ;
00160     strstream << "=========================================================" << endl
00161               << "test" << myrank << " recvreqsize " << recvreqsize << " KO" << endl
00162               << "=========================================================" << endl ;
00163     cout << strstream.str() << endl ;
00164     CPPUNIT_FAIL( strstream.str() ) ;
00165   }
00166 
00167   mpi_access.barrier() ;
00168 
00169   delete group ;
00170 
00171 //  MPI_Finalize();
00172 
00173   cout << "test" << myrank << " OK" << endl ;
00174 
00175   return ;
00176 }
00177 
00178 
00179 
00180