Salome HOME
Revert "Synchronize adm files"
[modules/med.git] / src / ParaMEDMEMTest / test_MPI_Access_ISend_IRecv_Length_1.cxx
1 // Copyright (C) 2007-2014  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, or (at your option) any later version.
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_ISend_IRecv_Length_1() {
44
45   //  MPI_Init(&argc, &argv) ; 
46
47   int size ;
48   int myrank ;
49   MPI_Comm_size(MPI_COMM_WORLD,&size) ;
50   MPI_Comm_rank(MPI_COMM_WORLD,&myrank) ;
51
52   if ( size < 2 ) {
53     ostringstream strstream ;
54     strstream << "test_MPI_Access_ISend_IRecv_Length_1 must be runned with 2 procs" << endl ;
55     cout << strstream.str() << endl ;
56     CPPUNIT_FAIL( strstream.str() ) ;
57   }
58
59   cout << "test_MPI_Access_ISend_IRecv_Length_1" << 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 #define maxreq 10
68
69   if ( myrank >= 2 ) {
70     mpi_access.barrier() ;
71     delete group ;
72     return ;
73   }
74
75   int target = 1 - myrank ;
76   int SendRequestId[maxreq] ;
77   int RecvRequestId[maxreq] ;
78   int sts ;
79   int sendbuf[1000*(maxreq-1)] ;
80   int recvbuf[maxreq-1][1000*(maxreq-1)] ;
81   int maxirecv = 1 ;
82   int i ;
83   RecvRequestId[0] = -1 ;
84   for ( i = 0 ; i < 1000*(maxreq-1) ; i++ ) {
85     sendbuf[i] = i ;
86   }
87   for ( i = 0 ; i < maxreq ; i++ ) {
88     sts = MPI_SUCCESS ;
89     if ( myrank == 0 ) {
90       sts = mpi_access.ISend( sendbuf, 1000*i, MPI_INT, target, SendRequestId[i] ) ;
91       cout << "test" << myrank << " ISend RequestId " << SendRequestId[i]
92            << " tag " << mpi_access.sendMPITag(target) << endl ;
93     }
94     int j ;
95     for (j = 1 ; j <= i ; j++) {
96       int source ;
97       MPI_Datatype datatype ;
98       int outcount ;
99       int flag ;
100       if ( myrank == 0 ) {
101         mpi_access.test( SendRequestId[j], flag ) ;
102       }
103       else {
104         int MPITag ;
105         sts = mpi_access.IProbe( target , source, MPITag, datatype,
106                                  outcount, flag) ;
107         char msgerr[MPI_MAX_ERROR_STRING] ;
108         int lenerr ;
109         mpi_access.errorString(sts, msgerr, &lenerr) ;
110         cout << "test" << myrank << " IProbe lenerr " << lenerr << " "
111              << msgerr << endl ;
112         if ( sts != MPI_SUCCESS ) {
113           ostringstream strstream ;
114           strstream << "==========================================================="
115                     << "test" << myrank << " IProbe KO"
116                     << "==========================================================="
117                     << endl ;
118           cout << strstream.str() << endl ;
119           CPPUNIT_FAIL( strstream.str() ) ;
120         }
121         cout << "test" << myrank << " IProbe i/j " << i << "/" << j
122              << " MPITag " << MPITag << " datatype " << datatype
123              << " outcount " << outcount << " flag " << flag << endl ;
124       }
125       if ( flag ) {
126         if ( myrank == 0 ) {
127           int target, tag, error, outcount ;
128           mpi_access.status( SendRequestId[j], target, tag, error, outcount,
129                              true ) ;
130           cout << "test" << myrank << " Test(Send RequestId " << SendRequestId[j]
131                << ") : target " << target << " tag " << tag << " error " << error
132                << " flag " << flag << endl ;
133         }
134         else {
135           sts = mpi_access.IRecv( recvbuf[maxirecv], outcount, datatype, source,
136                                   RecvRequestId[maxirecv] ) ;
137           cout << "test" << myrank << " maxirecv " << maxirecv << " IRecv RequestId "
138                << RecvRequestId[maxirecv] << " source " << source
139                << " outcount " << outcount << " tag "
140                << mpi_access.recvMPITag(target) << endl ;
141           maxirecv = maxirecv + 1 ;
142         }
143       }
144       else if ( myrank == 1 && i == maxreq-1 && j >= maxirecv ) {
145         sts = mpi_access.IRecv( recvbuf[j], 1000*j, MPI_INT, target,
146                                 RecvRequestId[j] ) ;
147         cout << "test" << myrank << " maxirecv " << maxirecv << " IRecv RequestId "
148              << RecvRequestId[j] << " target " << target << " length " << 1000*j
149              << " tag " << mpi_access.recvMPITag(target) << endl ;
150         maxirecv = maxirecv + 1 ;
151       }
152     }
153     char msgerr[MPI_MAX_ERROR_STRING] ;
154     int lenerr ;
155     mpi_access.errorString(sts, msgerr, &lenerr) ;
156     cout << "test" << myrank << " lenerr " << lenerr << " "
157          << msgerr << endl ;
158
159     if ( sts != MPI_SUCCESS ) {
160       ostringstream strstream ;
161       strstream << "==========================================================="
162                 << endl << "test" << myrank << " KO" << endl 
163                 << "==========================================================="
164                 << endl ;
165       cout << strstream.str() << endl ;
166       CPPUNIT_FAIL( strstream.str() ) ;
167     }
168   }
169
170   mpi_access.check() ;
171   int flag ;
172   if ( myrank == 0 ) {
173     mpi_access.testAll( maxreq, SendRequestId, flag ) ;
174     cout << "test" << myrank << " TestAll SendRequest flag " << flag << endl ;
175   }
176   else {
177     int i ;
178     int source ;
179     int outcount ;
180     int flag ;
181     if ( maxirecv != maxreq ) {
182       ostringstream strstream ;
183       strstream << "==========================================================="
184                 << endl << "test" << myrank << " KO" << " maxirecv " << maxirecv
185                 << " != maxreq " << maxreq << endl 
186                 << "==========================================================="
187                 << endl ;
188       cout << strstream.str() << endl ;
189       CPPUNIT_FAIL( strstream.str() ) ;
190     }
191     while ( maxirecv > 0 ) {
192       for ( i = 1 ; i < maxreq ; i++ ) {
193         cout << "test" << myrank << " IProbe : " << endl ;
194         sts = mpi_access.test( RecvRequestId[i] , flag ) ;
195         char msgerr[MPI_MAX_ERROR_STRING] ;
196         int lenerr ;
197         mpi_access.errorString(sts, msgerr, &lenerr) ;
198         cout << "test" << myrank << " flag " << flag << " lenerr "
199              << lenerr << " " << msgerr << " maxirecv " << maxirecv << endl ;
200         if ( sts != MPI_SUCCESS ) {
201           ostringstream strstream ;
202           strstream << "==========================================================="
203                     << "test" << myrank << " KO"
204                     << "==========================================================="
205                     << endl ;
206           cout << strstream.str() << endl ;
207           CPPUNIT_FAIL( strstream.str() ) ;
208         }
209         cout << "test" << myrank << " Test flag " << flag << endl ;
210         if ( flag ) {
211           int tag, error ;
212           mpi_access.status( RecvRequestId[i] , source , tag , error ,
213                              outcount ) ;
214           if ( i != 0 ) {
215             if ( outcount != 1000*i |
216                  (recvbuf[i][outcount-1] != (outcount-1)) ) {
217               ostringstream strstream ;
218               strstream << "========================================================"
219                         << endl << "test" << myrank << " outcount " << outcount
220                         << " KO" << " i " << i
221                         << " recvbuf " << recvbuf[i][outcount-1] << endl
222                         << "========================================================"
223                         << endl ;
224               cout << strstream.str() << endl ;
225               CPPUNIT_FAIL( strstream.str() ) ;
226             }
227           }
228           else if ( outcount != 0 ) {
229             ostringstream strstream ;
230             strstream << "========================================================"
231                       << endl << "test" << myrank << " outcount " << outcount
232                       << " KO" << " i " << i << endl
233                       << "========================================================"
234                       << endl ;
235             cout << strstream.str() << endl ;
236             CPPUNIT_FAIL( strstream.str() ) ;
237           }
238           maxirecv = maxirecv - 1 ;
239         }
240       }
241     }
242     mpi_access.testAll( maxreq, RecvRequestId, flag ) ;
243     cout << "test" << myrank << " TestAll RecvRequest flag " << flag << endl ;
244   }
245   mpi_access.check() ;
246   cout << "test" << myrank << " WaitAll :" << endl ;
247   if ( myrank == 0 ) {
248     mpi_access.waitAll( maxreq, SendRequestId ) ;
249     mpi_access.deleteRequests( maxreq, SendRequestId ) ;
250   }
251   else {
252     mpi_access.waitAll( maxreq, RecvRequestId ) ;
253     mpi_access.deleteRequests( maxreq, RecvRequestId ) ;
254   }
255
256   if ( myrank == 0 ) {
257     int sendrequests[maxreq] ;
258     int sendreqsize = mpi_access.sendRequestIds( target , maxreq , sendrequests ) ;
259     sendreqsize = mpi_access.sendRequestIds( target , maxreq , sendrequests ) ;
260     if ( sendreqsize != 0 ) {
261       ostringstream strstream ;
262       strstream << "=========================================================" << endl
263                 << "test" << myrank << " sendreqsize " << sendreqsize << " KO" << endl
264                 << "=========================================================" << endl ;
265       cout << strstream.str() << endl ;
266       CPPUNIT_FAIL( strstream.str() ) ;
267     }
268     else {
269       cout << "=========================================================" << endl
270            << "test" << myrank << " sendreqsize " << sendreqsize << " OK" << endl
271            << "=========================================================" << endl ;
272     }
273   }
274   else {
275     int recvrequests[maxreq] ;
276     int recvreqsize = mpi_access.sendRequestIds( target , maxreq , recvrequests ) ;
277     if ( recvreqsize != 0 ) {
278       ostringstream strstream ;
279       strstream << "=========================================================" << endl
280                 << "test" << myrank << " recvreqsize " << recvreqsize << " KO" << endl
281                 << "=========================================================" << endl ;
282       cout << strstream.str() << endl ;
283       CPPUNIT_FAIL( strstream.str() ) ;
284     }
285     else {
286       cout << "=========================================================" << endl
287            << "test" << myrank << " recvreqsize " << recvreqsize << " OK" << endl
288            << "=========================================================" << endl ;
289     }
290   }
291
292   mpi_access.barrier() ;
293
294   delete group ;
295
296   //  MPI_Finalize();
297
298   cout << "test" << myrank << " OK" << endl ;
299
300   return ;
301 }
302
303
304
305