Salome HOME
Merge from V6_main_20120808 08Aug12
[modules/med.git] / src / ParaMEDMEMTest / test_MPI_Access_ISendRecv.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_ISendRecv() {
44
45   cout << "test_MPI_Access_ISendRecv" << 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 << "test_MPI_Access_ISendRecv must be runned with 2 procs" << endl ;
56     CPPUNIT_FAIL("test_MPI_Access_ISendRecv must be runned with 2 procs") ;
57   }
58
59   cout << "test_MPI_Access_ISendRecv" << 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 sendbuf[10] ;
77   int recvbuf[10] ;
78   int sts ;
79   int i ;
80   for ( i = 0 ; i < 10 ; i++ ) {
81      sendbuf[i] = i ;
82      sts = mpi_access.ISendRecv(&sendbuf[i],1,MPI_INT,target, SendRequestId[i],
83                                 &recvbuf[i],1,MPI_INT,target, RecvRequestId[i]) ;
84      cout << "test" << myrank << " Send sendRequestId " << SendRequestId[i]
85           << " tag " << mpi_access.sendMPITag(target)
86           << " recvRequestId " << RecvRequestId[i]
87           << " tag " << mpi_access.recvMPITag(target) << endl ;
88      char msgerr[MPI_MAX_ERROR_STRING] ;
89      int lenerr ;
90      mpi_access.errorString(sts, msgerr, &lenerr) ;
91      cout << "test" << myrank << " lenerr " << lenerr
92           << " " << msgerr << endl ;
93
94      if ( sts != MPI_SUCCESS ) {
95        ostringstream strstream ;
96        strstream << "==========================================================="
97                  << "test" << myrank << " KO"
98                  << "==========================================================="
99                  << endl ;
100        cout << strstream.str() << endl ;
101        CPPUNIT_FAIL( strstream.str() ) ;
102      }
103      int j ;
104      for (j = 0 ; j <= i ; j++) {
105         int flag ;
106         if ( j < i ) {
107           cout << "test" << myrank << " " << j << " -> Test-Send("<< SendRequestId[j]
108                << ")" << endl ;
109           mpi_access.test( SendRequestId[j], flag ) ;
110           if ( flag ) {
111             int target, tag, error, outcount ;
112               mpi_access.status( SendRequestId[j], target, tag, error, outcount,
113                                  true ) ;
114               cout << "test" << myrank << " Send RequestId " << SendRequestId[j]
115                    << " target " << target << " tag " << tag << " error " << error
116                    << endl ;
117             mpi_access.deleteRequest( SendRequestId[j] ) ;
118           }
119         }
120         cout << "test" << myrank << " " << j << " -> Test-Recv("<< SendRequestId[j]
121              << ")" << endl ;
122         mpi_access.test( RecvRequestId[j], flag ) ;
123         if ( flag ) {
124           int source, tag, error, outcount ;
125           mpi_access.status( RecvRequestId[j], source, tag, error, outcount,
126                              true ) ;
127           cout << "test" << myrank << " Recv RequestId" << j << " "
128                << RecvRequestId[j] << " source " << source << " tag " << tag
129                << " error " << error << " outcount " << outcount << endl ;
130           if ( (outcount != 1) | (recvbuf[j] != j) ) {
131              ostringstream strstream ;
132              strstream << "==========================================================="
133                        << "test" << myrank << " outcount "
134                        << outcount << " recvbuf[ " << j << " ] " << recvbuf[j] << " KO"
135                        << "==========================================================="
136                        << endl ;
137             cout << strstream.str() << endl ;
138             CPPUNIT_FAIL( strstream.str() ) ;
139           }
140         }
141      }
142      mpi_access.errorString(sts, msgerr, &lenerr) ;
143      cout << "test" << myrank << " lenerr " << lenerr << " "
144           << msgerr << endl ;
145      mpi_access.check() ;
146   }
147
148   int flag ;
149   mpi_access.testAll(10,SendRequestId,flag) ;
150   mpi_access.waitAll(10,SendRequestId) ;
151   mpi_access.deleteRequests(10,SendRequestId) ;
152   mpi_access.testAll(10,SendRequestId,flag) ;
153   if ( !flag ) {
154     ostringstream strstream ;
155     strstream << "test" << myrank << " flag " << flag << " KO" << endl ;
156     cout << strstream.str() << endl ;
157     CPPUNIT_FAIL( strstream.str() ) ;
158   }
159
160   mpi_access.testAll(10,RecvRequestId,flag) ;
161   mpi_access.waitAll(10,RecvRequestId) ;
162   mpi_access.deleteRequests(10,RecvRequestId) ;
163   mpi_access.testAll(10,RecvRequestId,flag) ;
164   if ( !flag ) {
165     ostringstream strstream ;
166     strstream << "test" << myrank << " flag " << flag << " KO" << endl ;
167     cout << strstream.str() << endl ;
168     CPPUNIT_FAIL( strstream.str() ) ;
169   }
170   mpi_access.check() ;
171
172   int sendrequests[10] ;
173   int sendreqsize = mpi_access.sendRequestIds( target , 10 , sendrequests ) ;
174   if ( sendreqsize != 0 ) {
175     ostringstream strstream ;
176     strstream << "=========================================================" << endl
177               << "test" << myrank << " sendreqsize " << sendreqsize << " KO" << endl
178               << "=========================================================" << endl ;
179     cout << strstream.str() << endl ;
180     CPPUNIT_FAIL( strstream.str() ) ;
181   }
182   else {
183     cout << "=========================================================" << endl
184          << "test" << myrank << " sendreqsize " << sendreqsize << " OK" << endl
185          << "=========================================================" << endl ;
186   }
187   int recvrequests[10] ;
188   int recvreqsize = mpi_access.sendRequestIds( target , 10 , recvrequests ) ;
189   if ( recvreqsize != 0 ) {
190     ostringstream strstream ;
191     strstream << "=========================================================" << endl
192               << "test" << myrank << " recvreqsize " << recvreqsize << " KO" << endl
193               << "=========================================================" << endl ;
194     cout << strstream.str() << endl ;
195     CPPUNIT_FAIL( strstream.str() ) ;
196   }
197   else {
198     cout << "=========================================================" << endl
199          << "test" << myrank << " recvreqsize " << recvreqsize << " OK" << endl
200          << "=========================================================" << endl ;
201   }
202
203   mpi_access.barrier() ;
204
205   delete group ;
206
207 //  MPI_Finalize();
208
209   cout << "test" << myrank << " OK" << endl ;
210
211   return ;
212 }
213
214
215
216