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