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