Back to index

salome-med  6.5.0
test_MPI_Access_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 
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_Send_Recv() {
00044 
00045   cout << "test_MPI_Access_Send_Recv" << endl ;
00046 
00047   int size ;
00048   int myrank ;
00049   MPI_Comm_size(MPI_COMM_WORLD,&size) ;
00050   MPI_Comm_rank(MPI_COMM_WORLD,&myrank) ;
00051 
00052   if ( size < 2 ) {
00053     cout << "test_MPI_Access_Send_Recv must be runned with 2 procs" << endl ;
00054     CPPUNIT_FAIL("test_MPI_Access_Send_Recv must be runned with 2 procs") ;
00055   }
00056 
00057   cout << "test_MPI_Access_Send_Recv" << myrank << endl ;
00058 
00059   ParaMEDMEM::CommInterface interface ;
00060 
00061   ParaMEDMEM::MPIProcessorGroup* group = new ParaMEDMEM::MPIProcessorGroup(interface) ;
00062 
00063   ParaMEDMEM::MPIAccess mpi_access( group ) ;
00064 
00065   if ( myrank >= 2 ) {
00066     mpi_access.barrier() ;
00067     delete group ;
00068     return ;
00069   }
00070 
00071   int target = 1 - myrank ;
00072   int RequestId[10] ;
00073   int sts ;
00074   int i ;
00075   for ( i = 0 ; i < 10 ; i++ ) {
00076      if ( myrank == 0 ) {
00077        sts = mpi_access.send(&i,1,MPI_INT,target, RequestId[i]) ;
00078        cout << "test" << myrank << " Send RequestId " << RequestId[i]
00079             << " tag " << mpi_access.sendMPITag(target) << endl ;
00080      }
00081      else {
00082        int recvbuf ;
00083        int outcount ;
00084        sts = mpi_access.recv(&recvbuf,1,MPI_INT,target, RequestId[i],&outcount) ;
00085        //int source, tag, error, outcount ;
00086        //mpi_access.Status( RequestId[i], source, tag, error, outcount, true) ;
00087        cout << "test" << myrank << " Recv RequestId " << RequestId[i]
00088             << " tag " << mpi_access.recvMPITag(target)
00089             << " outcount " << outcount << endl ;
00090        if ( (outcount != 1) | (recvbuf != i) ) {
00091          ostringstream strstream ;
00092          strstream << "==========================================================="
00093                    << "test" << myrank << " outcount " << outcount
00094                    << " recvbuf " << recvbuf << " KO"
00095                    << "==========================================================="
00096                    << endl ;
00097          cout << strstream.str() << endl ;
00098          CPPUNIT_FAIL( strstream.str() ) ;
00099        }
00100      }
00101      char msgerr[MPI_MAX_ERROR_STRING] ;
00102      int lenerr ;
00103      mpi_access.errorString(sts, msgerr, &lenerr) ;
00104      cout << "test" << myrank << " lenerr " << lenerr << " "
00105           << msgerr << endl ;
00106 
00107      if ( sts != MPI_SUCCESS ) {
00108        ostringstream strstream ;
00109        strstream << "==========================================================="
00110                  << "test" << myrank << " KO"
00111                  << "==========================================================="
00112                  << endl ;
00113        CPPUNIT_FAIL( strstream.str() ) ;
00114      }
00115      mpi_access.check() ;
00116   }
00117   int flag ;
00118   mpi_access.testAll(10,RequestId,flag) ;
00119   if ( !flag ) {
00120     ostringstream strstream ;
00121     strstream << "test" << myrank << " flag " << flag << " KO" << endl ;
00122     cout << strstream.str() << endl ;
00123     CPPUNIT_FAIL( strstream.str() ) ;
00124   }
00125   mpi_access.waitAll(10,RequestId) ;
00126   mpi_access.check() ;
00127 
00128   if ( myrank == 0 ) {
00129     int sendrequests[10] ;
00130     int sendreqsize = mpi_access.sendRequestIds( target , 10 , sendrequests ) ;
00131     if ( sendreqsize != 0 ) {
00132       ostringstream strstream ;
00133       strstream << "=========================================================" << endl
00134                 << "test" << myrank << " sendreqsize " << sendreqsize << " KO" << endl
00135                 << "=========================================================" << endl ;
00136       cout << strstream.str() << endl ;
00137       CPPUNIT_FAIL( strstream.str() ) ;
00138     }
00139   }
00140   else {
00141     int recvrequests[10] ;
00142     int recvreqsize = mpi_access.sendRequestIds( target , 10 , recvrequests ) ;
00143     if ( recvreqsize != 0 ) {
00144       ostringstream strstream ;
00145       strstream << "=========================================================" << endl
00146                 << "test" << myrank << " recvreqsize " << recvreqsize << " KO" << endl
00147                 << "=========================================================" << endl ;
00148       cout << strstream.str() << endl ;
00149       CPPUNIT_FAIL( strstream.str() ) ;
00150     }
00151   }
00152 
00153   mpi_access.barrier() ;
00154 
00155   delete group ;
00156 
00157 //  MPI_Finalize();
00158 
00159   cout << "test" << myrank << " OK" << endl ;
00160 
00161   return ;
00162 }
00163 
00164 
00165 
00166