Back to index

salome-med  6.5.0
test_AllToAllDEC.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 "MPIAccessDECTest.hxx"
00027 #include <cppunit/TestAssert.h>
00028 #include "MPIAccessDEC.hxx"
00029 
00030 // use this define to enable lines, execution of which leads to Segmentation Fault
00031 #define ENABLE_FAULTS
00032 
00033 // use this define to enable CPPUNIT asserts and fails, showing bugs
00034 #define ENABLE_FORCED_FAILURES
00035 
00036 using namespace std;
00037 using namespace ParaMEDMEM;
00038 
00039 void MPIAccessDECTest::test_AllToAllDECSynchronousPointToPoint() {
00040   test_AllToAllDEC( false ) ;
00041 }
00042 void MPIAccessDECTest::test_AllToAllDECAsynchronousPointToPoint() {
00043   test_AllToAllDEC( true ) ;
00044 }
00045 
00046 static void chksts( int sts , int myrank , ParaMEDMEM::MPIAccess mpi_access ) {
00047   char msgerr[MPI_MAX_ERROR_STRING] ;
00048   int lenerr ;
00049   if ( sts != MPI_SUCCESS ) {
00050     mpi_access.errorString(sts, msgerr, &lenerr) ;
00051     cout << "test" << myrank << " lenerr " << lenerr << " "
00052          << msgerr << endl ;
00053     ostringstream strstream ;
00054     strstream << "===========================================================" << endl
00055               << "test_AllToAllDEC" << myrank << " KO" << endl
00056               << "==========================================================="
00057               << endl ;
00058     cout << strstream.str() << endl ;
00059     CPPUNIT_FAIL( strstream.str() ) ;
00060   }
00061   return ;
00062 }
00063 
00064 void MPIAccessDECTest::test_AllToAllDEC( bool Asynchronous ) {
00065 
00066   cout << "test_AllToAllDEC" << endl ;
00067 
00068   //  MPI_Init(&argc, &argv) ; 
00069 
00070   int size ;
00071   int myrank ;
00072   MPI_Comm_size(MPI_COMM_WORLD,&size) ;
00073   MPI_Comm_rank(MPI_COMM_WORLD,&myrank) ;
00074 
00075   if ( size < 2 || size > 11 ) {
00076     ostringstream strstream ;
00077     strstream << "usage :" << endl
00078               << "mpirun -np <nbprocs> test_AllToAllDEC" << endl
00079               << " (nbprocs >=2)" << endl
00080               << "test must be runned with more than 1 proc and less than 12 procs"
00081               << endl ;
00082     cout << strstream.str() << endl ;
00083     CPPUNIT_FAIL( strstream.str() ) ;
00084   }
00085 
00086   cout << "test_AllToAllDEC" << myrank << endl ;
00087 
00088   ParaMEDMEM::CommInterface interface ;
00089   std::set<int> sourceprocs;
00090   std::set<int> targetprocs;
00091   int i ;
00092   for ( i = 0 ; i < size/2 ; i++ ) {
00093     sourceprocs.insert(i);
00094   }
00095   for ( i = size/2 ; i < size ; i++ ) {
00096     targetprocs.insert(i);
00097   }
00098 
00099   ParaMEDMEM::MPIProcessorGroup* sourcegroup = new ParaMEDMEM::MPIProcessorGroup(interface,sourceprocs) ;
00100   ParaMEDMEM::MPIProcessorGroup* targetgroup = new ParaMEDMEM::MPIProcessorGroup(interface,targetprocs) ;
00101 
00102   MPIAccessDEC * MyMPIAccessDEC = new MPIAccessDEC( *sourcegroup , *targetgroup ,
00103                                                     Asynchronous ) ;
00104   
00105   MPIAccess * mpi_access = MyMPIAccessDEC->getMPIAccess() ;
00106 
00107 #define maxreq 100
00108 #define datamsglength 10
00109 
00110   //  int sts ;
00111   int sendcount = datamsglength ;
00112   int recvcount = datamsglength ;
00113   int * recvbuf = new int[datamsglength*size] ;
00114 
00115   int ireq ;
00116   for ( ireq = 0 ; ireq < maxreq ; ireq++ ) {
00117     int * sendbuf = new int[datamsglength*size] ;
00118     int j ;
00119     for ( j = 0 ; j < datamsglength*size ; j++ ) {
00120       sendbuf[j] = myrank*1000000 + ireq*1000 + j ;
00121       recvbuf[j] = -1 ;
00122     }
00123 
00124     MyMPIAccessDEC->allToAll( sendbuf, sendcount , MPI_INT ,
00125                               recvbuf, recvcount , MPI_INT ) ;
00126 
00127     int nRecvReq = mpi_access->recvRequestIdsSize() ;
00128     int *ArrayOfRecvRequests = new int[nRecvReq] ;
00129     int nReq = mpi_access->recvRequestIds( nRecvReq, ArrayOfRecvRequests ) ;
00130     mpi_access->waitAll( nReq , ArrayOfRecvRequests ) ;
00131     mpi_access->deleteRequests( nReq , ArrayOfRecvRequests ) ;
00132     delete [] ArrayOfRecvRequests ;
00133   }
00134 
00135   int nSendReq = mpi_access->sendRequestIdsSize() ;
00136   cout << "test_AllToAllDEC" << myrank << " final SendRequestIds " << nSendReq << " SendRequests"
00137        << endl ;
00138   if ( nSendReq ) {
00139     int *ArrayOfSendRequests = new int[nSendReq] ;
00140     int nReq = mpi_access->sendRequestIds( nSendReq, ArrayOfSendRequests ) ;
00141     mpi_access->waitAll( nReq , ArrayOfSendRequests ) ;
00142     delete [] ArrayOfSendRequests ;
00143   }
00144 
00145   int nRecvReq = mpi_access->recvRequestIdsSize() ;
00146   if ( nRecvReq ) {
00147     ostringstream strstream ;
00148     strstream << "test_AllToAllDEC" << myrank << " final RecvRequestIds " << nRecvReq
00149               << " RecvRequests # 0 Error" << endl ;
00150     cout << strstream.str() << endl ;
00151     CPPUNIT_FAIL( strstream.str() ) ;
00152   }
00153   else {
00154     cout << "test_AllToAllDEC" << myrank << " final RecvRequestIds " << nRecvReq
00155          << " RecvRequests = 0 OK" << endl ;
00156   }
00157 
00158   mpi_access->barrier() ;
00159 
00160   delete sourcegroup ;
00161   delete targetgroup ;
00162   delete MyMPIAccessDEC ;
00163   delete [] recvbuf ;
00164 
00165   //  MPI_Finalize();
00166 
00167   cout << "test_AllToAllDEC" << myrank << " OK" << endl ;
00168 
00169   return ;
00170 }