Salome HOME
Merge from BR_V5_DEV 16Feb09
[tools/medcoupling.git] / src / ParaMEDMEM / Test / test_MPI_Access_Send_Recv.cxx
1 //  Copyright (C) 2007-2008  CEA/DEN, EDF R&D
2 //
3 //  This library is free software; you can redistribute it and/or
4 //  modify it under the terms of the GNU Lesser General Public
5 //  License as published by the Free Software Foundation; either
6 //  version 2.1 of the License.
7 //
8 //  This library is distributed in the hope that it will be useful,
9 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
10 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 //  Lesser General Public License for more details.
12 //
13 //  You should have received a copy of the GNU Lesser General Public
14 //  License along with this library; if not, write to the Free Software
15 //  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 //  See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19 #include <string>
20 #include <vector>
21 #include <map>
22 #include <iostream>
23 #include <mpi.h>
24
25 #include "MPIAccessTest.hxx"
26 #include <cppunit/TestAssert.h>
27
28 //#include "CommInterface.hxx"
29 //#include "ProcessorGroup.hxx"
30 //#include "MPIProcessorGroup.hxx"
31 #include "MPIAccess.hxx"
32
33 // use this define to enable lines, execution of which leads to Segmentation Fault
34 #define ENABLE_FAULTS
35
36 // use this define to enable CPPUNIT asserts and fails, showing bugs
37 #define ENABLE_FORCED_FAILURES
38
39 using namespace std;
40 using namespace ParaMEDMEM;
41
42 void MPIAccessTest::test_MPI_Access_Send_Recv() {
43
44   cout << "test_MPI_Access_Send_Recv" << endl ;
45
46   int size ;
47   int myrank ;
48   MPI_Comm_size(MPI_COMM_WORLD,&size) ;
49   MPI_Comm_rank(MPI_COMM_WORLD,&myrank) ;
50
51   if ( size < 2 ) {
52     cout << "test_MPI_Access_Send_Recv must be runned with 2 procs" << endl ;
53     CPPUNIT_FAIL("test_MPI_Access_Send_Recv must be runned with 2 procs") ;
54   }
55
56   cout << "test_MPI_Access_Send_Recv" << myrank << endl ;
57
58   ParaMEDMEM::CommInterface interface ;
59
60   ParaMEDMEM::MPIProcessorGroup* group = new ParaMEDMEM::MPIProcessorGroup(interface) ;
61
62   ParaMEDMEM::MPIAccess mpi_access( group ) ;
63
64   if ( myrank >= 2 ) {
65     mpi_access.barrier() ;
66     delete group ;
67     return ;
68   }
69
70   int target = 1 - myrank ;
71   int RequestId[10] ;
72   int sts ;
73   int i ;
74   for ( i = 0 ; i < 10 ; i++ ) {
75      if ( myrank == 0 ) {
76        sts = mpi_access.send(&i,1,MPI_INT,target, RequestId[i]) ;
77        cout << "test" << myrank << " Send RequestId " << RequestId[i]
78             << " tag " << mpi_access.sendMPITag(target) << endl ;
79      }
80      else {
81        int recvbuf ;
82        int outcount ;
83        sts = mpi_access.recv(&recvbuf,1,MPI_INT,target, RequestId[i],&outcount) ;
84        //int source, tag, error, outcount ;
85        //mpi_access.Status( RequestId[i], source, tag, error, outcount, true) ;
86        cout << "test" << myrank << " Recv RequestId " << RequestId[i]
87             << " tag " << mpi_access.recvMPITag(target)
88             << " outcount " << outcount << endl ;
89        if ( (outcount != 1) | (recvbuf != i) ) {
90          ostringstream strstream ;
91          strstream << "==========================================================="
92                    << "test" << myrank << " outcount " << outcount
93                    << " recvbuf " << recvbuf << " KO"
94                    << "==========================================================="
95                    << endl ;
96          cout << strstream.str() << endl ;
97          CPPUNIT_FAIL( strstream.str() ) ;
98        }
99      }
100      char msgerr[MPI_MAX_ERROR_STRING] ;
101      int lenerr ;
102      mpi_access.errorString(sts, msgerr, &lenerr) ;
103      cout << "test" << myrank << " lenerr " << lenerr << " "
104           << msgerr << endl ;
105
106      if ( sts != MPI_SUCCESS ) {
107        ostringstream strstream ;
108        strstream << "==========================================================="
109                  << "test" << myrank << " KO"
110                  << "==========================================================="
111                  << endl ;
112        CPPUNIT_FAIL( strstream.str() ) ;
113      }
114      mpi_access.check() ;
115   }
116   int flag ;
117   mpi_access.testAll(10,RequestId,flag) ;
118   if ( !flag ) {
119     ostringstream strstream ;
120     strstream << "test" << myrank << " flag " << flag << " KO" << endl ;
121     cout << strstream.str() << endl ;
122     CPPUNIT_FAIL( strstream.str() ) ;
123   }
124   mpi_access.waitAll(10,RequestId) ;
125   mpi_access.check() ;
126
127   if ( myrank == 0 ) {
128     int sendrequests[10] ;
129     int sendreqsize = mpi_access.sendRequestIds( target , 10 , sendrequests ) ;
130     if ( sendreqsize != 0 ) {
131       ostringstream strstream ;
132       strstream << "=========================================================" << endl
133                 << "test" << myrank << " sendreqsize " << sendreqsize << " KO" << endl
134                 << "=========================================================" << endl ;
135       cout << strstream.str() << endl ;
136       CPPUNIT_FAIL( strstream.str() ) ;
137     }
138   }
139   else {
140     int recvrequests[10] ;
141     int recvreqsize = mpi_access.sendRequestIds( target , 10 , recvrequests ) ;
142     if ( recvreqsize != 0 ) {
143       ostringstream strstream ;
144       strstream << "=========================================================" << endl
145                 << "test" << myrank << " recvreqsize " << recvreqsize << " KO" << endl
146                 << "=========================================================" << endl ;
147       cout << strstream.str() << endl ;
148       CPPUNIT_FAIL( strstream.str() ) ;
149     }
150   }
151
152   mpi_access.barrier() ;
153
154   delete group ;
155
156 //  MPI_Finalize();
157
158   cout << "test" << myrank << " OK" << endl ;
159
160   return ;
161 }
162
163
164
165