Salome HOME
Merge from V6_main_20120808 08Aug12
[modules/med.git] / src / ParaMEDMEMTest / test_MPI_Access_SendRecv.cxx
1 // Copyright (C) 2007-2012  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
20 #include <string>
21 #include <vector>
22 #include <map>
23 #include <iostream>
24 #include <mpi.h>
25
26 #include "MPIAccessTest.hxx"
27 #include <cppunit/TestAssert.h>
28
29 //#include "CommInterface.hxx"
30 //#include "ProcessorGroup.hxx"
31 //#include "MPIProcessorGroup.hxx"
32 #include "MPIAccess.hxx"
33
34 // use this define to enable lines, execution of which leads to Segmentation Fault
35 #define ENABLE_FAULTS
36
37 // use this define to enable CPPUNIT asserts and fails, showing bugs
38 #define ENABLE_FORCED_FAILURES
39
40 using namespace std;
41 using namespace ParaMEDMEM;
42
43 void MPIAccessTest::test_MPI_Access_SendRecv() {
44
45   cout << "MPIAccessTest::test_MPI_Access_SendRecv" << endl ;
46
47 //  MPI_Init(&argc, &argv) ; 
48
49   int size ;
50   int myrank ;
51   MPI_Comm_size(MPI_COMM_WORLD,&size) ;
52   MPI_Comm_rank(MPI_COMM_WORLD,&myrank) ;
53
54   if ( size < 2 ) {
55     cout << "MPIAccessTest::test_MPI_Access_SendRecv must be runned with 2 procs" << endl ;
56     CPPUNIT_FAIL("test_MPI_Access_SendRecv must be runned with 2 procs") ;
57   }
58
59   cout << "MPIAccessTest::test_MPI_Access_SendRecv" << myrank << endl ;
60
61   ParaMEDMEM::CommInterface interface ;
62
63   ParaMEDMEM::MPIProcessorGroup* group = new ParaMEDMEM::MPIProcessorGroup(interface) ;
64
65   ParaMEDMEM::MPIAccess mpi_access( group ) ;
66
67   if ( myrank >= 2 ) {
68     mpi_access.barrier() ;
69     delete group ;
70     return ;
71   }
72
73   int target = 1 - myrank ;
74   int sendRequestId[10] ;
75   int recvRequestId[10] ;
76   int sts ;
77   int i ;
78   for ( i = 0 ; i < 10 ; i++ ) {
79      int recvbuf ;
80      int outcount ;
81      if ( i & 1 ) {
82        outcount = -1 ;
83        sts = mpi_access.sendRecv(&i,1,MPI_INT,target, sendRequestId[i],
84                                  &recvbuf,1,MPI_INT,target, recvRequestId[i],
85                                  &outcount) ;
86      }
87      else {
88        sts = mpi_access.sendRecv(&i,1,MPI_INT,target, sendRequestId[i],
89                                  &recvbuf,1,MPI_INT,target, recvRequestId[i]) ;
90 //       outcount = mpi_access.MPIOutCount( recvRequestId[i] ) ;
91        outcount = 1 ;
92      }
93      cout << "test" << myrank << " Send sendRequestId " << sendRequestId[i]
94           << " tag " << mpi_access.sendMPITag(target)
95           << " recvRequestId " << recvRequestId[i]
96           << " tag " << mpi_access.recvMPITag(target)
97           << " outcount " << outcount << " MPIOutCount "
98           << mpi_access.MPIOutCount( recvRequestId[i] ) << endl ;
99      if ( (outcount != 1) | (recvbuf != i) ) {
100        ostringstream strstream ;
101        strstream << "==========================================================="
102                  << "test" << myrank << " outcount " << outcount
103                  << " recvbuf " << recvbuf << " KO"
104                  << "==========================================================="
105                  << endl ;
106        cout << strstream.str() << endl ;
107        CPPUNIT_FAIL( strstream.str() ) ;
108      }
109      char msgerr[MPI_MAX_ERROR_STRING] ;
110      int lenerr ;
111      mpi_access.errorString(sts, msgerr, &lenerr) ;
112      cout << "test" << myrank << " lenerr " << lenerr << " "
113           << msgerr << endl ;
114
115      if ( sts != MPI_SUCCESS ) {
116        ostringstream strstream ;
117        strstream << "==========================================================="
118                  << "test" << myrank << " KO"
119                  << "==========================================================="
120                  << endl ;
121        cout << strstream.str() << endl ;
122        CPPUNIT_FAIL( strstream.str() ) ;
123      }
124      mpi_access.check() ;
125   }
126
127   int flag ;
128   mpi_access.testAll(10,sendRequestId,flag) ;
129   if ( !flag ) {
130     ostringstream strstream ;
131     strstream << "test" << myrank << " flag " << flag << " KO" << endl ;
132     cout << strstream.str() << endl ;
133     CPPUNIT_FAIL( strstream.str() ) ;
134   }
135   mpi_access.waitAll(10,sendRequestId) ;
136   mpi_access.testAll(10,recvRequestId,flag) ;
137   if ( !flag ) {
138     ostringstream strstream ;
139     strstream << "test" << myrank << " flag " << flag << " KO" << endl ;
140     cout << strstream.str() << endl ;
141     CPPUNIT_FAIL( strstream.str() ) ;
142   }
143   mpi_access.waitAll(10,recvRequestId) ;
144   mpi_access.check() ;
145
146   int sendrequests[10] ;
147   int sendreqsize = mpi_access.sendRequestIds( target , 10 , sendrequests ) ;
148   if ( sendreqsize != 0 ) {
149     ostringstream strstream ;
150     strstream << "=========================================================" << endl
151               << "test" << myrank << " sendreqsize " << sendreqsize << " KO" << endl
152               << "=========================================================" << endl ;
153     cout << strstream.str() << endl ;
154     CPPUNIT_FAIL( strstream.str() ) ;
155   }
156   int recvrequests[10] ;
157   int recvreqsize = mpi_access.sendRequestIds( target , 10 , recvrequests ) ;
158   if ( recvreqsize != 0 ) {
159     ostringstream strstream ;
160     strstream << "=========================================================" << endl
161               << "test" << myrank << " recvreqsize " << recvreqsize << " KO" << endl
162               << "=========================================================" << endl ;
163     cout << strstream.str() << endl ;
164     CPPUNIT_FAIL( strstream.str() ) ;
165   }
166
167   mpi_access.barrier() ;
168
169   delete group ;
170
171 //  MPI_Finalize();
172
173   cout << "test" << myrank << " OK" << endl ;
174
175   return ;
176 }
177
178
179
180