Back to index

salome-med  6.5.0
test_MPI_Access_Cyclic_Send_Recv.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 #include "MPIAccessTest.hxx"
00026 #include <cppunit/TestAssert.h>
00027 
00028 //#include "CommInterface.hxx"
00029 //#include "ProcessorGroup.hxx"
00030 //#include "MPIProcessorGroup.hxx"
00031 #include "MPIAccess.hxx"
00032 
00033 // use this define to enable lines, execution of which leads to Segmentation Fault
00034 #define ENABLE_FAULTS
00035 
00036 // use this define to enable CPPUNIT asserts and fails, showing bugs
00037 #define ENABLE_FORCED_FAILURES
00038 
00039 using namespace std;
00040 using namespace ParaMEDMEM;
00041 
00042 void MPIAccessTest::test_MPI_Access_Cyclic_Send_Recv() {
00043 
00044   cout << "test_MPI_Access_Cyclic_Send_Recv" << endl ;
00045 
00046 //  MPI_Init(&argc, &argv) ; 
00047 
00048   int size ;
00049   int myrank ;
00050   MPI_Comm_size(MPI_COMM_WORLD,&size) ;
00051   MPI_Comm_rank(MPI_COMM_WORLD,&myrank) ;
00052 
00053   if ( size < 3 ) {
00054     cout << "test_MPI_Access_Send_Recv must be runned with 3 procs" << endl ;
00055     CPPUNIT_FAIL("test_MPI_Access_Send_Recv must be runned with 3 procs") ;
00056   }
00057 
00058   cout << "test_MPI_Access_Cyclic_Send_Recv" << myrank << endl ;
00059 
00060   ParaMEDMEM::CommInterface interface ;
00061 
00062   ParaMEDMEM::MPIProcessorGroup* group = new ParaMEDMEM::MPIProcessorGroup(interface) ;
00063 
00064   ParaMEDMEM::MPIAccess mpi_access( group ) ;
00065 
00066   if ( myrank >= 3 ) {
00067     mpi_access.barrier() ;
00068     delete group ;
00069     return ;
00070   }
00071 
00072   int alltarget[3] = {1 , 2 , 0 } ;
00073   int allsource[3] = {2 , 0 , 1 } ;
00074   int RequestId[10] ;
00075   int sts ;
00076   int i = 0 ;
00077   if ( myrank == 0 ) {
00078     sts = mpi_access.send(&i,1,MPI_INT,alltarget[myrank], RequestId[i]) ;
00079     cout << "test" << myrank << " Send RequestId " << RequestId[i]
00080          << " tag " << mpi_access.sendMPITag(alltarget[myrank]) << endl ;
00081   }
00082   for ( i = 0 ; i < 10 ; i++ ) {
00083      int recvbuf ;
00084      int outcount ;
00085      if ( i & 1 ) {
00086        outcount = 0 ;
00087        sts = mpi_access.recv(&recvbuf,1,MPI_INT,allsource[myrank], RequestId[i],
00088                              &outcount) ;
00089      }
00090      else {
00091        sts = mpi_access.recv(&recvbuf,1,MPI_INT,allsource[myrank], RequestId[i]) ;
00092        outcount = 1 ;
00093      }
00094      //int source, tag, error, outcount ;
00095      //mpi_access.Status( RequestId[i], source, tag, error, outcount, true) ;
00096      cout << "test" << myrank << " Recv RequestId " << RequestId[i]
00097           << " tag " << mpi_access.recvMPITag(allsource[myrank])
00098           << " outcount " << outcount << endl ;
00099      if ( (outcount != 1) | (recvbuf != i) ) {
00100        ostringstream strstream ;
00101        strstream << "==========================================================="
00102                  << "test" << myrank << " outcount "
00103                  << outcount << " recvbuf " << recvbuf << " KO"
00104                  << "==========================================================="
00105                  << endl ;
00106        cout << strstream.str() << endl ;
00107        CPPUNIT_FAIL( strstream.str() ) ;
00108      }
00109      if ( myrank == 0 ) {
00110        if ( i != 9 ) {
00111          int ii = i + 1 ;
00112          sts = mpi_access.send(&ii,1,MPI_INT,alltarget[myrank], RequestId[i]) ;
00113          cout << "test" << myrank << " Send RequestId " << RequestId[i]
00114               << " tag " << mpi_access.sendMPITag(alltarget[myrank]) << endl ;
00115        }
00116      }
00117      else {
00118        sts = mpi_access.send(&i,1,MPI_INT,alltarget[myrank], RequestId[i]) ;
00119        cout << "test" << myrank << " Send RequestId " << RequestId[i]
00120             << " tag " << mpi_access.sendMPITag(alltarget[myrank]) << endl ;
00121      }
00122      char msgerr[MPI_MAX_ERROR_STRING] ;
00123      int lenerr ;
00124      mpi_access.errorString(sts, msgerr, &lenerr) ;
00125      cout << "test" << myrank << " lenerr " << lenerr
00126           << " " << msgerr << endl ;
00127 
00128      if ( sts != MPI_SUCCESS ) {
00129        ostringstream strstream ;
00130        strstream << "==========================================================="
00131                  << "test" << myrank << " KO"
00132                  << "==========================================================="
00133                  << endl ;
00134        cout << strstream.str() << endl ;
00135        CPPUNIT_FAIL( strstream.str() ) ;
00136      }
00137      mpi_access.check() ;
00138   }
00139 
00140   int flag ;
00141   mpi_access.testAll(10,RequestId,flag) ;
00142   if ( !flag ) {
00143     ostringstream strstream ;
00144     strstream << "test" << myrank << " flag " << flag << " KO" << endl ;
00145     cout << strstream.str() << endl ;
00146     CPPUNIT_FAIL( strstream.str() ) ;
00147   }
00148   mpi_access.waitAll(10,RequestId) ;
00149   mpi_access.check() ;
00150 
00151   int sendrequests[10] ;
00152   int sendreqsize = mpi_access.sendRequestIds( alltarget[myrank] , 10 ,
00153                                                sendrequests ) ;
00154   if ( sendreqsize != 0 ) {
00155     ostringstream strstream ;
00156     strstream << "=========================================================" << endl
00157               << "test" << myrank << " sendreqsize " << sendreqsize << " KO" << endl
00158               << "=========================================================" << endl ;
00159     cout << strstream.str() << endl ;
00160     CPPUNIT_FAIL( strstream.str() ) ;
00161   }
00162   int recvrequests[10] ;
00163   int recvreqsize = mpi_access.sendRequestIds( allsource[myrank] , 10 ,
00164                                                recvrequests ) ;
00165   if ( recvreqsize != 0 ) {
00166     ostringstream strstream ;
00167     strstream << "=========================================================" << endl
00168               << "test" << myrank << " recvreqsize " << recvreqsize << " KO" << endl
00169               << "=========================================================" << endl ;
00170     cout << strstream.str() << endl ;
00171     CPPUNIT_FAIL( strstream.str() ) ;
00172   }
00173 
00174   mpi_access.barrier() ;
00175 
00176   delete group ;
00177 
00178 //  MPI_Finalize();
00179 
00180   cout << "test" << myrank << " OK" << endl ;
00181 
00182   return ;
00183 }
00184 
00185 
00186 
00187